UnQL

Check-in [d780db9196]
Login

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

Overview
Comment:Enhance the shell with parser and command tracing and "echo".
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: d780db9196547519a64f931f32aa7a8a6f89bfd4
User & Date: drh 2011-06-27 12:49:57
Context
2011-06-27
14:08
Base test infrastructure working. Added "make test" to the makefile. check-in: 9432de00a5 user: drh tags: trunk
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/shell.c.

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
...
309
310
311
312
313
314
315








316
317
318
319
320
321
322
...
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
...
376
377
378
379
380
381
382





383
384





385
386
387
388
389
390
391
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);
}
................................................................................
** 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;
................................................................................
  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;
................................................................................

/*
** 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);







|
<








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







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







 







>
>
>
>
>
>
>
>







 







|
|
|
|
|
|
|
|
>
>
>




>
>
>







 







>
>
>
>
>


>
>
>
>
>







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
...
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
...
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
...
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
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 shellFlags;      /* Flag settings */

  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 */
};

/*
** Flag names
*/
#define SHELL_PARSER_TRACE     0x00001
#define SHELL_CMD_TRACE        0x00002
#define SHELL_ECHO             0x00004
static const struct {
  const char *zName;
  int iValue;
} FlagNames[] = {
  {  "parser-trace",   SHELL_PARSER_TRACE },
  {  "cmd-trace",      SHELL_CMD_TRACE    },
  {  "echo",           SHELL_ECHO         },
};

/*
** Convert a flag name into a mask.
*/
static int flagMask(const char *zName){
  int i;
  for(i=0; i<sizeof(FlagNames)/sizeof(FlagNames[0]); i++){
    if( strcmp(zName, FlagNames[i].zName)==0 ){
      return FlagNames[i].iValue;
    }
  }
  return 0;
}

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

/*
** Set flags
*/
static void shellSet(Shell *p, int argc, char **argv){
  int i;
  if( argc>=2 ){
    p->shellFlags |= flagMask(argv[1]);
  }else{
    for(i=0; i<sizeof(FlagNames)/sizeof(FlagNames[0]); i++){
      printf("%-20s %s\n", 
        FlagNames[i].zName,
        (p->shellFlags & FlagNames[i].iValue)!=0 ? "on" : "off");
    }
  }
}
static void shellClear(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    p->shellFlags &= ~flagMask(argv[1]);
  }
}

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

/*
** Command:  .breakpoint
** A place to seet a breakpoint
*/
static void shellBreakpoint(Shell *p, int argc, char **argv){
  /* no-op */
}

/*
** Process a command intended for this shell - not for the database.
*/
static void processMetaCommand(Shell *p){
  char *z;
  int n;
................................................................................
  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"       },
    { "set",        shellSet,         ".set FLAG"           },
    { "clear",      shellClear,       ".clear FLAG"         },
    { "breakpoint", shellBreakpoint,  ".breakpoint"         },
  };

  /* Remove trailing whitespace from the command */
  z = xjd1StringText(&p->inBuf);
  if( p->shellFlags & SHELL_ECHO ){
    fprintf(stdout, "%s", z);
  }
  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;
................................................................................

/*
** Run a single statment.
*/
static void processOneStatement(Shell *p, const char *zCmd){
  xjd1_stmt *pStmt;
  int N, rc;
  if( p->shellFlags & SHELL_ECHO ){
    fprintf(stdout, "%s\n", zCmd);
  }
  xjd1_config(p->pDb, XJD1_CONFIG_PARSERTRACE, 
              (p->shellFlags & SHELL_PARSER_TRACE)!=0);
  rc = xjd1_stmt_new(p->pDb, zCmd, &pStmt, &N);
  if( rc==XJD1_OK ){
    if( p->shellFlags & SHELL_CMD_TRACE ){
      char *zTrace = xjd1_stmt_debug_listing(pStmt);
      if( zTrace ) printf("%s", zTrace);
      free(zTrace);
    }
    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);