Check-in [3d243a5681]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:3d243a5681fb177e124adbdc5e729248b1cff76c
Date: 2012-07-15 13:14:37
User: stephan
Comment:Minor cleanups. Made Th_Ob_Man opaque. Renamed TH_USE_xxx to TH_ENABLE_xxx.
Tags And Properties
Changes

Changes to src/th.c

4 ** the implementation of the interface in th.h. 4 ** the implementation of the interface in th.h. 5 */ 5 */ 6 6 7 #include "th.h" 7 #include "th.h" 8 #include <string.h> 8 #include <string.h> 9 #include <assert.h> 9 #include <assert.h> 10 #include <stdio.h> /* FILE class */ 10 #include <stdio.h> /* FILE class */ > 11 11 #ifdef TH_USE_OUTBUF | 12 #ifdef TH_ENABLE_OUTBUF > 13 struct Th_Ob_Man { > 14 Blob ** aBuf; /* Stack of Blobs */ > 15 int nBuf; /* Number of blobs */ > 16 int cursor; /* Current level (-1=not active) */ > 17 Th_Interp * interp; /* The associated interpreter */ > 18 Th_Vtab_Output * aOutput > 19 /* Stack of output routines corresponding > 20 to the current buffering level. > 21 Has nBuf entries. > 22 */; > 23 }; 12 #endif 24 #endif 13 25 14 extern void *fossil_realloc(void *p, size_t n); 26 extern void *fossil_realloc(void *p, size_t n); 15 static void * th_fossil_realloc(void *p, unsigned int n){ 27 static void * th_fossil_realloc(void *p, unsigned int n){ 16 return fossil_realloc( p, n ); 28 return fossil_realloc( p, n ); 17 } 29 } 18 static int Th_output_f_ob( char const * zData, int len, void * pState ); 30 static int Th_output_f_ob( char const * zData, int len, void * pState ); ................................................................................................................................................................................ 2745 ? Th_HashFind(interp, interp->paGc, key, th_strlen(key), 0) 2757 ? Th_HashFind(interp, interp->paGc, key, th_strlen(key), 0) 2746 : NULL; 2758 : NULL; 2747 return e ? ((Th_GcEntry*)e->pData)->pData : NULL; 2759 return e ? ((Th_GcEntry*)e->pData)->pData : NULL; 2748 } 2760 } 2749 2761 2750 2762 2751 2763 2752 #ifdef TH_USE_OUTBUF | 2764 #ifdef TH_ENABLE_OUTBUF 2753 /* Reminder: the ob code "really" belongs in th_lang.c, 2765 /* Reminder: the ob code "really" belongs in th_lang.c, 2754 but we need access to Th_Interp internals in order to 2766 but we need access to Th_Interp internals in order to 2755 swap out Th_Vtab parts for purposes of stacking layers 2767 swap out Th_Vtab parts for purposes of stacking layers 2756 of buffers. 2768 of buffers. 2757 */ 2769 */ 2758 #define Th_Ob_Man_empty_m { \ 2770 #define Th_Ob_Man_empty_m { \ 2759 NULL/*aBuf*/, \ 2771 NULL/*aBuf*/, \ ................................................................................................................................................................................ 2879 assert(-1 == pMan->cursor); 2891 assert(-1 == pMan->cursor); 2880 } 2892 } 2881 /*printf( "post-pop: pMan->nBuf=%d, pMan->cursor=%d\n", pMan->nBuf, pMan->cu 2893 /*printf( "post-pop: pMan->nBuf=%d, pMan->cursor=%d\n", pMan->nBuf, pMan->cu 2882 return rc; 2894 return rc; 2883 } 2895 } 2884 } 2896 } 2885 2897 2886 void Th_ob_cleanup( Th_Ob_Man * man ){ | 2898 int Th_ob_pop_free( Th_Ob_Man * pMan ){ 2887 Blob * b; | 2899 Blob * b = Th_ob_pop( pMan ); 2888 while( (b = Th_ob_pop(man)) ){ | 2900 if(!b) return 1; > 2901 else { 2889 blob_reset(b); 2902 blob_reset(b); 2890 Th_Free( man->interp, b ); | 2903 Th_Free( pMan->interp, b ); 2891 } 2904 } 2892 } 2905 } 2893 2906 > 2907 > 2908 void Th_ob_cleanup( Th_Ob_Man * man ){ > 2909 while( 0 == Th_ob_pop_free(man) ){} > 2910 } > 2911 2894 2912 2895 /* 2913 /* 2896 ** TH Syntax: 2914 ** TH Syntax: 2897 ** 2915 ** 2898 ** ob clean 2916 ** ob clean 2899 ** 2917 ** 2900 ** Erases any currently buffered contents but does not modify 2918 ** Erases any currently buffered contents but does not modify ................................................................................................................................................................................ 3141 } 3159 } 3142 return rc; 3160 return rc; 3143 } 3161 } 3144 3162 3145 #undef Th_Ob_Man_empty_m 3163 #undef Th_Ob_Man_empty_m 3146 #undef Th_Ob_Man_KEY 3164 #undef Th_Ob_Man_KEY 3147 #endif 3165 #endif 3148 /* end TH_USE_OUTBUF */ | 3166 /* end TH_ENABLE_OUTBUF */ 3149 3167

