UnQL

Check-in [e5de41b4de]
Login

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

Overview
Comment:Rework the shell command to make it useful as a test harness.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e5de41b4de216d50f142aaa2774bd8d9a2cecdc7
User & Date: drh 2011-06-27 12:19:39
Context
2011-06-27
12:49
Enhance the shell with parser and command tracing and "echo". check-in: d780db9196 user: drh tags: trunk
12:19
Rework the shell command to make it useful as a test harness. check-in: e5de41b4de user: drh tags: trunk
2011-06-25
00:13
Fix memory leaks. check-in: 9ee98afd91 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/shell.c.

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
..
93
94
95
96
97
98
99




















100










101
102





































103


























104
105















106


107
108
109
110
111
112
113
114





















115
















































116
117
118
119




120
121
122
123
124
125
126
127
128
129
130
131
132




















































133
134


135
136













137
138
139
140
141
142










143
144

145

146
147
148
149





150

151
152
153
154
155

156
157
158
159
160
161

162



163
164
165

166
167

168
169



















170





























171
172





173

174
175






































176


177
178
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>


static void usage(const char *argv0){
  fprintf(stderr, "usage: %s DATABASE\n", argv0);
  exit(1);
}

/*
** Remove n elements from argv beginning with the i-th element.
*/
void remove_from_argv(char **argv, int *pArgc, int i, int n){
  int j;
  int argc = *pArgc;
  for(j=i+n; j<argc; i++, j++){
................................................................................
      break;
    }
  }
  *pArgc = argc;
  return zReturn;
}
































int main(int argc, char **argv){
  xjd1 *pDb;





































  int rc, n;


























  char *zStmt = 0;
  int nStmt = 0;















  int nAlloc = 0;


  int isTTY = 1;
  int parserTrace;
  int testJson;
  const char *zPrompt = "xjd1> ";
  char zLine[2000];

  parserTrace = find_option(argv, &argc, "trace", 0, 0)!=0;
  testJson = find_option(argv, &argc, "json", 0, 0)!=0;





















  if( argc!=2 ) usage(argv[0]);
















































  rc = xjd1_open(0, argv[1], &pDb);
  if( rc!=XJD1_OK ){
    fprintf(stderr, "cannot open \"%s\": %s\n", argv[1], xjd1_errmsg(pDb));
    exit(1);




  }
  xjd1_config(pDb, XJD1_CONFIG_PARSERTRACE, parserTrace);
  isTTY = isatty(0);
  while( !feof(stdin) ){
    if( isTTY ) printf("%s", zPrompt);
    if( fgets(zLine, sizeof(zLine), stdin)==0 ) break;
    if( testJson ){
      JsonNode *pNode = xjd1JsonParse(zLine, -1);
      if( pNode ){
        String out;
        xjd1StringInit(&out, 0, 0);
        xjd1JsonRender(&out, pNode);
        xjd1JsonFree(pNode);




















































        printf("%s\n", xjd1StringText(&out));
        xjd1StringClear(&out);


      }
      continue;













    }
    n = strlen(zLine);
    if( n+nStmt+1 >= nAlloc ){
      nAlloc = nAlloc + n + nStmt + 1000;
      zStmt = realloc(zStmt, nAlloc);
      if( zStmt==0 ){










        fprintf(stderr, "out of memory\n");
        exit(1);

      }

    }
    memcpy(&zStmt[nStmt], zLine, n+1);
    nStmt += n;
    if( xjd1_complete(zStmt) ){





      xjd1_stmt *pStmt;

      rc = xjd1_stmt_new(pDb, zStmt, &pStmt, &n);
      if( rc!=XJD1_OK ){
        fprintf(stderr, "ERROR: %s\n", xjd1_errmsg(pDb));
      }else{
        int res;

        char *zTrace = xjd1_stmt_debug_listing(pStmt);
        if( zTrace ) printf("%s", zTrace);
        free(zTrace);
        while( (res = xjd1_stmt_step(pStmt))==XJD1_ROW ){
          const char *zVal;
          xjd1_stmt_value(pStmt, &zVal);

          printf("%s\n", zVal);



        }
        if( res!=XJD1_DONE ){
          fprintf(stderr, "ERROR: %s\n", xjd1_errmsg(pDb));

        }
      }

      xjd1_stmt_delete(pStmt);
      zPrompt = "xjd1> ";



















      nStmt = 0;





























    }else{
      zPrompt = " ...> ";





    }

  }
  free(zStmt);






































  xjd1_close(pDb);


  return 0;
}







