Fossil

Check-in [1767603f]
Login

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

Overview
Comment:minor generic cleanups in th1 before continuing on to real work...
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | th1-query-api
Files: files | file ages | folders
SHA1:1767603f23ab757ac54e8a9212461ca846065a0d
User & Date: stephan 2012-07-14 13:20:39
Context
2012-07-14
13:43
Refactored th1 function registration code to be reusable across modules. check-in: e923b4a6 user: stephan tags: th1-query-api
13:20
minor generic cleanups in th1 before continuing on to real work... check-in: 1767603f user: stephan tags: th1-query-api
12:02
Refactored sendText() to use the new output abstraction layer. check-in: d3a62349 user: stephan tags: th1-query-api
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/th.h.

21
22
23
24
25
26
27







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
...
202
203
204
205
206
207
208






209
210
211
212
213
214
215
...
221
222
223
224
225
226
227
228
229

230
231
232
233
234
235
236
** implementation-specific state pointer (may be NULL, depending on
** the implementation). The return value is the number of bytes output
** (which may differ from len due to encoding and whatnot).  On error
** a negative value must be returned.
*/
typedef int (*Th_output_f)( char const * zData, int len, void * pState );








/*
** Before creating an interpreter, the application must allocate and
** populate an instance of the following structure. It must remain valid
** for the lifetime of the interpreter.
*/
struct Th_Vtab {
  void *(*xMalloc)(unsigned int);
  void (*xFree)(void *);
  struct {
    Th_output_f f;   /* output handler */
    void * pState;   /* final argument for xOut() */
    char enabled;    /* if 0, Th_output() does nothing. */
  } out;
};
typedef struct Th_Vtab Th_Vtab;


/*
** Opaque handle for interpeter.
*/
................................................................................
void Th_HashIterate(Th_Interp*,Th_Hash*,void (*x)(Th_HashEntry*, void*),void*);
Th_HashEntry *Th_HashFind(Th_Interp*, Th_Hash*, const char*, int, int);

/*
** Useful functions from th_lang.c.
*/
int Th_WrongNumArgs(Th_Interp *interp, const char *zMsg);







typedef struct Th_SubCommand {char *zName; Th_CommandProc xProc;} Th_SubCommand;
int Th_CallSubCommand(Th_Interp*,void*,int,const char**,int*,Th_SubCommand*);

/*
** Sends the given data through vTab->out.f() if vTab->out.enabled is
** true, otherwise this is a no-op. Returns 0 or higher on success, *
................................................................................
** Sends the given output through pInterp's v-table's output
** implementation. See Th_Vtab_output() for the argument and
** return value semantics.
*/
int Th_output( Th_Interp *pInterp, char const * zData, int len );

/*
** Th_output_f() implementation which sends its output to either pState
** (which must be NULL or a (FILE*)) or stdout (if pState is NULL).

*/
int Th_output_f_FILE( char const * zData, int len, void * pState );


#ifdef TH_USE_SQLITE
#include "stddef.h" /* size_t */
extern void *fossil_realloc(void *p, size_t n);







>
>
>
>
>
>
>








<
<
<
<
|







 







>
>
>
>
>
>







 







|
|
>







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42




43
44
45
46
47
48
49
50
...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
** implementation-specific state pointer (may be NULL, depending on
** the implementation). The return value is the number of bytes output
** (which may differ from len due to encoding and whatnot).  On error
** a negative value must be returned.
*/
typedef int (*Th_output_f)( char const * zData, int len, void * pState );

struct Th_Vtab_Output {
  Th_output_f f;   /* output handler */
  void * pState;   /* final argument for xOut() */
  char enabled;    /* if 0, Th_output() does nothing. */
};
typedef struct Th_Vtab_Output Th_Vtab_Output;

/*
** Before creating an interpreter, the application must allocate and
** populate an instance of the following structure. It must remain valid
** for the lifetime of the interpreter.
*/
struct Th_Vtab {
  void *(*xMalloc)(unsigned int);
  void (*xFree)(void *);




  Th_Vtab_Output out;
};
typedef struct Th_Vtab Th_Vtab;


/*
** Opaque handle for interpeter.
*/
................................................................................
void Th_HashIterate(Th_Interp*,Th_Hash*,void (*x)(Th_HashEntry*, void*),void*);
Th_HashEntry *Th_HashFind(Th_Interp*, Th_Hash*, const char*, int, int);

