Fossil SCM

Refactored th1 function registration code to be reusable across modules.

stephan 2012-07-14 13:43 th1-query-api
Commit e923b4a64dd2d0b0c9d5df5a364feb225ad6746f
3 files changed +19 -1 +2 -16 +53 -45
+19 -1
--- src/th.h
+++ src/th.h
@@ -182,11 +182,11 @@
182182
183183
/*
184184
** Interfaces to register the language extensions.
185185
*/
186186
int th_register_language(Th_Interp *interp); /* th_lang.c */
187
-int th_register_sqlite(Th_Interp *interp); /* th_sqlite.c */
187
+int th_register_sqlite(Th_Interp *interp); /* th_main.c */
188188
int th_register_vfs(Th_Interp *interp); /* th_vfs.c */
189189
int th_register_testvfs(Th_Interp *interp); /* th_testvfs.c */
190190
int th_register_tcl(Th_Interp *interp, void *pContext); /* th_tcl.c */
191191
192192
/*
@@ -238,10 +238,28 @@
238238
** pState (which must be NULL or a (FILE*)) or stdout (if pState is
239239
** NULL).
240240
*/
241241
int Th_output_f_FILE( char const * zData, int len, void * pState );
242242
243
+typedef struct Th_Command_Reg Th_Command_Reg;
244
+/*
245
+** A helper type for holding lists of function registration information.
246
+** For use with Th_register_commands().
247
+*/
248
+struct Th_Command_Reg {
249
+ const char *zName; /* Function name. */
250
+ Th_CommandProc xProc; /* Callback function */
251
+ void *pContext; /* Arbitrary data for the callback. */
252
+};
253
+
254
+/*
255
+** Registers a list of commands with the interpreter. pList must be a non-NULL
256
+** pointer to an array of Th_Command_Reg objects, the last one of which MUST
257
+** have a NULL zName field (that is the end-of-list marker).
258
+** Returns TH_OK on success, "something else" on error.
259
+*/
260
+int Th_register_commands( Th_Interp * interp, Th_Command_Reg const * pList );
243261
244262
#ifdef TH_USE_SQLITE
245263
#include "stddef.h" /* size_t */
246264
extern void *fossil_realloc(void *p, size_t n);
247265
248266
--- src/th.h
+++ src/th.h
@@ -182,11 +182,11 @@
182
183 /*
184 ** Interfaces to register the language extensions.
185 */
186 int th_register_language(Th_Interp *interp); /* th_lang.c */
187 int th_register_sqlite(Th_Interp *interp); /* th_sqlite.c */
188 int th_register_vfs(Th_Interp *interp); /* th_vfs.c */
189 int th_register_testvfs(Th_Interp *interp); /* th_testvfs.c */
190 int th_register_tcl(Th_Interp *interp, void *pContext); /* th_tcl.c */
191
192 /*
@@ -238,10 +238,28 @@
238 ** pState (which must be NULL or a (FILE*)) or stdout (if pState is
239 ** NULL).
240 */
241 int Th_output_f_FILE( char const * zData, int len, void * pState );
242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
244 #ifdef TH_USE_SQLITE
245 #include "stddef.h" /* size_t */
246 extern void *fossil_realloc(void *p, size_t n);
247
248
--- src/th.h
+++ src/th.h
@@ -182,11 +182,11 @@
182
183 /*
184 ** Interfaces to register the language extensions.
185 */
186 int th_register_language(Th_Interp *interp); /* th_lang.c */
187 int th_register_sqlite(Th_Interp *interp); /* th_main.c */
188 int th_register_vfs(Th_Interp *interp); /* th_vfs.c */
189 int th_register_testvfs(Th_Interp *interp); /* th_testvfs.c */
190 int th_register_tcl(Th_Interp *interp, void *pContext); /* th_tcl.c */
191
192 /*
@@ -238,10 +238,28 @@
238 ** pState (which must be NULL or a (FILE*)) or stdout (if pState is
239 ** NULL).
240 */
241 int Th_output_f_FILE( char const * zData, int len, void * pState );
242
243 typedef struct Th_Command_Reg Th_Command_Reg;
244 /*
245 ** A helper type for holding lists of function registration information.
246 ** For use with Th_register_commands().
247 */
248 struct Th_Command_Reg {
249 const char *zName; /* Function name. */
250 Th_CommandProc xProc; /* Callback function */
251 void *pContext; /* Arbitrary data for the callback. */
252 };
253
254 /*
255 ** Registers a list of commands with the interpreter. pList must be a non-NULL
256 ** pointer to an array of Th_Command_Reg objects, the last one of which MUST
257 ** have a NULL zName field (that is the end-of-list marker).
258 ** Returns TH_OK on success, "something else" on error.
259 */
260 int Th_register_commands( Th_Interp * interp, Th_Command_Reg const * pList );
261
262 #ifdef TH_USE_SQLITE
263 #include "stddef.h" /* size_t */
264 extern void *fossil_realloc(void *p, size_t n);
265
266
+2 -16
--- src/th_lang.c
+++ src/th_lang.c
@@ -1038,15 +1038,11 @@
10381038
** Register the built-in th1 language commands with interpreter interp.
10391039
** Usually this is called soon after interpreter creation.
10401040
*/
10411041
int th_register_language(Th_Interp *interp){
10421042
/* Array of built-in commands. */
1043
- struct _Command {
1044
- const char *zName;
1045
- Th_CommandProc xProc;
1046
- void *pContext;
1047
- } aCommand[] = {
1043
+ struct Th_Command_Reg aCommand[] = {
10481044
{"catch", catch_command, 0},
10491045
{"expr", expr_command, 0},
10501046
{"for", for_command, 0},
10511047
{"if", if_command, 0},
10521048
{"info", info_command, 0},
@@ -1068,17 +1064,7 @@
10681064
{"continue", simple_command, (void *)TH_CONTINUE},
10691065
{"error", simple_command, (void *)TH_ERROR},
10701066
10711067
{0, 0, 0}
10721068
};
1073
- int i;
1074
-
1075
- /* Add the language commands. */
1076
- for(i=0; i<(sizeof(aCommand)/sizeof(aCommand[0])); i++){
1077
- void *ctx;
1078
- if ( !aCommand[i].zName || !aCommand[i].xProc ) continue;
1079
- ctx = aCommand[i].pContext;
1080
- Th_CreateCommand(interp, aCommand[i].zName, aCommand[i].xProc, ctx, 0);
1081
- }
1082
-
1083
- return TH_OK;
1069
+ return Th_register_commands(interp, aCommand);
10841070
}
10851071
--- src/th_lang.c
+++ src/th_lang.c
@@ -1038,15 +1038,11 @@
1038 ** Register the built-in th1 language commands with interpreter interp.
1039 ** Usually this is called soon after interpreter creation.
1040 */
1041 int th_register_language(Th_Interp *interp){
1042 /* Array of built-in commands. */
1043 struct _Command {
1044 const char *zName;
1045 Th_CommandProc xProc;
1046 void *pContext;
1047 } aCommand[] = {
1048 {"catch", catch_command, 0},
1049 {"expr", expr_command, 0},
1050 {"for", for_command, 0},
1051 {"if", if_command, 0},
1052 {"info", info_command, 0},
@@ -1068,17 +1064,7 @@
1068 {"continue", simple_command, (void *)TH_CONTINUE},
1069 {"error", simple_command, (void *)TH_ERROR},
1070
1071 {0, 0, 0}
1072 };
1073 int i;
1074
1075 /* Add the language commands. */
1076 for(i=0; i<(sizeof(aCommand)/sizeof(aCommand[0])); i++){
1077 void *ctx;
1078 if ( !aCommand[i].zName || !aCommand[i].xProc ) continue;
1079 ctx = aCommand[i].pContext;
1080 Th_CreateCommand(interp, aCommand[i].zName, aCommand[i].xProc, ctx, 0);
1081 }
1082
1083 return TH_OK;
1084 }
1085
--- src/th_lang.c
+++ src/th_lang.c
@@ -1038,15 +1038,11 @@
1038 ** Register the built-in th1 language commands with interpreter interp.
1039 ** Usually this is called soon after interpreter creation.
1040 */
1041 int th_register_language(Th_Interp *interp){
1042 /* Array of built-in commands. */
1043 struct Th_Command_Reg aCommand[] = {
 
 
 
 
1044 {"catch", catch_command, 0},
1045 {"expr", expr_command, 0},
1046 {"for", for_command, 0},
1047 {"if", if_command, 0},
1048 {"info", info_command, 0},
@@ -1068,17 +1064,7 @@
1064 {"continue", simple_command, (void *)TH_CONTINUE},
1065 {"error", simple_command, (void *)TH_ERROR},
1066
1067 {0, 0, 0}
1068 };
1069 return Th_register_commands(interp, aCommand);
 
 
 
 
 
 
 
 
 
 
1070 }
1071
+53 -45
--- src/th_main.c
+++ src/th_main.c
@@ -1296,15 +1296,64 @@
12961296
return queryReportDbErr( interp );
12971297
}
12981298
Th_SetResultInt( interp, 0 );
12991299
return TH_OK;
13001300
}
1301
+
1302
+int th_register_sqlite(Th_Interp *interp){
1303
+ enum { BufLen = 100 };
1304
+ char buf[BufLen];
1305
+ int i, l;
1306
+#define SET(K) l = snprintf(buf, BufLen, "%d", K); \
1307
+ Th_SetVar( interp, #K, strlen(#K), buf, l );
1308
+ SET(SQLITE_BLOB);
1309
+ SET(SQLITE_DONE);
1310
+ SET(SQLITE_ERROR);
1311
+ SET(SQLITE_FLOAT);
1312
+ SET(SQLITE_INTEGER);
1313
+ SET(SQLITE_NULL);
1314
+ SET(SQLITE_OK);
1315
+ SET(SQLITE_ROW);
1316
+ SET(SQLITE_TEXT);
1317
+#undef SET
1318
+ static Th_Command_Reg aCommand[] = {
1319
+ {"query_bind_int", queryBindIntCmd, 0},
1320
+ {"query_bind_double", queryBindDoubleCmd,0},
1321
+ {"query_bind_null", queryBindNullCmd, 0},
1322
+ {"query_bind_string", queryBindStringCmd,0},
1323
+ {"query_col_count", queryColCountCmd, 0},
1324
+ {"query_col_double", queryColDoubleCmd, 0},
1325
+ {"query_col_int", queryColIntCmd, 0},
1326
+ {"query_col_is_null", queryColIsNullCmd, 0},
1327
+ {"query_col_name", queryColNameCmd, 0},
1328
+ {"query_col_string", queryColStringCmd, 0},
1329
+ {"query_col_type", queryColTypeCmd, 0},
1330
+ {"query_finalize", queryFinalizeCmd, 0},
1331
+ {"query_prepare", queryPrepareCmd, 0},
1332
+ {"query_step", queryStepCmd, 0},
1333
+ {0, 0, 0}
1334
+ };
1335
+ Th_register_commands( interp, aCommand );
1336
+}
13011337
13021338
#endif
13031339
/* end TH_USE_SQLITE */
13041340
1305
-
1341
+int Th_register_commands( Th_Interp * interp,
1342
+ Th_Command_Reg const * aCommand ){
1343
+ int i;
1344
+ int rc = TH_OK;
1345
+ for(i=0; (TH_OK==rc) && aCommand[i].zName; ++i){
1346
+ if ( !aCommand[i].zName ) break;
1347
+ else if( !aCommand[i].xProc ) continue;
1348
+ else{
1349
+ rc = Th_CreateCommand(interp, aCommand[i].zName, aCommand[i].xProc,
1350
+ aCommand[i].pContext, 0);
1351
+ }
1352
+ }
1353
+ return rc;
1354
+}
13061355
13071356
/*
13081357
** Make sure the interpreter has been initialized. Initialize it if
13091358
** it has not been already.
13101359
**
@@ -1311,15 +1360,11 @@
13111360
** The interpreter is stored in the g.interp global variable.
13121361
*/
13131362
void Th_FossilInit(void){
13141363
static PutsCmdData puts_Html = {0, 0, 0};
13151364
static PutsCmdData puts_Normal = {1, 0, 0};
1316
- static struct _Command {
1317
- const char *zName;
1318
- Th_CommandProc xProc;
1319
- void *pContext;
1320
- } aCommand[] = {
1365
+ static Th_Command_Reg aCommand[] = {
13211366
{"anycap", anycapCmd, 0},
13221367
{"combobox", comboboxCmd, 0},
13231368
{"enable_output", enableOutputCmd, 0},
13241369
{"linecount", linecntCmd, 0},
13251370
{"hascap", hascapCmd, 0},
@@ -1337,27 +1382,10 @@
13371382
{"argv_getstr", argvFindOptionStringCmd, 0},
13381383
{"argv_getbool", argvFindOptionBoolCmd, 0},
13391384
{"argv_getint", argvFindOptionIntCmd, 0},
13401385
#endif
13411386
1342
-#ifdef TH_USE_SQLITE
1343
- {"query_bind_int", queryBindIntCmd, 0},
1344
- {"query_bind_double", queryBindDoubleCmd,0},
1345
- {"query_bind_null", queryBindNullCmd, 0},
1346
- {"query_bind_string", queryBindStringCmd,0},
1347
- {"query_col_count", queryColCountCmd, 0},
1348
- {"query_col_double", queryColDoubleCmd, 0},
1349
- {"query_col_int", queryColIntCmd, 0},
1350
- {"query_col_is_null", queryColIsNullCmd, 0},
1351
- {"query_col_name", queryColNameCmd, 0},
1352
- {"query_col_string", queryColStringCmd, 0},
1353
- {"query_col_type", queryColTypeCmd, 0},
1354
- {"query_finalize", queryFinalizeCmd, 0},
1355
- {"query_prepare", queryPrepareCmd, 0},
1356
- {"query_step", queryStepCmd, 0},
1357
-#endif
1358
-
13591387
{0, 0, 0}
13601388
};
13611389
if( g.interp==0 ){
13621390
int i;
13631391
if(g.cgiOutput){
@@ -1372,34 +1400,14 @@
13721400
#ifdef FOSSIL_ENABLE_TCL
13731401
if( getenv("TH1_ENABLE_TCL")!=0 || db_get_boolean("tcl", 0) ){
13741402
th_register_tcl(g.interp, &g.tcl); /* Tcl integration commands. */
13751403
}
13761404
#endif
1377
- for(i=0; i<sizeof(aCommand)/sizeof(aCommand[0]); i++){
1378
- if ( !aCommand[i].zName || !aCommand[i].xProc ) continue;
1379
- Th_CreateCommand(g.interp, aCommand[i].zName, aCommand[i].xProc,
1380
- aCommand[i].pContext, 0);
1381
- }
13821405
#ifdef TH_USE_SQLITE
1383
- {
1384
- enum { BufLen = 100 };
1385
- char buf[BufLen];
1386
- int i, l;
1387
-#define SET(K) l = snprintf(buf, BufLen, "%d", K); \
1388
- Th_SetVar( g.interp, #K, strlen(#K), buf, l );
1389
- SET(SQLITE_BLOB);
1390
- SET(SQLITE_DONE);
1391
- SET(SQLITE_ERROR);
1392
- SET(SQLITE_FLOAT);
1393
- SET(SQLITE_INTEGER);
1394
- SET(SQLITE_NULL);
1395
- SET(SQLITE_OK);
1396
- SET(SQLITE_ROW);
1397
- SET(SQLITE_TEXT);
1398
-#undef SET
1399
- }
1406
+ th_register_sqlite(g.interp);
14001407
#endif
1408
+ Th_register_commands( g.interp, aCommand );
14011409
}
14021410
}
14031411
14041412
/*
14051413
** Store a string value in a variable in the interpreter.
14061414
--- src/th_main.c
+++ src/th_main.c
@@ -1296,15 +1296,64 @@
1296 return queryReportDbErr( interp );
1297 }
1298 Th_SetResultInt( interp, 0 );
1299 return TH_OK;
1300 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1301
1302 #endif
1303 /* end TH_USE_SQLITE */
1304
1305
 
 
 
 
 
 
 
 
 
 
 
 
 
1306
1307 /*
1308 ** Make sure the interpreter has been initialized. Initialize it if
1309 ** it has not been already.
1310 **
@@ -1311,15 +1360,11 @@
1311 ** The interpreter is stored in the g.interp global variable.
1312 */
1313 void Th_FossilInit(void){
1314 static PutsCmdData puts_Html = {0, 0, 0};
1315 static PutsCmdData puts_Normal = {1, 0, 0};
1316 static struct _Command {
1317 const char *zName;
1318 Th_CommandProc xProc;
1319 void *pContext;
1320 } aCommand[] = {
1321 {"anycap", anycapCmd, 0},
1322 {"combobox", comboboxCmd, 0},
1323 {"enable_output", enableOutputCmd, 0},
1324 {"linecount", linecntCmd, 0},
1325 {"hascap", hascapCmd, 0},
@@ -1337,27 +1382,10 @@
1337 {"argv_getstr", argvFindOptionStringCmd, 0},
1338 {"argv_getbool", argvFindOptionBoolCmd, 0},
1339 {"argv_getint", argvFindOptionIntCmd, 0},
1340 #endif
1341
1342 #ifdef TH_USE_SQLITE
1343 {"query_bind_int", queryBindIntCmd, 0},
1344 {"query_bind_double", queryBindDoubleCmd,0},
1345 {"query_bind_null", queryBindNullCmd, 0},
1346 {"query_bind_string", queryBindStringCmd,0},
1347 {"query_col_count", queryColCountCmd, 0},
1348 {"query_col_double", queryColDoubleCmd, 0},
1349 {"query_col_int", queryColIntCmd, 0},
1350 {"query_col_is_null", queryColIsNullCmd, 0},
1351 {"query_col_name", queryColNameCmd, 0},
1352 {"query_col_string", queryColStringCmd, 0},
1353 {"query_col_type", queryColTypeCmd, 0},
1354 {"query_finalize", queryFinalizeCmd, 0},
1355 {"query_prepare", queryPrepareCmd, 0},
1356 {"query_step", queryStepCmd, 0},
1357 #endif
1358
1359 {0, 0, 0}
1360 };
1361 if( g.interp==0 ){
1362 int i;
1363 if(g.cgiOutput){
@@ -1372,34 +1400,14 @@
1372 #ifdef FOSSIL_ENABLE_TCL
1373 if( getenv("TH1_ENABLE_TCL")!=0 || db_get_boolean("tcl", 0) ){
1374 th_register_tcl(g.interp, &g.tcl); /* Tcl integration commands. */
1375 }
1376 #endif
1377 for(i=0; i<sizeof(aCommand)/sizeof(aCommand[0]); i++){
1378 if ( !aCommand[i].zName || !aCommand[i].xProc ) continue;
1379 Th_CreateCommand(g.interp, aCommand[i].zName, aCommand[i].xProc,
1380 aCommand[i].pContext, 0);
1381 }
1382 #ifdef TH_USE_SQLITE
1383 {
1384 enum { BufLen = 100 };
1385 char buf[BufLen];
1386 int i, l;
1387 #define SET(K) l = snprintf(buf, BufLen, "%d", K); \
1388 Th_SetVar( g.interp, #K, strlen(#K), buf, l );
1389 SET(SQLITE_BLOB);
1390 SET(SQLITE_DONE);
1391 SET(SQLITE_ERROR);
1392 SET(SQLITE_FLOAT);
1393 SET(SQLITE_INTEGER);
1394 SET(SQLITE_NULL);
1395 SET(SQLITE_OK);
1396 SET(SQLITE_ROW);
1397 SET(SQLITE_TEXT);
1398 #undef SET
1399 }
1400 #endif
 
1401 }
1402 }
1403
1404 /*
1405 ** Store a string value in a variable in the interpreter.
1406
--- src/th_main.c
+++ src/th_main.c
@@ -1296,15 +1296,64 @@
1296 return queryReportDbErr( interp );
1297 }
1298 Th_SetResultInt( interp, 0 );
1299 return TH_OK;
1300 }
1301
1302 int th_register_sqlite(Th_Interp *interp){
1303 enum { BufLen = 100 };
1304 char buf[BufLen];
1305 int i, l;
1306 #define SET(K) l = snprintf(buf, BufLen, "%d", K); \
1307 Th_SetVar( interp, #K, strlen(#K), buf, l );
1308 SET(SQLITE_BLOB);
1309 SET(SQLITE_DONE);
1310 SET(SQLITE_ERROR);
1311 SET(SQLITE_FLOAT);
1312 SET(SQLITE_INTEGER);
1313 SET(SQLITE_NULL);
1314 SET(SQLITE_OK);
1315 SET(SQLITE_ROW);
1316 SET(SQLITE_TEXT);
1317 #undef SET
1318 static Th_Command_Reg aCommand[] = {
1319 {"query_bind_int", queryBindIntCmd, 0},
1320 {"query_bind_double", queryBindDoubleCmd,0},
1321 {"query_bind_null", queryBindNullCmd, 0},
1322 {"query_bind_string", queryBindStringCmd,0},
1323 {"query_col_count", queryColCountCmd, 0},
1324 {"query_col_double", queryColDoubleCmd, 0},
1325 {"query_col_int", queryColIntCmd, 0},
1326 {"query_col_is_null", queryColIsNullCmd, 0},
1327 {"query_col_name", queryColNameCmd, 0},
1328 {"query_col_string", queryColStringCmd, 0},
1329 {"query_col_type", queryColTypeCmd, 0},
1330 {"query_finalize", queryFinalizeCmd, 0},
1331 {"query_prepare", queryPrepareCmd, 0},
1332 {"query_step", queryStepCmd, 0},
1333 {0, 0, 0}
1334 };
1335 Th_register_commands( interp, aCommand );
1336 }
1337
1338 #endif
1339 /* end TH_USE_SQLITE */
1340
1341 int Th_register_commands( Th_Interp * interp,
1342 Th_Command_Reg const * aCommand ){
1343 int i;
1344 int rc = TH_OK;
1345 for(i=0; (TH_OK==rc) && aCommand[i].zName; ++i){
1346 if ( !aCommand[i].zName ) break;
1347 else if( !aCommand[i].xProc ) continue;
1348 else{
1349 rc = Th_CreateCommand(interp, aCommand[i].zName, aCommand[i].xProc,
1350 aCommand[i].pContext, 0);
1351 }
1352 }
1353 return rc;
1354 }
1355
1356 /*
1357 ** Make sure the interpreter has been initialized. Initialize it if
1358 ** it has not been already.
1359 **
@@ -1311,15 +1360,11 @@
1360 ** The interpreter is stored in the g.interp global variable.
1361 */
1362 void Th_FossilInit(void){
1363 static PutsCmdData puts_Html = {0, 0, 0};
1364 static PutsCmdData puts_Normal = {1, 0, 0};
1365 static Th_Command_Reg aCommand[] = {
 
 
 
 
1366 {"anycap", anycapCmd, 0},
1367 {"combobox", comboboxCmd, 0},
1368 {"enable_output", enableOutputCmd, 0},
1369 {"linecount", linecntCmd, 0},
1370 {"hascap", hascapCmd, 0},
@@ -1337,27 +1382,10 @@
1382 {"argv_getstr", argvFindOptionStringCmd, 0},
1383 {"argv_getbool", argvFindOptionBoolCmd, 0},
1384 {"argv_getint", argvFindOptionIntCmd, 0},
1385 #endif
1386
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1387 {0, 0, 0}
1388 };
1389 if( g.interp==0 ){
1390 int i;
1391 if(g.cgiOutput){
@@ -1372,34 +1400,14 @@
1400 #ifdef FOSSIL_ENABLE_TCL
1401 if( getenv("TH1_ENABLE_TCL")!=0 || db_get_boolean("tcl", 0) ){
1402 th_register_tcl(g.interp, &g.tcl); /* Tcl integration commands. */
1403 }
1404 #endif
 
 
 
 
 
1405 #ifdef TH_USE_SQLITE
1406 th_register_sqlite(g.interp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1407 #endif
1408 Th_register_commands( g.interp, aCommand );
1409 }
1410 }
1411
1412 /*
1413 ** Store a string value in a variable in the interpreter.
1414

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button