<
<
<
<
<







 







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

>
>
>
>
>
>
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
<
<
<
<
|
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
<
<
>
>
>
>
|
<
<
<
<
<
<
<
<
<
<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
<
<
>
>
>
>
>
>
>
>
>
>
|
<
>
|
>
|
<
<
<
>
>
>
>
>
|
>
|
|
<
<
<
>
|
<
<
|
|
|
>
|
>
>
>
|
<
<
>


>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
>
>
>
>
>

>

<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>


21
22
23
24
25
26
27





28
29
30
31
32
33
34
..
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211




212


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284


285
286
287
288
289












290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

347
348
349
350
351
352
353
354
355
356
357
358
359
360





361
362
363
364
365
366
367
368
369
370
371

372
373
374
375



376
377
378
379
380
381
382
383
384



385
386


387
388
389
390
391
392
393
394
395


396
397
398
399
400

401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

451
452
453
454
455
456
457
458

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>







/*
** Remove n elements from argv beginning with the i-th element.
*/
void remove_from_argv(char **argv, int *pArgc, int i, int n){
  int j;
  int argc = *pArgc;
  for(j=i+n; j<argc; i++, j++){
................................................................................
      break;
    }
  }
  *pArgc = argc;
  return zReturn;
}

/*
** State information
*/
typedef struct Shell Shell;
struct Shell {
  xjd1 *pDb;           /* Open database connection */
  const char *zFile;   /* Current filename */
  int nLine;           /* Current line number */
  FILE *pIn;           /* Input file */
  int isTTY;           /* True if pIn is a TTY */
  int isTest;          /* True if the --test flag is used */
  int parseTrace;      /* True if the --parser-trace flag is used */
  String testOut;      /* Output from a test case */
  char zModule[50];    /* Mame of current test module */
  char zTestCase[50];  /* Name of current testcase */
  String inBuf;        /* Buffered input text */
  int nCase;           /* Number of --testcase commands seen */
  int nTest;           /* Number of tests performed */
  int nErr;            /* Number of test errors */
};

/*
** True for space characters.
*/
static int shellIsSpace(char c){
  return c==' ' || c=='\t' || c=='\r' || c=='\n';
}

/*
** Command:  .quit
*/
static void shellQuit(Shell *p, int argc, char **argv){

  exit(0);
}

/*
** Command:  .testcase NAME
*/
static void shellTestcase(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    int n = strlen(argv[1]);
    if( n>=sizeof(p->zTestCase) ) n = sizeof(p->zTestCase)-1;
    memcpy(p->zTestCase, argv[1], n);
    p->zTestCase[n] = 0;
    xjd1StringTruncate(&p->testOut);
  }
}

