Home | History | Annotate | Download | only in dist
      1 /*
      2 ** 2001 September 15
      3 **
      4 ** The author disclaims copyright to this source code.  In place of
      5 ** a legal notice, here is a blessing:
      6 **
      7 **    May you do good and not evil.
      8 **    May you find forgiveness for yourself and forgive others.
      9 **    May you share freely, never taking more than you give.
     10 **
     11 *************************************************************************
     12 ** This file contains code to implement the "sqlite" command line
     13 ** utility for accessing SQLite databases.
     14 */
     15 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
     16 /* This needs to come before any includes for MSVC compiler */
     17 #define _CRT_SECURE_NO_WARNINGS
     18 #endif
     19 
     20 /*
     21 ** If requested, include the SQLite compiler options file for MSVC.
     22 */
     23 #if defined(INCLUDE_MSVC_H)
     24 #include "msvc.h"
     25 #endif
     26 
     27 /*
     28 ** No support for loadable extensions in VxWorks.
     29 */
     30 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
     31 # define SQLITE_OMIT_LOAD_EXTENSION 1
     32 #endif
     33 
     34 /*
     35 ** Enable large-file support for fopen() and friends on unix.
     36 */
     37 #ifndef SQLITE_DISABLE_LFS
     38 # define _LARGE_FILE       1
     39 # ifndef _FILE_OFFSET_BITS
     40 #   define _FILE_OFFSET_BITS 64
     41 # endif
     42 # define _LARGEFILE_SOURCE 1
     43 #endif
     44 
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <stdio.h>
     48 #include <assert.h>
     49 #include "sqlite3.h"
     50 #if SQLITE_USER_AUTHENTICATION
     51 # include "sqlite3userauth.h"
     52 #endif
     53 #include <ctype.h>
     54 #include <stdarg.h>
     55 // Begin Android Add
     56 #ifndef NO_ANDROID_FUNCS
     57 #include "IcuUtils.h"
     58 #include <sqlite3_android.h>
     59 #endif
     60 // End Android Add
     61 
     62 #if !defined(_WIN32) && !defined(WIN32)
     63 # include <signal.h>
     64 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
     65 #  include <pwd.h>
     66 # endif
     67 # include <unistd.h>
     68 # include <sys/types.h>
     69 #endif
     70 
     71 #if HAVE_READLINE
     72 # include <readline/readline.h>
     73 # include <readline/history.h>
     74 #endif
     75 
     76 #if HAVE_EDITLINE
     77 # include <editline/readline.h>
     78 #endif
     79 
     80 #if HAVE_EDITLINE || HAVE_READLINE
     81 
     82 # define shell_add_history(X) add_history(X)
     83 # define shell_read_history(X) read_history(X)
     84 # define shell_write_history(X) write_history(X)
     85 # define shell_stifle_history(X) stifle_history(X)
     86 # define shell_readline(X) readline(X)
     87 
     88 #elif HAVE_LINENOISE
     89 
     90 # include "linenoise.h"
     91 # define shell_add_history(X) linenoiseHistoryAdd(X)
     92 # define shell_read_history(X) linenoiseHistoryLoad(X)
     93 # define shell_write_history(X) linenoiseHistorySave(X)
     94 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
     95 # define shell_readline(X) linenoise(X)
     96 
     97 #else
     98 
     99 # define shell_read_history(X)
    100 # define shell_write_history(X)
    101 # define shell_stifle_history(X)
    102 
    103 # define SHELL_USE_LOCAL_GETLINE 1
    104 #endif
    105 
    106 
    107 #if defined(_WIN32) || defined(WIN32)
    108 # include <io.h>
    109 # include <fcntl.h>
    110 # define isatty(h) _isatty(h)
    111 # ifndef access
    112 #  define access(f,m) _access((f),(m))
    113 # endif
    114 # undef popen
    115 # define popen _popen
    116 # undef pclose
    117 # define pclose _pclose
    118 #else
    119  /* Make sure isatty() has a prototype. */
    120  extern int isatty(int);
    121 
    122 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
    123   /* popen and pclose are not C89 functions and so are
    124   ** sometimes omitted from the <stdio.h> header */
    125    extern FILE *popen(const char*,const char*);
    126    extern int pclose(FILE*);
    127 # else
    128 #  define SQLITE_OMIT_POPEN 1
    129 # endif
    130 #endif
    131 
    132 #if defined(_WIN32_WCE)
    133 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
    134  * thus we always assume that we have a console. That can be
    135  * overridden with the -batch command line option.
    136  */
    137 #define isatty(x) 1
    138 #endif
    139 
    140 /* ctype macros that work with signed characters */
    141 #define IsSpace(X)  isspace((unsigned char)X)
    142 #define IsDigit(X)  isdigit((unsigned char)X)
    143 #define ToLower(X)  (char)tolower((unsigned char)X)
    144 
    145 /* On Windows, we normally run with output mode of TEXT so that \n characters
    146 ** are automatically translated into \r\n.  However, this behavior needs
    147 ** to be disabled in some cases (ex: when generating CSV output and when
    148 ** rendering quoted strings that contain \n characters).  The following
    149 ** routines take care of that.
    150 */
    151 #if defined(_WIN32) || defined(WIN32)
    152 static void setBinaryMode(FILE *out){
    153   fflush(out);
    154   _setmode(_fileno(out), _O_BINARY);
    155 }
    156 static void setTextMode(FILE *out){
    157   fflush(out);
    158   _setmode(_fileno(out), _O_TEXT);
    159 }
    160 #else
    161 # define setBinaryMode(X)
    162 # define setTextMode(X)
    163 #endif
    164 
    165 
    166 /* True if the timer is enabled */
    167 static int enableTimer = 0;
    168 
    169 /* Return the current wall-clock time */
    170 static sqlite3_int64 timeOfDay(void){
    171   static sqlite3_vfs *clockVfs = 0;
    172   sqlite3_int64 t;
    173   if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
    174   if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
    175     clockVfs->xCurrentTimeInt64(clockVfs, &t);
    176   }else{
    177     double r;
    178     clockVfs->xCurrentTime(clockVfs, &r);
    179     t = (sqlite3_int64)(r*86400000.0);
    180   }
    181   return t;
    182 }
    183 
    184 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
    185 #include <sys/time.h>
    186 #include <sys/resource.h>
    187 
    188 /* VxWorks does not support getrusage() as far as we can determine */
    189 #if defined(_WRS_KERNEL) || defined(__RTP__)
    190 struct rusage {
    191   struct timeval ru_utime; /* user CPU time used */
    192   struct timeval ru_stime; /* system CPU time used */
    193 };
    194 #define getrusage(A,B) memset(B,0,sizeof(*B))
    195 #endif
    196 
    197 /* Saved resource information for the beginning of an operation */
    198 static struct rusage sBegin;  /* CPU time at start */
    199 static sqlite3_int64 iBegin;  /* Wall-clock time at start */
    200 
    201 /*
    202 ** Begin timing an operation
    203 */
    204 static void beginTimer(void){
    205   if( enableTimer ){
    206     getrusage(RUSAGE_SELF, &sBegin);
    207     iBegin = timeOfDay();
    208   }
    209 }
    210 
    211 /* Return the difference of two time_structs in seconds */
    212 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
    213   return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
    214          (double)(pEnd->tv_sec - pStart->tv_sec);
    215 }
    216 
    217 /*
    218 ** Print the timing results.
    219 */
    220 static void endTimer(void){
    221   if( enableTimer ){
    222     sqlite3_int64 iEnd = timeOfDay();
    223     struct rusage sEnd;
    224     getrusage(RUSAGE_SELF, &sEnd);
    225     printf("Run Time: real %.3f user %f sys %f\n",
    226        (iEnd - iBegin)*0.001,
    227        timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
    228        timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
    229   }
    230 }
    231 
    232 #define BEGIN_TIMER beginTimer()
    233 #define END_TIMER endTimer()
    234 #define HAS_TIMER 1
    235 
    236 #elif (defined(_WIN32) || defined(WIN32))
    237 
    238 #include <windows.h>
    239 
    240 /* Saved resource information for the beginning of an operation */
    241 static HANDLE hProcess;
    242 static FILETIME ftKernelBegin;
    243 static FILETIME ftUserBegin;
    244 static sqlite3_int64 ftWallBegin;
    245 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
    246                                     LPFILETIME, LPFILETIME);
    247 static GETPROCTIMES getProcessTimesAddr = NULL;
    248 
    249 /*
    250 ** Check to see if we have timer support.  Return 1 if necessary
    251 ** support found (or found previously).
    252 */
    253 static int hasTimer(void){
    254   if( getProcessTimesAddr ){
    255     return 1;
    256   } else {
    257     /* GetProcessTimes() isn't supported in WIN95 and some other Windows
    258     ** versions. See if the version we are running on has it, and if it
    259     ** does, save off a pointer to it and the current process handle.
    260     */
    261     hProcess = GetCurrentProcess();
    262     if( hProcess ){
    263       HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
    264       if( NULL != hinstLib ){
    265         getProcessTimesAddr =
    266             (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
    267         if( NULL != getProcessTimesAddr ){
    268           return 1;
    269         }
    270         FreeLibrary(hinstLib);
    271       }
    272     }
    273   }
    274   return 0;
    275 }
    276 
    277 /*
    278 ** Begin timing an operation
    279 */
    280 static void beginTimer(void){
    281   if( enableTimer && getProcessTimesAddr ){
    282     FILETIME ftCreation, ftExit;
    283     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
    284                         &ftKernelBegin,&ftUserBegin);
    285     ftWallBegin = timeOfDay();
    286   }
    287 }
    288 
    289 /* Return the difference of two FILETIME structs in seconds */
    290 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
    291   sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
    292   sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
    293   return (double) ((i64End - i64Start) / 10000000.0);
    294 }
    295 
    296 /*
    297 ** Print the timing results.
    298 */
    299 static void endTimer(void){
    300   if( enableTimer && getProcessTimesAddr){
    301     FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
    302     sqlite3_int64 ftWallEnd = timeOfDay();
    303     getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
    304     printf("Run Time: real %.3f user %f sys %f\n",
    305        (ftWallEnd - ftWallBegin)*0.001,
    306        timeDiff(&ftUserBegin, &ftUserEnd),
    307        timeDiff(&ftKernelBegin, &ftKernelEnd));
    308   }
    309 }
    310 
    311 #define BEGIN_TIMER beginTimer()
    312 #define END_TIMER endTimer()
    313 #define HAS_TIMER hasTimer()
    314 
    315 #else
    316 #define BEGIN_TIMER
    317 #define END_TIMER
    318 #define HAS_TIMER 0
    319 #endif
    320 
    321 /*
    322 ** Used to prevent warnings about unused parameters
    323 */
    324 #define UNUSED_PARAMETER(x) (void)(x)
    325 
    326 /*
    327 ** If the following flag is set, then command execution stops
    328 ** at an error if we are not interactive.
    329 */
    330 static int bail_on_error = 0;
    331 
    332 /*
    333 ** Threat stdin as an interactive input if the following variable
    334 ** is true.  Otherwise, assume stdin is connected to a file or pipe.
    335 */
    336 static int stdin_is_interactive = 1;
    337 
    338 /*
    339 ** The following is the open SQLite database.  We make a pointer
    340 ** to this database a static variable so that it can be accessed
    341 ** by the SIGINT handler to interrupt database processing.
    342 */
    343 static sqlite3 *globalDb = 0;
    344 
    345 /*
    346 ** True if an interrupt (Control-C) has been received.
    347 */
    348 static volatile int seenInterrupt = 0;
    349 
    350 /*
    351 ** This is the name of our program. It is set in main(), used
    352 ** in a number of other places, mostly for error messages.
    353 */
    354 static char *Argv0;
    355 
    356 /*
    357 ** Prompt strings. Initialized in main. Settable with
    358 **   .prompt main continue
    359 */
    360 static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
    361 static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
    362 
    363 /*
    364 ** Write I/O traces to the following stream.
    365 */
    366 #ifdef SQLITE_ENABLE_IOTRACE
    367 static FILE *iotrace = 0;
    368 #endif
    369 
    370 /*
    371 ** This routine works like printf in that its first argument is a
    372 ** format string and subsequent arguments are values to be substituted
    373 ** in place of % fields.  The result of formatting this string
    374 ** is written to iotrace.
    375 */
    376 #ifdef SQLITE_ENABLE_IOTRACE
    377 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
    378   va_list ap;
    379   char *z;
    380   if( iotrace==0 ) return;
    381   va_start(ap, zFormat);
    382   z = sqlite3_vmprintf(zFormat, ap);
    383   va_end(ap);
    384   fprintf(iotrace, "%s", z);
    385   sqlite3_free(z);
    386 }
    387 #endif
    388 
    389 
    390 /*
    391 ** Determines if a string is a number of not.
    392 */
    393 static int isNumber(const char *z, int *realnum){
    394   if( *z=='-' || *z=='+' ) z++;
    395   if( !IsDigit(*z) ){
    396     return 0;
    397   }
    398   z++;
    399   if( realnum ) *realnum = 0;
    400   while( IsDigit(*z) ){ z++; }
    401   if( *z=='.' ){
    402     z++;
    403     if( !IsDigit(*z) ) return 0;
    404     while( IsDigit(*z) ){ z++; }
    405     if( realnum ) *realnum = 1;
    406   }
    407   if( *z=='e' || *z=='E' ){
    408     z++;
    409     if( *z=='+' || *z=='-' ) z++;
    410     if( !IsDigit(*z) ) return 0;
    411     while( IsDigit(*z) ){ z++; }
    412     if( realnum ) *realnum = 1;
    413   }
    414   return *z==0;
    415 }
    416 
    417 /*
    418 ** A global char* and an SQL function to access its current value
    419 ** from within an SQL statement. This program used to use the
    420 ** sqlite_exec_printf() API to substitue a string into an SQL statement.
    421 ** The correct way to do this with sqlite3 is to use the bind API, but
    422 ** since the shell is built around the callback paradigm it would be a lot
    423 ** of work. Instead just use this hack, which is quite harmless.
    424 */
    425 static const char *zShellStatic = 0;
    426 static void shellstaticFunc(
    427   sqlite3_context *context,
    428   int argc,
    429   sqlite3_value **argv
    430 ){
    431   assert( 0==argc );
    432   assert( zShellStatic );
    433   UNUSED_PARAMETER(argc);
    434   UNUSED_PARAMETER(argv);
    435   sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
    436 }
    437 
    438 
    439 /*
    440 ** This routine reads a line of text from FILE in, stores
    441 ** the text in memory obtained from malloc() and returns a pointer
    442 ** to the text.  NULL is returned at end of file, or if malloc()
    443 ** fails.
    444 **
    445 ** If zLine is not NULL then it is a malloced buffer returned from
    446 ** a previous call to this routine that may be reused.
    447 */
    448 static char *local_getline(char *zLine, FILE *in){
    449   int nLine = zLine==0 ? 0 : 100;
    450   int n = 0;
    451 
    452   while( 1 ){
    453     if( n+100>nLine ){
    454       nLine = nLine*2 + 100;
    455       zLine = realloc(zLine, nLine);
    456       if( zLine==0 ) return 0;
    457     }
    458     if( fgets(&zLine[n], nLine - n, in)==0 ){
    459       if( n==0 ){
    460         free(zLine);
    461         return 0;
    462       }
    463       zLine[n] = 0;
    464       break;
    465     }
    466     while( zLine[n] ) n++;
    467     if( n>0 && zLine[n-1]=='\n' ){
    468       n--;
    469       if( n>0 && zLine[n-1]=='\r' ) n--;
    470       zLine[n] = 0;
    471       break;
    472     }
    473   }
    474   return zLine;
    475 }
    476 
    477 /*
    478 ** Retrieve a single line of input text.
    479 **
    480 ** If in==0 then read from standard input and prompt before each line.
    481 ** If isContinuation is true, then a continuation prompt is appropriate.
    482 ** If isContinuation is zero, then the main prompt should be used.
    483 **
    484 ** If zPrior is not NULL then it is a buffer from a prior call to this
    485 ** routine that can be reused.
    486 **
    487 ** The result is stored in space obtained from malloc() and must either
    488 ** be freed by the caller or else passed back into this routine via the
    489 ** zPrior argument for reuse.
    490 */
    491 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
    492   char *zPrompt;
    493   char *zResult;
    494   if( in!=0 ){
    495     zResult = local_getline(zPrior, in);
    496   }else{
    497     zPrompt = isContinuation ? continuePrompt : mainPrompt;
    498 #if SHELL_USE_LOCAL_GETLINE
    499     printf("%s", zPrompt);
    500     fflush(stdout);
    501     zResult = local_getline(zPrior, stdin);
    502 #else
    503     free(zPrior);
    504     zResult = shell_readline(zPrompt);
    505     if( zResult && *zResult ) shell_add_history(zResult);
    506 #endif
    507   }
    508   return zResult;
    509 }
    510 
    511 /*
    512 ** Shell output mode information from before ".explain on",
    513 ** saved so that it can be restored by ".explain off"
    514 */
    515 typedef struct SavedModeInfo SavedModeInfo;
    516 struct SavedModeInfo {
    517   int valid;          /* Is there legit data in here? */
    518   int mode;           /* Mode prior to ".explain on" */
    519   int showHeader;     /* The ".header" setting prior to ".explain on" */
    520   int colWidth[100];  /* Column widths prior to ".explain on" */
    521 };
    522 
    523 /*
    524 ** State information about the database connection is contained in an
    525 ** instance of the following structure.
    526 */
    527 typedef struct ShellState ShellState;
    528 struct ShellState {
    529   sqlite3 *db;           /* The database */
    530   int echoOn;            /* True to echo input commands */
    531   int autoEQP;           /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
    532   int statsOn;           /* True to display memory stats before each finalize */
    533   int scanstatsOn;       /* True to display scan stats before each finalize */
    534   int backslashOn;       /* Resolve C-style \x escapes in SQL input text */
    535   int outCount;          /* Revert to stdout when reaching zero */
    536   int cnt;               /* Number of records displayed so far */
    537   FILE *out;             /* Write results here */
    538   FILE *traceOut;        /* Output for sqlite3_trace() */
    539   int nErr;              /* Number of errors seen */
    540   int mode;              /* An output mode setting */
    541   int writableSchema;    /* True if PRAGMA writable_schema=ON */
    542   int showHeader;        /* True to show column names in List or Column mode */
    543   unsigned shellFlgs;    /* Various flags */
    544   char *zDestTable;      /* Name of destination table when MODE_Insert */
    545   char colSeparator[20]; /* Column separator character for several modes */
    546   char rowSeparator[20]; /* Row separator character for MODE_Ascii */
    547   int colWidth[100];     /* Requested width of each column when in column mode*/
    548   int actualWidth[100];  /* Actual width of each column */
    549   char nullValue[20];    /* The text to print when a NULL comes back from
    550                          ** the database */
    551   SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
    552   char outfile[FILENAME_MAX]; /* Filename for *out */
    553   const char *zDbFilename;    /* name of the database file */
    554   char *zFreeOnClose;         /* Filename to free when closing */
    555   const char *zVfs;           /* Name of VFS to use */
    556   sqlite3_stmt *pStmt;   /* Current statement if any. */
    557   FILE *pLog;            /* Write log output here */
    558   int *aiIndent;         /* Array of indents used in MODE_Explain */
    559   int nIndent;           /* Size of array aiIndent[] */
    560   int iIndent;           /* Index of current op in aiIndent[] */
    561 };
    562 
    563 /*
    564 ** These are the allowed shellFlgs values
    565 */
    566 #define SHFLG_Scratch     0x00001     /* The --scratch option is used */
    567 #define SHFLG_Pagecache   0x00002     /* The --pagecache option is used */
    568 #define SHFLG_Lookaside   0x00004     /* Lookaside memory is used */
    569 
    570 /*
    571 ** These are the allowed modes.
    572 */
    573 #define MODE_Line     0  /* One column per line.  Blank line between records */
    574 #define MODE_Column   1  /* One record per line in neat columns */
    575 #define MODE_List     2  /* One record per line with a separator */
    576 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
    577 #define MODE_Html     4  /* Generate an XHTML table */
    578 #define MODE_Insert   5  /* Generate SQL "insert" statements */
    579 #define MODE_Tcl      6  /* Generate ANSI-C or TCL quoted elements */
    580 #define MODE_Csv      7  /* Quote strings, numbers are plain */
    581 #define MODE_Explain  8  /* Like MODE_Column, but do not truncate data */
    582 #define MODE_Ascii    9  /* Use ASCII unit and record separators (0x1F/0x1E) */
    583 
    584 static const char *modeDescr[] = {
    585   "line",
    586   "column",
    587   "list",
    588   "semi",
    589   "html",
    590   "insert",
    591   "tcl",
    592   "csv",
    593   "explain",
    594   "ascii",
    595 };
    596 
    597 /*
    598 ** These are the column/row/line separators used by the various
    599 ** import/export modes.
    600 */
    601 #define SEP_Column    "|"
    602 #define SEP_Row       "\n"
    603 #define SEP_Tab       "\t"
    604 #define SEP_Space     " "
    605 #define SEP_Comma     ","
    606 #define SEP_CrLf      "\r\n"
    607 #define SEP_Unit      "\x1F"
    608 #define SEP_Record    "\x1E"
    609 
    610 /*
    611 ** Number of elements in an array
    612 */
    613 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
    614 
    615 /*
    616 ** Compute a string length that is limited to what can be stored in
    617 ** lower 30 bits of a 32-bit signed integer.
    618 */
    619 static int strlen30(const char *z){
    620   const char *z2 = z;
    621   while( *z2 ){ z2++; }
    622   return 0x3fffffff & (int)(z2 - z);
    623 }
    624 
    625 /*
    626 ** A callback for the sqlite3_log() interface.
    627 */
    628 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
    629   ShellState *p = (ShellState*)pArg;
    630   if( p->pLog==0 ) return;
    631   fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
    632   fflush(p->pLog);
    633 }
    634 
    635 /*
    636 ** Output the given string as a hex-encoded blob (eg. X'1234' )
    637 */
    638 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
    639   int i;
    640   char *zBlob = (char *)pBlob;
    641   fprintf(out,"X'");
    642   for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]&0xff); }
    643   fprintf(out,"'");
    644 }
    645 
    646 /*
    647 ** Output the given string as a quoted string using SQL quoting conventions.
    648 */
    649 static void output_quoted_string(FILE *out, const char *z){
    650   int i;
    651   int nSingle = 0;
    652   setBinaryMode(out);
    653   for(i=0; z[i]; i++){
    654     if( z[i]=='\'' ) nSingle++;
    655   }
    656   if( nSingle==0 ){
    657     fprintf(out,"'%s'",z);
    658   }else{
    659     fprintf(out,"'");
    660     while( *z ){
    661       for(i=0; z[i] && z[i]!='\''; i++){}
    662       if( i==0 ){
    663         fprintf(out,"''");
    664         z++;
    665       }else if( z[i]=='\'' ){
    666         fprintf(out,"%.*s''",i,z);
    667         z += i+1;
    668       }else{
    669         fprintf(out,"%s",z);
    670         break;
    671       }
    672     }
    673     fprintf(out,"'");
    674   }
    675   setTextMode(out);
    676 }
    677 
    678 /*
    679 ** Output the given string as a quoted according to C or TCL quoting rules.
    680 */
    681 static void output_c_string(FILE *out, const char *z){
    682   unsigned int c;
    683   fputc('"', out);
    684   while( (c = *(z++))!=0 ){
    685     if( c=='\\' ){
    686       fputc(c, out);
    687       fputc(c, out);
    688     }else if( c=='"' ){
    689       fputc('\\', out);
    690       fputc('"', out);
    691     }else if( c=='\t' ){
    692       fputc('\\', out);
    693       fputc('t', out);
    694     }else if( c=='\n' ){
    695       fputc('\\', out);
    696       fputc('n', out);
    697     }else if( c=='\r' ){
    698       fputc('\\', out);
    699       fputc('r', out);
    700     }else if( !isprint(c&0xff) ){
    701       fprintf(out, "\\%03o", c&0xff);
    702     }else{
    703       fputc(c, out);
    704     }
    705   }
    706   fputc('"', out);
    707 }
    708 
    709 /*
    710 ** Output the given string with characters that are special to
    711 ** HTML escaped.
    712 */
    713 static void output_html_string(FILE *out, const char *z){
    714   int i;
    715   if( z==0 ) z = "";
    716   while( *z ){
    717     for(i=0;   z[i]
    718             && z[i]!='<'
    719             && z[i]!='&'
    720             && z[i]!='>'
    721             && z[i]!='\"'
    722             && z[i]!='\'';
    723         i++){}
    724     if( i>0 ){
    725       fprintf(out,"%.*s",i,z);
    726     }
    727     if( z[i]=='<' ){
    728       fprintf(out,"&lt;");
    729     }else if( z[i]=='&' ){
    730       fprintf(out,"&amp;");
    731     }else if( z[i]=='>' ){
    732       fprintf(out,"&gt;");
    733     }else if( z[i]=='\"' ){
    734       fprintf(out,"&quot;");
    735     }else if( z[i]=='\'' ){
    736       fprintf(out,"&#39;");
    737     }else{
    738       break;
    739     }
    740     z += i + 1;
    741   }
    742 }
    743 
    744 /*
    745 ** If a field contains any character identified by a 1 in the following
    746 ** array, then the string must be quoted for CSV.
    747 */
    748 static const char needCsvQuote[] = {
    749   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    750   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    751   1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
    752   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
    753   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
    754   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
    755   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
    756   0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
    757   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    758   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    759   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    760   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    761   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    762   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    763   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    764   1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
    765 };
    766 
    767 /*
    768 ** Output a single term of CSV.  Actually, p->colSeparator is used for
    769 ** the separator, which may or may not be a comma.  p->nullValue is
    770 ** the null value.  Strings are quoted if necessary.  The separator
    771 ** is only issued if bSep is true.
    772 */
    773 static void output_csv(ShellState *p, const char *z, int bSep){
    774   FILE *out = p->out;
    775   if( z==0 ){
    776     fprintf(out,"%s",p->nullValue);
    777   }else{
    778     int i;
    779     int nSep = strlen30(p->colSeparator);
    780     for(i=0; z[i]; i++){
    781       if( needCsvQuote[((unsigned char*)z)[i]]
    782          || (z[i]==p->colSeparator[0] &&
    783              (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
    784         i = 0;
    785         break;
    786       }
    787     }
    788     if( i==0 ){
    789       putc('"', out);
    790       for(i=0; z[i]; i++){
    791         if( z[i]=='"' ) putc('"', out);
    792         putc(z[i], out);
    793       }
    794       putc('"', out);
    795     }else{
    796       fprintf(out, "%s", z);
    797     }
    798   }
    799   if( bSep ){
    800     fprintf(p->out, "%s", p->colSeparator);
    801   }
    802 }
    803 
    804 #ifdef SIGINT
    805 /*
    806 ** This routine runs when the user presses Ctrl-C
    807 */
    808 static void interrupt_handler(int NotUsed){
    809   UNUSED_PARAMETER(NotUsed);
    810   seenInterrupt++;
    811   if( seenInterrupt>2 ) exit(1);
    812   if( globalDb ) sqlite3_interrupt(globalDb);
    813 }
    814 #endif
    815 
    816 /*
    817 ** This is the callback routine that the shell
    818 ** invokes for each row of a query result.
    819 */
    820 static int shell_callback(
    821   void *pArg,
    822   int nArg,        /* Number of result columns */
    823   char **azArg,    /* Text of each result column */
    824   char **azCol,    /* Column names */
    825   int *aiType      /* Column types */
    826 ){
    827   int i;
    828   ShellState *p = (ShellState*)pArg;
    829 
    830   switch( p->mode ){
    831     case MODE_Line: {
    832       int w = 5;
    833       if( azArg==0 ) break;
    834       for(i=0; i<nArg; i++){
    835         int len = strlen30(azCol[i] ? azCol[i] : "");
    836         if( len>w ) w = len;
    837       }
    838       if( p->cnt++>0 ) fprintf(p->out, "%s", p->rowSeparator);
    839       for(i=0; i<nArg; i++){
    840         fprintf(p->out,"%*s = %s%s", w, azCol[i],
    841                 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
    842       }
    843       break;
    844     }
    845     case MODE_Explain:
    846     case MODE_Column: {
    847       if( p->cnt++==0 ){
    848         for(i=0; i<nArg; i++){
    849           int w, n;
    850           if( i<ArraySize(p->colWidth) ){
    851             w = p->colWidth[i];
    852           }else{
    853             w = 0;
    854           }
    855           if( w==0 ){
    856             w = strlen30(azCol[i] ? azCol[i] : "");
    857             if( w<10 ) w = 10;
    858             n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullValue);
    859             if( w<n ) w = n;
    860           }
    861           if( i<ArraySize(p->actualWidth) ){
    862             p->actualWidth[i] = w;
    863           }
    864           if( p->showHeader ){
    865             if( w<0 ){
    866               fprintf(p->out,"%*.*s%s",-w,-w,azCol[i],
    867                       i==nArg-1 ? p->rowSeparator : "  ");
    868             }else{
    869               fprintf(p->out,"%-*.*s%s",w,w,azCol[i],
    870                       i==nArg-1 ? p->rowSeparator : "  ");
    871             }
    872           }
    873         }
    874         if( p->showHeader ){
    875           for(i=0; i<nArg; i++){
    876             int w;
    877             if( i<ArraySize(p->actualWidth) ){
    878                w = p->actualWidth[i];
    879                if( w<0 ) w = -w;
    880             }else{
    881                w = 10;
    882             }
    883             fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
    884                    "----------------------------------------------------------",
    885                     i==nArg-1 ? p->rowSeparator : "  ");
    886           }
    887         }
    888       }
    889       if( azArg==0 ) break;
    890       for(i=0; i<nArg; i++){
    891         int w;
    892         if( i<ArraySize(p->actualWidth) ){
    893            w = p->actualWidth[i];
    894         }else{
    895            w = 10;
    896         }
    897         if( p->mode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
    898           w = strlen30(azArg[i]);
    899         }
    900         if( i==1 && p->aiIndent && p->pStmt ){
    901           if( p->iIndent<p->nIndent ){
    902             fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
    903           }
    904           p->iIndent++;
    905         }
    906         if( w<0 ){
    907           fprintf(p->out,"%*.*s%s",-w,-w,
    908               azArg[i] ? azArg[i] : p->nullValue,
    909               i==nArg-1 ? p->rowSeparator : "  ");
    910         }else{
    911           fprintf(p->out,"%-*.*s%s",w,w,
    912               azArg[i] ? azArg[i] : p->nullValue,
    913               i==nArg-1 ? p->rowSeparator : "  ");
    914         }
    915       }
    916       break;
    917     }
    918     case MODE_Semi:
    919     case MODE_List: {
    920       if( p->cnt++==0 && p->showHeader ){
    921         for(i=0; i<nArg; i++){
    922           fprintf(p->out,"%s%s",azCol[i],
    923                   i==nArg-1 ? p->rowSeparator : p->colSeparator);
    924         }
    925       }
    926       if( azArg==0 ) break;
    927       for(i=0; i<nArg; i++){
    928         char *z = azArg[i];
    929         if( z==0 ) z = p->nullValue;
    930         fprintf(p->out, "%s", z);
    931         if( i<nArg-1 ){
    932           fprintf(p->out, "%s", p->colSeparator);
    933         }else if( p->mode==MODE_Semi ){
    934           fprintf(p->out, ";%s", p->rowSeparator);
    935         }else{
    936           fprintf(p->out, "%s", p->rowSeparator);
    937         }
    938       }
    939       break;
    940     }
    941     case MODE_Html: {
    942       if( p->cnt++==0 && p->showHeader ){
    943         fprintf(p->out,"<TR>");
    944         for(i=0; i<nArg; i++){
    945           fprintf(p->out,"<TH>");
    946           output_html_string(p->out, azCol[i]);
    947           fprintf(p->out,"</TH>\n");
    948         }
    949         fprintf(p->out,"</TR>\n");
    950       }
    951       if( azArg==0 ) break;
    952       fprintf(p->out,"<TR>");
    953       for(i=0; i<nArg; i++){
    954         fprintf(p->out,"<TD>");
    955         output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
    956         fprintf(p->out,"</TD>\n");
    957       }
    958       fprintf(p->out,"</TR>\n");
    959       break;
    960     }
    961     case MODE_Tcl: {
    962       if( p->cnt++==0 && p->showHeader ){
    963         for(i=0; i<nArg; i++){
    964           output_c_string(p->out,azCol[i] ? azCol[i] : "");
    965           if(i<nArg-1) fprintf(p->out, "%s", p->colSeparator);
    966         }
    967         fprintf(p->out, "%s", p->rowSeparator);
    968       }
    969       if( azArg==0 ) break;
    970       for(i=0; i<nArg; i++){
    971         output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
    972         if(i<nArg-1) fprintf(p->out, "%s", p->colSeparator);
    973       }
    974       fprintf(p->out, "%s", p->rowSeparator);
    975       break;
    976     }
    977     case MODE_Csv: {
    978       setBinaryMode(p->out);
    979       if( p->cnt++==0 && p->showHeader ){
    980         for(i=0; i<nArg; i++){
    981           output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
    982         }
    983         fprintf(p->out, "%s", p->rowSeparator);
    984       }
    985       if( nArg>0 ){
    986         for(i=0; i<nArg; i++){
    987           output_csv(p, azArg[i], i<nArg-1);
    988         }
    989         fprintf(p->out, "%s", p->rowSeparator);
    990       }
    991       setTextMode(p->out);
    992       break;
    993     }
    994     case MODE_Insert: {
    995       p->cnt++;
    996       if( azArg==0 ) break;
    997       fprintf(p->out,"INSERT INTO %s",p->zDestTable);
    998       if( p->showHeader ){
    999         fprintf(p->out,"(");
   1000         for(i=0; i<nArg; i++){
   1001           char *zSep = i>0 ? ",": "";
   1002           fprintf(p->out, "%s%s", zSep, azCol[i]);
   1003         }
   1004         fprintf(p->out,")");
   1005       }
   1006       fprintf(p->out," VALUES(");
   1007       for(i=0; i<nArg; i++){
   1008         char *zSep = i>0 ? ",": "";
   1009         if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
   1010           fprintf(p->out,"%sNULL",zSep);
   1011         }else if( aiType && aiType[i]==SQLITE_TEXT ){
   1012           if( zSep[0] ) fprintf(p->out,"%s",zSep);
   1013           output_quoted_string(p->out, azArg[i]);
   1014         }else if( aiType && (aiType[i]==SQLITE_INTEGER
   1015                              || aiType[i]==SQLITE_FLOAT) ){
   1016           fprintf(p->out,"%s%s",zSep, azArg[i]);
   1017         }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
   1018           const void *pBlob = sqlite3_column_blob(p->pStmt, i);
   1019           int nBlob = sqlite3_column_bytes(p->pStmt, i);
   1020           if( zSep[0] ) fprintf(p->out,"%s",zSep);
   1021           output_hex_blob(p->out, pBlob, nBlob);
   1022         }else if( isNumber(azArg[i], 0) ){
   1023           fprintf(p->out,"%s%s",zSep, azArg[i]);
   1024         }else{
   1025           if( zSep[0] ) fprintf(p->out,"%s",zSep);
   1026           output_quoted_string(p->out, azArg[i]);
   1027         }
   1028       }
   1029       fprintf(p->out,");\n");
   1030       break;
   1031     }
   1032     case MODE_Ascii: {
   1033       if( p->cnt++==0 && p->showHeader ){
   1034         for(i=0; i<nArg; i++){
   1035           if( i>0 ) fprintf(p->out, "%s", p->colSeparator);
   1036           fprintf(p->out,"%s",azCol[i] ? azCol[i] : "");
   1037         }
   1038         fprintf(p->out, "%s", p->rowSeparator);
   1039       }
   1040       if( azArg==0 ) break;
   1041       for(i=0; i<nArg; i++){
   1042         if( i>0 ) fprintf(p->out, "%s", p->colSeparator);
   1043         fprintf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
   1044       }
   1045       fprintf(p->out, "%s", p->rowSeparator);
   1046       break;
   1047     }
   1048   }
   1049   return 0;
   1050 }
   1051 
   1052 /*
   1053 ** This is the callback routine that the SQLite library
   1054 ** invokes for each row of a query result.
   1055 */
   1056 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
   1057   /* since we don't have type info, call the shell_callback with a NULL value */
   1058   return shell_callback(pArg, nArg, azArg, azCol, NULL);
   1059 }
   1060 
   1061 /*
   1062 ** Set the destination table field of the ShellState structure to
   1063 ** the name of the table given.  Escape any quote characters in the
   1064 ** table name.
   1065 */
   1066 static void set_table_name(ShellState *p, const char *zName){
   1067   int i, n;
   1068   int needQuote;
   1069   char *z;
   1070 
   1071   if( p->zDestTable ){
   1072     free(p->zDestTable);
   1073     p->zDestTable = 0;
   1074   }
   1075   if( zName==0 ) return;
   1076   needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
   1077   for(i=n=0; zName[i]; i++, n++){
   1078     if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
   1079       needQuote = 1;
   1080       if( zName[i]=='\'' ) n++;
   1081     }
   1082   }
   1083   if( needQuote ) n += 2;
   1084   z = p->zDestTable = malloc( n+1 );
   1085   if( z==0 ){
   1086     fprintf(stderr,"Error: out of memory\n");
   1087     exit(1);
   1088   }
   1089   n = 0;
   1090   if( needQuote ) z[n++] = '\'';
   1091   for(i=0; zName[i]; i++){
   1092     z[n++] = zName[i];
   1093     if( zName[i]=='\'' ) z[n++] = '\'';
   1094   }
   1095   if( needQuote ) z[n++] = '\'';
   1096   z[n] = 0;
   1097 }
   1098 
   1099 /* zIn is either a pointer to a NULL-terminated string in memory obtained
   1100 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
   1101 ** added to zIn, and the result returned in memory obtained from malloc().
   1102 ** zIn, if it was not NULL, is freed.
   1103 **
   1104 ** If the third argument, quote, is not '\0', then it is used as a
   1105 ** quote character for zAppend.
   1106 */
   1107 static char *appendText(char *zIn, char const *zAppend, char quote){
   1108   int len;
   1109   int i;
   1110   int nAppend = strlen30(zAppend);
   1111   int nIn = (zIn?strlen30(zIn):0);
   1112 
   1113   len = nAppend+nIn+1;
   1114   if( quote ){
   1115     len += 2;
   1116     for(i=0; i<nAppend; i++){
   1117       if( zAppend[i]==quote ) len++;
   1118     }
   1119   }
   1120 
   1121   zIn = (char *)realloc(zIn, len);
   1122   if( !zIn ){
   1123     return 0;
   1124   }
   1125 
   1126   if( quote ){
   1127     char *zCsr = &zIn[nIn];
   1128     *zCsr++ = quote;
   1129     for(i=0; i<nAppend; i++){
   1130       *zCsr++ = zAppend[i];
   1131       if( zAppend[i]==quote ) *zCsr++ = quote;
   1132     }
   1133     *zCsr++ = quote;
   1134     *zCsr++ = '\0';
   1135     assert( (zCsr-zIn)==len );
   1136   }else{
   1137     memcpy(&zIn[nIn], zAppend, nAppend);
   1138     zIn[len-1] = '\0';
   1139   }
   1140 
   1141   return zIn;
   1142 }
   1143 
   1144 
   1145 /*
   1146 ** Execute a query statement that will generate SQL output.  Print
   1147 ** the result columns, comma-separated, on a line and then add a
   1148 ** semicolon terminator to the end of that line.
   1149 **
   1150 ** If the number of columns is 1 and that column contains text "--"
   1151 ** then write the semicolon on a separate line.  That way, if a
   1152 ** "--" comment occurs at the end of the statement, the comment
   1153 ** won't consume the semicolon terminator.
   1154 */
   1155 static int run_table_dump_query(
   1156   ShellState *p,           /* Query context */
   1157   const char *zSelect,     /* SELECT statement to extract content */
   1158   const char *zFirstRow    /* Print before first row, if not NULL */
   1159 ){
   1160   sqlite3_stmt *pSelect;
   1161   int rc;
   1162   int nResult;
   1163   int i;
   1164   const char *z;
   1165   rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
   1166   if( rc!=SQLITE_OK || !pSelect ){
   1167     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
   1168     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
   1169     return rc;
   1170   }
   1171   rc = sqlite3_step(pSelect);
   1172   nResult = sqlite3_column_count(pSelect);
   1173   while( rc==SQLITE_ROW ){
   1174     if( zFirstRow ){
   1175       fprintf(p->out, "%s", zFirstRow);
   1176       zFirstRow = 0;
   1177     }
   1178     z = (const char*)sqlite3_column_text(pSelect, 0);
   1179     fprintf(p->out, "%s", z);
   1180     for(i=1; i<nResult; i++){
   1181       fprintf(p->out, ",%s", sqlite3_column_text(pSelect, i));
   1182     }
   1183     if( z==0 ) z = "";
   1184     while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
   1185     if( z[0] ){
   1186       fprintf(p->out, "\n;\n");
   1187     }else{
   1188       fprintf(p->out, ";\n");
   1189     }
   1190     rc = sqlite3_step(pSelect);
   1191   }
   1192   rc = sqlite3_finalize(pSelect);
   1193   if( rc!=SQLITE_OK ){
   1194     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
   1195     if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
   1196   }
   1197   return rc;
   1198 }
   1199 
   1200 /*
   1201 ** Allocate space and save off current error string.
   1202 */
   1203 static char *save_err_msg(
   1204   sqlite3 *db            /* Database to query */
   1205 ){
   1206   int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
   1207   char *zErrMsg = sqlite3_malloc64(nErrMsg);
   1208   if( zErrMsg ){
   1209     memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
   1210   }
   1211   return zErrMsg;
   1212 }
   1213 
   1214 /*
   1215 ** Display memory stats.
   1216 */
   1217 static int display_stats(
   1218   sqlite3 *db,                /* Database to query */
   1219   ShellState *pArg,           /* Pointer to ShellState */
   1220   int bReset                  /* True to reset the stats */
   1221 ){
   1222   int iCur;
   1223   int iHiwtr;
   1224 
   1225   if( pArg && pArg->out ){
   1226 
   1227     iHiwtr = iCur = -1;
   1228     sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
   1229     fprintf(pArg->out,
   1230             "Memory Used:                         %d (max %d) bytes\n",
   1231             iCur, iHiwtr);
   1232     iHiwtr = iCur = -1;
   1233     sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
   1234     fprintf(pArg->out, "Number of Outstanding Allocations:   %d (max %d)\n",
   1235             iCur, iHiwtr);
   1236     if( pArg->shellFlgs & SHFLG_Pagecache ){
   1237       iHiwtr = iCur = -1;
   1238       sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
   1239       fprintf(pArg->out,
   1240               "Number of Pcache Pages Used:         %d (max %d) pages\n",
   1241               iCur, iHiwtr);
   1242     }
   1243     iHiwtr = iCur = -1;
   1244     sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
   1245     fprintf(pArg->out,
   1246             "Number of Pcache Overflow Bytes:     %d (max %d) bytes\n",
   1247             iCur, iHiwtr);
   1248     if( pArg->shellFlgs & SHFLG_Scratch ){
   1249       iHiwtr = iCur = -1;
   1250       sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
   1251       fprintf(pArg->out, "Number of Scratch Allocations Used:  %d (max %d)\n",
   1252               iCur, iHiwtr);
   1253     }
   1254     iHiwtr = iCur = -1;
   1255     sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
   1256     fprintf(pArg->out,
   1257             "Number of Scratch Overflow Bytes:    %d (max %d) bytes\n",
   1258             iCur, iHiwtr);
   1259     iHiwtr = iCur = -1;
   1260     sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
   1261     fprintf(pArg->out, "Largest Allocation:                  %d bytes\n",
   1262             iHiwtr);
   1263     iHiwtr = iCur = -1;
   1264     sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
   1265     fprintf(pArg->out, "Largest Pcache Allocation:           %d bytes\n",
   1266             iHiwtr);
   1267     iHiwtr = iCur = -1;
   1268     sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
   1269     fprintf(pArg->out, "Largest Scratch Allocation:          %d bytes\n",
   1270             iHiwtr);
   1271 #ifdef YYTRACKMAXSTACKDEPTH
   1272     iHiwtr = iCur = -1;
   1273     sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
   1274     fprintf(pArg->out, "Deepest Parser Stack:                %d (max %d)\n",
   1275             iCur, iHiwtr);
   1276 #endif
   1277   }
   1278 
   1279   if( pArg && pArg->out && db ){
   1280     if( pArg->shellFlgs & SHFLG_Lookaside ){
   1281       iHiwtr = iCur = -1;
   1282       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
   1283                         &iCur, &iHiwtr, bReset);
   1284       fprintf(pArg->out, "Lookaside Slots Used:                %d (max %d)\n",
   1285               iCur, iHiwtr);
   1286       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
   1287                         &iCur, &iHiwtr, bReset);
   1288       fprintf(pArg->out, "Successful lookaside attempts:       %d\n", iHiwtr);
   1289       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
   1290                         &iCur, &iHiwtr, bReset);
   1291       fprintf(pArg->out, "Lookaside failures due to size:      %d\n", iHiwtr);
   1292       sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
   1293                         &iCur, &iHiwtr, bReset);
   1294       fprintf(pArg->out, "Lookaside failures due to OOM:       %d\n", iHiwtr);
   1295     }
   1296     iHiwtr = iCur = -1;
   1297     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
   1298     fprintf(pArg->out, "Pager Heap Usage:                    %d bytes\n",iCur);
   1299     iHiwtr = iCur = -1;
   1300     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
   1301     fprintf(pArg->out, "Page cache hits:                     %d\n", iCur);
   1302     iHiwtr = iCur = -1;
   1303     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
   1304     fprintf(pArg->out, "Page cache misses:                   %d\n", iCur);
   1305     iHiwtr = iCur = -1;
   1306     sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
   1307     fprintf(pArg->out, "Page cache writes:                   %d\n", iCur);
   1308     iHiwtr = iCur = -1;
   1309     sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
   1310     fprintf(pArg->out, "Schema Heap Usage:                   %d bytes\n",iCur);
   1311     iHiwtr = iCur = -1;
   1312     sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
   1313     fprintf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",iCur);
   1314   }
   1315 
   1316   if( pArg && pArg->out && db && pArg->pStmt ){
   1317     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
   1318                                bReset);
   1319     fprintf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
   1320     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
   1321     fprintf(pArg->out, "Sort Operations:                     %d\n", iCur);
   1322     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
   1323     fprintf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
   1324     iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
   1325     fprintf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
   1326   }
   1327 
   1328   /* Do not remove this machine readable comment: extra-stats-output-here */
   1329 
   1330   return 0;
   1331 }
   1332 
   1333 /*
   1334 ** Display scan stats.
   1335 */
   1336 static void display_scanstats(
   1337   sqlite3 *db,                    /* Database to query */
   1338   ShellState *pArg                /* Pointer to ShellState */
   1339 ){
   1340 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
   1341   UNUSED_PARAMETER(db);
   1342   UNUSED_PARAMETER(pArg);
   1343 #else
   1344   int i, k, n, mx;
   1345   fprintf(pArg->out, "-------- scanstats --------\n");
   1346   mx = 0;
   1347   for(k=0; k<=mx; k++){
   1348     double rEstLoop = 1.0;
   1349     for(i=n=0; 1; i++){
   1350       sqlite3_stmt *p = pArg->pStmt;
   1351       sqlite3_int64 nLoop, nVisit;
   1352       double rEst;
   1353       int iSid;
   1354       const char *zExplain;
   1355       if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
   1356         break;
   1357       }
   1358       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
   1359       if( iSid>mx ) mx = iSid;
   1360       if( iSid!=k ) continue;
   1361       if( n==0 ){
   1362         rEstLoop = (double)nLoop;
   1363         if( k>0 ) fprintf(pArg->out, "-------- subquery %d -------\n", k);
   1364       }
   1365       n++;
   1366       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
   1367       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
   1368       sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
   1369       fprintf(pArg->out, "Loop %2d: %s\n", n, zExplain);
   1370       rEstLoop *= rEst;
   1371       fprintf(pArg->out,
   1372           "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
   1373           nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
   1374       );
   1375     }
   1376   }
   1377   fprintf(pArg->out, "---------------------------\n");
   1378 #endif
   1379 }
   1380 
   1381 /*
   1382 ** Parameter azArray points to a zero-terminated array of strings. zStr
   1383 ** points to a single nul-terminated string. Return non-zero if zStr
   1384 ** is equal, according to strcmp(), to any of the strings in the array.
   1385 ** Otherwise, return zero.
   1386 */
   1387 static int str_in_array(const char *zStr, const char **azArray){
   1388   int i;
   1389   for(i=0; azArray[i]; i++){
   1390     if( 0==strcmp(zStr, azArray[i]) ) return 1;
   1391   }
   1392   return 0;
   1393 }
   1394 
   1395 /*
   1396 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
   1397 ** and populate the ShellState.aiIndent[] array with the number of
   1398 ** spaces each opcode should be indented before it is output.
   1399 **
   1400 ** The indenting rules are:
   1401 **
   1402 **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
   1403 **       all opcodes that occur between the p2 jump destination and the opcode
   1404 **       itself by 2 spaces.
   1405 **
   1406 **     * For each "Goto", if the jump destination is earlier in the program
   1407 **       and ends on one of:
   1408 **          Yield  SeekGt  SeekLt  RowSetRead  Rewind
   1409 **       or if the P1 parameter is one instead of zero,
   1410 **       then indent all opcodes between the earlier instruction
   1411 **       and "Goto" by 2 spaces.
   1412 */
   1413 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
   1414   const char *zSql;               /* The text of the SQL statement */
   1415   const char *z;                  /* Used to check if this is an EXPLAIN */
   1416   int *abYield = 0;               /* True if op is an OP_Yield */
   1417   int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
   1418   int iOp;                        /* Index of operation in p->aiIndent[] */
   1419 
   1420   const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
   1421                            "NextIfOpen", "PrevIfOpen", 0 };
   1422   const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
   1423                             "Rewind", 0 };
   1424   const char *azGoto[] = { "Goto", 0 };
   1425 
   1426   /* Try to figure out if this is really an EXPLAIN statement. If this
   1427   ** cannot be verified, return early.  */
   1428   zSql = sqlite3_sql(pSql);
   1429   if( zSql==0 ) return;
   1430   for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
   1431   if( sqlite3_strnicmp(z, "explain", 7) ) return;
   1432 
   1433   for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
   1434     int i;
   1435     int iAddr = sqlite3_column_int(pSql, 0);
   1436     const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
   1437 
   1438     /* Set p2 to the P2 field of the current opcode. Then, assuming that
   1439     ** p2 is an instruction address, set variable p2op to the index of that
   1440     ** instruction in the aiIndent[] array. p2 and p2op may be different if
   1441     ** the current instruction is part of a sub-program generated by an
   1442     ** SQL trigger or foreign key.  */
   1443     int p2 = sqlite3_column_int(pSql, 3);
   1444     int p2op = (p2 + (iOp-iAddr));
   1445 
   1446     /* Grow the p->aiIndent array as required */
   1447     if( iOp>=nAlloc ){
   1448       nAlloc += 100;
   1449       p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
   1450       abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
   1451     }
   1452     abYield[iOp] = str_in_array(zOp, azYield);
   1453     p->aiIndent[iOp] = 0;
   1454     p->nIndent = iOp+1;
   1455 
   1456     if( str_in_array(zOp, azNext) ){
   1457       for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
   1458     }
   1459     if( str_in_array(zOp, azGoto) && p2op<p->nIndent
   1460      && (abYield[p2op] || sqlite3_column_int(pSql, 2))
   1461     ){
   1462       for(i=p2op+1; i<iOp; i++) p->aiIndent[i] += 2;
   1463     }
   1464   }
   1465 
   1466   p->iIndent = 0;
   1467   sqlite3_free(abYield);
   1468   sqlite3_reset(pSql);
   1469 }
   1470 
   1471 /*
   1472 ** Free the array allocated by explain_data_prepare().
   1473 */
   1474 static void explain_data_delete(ShellState *p){
   1475   sqlite3_free(p->aiIndent);
   1476   p->aiIndent = 0;
   1477   p->nIndent = 0;
   1478   p->iIndent = 0;
   1479 }
   1480 
   1481 /*
   1482 ** Execute a statement or set of statements.  Print
   1483 ** any result rows/columns depending on the current mode
   1484 ** set via the supplied callback.
   1485 **
   1486 ** This is very similar to SQLite's built-in sqlite3_exec()
   1487 ** function except it takes a slightly different callback
   1488 ** and callback data argument.
   1489 */
   1490 static int shell_exec(
   1491   sqlite3 *db,                              /* An open database */
   1492   const char *zSql,                         /* SQL to be evaluated */
   1493   int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
   1494                                             /* (not the same as sqlite3_exec) */
   1495   ShellState *pArg,                         /* Pointer to ShellState */
   1496   char **pzErrMsg                           /* Error msg written here */
   1497 ){
   1498   sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
   1499   int rc = SQLITE_OK;             /* Return Code */
   1500   int rc2;
   1501   const char *zLeftover;          /* Tail of unprocessed SQL */
   1502 
   1503   if( pzErrMsg ){
   1504     *pzErrMsg = NULL;
   1505   }
   1506 
   1507   while( zSql[0] && (SQLITE_OK == rc) ){
   1508     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
   1509     if( SQLITE_OK != rc ){
   1510       if( pzErrMsg ){
   1511         *pzErrMsg = save_err_msg(db);
   1512       }
   1513     }else{
   1514       if( !pStmt ){
   1515         /* this happens for a comment or white-space */
   1516         zSql = zLeftover;
   1517         while( IsSpace(zSql[0]) ) zSql++;
   1518         continue;
   1519       }
   1520 
   1521       /* save off the prepared statment handle and reset row count */
   1522       if( pArg ){
   1523         pArg->pStmt = pStmt;
   1524         pArg->cnt = 0;
   1525       }
   1526 
   1527       /* echo the sql statement if echo on */
   1528       if( pArg && pArg->echoOn ){
   1529         const char *zStmtSql = sqlite3_sql(pStmt);
   1530         fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
   1531       }
   1532 
   1533       /* Show the EXPLAIN QUERY PLAN if .eqp is on */
   1534       if( pArg && pArg->autoEQP ){
   1535         sqlite3_stmt *pExplain;
   1536         char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s",
   1537                                      sqlite3_sql(pStmt));
   1538         rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
   1539         if( rc==SQLITE_OK ){
   1540           while( sqlite3_step(pExplain)==SQLITE_ROW ){
   1541             fprintf(pArg->out,"--EQP-- %d,", sqlite3_column_int(pExplain, 0));
   1542             fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
   1543             fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
   1544             fprintf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
   1545           }
   1546         }
   1547         sqlite3_finalize(pExplain);
   1548         sqlite3_free(zEQP);
   1549       }
   1550 
   1551       /* If the shell is currently in ".explain" mode, gather the extra
   1552       ** data required to add indents to the output.*/
   1553       if( pArg && pArg->mode==MODE_Explain ){
   1554         explain_data_prepare(pArg, pStmt);
   1555       }
   1556 
   1557       /* perform the first step.  this will tell us if we
   1558       ** have a result set or not and how wide it is.
   1559       */
   1560       rc = sqlite3_step(pStmt);
   1561       /* if we have a result set... */
   1562       if( SQLITE_ROW == rc ){
   1563         /* if we have a callback... */
   1564         if( xCallback ){
   1565           /* allocate space for col name ptr, value ptr, and type */
   1566           int nCol = sqlite3_column_count(pStmt);
   1567           void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
   1568           if( !pData ){
   1569             rc = SQLITE_NOMEM;
   1570           }else{
   1571             char **azCols = (char **)pData;      /* Names of result columns */
   1572             char **azVals = &azCols[nCol];       /* Results */
   1573             int *aiTypes = (int *)&azVals[nCol]; /* Result types */
   1574             int i, x;
   1575             assert(sizeof(int) <= sizeof(char *));
   1576             /* save off ptrs to column names */
   1577             for(i=0; i<nCol; i++){
   1578               azCols[i] = (char *)sqlite3_column_name(pStmt, i);
   1579             }
   1580             do{
   1581               /* extract the data and data types */
   1582               for(i=0; i<nCol; i++){
   1583                 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
   1584                 if( x==SQLITE_BLOB && pArg && pArg->mode==MODE_Insert ){
   1585                   azVals[i] = "";
   1586                 }else{
   1587                   azVals[i] = (char*)sqlite3_column_text(pStmt, i);
   1588                 }
   1589                 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
   1590                   rc = SQLITE_NOMEM;
   1591                   break; /* from for */
   1592                 }
   1593               } /* end for */
   1594 
   1595               /* if data and types extracted successfully... */
   1596               if( SQLITE_ROW == rc ){
   1597                 /* call the supplied callback with the result row data */
   1598                 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
   1599                   rc = SQLITE_ABORT;
   1600                 }else{
   1601                   rc = sqlite3_step(pStmt);
   1602                 }
   1603               }
   1604             } while( SQLITE_ROW == rc );
   1605             sqlite3_free(pData);
   1606           }
   1607         }else{
   1608           do{
   1609             rc = sqlite3_step(pStmt);
   1610           } while( rc == SQLITE_ROW );
   1611         }
   1612       }
   1613 
   1614       explain_data_delete(pArg);
   1615 
   1616       /* print usage stats if stats on */
   1617       if( pArg && pArg->statsOn ){
   1618         display_stats(db, pArg, 0);
   1619       }
   1620 
   1621       /* print loop-counters if required */
   1622       if( pArg && pArg->scanstatsOn ){
   1623         display_scanstats(db, pArg);
   1624       }
   1625 
   1626       /* Finalize the statement just executed. If this fails, save a
   1627       ** copy of the error message. Otherwise, set zSql to point to the
   1628       ** next statement to execute. */
   1629       rc2 = sqlite3_finalize(pStmt);
   1630       if( rc!=SQLITE_NOMEM ) rc = rc2;
   1631       if( rc==SQLITE_OK ){
   1632         zSql = zLeftover;
   1633         while( IsSpace(zSql[0]) ) zSql++;
   1634       }else if( pzErrMsg ){
   1635         *pzErrMsg = save_err_msg(db);
   1636       }
   1637 
   1638       /* clear saved stmt handle */
   1639       if( pArg ){
   1640         pArg->pStmt = NULL;
   1641       }
   1642     }
   1643   } /* end while */
   1644 
   1645   return rc;
   1646 }
   1647 
   1648 
   1649 /*
   1650 ** This is a different callback routine used for dumping the database.
   1651 ** Each row received by this callback consists of a table name,
   1652 ** the table type ("index" or "table") and SQL to create the table.
   1653 ** This routine should print text sufficient to recreate the table.
   1654 */
   1655 static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
   1656   int rc;
   1657   const char *zTable;
   1658   const char *zType;
   1659   const char *zSql;
   1660   const char *zPrepStmt = 0;
   1661   ShellState *p = (ShellState *)pArg;
   1662 
   1663   UNUSED_PARAMETER(azCol);
   1664   if( nArg!=3 ) return 1;
   1665   zTable = azArg[0];
   1666   zType = azArg[1];
   1667   zSql = azArg[2];
   1668 
   1669   if( strcmp(zTable, "sqlite_sequence")==0 ){
   1670     zPrepStmt = "DELETE FROM sqlite_sequence;\n";
   1671   }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
   1672     fprintf(p->out, "ANALYZE sqlite_master;\n");
   1673   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
   1674     return 0;
   1675   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
   1676     char *zIns;
   1677     if( !p->writableSchema ){
   1678       fprintf(p->out, "PRAGMA writable_schema=ON;\n");
   1679       p->writableSchema = 1;
   1680     }
   1681     zIns = sqlite3_mprintf(
   1682        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
   1683        "VALUES('table','%q','%q',0,'%q');",
   1684        zTable, zTable, zSql);
   1685     fprintf(p->out, "%s\n", zIns);
   1686     sqlite3_free(zIns);
   1687     return 0;
   1688   }else{
   1689     fprintf(p->out, "%s;\n", zSql);
   1690   }
   1691 
   1692   if( strcmp(zType, "table")==0 ){
   1693     sqlite3_stmt *pTableInfo = 0;
   1694     char *zSelect = 0;
   1695     char *zTableInfo = 0;
   1696     char *zTmp = 0;
   1697     int nRow = 0;
   1698 
   1699     zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
   1700     zTableInfo = appendText(zTableInfo, zTable, '"');
   1701     zTableInfo = appendText(zTableInfo, ");", 0);
   1702 
   1703     rc = sqlite3_prepare_v2(p->db, zTableInfo, -1, &pTableInfo, 0);
   1704     free(zTableInfo);
   1705     if( rc!=SQLITE_OK || !pTableInfo ){
   1706       return 1;
   1707     }
   1708 
   1709     zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
   1710     /* Always quote the table name, even if it appears to be pure ascii,
   1711     ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
   1712     zTmp = appendText(zTmp, zTable, '"');
   1713     if( zTmp ){
   1714       zSelect = appendText(zSelect, zTmp, '\'');
   1715       free(zTmp);
   1716     }
   1717     zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
   1718     rc = sqlite3_step(pTableInfo);
   1719     while( rc==SQLITE_ROW ){
   1720       const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
   1721       zSelect = appendText(zSelect, "quote(", 0);
   1722       zSelect = appendText(zSelect, zText, '"');
   1723       rc = sqlite3_step(pTableInfo);
   1724       if( rc==SQLITE_ROW ){
   1725         zSelect = appendText(zSelect, "), ", 0);
   1726       }else{
   1727         zSelect = appendText(zSelect, ") ", 0);
   1728       }
   1729       nRow++;
   1730     }
   1731     rc = sqlite3_finalize(pTableInfo);
   1732     if( rc!=SQLITE_OK || nRow==0 ){
   1733       free(zSelect);
   1734       return 1;
   1735     }
   1736     zSelect = appendText(zSelect, "|| ')' FROM  ", 0);
   1737     zSelect = appendText(zSelect, zTable, '"');
   1738 
   1739     rc = run_table_dump_query(p, zSelect, zPrepStmt);
   1740     if( rc==SQLITE_CORRUPT ){
   1741       zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
   1742       run_table_dump_query(p, zSelect, 0);
   1743     }
   1744     free(zSelect);
   1745   }
   1746   return 0;
   1747 }
   1748 
   1749 /*
   1750 ** Run zQuery.  Use dump_callback() as the callback routine so that
   1751 ** the contents of the query are output as SQL statements.
   1752 **
   1753 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
   1754 ** "ORDER BY rowid DESC" to the end.
   1755 */
   1756 static int run_schema_dump_query(
   1757   ShellState *p,
   1758   const char *zQuery
   1759 ){
   1760   int rc;
   1761   char *zErr = 0;
   1762   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
   1763   if( rc==SQLITE_CORRUPT ){
   1764     char *zQ2;
   1765     int len = strlen30(zQuery);
   1766     fprintf(p->out, "/****** CORRUPTION ERROR *******/\n");
   1767     if( zErr ){
   1768       fprintf(p->out, "/****** %s ******/\n", zErr);
   1769       sqlite3_free(zErr);
   1770       zErr = 0;
   1771     }
   1772     zQ2 = malloc( len+100 );
   1773     if( zQ2==0 ) return rc;
   1774     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
   1775     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
   1776     if( rc ){
   1777       fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
   1778     }else{
   1779       rc = SQLITE_CORRUPT;
   1780     }
   1781     sqlite3_free(zErr);
   1782     free(zQ2);
   1783   }
   1784   return rc;
   1785 }
   1786 
   1787 /*
   1788 ** Text of a help message
   1789 */
   1790 static char zHelp[] =
   1791   ".backup ?DB? FILE      Backup DB (default \"main\") to FILE\n"
   1792   ".bail on|off           Stop after hitting an error.  Default OFF\n"
   1793   ".binary on|off         Turn binary output on or off.  Default OFF\n"
   1794   ".clone NEWDB           Clone data into NEWDB from the existing database\n"
   1795   ".databases             List names and files of attached databases\n"
   1796   ".dbinfo ?DB?           Show status information about the database\n"
   1797   ".dump ?TABLE? ...      Dump the database in an SQL text format\n"
   1798   "                         If TABLE specified, only dump tables matching\n"
   1799   "                         LIKE pattern TABLE.\n"
   1800   ".echo on|off           Turn command echo on or off\n"
   1801   ".eqp on|off            Enable or disable automatic EXPLAIN QUERY PLAN\n"
   1802   ".exit                  Exit this program\n"
   1803   ".explain ?on|off?      Turn output mode suitable for EXPLAIN on or off.\n"
   1804   "                         With no args, it turns EXPLAIN on.\n"
   1805   ".fullschema            Show schema and the content of sqlite_stat tables\n"
   1806   ".headers on|off        Turn display of headers on or off\n"
   1807   ".help                  Show this message\n"
   1808   ".import FILE TABLE     Import data from FILE into TABLE\n"
   1809   ".indexes ?TABLE?       Show names of all indexes\n"
   1810   "                         If TABLE specified, only show indexes for tables\n"
   1811   "                         matching LIKE pattern TABLE.\n"
   1812 #ifdef SQLITE_ENABLE_IOTRACE
   1813   ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"
   1814 #endif
   1815   ".limit ?LIMIT? ?VAL?   Display or change the value of an SQLITE_LIMIT\n"
   1816 #ifndef SQLITE_OMIT_LOAD_EXTENSION
   1817   ".load FILE ?ENTRY?     Load an extension library\n"
   1818 #endif
   1819   ".log FILE|off          Turn logging on or off.  FILE can be stderr/stdout\n"
   1820   ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"
   1821   "                         ascii    Columns/rows delimited by 0x1F and 0x1E\n"
   1822   "                         csv      Comma-separated values\n"
   1823   "                         column   Left-aligned columns.  (See .width)\n"
   1824   "                         html     HTML <table> code\n"
   1825   "                         insert   SQL insert statements for TABLE\n"
   1826   "                         line     One value per line\n"
   1827   "                         list     Values delimited by .separator strings\n"
   1828   "                         tabs     Tab-separated values\n"
   1829   "                         tcl      TCL list elements\n"
   1830   ".nullvalue STRING      Use STRING in place of NULL values\n"
   1831   ".once FILENAME         Output for the next SQL command only to FILENAME\n"
   1832   ".open ?FILENAME?       Close existing database and reopen FILENAME\n"
   1833   ".output ?FILENAME?     Send output to FILENAME or stdout\n"
   1834   ".print STRING...       Print literal STRING\n"
   1835   ".prompt MAIN CONTINUE  Replace the standard prompts\n"
   1836   ".quit                  Exit this program\n"
   1837   ".read FILENAME         Execute SQL in FILENAME\n"
   1838   ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
   1839   ".save FILE             Write in-memory database into FILE\n"
   1840   ".scanstats on|off      Turn sqlite3_stmt_scanstatus() metrics on or off\n"
   1841   ".schema ?TABLE?        Show the CREATE statements\n"
   1842   "                         If TABLE specified, only show tables matching\n"
   1843   "                         LIKE pattern TABLE.\n"
   1844   ".separator COL ?ROW?   Change the column separator and optionally the row\n"
   1845   "                         separator for both the output mode and .import\n"
   1846   ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
   1847   ".show                  Show the current values for various settings\n"
   1848   ".stats on|off          Turn stats on or off\n"
   1849   ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
   1850   ".tables ?TABLE?        List names of tables\n"
   1851   "                         If TABLE specified, only list tables matching\n"
   1852   "                         LIKE pattern TABLE.\n"
   1853   ".timeout MS            Try opening locked tables for MS milliseconds\n"
   1854   ".timer on|off          Turn SQL timer on or off\n"
   1855   ".trace FILE|off        Output each SQL statement as it is run\n"
   1856   ".vfsname ?AUX?         Print the name of the VFS stack\n"
   1857   ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
   1858   "                         Negative values right-justify\n"
   1859 ;
   1860 
   1861 /* Forward reference */
   1862 static int process_input(ShellState *p, FILE *in);
   1863 /*
   1864 ** Implementation of the "readfile(X)" SQL function.  The entire content
   1865 ** of the file named X is read and returned as a BLOB.  NULL is returned
   1866 ** if the file does not exist or is unreadable.
   1867 */
   1868 static void readfileFunc(
   1869   sqlite3_context *context,
   1870   int argc,
   1871   sqlite3_value **argv
   1872 ){
   1873   const char *zName;
   1874   FILE *in;
   1875   long nIn;
   1876   void *pBuf;
   1877 
   1878   UNUSED_PARAMETER(argc);
   1879   zName = (const char*)sqlite3_value_text(argv[0]);
   1880   if( zName==0 ) return;
   1881   in = fopen(zName, "rb");
   1882   if( in==0 ) return;
   1883   fseek(in, 0, SEEK_END);
   1884   nIn = ftell(in);
   1885   rewind(in);
   1886   pBuf = sqlite3_malloc64( nIn );
   1887   if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
   1888     sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
   1889   }else{
   1890     sqlite3_free(pBuf);
   1891   }
   1892   fclose(in);
   1893 }
   1894 
   1895 /*
   1896 ** Implementation of the "writefile(X,Y)" SQL function.  The argument Y
   1897 ** is written into file X.  The number of bytes written is returned.  Or
   1898 ** NULL is returned if something goes wrong, such as being unable to open
   1899 ** file X for writing.
   1900 */
   1901 static void writefileFunc(
   1902   sqlite3_context *context,
   1903   int argc,
   1904   sqlite3_value **argv
   1905 ){
   1906   FILE *out;
   1907   const char *z;
   1908   sqlite3_int64 rc;
   1909   const char *zFile;
   1910 
   1911   UNUSED_PARAMETER(argc);
   1912   zFile = (const char*)sqlite3_value_text(argv[0]);
   1913   if( zFile==0 ) return;
   1914   out = fopen(zFile, "wb");
   1915   if( out==0 ) return;
   1916   z = (const char*)sqlite3_value_blob(argv[1]);
   1917   if( z==0 ){
   1918     rc = 0;
   1919   }else{
   1920     rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
   1921   }
   1922   fclose(out);
   1923   sqlite3_result_int64(context, rc);
   1924 }
   1925 
   1926 /*
   1927 ** Make sure the database is open.  If it is not, then open it.  If
   1928 ** the database fails to open, print an error message and exit.
   1929 */
   1930 static void open_db(ShellState *p, int keepAlive){
   1931   if( p->db==0 ){
   1932     sqlite3_initialize();
   1933     sqlite3_open(p->zDbFilename, &p->db);
   1934     globalDb = p->db;
   1935     if( p->db && sqlite3_errcode(p->db)==SQLITE_OK ){
   1936       sqlite3_create_function(p->db, "shellstatic", 0, SQLITE_UTF8, 0,
   1937           shellstaticFunc, 0, 0);
   1938     }
   1939     if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
   1940       fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
   1941           p->zDbFilename, sqlite3_errmsg(p->db));
   1942       if( keepAlive ) return;
   1943       exit(1);
   1944     }
   1945 #ifndef SQLITE_OMIT_LOAD_EXTENSION
   1946     sqlite3_enable_load_extension(p->db, 1);
   1947 #endif
   1948     sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0,
   1949                             readfileFunc, 0, 0);
   1950     sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0,
   1951                             writefileFunc, 0, 0);
   1952 
   1953     // Begin Android Add
   1954     #ifndef NO_ANDROID_FUNCS
   1955         InitializeIcuOrDie();
   1956         int err = register_localized_collators(p->db, "en_US", 0);
   1957         if (err != SQLITE_OK) {
   1958           fprintf(stderr, "register_localized_collators() failed\n");
   1959           exit(1);
   1960         }
   1961         err = register_android_functions(p->db, 0);
   1962         if (err != SQLITE_OK) {
   1963           fprintf(stderr, "register_android_functions() failed\n");
   1964           exit(1);
   1965         }
   1966     #endif
   1967     // End Android Add
   1968   }
   1969 }
   1970 
   1971 /*
   1972 ** Do C-language style dequoting.
   1973 **
   1974 **    \a    -> alarm
   1975 **    \b    -> backspace
   1976 **    \t    -> tab
   1977 **    \n    -> newline
   1978 **    \v    -> vertical tab
   1979 **    \f    -> form feed
   1980 **    \r    -> carriage return
   1981 **    \s    -> space
   1982 **    \"    -> "
   1983 **    \'    -> '
   1984 **    \\    -> backslash
   1985 **    \NNN  -> ascii character NNN in octal
   1986 */
   1987 static void resolve_backslashes(char *z){
   1988   int i, j;
   1989   char c;
   1990   while( *z && *z!='\\' ) z++;
   1991   for(i=j=0; (c = z[i])!=0; i++, j++){
   1992     if( c=='\\' && z[i+1]!=0 ){
   1993       c = z[++i];
   1994       if( c=='a' ){
   1995         c = '\a';
   1996       }else if( c=='b' ){
   1997         c = '\b';
   1998       }else if( c=='t' ){
   1999         c = '\t';
   2000       }else if( c=='n' ){
   2001         c = '\n';
   2002       }else if( c=='v' ){
   2003         c = '\v';
   2004       }else if( c=='f' ){
   2005         c = '\f';
   2006       }else if( c=='r' ){
   2007         c = '\r';
   2008       }else if( c=='"' ){
   2009         c = '"';
   2010       }else if( c=='\'' ){
   2011         c = '\'';
   2012       }else if( c=='\\' ){
   2013         c = '\\';
   2014       }else if( c>='0' && c<='7' ){
   2015         c -= '0';
   2016         if( z[i+1]>='0' && z[i+1]<='7' ){
   2017           i++;
   2018           c = (c<<3) + z[i] - '0';
   2019           if( z[i+1]>='0' && z[i+1]<='7' ){
   2020             i++;
   2021             c = (c<<3) + z[i] - '0';
   2022           }
   2023         }
   2024       }
   2025     }
   2026     z[j] = c;
   2027   }
   2028   if( j<i ) z[j] = 0;
   2029 }
   2030 
   2031 /*
   2032 ** Return the value of a hexadecimal digit.  Return -1 if the input
   2033 ** is not a hex digit.
   2034 */
   2035 static int hexDigitValue(char c){
   2036   if( c>='0' && c<='9' ) return c - '0';
   2037   if( c>='a' && c<='f' ) return c - 'a' + 10;
   2038   if( c>='A' && c<='F' ) return c - 'A' + 10;
   2039   return -1;
   2040 }
   2041 
   2042 /*
   2043 ** Interpret zArg as an integer value, possibly with suffixes.
   2044 */
   2045 static sqlite3_int64 integerValue(const char *zArg){
   2046   sqlite3_int64 v = 0;
   2047   static const struct { char *zSuffix; int iMult; } aMult[] = {
   2048     { "KiB", 1024 },
   2049     { "MiB", 1024*1024 },
   2050     { "GiB", 1024*1024*1024 },
   2051     { "KB",  1000 },
   2052     { "MB",  1000000 },
   2053     { "GB",  1000000000 },
   2054     { "K",   1000 },
   2055     { "M",   1000000 },
   2056     { "G",   1000000000 },
   2057   };
   2058   int i;
   2059   int isNeg = 0;
   2060   if( zArg[0]=='-' ){
   2061     isNeg = 1;
   2062     zArg++;
   2063   }else if( zArg[0]=='+' ){
   2064     zArg++;
   2065   }
   2066   if( zArg[0]=='0' && zArg[1]=='x' ){
   2067     int x;
   2068     zArg += 2;
   2069     while( (x = hexDigitValue(zArg[0]))>=0 ){
   2070       v = (v<<4) + x;
   2071       zArg++;
   2072     }
   2073   }else{
   2074     while( IsDigit(zArg[0]) ){
   2075       v = v*10 + zArg[0] - '0';
   2076       zArg++;
   2077     }
   2078   }
   2079   for(i=0; i<ArraySize(aMult); i++){
   2080     if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
   2081       v *= aMult[i].iMult;
   2082       break;
   2083     }
   2084   }
   2085   return isNeg? -v : v;
   2086 }
   2087 
   2088 /*
   2089 ** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
   2090 ** for TRUE and FALSE.  Return the integer value if appropriate.
   2091 */
   2092 static int booleanValue(char *zArg){
   2093   int i;
   2094   if( zArg[0]=='0' && zArg[1]=='x' ){
   2095     for(i=2; hexDigitValue(zArg[i])>=0; i++){}
   2096   }else{
   2097     for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
   2098   }
   2099   if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
   2100   if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
   2101     return 1;
   2102   }
   2103   if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
   2104     return 0;
   2105   }
   2106   fprintf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
   2107           zArg);
   2108   return 0;
   2109 }
   2110 
   2111 /*
   2112 ** Close an output file, assuming it is not stderr or stdout
   2113 */
   2114 static void output_file_close(FILE *f){
   2115   if( f && f!=stdout && f!=stderr ) fclose(f);
   2116 }
   2117 
   2118 /*
   2119 ** Try to open an output file.   The names "stdout" and "stderr" are
   2120 ** recognized and do the right thing.  NULL is returned if the output
   2121 ** filename is "off".
   2122 */
   2123 static FILE *output_file_open(const char *zFile){
   2124   FILE *f;
   2125   if( strcmp(zFile,"stdout")==0 ){
   2126     f = stdout;
   2127   }else if( strcmp(zFile, "stderr")==0 ){
   2128     f = stderr;
   2129   }else if( strcmp(zFile, "off")==0 ){
   2130     f = 0;
   2131   }else{
   2132     f = fopen(zFile, "wb");
   2133     if( f==0 ){
   2134       fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
   2135     }
   2136   }
   2137   return f;
   2138 }
   2139 
   2140 /*
   2141 ** A routine for handling output from sqlite3_trace().
   2142 */
   2143 static void sql_trace_callback(void *pArg, const char *z){
   2144   FILE *f = (FILE*)pArg;
   2145   if( f ){
   2146     int i = (int)strlen(z);
   2147     while( i>0 && z[i-1]==';' ){ i--; }
   2148     fprintf(f, "%.*s;\n", i, z);
   2149   }
   2150 }
   2151 
   2152 /*
   2153 ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
   2154 ** a useful spot to set a debugger breakpoint.
   2155 */
   2156 static void test_breakpoint(void){
   2157   static int nCall = 0;
   2158   nCall++;
   2159 }
   2160 
   2161 /*
   2162 ** An object used to read a CSV and other files for import.
   2163 */
   2164 typedef struct ImportCtx ImportCtx;
   2165 struct ImportCtx {
   2166   const char *zFile;  /* Name of the input file */
   2167   FILE *in;           /* Read the CSV text from this input stream */
   2168   char *z;            /* Accumulated text for a field */
   2169   int n;              /* Number of bytes in z */
   2170   int nAlloc;         /* Space allocated for z[] */
   2171   int nLine;          /* Current line number */
   2172   int cTerm;          /* Character that terminated the most recent field */
   2173   int cColSep;        /* The column separator character.  (Usually ",") */
   2174   int cRowSep;        /* The row separator character.  (Usually "\n") */
   2175 };
   2176 
   2177 /* Append a single byte to z[] */
   2178 static void import_append_char(ImportCtx *p, int c){
   2179   if( p->n+1>=p->nAlloc ){
   2180     p->nAlloc += p->nAlloc + 100;
   2181     p->z = sqlite3_realloc64(p->z, p->nAlloc);
   2182     if( p->z==0 ){
   2183       fprintf(stderr, "out of memory\n");
   2184       exit(1);
   2185     }
   2186   }
   2187   p->z[p->n++] = (char)c;
   2188 }
   2189 
   2190 /* Read a single field of CSV text.  Compatible with rfc4180 and extended
   2191 ** with the option of having a separator other than ",".
   2192 **
   2193 **   +  Input comes from p->in.
   2194 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
   2195 **      from sqlite3_malloc64().
   2196 **   +  Use p->cSep as the column separator.  The default is ",".
   2197 **   +  Use p->rSep as the row separator.  The default is "\n".
   2198 **   +  Keep track of the line number in p->nLine.
   2199 **   +  Store the character that terminates the field in p->cTerm.  Store
   2200 **      EOF on end-of-file.
   2201 **   +  Report syntax errors on stderr
   2202 */
   2203 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
   2204   int c;
   2205   int cSep = p->cColSep;
   2206   int rSep = p->cRowSep;
   2207   p->n = 0;
   2208   c = fgetc(p->in);
   2209   if( c==EOF || seenInterrupt ){
   2210     p->cTerm = EOF;
   2211     return 0;
   2212   }
   2213   if( c=='"' ){
   2214     int pc, ppc;
   2215     int startLine = p->nLine;
   2216     int cQuote = c;
   2217     pc = ppc = 0;
   2218     while( 1 ){
   2219       c = fgetc(p->in);
   2220       if( c==rSep ) p->nLine++;
   2221       if( c==cQuote ){
   2222         if( pc==cQuote ){
   2223           pc = 0;
   2224           continue;
   2225         }
   2226       }
   2227       if( (c==cSep && pc==cQuote)
   2228        || (c==rSep && pc==cQuote)
   2229        || (c==rSep && pc=='\r' && ppc==cQuote)
   2230        || (c==EOF && pc==cQuote)
   2231       ){
   2232         do{ p->n--; }while( p->z[p->n]!=cQuote );
   2233         p->cTerm = c;
   2234         break;
   2235       }
   2236       if( pc==cQuote && c!='\r' ){
   2237         fprintf(stderr, "%s:%d: unescaped %c character\n",
   2238                 p->zFile, p->nLine, cQuote);
   2239       }
   2240       if( c==EOF ){
   2241         fprintf(stderr, "%s:%d: unterminated %c-quoted field\n",
   2242                 p->zFile, startLine, cQuote);
   2243         p->cTerm = c;
   2244         break;
   2245       }
   2246       import_append_char(p, c);
   2247       ppc = pc;
   2248       pc = c;
   2249     }
   2250   }else{
   2251     while( c!=EOF && c!=cSep && c!=rSep ){
   2252       import_append_char(p, c);
   2253       c = fgetc(p->in);
   2254     }
   2255     if( c==rSep ){
   2256       p->nLine++;
   2257       if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
   2258     }
   2259     p->cTerm = c;
   2260   }
   2261   if( p->z ) p->z[p->n] = 0;
   2262   return p->z;
   2263 }
   2264 
   2265 /* Read a single field of ASCII delimited text.
   2266 **
   2267 **   +  Input comes from p->in.
   2268 **   +  Store results in p->z of length p->n.  Space to hold p->z comes
   2269 **      from sqlite3_malloc64().
   2270 **   +  Use p->cSep as the column separator.  The default is "\x1F".
   2271 **   +  Use p->rSep as the row separator.  The default is "\x1E".
   2272 **   +  Keep track of the row number in p->nLine.
   2273 **   +  Store the character that terminates the field in p->cTerm.  Store
   2274 **      EOF on end-of-file.
   2275 **   +  Report syntax errors on stderr
   2276 */
   2277 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
   2278   int c;
   2279   int cSep = p->cColSep;
   2280   int rSep = p->cRowSep;
   2281   p->n = 0;
   2282   c = fgetc(p->in);
   2283   if( c==EOF || seenInterrupt ){
   2284     p->cTerm = EOF;
   2285     return 0;
   2286   }
   2287   while( c!=EOF && c!=cSep && c!=rSep ){
   2288     import_append_char(p, c);
   2289     c = fgetc(p->in);
   2290   }
   2291   if( c==rSep ){
   2292     p->nLine++;
   2293   }
   2294   p->cTerm = c;
   2295   if( p->z ) p->z[p->n] = 0;
   2296   return p->z;
   2297 }
   2298 
   2299 /*
   2300 ** Try to transfer data for table zTable.  If an error is seen while
   2301 ** moving forward, try to go backwards.  The backwards movement won't
   2302 ** work for WITHOUT ROWID tables.
   2303 */
   2304 static void tryToCloneData(
   2305   ShellState *p,
   2306   sqlite3 *newDb,
   2307   const char *zTable
   2308 ){
   2309   sqlite3_stmt *pQuery = 0;
   2310   sqlite3_stmt *pInsert = 0;
   2311   char *zQuery = 0;
   2312   char *zInsert = 0;
   2313   int rc;
   2314   int i, j, n;
   2315   int nTable = (int)strlen(zTable);
   2316   int k = 0;
   2317   int cnt = 0;
   2318   const int spinRate = 10000;
   2319 
   2320   zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
   2321   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
   2322   if( rc ){
   2323     fprintf(stderr, "Error %d: %s on [%s]\n",
   2324             sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
   2325             zQuery);
   2326     goto end_data_xfer;
   2327   }
   2328   n = sqlite3_column_count(pQuery);
   2329   zInsert = sqlite3_malloc64(200 + nTable + n*3);
   2330   if( zInsert==0 ){
   2331     fprintf(stderr, "out of memory\n");
   2332     goto end_data_xfer;
   2333   }
   2334   sqlite3_snprintf(200+nTable,zInsert,
   2335                    "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
   2336   i = (int)strlen(zInsert);
   2337   for(j=1; j<n; j++){
   2338     memcpy(zInsert+i, ",?", 2);
   2339     i += 2;
   2340   }
   2341   memcpy(zInsert+i, ");", 3);
   2342   rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
   2343   if( rc ){
   2344     fprintf(stderr, "Error %d: %s on [%s]\n",
   2345             sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
   2346             zQuery);
   2347     goto end_data_xfer;
   2348   }
   2349   for(k=0; k<2; k++){
   2350     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
   2351       for(i=0; i<n; i++){
   2352         switch( sqlite3_column_type(pQuery, i) ){
   2353           case SQLITE_NULL: {
   2354             sqlite3_bind_null(pInsert, i+1);
   2355             break;
   2356           }
   2357           case SQLITE_INTEGER: {
   2358             sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
   2359             break;
   2360           }
   2361           case SQLITE_FLOAT: {
   2362             sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
   2363             break;
   2364           }
   2365           case SQLITE_TEXT: {
   2366             sqlite3_bind_text(pInsert, i+1,
   2367                              (const char*)sqlite3_column_text(pQuery,i),
   2368                              -1, SQLITE_STATIC);
   2369             break;
   2370           }
   2371           case SQLITE_BLOB: {
   2372             sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
   2373                                             sqlite3_column_bytes(pQuery,i),
   2374                                             SQLITE_STATIC);
   2375             break;
   2376           }
   2377         }
   2378       } /* End for */
   2379       rc = sqlite3_step(pInsert);
   2380       if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
   2381         fprintf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
   2382                         sqlite3_errmsg(newDb));
   2383       }
   2384       sqlite3_reset(pInsert);
   2385       cnt++;
   2386       if( (cnt%spinRate)==0 ){
   2387         printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
   2388         fflush(stdout);
   2389       }
   2390     } /* End while */
   2391     if( rc==SQLITE_DONE ) break;
   2392     sqlite3_finalize(pQuery);
   2393     sqlite3_free(zQuery);
   2394     zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
   2395                              zTable);
   2396     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
   2397     if( rc ){
   2398       fprintf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
   2399       break;
   2400     }
   2401   } /* End for(k=0...) */
   2402 
   2403 end_data_xfer:
   2404   sqlite3_finalize(pQuery);
   2405   sqlite3_finalize(pInsert);
   2406   sqlite3_free(zQuery);
   2407   sqlite3_free(zInsert);
   2408 }
   2409 
   2410 
   2411 /*
   2412 ** Try to transfer all rows of the schema that match zWhere.  For
   2413 ** each row, invoke xForEach() on the object defined by that row.
   2414 ** If an error is encountered while moving forward through the
   2415 ** sqlite_master table, try again moving backwards.
   2416 */
   2417 static void tryToCloneSchema(
   2418   ShellState *p,
   2419   sqlite3 *newDb,
   2420   const char *zWhere,
   2421   void (*xForEach)(ShellState*,sqlite3*,const char*)
   2422 ){
   2423   sqlite3_stmt *pQuery = 0;
   2424   char *zQuery = 0;
   2425   int rc;
   2426   const unsigned char *zName;
   2427   const unsigned char *zSql;
   2428   char *zErrMsg = 0;
   2429 
   2430   zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
   2431                            " WHERE %s", zWhere);
   2432   rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
   2433   if( rc ){
   2434     fprintf(stderr, "Error: (%d) %s on [%s]\n",
   2435                     sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
   2436                     zQuery);
   2437     goto end_schema_xfer;
   2438   }
   2439   while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
   2440     zName = sqlite3_column_text(pQuery, 0);
   2441     zSql = sqlite3_column_text(pQuery, 1);
   2442     printf("%s... ", zName); fflush(stdout);
   2443     sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
   2444     if( zErrMsg ){
   2445       fprintf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
   2446       sqlite3_free(zErrMsg);
   2447       zErrMsg = 0;
   2448     }
   2449     if( xForEach ){
   2450       xForEach(p, newDb, (const char*)zName);
   2451     }
   2452     printf("done\n");
   2453   }
   2454   if( rc!=SQLITE_DONE ){
   2455     sqlite3_finalize(pQuery);
   2456     sqlite3_free(zQuery);
   2457     zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
   2458                              " WHERE %s ORDER BY rowid DESC", zWhere);
   2459     rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
   2460     if( rc ){
   2461       fprintf(stderr, "Error: (%d) %s on [%s]\n",
   2462                       sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
   2463                       zQuery);
   2464       goto end_schema_xfer;
   2465     }
   2466     while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
   2467       zName = sqlite3_column_text(pQuery, 0);
   2468       zSql = sqlite3_column_text(pQuery, 1);
   2469       printf("%s... ", zName); fflush(stdout);
   2470       sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
   2471       if( zErrMsg ){
   2472         fprintf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
   2473         sqlite3_free(zErrMsg);
   2474         zErrMsg = 0;
   2475       }
   2476       if( xForEach ){
   2477         xForEach(p, newDb, (const char*)zName);
   2478       }
   2479       printf("done\n");
   2480     }
   2481   }
   2482 end_schema_xfer:
   2483   sqlite3_finalize(pQuery);
   2484   sqlite3_free(zQuery);
   2485 }
   2486 
   2487 /*
   2488 ** Open a new database file named "zNewDb".  Try to recover as much information
   2489 ** as possible out of the main database (which might be corrupt) and write it
   2490 ** into zNewDb.
   2491 */
   2492 static void tryToClone(ShellState *p, const char *zNewDb){
   2493   int rc;
   2494   sqlite3 *newDb = 0;
   2495   if( access(zNewDb,0)==0 ){
   2496     fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
   2497     return;
   2498   }
   2499   rc = sqlite3_open(zNewDb, &newDb);
   2500   if( rc ){
   2501     fprintf(stderr, "Cannot create output database: %s\n",
   2502             sqlite3_errmsg(newDb));
   2503   }else{
   2504     sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
   2505     sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
   2506     tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
   2507     tryToCloneSchema(p, newDb, "type!='table'", 0);
   2508     sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
   2509     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
   2510   }
   2511   sqlite3_close(newDb);
   2512 }
   2513 
   2514 /*
   2515 ** Change the output file back to stdout
   2516 */
   2517 static void output_reset(ShellState *p){
   2518   if( p->outfile[0]=='|' ){
   2519 #ifndef SQLITE_OMIT_POPEN
   2520     pclose(p->out);
   2521 #endif
   2522   }else{
   2523     output_file_close(p->out);
   2524   }
   2525   p->outfile[0] = 0;
   2526   p->out = stdout;
   2527 }
   2528 
   2529 /*
   2530 ** Run an SQL command and return the single integer result.
   2531 */
   2532 static int db_int(ShellState *p, const char *zSql){
   2533   sqlite3_stmt *pStmt;
   2534   int res = 0;
   2535   sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
   2536   if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
   2537     res = sqlite3_column_int(pStmt,0);
   2538   }
   2539   sqlite3_finalize(pStmt);
   2540   return res;
   2541 }
   2542 
   2543 /*
   2544 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
   2545 */
   2546 unsigned int get2byteInt(unsigned char *a){
   2547   return (a[0]<<8) + a[1];
   2548 }
   2549 unsigned int get4byteInt(unsigned char *a){
   2550   return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
   2551 }
   2552 
   2553 /*
   2554 ** Implementation of the ".info" command.
   2555 **
   2556 ** Return 1 on error, 2 to exit, and 0 otherwise.
   2557 */
   2558 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
   2559   static const struct { const char *zName; int ofst; } aField[] = {
   2560      { "file change counter:",  24  },
   2561      { "database page count:",  28  },
   2562      { "freelist page count:",  36  },
   2563      { "schema cookie:",        40  },
   2564      { "schema format:",        44  },
   2565      { "default cache size:",   48  },
   2566      { "autovacuum top root:",  52  },
   2567      { "incremental vacuum:",   64  },
   2568      { "text encoding:",        56  },
   2569      { "user version:",         60  },
   2570      { "application id:",       68  },
   2571      { "software version:",     96  },
   2572   };
   2573   static const struct { const char *zName; const char *zSql; } aQuery[] = {
   2574      { "number of tables:",
   2575        "SELECT count(*) FROM %s WHERE type='table'" },
   2576      { "number of indexes:",
   2577        "SELECT count(*) FROM %s WHERE type='index'" },
   2578      { "number of triggers:",
   2579        "SELECT count(*) FROM %s WHERE type='trigger'" },
   2580      { "number of views:",
   2581        "SELECT count(*) FROM %s WHERE type='view'" },
   2582      { "schema size:",
   2583        "SELECT total(length(sql)) FROM %s" },
   2584   };
   2585   sqlite3_file *pFile;
   2586   int i;
   2587   char *zSchemaTab;
   2588   char *zDb = nArg>=2 ? azArg[1] : "main";
   2589   unsigned char aHdr[100];
   2590   open_db(p, 0);
   2591   if( p->db==0 ) return 1;
   2592   sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile);
   2593   if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){
   2594     return 1;
   2595   }
   2596   i = pFile->pMethods->xRead(pFile, aHdr, 100, 0);
   2597   if( i!=SQLITE_OK ){
   2598     fprintf(stderr, "unable to read database header\n");
   2599     return 1;
   2600   }
   2601   i = get2byteInt(aHdr+16);
   2602   if( i==1 ) i = 65536;
   2603   fprintf(p->out, "%-20s %d\n", "database page size:", i);
   2604   fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
   2605   fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
   2606   fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
   2607   for(i=0; i<ArraySize(aField); i++){
   2608     int ofst = aField[i].ofst;
   2609     unsigned int val = get4byteInt(aHdr + ofst);
   2610     fprintf(p->out, "%-20s %u", aField[i].zName, val);
   2611     switch( ofst ){
   2612       case 56: {
   2613         if( val==1 ) fprintf(p->out, " (utf8)");
   2614         if( val==2 ) fprintf(p->out, " (utf16le)");
   2615         if( val==3 ) fprintf(p->out, " (utf16be)");
   2616       }
   2617     }
   2618     fprintf(p->out, "\n");
   2619   }
   2620   if( zDb==0 ){
   2621     zSchemaTab = sqlite3_mprintf("main.sqlite_master");
   2622   }else if( strcmp(zDb,"temp")==0 ){
   2623     zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
   2624   }else{
   2625     zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
   2626   }
   2627   for(i=0; i<ArraySize(aQuery); i++){
   2628     char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
   2629     int val = db_int(p, zSql);
   2630     sqlite3_free(zSql);
   2631     fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
   2632   }
   2633   sqlite3_free(zSchemaTab);
   2634   return 0;
   2635 }
   2636 
   2637 /*
   2638 ** Print the current sqlite3_errmsg() value to stderr and return 1.
   2639 */
   2640 static int shellDatabaseError(sqlite3 *db){
   2641   const char *zErr = sqlite3_errmsg(db);
   2642   fprintf(stderr, "Error: %s\n", zErr);
   2643   return 1;
   2644 }
   2645 
   2646 /*
   2647 ** Print an out-of-memory message to stderr and return 1.
   2648 */
   2649 static int shellNomemError(void){
   2650   fprintf(stderr, "Error: out of memory\n");
   2651   return 1;
   2652 }
   2653 
   2654 /*
   2655 ** If an input line begins with "." then invoke this routine to
   2656 ** process that line.
   2657 **
   2658 ** Return 1 on error, 2 to exit, and 0 otherwise.
   2659 */
   2660 static int do_meta_command(char *zLine, ShellState *p){
   2661   int h = 1;
   2662   int nArg = 0;
   2663   int n, c;
   2664   int rc = 0;
   2665   char *azArg[50];
   2666 
   2667   /* Parse the input line into tokens.
   2668   */
   2669   while( zLine[h] && nArg<ArraySize(azArg) ){
   2670     while( IsSpace(zLine[h]) ){ h++; }
   2671     if( zLine[h]==0 ) break;
   2672     if( zLine[h]=='\'' || zLine[h]=='"' ){
   2673       int delim = zLine[h++];
   2674       azArg[nArg++] = &zLine[h];
   2675       while( zLine[h] && zLine[h]!=delim ){
   2676         if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
   2677         h++;
   2678       }
   2679       if( zLine[h]==delim ){
   2680         zLine[h++] = 0;
   2681       }
   2682       if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
   2683     }else{
   2684       azArg[nArg++] = &zLine[h];
   2685       while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
   2686       if( zLine[h] ) zLine[h++] = 0;
   2687       resolve_backslashes(azArg[nArg-1]);
   2688     }
   2689   }
   2690 
   2691   /* Process the input line.
   2692   */
   2693   if( nArg==0 ) return 0; /* no tokens, no error */
   2694   n = strlen30(azArg[0]);
   2695   c = azArg[0][0];
   2696   if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
   2697    || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
   2698   ){
   2699     const char *zDestFile = 0;
   2700     const char *zDb = 0;
   2701     sqlite3 *pDest;
   2702     sqlite3_backup *pBackup;
   2703     int j;
   2704     for(j=1; j<nArg; j++){
   2705       const char *z = azArg[j];
   2706       if( z[0]=='-' ){
   2707         while( z[0]=='-' ) z++;
   2708         /* No options to process at this time */
   2709         {
   2710           fprintf(stderr, "unknown option: %s\n", azArg[j]);
   2711           return 1;
   2712         }
   2713       }else if( zDestFile==0 ){
   2714         zDestFile = azArg[j];
   2715       }else if( zDb==0 ){
   2716         zDb = zDestFile;
   2717         zDestFile = azArg[j];
   2718       }else{
   2719         fprintf(stderr, "too many arguments to .backup\n");
   2720         return 1;
   2721       }
   2722     }
   2723     if( zDestFile==0 ){
   2724       fprintf(stderr, "missing FILENAME argument on .backup\n");
   2725       return 1;
   2726     }
   2727     if( zDb==0 ) zDb = "main";
   2728     rc = sqlite3_open(zDestFile, &pDest);
   2729     if( rc!=SQLITE_OK ){
   2730       fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
   2731       sqlite3_close(pDest);
   2732       return 1;
   2733     }
   2734     open_db(p, 0);
   2735     pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
   2736     if( pBackup==0 ){
   2737       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
   2738       sqlite3_close(pDest);
   2739       return 1;
   2740     }
   2741     while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
   2742     sqlite3_backup_finish(pBackup);
   2743     if( rc==SQLITE_DONE ){
   2744       rc = 0;
   2745     }else{
   2746       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
   2747       rc = 1;
   2748     }
   2749     sqlite3_close(pDest);
   2750   }else
   2751 
   2752   if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
   2753     if( nArg==2 ){
   2754       bail_on_error = booleanValue(azArg[1]);
   2755     }else{
   2756       fprintf(stderr, "Usage: .bail on|off\n");
   2757       rc = 1;
   2758     }
   2759   }else
   2760 
   2761   if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
   2762     if( nArg==2 ){
   2763       if( booleanValue(azArg[1]) ){
   2764         setBinaryMode(p->out);
   2765       }else{
   2766         setTextMode(p->out);
   2767       }
   2768     }else{
   2769       fprintf(stderr, "Usage: .binary on|off\n");
   2770       rc = 1;
   2771     }
   2772   }else
   2773 
   2774   /* The undocumented ".breakpoint" command causes a call to the no-op
   2775   ** routine named test_breakpoint().
   2776   */
   2777   if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
   2778     test_breakpoint();
   2779   }else
   2780 
   2781   if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
   2782     if( nArg==2 ){
   2783       tryToClone(p, azArg[1]);
   2784     }else{
   2785       fprintf(stderr, "Usage: .clone FILENAME\n");
   2786       rc = 1;
   2787     }
   2788   }else
   2789 
   2790   if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
   2791     ShellState data;
   2792     char *zErrMsg = 0;
   2793     open_db(p, 0);
   2794     memcpy(&data, p, sizeof(data));
   2795     data.showHeader = 1;
   2796     data.mode = MODE_Column;
   2797     data.colWidth[0] = 3;
   2798     data.colWidth[1] = 15;
   2799     data.colWidth[2] = 58;
   2800     data.cnt = 0;
   2801     sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
   2802     if( zErrMsg ){
   2803       fprintf(stderr,"Error: %s\n", zErrMsg);
   2804       sqlite3_free(zErrMsg);
   2805       rc = 1;
   2806     }
   2807   }else
   2808 
   2809   if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
   2810     rc = shell_dbinfo_command(p, nArg, azArg);
   2811   }else
   2812 
   2813   if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
   2814     open_db(p, 0);
   2815     /* When playing back a "dump", the content might appear in an order
   2816     ** which causes immediate foreign key constraints to be violated.
   2817     ** So disable foreign-key constraint enforcement to prevent problems. */
   2818     if( nArg!=1 && nArg!=2 ){
   2819       fprintf(stderr, "Usage: .dump ?LIKE-PATTERN?\n");
   2820       rc = 1;
   2821       goto meta_command_exit;
   2822     }
   2823     fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
   2824     fprintf(p->out, "BEGIN TRANSACTION;\n");
   2825     p->writableSchema = 0;
   2826     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
   2827     p->nErr = 0;
   2828     if( nArg==1 ){
   2829       run_schema_dump_query(p,
   2830         "SELECT name, type, sql FROM sqlite_master "
   2831         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
   2832       );
   2833       run_schema_dump_query(p,
   2834         "SELECT name, type, sql FROM sqlite_master "
   2835         "WHERE name=='sqlite_sequence'"
   2836       );
   2837       run_table_dump_query(p,
   2838         "SELECT sql FROM sqlite_master "
   2839         "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
   2840       );
   2841     }else{
   2842       int i;
   2843       for(i=1; i<nArg; i++){
   2844         zShellStatic = azArg[i];
   2845         run_schema_dump_query(p,
   2846           "SELECT name, type, sql FROM sqlite_master "
   2847           "WHERE tbl_name LIKE shellstatic() AND type=='table'"
   2848           "  AND sql NOT NULL");
   2849         run_table_dump_query(p,
   2850           "SELECT sql FROM sqlite_master "
   2851           "WHERE sql NOT NULL"
   2852           "  AND type IN ('index','trigger','view')"
   2853           "  AND tbl_name LIKE shellstatic()", 0
   2854         );
   2855         zShellStatic = 0;
   2856       }
   2857     }
   2858     if( p->writableSchema ){
   2859       fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
   2860       p->writableSchema = 0;
   2861     }
   2862     sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
   2863     sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
   2864     fprintf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
   2865   }else
   2866 
   2867   if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
   2868     if( nArg==2 ){
   2869       p->echoOn = booleanValue(azArg[1]);
   2870     }else{
   2871       fprintf(stderr, "Usage: .echo on|off\n");
   2872       rc = 1;
   2873     }
   2874   }else
   2875 
   2876   if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
   2877     if( nArg==2 ){
   2878       p->autoEQP = booleanValue(azArg[1]);
   2879     }else{
   2880       fprintf(stderr, "Usage: .eqp on|off\n");
   2881       rc = 1;
   2882     }
   2883   }else
   2884 
   2885   if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
   2886     if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
   2887     rc = 2;
   2888   }else
   2889 
   2890   if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
   2891     int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
   2892     if(val == 1) {
   2893       if(!p->normalMode.valid) {
   2894         p->normalMode.valid = 1;
   2895         p->normalMode.mode = p->mode;
   2896         p->normalMode.showHeader = p->showHeader;
   2897         memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
   2898       }
   2899       /* We could put this code under the !p->explainValid
   2900       ** condition so that it does not execute if we are already in
   2901       ** explain mode. However, always executing it allows us an easy
   2902       ** was to reset to explain mode in case the user previously
   2903       ** did an .explain followed by a .width, .mode or .header
   2904       ** command.
   2905       */
   2906       p->mode = MODE_Explain;
   2907       p->showHeader = 1;
   2908       memset(p->colWidth,0,sizeof(p->colWidth));
   2909       p->colWidth[0] = 4;                  /* addr */
   2910       p->colWidth[1] = 13;                 /* opcode */
   2911       p->colWidth[2] = 4;                  /* P1 */
   2912       p->colWidth[3] = 4;                  /* P2 */
   2913       p->colWidth[4] = 4;                  /* P3 */
   2914       p->colWidth[5] = 13;                 /* P4 */
   2915       p->colWidth[6] = 2;                  /* P5 */
   2916       p->colWidth[7] = 13;                  /* Comment */
   2917     }else if (p->normalMode.valid) {
   2918       p->normalMode.valid = 0;
   2919       p->mode = p->normalMode.mode;
   2920       p->showHeader = p->normalMode.showHeader;
   2921       memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
   2922     }
   2923   }else
   2924 
   2925   if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
   2926     ShellState data;
   2927     char *zErrMsg = 0;
   2928     int doStats = 0;
   2929     if( nArg!=1 ){
   2930       fprintf(stderr, "Usage: .fullschema\n");
   2931       rc = 1;
   2932       goto meta_command_exit;
   2933     }
   2934     open_db(p, 0);
   2935     memcpy(&data, p, sizeof(data));
   2936     data.showHeader = 0;
   2937     data.mode = MODE_Semi;
   2938     rc = sqlite3_exec(p->db,
   2939        "SELECT sql FROM"
   2940        "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
   2941        "     FROM sqlite_master UNION ALL"
   2942        "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
   2943        "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
   2944        "ORDER BY rowid",
   2945        callback, &data, &zErrMsg
   2946     );
   2947     if( rc==SQLITE_OK ){
   2948       sqlite3_stmt *pStmt;
   2949       rc = sqlite3_prepare_v2(p->db,
   2950                "SELECT rowid FROM sqlite_master"
   2951                " WHERE name GLOB 'sqlite_stat[134]'",
   2952                -1, &pStmt, 0);
   2953       doStats = sqlite3_step(pStmt)==SQLITE_ROW;
   2954       sqlite3_finalize(pStmt);
   2955     }
   2956     if( doStats==0 ){
   2957       fprintf(p->out, "/* No STAT tables available */\n");
   2958     }else{
   2959       fprintf(p->out, "ANALYZE sqlite_master;\n");
   2960       sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
   2961                    callback, &data, &zErrMsg);
   2962       data.mode = MODE_Insert;
   2963       data.zDestTable = "sqlite_stat1";
   2964       shell_exec(p->db, "SELECT * FROM sqlite_stat1",
   2965                  shell_callback, &data,&zErrMsg);
   2966       data.zDestTable = "sqlite_stat3";
   2967       shell_exec(p->db, "SELECT * FROM sqlite_stat3",
   2968                  shell_callback, &data,&zErrMsg);
   2969       data.zDestTable = "sqlite_stat4";
   2970       shell_exec(p->db, "SELECT * FROM sqlite_stat4",
   2971                  shell_callback, &data, &zErrMsg);
   2972       fprintf(p->out, "ANALYZE sqlite_master;\n");
   2973     }
   2974   }else
   2975 
   2976   if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
   2977     if( nArg==2 ){
   2978       p->showHeader = booleanValue(azArg[1]);
   2979     }else{
   2980       fprintf(stderr, "Usage: .headers on|off\n");
   2981       rc = 1;
   2982     }
   2983   }else
   2984 
   2985   if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
   2986     fprintf(p->out, "%s", zHelp);
   2987   }else
   2988 
   2989   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
   2990     char *zTable;               /* Insert data into this table */
   2991     char *zFile;                /* Name of file to extra content from */
   2992     sqlite3_stmt *pStmt = NULL; /* A statement */
   2993     int nCol;                   /* Number of columns in the table */
   2994     int nByte;                  /* Number of bytes in an SQL string */
   2995     int i, j;                   /* Loop counters */
   2996     int needCommit;             /* True to COMMIT or ROLLBACK at end */
   2997     int nSep;                   /* Number of bytes in p->colSeparator[] */
   2998     char *zSql;                 /* An SQL statement */
   2999     ImportCtx sCtx;             /* Reader context */
   3000     char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
   3001     int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
   3002 
   3003     if( nArg!=3 ){
   3004       fprintf(stderr, "Usage: .import FILE TABLE\n");
   3005       goto meta_command_exit;
   3006     }
   3007     zFile = azArg[1];
   3008     zTable = azArg[2];
   3009     seenInterrupt = 0;
   3010     memset(&sCtx, 0, sizeof(sCtx));
   3011     open_db(p, 0);
   3012     nSep = strlen30(p->colSeparator);
   3013     if( nSep==0 ){
   3014       fprintf(stderr, "Error: non-null column separator required for import\n");
   3015       return 1;
   3016     }
   3017     if( nSep>1 ){
   3018       fprintf(stderr, "Error: multi-character column separators not allowed"
   3019                       " for import\n");
   3020       return 1;
   3021     }
   3022     nSep = strlen30(p->rowSeparator);
   3023     if( nSep==0 ){
   3024       fprintf(stderr, "Error: non-null row separator required for import\n");
   3025       return 1;
   3026     }
   3027     if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
   3028       /* When importing CSV (only), if the row separator is set to the
   3029       ** default output row separator, change it to the default input
   3030       ** row separator.  This avoids having to maintain different input
   3031       ** and output row separators. */
   3032       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
   3033       nSep = strlen30(p->rowSeparator);
   3034     }
   3035     if( nSep>1 ){
   3036       fprintf(stderr, "Error: multi-character row separators not allowed"
   3037                       " for import\n");
   3038       return 1;
   3039     }
   3040     sCtx.zFile = zFile;
   3041     sCtx.nLine = 1;
   3042     if( sCtx.zFile[0]=='|' ){
   3043 #ifdef SQLITE_OMIT_POPEN
   3044       fprintf(stderr, "Error: pipes are not supported in this OS\n");
   3045       return 1;
   3046 #else
   3047       sCtx.in = popen(sCtx.zFile+1, "r");
   3048       sCtx.zFile = "<pipe>";
   3049       xCloser = pclose;
   3050 #endif
   3051     }else{
   3052       sCtx.in = fopen(sCtx.zFile, "rb");
   3053       xCloser = fclose;
   3054     }
   3055     if( p->mode==MODE_Ascii ){
   3056       xRead = ascii_read_one_field;
   3057     }else{
   3058       xRead = csv_read_one_field;
   3059     }
   3060     if( sCtx.in==0 ){
   3061       fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
   3062       return 1;
   3063     }
   3064     sCtx.cColSep = p->colSeparator[0];
   3065     sCtx.cRowSep = p->rowSeparator[0];
   3066     zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
   3067     if( zSql==0 ){
   3068       fprintf(stderr, "Error: out of memory\n");
   3069       xCloser(sCtx.in);
   3070       return 1;
   3071     }
   3072     nByte = strlen30(zSql);
   3073     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
   3074     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
   3075     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
   3076       char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
   3077       char cSep = '(';
   3078       while( xRead(&sCtx) ){
   3079         zCreate = sqlite3_mprintf("%z%c\n  \"%s\" TEXT", zCreate, cSep, sCtx.z);
   3080         cSep = ',';
   3081         if( sCtx.cTerm!=sCtx.cColSep ) break;
   3082       }
   3083       if( cSep=='(' ){
   3084         sqlite3_free(zCreate);
   3085         sqlite3_free(sCtx.z);
   3086         xCloser(sCtx.in);
   3087         fprintf(stderr,"%s: empty file\n", sCtx.zFile);
   3088         return 1;
   3089       }
   3090       zCreate = sqlite3_mprintf("%z\n)", zCreate);
   3091       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
   3092       sqlite3_free(zCreate);
   3093       if( rc ){
   3094         fprintf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
   3095                 sqlite3_errmsg(p->db));
   3096         sqlite3_free(sCtx.z);
   3097         xCloser(sCtx.in);
   3098         return 1;
   3099       }
   3100       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
   3101     }
   3102     sqlite3_free(zSql);
   3103     if( rc ){
   3104       if (pStmt) sqlite3_finalize(pStmt);
   3105       fprintf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
   3106       xCloser(sCtx.in);
   3107       return 1;
   3108     }
   3109     nCol = sqlite3_column_count(pStmt);
   3110     sqlite3_finalize(pStmt);
   3111     pStmt = 0;
   3112     if( nCol==0 ) return 0; /* no columns, no error */
   3113     zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
   3114     if( zSql==0 ){
   3115       fprintf(stderr, "Error: out of memory\n");
   3116       xCloser(sCtx.in);
   3117       return 1;
   3118     }
   3119     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
   3120     j = strlen30(zSql);
   3121     for(i=1; i<nCol; i++){
   3122       zSql[j++] = ',';
   3123       zSql[j++] = '?';
   3124     }
   3125     zSql[j++] = ')';
   3126     zSql[j] = 0;
   3127     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
   3128     sqlite3_free(zSql);
   3129     if( rc ){
   3130       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
   3131       if (pStmt) sqlite3_finalize(pStmt);
   3132       xCloser(sCtx.in);
   3133       return 1;
   3134     }
   3135     needCommit = sqlite3_get_autocommit(p->db);
   3136     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
   3137     do{
   3138       int startLine = sCtx.nLine;
   3139       for(i=0; i<nCol; i++){
   3140         char *z = xRead(&sCtx);
   3141         /*
   3142         ** Did we reach end-of-file before finding any columns?
   3143         ** If so, stop instead of NULL filling the remaining columns.
   3144         */
   3145         if( z==0 && i==0 ) break;
   3146         /*
   3147         ** Did we reach end-of-file OR end-of-line before finding any
   3148         ** columns in ASCII mode?  If so, stop instead of NULL filling
   3149         ** the remaining columns.
   3150         */
   3151         if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
   3152         sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
   3153         if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
   3154           fprintf(stderr, "%s:%d: expected %d columns but found %d - "
   3155                           "filling the rest with NULL\n",
   3156                           sCtx.zFile, startLine, nCol, i+1);
   3157           i += 2;
   3158           while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
   3159         }
   3160       }
   3161       if( sCtx.cTerm==sCtx.cColSep ){
   3162         do{
   3163           xRead(&sCtx);
   3164           i++;
   3165         }while( sCtx.cTerm==sCtx.cColSep );
   3166         fprintf(stderr, "%s:%d: expected %d columns but found %d - "
   3167                         "extras ignored\n",
   3168                         sCtx.zFile, startLine, nCol, i);
   3169       }
   3170       if( i>=nCol ){
   3171         sqlite3_step(pStmt);
   3172         rc = sqlite3_reset(pStmt);
   3173         if( rc!=SQLITE_OK ){
   3174           fprintf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, startLine,
   3175                   sqlite3_errmsg(p->db));
   3176         }
   3177       }
   3178     }while( sCtx.cTerm!=EOF );
   3179 
   3180     xCloser(sCtx.in);
   3181     sqlite3_free(sCtx.z);
   3182     sqlite3_finalize(pStmt);
   3183     if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
   3184   }else
   3185 
   3186   if( c=='i' && (strncmp(azArg[0], "indices", n)==0
   3187                  || strncmp(azArg[0], "indexes", n)==0) ){
   3188     ShellState data;
   3189     char *zErrMsg = 0;
   3190     open_db(p, 0);
   3191     memcpy(&data, p, sizeof(data));
   3192     data.showHeader = 0;
   3193     data.mode = MODE_List;
   3194     if( nArg==1 ){
   3195       rc = sqlite3_exec(p->db,
   3196         "SELECT name FROM sqlite_master "
   3197         "WHERE type='index' AND name NOT LIKE 'sqlite_%' "
   3198         "UNION ALL "
   3199         "SELECT name FROM sqlite_temp_master "
   3200         "WHERE type='index' "
   3201         "ORDER BY 1",
   3202         callback, &data, &zErrMsg
   3203       );
   3204     }else if( nArg==2 ){
   3205       zShellStatic = azArg[1];
   3206       rc = sqlite3_exec(p->db,
   3207         "SELECT name FROM sqlite_master "
   3208         "WHERE type='index' AND tbl_name LIKE shellstatic() "
   3209         "UNION ALL "
   3210         "SELECT name FROM sqlite_temp_master "
   3211         "WHERE type='index' AND tbl_name LIKE shellstatic() "
   3212         "ORDER BY 1",
   3213         callback, &data, &zErrMsg
   3214       );
   3215       zShellStatic = 0;
   3216     }else{
   3217       fprintf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
   3218       rc = 1;
   3219       goto meta_command_exit;
   3220     }
   3221     if( zErrMsg ){
   3222       fprintf(stderr,"Error: %s\n", zErrMsg);
   3223       sqlite3_free(zErrMsg);
   3224       rc = 1;
   3225     }else if( rc != SQLITE_OK ){
   3226       fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
   3227       rc = 1;
   3228     }
   3229   }else
   3230 
   3231 #ifdef SQLITE_ENABLE_IOTRACE
   3232   if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
   3233     SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
   3234     if( iotrace && iotrace!=stdout ) fclose(iotrace);
   3235     iotrace = 0;
   3236     if( nArg<2 ){
   3237       sqlite3IoTrace = 0;
   3238     }else if( strcmp(azArg[1], "-")==0 ){
   3239       sqlite3IoTrace = iotracePrintf;
   3240       iotrace = stdout;
   3241     }else{
   3242       iotrace = fopen(azArg[1], "w");
   3243       if( iotrace==0 ){
   3244         fprintf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
   3245         sqlite3IoTrace = 0;
   3246         rc = 1;
   3247       }else{
   3248         sqlite3IoTrace = iotracePrintf;
   3249       }
   3250     }
   3251   }else
   3252 #endif
   3253   if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
   3254     static const struct {
   3255        const char *zLimitName;   /* Name of a limit */
   3256        int limitCode;            /* Integer code for that limit */
   3257     } aLimit[] = {
   3258       { "length",                SQLITE_LIMIT_LENGTH                    },
   3259       { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
   3260       { "column",                SQLITE_LIMIT_COLUMN                    },
   3261       { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
   3262       { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
   3263       { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
   3264       { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
   3265       { "attached",              SQLITE_LIMIT_ATTACHED                  },
   3266       { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
   3267       { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
   3268       { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
   3269       { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
   3270     };
   3271     int i, n2;
   3272     open_db(p, 0);
   3273     if( nArg==1 ){
   3274       for(i=0; i<ArraySize(aLimit); i++){
   3275         printf("%20s %d\n", aLimit[i].zLimitName,
   3276                sqlite3_limit(p->db, aLimit[i].limitCode, -1));
   3277       }
   3278     }else if( nArg>3 ){
   3279       fprintf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
   3280       rc = 1;
   3281       goto meta_command_exit;
   3282     }else{
   3283       int iLimit = -1;
   3284       n2 = strlen30(azArg[1]);
   3285       for(i=0; i<ArraySize(aLimit); i++){
   3286         if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
   3287           if( iLimit<0 ){
   3288             iLimit = i;
   3289           }else{
   3290             fprintf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
   3291             rc = 1;
   3292             goto meta_command_exit;
   3293           }
   3294         }
   3295       }
   3296       if( iLimit<0 ){
   3297         fprintf(stderr, "unknown limit: \"%s\"\n"
   3298                         "enter \".limits\" with no arguments for a list.\n",
   3299                          azArg[1]);
   3300         rc = 1;
   3301         goto meta_command_exit;
   3302       }
   3303       if( nArg==3 ){
   3304         sqlite3_limit(p->db, aLimit[iLimit].limitCode,
   3305                       (int)integerValue(azArg[2]));
   3306       }
   3307       printf("%20s %d\n", aLimit[iLimit].zLimitName,
   3308              sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
   3309     }
   3310   }else
   3311 
   3312 #ifndef SQLITE_OMIT_LOAD_EXTENSION
   3313   if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
   3314     const char *zFile, *zProc;
   3315     char *zErrMsg = 0;
   3316     if( nArg<2 ){
   3317       fprintf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
   3318       rc = 1;
   3319       goto meta_command_exit;
   3320     }
   3321     zFile = azArg[1];
   3322     zProc = nArg>=3 ? azArg[2] : 0;
   3323     open_db(p, 0);
   3324     rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
   3325     if( rc!=SQLITE_OK ){
   3326       fprintf(stderr, "Error: %s\n", zErrMsg);
   3327       sqlite3_free(zErrMsg);
   3328       rc = 1;
   3329     }
   3330   }else
   3331 #endif
   3332 
   3333   if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
   3334     if( nArg!=2 ){
   3335       fprintf(stderr, "Usage: .log FILENAME\n");
   3336       rc = 1;
   3337     }else{
   3338       const char *zFile = azArg[1];
   3339       output_file_close(p->pLog);
   3340       p->pLog = output_file_open(zFile);
   3341     }
   3342   }else
   3343 
   3344   if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
   3345     const char *zMode = nArg>=2 ? azArg[1] : "";
   3346     int n2 = (int)strlen(zMode);
   3347     int c2 = zMode[0];
   3348     if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
   3349       p->mode = MODE_Line;
   3350     }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
   3351       p->mode = MODE_Column;
   3352     }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
   3353       p->mode = MODE_List;
   3354     }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
   3355       p->mode = MODE_Html;
   3356     }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
   3357       p->mode = MODE_Tcl;
   3358       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
   3359     }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
   3360       p->mode = MODE_Csv;
   3361       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
   3362       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
   3363     }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
   3364       p->mode = MODE_List;
   3365       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
   3366     }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
   3367       p->mode = MODE_Insert;
   3368       set_table_name(p, nArg>=3 ? azArg[2] : "table");
   3369     }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
   3370       p->mode = MODE_Ascii;
   3371       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
   3372       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
   3373     }else {
   3374       fprintf(stderr,"Error: mode should be one of: "
   3375          "ascii column csv html insert line list tabs tcl\n");
   3376       rc = 1;
   3377     }
   3378   }else
   3379 
   3380   if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
   3381     if( nArg==2 ){
   3382       sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
   3383                        "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
   3384     }else{
   3385       fprintf(stderr, "Usage: .nullvalue STRING\n");
   3386       rc = 1;
   3387     }
   3388   }else
   3389 
   3390   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
   3391     sqlite3 *savedDb = p->db;
   3392     const char *zSavedFilename = p->zDbFilename;
   3393     char *zNewFilename = 0;
   3394     p->db = 0;
   3395     if( nArg>=2 ) zNewFilename = sqlite3_mprintf("%s", azArg[1]);
   3396     p->zDbFilename = zNewFilename;
   3397     open_db(p, 1);
   3398     if( p->db!=0 ){
   3399       sqlite3_close(savedDb);
   3400       sqlite3_free(p->zFreeOnClose);
   3401       p->zFreeOnClose = zNewFilename;
   3402     }else{
   3403       sqlite3_free(zNewFilename);
   3404       p->db = savedDb;
   3405       p->zDbFilename = zSavedFilename;
   3406     }
   3407   }else
   3408 
   3409   if( c=='o'
   3410    && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0)
   3411   ){
   3412     const char *zFile = nArg>=2 ? azArg[1] : "stdout";
   3413     if( nArg>2 ){
   3414       fprintf(stderr, "Usage: .%s FILE\n", azArg[0]);
   3415       rc = 1;
   3416       goto meta_command_exit;
   3417     }
   3418     if( n>1 && strncmp(azArg[0], "once", n)==0 ){
   3419       if( nArg<2 ){
   3420         fprintf(stderr, "Usage: .once FILE\n");
   3421         rc = 1;
   3422         goto meta_command_exit;
   3423       }
   3424       p->outCount = 2;
   3425     }else{
   3426       p->outCount = 0;
   3427     }
   3428     output_reset(p);
   3429     if( zFile[0]=='|' ){
   3430 #ifdef SQLITE_OMIT_POPEN
   3431       fprintf(stderr,"Error: pipes are not supported in this OS\n");
   3432       rc = 1;
   3433       p->out = stdout;
   3434 #else
   3435       p->out = popen(zFile + 1, "w");
   3436       if( p->out==0 ){
   3437         fprintf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
   3438         p->out = stdout;
   3439         rc = 1;
   3440       }else{
   3441         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
   3442       }
   3443 #endif
   3444     }else{
   3445       p->out = output_file_open(zFile);
   3446       if( p->out==0 ){
   3447         if( strcmp(zFile,"off")!=0 ){
   3448           fprintf(stderr,"Error: cannot write to \"%s\"\n", zFile);
   3449         }
   3450         p->out = stdout;
   3451         rc = 1;
   3452       } else {
   3453         sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
   3454       }
   3455     }
   3456   }else
   3457 
   3458   if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
   3459     int i;
   3460     for(i=1; i<nArg; i++){
   3461       if( i>1 ) fprintf(p->out, " ");
   3462       fprintf(p->out, "%s", azArg[i]);
   3463     }
   3464     fprintf(p->out, "\n");
   3465   }else
   3466 
   3467   if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
   3468     if( nArg >= 2) {
   3469       strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
   3470     }
   3471     if( nArg >= 3) {
   3472       strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
   3473     }
   3474   }else
   3475 
   3476   if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
   3477     rc = 2;
   3478   }else
   3479 
   3480   if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
   3481     FILE *alt;
   3482     if( nArg!=2 ){
   3483       fprintf(stderr, "Usage: .read FILE\n");
   3484       rc = 1;
   3485       goto meta_command_exit;
   3486     }
   3487     alt = fopen(azArg[1], "rb");
   3488     if( alt==0 ){
   3489       fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
   3490       rc = 1;
   3491     }else{
   3492       rc = process_input(p, alt);
   3493       fclose(alt);
   3494     }
   3495   }else
   3496 
   3497   if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
   3498     const char *zSrcFile;
   3499     const char *zDb;
   3500     sqlite3 *pSrc;
   3501     sqlite3_backup *pBackup;
   3502     int nTimeout = 0;
   3503 
   3504     if( nArg==2 ){
   3505       zSrcFile = azArg[1];
   3506       zDb = "main";
   3507     }else if( nArg==3 ){
   3508       zSrcFile = azArg[2];
   3509       zDb = azArg[1];
   3510     }else{
   3511       fprintf(stderr, "Usage: .restore ?DB? FILE\n");
   3512       rc = 1;
   3513       goto meta_command_exit;
   3514     }
   3515     rc = sqlite3_open(zSrcFile, &pSrc);
   3516     if( rc!=SQLITE_OK ){
   3517       fprintf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
   3518       sqlite3_close(pSrc);
   3519       return 1;
   3520     }
   3521     open_db(p, 0);
   3522     pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
   3523     if( pBackup==0 ){
   3524       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
   3525       sqlite3_close(pSrc);
   3526       return 1;
   3527     }
   3528     while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
   3529           || rc==SQLITE_BUSY  ){
   3530       if( rc==SQLITE_BUSY ){
   3531         if( nTimeout++ >= 3 ) break;
   3532         sqlite3_sleep(100);
   3533       }
   3534     }
   3535     sqlite3_backup_finish(pBackup);
   3536     if( rc==SQLITE_DONE ){
   3537       rc = 0;
   3538     }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
   3539       fprintf(stderr, "Error: source database is busy\n");
   3540       rc = 1;
   3541     }else{
   3542       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
   3543       rc = 1;
   3544     }
   3545     sqlite3_close(pSrc);
   3546   }else
   3547 
   3548 
   3549   if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
   3550     if( nArg==2 ){
   3551       p->scanstatsOn = booleanValue(azArg[1]);
   3552 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
   3553       fprintf(stderr, "Warning: .scanstats not available in this build.\n");
   3554 #endif
   3555     }else{
   3556       fprintf(stderr, "Usage: .scanstats on|off\n");
   3557       rc = 1;
   3558     }
   3559   }else
   3560 
   3561   if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
   3562     ShellState data;
   3563     char *zErrMsg = 0;
   3564     open_db(p, 0);
   3565     memcpy(&data, p, sizeof(data));
   3566     data.showHeader = 0;
   3567     data.mode = MODE_Semi;
   3568     if( nArg==2 ){
   3569       int i;
   3570       for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
   3571       if( strcmp(azArg[1],"sqlite_master")==0 ){
   3572         char *new_argv[2], *new_colv[2];
   3573         new_argv[0] = "CREATE TABLE sqlite_master (\n"
   3574                       "  type text,\n"
   3575                       "  name text,\n"
   3576                       "  tbl_name text,\n"
   3577                       "  rootpage integer,\n"
   3578                       "  sql text\n"
   3579                       ")";
   3580         new_argv[1] = 0;
   3581         new_colv[0] = "sql";
   3582         new_colv[1] = 0;
   3583         callback(&data, 1, new_argv, new_colv);
   3584         rc = SQLITE_OK;
   3585       }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
   3586         char *new_argv[2], *new_colv[2];
   3587         new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
   3588                       "  type text,\n"
   3589                       "  name text,\n"
   3590                       "  tbl_name text,\n"
   3591                       "  rootpage integer,\n"
   3592                       "  sql text\n"
   3593                       ")";
   3594         new_argv[1] = 0;
   3595         new_colv[0] = "sql";
   3596         new_colv[1] = 0;
   3597         callback(&data, 1, new_argv, new_colv);
   3598         rc = SQLITE_OK;
   3599       }else{
   3600         zShellStatic = azArg[1];
   3601         rc = sqlite3_exec(p->db,
   3602           "SELECT sql FROM "
   3603           "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
   3604           "     FROM sqlite_master UNION ALL"
   3605           "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
   3606           "WHERE lower(tbl_name) LIKE shellstatic()"
   3607           "  AND type!='meta' AND sql NOTNULL "
   3608           "ORDER BY rowid",
   3609           callback, &data, &zErrMsg);
   3610         zShellStatic = 0;
   3611       }
   3612     }else if( nArg==1 ){
   3613       rc = sqlite3_exec(p->db,
   3614          "SELECT sql FROM "
   3615          "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
   3616          "     FROM sqlite_master UNION ALL"
   3617          "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
   3618          "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
   3619          "ORDER BY rowid",
   3620          callback, &data, &zErrMsg
   3621       );
   3622     }else{
   3623       fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
   3624       rc = 1;
   3625       goto meta_command_exit;
   3626     }
   3627     if( zErrMsg ){
   3628       fprintf(stderr,"Error: %s\n", zErrMsg);
   3629       sqlite3_free(zErrMsg);
   3630       rc = 1;
   3631     }else if( rc != SQLITE_OK ){
   3632       fprintf(stderr,"Error: querying schema information\n");
   3633       rc = 1;
   3634     }else{
   3635       rc = 0;
   3636     }
   3637   }else
   3638 
   3639 
   3640 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
   3641   if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
   3642     extern int sqlite3SelectTrace;
   3643     sqlite3SelectTrace = integerValue(azArg[1]);
   3644   }else
   3645 #endif
   3646 
   3647 
   3648 #ifdef SQLITE_DEBUG
   3649   /* Undocumented commands for internal testing.  Subject to change
   3650   ** without notice. */
   3651   if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
   3652     if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
   3653       int i, v;
   3654       for(i=1; i<nArg; i++){
   3655         v = booleanValue(azArg[i]);
   3656         fprintf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
   3657       }
   3658     }
   3659     if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
   3660       int i; sqlite3_int64 v;
   3661       for(i=1; i<nArg; i++){
   3662         char zBuf[200];
   3663         v = integerValue(azArg[i]);
   3664         sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
   3665         fprintf(p->out, "%s", zBuf);
   3666       }
   3667     }
   3668   }else
   3669 #endif
   3670 
   3671   if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
   3672     if( nArg<2 || nArg>3 ){
   3673       fprintf(stderr, "Usage: .separator COL ?ROW?\n");
   3674       rc = 1;
   3675     }
   3676     if( nArg>=2 ){
   3677       sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
   3678                        "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
   3679     }
   3680     if( nArg>=3 ){
   3681       sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
   3682                        "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
   3683     }
   3684   }else
   3685 
   3686   if( c=='s'
   3687    && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
   3688   ){
   3689     char *zCmd;
   3690     int i, x;
   3691     if( nArg<2 ){
   3692       fprintf(stderr, "Usage: .system COMMAND\n");
   3693       rc = 1;
   3694       goto meta_command_exit;
   3695     }
   3696     zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
   3697     for(i=2; i<nArg; i++){
   3698       zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
   3699                              zCmd, azArg[i]);
   3700     }
   3701     x = system(zCmd);
   3702     sqlite3_free(zCmd);
   3703     if( x ) fprintf(stderr, "System command returns %d\n", x);
   3704   }else
   3705 
   3706   if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
   3707     int i;
   3708     if( nArg!=1 ){
   3709       fprintf(stderr, "Usage: .show\n");
   3710       rc = 1;
   3711       goto meta_command_exit;
   3712     }
   3713     fprintf(p->out,"%12.12s: %s\n","echo", p->echoOn ? "on" : "off");
   3714     fprintf(p->out,"%12.12s: %s\n","eqp", p->autoEQP ? "on" : "off");
   3715     fprintf(p->out,"%9.9s: %s\n","explain", p->normalMode.valid ? "on" :"off");
   3716     fprintf(p->out,"%12.12s: %s\n","headers", p->showHeader ? "on" : "off");
   3717     fprintf(p->out,"%12.12s: %s\n","mode", modeDescr[p->mode]);
   3718     fprintf(p->out,"%12.12s: ", "nullvalue");
   3719       output_c_string(p->out, p->nullValue);
   3720       fprintf(p->out, "\n");
   3721     fprintf(p->out,"%12.12s: %s\n","output",
   3722             strlen30(p->outfile) ? p->outfile : "stdout");
   3723     fprintf(p->out,"%12.12s: ", "colseparator");
   3724       output_c_string(p->out, p->colSeparator);
   3725       fprintf(p->out, "\n");
   3726     fprintf(p->out,"%12.12s: ", "rowseparator");
   3727       output_c_string(p->out, p->rowSeparator);
   3728       fprintf(p->out, "\n");
   3729     fprintf(p->out,"%12.12s: %s\n","stats", p->statsOn ? "on" : "off");
   3730     fprintf(p->out,"%12.12s: ","width");
   3731     for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
   3732       fprintf(p->out,"%d ",p->colWidth[i]);
   3733     }
   3734     fprintf(p->out,"\n");
   3735   }else
   3736 
   3737   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
   3738     if( nArg==2 ){
   3739       p->statsOn = booleanValue(azArg[1]);
   3740     }else{
   3741       fprintf(stderr, "Usage: .stats on|off\n");
   3742       rc = 1;
   3743     }
   3744   }else
   3745 
   3746   if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){
   3747     sqlite3_stmt *pStmt;
   3748     char **azResult;
   3749     int nRow, nAlloc;
   3750     char *zSql = 0;
   3751     int ii;
   3752     open_db(p, 0);
   3753     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
   3754     if( rc ) return shellDatabaseError(p->db);
   3755 
   3756     /* Create an SQL statement to query for the list of tables in the
   3757     ** main and all attached databases where the table name matches the
   3758     ** LIKE pattern bound to variable "?1". */
   3759     zSql = sqlite3_mprintf(
   3760         "SELECT name FROM sqlite_master"
   3761         " WHERE type IN ('table','view')"
   3762         "   AND name NOT LIKE 'sqlite_%%'"
   3763         "   AND name LIKE ?1");
   3764     while( zSql && sqlite3_step(pStmt)==SQLITE_ROW ){
   3765       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
   3766       if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
   3767       if( strcmp(zDbName,"temp")==0 ){
   3768         zSql = sqlite3_mprintf(
   3769                  "%z UNION ALL "
   3770                  "SELECT 'temp.' || name FROM sqlite_temp_master"
   3771                  " WHERE type IN ('table','view')"
   3772                  "   AND name NOT LIKE 'sqlite_%%'"
   3773                  "   AND name LIKE ?1", zSql);
   3774       }else{
   3775         zSql = sqlite3_mprintf(
   3776                  "%z UNION ALL "
   3777                  "SELECT '%q.' || name FROM \"%w\".sqlite_master"
   3778                  " WHERE type IN ('table','view')"
   3779                  "   AND name NOT LIKE 'sqlite_%%'"
   3780                  "   AND name LIKE ?1", zSql, zDbName, zDbName);
   3781       }
   3782     }
   3783     rc = sqlite3_finalize(pStmt);
   3784     if( zSql && rc==SQLITE_OK ){
   3785       zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
   3786       if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
   3787     }
   3788     sqlite3_free(zSql);
   3789     if( !zSql ) return shellNomemError();
   3790     if( rc ) return shellDatabaseError(p->db);
   3791 
   3792     /* Run the SQL statement prepared by the above block. Store the results
   3793     ** as an array of nul-terminated strings in azResult[].  */
   3794     nRow = nAlloc = 0;
   3795     azResult = 0;
   3796     if( nArg>1 ){
   3797       sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
   3798     }else{
   3799       sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
   3800     }
   3801     while( sqlite3_step(pStmt)==SQLITE_ROW ){
   3802       if( nRow>=nAlloc ){
   3803         char **azNew;
   3804         int n2 = nAlloc*2 + 10;
   3805         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
   3806         if( azNew==0 ){
   3807           rc = shellNomemError();
   3808           break;
   3809         }
   3810         nAlloc = n2;
   3811         azResult = azNew;
   3812       }
   3813       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
   3814       if( 0==azResult[nRow] ){
   3815         rc = shellNomemError();
   3816         break;
   3817       }
   3818       nRow++;
   3819     }
   3820     if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
   3821       rc = shellDatabaseError(p->db);
   3822     }
   3823 
   3824     /* Pretty-print the contents of array azResult[] to the output */
   3825     if( rc==0 && nRow>0 ){
   3826       int len, maxlen = 0;
   3827       int i, j;
   3828       int nPrintCol, nPrintRow;
   3829       for(i=0; i<nRow; i++){
   3830         len = strlen30(azResult[i]);
   3831         if( len>maxlen ) maxlen = len;
   3832       }
   3833       nPrintCol = 80/(maxlen+2);
   3834       if( nPrintCol<1 ) nPrintCol = 1;
   3835       nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
   3836       for(i=0; i<nPrintRow; i++){
   3837         for(j=i; j<nRow; j+=nPrintRow){
   3838           char *zSp = j<nPrintRow ? "" : "  ";
   3839           fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
   3840         }
   3841         fprintf(p->out, "\n");
   3842       }
   3843     }
   3844 
   3845     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
   3846     sqlite3_free(azResult);
   3847   }else
   3848 
   3849   if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
   3850     static const struct {
   3851        const char *zCtrlName;   /* Name of a test-control option */
   3852        int ctrlCode;            /* Integer code for that option */
   3853     } aCtrl[] = {
   3854       { "prng_save",             SQLITE_TESTCTRL_PRNG_SAVE              },
   3855       { "prng_restore",          SQLITE_TESTCTRL_PRNG_RESTORE           },
   3856       { "prng_reset",            SQLITE_TESTCTRL_PRNG_RESET             },
   3857       { "bitvec_test",           SQLITE_TESTCTRL_BITVEC_TEST            },
   3858       { "fault_install",         SQLITE_TESTCTRL_FAULT_INSTALL          },
   3859       { "benign_malloc_hooks",   SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS    },
   3860       { "pending_byte",          SQLITE_TESTCTRL_PENDING_BYTE           },
   3861       { "assert",                SQLITE_TESTCTRL_ASSERT                 },
   3862       { "always",                SQLITE_TESTCTRL_ALWAYS                 },
   3863       { "reserve",               SQLITE_TESTCTRL_RESERVE                },
   3864       { "optimizations",         SQLITE_TESTCTRL_OPTIMIZATIONS          },
   3865       { "iskeyword",             SQLITE_TESTCTRL_ISKEYWORD              },
   3866       { "scratchmalloc",         SQLITE_TESTCTRL_SCRATCHMALLOC          },
   3867       { "byteorder",             SQLITE_TESTCTRL_BYTEORDER              },
   3868       { "never_corrupt",         SQLITE_TESTCTRL_NEVER_CORRUPT          },
   3869       { "imposter",              SQLITE_TESTCTRL_IMPOSTER               },
   3870     };
   3871     int testctrl = -1;
   3872     int rc2 = 0;
   3873     int i, n2;
   3874     open_db(p, 0);
   3875 
   3876     /* convert testctrl text option to value. allow any unique prefix
   3877     ** of the option name, or a numerical value. */
   3878     n2 = strlen30(azArg[1]);
   3879     for(i=0; i<ArraySize(aCtrl); i++){
   3880       if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
   3881         if( testctrl<0 ){
   3882           testctrl = aCtrl[i].ctrlCode;
   3883         }else{
   3884           fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
   3885           testctrl = -1;
   3886           break;
   3887         }
   3888       }
   3889     }
   3890     if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]);
   3891     if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
   3892       fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
   3893     }else{
   3894       switch(testctrl){
   3895 
   3896         /* sqlite3_test_control(int, db, int) */
   3897         case SQLITE_TESTCTRL_OPTIMIZATIONS:
   3898         case SQLITE_TESTCTRL_RESERVE:
   3899           if( nArg==3 ){
   3900             int opt = (int)strtol(azArg[2], 0, 0);
   3901             rc2 = sqlite3_test_control(testctrl, p->db, opt);
   3902             fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
   3903           } else {
   3904             fprintf(stderr,"Error: testctrl %s takes a single int option\n",
   3905                     azArg[1]);
   3906           }
   3907           break;
   3908 
   3909         /* sqlite3_test_control(int) */
   3910         case SQLITE_TESTCTRL_PRNG_SAVE:
   3911         case SQLITE_TESTCTRL_PRNG_RESTORE:
   3912         case SQLITE_TESTCTRL_PRNG_RESET:
   3913         case SQLITE_TESTCTRL_BYTEORDER:
   3914           if( nArg==2 ){
   3915             rc2 = sqlite3_test_control(testctrl);
   3916             fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
   3917           } else {
   3918             fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
   3919           }
   3920           break;
   3921 
   3922         /* sqlite3_test_control(int, uint) */
   3923         case SQLITE_TESTCTRL_PENDING_BYTE:
   3924           if( nArg==3 ){
   3925             unsigned int opt = (unsigned int)integerValue(azArg[2]);
   3926             rc2 = sqlite3_test_control(testctrl, opt);
   3927             fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
   3928           } else {
   3929             fprintf(stderr,"Error: testctrl %s takes a single unsigned"
   3930                            " int option\n", azArg[1]);
   3931           }
   3932           break;
   3933 
   3934         /* sqlite3_test_control(int, int) */
   3935         case SQLITE_TESTCTRL_ASSERT:
   3936         case SQLITE_TESTCTRL_ALWAYS:
   3937         case SQLITE_TESTCTRL_NEVER_CORRUPT:
   3938           if( nArg==3 ){
   3939             int opt = booleanValue(azArg[2]);
   3940             rc2 = sqlite3_test_control(testctrl, opt);
   3941             fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
   3942           } else {
   3943             fprintf(stderr,"Error: testctrl %s takes a single int option\n",
   3944                             azArg[1]);
   3945           }
   3946           break;
   3947 
   3948         /* sqlite3_test_control(int, char *) */
   3949 #ifdef SQLITE_N_KEYWORD
   3950         case SQLITE_TESTCTRL_ISKEYWORD:
   3951           if( nArg==3 ){
   3952             const char *opt = azArg[2];
   3953             rc2 = sqlite3_test_control(testctrl, opt);
   3954             fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
   3955           } else {
   3956             fprintf(stderr,"Error: testctrl %s takes a single char * option\n",
   3957                             azArg[1]);
   3958           }
   3959           break;
   3960 #endif
   3961 
   3962         case SQLITE_TESTCTRL_IMPOSTER:
   3963           if( nArg==5 ){
   3964             rc2 = sqlite3_test_control(testctrl, p->db,
   3965                           azArg[2],
   3966                           integerValue(azArg[3]),
   3967                           integerValue(azArg[4]));
   3968             fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
   3969           }else{
   3970             fprintf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
   3971           }
   3972           break;
   3973 
   3974         case SQLITE_TESTCTRL_BITVEC_TEST:
   3975         case SQLITE_TESTCTRL_FAULT_INSTALL:
   3976         case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
   3977         case SQLITE_TESTCTRL_SCRATCHMALLOC:
   3978         default:
   3979           fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n",
   3980                   azArg[1]);
   3981           break;
   3982       }
   3983     }
   3984   }else
   3985 
   3986   if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
   3987     open_db(p, 0);
   3988     sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
   3989   }else
   3990 
   3991   if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
   3992     if( nArg==2 ){
   3993       enableTimer = booleanValue(azArg[1]);
   3994       if( enableTimer && !HAS_TIMER ){
   3995         fprintf(stderr, "Error: timer not available on this system.\n");
   3996         enableTimer = 0;
   3997       }
   3998     }else{
   3999       fprintf(stderr, "Usage: .timer on|off\n");
   4000       rc = 1;
   4001     }
   4002   }else
   4003 
   4004   if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
   4005     open_db(p, 0);
   4006     if( nArg!=2 ){
   4007       fprintf(stderr, "Usage: .trace FILE|off\n");
   4008       rc = 1;
   4009       goto meta_command_exit;
   4010     }
   4011     output_file_close(p->traceOut);
   4012     p->traceOut = output_file_open(azArg[1]);
   4013 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
   4014     if( p->traceOut==0 ){
   4015       sqlite3_trace(p->db, 0, 0);
   4016     }else{
   4017       sqlite3_trace(p->db, sql_trace_callback, p->traceOut);
   4018     }
   4019 #endif
   4020   }else
   4021 
   4022 #if SQLITE_USER_AUTHENTICATION
   4023   if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
   4024     if( nArg<2 ){
   4025       fprintf(stderr, "Usage: .user SUBCOMMAND ...\n");
   4026       rc = 1;
   4027       goto meta_command_exit;
   4028     }
   4029     open_db(p, 0);
   4030     if( strcmp(azArg[1],"login")==0 ){
   4031       if( nArg!=4 ){
   4032         fprintf(stderr, "Usage: .user login USER PASSWORD\n");
   4033         rc = 1;
   4034         goto meta_command_exit;
   4035       }
   4036       rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
   4037                                     (int)strlen(azArg[3]));
   4038       if( rc ){
   4039         fprintf(stderr, "Authentication failed for user %s\n", azArg[2]);
   4040         rc = 1;
   4041       }
   4042     }else if( strcmp(azArg[1],"add")==0 ){
   4043       if( nArg!=5 ){
   4044         fprintf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
   4045         rc = 1;
   4046         goto meta_command_exit;
   4047       }
   4048       rc = sqlite3_user_add(p->db, azArg[2],
   4049                             azArg[3], (int)strlen(azArg[3]),
   4050                             booleanValue(azArg[4]));
   4051       if( rc ){
   4052         fprintf(stderr, "User-Add failed: %d\n", rc);
   4053         rc = 1;
   4054       }
   4055     }else if( strcmp(azArg[1],"edit")==0 ){
   4056       if( nArg!=5 ){
   4057         fprintf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
   4058         rc = 1;
   4059         goto meta_command_exit;
   4060       }
   4061       rc = sqlite3_user_change(p->db, azArg[2],
   4062                               azArg[3], (int)strlen(azArg[3]),
   4063                               booleanValue(azArg[4]));
   4064       if( rc ){
   4065         fprintf(stderr, "User-Edit failed: %d\n", rc);
   4066         rc = 1;
   4067       }
   4068     }else if( strcmp(azArg[1],"delete")==0 ){
   4069       if( nArg!=3 ){
   4070         fprintf(stderr, "Usage: .user delete USER\n");
   4071         rc = 1;
   4072         goto meta_command_exit;
   4073       }
   4074       rc = sqlite3_user_delete(p->db, azArg[2]);
   4075       if( rc ){
   4076         fprintf(stderr, "User-Delete failed: %d\n", rc);
   4077         rc = 1;
   4078       }
   4079     }else{
   4080       fprintf(stderr, "Usage: .user login|add|edit|delete ...\n");
   4081       rc = 1;
   4082       goto meta_command_exit;
   4083     }
   4084   }else
   4085 #endif /* SQLITE_USER_AUTHENTICATION */
   4086 
   4087   if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
   4088     fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
   4089         sqlite3_libversion(), sqlite3_sourceid());
   4090   }else
   4091 
   4092   if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
   4093     const char *zDbName = nArg==2 ? azArg[1] : "main";
   4094     char *zVfsName = 0;
   4095     if( p->db ){
   4096       sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
   4097       if( zVfsName ){
   4098         fprintf(p->out, "%s\n", zVfsName);
   4099         sqlite3_free(zVfsName);
   4100       }
   4101     }
   4102   }else
   4103 
   4104 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
   4105   if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
   4106     extern int sqlite3WhereTrace;
   4107     sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
   4108   }else
   4109 #endif
   4110 
   4111   if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
   4112     int j;
   4113     assert( nArg<=ArraySize(azArg) );
   4114     for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
   4115       p->colWidth[j-1] = (int)integerValue(azArg[j]);
   4116     }
   4117   }else
   4118 
   4119   {
   4120     fprintf(stderr, "Error: unknown command or invalid arguments: "
   4121       " \"%s\". Enter \".help\" for help\n", azArg[0]);
   4122     rc = 1;
   4123   }
   4124 
   4125 meta_command_exit:
   4126   if( p->outCount ){
   4127     p->outCount--;
   4128     if( p->outCount==0 ) output_reset(p);
   4129   }
   4130   return rc;
   4131 }
   4132 
   4133 /*
   4134 ** Return TRUE if a semicolon occurs anywhere in the first N characters
   4135 ** of string z[].
   4136 */
   4137 static int line_contains_semicolon(const char *z, int N){
   4138   int i;
   4139   for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
   4140   return 0;
   4141 }
   4142 
   4143 /*
   4144 ** Test to see if a line consists entirely of whitespace.
   4145 */
   4146 static int _all_whitespace(const char *z){
   4147   for(; *z; z++){
   4148     if( IsSpace(z[0]) ) continue;
   4149     if( *z=='/' && z[1]=='*' ){
   4150       z += 2;
   4151       while( *z && (*z!='*' || z[1]!='/') ){ z++; }
   4152       if( *z==0 ) return 0;
   4153       z++;
   4154       continue;
   4155     }
   4156     if( *z=='-' && z[1]=='-' ){
   4157       z += 2;
   4158       while( *z && *z!='\n' ){ z++; }
   4159       if( *z==0 ) return 1;
   4160       continue;
   4161     }
   4162     return 0;
   4163   }
   4164   return 1;
   4165 }
   4166 
   4167 /*
   4168 ** Return TRUE if the line typed in is an SQL command terminator other
   4169 ** than a semi-colon.  The SQL Server style "go" command is understood
   4170 ** as is the Oracle "/".
   4171 */
   4172 static int line_is_command_terminator(const char *zLine){
   4173   while( IsSpace(zLine[0]) ){ zLine++; };
   4174   if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
   4175     return 1;  /* Oracle */
   4176   }
   4177   if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
   4178          && _all_whitespace(&zLine[2]) ){
   4179     return 1;  /* SQL Server */
   4180   }
   4181   return 0;
   4182 }
   4183 
   4184 /*
   4185 ** Return true if zSql is a complete SQL statement.  Return false if it
   4186 ** ends in the middle of a string literal or C-style comment.
   4187 */
   4188 static int line_is_complete(char *zSql, int nSql){
   4189   int rc;
   4190   if( zSql==0 ) return 1;
   4191   zSql[nSql] = ';';
   4192   zSql[nSql+1] = 0;
   4193   rc = sqlite3_complete(zSql);
   4194   zSql[nSql] = 0;
   4195   return rc;
   4196 }
   4197 
   4198 /*
   4199 ** Read input from *in and process it.  If *in==0 then input
   4200 ** is interactive - the user is typing it it.  Otherwise, input
   4201 ** is coming from a file or device.  A prompt is issued and history
   4202 ** is saved only if input is interactive.  An interrupt signal will
   4203 ** cause this routine to exit immediately, unless input is interactive.
   4204 **
   4205 ** Return the number of errors.
   4206 */
   4207 static int process_input(ShellState *p, FILE *in){
   4208   char *zLine = 0;          /* A single input line */
   4209   char *zSql = 0;           /* Accumulated SQL text */
   4210   int nLine;                /* Length of current line */
   4211   int nSql = 0;             /* Bytes of zSql[] used */
   4212   int nAlloc = 0;           /* Allocated zSql[] space */
   4213   int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
   4214   char *zErrMsg;            /* Error message returned */
   4215   int rc;                   /* Error code */
   4216   int errCnt = 0;           /* Number of errors seen */
   4217   int lineno = 0;           /* Current line number */
   4218   int startline = 0;        /* Line number for start of current input */
   4219 
   4220   while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
   4221     fflush(p->out);
   4222     zLine = one_input_line(in, zLine, nSql>0);
   4223     if( zLine==0 ){
   4224       /* End of input */
   4225       if( stdin_is_interactive ) printf("\n");
   4226       break;
   4227     }
   4228     if( seenInterrupt ){
   4229       if( in!=0 ) break;
   4230       seenInterrupt = 0;
   4231     }
   4232     lineno++;
   4233     if( nSql==0 && _all_whitespace(zLine) ){
   4234       if( p->echoOn ) printf("%s\n", zLine);
   4235       continue;
   4236     }
   4237     if( zLine && zLine[0]=='.' && nSql==0 ){
   4238       if( p->echoOn ) printf("%s\n", zLine);
   4239       rc = do_meta_command(zLine, p);
   4240       if( rc==2 ){ /* exit requested */
   4241         break;
   4242       }else if( rc ){
   4243         errCnt++;
   4244       }
   4245       continue;
   4246     }
   4247     if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
   4248       memcpy(zLine,";",2);
   4249     }
   4250     nLine = strlen30(zLine);
   4251     if( nSql+nLine+2>=nAlloc ){
   4252       nAlloc = nSql+nLine+100;
   4253       zSql = realloc(zSql, nAlloc);
   4254       if( zSql==0 ){
   4255         fprintf(stderr, "Error: out of memory\n");
   4256         exit(1);
   4257       }
   4258     }
   4259     nSqlPrior = nSql;
   4260     if( nSql==0 ){
   4261       int i;
   4262       for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
   4263       assert( nAlloc>0 && zSql!=0 );
   4264       memcpy(zSql, zLine+i, nLine+1-i);
   4265       startline = lineno;
   4266       nSql = nLine-i;
   4267     }else{
   4268       zSql[nSql++] = '\n';
   4269       memcpy(zSql+nSql, zLine, nLine+1);
   4270       nSql += nLine;
   4271     }
   4272     if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
   4273                 && sqlite3_complete(zSql) ){
   4274       p->cnt = 0;
   4275       open_db(p, 0);
   4276       if( p->backslashOn ) resolve_backslashes(zSql);
   4277       BEGIN_TIMER;
   4278       rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
   4279       END_TIMER;
   4280       if( rc || zErrMsg ){
   4281         char zPrefix[100];
   4282         if( in!=0 || !stdin_is_interactive ){
   4283           sqlite3_snprintf(sizeof(zPrefix), zPrefix,
   4284                            "Error: near line %d:", startline);
   4285         }else{
   4286           sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
   4287         }
   4288         if( zErrMsg!=0 ){
   4289           fprintf(stderr, "%s %s\n", zPrefix, zErrMsg);
   4290           sqlite3_free(zErrMsg);
   4291           zErrMsg = 0;
   4292         }else{
   4293           fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
   4294         }
   4295         errCnt++;
   4296       }
   4297       nSql = 0;
   4298       if( p->outCount ){
   4299         output_reset(p);
   4300         p->outCount = 0;
   4301       }
   4302     }else if( nSql && _all_whitespace(zSql) ){
   4303       if( p->echoOn ) printf("%s\n", zSql);
   4304       nSql = 0;
   4305     }
   4306   }
   4307   if( nSql ){
   4308     if( !_all_whitespace(zSql) ){
   4309       fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
   4310       errCnt++;
   4311     }
   4312   }
   4313   free(zSql);
   4314   free(zLine);
   4315   return errCnt>0;
   4316 }
   4317 
   4318 /*
   4319 ** Return a pathname which is the user's home directory.  A
   4320 ** 0 return indicates an error of some kind.
   4321 */
   4322 static char *find_home_dir(void){
   4323   static char *home_dir = NULL;
   4324   if( home_dir ) return home_dir;
   4325 
   4326 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
   4327      && !defined(__RTP__) && !defined(_WRS_KERNEL)
   4328   {
   4329     struct passwd *pwent;
   4330     uid_t uid = getuid();
   4331     if( (pwent=getpwuid(uid)) != NULL) {
   4332       home_dir = pwent->pw_dir;
   4333     }
   4334   }
   4335 #endif
   4336 
   4337 #if defined(_WIN32_WCE)
   4338   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
   4339    */
   4340   home_dir = "/";
   4341 #else
   4342 
   4343 #if defined(_WIN32) || defined(WIN32)
   4344   if (!home_dir) {
   4345     home_dir = getenv("USERPROFILE");
   4346   }
   4347 #endif
   4348 
   4349   if (!home_dir) {
   4350     home_dir = getenv("HOME");
   4351   }
   4352 
   4353 #if defined(_WIN32) || defined(WIN32)
   4354   if (!home_dir) {
   4355     char *zDrive, *zPath;
   4356     int n;
   4357     zDrive = getenv("HOMEDRIVE");
   4358     zPath = getenv("HOMEPATH");
   4359     if( zDrive && zPath ){
   4360       n = strlen30(zDrive) + strlen30(zPath) + 1;
   4361       home_dir = malloc( n );
   4362       if( home_dir==0 ) return 0;
   4363       sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
   4364       return home_dir;
   4365     }
   4366     home_dir = "c:\\";
   4367   }
   4368 #endif
   4369 
   4370 #endif /* !_WIN32_WCE */
   4371 
   4372   if( home_dir ){
   4373     int n = strlen30(home_dir) + 1;
   4374     char *z = malloc( n );
   4375     if( z ) memcpy(z, home_dir, n);
   4376     home_dir = z;
   4377   }
   4378 
   4379   return home_dir;
   4380 }
   4381 
   4382 /*
   4383 ** Read input from the file given by sqliterc_override.  Or if that
   4384 ** parameter is NULL, take input from ~/.sqliterc
   4385 **
   4386 ** Returns the number of errors.
   4387 */
   4388 static void process_sqliterc(
   4389   ShellState *p,                  /* Configuration data */
   4390   const char *sqliterc_override   /* Name of config file. NULL to use default */
   4391 ){
   4392   char *home_dir = NULL;
   4393   const char *sqliterc = sqliterc_override;
   4394   char *zBuf = 0;
   4395   FILE *in = NULL;
   4396 
   4397   if (sqliterc == NULL) {
   4398     home_dir = find_home_dir();
   4399     if( home_dir==0 ){
   4400       fprintf(stderr, "-- warning: cannot find home directory;"
   4401                       " cannot read ~/.sqliterc\n");
   4402       return;
   4403     }
   4404     sqlite3_initialize();
   4405     zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
   4406     sqliterc = zBuf;
   4407   }
   4408   in = fopen(sqliterc,"rb");
   4409   if( in ){
   4410     if( stdin_is_interactive ){
   4411       fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
   4412     }
   4413     process_input(p,in);
   4414     fclose(in);
   4415   }
   4416   sqlite3_free(zBuf);
   4417 }
   4418 
   4419 /*
   4420 ** Show available command line options
   4421 */
   4422 static const char zOptions[] =
   4423   "   -ascii               set output mode to 'ascii'\n"
   4424   "   -bail                stop after hitting an error\n"
   4425   "   -batch               force batch I/O\n"
   4426   "   -column              set output mode to 'column'\n"
   4427   "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
   4428   "   -csv                 set output mode to 'csv'\n"
   4429   "   -echo                print commands before execution\n"
   4430   "   -init FILENAME       read/process named file\n"
   4431   "   -[no]header          turn headers on or off\n"
   4432 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
   4433   "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
   4434 #endif
   4435   "   -help                show this message\n"
   4436   "   -html                set output mode to HTML\n"
   4437   "   -interactive         force interactive I/O\n"
   4438   "   -line                set output mode to 'line'\n"
   4439   "   -list                set output mode to 'list'\n"
   4440   "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
   4441   "   -mmap N              default mmap size set to N\n"
   4442 #ifdef SQLITE_ENABLE_MULTIPLEX
   4443   "   -multiplex           enable the multiplexor VFS\n"
   4444 #endif
   4445   "   -newline SEP         set output row separator. Default: '\\n'\n"
   4446   "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
   4447   "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
   4448   "   -scratch SIZE N      use N slots of SZ bytes each for scratch memory\n"
   4449   "   -separator SEP       set output column separator. Default: '|'\n"
   4450   "   -stats               print memory stats before each finalize\n"
   4451   "   -version             show SQLite version\n"
   4452   "   -vfs NAME            use NAME as the default VFS\n"
   4453 #ifdef SQLITE_ENABLE_VFSTRACE
   4454   "   -vfstrace            enable tracing of all VFS calls\n"
   4455 #endif
   4456 ;
   4457 static void usage(int showDetail){
   4458   fprintf(stderr,
   4459       "Usage: %s [OPTIONS] FILENAME [SQL]\n"
   4460       "FILENAME is the name of an SQLite database. A new database is created\n"
   4461       "if the file does not previously exist.\n", Argv0);
   4462   if( showDetail ){
   4463     fprintf(stderr, "OPTIONS include:\n%s", zOptions);
   4464   }else{
   4465     fprintf(stderr, "Use the -help option for additional information\n");
   4466   }
   4467   exit(1);
   4468 }
   4469 
   4470 /*
   4471 ** Initialize the state information in data
   4472 */
   4473 static void main_init(ShellState *data) {
   4474   memset(data, 0, sizeof(*data));
   4475   data->mode = MODE_List;
   4476   memcpy(data->colSeparator,SEP_Column, 2);
   4477   memcpy(data->rowSeparator,SEP_Row, 2);
   4478   data->showHeader = 0;
   4479   data->shellFlgs = SHFLG_Lookaside;
   4480   sqlite3_config(SQLITE_CONFIG_URI, 1);
   4481   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
   4482   sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
   4483   sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
   4484   sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
   4485 }
   4486 
   4487 /*
   4488 ** Output text to the console in a font that attracts extra attention.
   4489 */
   4490 #ifdef _WIN32
   4491 static void printBold(const char *zText){
   4492   HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
   4493   CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
   4494   GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
   4495   SetConsoleTextAttribute(out,
   4496          FOREGROUND_RED|FOREGROUND_INTENSITY
   4497   );
   4498   printf("%s", zText);
   4499   SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
   4500 }
   4501 #else
   4502 static void printBold(const char *zText){
   4503   printf("\033[1m%s\033[0m", zText);
   4504 }
   4505 #endif
   4506 
   4507 /*
   4508 ** Get the argument to an --option.  Throw an error and die if no argument
   4509 ** is available.
   4510 */
   4511 static char *cmdline_option_value(int argc, char **argv, int i){
   4512   if( i==argc ){
   4513     fprintf(stderr, "%s: Error: missing argument to %s\n",
   4514             argv[0], argv[argc-1]);
   4515     exit(1);
   4516   }
   4517   return argv[i];
   4518 }
   4519 
   4520 int SQLITE_CDECL main(int argc, char **argv){
   4521   char *zErrMsg = 0;
   4522   ShellState data;
   4523   const char *zInitFile = 0;
   4524   int i;
   4525   int rc = 0;
   4526   int warnInmemoryDb = 0;
   4527   int readStdin = 1;
   4528   int nCmd = 0;
   4529   char **azCmd = 0;
   4530 
   4531 #if USE_SYSTEM_SQLITE+0!=1
   4532   if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){
   4533     fprintf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
   4534             sqlite3_sourceid(), SQLITE_SOURCE_ID);
   4535     exit(1);
   4536   }
   4537 #endif
   4538   setBinaryMode(stdin);
   4539   setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
   4540   Argv0 = argv[0];
   4541   main_init(&data);
   4542   stdin_is_interactive = isatty(0);
   4543 
   4544   /* Make sure we have a valid signal handler early, before anything
   4545   ** else is done.
   4546   */
   4547 #ifdef SIGINT
   4548   signal(SIGINT, interrupt_handler);
   4549 #endif
   4550 
   4551 #ifdef SQLITE_SHELL_DBNAME_PROC
   4552   {
   4553     /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
   4554     ** of a C-function that will provide the name of the database file.  Use
   4555     ** this compile-time option to embed this shell program in larger
   4556     ** applications. */
   4557     extern void SQLITE_SHELL_DBNAME_PROC(const char**);
   4558     SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
   4559     warnInmemoryDb = 0;
   4560   }
   4561 #endif
   4562 
   4563   /* Do an initial pass through the command-line argument to locate
   4564   ** the name of the database file, the name of the initialization file,
   4565   ** the size of the alternative malloc heap,
   4566   ** and the first command to execute.
   4567   */
   4568   for(i=1; i<argc; i++){
   4569     char *z;
   4570     z = argv[i];
   4571     if( z[0]!='-' ){
   4572       if( data.zDbFilename==0 ){
   4573         data.zDbFilename = z;
   4574       }else{
   4575         /* Excesss arguments are interpreted as SQL (or dot-commands) and
   4576         ** mean that nothing is read from stdin */
   4577         readStdin = 0;
   4578         nCmd++;
   4579         azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
   4580         if( azCmd==0 ){
   4581           fprintf(stderr, "out of memory\n");
   4582           exit(1);
   4583         }
   4584         azCmd[nCmd-1] = z;
   4585       }
   4586     }
   4587     if( z[1]=='-' ) z++;
   4588     if( strcmp(z,"-separator")==0
   4589      || strcmp(z,"-nullvalue")==0
   4590      || strcmp(z,"-newline")==0
   4591      || strcmp(z,"-cmd")==0
   4592     ){
   4593       (void)cmdline_option_value(argc, argv, ++i);
   4594     }else if( strcmp(z,"-init")==0 ){
   4595       zInitFile = cmdline_option_value(argc, argv, ++i);
   4596     }else if( strcmp(z,"-batch")==0 ){
   4597       /* Need to check for batch mode here to so we can avoid printing
   4598       ** informational messages (like from process_sqliterc) before
   4599       ** we do the actual processing of arguments later in a second pass.
   4600       */
   4601       stdin_is_interactive = 0;
   4602     }else if( strcmp(z,"-heap")==0 ){
   4603 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
   4604       const char *zSize;
   4605       sqlite3_int64 szHeap;
   4606 
   4607       zSize = cmdline_option_value(argc, argv, ++i);
   4608       szHeap = integerValue(zSize);
   4609       if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
   4610       sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
   4611 #endif
   4612     }else if( strcmp(z,"-scratch")==0 ){
   4613       int n, sz;
   4614       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
   4615       if( sz>400000 ) sz = 400000;
   4616       if( sz<2500 ) sz = 2500;
   4617       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
   4618       if( n>10 ) n = 10;
   4619       if( n<1 ) n = 1;
   4620       sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n);
   4621       data.shellFlgs |= SHFLG_Scratch;
   4622     }else if( strcmp(z,"-pagecache")==0 ){
   4623       int n, sz;
   4624       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
   4625       if( sz>70000 ) sz = 70000;
   4626       if( sz<800 ) sz = 800;
   4627       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
   4628       if( n<10 ) n = 10;
   4629       sqlite3_config(SQLITE_CONFIG_PAGECACHE, malloc(n*sz+1), sz, n);
   4630       data.shellFlgs |= SHFLG_Pagecache;
   4631     }else if( strcmp(z,"-lookaside")==0 ){
   4632       int n, sz;
   4633       sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
   4634       if( sz<0 ) sz = 0;
   4635       n = (int)integerValue(cmdline_option_value(argc,argv,++i));
   4636       if( n<0 ) n = 0;
   4637       sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
   4638       if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
   4639 #ifdef SQLITE_ENABLE_VFSTRACE
   4640     }else if( strcmp(z,"-vfstrace")==0 ){
   4641       extern int vfstrace_register(
   4642          const char *zTraceName,
   4643          const char *zOldVfsName,
   4644          int (*xOut)(const char*,void*),
   4645          void *pOutArg,
   4646          int makeDefault
   4647       );
   4648       vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
   4649 #endif
   4650 #ifdef SQLITE_ENABLE_MULTIPLEX
   4651     }else if( strcmp(z,"-multiplex")==0 ){
   4652       extern int sqlite3_multiple_initialize(const char*,int);
   4653       sqlite3_multiplex_initialize(0, 1);
   4654 #endif
   4655     }else if( strcmp(z,"-mmap")==0 ){
   4656       sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
   4657       sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
   4658     }else if( strcmp(z,"-vfs")==0 ){
   4659       sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
   4660       if( pVfs ){
   4661         sqlite3_vfs_register(pVfs, 1);
   4662       }else{
   4663         fprintf(stderr, "no such VFS: \"%s\"\n", argv[i]);
   4664         exit(1);
   4665       }
   4666     }
   4667   }
   4668   if( data.zDbFilename==0 ){
   4669 #ifndef SQLITE_OMIT_MEMORYDB
   4670     data.zDbFilename = ":memory:";
   4671     warnInmemoryDb = argc==1;
   4672 #else
   4673     fprintf(stderr,"%s: Error: no database filename specified\n", Argv0);
   4674     return 1;
   4675 #endif
   4676   }
   4677   data.out = stdout;
   4678 
   4679   /* Go ahead and open the database file if it already exists.  If the
   4680   ** file does not exist, delay opening it.  This prevents empty database
   4681   ** files from being created if a user mistypes the database name argument
   4682   ** to the sqlite command-line tool.
   4683   */
   4684   if( access(data.zDbFilename, 0)==0 ){
   4685     open_db(&data, 0);
   4686   }
   4687 
   4688   /* Process the initialization file if there is one.  If no -init option
   4689   ** is given on the command line, look for a file named ~/.sqliterc and
   4690   ** try to process it.
   4691   */
   4692   process_sqliterc(&data,zInitFile);
   4693 
   4694   /* Make a second pass through the command-line argument and set
   4695   ** options.  This second pass is delayed until after the initialization
   4696   ** file is processed so that the command-line arguments will override
   4697   ** settings in the initialization file.
   4698   */
   4699   for(i=1; i<argc; i++){
   4700     char *z = argv[i];
   4701     if( z[0]!='-' ) continue;
   4702     if( z[1]=='-' ){ z++; }
   4703     if( strcmp(z,"-init")==0 ){
   4704       i++;
   4705     }else if( strcmp(z,"-html")==0 ){
   4706       data.mode = MODE_Html;
   4707     }else if( strcmp(z,"-list")==0 ){
   4708       data.mode = MODE_List;
   4709     }else if( strcmp(z,"-line")==0 ){
   4710       data.mode = MODE_Line;
   4711     }else if( strcmp(z,"-column")==0 ){
   4712       data.mode = MODE_Column;
   4713     }else if( strcmp(z,"-csv")==0 ){
   4714       data.mode = MODE_Csv;
   4715       memcpy(data.colSeparator,",",2);
   4716     }else if( strcmp(z,"-ascii")==0 ){
   4717       data.mode = MODE_Ascii;
   4718       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
   4719                        SEP_Unit);
   4720       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
   4721                        SEP_Record);
   4722     }else if( strcmp(z,"-separator")==0 ){
   4723       sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
   4724                        "%s",cmdline_option_value(argc,argv,++i));
   4725     }else if( strcmp(z,"-newline")==0 ){
   4726       sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
   4727                        "%s",cmdline_option_value(argc,argv,++i));
   4728     }else if( strcmp(z,"-nullvalue")==0 ){
   4729       sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
   4730                        "%s",cmdline_option_value(argc,argv,++i));
   4731     }else if( strcmp(z,"-header")==0 ){
   4732       data.showHeader = 1;
   4733     }else if( strcmp(z,"-noheader")==0 ){
   4734       data.showHeader = 0;
   4735     }else if( strcmp(z,"-echo")==0 ){
   4736       data.echoOn = 1;
   4737     }else if( strcmp(z,"-eqp")==0 ){
   4738       data.autoEQP = 1;
   4739     }else if( strcmp(z,"-stats")==0 ){
   4740       data.statsOn = 1;
   4741     }else if( strcmp(z,"-scanstats")==0 ){
   4742       data.scanstatsOn = 1;
   4743     }else if( strcmp(z,"-backslash")==0 ){
   4744       /* Undocumented command-line option: -backslash
   4745       ** Causes C-style backslash escapes to be evaluated in SQL statements
   4746       ** prior to sending the SQL into SQLite.  Useful for injecting
   4747       ** crazy bytes in the middle of SQL statements for testing and debugging.
   4748       */
   4749       data.backslashOn = 1;
   4750     }else if( strcmp(z,"-bail")==0 ){
   4751       bail_on_error = 1;
   4752     }else if( strcmp(z,"-version")==0 ){
   4753       printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
   4754       return 0;
   4755     }else if( strcmp(z,"-interactive")==0 ){
   4756       stdin_is_interactive = 1;
   4757     }else if( strcmp(z,"-batch")==0 ){
   4758       stdin_is_interactive = 0;
   4759     }else if( strcmp(z,"-heap")==0 ){
   4760       i++;
   4761     }else if( strcmp(z,"-scratch")==0 ){
   4762       i+=2;
   4763     }else if( strcmp(z,"-pagecache")==0 ){
   4764       i+=2;
   4765     }else if( strcmp(z,"-lookaside")==0 ){
   4766       i+=2;
   4767     }else if( strcmp(z,"-mmap")==0 ){
   4768       i++;
   4769     }else if( strcmp(z,"-vfs")==0 ){
   4770       i++;
   4771 #ifdef SQLITE_ENABLE_VFSTRACE
   4772     }else if( strcmp(z,"-vfstrace")==0 ){
   4773       i++;
   4774 #endif
   4775 #ifdef SQLITE_ENABLE_MULTIPLEX
   4776     }else if( strcmp(z,"-multiplex")==0 ){
   4777       i++;
   4778 #endif
   4779     }else if( strcmp(z,"-help")==0 ){
   4780       usage(1);
   4781     }else if( strcmp(z,"-cmd")==0 ){
   4782       /* Run commands that follow -cmd first and separately from commands
   4783       ** that simply appear on the command-line.  This seems goofy.  It would
   4784       ** be better if all commands ran in the order that they appear.  But
   4785       ** we retain the goofy behavior for historical compatibility. */
   4786       if( i==argc-1 ) break;
   4787       z = cmdline_option_value(argc,argv,++i);
   4788       if( z[0]=='.' ){
   4789         rc = do_meta_command(z, &data);
   4790         if( rc && bail_on_error ) return rc==2 ? 0 : rc;
   4791       }else{
   4792         open_db(&data, 0);
   4793         rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
   4794         if( zErrMsg!=0 ){
   4795           fprintf(stderr,"Error: %s\n", zErrMsg);
   4796           if( bail_on_error ) return rc!=0 ? rc : 1;
   4797         }else if( rc!=0 ){
   4798           fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z);
   4799           if( bail_on_error ) return rc;
   4800         }
   4801       }
   4802     }else{
   4803       fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
   4804       fprintf(stderr,"Use -help for a list of options.\n");
   4805       return 1;
   4806     }
   4807   }
   4808 
   4809   if( !readStdin ){
   4810     /* Run all arguments that do not begin with '-' as if they were separate
   4811     ** command-line inputs, except for the argToSkip argument which contains
   4812     ** the database filename.
   4813     */
   4814     for(i=0; i<nCmd; i++){
   4815       if( azCmd[i][0]=='.' ){
   4816         rc = do_meta_command(azCmd[i], &data);
   4817         if( rc ) return rc==2 ? 0 : rc;
   4818       }else{
   4819         open_db(&data, 0);
   4820         rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
   4821         if( zErrMsg!=0 ){
   4822           fprintf(stderr,"Error: %s\n", zErrMsg);
   4823           return rc!=0 ? rc : 1;
   4824         }else if( rc!=0 ){
   4825           fprintf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
   4826           return rc;
   4827         }
   4828       }
   4829     }
   4830     free(azCmd);
   4831   }else{
   4832     /* Run commands received from standard input
   4833     */
   4834     if( stdin_is_interactive ){
   4835       char *zHome;
   4836       char *zHistory = 0;
   4837       int nHistory;
   4838       printf(
   4839         "SQLite version %s %.19s\n" /*extra-version-info*/
   4840         "Enter \".help\" for usage hints.\n",
   4841         sqlite3_libversion(), sqlite3_sourceid()
   4842       );
   4843       if( warnInmemoryDb ){
   4844         printf("Connected to a ");
   4845         printBold("transient in-memory database");
   4846         printf(".\nUse \".open FILENAME\" to reopen on a "
   4847                "persistent database.\n");
   4848       }
   4849       zHome = find_home_dir();
   4850       if( zHome ){
   4851         nHistory = strlen30(zHome) + 20;
   4852         if( (zHistory = malloc(nHistory))!=0 ){
   4853           sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
   4854         }
   4855       }
   4856       if( zHistory ){ shell_read_history(zHistory); }
   4857       rc = process_input(&data, 0);
   4858       if( zHistory ){
   4859         shell_stifle_history(100);
   4860         shell_write_history(zHistory);
   4861         free(zHistory);
   4862       }
   4863     }else{
   4864       rc = process_input(&data, stdin);
   4865     }
   4866   }
   4867   set_table_name(&data, 0);
   4868   if( data.db ){
   4869     sqlite3_close(data.db);
   4870   }
   4871   sqlite3_free(data.zFreeOnClose);
   4872   return rc;
   4873 }
   4874