Changes to src/th.h

1 #include "config.h" 1 #include "config.h" 2 2 3 /* 3 /* 4 ** TH_USE_SQLITE, if defined, enables the "query" family of functions. | 4 ** TH_ENABLE_SQLITE, if defined, enables the "query" family of functions. 5 ** They provide SELECT-only access to the repository db. 5 ** They provide SELECT-only access to the repository db. 6 */ 6 */ 7 #define TH_USE_SQLITE | 7 #define TH_ENABLE_SQLITE 8 8 9 /* 9 /* 10 ** TH_USE_OUTBUF, if defined, enables the "ob" family of functions. | 10 ** TH_ENABLE_OUTBUF, if defined, enables the "ob" family of functions. 11 ** They are functionally similar to PHP's ob_start(), ob_end(), etc. 11 ** They are functionally similar to PHP's ob_start(), ob_end(), etc. 12 ** family of functions, providing output capturing/buffering. 12 ** family of functions, providing output capturing/buffering. 13 */ 13 */ 14 #define TH_USE_OUTBUF | 14 #define TH_ENABLE_OUTBUF 15 15 16 /* 16 /* 17 ** TH_USE_ARGV, if defined, enables the "argv" family of functions. | 17 ** TH_ENABLE_ARGV, if defined, enables the "argv" family of functions. 18 ** They provide access to CLI arguments as well as GET/POST arguments. 18 ** They provide access to CLI arguments as well as GET/POST arguments. 19 ** They do not provide access to POST data submitted in JSON mode. 19 ** They do not provide access to POST data submitted in JSON mode. 20 */ 20 */ 21 #define TH_USE_ARGV | 21 #define TH_ENABLE_ARGV 22 22 23 #ifdef TH_USE_OUTBUF | 23 #ifdef TH_ENABLE_OUTBUF 24 #ifndef INTERFACE 24 #ifndef INTERFACE 25 #include "blob.h" 25 #include "blob.h" 26 #endif 26 #endif 27 #endif 27 #endif 28 28 29 /* This header file defines the external interface to the custom Scripting 29 /* This header file defines the external interface to the custom Scripting 30 ** Language (TH) interpreter. TH is very similar to TCL but is not an 30 ** Language (TH) interpreter. TH is very similar to TCL but is not an ................................................................................................................................................................................ 198 int th_isspecial(char); 198 int th_isspecial(char); 199 char *th_strdup(Th_Interp *interp, const char *z, int n); 199 char *th_strdup(Th_Interp *interp, const char *z, int n); 200 200 201 /* 201 /* 202 ** Interfaces to register the language extensions. 202 ** Interfaces to register the language extensions. 203 */ 203 */ 204 int th_register_language(Th_Interp *interp); /* th_lang.c */ 204 int th_register_language(Th_Interp *interp); /* th_lang.c */ 205 int th_register_sqlite(Th_Interp *interp); /* th_main.c */ | 205 int th_register_query(Th_Interp *interp); /* th_main.c */ 206 int th_register_argv(Th_Interp *interp); /* th_main.c */ 206 int th_register_argv(Th_Interp *interp); /* th_main.c */ 207 int th_register_vfs(Th_Interp *interp); /* th_vfs.c */ 207 int th_register_vfs(Th_Interp *interp); /* th_vfs.c */ 208 int th_register_testvfs(Th_Interp *interp); /* th_testvfs.c */ 208 int th_register_testvfs(Th_Interp *interp); /* th_testvfs.c */ 209 int th_register_tcl(Th_Interp *interp, void *pContext); /* th_tcl.c */ 209 int th_register_tcl(Th_Interp *interp, void *pContext); /* th_tcl.c */ 210 int th_register_ob(Th_Interp * interp); /* th.c */ 210 int th_register_ob(Th_Interp * interp); /* th.c */ 211 /* 211 /* 212 ** General purpose hash table from th_lang.c. 212 ** General purpose hash table from th_lang.c. ................................................................................................................................................................................ 311 ** pointer to an array of Th_Command_Reg objects, the last one of which MUST 311 ** pointer to an array of Th_Command_Reg objects, the last one of which MUST 312 ** have a NULL zName field (that is the end-of-list marker). 312 ** have a NULL zName field (that is the end-of-list marker). 313 ** Returns TH_OK on success, "something else" on error. 313 ** Returns TH_OK on success, "something else" on error. 314 */ 314 */ 315 int Th_register_commands( Th_Interp * interp, Th_Command_Reg const * pList ); 315 int Th_register_commands( Th_Interp * interp, Th_Command_Reg const * pList ); 316 316 317 317 318 #ifdef TH_USE_OUTBUF | 318 #ifdef TH_ENABLE_OUTBUF 319 /* 319 /* 320 ** Manager of a stack of Blob objects for output buffering. 320 ** Manager of a stack of Blob objects for output buffering. 321 */ | 321 ** See Th_ob_manager(). 322 struct Th_Ob_Man { < 323 Blob ** aBuf; /* Stack of Blobs */ < 324 int nBuf; /* Number of blobs */ < 325 int cursor; /* Current level (-1=not active) */ < 326 Th_Interp * interp; /* The associated interpreter */ < 327 Th_Vtab_Output * aOutput < 328 /* Stack of output routines corresponding < 329 to the current buffering level. < 330 Has nBuf entries. < 331 */; | 322 */ 332 }; < 333 < 334 typedef struct Th_Ob_Man Th_Ob_Man; 323 typedef struct Th_Ob_Man Th_Ob_Man; 335 324 336 /* 325 /* 337 ** Returns the ob manager for the given interpreter. | 326 ** Returns the ob manager for the given interpreter. The manager gets > 327 ** installed by the th_register_ob(). In Fossil ob support is > 328 ** installed automatically if it is available at built time. 338 */ 329 */ 339 Th_Ob_Man * Th_ob_manager(Th_Interp *ignored); 330 Th_Ob_Man * Th_ob_manager(Th_Interp *ignored); 340 331 341 /* 332 /* 342 ** Returns the top-most Blob in pMan's stack, or NULL 333 ** Returns the top-most Blob in pMan's stack, or NULL 343 ** if buffering is not active. 334 ** if buffering is not active. 344 */ 335 */ ................................................................................................................................................................................ 350 ** to the new blob (which is owned by pMan). On error 341 ** to the new blob (which is owned by pMan). On error 351 ** pOut is not modified and non-0 is returned. 342 ** pOut is not modified and non-0 is returned. 352 */ 343 */ 353 int Th_ob_push( Th_Ob_Man * pMan, Blob ** pOut ); 344 int Th_ob_push( Th_Ob_Man * pMan, Blob ** pOut ); 354 345 355 /* 346 /* 356 ** Pops the top-most output buffer off the stack and returns 347 ** Pops the top-most output buffer off the stack and returns 357 ** it. Returns NULL if there is no current buffer. When the last | 348 ** it. Returns NULL if there is no current buffer. When the last 358 ** buffer is popped, pMan's internals are cleaned up. | 349 ** buffer is popped, pMan's internals are cleaned up (but pMan is not > 350 ** freed). 359 ** 351 ** 360 ** The caller owns the returned object and must eventually call | 352 ** The caller owns the returned object and must eventually clean it up 361 ** blob_reset() on it and Th_Free() it. < > 353 ** by first passing it to blob_reset() and then Th_Free() it. 362 */ 354 */ 363 Blob * Th_ob_pop( Th_Ob_Man * pMan ); 355 Blob * Th_ob_pop( Th_Ob_Man * pMan ); > 356 /* > 357 ** Convenience form of Th_ob_pop() which pops and frees the > 358 ** top-most buffer. Returns 0 on success, non-0 if there is no > 359 ** stack to pop. > 360 */ > 361 int Th_ob_pop_free( Th_Ob_Man * pMan ); 364 362 365 #endif 363 #endif 366 /* TH_USE_OUTBUF */ | 364 /* TH_ENABLE_OUTBUF */