/*
** Useful functions from th_lang.c.
*/
int Th_WrongNumArgs(Th_Interp *interp, const char *zMsg);
/*
** Works like Th_WrongNumArgs() but expects (zCmdName,zCmdLen) to be
** the current command's (name,length), i.e. (argv[0],argl[0]).
*/
int Th_WrongNumArgs2(Th_Interp *interp, const char *zCmdName,
                     int zCmdLen, const char *zMsg);

typedef struct Th_SubCommand {char *zName; Th_CommandProc xProc;} Th_SubCommand;
int Th_CallSubCommand(Th_Interp*,void*,int,const char**,int*,Th_SubCommand*);

/*
** Sends the given data through vTab->out.f() if vTab->out.enabled is
** true, otherwise this is a no-op. Returns 0 or higher on success, *
................................................................................
** Sends the given output through pInterp's v-table's output
** implementation. See Th_Vtab_output() for the argument and
** return value semantics.
*/
int Th_output( Th_Interp *pInterp, char const * zData, int len );

/*
** Th_output_f() implementation which sends its output to either
** pState (which must be NULL or a (FILE*)) or stdout (if pState is
** NULL).
*/
int Th_output_f_FILE( char const * zData, int len, void * pState );


#ifdef TH_USE_SQLITE
#include "stddef.h" /* size_t */
extern void *fossil_realloc(void *p, size_t n);

Changes to src/th_lang.c.

14
15
16
17
18
19
20












21
22
23
24
25
26
27
#include <string.h>
#include <assert.h>

int Th_WrongNumArgs(Th_Interp *interp, const char *zMsg){
  Th_ErrorMessage(interp, "wrong # args: should be \"", zMsg, -1);
  return TH_ERROR;
}