/*
** Return non-zero if string z matches glob pattern zGlob and zero if the
** pattern does not match.
**
** Globbing rules:
**
**      '*'       Matches any sequence of zero or more characters.
**
**      '?'       Matches exactly one character.
**
**     [...]      Matches one character from the enclosed list of
**                characters.
**
**     [^...]     Matches one character not in the enclosed list.
**
**      '#'       Matches any sequence of one or more digits with an
**                optional + or - sign in front
*/
static int strglob(const char *zGlob, const char *z){
  int c, c2;
  int invert;
  int seen;

  while( (c = (*(zGlob++)))!=0 ){
    if( c=='*' ){
      while( (c=(*(zGlob++))) == '*' || c=='?' ){
        if( c=='?' && (*(z++))==0 ) return 0;
      }
      if( c==0 ){
        return 1;
      }else if( c=='[' ){
        while( *z && strglob(zGlob-1,z)==0 ){
          z++;
        }
        return (*z)!=0;
      }
      while( (c2 = (*(z++)))!=0 ){
        while( c2!=c ){
          c2 = *(z++);
          if( c2==0 ) return 0;
        }
        if( strglob(zGlob,z) ) return 1;
      }
      return 0;
    }else if( c=='?' ){
      if( (*(z++))==0 ) return 0;
    }else if( c=='[' ){
      int prior_c = 0;
      seen = 0;
      invert = 0;
      c = *(z++);
      if( c==0 ) return 0;
      c2 = *(zGlob++);
      if( c2=='^' ){
        invert = 1;
        c2 = *(zGlob++);
      }
      if( c2==']' ){
        if( c==']' ) seen = 1;
        c2 = *(zGlob++);
      }
      while( c2 && c2!=']' ){
        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
          c2 = *(zGlob++);
          if( c>=prior_c && c<=c2 ) seen = 1;
          prior_c = 0;
        }else{
          if( c==c2 ){
            seen = 1;




          }


          prior_c = c2;
        }
        c2 = *(zGlob++);
      }
      if( c2==0 || (seen ^ invert)==0 ) return 0;
    }else if( c=='#' ){
      if( (z[0]=='-' || z[0]=='+') && (z[1]>='0' && z[1]<='9') ) z++;
      if( z[0]<'0' || z[0]>'9' ) return 0;
      z++;
      while( z[0]>='0' && z[0]<='9' ){ z++; }
    }else{
      if( c!=(*(z++)) ) return 0;
    }
  }
  return *z==0;
}

/*
** Command:  .result PATTERN
*/
static void shellResult(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    char *zRes = xjd1StringText(&p->testOut);
    if( zRes==0 ) zRes = "";
    p->nTest++;
    if( strcmp(argv[1], zRes) ){
      p->nErr++;
      fprintf(stderr, " Test failed: %s\n Expected: [%s]\n      Got: [%s]\n",
        p->zTestCase, argv[1], zRes);
    }
  }
}
/*
** Command:  .glob PATTERN
*/
static void shellGlob(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    char *zRes = xjd1StringText(&p->testOut);
    if( zRes==0 ) zRes = "";
    p->nTest++;
    if( strglob(argv[1], zRes) ){
      p->nErr++;
      fprintf(stderr, " Test failed: %s\n Expected: [%s]\n      Got: [%s]\n",
        p->zTestCase, argv[1], zRes);
    }
  }
}
/*
** Command:  .notglob PATTERN
*/
static void shellNotGlob(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    char *zRes = xjd1StringText(&p->testOut);
    if( zRes==0 ) zRes = "";
    p->nTest++;
    if( strglob(argv[1], zRes)==0 ){
      p->nErr++;
      fprintf(stderr, " Test failed: %s\n Expected: [%s]\n      Got: [%s]\n",
        p->zTestCase, argv[1], zRes);
    }
  }
}

/*
** Command:  .open FILENAME
*/
static void shellOpenDB(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    int rc;
    if( p->pDb ) xjd1_close(p->pDb);
    rc = xjd1_open(0, argv[1], &p->pDb);
    if( rc!=XJD1_OK ){


      fprintf(stderr, "%s:%d: cannot open \"%s\"\n",
              p->zFile, p->nLine, argv[1]);
      p->nErr++;
      p->pDb = 0;
    }












  }    
}
/*
** Command:  .new FILENAME
*/
static void shellNewDB(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    if( p->pDb ) xjd1_close(p->pDb);
    p->pDb = 0;
    unlink(argv[1]);
    shellOpenDB(p, argc, argv);
  }
}

/* Forward declaration */
static void processOneFile(Shell*, const char*);

/*
** Command:  .read FILENAME
** Read and process text from a file.
*/
static void shellRead(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    processOneFile(p, argv[1]);
  }
}