Changes to src/th_lang.c

1053 int *argl 1053 int *argl 1054 ){ 1054 ){ 1055 int cnt = 0; 1055 int cnt = 0; 1056 cnt++; 1056 cnt++; 1057 return TH_OK; 1057 return TH_OK; 1058 } 1058 } 1059 1059 1060 static int call_command( < 1061 Th_Interp *interp, < 1062 void *ctx, < 1063 int argc, < 1064 const char **argv, < 1065 int *argl < 1066 ){ < 1067 if( argc<2 ){ < 1068 return Th_WrongNumArgs2(interp, < 1069 argv[0], argl[0], < 1070 "funcName ?args...?"); < 1071 } < 1072 < 1073 < 1074 } < 1075 < 1076 /* 1060 /* 1077 ** Register the built-in th1 language commands with interpreter interp. 1061 ** Register the built-in th1 language commands with interpreter interp. 1078 ** Usually this is called soon after interpreter creation. 1062 ** Usually this is called soon after interpreter creation. 1079 */ 1063 */ 1080 int th_register_language(Th_Interp *interp){ 1064 int th_register_language(Th_Interp *interp){ 1081 int rc; 1065 int rc; 1082 /* Array of built-in commands. */ 1066 /* Array of built-in commands. */ ................................................................................................................................................................................ 1103 {"break", simple_command, (void *)TH_BREAK}, 1087 {"break", simple_command, (void *)TH_BREAK}, 1104 {"continue", simple_command, (void *)TH_CONTINUE}, 1088 {"continue", simple_command, (void *)TH_CONTINUE}, 1105 {"error", simple_command, (void *)TH_ERROR}, 1089 {"error", simple_command, (void *)TH_ERROR}, 1106 1090 1107 {0, 0, 0} 1091 {0, 0, 0} 1108 }; 1092 }; 1109 rc = Th_register_commands(interp, aCommand); 1093 rc = Th_register_commands(interp, aCommand); 1110 #ifdef TH_USE_OUTBUF < 1111 rc |= th_register_ob(interp); < 1112 #endif < 1113 return rc; 1094 return rc; 1114 } 1095 }