/*
** Syntax: 
**
**   catch script ?varname?
*/
static int catch_command(







>
>
>
>
>
>
>
>
>
>
>
>







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string.h>
#include <assert.h>

int Th_WrongNumArgs(Th_Interp *interp, const char *zMsg){
  Th_ErrorMessage(interp, "wrong # args: should be \"", zMsg, -1);
  return TH_ERROR;
}

int Th_WrongNumArgs2(Th_Interp *interp, const char *zCmdName,
                     int zCmdLen, const char *zMsg){
  char * zBuf = 0;
  int nBuf = 0;
  Th_StringAppend(interp, &zBuf, &nBuf, zCmdName, zCmdLen);
  Th_StringAppend(interp, &zBuf, &nBuf, ": wrong # args: expecting: ", -1);
  Th_StringAppend(interp, &zBuf, &nBuf, zMsg, -1);
  Th_ErrorMessage(interp, zBuf, NULL, 0);
  Th_Free(interp, zBuf);
  return TH_ERROR;
}

/*
** Syntax: 
**
**   catch script ?varname?
*/
static int catch_command(

Changes to src/th_main.c.

41
42
43
44
45
46
47
48
49
50

51
52
53
54
55
56
57
..
78
79
80
81
82
83
84
85


86
87
88
89
90
91
92
...
136
137
138
139
140
141
142


143
144
145
146
147
148
149
150
...
163
164
165
166
167
168
169
170


171
172
173
174
175
176
177
...
189
190
191
192
193
194
195
196


197
198
199
200
201
202
203
...
236
237
238
239
240
241
242
243


244
245
246
247
248
249
250
...
267
268
269
270
271
272
273
274


275
276
277
278
279
280
281
...
311
312
313
314
315
316
317
318


319
320
321
322
323
324
325
...
341
342
343
344
345
346
347


348
349
350
351
352
353
354
355
...
399
400
401
402
403
404
405
406


407
408
409
410
411
412
413
...
434
435
436
437
438
439
440
441


442
443
444
445
446
447
448
...
487
488
489
490
491
492
493
494


495
496
497
498
499
500
501
...
538
539
540
541
542
543
544

545
546
547

548
549
550
551

552
553
554
555
556
557


558
559
560
561
562
563
564
565
...
591
592
593
594
595
596
597

598
599
600

601
602
603
604

605
606
607
608
609
610


611
612
613
614
615
616
617
618
...
645
646
647
648
649
650
651

652
653
654

655
656
657
658

659
660
661
662
663
664


665
666
667
668
669
670
671
672
...
695
696
697
698
699
700
701
702


703
704
705
706
707
708
709
...
766
767
768
769
770
771
772
773


774
775
776
777
778
779
780
...
812
813
814
815
816
817
818
819


820
821
822
823


824
825
826
827
828
829
830
...
852
853
854
855
856
857
858
859


860
861
862
863
864
865
866
...
892
893
894
895
896
897
898
899


900
901
902
903
904
905
906
...
922
923
924
925
926
927
928
929


930
931
932
933
934
935
936
...
950
951
952
953
954
955
956
957


958
959
960
961
962
963
964
...
979
980
981
982
983
984
985
986


987
988
989
990
991
992
993
....
1011
1012
1013
1014
1015
1016
1017
1018


1019
1020
1021
1022
1023
1024
1025
....
1038
1039
1040
1041
1042
1043
1044
1045


1046
1047
1048
1049
1050
1051
1052
....
1069
1070
1071
1072
1073
1074
1075
1076


1077
1078
1079
1080
1081
1082
1083
....
1106
1107
1108
1109
1110
1111
1112
1113


1114
1115
1116
1117
1118
1119
1120
....
1139
1140
1141
1142
1143
1144
1145
1146


1147
1148
1149
1150
1151
1152
1153
....
1172
1173
1174
1175
1176
1177
1178
1179


1180
1181
1182
1183
1184
1185
1186
....
1209
1210
1211
1212
1213
1214
1215
1216


1217
1218
1219
1220
1221
1222
1223
....
1229
1230
1231
1232
1233
1234
1235


1236
1237
1238
1239
1240
1241
1242
static void xFree(void *p){
  if( p ){
    nOutstandingMalloc--;
  }
  free(p);
}


static Th_Vtab vtab = { xMalloc, xFree, {
  Th_output_f_FILE,

  NULL,
  1
  }
};

/*
** Generate a TH1 trace message if debugging is enabled.
................................................................................
  Th_Interp *interp, 
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "enable_output BOOLEAN");


  }else{
    int rc = Th_ToInt(interp, argv[1], argl[1], &enableOutput);
    vtab.out.enabled = enableOutput;
    return rc;
  }
}

................................................................................
  const char **argv, 
  int *argl
){
  PutsCmdData const * fmt = (PutsCmdData const *)pConvert;
  const int sepLen = fmt->sep ? strlen(fmt->sep) : 0;
  int i;
  if( argc<2 ){


    return Th_WrongNumArgs(interp, "puts STRING ...STRING_N");
  }
  for( i = 1; i < argc; ++i ){
    if(sepLen && (i>1)){
      sendText(interp, fmt->sep, sepLen, 0);
    }
    sendText(interp, (char const*)argv[i], argl[i], fmt->escapeHtml);
  }
................................................................................
  Th_Interp *interp, 
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "wiki STRING");


  }
  if( enableOutput ){
    Blob src;
    blob_init(&src, (char*)argv[1], argl[1]);
    wiki_convert(&src, 0, WIKI_INLINE);
    blob_reset(&src);
  }
................................................................................
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  char *zOut;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "htmlize STRING");


  }
  zOut = htmlize((char*)argv[1], argl[1]);
  Th_SetResult(interp, zOut, -1);
  free(zOut);
  return TH_OK;
}

................................................................................
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  int rc;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "hascap STRING");


  }
  rc = login_has_capability((char*)argv[1],argl[1]);
  if( g.thTrace ){
    Th_Trace("[hascap %#h] => %d<br />\n", argl[1], argv[1], rc);
  }
  Th_SetResultInt(interp, rc);
  return TH_OK;
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  int rc = 0;
  char const * zArg;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "hasfeature STRING");


  }
  zArg = (char const*)argv[1];
  if(NULL==zArg){
    /* placeholder for following ifdefs... */
  }
#if defined(FOSSIL_ENABLE_JSON)
  else if( 0 == fossil_strnicmp( zArg, "json", 4 ) ){
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  int rc = 0;
  int i;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "anycap STRING");


  }
  for(i=0; rc==0 && i<argl[1]; i++){
    rc = login_has_capability((char*)&argv[1][i],1);
  }
  if( g.thTrace ){
    Th_Trace("[hascap %#h] => %d<br />\n", argl[1], argv[1], rc);
  }