/*
** Process a command intended for this shell - not for the database.
*/
static void processMetaCommand(Shell *p){
  char *z;
  int n;
  char *azArg[2];
  int nArg;
  int i;
  static const struct shcmd {
    const char *zCmd;                   /* Name of the command */
    void (*xCmd)(Shell*,int,char**);    /* Procedure to run the command */
    const char *zHelp;                  /* Help string */
  } cmds[] = {
    { "quit",     shellQuit,      ".quit"               },
    { "testcase", shellTestcase,  ".testcase NAME"      },
    { "result",   shellResult,    ".result TEXT"        },
    { "glob",     shellGlob,      ".glob PATTERN"       },
    { "notglob",  shellNotGlob,   ".notglob PATTERN"    },
    { "read",     shellRead,      ".read FILENAME"      },
    { "open",     shellOpenDB,    ".open DATABASE"      },
    { "new",      shellNewDB,     ".new DATABASE"       },
  };

  /* Remove trailing whitespace from the command */
  z = xjd1StringText(&p->inBuf);
  n = xjd1StringLen(&p->inBuf);
  while( n>0 && shellIsSpace(z[n-1]) ){ n--; }
  z[n] = 0;


  /* Find the command name text and its argument (if any) */
  z++;
  azArg[0] = z;
  for(i=0; z[i] && !shellIsSpace(z[i]); i++){}
  if( z[i] ){
    z[i] = 0;
    z += i+1;
    while( shellIsSpace(z[0]) ) z++;
    azArg[1] = z;
    nArg = 2;
  }else{
    azArg[1] = 0;
    nArg = 1;
  }






  /* Find the command */
  for(i=0; i<sizeof(cmds)/sizeof(cmds[0]); i++){
    if( strcmp(azArg[0], cmds[i].zCmd)==0 ){
      cmds[i].xCmd(p, nArg, azArg);
      break;
    }
  }
  if( i>=sizeof(cmds)/sizeof(cmds[0]) ){
    p->nErr++;
    fprintf(stderr, "%s:%d: unknown command \".%s\"\n", 

            p->zFile, p->nLine, azArg[0]);
  }
  xjd1StringTruncate(&p->inBuf);
}




/*
** Run a single statment.
*/
static void processOneStatement(Shell *p, const char *zCmd){
  xjd1_stmt *pStmt;
  int N, rc;
  rc = xjd1_stmt_new(p->pDb, zCmd, &pStmt, &N);
  if( rc==XJD1_OK ){



    do{
      rc = xjd1_stmt_step(pStmt);


      if( rc==XJD1_ROW ){
        const char *zValue;
        xjd1_stmt_value(pStmt, &zValue);
        if( p->zTestCase[0]==0 ){
          printf("%s\n", zValue);
        }else{
          if( xjd1StringLen(&p->testOut)>0 ){
            xjd1StringAppend(&p->testOut, " ", 1);
          }


          xjd1StringAppend(&p->testOut, zValue, -1);
        }
      }
    }while( rc==XJD1_ROW );
    xjd1_stmt_delete(pStmt);

  }else{
    fprintf(stderr, "%s:%d: ERROR: %s\n",
            p->zFile, p->nLine, xjd1_errmsg(p->pDb));
    p->nErr++;
  }
}

/*
** Process one or more database commands
*/
static void processScript(Shell *p){
  char *z, c;
  int i;
  if( p->pDb==0 ) return;
  while( xjd1StringLen(&p->inBuf) ){
    z = xjd1StringText(&p->inBuf);
    for(i=0; z[i]; i++){
      if( z[i]!=';' ) continue;
      c = z[i+1];
      z[i+1] = 0;
      if( !xjd1_complete(z) ){
        z[i+1] = c;
        continue;
      }
      processOneStatement(p, z);
      z[i+1] = c;
      while( shellIsSpace(z[i+1]) ){ i++; }
      xjd1StringRemovePrefix(&p->inBuf, i+1);
      break;
    }
  }
}