Changes to src/th_main.c

19 ** (an independent project) and fossil. 19 ** (an independent project) and fossil. 20 */ 20 */ 21 #include "config.h" 21 #include "config.h" 22 #include "th_main.h" 22 #include "th_main.h" 23 #ifndef INTERFACE 23 #ifndef INTERFACE 24 #include "blob.h" 24 #include "blob.h" 25 #endif 25 #endif 26 #ifdef TH_USE_SQLITE | 26 #ifdef TH_ENABLE_SQLITE 27 #include "sqlite3.h" 27 #include "sqlite3.h" 28 #endif 28 #endif 29 29 30 /*#include "th_main.h"*/ 30 /*#include "th_main.h"*/ 31 /* 31 /* 32 ** Global variable counting the number of outstanding calls to malloc() 32 ** Global variable counting the number of outstanding calls to malloc() 33 ** made by the th1 implementation. This is used to catch memory leaks 33 ** made by the th1 implementation. This is used to catch memory leaks ................................................................................................................................................................................ 540 if( openRepository ) db_find_and_open_repository(OPEN_OK_NOT_FOUND, 0); 540 if( openRepository ) db_find_and_open_repository(OPEN_OK_NOT_FOUND, 0); 541 } 541 } 542 Th_SetResult(interp, g.zRepositoryName, -1); 542 Th_SetResult(interp, g.zRepositoryName, -1); 543 return TH_OK; 543 return TH_OK; 544 } 544 } 545 545 546 546 547 #ifdef TH_USE_ARGV | 547 #ifdef TH_ENABLE_ARGV 548 extern const char *find_option(const char *zLong, 548 extern const char *find_option(const char *zLong, 549 const char *zShort, 549 const char *zShort, 550 int hasArg) /* from main.c */; 550 int hasArg) /* from main.c */; 551 /* 551 /* 552 ** TH Syntax: 552 ** TH Syntax: 553 ** 553 ** 554 ** argv len 554 ** argv len ................................................................................................................................................................................ 835 {"argv", argvTopLevelCmd, 0 }, 835 {"argv", argvTopLevelCmd, 0 }, 836 {0, 0, 0} 836 {0, 0, 0} 837 }; 837 }; 838 Th_register_commands( interp, aCommand ); 838 Th_register_commands( interp, aCommand ); 839 } 839 } 840 840 841 #endif 841 #endif 842 /* end TH_USE_ARGV */ | 842 /* end TH_ENABLE_ARGV */ 843 843 844 #ifdef TH_USE_SQLITE | 844 #ifdef TH_ENABLE_SQLITE 845 845 846 /* 846 /* 847 ** Adds the given prepared statement to the interpreter. Returns the 847 ** Adds the given prepared statement to the interpreter. Returns the 848 ** statements opaque identifier (a positive value). Ownerships of 848 ** statements opaque identifier (a positive value). Ownerships of 849 ** pStmt is transfered to interp and it must be cleaned up by the 849 ** pStmt is transfered to interp and it must be cleaned up by the 850 ** client by calling Th_FinalizeStmt(), passing it the value returned 850 ** client by calling Th_FinalizeStmt(), passing it the value returned 851 ** by this function. 851 ** by this function. ................................................................................................................................................................................ 1672 {0, 0} 1672 {0, 0} 1673 }; 1673 }; 1674 assert( NULL != sq ); 1674 assert( NULL != sq ); 1675 Th_CallSubCommand2( interp, sq, argc, argv, argl, aSub ); 1675 Th_CallSubCommand2( interp, sq, argc, argv, argl, aSub ); 1676 } 1676 } 1677 1677 1678 1678 1679 int th_register_sqlite(Th_Interp *interp){ | 1679 int th_register_query(Th_Interp *interp){ 1680 enum { BufLen = 100 }; 1680 enum { BufLen = 100 }; 1681 char buf[BufLen]; 1681 char buf[BufLen]; 1682 int i, l; 1682 int i, l; 1683 #define SET(K) l = snprintf(buf, BufLen, "%d", K); \ 1683 #define SET(K) l = snprintf(buf, BufLen, "%d", K); \ 1684 Th_SetVar( interp, #K, strlen(#K), buf, l ); 1684 Th_SetVar( interp, #K, strlen(#K), buf, l ); 1685 SET(SQLITE_BLOB); 1685 SET(SQLITE_BLOB); 1686 SET(SQLITE_DONE); 1686 SET(SQLITE_DONE); ................................................................................................................................................................................ 1709 assert( sq == Th_sqlite_manager(interp) ); 1709 assert( sq == Th_sqlite_manager(interp) ); 1710 } 1710 } 1711 } 1711 } 1712 return rc; 1712 return rc; 1713 } 1713 } 1714 1714 1715 #endif 1715 #endif 1716 /* end TH_USE_SQLITE */ | 1716 /* end TH_ENABLE_SQLITE */ 1717 1717 1718 int Th_register_commands( Th_Interp * interp, 1718 int Th_register_commands( Th_Interp * interp, 1719 Th_Command_Reg const * aCommand ){ 1719 Th_Command_Reg const * aCommand ){ 1720 int i; 1720 int i; 1721 int rc = TH_OK; 1721 int rc = TH_OK; 1722 for(i=0; (TH_OK==rc) && aCommand[i].zName; ++i){ 1722 for(i=0; (TH_OK==rc) && aCommand[i].zName; ++i){ 1723 if ( !aCommand[i].zName ) break; 1723 if ( !aCommand[i].zName ) break; ................................................................................................................................................................................ 1770 g.interp = Th_CreateInterp(&vtab); 1770 g.interp = Th_CreateInterp(&vtab); 1771 th_register_language(g.interp); /* Basic scripting commands. */ 1771 th_register_language(g.interp); /* Basic scripting commands. */ 1772 #ifdef FOSSIL_ENABLE_TCL 1772 #ifdef FOSSIL_ENABLE_TCL 1773 if( getenv("TH1_ENABLE_TCL")!=0 || db_get_boolean("tcl", 0) ){ 1773 if( getenv("TH1_ENABLE_TCL")!=0 || db_get_boolean("tcl", 0) ){ 1774 th_register_tcl(g.interp, &g.tcl); /* Tcl integration commands. */ 1774 th_register_tcl(g.interp, &g.tcl); /* Tcl integration commands. */ 1775 } 1775 } 1776 #endif 1776 #endif 1777 #ifdef TH_USE_SQLITE | 1777 #ifdef TH_ENABLE_OUTBUF 1778 th_register_sqlite(g.interp); | 1778 th_register_ob(g.interp); 1779 #endif 1779 #endif > 1780 #ifdef TH_ENABLE_SQLITE > 1781 th_register_query(g.interp); > 1782 #endif 1780 #ifdef TH_USE_ARGV | 1783 #ifdef TH_ENABLE_ARGV 1781 th_register_argv(g.interp); 1784 th_register_argv(g.interp); 1782 #endif 1785 #endif 1783 Th_register_commands( g.interp, aCommand ); 1786 Th_register_commands( g.interp, aCommand ); 1784 Th_Eval( g.interp, 0, "proc incr {name {step 1}} {\n" 1787 Th_Eval( g.interp, 0, "proc incr {name {step 1}} {\n" 1785 "upvar $name x\n" 1788 "upvar $name x\n" 1786 "set x [expr $x+$step]\n" 1789 "set x [expr $x+$step]\n" 1787 "}", -1 ); 1790 "}", -1 ); ................................................................................................................................................................................ 1960 Blob in; 1963 Blob in; 1961 if( g.argc<3 ){ 1964 if( g.argc<3 ){ 1962 usage("FILE"); 1965 usage("FILE"); 1963 assert(0 && "usage() does not return"); 1966 assert(0 && "usage() does not return"); 1964 } 1967 } 1965 blob_zero(&in); 1968 blob_zero(&in); 1966 db_open_config(0); /* Needed for global "tcl" setting. */ 1969 db_open_config(0); /* Needed for global "tcl" setting. */ 1967 #ifdef TH_USE_SQLITE | 1970 #ifdef TH_ENABLE_SQLITE 1968 db_find_and_open_repository(OPEN_ANY_SCHEMA,0) 1971 db_find_and_open_repository(OPEN_ANY_SCHEMA,0) 1969 /* required for th1 query API. */; 1972 /* required for th1 query API. */; 1970 #endif 1973 #endif 1971 blob_read_from_file(&in, g.argv[2]); 1974 blob_read_from_file(&in, g.argv[2]); 1972 Th_Render(blob_str(&in), Th_Render_Flags_DEFAULT); 1975 Th_Render(blob_str(&in), Th_Render_Flags_DEFAULT); 1973 } 1976 }