................................................................................
  Th_Interp *interp,
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  if( argc!=4 ){


    return Th_WrongNumArgs(interp, "combobox NAME TEXT-LIST NUMLINES");
  }
  if( enableOutput ){
    int height;
    Blob name;
    int nValue;
    const char *zValue;
    char *z, *zH;
................................................................................
  const char **argv, 
  int *argl
){
  const char *z;
  int size, n, i;
  int iMin, iMax;
  if( argc!=4 ){
    return Th_WrongNumArgs(interp, "linecount STRING MAX MIN");


  }
  if( Th_ToInt(interp, argv[2], argl[2], &iMax) ) return TH_ERROR;
  if( Th_ToInt(interp, argv[3], argl[3], &iMin) ) return TH_ERROR;
  z = argv[1];
  size = argl[1];
  for(n=1, i=0; i<size; i++){
    if( z[i]=='\n' ){
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  int openRepository;

  if( argc!=1 && argc!=2 ){
    return Th_WrongNumArgs(interp, "repository ?BOOLEAN?");


  }
  if( argc==2 ){
    if( Th_ToInt(interp, argv[1], argl[1], &openRepository) ){
      return TH_ERROR;
    }
    if( openRepository ) db_find_and_open_repository(OPEN_OK_NOT_FOUND, 0);
  }
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  char const * zVal;
  int pos = 0;
  if( argc != 2 ){
    return Th_WrongNumArgs(interp, "argv_get Index");


  }
  if( TH_OK != Th_ToInt(interp, argv[1], argl[1], &pos) ){
    return TH_ERROR;
  }
  if( pos < 0 || pos >= g.argc ){
    Th_ErrorMessage(interp, "Argument out of range:", argv[1], argl[1]);
    return TH_ERROR;
................................................................................
  enum { BufLen = 100 };
  char zLong[BufLen] = {0};
  char zShort[BufLen] = {0};
  char aBuf[BufLen] = {0};
  int hasArg;
  char const * zVal = NULL;
  char const * zDefault = NULL;

  if( 1 < argc ){
    assert( argl[1] < BufLen );
    snprintf( zLong, BufLen, "%s", argv[1] );

  }
  if( (2 < argc) && (0 < argl[2]) ){
    assert( argl[2] < BufLen );
    snprintf( zShort, BufLen, "%s", argv[2] );

  }
  if( 3 < argc){
    zDefault = argv[3];
  }

  if(0 == zLong[0]){


    return Th_WrongNumArgs(interp, "argv_getstr longName ?shortName? ?defaultVal?");
  }
  zVal = find_option( zLong, zShort[0] ? zShort : NULL, 1 );
  if(!zVal){
    zVal = zDefault;
    if(!zVal){
      Th_ErrorMessage(interp, "Option not found and no default provided:", zLong, -1);
      return TH_ERROR;
................................................................................
  char zShort[BufLen] = {0};
  char aBuf[BufLen] = {0};
  int hasArg;
  char const * zVal = NULL;
  char const * zDefault = NULL;
  int val;
  int rc;

  if( 1 < argc ){
    assert( argl[1] < BufLen );
    snprintf( zLong, BufLen, "%s", argv[1] );

  }
  if( (2 < argc) && (0 < argl[2]) ){
    assert( argl[2] < BufLen );
    snprintf( zShort, BufLen, "%s", argv[2] );

  }
  if( 3 < argc){
    zDefault = argv[3];
  }

  if(0 == zLong[0]){


    return Th_WrongNumArgs(interp, "argv_getbool longName ?shortName? ?defaultVal?");
  }
  zVal = find_option( zLong, zShort[0] ? zShort : NULL, 0 );
  if(zVal && !*zVal){
    zVal = "1";
  }
  if(!zVal){
    zVal = zDefault;
................................................................................
  char zLong[BufLen] = {0};
  char zShort[BufLen] = {0};
  char aBuf[BufLen] = {0};
  int hasArg;
  char const * zVal = NULL;
  char const * zDefault = NULL;
  int val = 0;

  if( 1 < argc ){
    assert( argl[1] < BufLen );
    snprintf( zLong, BufLen, "%s", argv[1] );

  }
  if( (2 < argc) && (0 < argl[2]) ){
    assert( argl[2] < BufLen );
    snprintf( zShort, BufLen, "%s", argv[2] );

  }
  if( 3 < argc){
    zDefault = argv[3];
  }

  if(0 == zLong[0]){


    return Th_WrongNumArgs(interp, "argv_getint longName ?shortName? ?defaultVal?");
  }
  zVal = find_option( zLong, zShort[0] ? zShort : NULL, 0 );
  if(!zVal){
    zVal = zDefault;
    if(!zVal){
      Th_ErrorMessage(interp, "Option not found and no default provided:", zLong, -1);
      return TH_ERROR;
................................................................................
  int *argl
){
  char const * zSql;
  sqlite3_stmt * pStmt = NULL;
  int rc;
  char const * errMsg = NULL;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "query_prepare STRING");


  }
  zSql = argv[1];
  rc = sqlite3_prepare( g.db, zSql, strlen(zSql), &pStmt, NULL );
  if(SQLITE_OK==rc){
    if(sqlite3_column_count( pStmt ) < 1){
      errMsg = "Only SELECT-like queries are supported.";
      rc = SQLITE_ERROR;
................................................................................
  int *argl
){
  char * zSql;
  sqlite3_stmt * pStmt = NULL;
  int rc = 0;
  char const * arg;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "query_finalize StmtHandle");


  }
  arg = argv[1];
  pStmt = queryStmtHandle(interp, arg, argl[1], &rc);
  if( rc < 1 ){
    return TH_ERROR;
  }
  assert( NULL != pStmt );
................................................................................
  int *argl,
  sqlite3_stmt ** pStmt,
  int * pIndex ){
  int index = 0;
  sqlite3_stmt * stmt;
  if( !pIndex ){
    if(argc<2){
      return Th_WrongNumArgs(interp, "StmtHandle");


    }
  }else{
    if( argc<3 ){
      return Th_WrongNumArgs(interp, "StmtHandle, Index");


    }
    if( 0 != Th_ToInt( interp, argv[2], argl[2], &index ) ){
      return TH_ERROR;
    }
  }
  stmt = queryStmtHandle(interp, argv[1], argl[1], NULL);
  if( NULL == stmt ){
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc = 0;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "query_step StmtHandle");


  }
  if(0 != queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, NULL)){
    return TH_ERROR;
  }
  assert(NULL != pStmt);
  rc = sqlite3_step( pStmt );
  switch(rc){
................................................................................
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  char const * val;
  int index = -1;
  int valLen;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_col_string StmtHandle Index");


  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  val = sqlite3_column_text( pStmt, index );
  valLen = val ? sqlite3_column_bytes( pStmt, index ) : 0;
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc = 0;
  int index = -1;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_col_int StmtHandle Index");


  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  Th_SetResultInt( interp, sqlite3_column_int( pStmt, index ) );
  return TH_OK;
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  double rc = 0;
  int index = -1;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_col_double StmtHandle Index");


  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  Th_SetResultDouble( interp, sqlite3_column_double( pStmt, index ) );
  return TH_OK;
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  double rc = 0;
  int index = -1;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_col_is_null StmtHandle Index");


  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  Th_SetResultInt( interp,
                   SQLITE_NULL==sqlite3_column_type( pStmt, index )
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  double rc = 0;
  int index = -1;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_col_type StmtHandle Index");


  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  Th_SetResultInt( interp, sqlite3_column_type( pStmt, index ) );
  return TH_OK;
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  int rc;
  sqlite3_stmt * pStmt = NULL;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "query_col_count StmtHandle");


  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], NULL);
  if( NULL == pStmt ){
    return TH_ERROR;
  }
  rc = sqlite3_column_count( pStmt );
  Th_SetResultInt( interp, rc );
................................................................................
){
  sqlite3_stmt * pStmt = NULL;
  char const * val;
  int index;
  int rc = 0;
  int valLen;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_col_name StmtHandle Index");


  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], &rc);
  if( rc < 1 ){
    return TH_ERROR;
  }
  if( 0 != Th_ToInt( interp, argv[2], argl[2], &index ) ){
    return TH_ERROR;
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc;
  int index = 0;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_bind_null StmtHandle Index");


  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 1){
    return TH_ERROR;
  }
  rc = sqlite3_bind_null( pStmt, index );
  if(rc){
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc;
  int index = 0;
  if( argc!=4 ){
    return Th_WrongNumArgs(interp, "query_bind_string StmtHandle Index val");


  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 1){
    return TH_ERROR;
  }
  rc = sqlite3_bind_text( pStmt, index, argv[3], argl[3], SQLITE_TRANSIENT );
  if(rc){
................................................................................
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc;
  int val;
  int index = 0;
  if( argc!=4 ){
    return Th_WrongNumArgs(interp, "query_bind_int StmtHandle Index val");


  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 1){
    return TH_ERROR;
  }
  if( 0 != Th_ToInt( interp, argv[3], argl[3], &val ) ){
    return TH_ERROR;
................................................................................
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc;
  double val;
  int index = 0;
  if( argc!=4 ){
    return Th_WrongNumArgs(interp, "query_bind_double StmtHandle Index val");


  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 1){
    return TH_ERROR;
  }
  if( 0 != Th_ToDouble( interp, argv[3], argl[3], &val ) ){
    return TH_ERROR;
................................................................................
  }
  Th_SetResultInt( interp, 0 );
  return TH_OK;
}

#endif
/* end TH_USE_SQLITE */



/*
** Make sure the interpreter has been initialized.  Initialize it if
** it has not been already.
**
** The interpreter is stored in the g.interp global variable.
*/







<

<
>







 







|
>
>







 







>
>
|







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







>
>
|







 







|
>
>







 







|
>
>







 







|
>
>







 







>


|
>



|
>






>
>
|







 







>


|
>



|
>






>
>
|







 







>


|
>



|
>






>
>
|







 







|
>
>







 







|
>
>







 







|
>
>



|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







|
>
>







 







>
>







41
42
43
44
45
46
47

48

49
50
51
52
53
54
55
56
..
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
...
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
...
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
...
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
...
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
...
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
...
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
...
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
...
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
...
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
...
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
...
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
...
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
...
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
....
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
....
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
....
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
....
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
....
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
....
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
....
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
....
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
....
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
....
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
static void xFree(void *p){
  if( p ){
    nOutstandingMalloc--;
  }
  free(p);
}


static Th_Vtab vtab = { xMalloc, xFree, {

  NULL,
  NULL,
  1
  }
};

/*
** Generate a TH1 trace message if debugging is enabled.
................................................................................
  Th_Interp *interp, 
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                           "BOOLEAN");
  }else{
    int rc = Th_ToInt(interp, argv[1], argl[1], &enableOutput);
    vtab.out.enabled = enableOutput;
    return rc;
  }
}

................................................................................
  const char **argv, 
  int *argl
){
  PutsCmdData const * fmt = (PutsCmdData const *)pConvert;
  const int sepLen = fmt->sep ? strlen(fmt->sep) : 0;
  int i;
  if( argc<2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "STRING ...STRING_N");
  }
  for( i = 1; i < argc; ++i ){
    if(sepLen && (i>1)){
      sendText(interp, fmt->sep, sepLen, 0);
    }
    sendText(interp, (char const*)argv[i], argl[i], fmt->escapeHtml);
  }
................................................................................
  Th_Interp *interp, 
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "STRING");
  }
  if( enableOutput ){
    Blob src;
    blob_init(&src, (char*)argv[1], argl[1]);
    wiki_convert(&src, 0, WIKI_INLINE);
    blob_reset(&src);
  }
................................................................................
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  char *zOut;
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "STRING");
  }
  zOut = htmlize((char*)argv[1], argl[1]);
  Th_SetResult(interp, zOut, -1);
  free(zOut);
  return TH_OK;
}

................................................................................
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  int rc;
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                           argv[0], argl[0],
                           "STRING");
  }
  rc = login_has_capability((char*)argv[1],argl[1]);
  if( g.thTrace ){
    Th_Trace("[hascap %#h] => %d<br />\n", argl[1], argv[1], rc);
  }
  Th_SetResultInt(interp, rc);
  return TH_OK;
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  int rc = 0;
  char const * zArg;
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "STRING");
  }
  zArg = (char const*)argv[1];
  if(NULL==zArg){
    /* placeholder for following ifdefs... */
  }
#if defined(FOSSIL_ENABLE_JSON)
  else if( 0 == fossil_strnicmp( zArg, "json", 4 ) ){
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  int rc = 0;
  int i;
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "STRING");
  }
  for(i=0; rc==0 && i<argl[1]; i++){
    rc = login_has_capability((char*)&argv[1][i],1);
  }
  if( g.thTrace ){
    Th_Trace("[hascap %#h] => %d<br />\n", argl[1], argv[1], rc);
  }
................................................................................
  Th_Interp *interp,
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  if( argc!=4 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "NAME TEXT-LIST NUMLINES");
  }
  if( enableOutput ){
    int height;
    Blob name;
    int nValue;
    const char *zValue;
    char *z, *zH;
................................................................................
  const char **argv, 
  int *argl
){
  const char *z;
  int size, n, i;
  int iMin, iMax;
  if( argc!=4 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "STRING MAX MIN");
  }
  if( Th_ToInt(interp, argv[2], argl[2], &iMax) ) return TH_ERROR;
  if( Th_ToInt(interp, argv[3], argl[3], &iMin) ) return TH_ERROR;
  z = argv[1];
  size = argl[1];
  for(n=1, i=0; i<size; i++){
    if( z[i]=='\n' ){
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  int openRepository;

  if( argc!=1 && argc!=2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "?BOOLEAN?");
  }
  if( argc==2 ){
    if( Th_ToInt(interp, argv[1], argl[1], &openRepository) ){
      return TH_ERROR;
    }
    if( openRepository ) db_find_and_open_repository(OPEN_OK_NOT_FOUND, 0);
  }
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  char const * zVal;
  int pos = 0;
  if( argc != 2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "Index");
  }
  if( TH_OK != Th_ToInt(interp, argv[1], argl[1], &pos) ){
    return TH_ERROR;
  }
  if( pos < 0 || pos >= g.argc ){
    Th_ErrorMessage(interp, "Argument out of range:", argv[1], argl[1]);
    return TH_ERROR;
................................................................................
  enum { BufLen = 100 };
  char zLong[BufLen] = {0};
  char zShort[BufLen] = {0};
  char aBuf[BufLen] = {0};
  int hasArg;
  char const * zVal = NULL;
  char const * zDefault = NULL;
  int check;
  if( 1 < argc ){
    assert( argl[1] < BufLen );
    check = snprintf( zLong, BufLen, "%s", argv[1] );
    assert( check <= BufLen );
  }
  if( (2 < argc) && (0 < argl[2]) ){
    assert( argl[2] < BufLen );
    check = snprintf( zShort, BufLen, "%s", argv[2] );
    assert( check <= BufLen );
  }
  if( 3 < argc){
    zDefault = argv[3];
  }

  if(0 == zLong[0]){
    return Th_WrongNumArgs2(interp,
                           argv[0], argl[0],
                           "longName ?shortName? ?defaultVal?");
  }
  zVal = find_option( zLong, zShort[0] ? zShort : NULL, 1 );
  if(!zVal){
    zVal = zDefault;
    if(!zVal){
      Th_ErrorMessage(interp, "Option not found and no default provided:", zLong, -1);
      return TH_ERROR;
................................................................................
  char zShort[BufLen] = {0};
  char aBuf[BufLen] = {0};
  int hasArg;
  char const * zVal = NULL;
  char const * zDefault = NULL;
  int val;
  int rc;
  int check;
  if( 1 < argc ){
    assert( argl[1] < BufLen );
    check = snprintf( zLong, BufLen, "%s", argv[1] );
    assert( check <= BufLen );
  }
  if( (2 < argc) && (0 < argl[2]) ){
    assert( argl[2] < BufLen );
    check = snprintf( zShort, BufLen, "%s", argv[2] );
    assert( check <= BufLen );
  }
  if( 3 < argc){
    zDefault = argv[3];
  }

  if(0 == zLong[0]){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                           "longName ?shortName? ?defaultVal?");
  }
  zVal = find_option( zLong, zShort[0] ? zShort : NULL, 0 );
  if(zVal && !*zVal){
    zVal = "1";
  }
  if(!zVal){
    zVal = zDefault;
................................................................................
  char zLong[BufLen] = {0};
  char zShort[BufLen] = {0};
  char aBuf[BufLen] = {0};
  int hasArg;
  char const * zVal = NULL;
  char const * zDefault = NULL;
  int val = 0;
  int check;
  if( 1 < argc ){
    assert( argl[1] < BufLen );
    check = snprintf( zLong, BufLen, "%s", argv[1] );
    assert( check <= BufLen );
  }
  if( (2 < argc) && (0 < argl[2]) ){
    assert( argl[2] < BufLen );
    check = snprintf( zShort, BufLen, "%s", argv[2] );
    assert( check <= BufLen );
  }
  if( 3 < argc){
    zDefault = argv[3];
  }

  if(0 == zLong[0]){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                           "longName ?shortName? ?defaultVal?");
  }
  zVal = find_option( zLong, zShort[0] ? zShort : NULL, 0 );
  if(!zVal){
    zVal = zDefault;
    if(!zVal){
      Th_ErrorMessage(interp, "Option not found and no default provided:", zLong, -1);
      return TH_ERROR;
................................................................................
  int *argl
){
  char const * zSql;
  sqlite3_stmt * pStmt = NULL;
  int rc;
  char const * errMsg = NULL;
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "STRING");
  }
  zSql = argv[1];
  rc = sqlite3_prepare( g.db, zSql, strlen(zSql), &pStmt, NULL );
  if(SQLITE_OK==rc){
    if(sqlite3_column_count( pStmt ) < 1){
      errMsg = "Only SELECT-like queries are supported.";
      rc = SQLITE_ERROR;
................................................................................
  int *argl
){
  char * zSql;
  sqlite3_stmt * pStmt = NULL;
  int rc = 0;
  char const * arg;
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle");
  }
  arg = argv[1];
  pStmt = queryStmtHandle(interp, arg, argl[1], &rc);
  if( rc < 1 ){
    return TH_ERROR;
  }
  assert( NULL != pStmt );
................................................................................
  int *argl,
  sqlite3_stmt ** pStmt,
  int * pIndex ){
  int index = 0;
  sqlite3_stmt * stmt;
  if( !pIndex ){
    if(argc<2){
      return Th_WrongNumArgs2(interp,
                              argv[0], argl[0],
                              "StmtHandle");
    }
  }else{
    if( argc<3 ){
      return Th_WrongNumArgs2(interp,
                              argv[0], argl[0],
                              "StmtHandle Index");
    }
    if( 0 != Th_ToInt( interp, argv[2], argl[2], &index ) ){
      return TH_ERROR;
    }
  }
  stmt = queryStmtHandle(interp, argv[1], argl[1], NULL);
  if( NULL == stmt ){
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc = 0;
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle");
  }
  if(0 != queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, NULL)){
    return TH_ERROR;
  }
  assert(NULL != pStmt);
  rc = sqlite3_step( pStmt );
  switch(rc){
................................................................................
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  char const * val;
  int index = -1;
  int valLen;
  if( argc!=3 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index");
  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  val = sqlite3_column_text( pStmt, index );
  valLen = val ? sqlite3_column_bytes( pStmt, index ) : 0;
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc = 0;
  int index = -1;
  if( argc!=3 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index");
  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  Th_SetResultInt( interp, sqlite3_column_int( pStmt, index ) );
  return TH_OK;
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  double rc = 0;
  int index = -1;
  if( argc!=3 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index");
  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  Th_SetResultDouble( interp, sqlite3_column_double( pStmt, index ) );
  return TH_OK;
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  double rc = 0;
  int index = -1;
  if( argc!=3 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index");
  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  Th_SetResultInt( interp,
                   SQLITE_NULL==sqlite3_column_type( pStmt, index )
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  double rc = 0;
  int index = -1;
  if( argc!=3 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index");
  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 0){
    return TH_ERROR;
  }
  Th_SetResultInt( interp, sqlite3_column_type( pStmt, index ) );
  return TH_OK;
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  int rc;
  sqlite3_stmt * pStmt = NULL;
  if( argc!=2 ){
    return Th_WrongNumArgs2(interp,
                           argv[0], argl[0],
                           "StmtHandle");
  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], NULL);
  if( NULL == pStmt ){
    return TH_ERROR;
  }
  rc = sqlite3_column_count( pStmt );
  Th_SetResultInt( interp, rc );
................................................................................
){
  sqlite3_stmt * pStmt = NULL;
  char const * val;
  int index;
  int rc = 0;
  int valLen;
  if( argc!=3 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index");
  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], &rc);
  if( rc < 1 ){
    return TH_ERROR;
  }
  if( 0 != Th_ToInt( interp, argv[2], argl[2], &index ) ){
    return TH_ERROR;
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc;
  int index = 0;
  if( argc!=3 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index");
  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 1){
    return TH_ERROR;
  }
  rc = sqlite3_bind_null( pStmt, index );
  if(rc){
................................................................................
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc;
  int index = 0;
  if( argc!=4 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index Value");
  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 1){
    return TH_ERROR;
  }
  rc = sqlite3_bind_text( pStmt, index, argv[3], argl[3], SQLITE_TRANSIENT );
  if(rc){
................................................................................
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc;
  int val;
  int index = 0;
  if( argc!=4 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index Value");
  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 1){
    return TH_ERROR;
  }
  if( 0 != Th_ToInt( interp, argv[3], argl[3], &val ) ){
    return TH_ERROR;
................................................................................
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc;
  double val;
  int index = 0;
  if( argc!=4 ){
    return Th_WrongNumArgs2(interp,
                            argv[0], argl[0],
                            "StmtHandle Index Value");
  }
  queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index);
  if(index < 1){
    return TH_ERROR;
  }
  if( 0 != Th_ToDouble( interp, argv[3], argl[3], &val ) ){
    return TH_ERROR;
................................................................................
  }
  Th_SetResultInt( interp, 0 );
  return TH_OK;
}

#endif
/* end TH_USE_SQLITE */



/*
** Make sure the interpreter has been initialized.  Initialize it if
** it has not been already.
**
** The interpreter is stored in the g.interp global variable.
*/