/*
** Process a single file of input by reading the text of the file and
** sending statements into the XJD1 database connection.
*/
static void processOneFile(
  Shell *p,               /* Current state of the shell */
  const char *zFile       /* Name of file to process.  "-" for stdin. */
){
  Shell savedState;
  char *zIn;
  char zLine[1000];

  savedState = *p;
  if( strcmp(zFile, "-")==0 ){
    p->pIn = stdin;
    p->isTTY = 1;
  }else{

    p->pIn = fopen(zFile, "r");
    if( p->pIn==0 ){
      fprintf(stderr, "%s:%d: cannot open \"%s\"\n", p->zFile, p->nLine, zFile);
      p->nErr++;
      return;
    }
    p->isTTY = 0;
  }

  p->zFile = zFile;
  p->nLine = 0;
  while( !feof(p->pIn) ){
    if( p->isTTY ){
      printf("%s", xjd1StringLen(&p->inBuf)>0 ? " ...> " : "xjd1> ");
    }
    if( fgets(zLine, sizeof(zLine), p->pIn)==0 ) break;
    p->nLine++;
    xjd1StringAppend(&p->inBuf, zLine, -1);
    zIn = xjd1StringText(&p->inBuf);
    if( zIn[0]=='.' ){
      processMetaCommand(p);
    }else{
      processScript(p);
    }
  }
  if( p->isTTY==0 ){
    fclose(p->pIn);
  }
  p->zFile = savedState.zFile;
  p->nLine = savedState.nLine;
  p->pIn = savedState.pIn;
  p->isTTY = savedState.isTTY;
}


int main(int argc, char **argv){
  int i;
  Shell s;

  memset(&s, 0, sizeof(s));
  if( argc>1 ){
    for(i=1; i<argc; i++){
      processOneFile(&s, argv[i]);
    }
  }else{
    processOneFile(&s, "-");
  }
  if( s.pDb ) xjd1_close(s.pDb);
  xjd1StringClear(&s.inBuf);
  xjd1StringClear(&s.testOut);
  return 0;
}

Changes to src/string.c.

128
129
130
131
132
133
134













135
136
137
138
139
140
141
*/
PRIVATE void xjd1StringDelete(String *pStr){
  if( pStr && pStr->pPool==0 ){
    free(pStr->zBuf);
    free(pStr);
  }
}














/*
** Conversion types fall into various categories as defined by the
** following enumeration.
*/
#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
#define etFLOAT       2 /* Floating point.  %f */







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







128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
*/
PRIVATE void xjd1StringDelete(String *pStr){
  if( pStr && pStr->pPool==0 ){
    free(pStr->zBuf);
    free(pStr);
  }
}

/*
** Consume the first N characters from the front of the string.
*/
PRIVATE void xjd1StringRemovePrefix(String *pStr, int N){
  if( pStr==0 ) return;
  if( pStr->nUsed<=N ){
    xjd1StringTruncate(pStr);
  }else{
    memmove(pStr->zBuf, &pStr->zBuf[N], (pStr->nUsed+1)-N);
    pStr->nUsed -= N;
  }
}

/*
** Conversion types fall into various categories as defined by the
** following enumeration.
*/
#define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
#define etFLOAT       2 /* Floating point.  %f */

Changes to src/xjd1Int.h.

334
335
336
337
338
339
340

341
342
343
344
345
346
347
String *xjd1StringNew(Pool*, int);
int xjd1StringAppend(String*, const char*, int);
#define xjd1StringText(S)      ((S)->zBuf)
#define xjd1StringLen(S)       ((S)->nUsed)
void xjd1StringTruncate(String*);
void xjd1StringClear(String*);
void xjd1StringDelete(String*);

int xjd1StringVAppendF(String*, const char*, va_list);
int xjd1StringAppendF(String*, const char*, ...);


/******************************** tokenize.c *********************************/
extern const unsigned char xjd1CtypeMap[];
#define xjd1Isspace(x)   (xjd1CtypeMap[(unsigned char)(x)]&0x01)







>







334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
String *xjd1StringNew(Pool*, int);
int xjd1StringAppend(String*, const char*, int);
#define xjd1StringText(S)      ((S)->zBuf)
#define xjd1StringLen(S)       ((S)->nUsed)
void xjd1StringTruncate(String*);
void xjd1StringClear(String*);
void xjd1StringDelete(String*);
void xjd1StringRemovePrefix(String*,int);
int xjd1StringVAppendF(String*, const char*, va_list);
int xjd1StringAppendF(String*, const char*, ...);


/******************************** tokenize.c *********************************/
extern const unsigned char xjd1CtypeMap[];
#define xjd1Isspace(x)   (xjd1CtypeMap[(unsigned char)(x)]&0x01)