xref: /sqlite-3.40.0/src/shell.c.in (revision cbf1c8c2)
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** Warning pragmas copied from msvc.h in the core.
22*/
23#if defined(_MSC_VER)
24#pragma warning(disable : 4054)
25#pragma warning(disable : 4055)
26#pragma warning(disable : 4100)
27#pragma warning(disable : 4127)
28#pragma warning(disable : 4130)
29#pragma warning(disable : 4152)
30#pragma warning(disable : 4189)
31#pragma warning(disable : 4206)
32#pragma warning(disable : 4210)
33#pragma warning(disable : 4232)
34#pragma warning(disable : 4244)
35#pragma warning(disable : 4305)
36#pragma warning(disable : 4306)
37#pragma warning(disable : 4702)
38#pragma warning(disable : 4706)
39#endif /* defined(_MSC_VER) */
40
41/*
42** No support for loadable extensions in VxWorks.
43*/
44#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
45# define SQLITE_OMIT_LOAD_EXTENSION 1
46#endif
47
48/*
49** Enable large-file support for fopen() and friends on unix.
50*/
51#ifndef SQLITE_DISABLE_LFS
52# define _LARGE_FILE       1
53# ifndef _FILE_OFFSET_BITS
54#   define _FILE_OFFSET_BITS 64
55# endif
56# define _LARGEFILE_SOURCE 1
57#endif
58
59#include <stdlib.h>
60#include <string.h>
61#include <stdio.h>
62#include <assert.h>
63#include "sqlite3.h"
64typedef sqlite3_int64 i64;
65typedef sqlite3_uint64 u64;
66typedef unsigned char u8;
67#if SQLITE_USER_AUTHENTICATION
68# include "sqlite3userauth.h"
69#endif
70#include <ctype.h>
71#include <stdarg.h>
72
73#if !defined(_WIN32) && !defined(WIN32)
74# include <signal.h>
75# if !defined(__RTP__) && !defined(_WRS_KERNEL)
76#  include <pwd.h>
77# endif
78#endif
79#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
80# include <unistd.h>
81# include <dirent.h>
82# define GETPID getpid
83# if defined(__MINGW32__)
84#  define DIRENT dirent
85#  ifndef S_ISLNK
86#   define S_ISLNK(mode) (0)
87#  endif
88# endif
89#else
90# define GETPID (int)GetCurrentProcessId
91#endif
92#include <sys/types.h>
93#include <sys/stat.h>
94
95#if HAVE_READLINE
96# include <readline/readline.h>
97# include <readline/history.h>
98#endif
99
100#if HAVE_EDITLINE
101# include <editline/readline.h>
102#endif
103
104#if HAVE_EDITLINE || HAVE_READLINE
105
106# define shell_add_history(X) add_history(X)
107# define shell_read_history(X) read_history(X)
108# define shell_write_history(X) write_history(X)
109# define shell_stifle_history(X) stifle_history(X)
110# define shell_readline(X) readline(X)
111
112#elif HAVE_LINENOISE
113
114# include "linenoise.h"
115# define shell_add_history(X) linenoiseHistoryAdd(X)
116# define shell_read_history(X) linenoiseHistoryLoad(X)
117# define shell_write_history(X) linenoiseHistorySave(X)
118# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
119# define shell_readline(X) linenoise(X)
120
121#else
122
123# define shell_read_history(X)
124# define shell_write_history(X)
125# define shell_stifle_history(X)
126
127# define SHELL_USE_LOCAL_GETLINE 1
128#endif
129
130
131#if defined(_WIN32) || defined(WIN32)
132# include <io.h>
133# include <fcntl.h>
134# define isatty(h) _isatty(h)
135# ifndef access
136#  define access(f,m) _access((f),(m))
137# endif
138# ifndef unlink
139#  define unlink _unlink
140# endif
141# ifndef strdup
142#  define strdup _strdup
143# endif
144# undef popen
145# define popen _popen
146# undef pclose
147# define pclose _pclose
148#else
149 /* Make sure isatty() has a prototype. */
150 extern int isatty(int);
151
152# if !defined(__RTP__) && !defined(_WRS_KERNEL)
153  /* popen and pclose are not C89 functions and so are
154  ** sometimes omitted from the <stdio.h> header */
155   extern FILE *popen(const char*,const char*);
156   extern int pclose(FILE*);
157# else
158#  define SQLITE_OMIT_POPEN 1
159# endif
160#endif
161
162#if defined(_WIN32_WCE)
163/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
164 * thus we always assume that we have a console. That can be
165 * overridden with the -batch command line option.
166 */
167#define isatty(x) 1
168#endif
169
170/* ctype macros that work with signed characters */
171#define IsSpace(X)  isspace((unsigned char)X)
172#define IsDigit(X)  isdigit((unsigned char)X)
173#define ToLower(X)  (char)tolower((unsigned char)X)
174
175#if defined(_WIN32) || defined(WIN32)
176#include <windows.h>
177
178/* string conversion routines only needed on Win32 */
179extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
180extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
181extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
182extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
183#endif
184
185/* On Windows, we normally run with output mode of TEXT so that \n characters
186** are automatically translated into \r\n.  However, this behavior needs
187** to be disabled in some cases (ex: when generating CSV output and when
188** rendering quoted strings that contain \n characters).  The following
189** routines take care of that.
190*/
191#if defined(_WIN32) || defined(WIN32)
192static void setBinaryMode(FILE *file, int isOutput){
193  if( isOutput ) fflush(file);
194  _setmode(_fileno(file), _O_BINARY);
195}
196static void setTextMode(FILE *file, int isOutput){
197  if( isOutput ) fflush(file);
198  _setmode(_fileno(file), _O_TEXT);
199}
200#else
201# define setBinaryMode(X,Y)
202# define setTextMode(X,Y)
203#endif
204
205
206/* True if the timer is enabled */
207static int enableTimer = 0;
208
209/* Return the current wall-clock time */
210static sqlite3_int64 timeOfDay(void){
211  static sqlite3_vfs *clockVfs = 0;
212  sqlite3_int64 t;
213  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
214  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
215    clockVfs->xCurrentTimeInt64(clockVfs, &t);
216  }else{
217    double r;
218    clockVfs->xCurrentTime(clockVfs, &r);
219    t = (sqlite3_int64)(r*86400000.0);
220  }
221  return t;
222}
223
224#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
225#include <sys/time.h>
226#include <sys/resource.h>
227
228/* VxWorks does not support getrusage() as far as we can determine */
229#if defined(_WRS_KERNEL) || defined(__RTP__)
230struct rusage {
231  struct timeval ru_utime; /* user CPU time used */
232  struct timeval ru_stime; /* system CPU time used */
233};
234#define getrusage(A,B) memset(B,0,sizeof(*B))
235#endif
236
237/* Saved resource information for the beginning of an operation */
238static struct rusage sBegin;  /* CPU time at start */
239static sqlite3_int64 iBegin;  /* Wall-clock time at start */
240
241/*
242** Begin timing an operation
243*/
244static void beginTimer(void){
245  if( enableTimer ){
246    getrusage(RUSAGE_SELF, &sBegin);
247    iBegin = timeOfDay();
248  }
249}
250
251/* Return the difference of two time_structs in seconds */
252static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
253  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
254         (double)(pEnd->tv_sec - pStart->tv_sec);
255}
256
257/*
258** Print the timing results.
259*/
260static void endTimer(void){
261  if( enableTimer ){
262    sqlite3_int64 iEnd = timeOfDay();
263    struct rusage sEnd;
264    getrusage(RUSAGE_SELF, &sEnd);
265    printf("Run Time: real %.3f user %f sys %f\n",
266       (iEnd - iBegin)*0.001,
267       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
268       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
269  }
270}
271
272#define BEGIN_TIMER beginTimer()
273#define END_TIMER endTimer()
274#define HAS_TIMER 1
275
276#elif (defined(_WIN32) || defined(WIN32))
277
278/* Saved resource information for the beginning of an operation */
279static HANDLE hProcess;
280static FILETIME ftKernelBegin;
281static FILETIME ftUserBegin;
282static sqlite3_int64 ftWallBegin;
283typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
284                                    LPFILETIME, LPFILETIME);
285static GETPROCTIMES getProcessTimesAddr = NULL;
286
287/*
288** Check to see if we have timer support.  Return 1 if necessary
289** support found (or found previously).
290*/
291static int hasTimer(void){
292  if( getProcessTimesAddr ){
293    return 1;
294  } else {
295    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
296    ** versions. See if the version we are running on has it, and if it
297    ** does, save off a pointer to it and the current process handle.
298    */
299    hProcess = GetCurrentProcess();
300    if( hProcess ){
301      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
302      if( NULL != hinstLib ){
303        getProcessTimesAddr =
304            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
305        if( NULL != getProcessTimesAddr ){
306          return 1;
307        }
308        FreeLibrary(hinstLib);
309      }
310    }
311  }
312  return 0;
313}
314
315/*
316** Begin timing an operation
317*/
318static void beginTimer(void){
319  if( enableTimer && getProcessTimesAddr ){
320    FILETIME ftCreation, ftExit;
321    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
322                        &ftKernelBegin,&ftUserBegin);
323    ftWallBegin = timeOfDay();
324  }
325}
326
327/* Return the difference of two FILETIME structs in seconds */
328static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
329  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
330  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
331  return (double) ((i64End - i64Start) / 10000000.0);
332}
333
334/*
335** Print the timing results.
336*/
337static void endTimer(void){
338  if( enableTimer && getProcessTimesAddr){
339    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
340    sqlite3_int64 ftWallEnd = timeOfDay();
341    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
342    printf("Run Time: real %.3f user %f sys %f\n",
343       (ftWallEnd - ftWallBegin)*0.001,
344       timeDiff(&ftUserBegin, &ftUserEnd),
345       timeDiff(&ftKernelBegin, &ftKernelEnd));
346  }
347}
348
349#define BEGIN_TIMER beginTimer()
350#define END_TIMER endTimer()
351#define HAS_TIMER hasTimer()
352
353#else
354#define BEGIN_TIMER
355#define END_TIMER
356#define HAS_TIMER 0
357#endif
358
359/*
360** Used to prevent warnings about unused parameters
361*/
362#define UNUSED_PARAMETER(x) (void)(x)
363
364/*
365** Number of elements in an array
366*/
367#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
368
369/*
370** If the following flag is set, then command execution stops
371** at an error if we are not interactive.
372*/
373static int bail_on_error = 0;
374
375/*
376** Threat stdin as an interactive input if the following variable
377** is true.  Otherwise, assume stdin is connected to a file or pipe.
378*/
379static int stdin_is_interactive = 1;
380
381/*
382** On Windows systems we have to know if standard output is a console
383** in order to translate UTF-8 into MBCS.  The following variable is
384** true if translation is required.
385*/
386static int stdout_is_console = 1;
387
388/*
389** The following is the open SQLite database.  We make a pointer
390** to this database a static variable so that it can be accessed
391** by the SIGINT handler to interrupt database processing.
392*/
393static sqlite3 *globalDb = 0;
394
395/*
396** True if an interrupt (Control-C) has been received.
397*/
398static volatile int seenInterrupt = 0;
399
400/*
401** This is the name of our program. It is set in main(), used
402** in a number of other places, mostly for error messages.
403*/
404static char *Argv0;
405
406/*
407** Prompt strings. Initialized in main. Settable with
408**   .prompt main continue
409*/
410static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
411static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
412
413/*
414** Render output like fprintf().  Except, if the output is going to the
415** console and if this is running on a Windows machine, translate the
416** output from UTF-8 into MBCS.
417*/
418#if defined(_WIN32) || defined(WIN32)
419void utf8_printf(FILE *out, const char *zFormat, ...){
420  va_list ap;
421  va_start(ap, zFormat);
422  if( stdout_is_console && (out==stdout || out==stderr) ){
423    char *z1 = sqlite3_vmprintf(zFormat, ap);
424    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
425    sqlite3_free(z1);
426    fputs(z2, out);
427    sqlite3_free(z2);
428  }else{
429    vfprintf(out, zFormat, ap);
430  }
431  va_end(ap);
432}
433#elif !defined(utf8_printf)
434# define utf8_printf fprintf
435#endif
436
437/*
438** Render output like fprintf().  This should not be used on anything that
439** includes string formatting (e.g. "%s").
440*/
441#if !defined(raw_printf)
442# define raw_printf fprintf
443#endif
444
445/* Indicate out-of-memory and exit. */
446static void shell_out_of_memory(void){
447  raw_printf(stderr,"Error: out of memory\n");
448  exit(1);
449}
450
451/*
452** Write I/O traces to the following stream.
453*/
454#ifdef SQLITE_ENABLE_IOTRACE
455static FILE *iotrace = 0;
456#endif
457
458/*
459** This routine works like printf in that its first argument is a
460** format string and subsequent arguments are values to be substituted
461** in place of % fields.  The result of formatting this string
462** is written to iotrace.
463*/
464#ifdef SQLITE_ENABLE_IOTRACE
465static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
466  va_list ap;
467  char *z;
468  if( iotrace==0 ) return;
469  va_start(ap, zFormat);
470  z = sqlite3_vmprintf(zFormat, ap);
471  va_end(ap);
472  utf8_printf(iotrace, "%s", z);
473  sqlite3_free(z);
474}
475#endif
476
477/*
478** Output string zUtf to stream pOut as w characters.  If w is negative,
479** then right-justify the text.  W is the width in UTF-8 characters, not
480** in bytes.  This is different from the %*.*s specification in printf
481** since with %*.*s the width is measured in bytes, not characters.
482*/
483static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
484  int i;
485  int n;
486  int aw = w<0 ? -w : w;
487  char zBuf[1000];
488  if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
489  for(i=n=0; zUtf[i]; i++){
490    if( (zUtf[i]&0xc0)!=0x80 ){
491      n++;
492      if( n==aw ){
493        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
494        break;
495      }
496    }
497  }
498  if( n>=aw ){
499    utf8_printf(pOut, "%.*s", i, zUtf);
500  }else if( w<0 ){
501    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
502  }else{
503    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
504  }
505}
506
507
508/*
509** Determines if a string is a number of not.
510*/
511static int isNumber(const char *z, int *realnum){
512  if( *z=='-' || *z=='+' ) z++;
513  if( !IsDigit(*z) ){
514    return 0;
515  }
516  z++;
517  if( realnum ) *realnum = 0;
518  while( IsDigit(*z) ){ z++; }
519  if( *z=='.' ){
520    z++;
521    if( !IsDigit(*z) ) return 0;
522    while( IsDigit(*z) ){ z++; }
523    if( realnum ) *realnum = 1;
524  }
525  if( *z=='e' || *z=='E' ){
526    z++;
527    if( *z=='+' || *z=='-' ) z++;
528    if( !IsDigit(*z) ) return 0;
529    while( IsDigit(*z) ){ z++; }
530    if( realnum ) *realnum = 1;
531  }
532  return *z==0;
533}
534
535/*
536** Compute a string length that is limited to what can be stored in
537** lower 30 bits of a 32-bit signed integer.
538*/
539static int strlen30(const char *z){
540  const char *z2 = z;
541  while( *z2 ){ z2++; }
542  return 0x3fffffff & (int)(z2 - z);
543}
544
545/*
546** Return the length of a string in characters.  Multibyte UTF8 characters
547** count as a single character.
548*/
549static int strlenChar(const char *z){
550  int n = 0;
551  while( *z ){
552    if( (0xc0&*(z++))!=0x80 ) n++;
553  }
554  return n;
555}
556
557/*
558** This routine reads a line of text from FILE in, stores
559** the text in memory obtained from malloc() and returns a pointer
560** to the text.  NULL is returned at end of file, or if malloc()
561** fails.
562**
563** If zLine is not NULL then it is a malloced buffer returned from
564** a previous call to this routine that may be reused.
565*/
566static char *local_getline(char *zLine, FILE *in){
567  int nLine = zLine==0 ? 0 : 100;
568  int n = 0;
569
570  while( 1 ){
571    if( n+100>nLine ){
572      nLine = nLine*2 + 100;
573      zLine = realloc(zLine, nLine);
574      if( zLine==0 ) shell_out_of_memory();
575    }
576    if( fgets(&zLine[n], nLine - n, in)==0 ){
577      if( n==0 ){
578        free(zLine);
579        return 0;
580      }
581      zLine[n] = 0;
582      break;
583    }
584    while( zLine[n] ) n++;
585    if( n>0 && zLine[n-1]=='\n' ){
586      n--;
587      if( n>0 && zLine[n-1]=='\r' ) n--;
588      zLine[n] = 0;
589      break;
590    }
591  }
592#if defined(_WIN32) || defined(WIN32)
593  /* For interactive input on Windows systems, translate the
594  ** multi-byte characterset characters into UTF-8. */
595  if( stdin_is_interactive && in==stdin ){
596    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
597    if( zTrans ){
598      int nTrans = strlen30(zTrans)+1;
599      if( nTrans>nLine ){
600        zLine = realloc(zLine, nTrans);
601        if( zLine==0 ) shell_out_of_memory();
602      }
603      memcpy(zLine, zTrans, nTrans);
604      sqlite3_free(zTrans);
605    }
606  }
607#endif /* defined(_WIN32) || defined(WIN32) */
608  return zLine;
609}
610
611/*
612** Retrieve a single line of input text.
613**
614** If in==0 then read from standard input and prompt before each line.
615** If isContinuation is true, then a continuation prompt is appropriate.
616** If isContinuation is zero, then the main prompt should be used.
617**
618** If zPrior is not NULL then it is a buffer from a prior call to this
619** routine that can be reused.
620**
621** The result is stored in space obtained from malloc() and must either
622** be freed by the caller or else passed back into this routine via the
623** zPrior argument for reuse.
624*/
625static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
626  char *zPrompt;
627  char *zResult;
628  if( in!=0 ){
629    zResult = local_getline(zPrior, in);
630  }else{
631    zPrompt = isContinuation ? continuePrompt : mainPrompt;
632#if SHELL_USE_LOCAL_GETLINE
633    printf("%s", zPrompt);
634    fflush(stdout);
635    zResult = local_getline(zPrior, stdin);
636#else
637    free(zPrior);
638    zResult = shell_readline(zPrompt);
639    if( zResult && *zResult ) shell_add_history(zResult);
640#endif
641  }
642  return zResult;
643}
644
645
646/*
647** Return the value of a hexadecimal digit.  Return -1 if the input
648** is not a hex digit.
649*/
650static int hexDigitValue(char c){
651  if( c>='0' && c<='9' ) return c - '0';
652  if( c>='a' && c<='f' ) return c - 'a' + 10;
653  if( c>='A' && c<='F' ) return c - 'A' + 10;
654  return -1;
655}
656
657/*
658** Interpret zArg as an integer value, possibly with suffixes.
659*/
660static sqlite3_int64 integerValue(const char *zArg){
661  sqlite3_int64 v = 0;
662  static const struct { char *zSuffix; int iMult; } aMult[] = {
663    { "KiB", 1024 },
664    { "MiB", 1024*1024 },
665    { "GiB", 1024*1024*1024 },
666    { "KB",  1000 },
667    { "MB",  1000000 },
668    { "GB",  1000000000 },
669    { "K",   1000 },
670    { "M",   1000000 },
671    { "G",   1000000000 },
672  };
673  int i;
674  int isNeg = 0;
675  if( zArg[0]=='-' ){
676    isNeg = 1;
677    zArg++;
678  }else if( zArg[0]=='+' ){
679    zArg++;
680  }
681  if( zArg[0]=='0' && zArg[1]=='x' ){
682    int x;
683    zArg += 2;
684    while( (x = hexDigitValue(zArg[0]))>=0 ){
685      v = (v<<4) + x;
686      zArg++;
687    }
688  }else{
689    while( IsDigit(zArg[0]) ){
690      v = v*10 + zArg[0] - '0';
691      zArg++;
692    }
693  }
694  for(i=0; i<ArraySize(aMult); i++){
695    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
696      v *= aMult[i].iMult;
697      break;
698    }
699  }
700  return isNeg? -v : v;
701}
702
703/*
704** A variable length string to which one can append text.
705*/
706typedef struct ShellText ShellText;
707struct ShellText {
708  char *z;
709  int n;
710  int nAlloc;
711};
712
713/*
714** Initialize and destroy a ShellText object
715*/
716static void initText(ShellText *p){
717  memset(p, 0, sizeof(*p));
718}
719static void freeText(ShellText *p){
720  free(p->z);
721  initText(p);
722}
723
724/* zIn is either a pointer to a NULL-terminated string in memory obtained
725** from malloc(), or a NULL pointer. The string pointed to by zAppend is
726** added to zIn, and the result returned in memory obtained from malloc().
727** zIn, if it was not NULL, is freed.
728**
729** If the third argument, quote, is not '\0', then it is used as a
730** quote character for zAppend.
731*/
732static void appendText(ShellText *p, char const *zAppend, char quote){
733  int len;
734  int i;
735  int nAppend = strlen30(zAppend);
736
737  len = nAppend+p->n+1;
738  if( quote ){
739    len += 2;
740    for(i=0; i<nAppend; i++){
741      if( zAppend[i]==quote ) len++;
742    }
743  }
744
745  if( p->n+len>=p->nAlloc ){
746    p->nAlloc = p->nAlloc*2 + len + 20;
747    p->z = realloc(p->z, p->nAlloc);
748    if( p->z==0 ) shell_out_of_memory();
749  }
750
751  if( quote ){
752    char *zCsr = p->z+p->n;
753    *zCsr++ = quote;
754    for(i=0; i<nAppend; i++){
755      *zCsr++ = zAppend[i];
756      if( zAppend[i]==quote ) *zCsr++ = quote;
757    }
758    *zCsr++ = quote;
759    p->n = (int)(zCsr - p->z);
760    *zCsr = '\0';
761  }else{
762    memcpy(p->z+p->n, zAppend, nAppend);
763    p->n += nAppend;
764    p->z[p->n] = '\0';
765  }
766}
767
768/*
769** Attempt to determine if identifier zName needs to be quoted, either
770** because it contains non-alphanumeric characters, or because it is an
771** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
772** that quoting is required.
773**
774** Return '"' if quoting is required.  Return 0 if no quoting is required.
775*/
776static char quoteChar(const char *zName){
777  int i;
778  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
779  for(i=0; zName[i]; i++){
780    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
781  }
782  return sqlite3_keyword_check(zName, i) ? '"' : 0;
783}
784
785/*
786** Construct a fake object name and column list to describe the structure
787** of the view, virtual table, or table valued function zSchema.zName.
788*/
789static char *shellFakeSchema(
790  sqlite3 *db,            /* The database connection containing the vtab */
791  const char *zSchema,    /* Schema of the database holding the vtab */
792  const char *zName       /* The name of the virtual table */
793){
794  sqlite3_stmt *pStmt = 0;
795  char *zSql;
796  ShellText s;
797  char cQuote;
798  char *zDiv = "(";
799  int nRow = 0;
800
801  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
802                         zSchema ? zSchema : "main", zName);
803  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
804  sqlite3_free(zSql);
805  initText(&s);
806  if( zSchema ){
807    cQuote = quoteChar(zSchema);
808    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
809    appendText(&s, zSchema, cQuote);
810    appendText(&s, ".", 0);
811  }
812  cQuote = quoteChar(zName);
813  appendText(&s, zName, cQuote);
814  while( sqlite3_step(pStmt)==SQLITE_ROW ){
815    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
816    nRow++;
817    appendText(&s, zDiv, 0);
818    zDiv = ",";
819    cQuote = quoteChar(zCol);
820    appendText(&s, zCol, cQuote);
821  }
822  appendText(&s, ")", 0);
823  sqlite3_finalize(pStmt);
824  if( nRow==0 ){
825    freeText(&s);
826    s.z = 0;
827  }
828  return s.z;
829}
830
831/*
832** SQL function:  shell_module_schema(X)
833**
834** Return a fake schema for the table-valued function or eponymous virtual
835** table X.
836*/
837static void shellModuleSchema(
838  sqlite3_context *pCtx,
839  int nVal,
840  sqlite3_value **apVal
841){
842  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
843  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
844  UNUSED_PARAMETER(nVal);
845  if( zFake ){
846    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
847                        -1, sqlite3_free);
848    free(zFake);
849  }
850}
851
852/*
853** SQL function:  shell_add_schema(S,X)
854**
855** Add the schema name X to the CREATE statement in S and return the result.
856** Examples:
857**
858**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
859**
860** Also works on
861**
862**    CREATE INDEX
863**    CREATE UNIQUE INDEX
864**    CREATE VIEW
865**    CREATE TRIGGER
866**    CREATE VIRTUAL TABLE
867**
868** This UDF is used by the .schema command to insert the schema name of
869** attached databases into the middle of the sqlite_master.sql field.
870*/
871static void shellAddSchemaName(
872  sqlite3_context *pCtx,
873  int nVal,
874  sqlite3_value **apVal
875){
876  static const char *aPrefix[] = {
877     "TABLE",
878     "INDEX",
879     "UNIQUE INDEX",
880     "VIEW",
881     "TRIGGER",
882     "VIRTUAL TABLE"
883  };
884  int i = 0;
885  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
886  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
887  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
888  sqlite3 *db = sqlite3_context_db_handle(pCtx);
889  UNUSED_PARAMETER(nVal);
890  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
891    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
892      int n = strlen30(aPrefix[i]);
893      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
894        char *z = 0;
895        char *zFake = 0;
896        if( zSchema ){
897          char cQuote = quoteChar(zSchema);
898          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
899            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
900          }else{
901            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
902          }
903        }
904        if( zName
905         && aPrefix[i][0]=='V'
906         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
907        ){
908          if( z==0 ){
909            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
910          }else{
911            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
912          }
913          free(zFake);
914        }
915        if( z ){
916          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
917          return;
918        }
919      }
920    }
921  }
922  sqlite3_result_value(pCtx, apVal[0]);
923}
924
925/*
926** The source code for several run-time loadable extensions is inserted
927** below by the ../tool/mkshellc.tcl script.  Before processing that included
928** code, we need to override some macros to make the included program code
929** work here in the middle of this regular program.
930*/
931#define SQLITE_EXTENSION_INIT1
932#define SQLITE_EXTENSION_INIT2(X) (void)(X)
933
934#if defined(_WIN32) && defined(_MSC_VER)
935INCLUDE test_windirent.h
936INCLUDE test_windirent.c
937#define dirent DIRENT
938#endif
939INCLUDE ../ext/misc/shathree.c
940INCLUDE ../ext/misc/fileio.c
941INCLUDE ../ext/misc/completion.c
942INCLUDE ../ext/misc/appendvfs.c
943INCLUDE ../ext/misc/memtrace.c
944#ifdef SQLITE_HAVE_ZLIB
945INCLUDE ../ext/misc/zipfile.c
946INCLUDE ../ext/misc/sqlar.c
947#endif
948INCLUDE ../ext/expert/sqlite3expert.h
949INCLUDE ../ext/expert/sqlite3expert.c
950
951#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
952INCLUDE ../ext/misc/dbdata.c
953#endif
954
955#if defined(SQLITE_ENABLE_SESSION)
956/*
957** State information for a single open session
958*/
959typedef struct OpenSession OpenSession;
960struct OpenSession {
961  char *zName;             /* Symbolic name for this session */
962  int nFilter;             /* Number of xFilter rejection GLOB patterns */
963  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
964  sqlite3_session *p;      /* The open session */
965};
966#endif
967
968/*
969** Shell output mode information from before ".explain on",
970** saved so that it can be restored by ".explain off"
971*/
972typedef struct SavedModeInfo SavedModeInfo;
973struct SavedModeInfo {
974  int valid;          /* Is there legit data in here? */
975  int mode;           /* Mode prior to ".explain on" */
976  int showHeader;     /* The ".header" setting prior to ".explain on" */
977  int colWidth[100];  /* Column widths prior to ".explain on" */
978};
979
980typedef struct ExpertInfo ExpertInfo;
981struct ExpertInfo {
982  sqlite3expert *pExpert;
983  int bVerbose;
984};
985
986/* A single line in the EQP output */
987typedef struct EQPGraphRow EQPGraphRow;
988struct EQPGraphRow {
989  int iEqpId;           /* ID for this row */
990  int iParentId;        /* ID of the parent row */
991  EQPGraphRow *pNext;   /* Next row in sequence */
992  char zText[1];        /* Text to display for this row */
993};
994
995/* All EQP output is collected into an instance of the following */
996typedef struct EQPGraph EQPGraph;
997struct EQPGraph {
998  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
999  EQPGraphRow *pLast;   /* Last element of the pRow list */
1000  char zPrefix[100];    /* Graph prefix */
1001};
1002
1003/*
1004** State information about the database connection is contained in an
1005** instance of the following structure.
1006*/
1007typedef struct ShellState ShellState;
1008struct ShellState {
1009  sqlite3 *db;           /* The database */
1010  u8 autoExplain;        /* Automatically turn on .explain mode */
1011  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1012  u8 autoEQPtest;        /* autoEQP is in test mode */
1013  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1014  u8 statsOn;            /* True to display memory stats before each finalize */
1015  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1016  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1017  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1018  u8 nEqpLevel;          /* Depth of the EQP output graph */
1019  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1020  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1021  int outCount;          /* Revert to stdout when reaching zero */
1022  int cnt;               /* Number of records displayed so far */
1023  int lineno;            /* Line number of last line read from in */
1024  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1025  FILE *in;              /* Read commands from this stream */
1026  FILE *out;             /* Write results here */
1027  FILE *traceOut;        /* Output for sqlite3_trace() */
1028  int nErr;              /* Number of errors seen */
1029  int mode;              /* An output mode setting */
1030  int modePrior;         /* Saved mode */
1031  int cMode;             /* temporary output mode for the current query */
1032  int normalMode;        /* Output mode before ".explain on" */
1033  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1034  int showHeader;        /* True to show column names in List or Column mode */
1035  int nCheck;            /* Number of ".check" commands run */
1036  unsigned nProgress;    /* Number of progress callbacks encountered */
1037  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1038  unsigned flgProgress;  /* Flags for the progress callback */
1039  unsigned shellFlgs;    /* Various flags */
1040  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1041  char *zDestTable;      /* Name of destination table when MODE_Insert */
1042  char *zTempFile;       /* Temporary file that might need deleting */
1043  char zTestcase[30];    /* Name of current test case */
1044  char colSeparator[20]; /* Column separator character for several modes */
1045  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1046  char colSepPrior[20];  /* Saved column separator */
1047  char rowSepPrior[20];  /* Saved row separator */
1048  int colWidth[100];     /* Requested width of each column when in column mode*/
1049  int actualWidth[100];  /* Actual width of each column */
1050  char nullValue[20];    /* The text to print when a NULL comes back from
1051                         ** the database */
1052  char outfile[FILENAME_MAX]; /* Filename for *out */
1053  const char *zDbFilename;    /* name of the database file */
1054  char *zFreeOnClose;         /* Filename to free when closing */
1055  const char *zVfs;           /* Name of VFS to use */
1056  sqlite3_stmt *pStmt;   /* Current statement if any. */
1057  FILE *pLog;            /* Write log output here */
1058  int *aiIndent;         /* Array of indents used in MODE_Explain */
1059  int nIndent;           /* Size of array aiIndent[] */
1060  int iIndent;           /* Index of current op in aiIndent[] */
1061  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1062#if defined(SQLITE_ENABLE_SESSION)
1063  int nSession;             /* Number of active sessions */
1064  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1065#endif
1066  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1067};
1068
1069
1070/* Allowed values for ShellState.autoEQP
1071*/
1072#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1073#define AUTOEQP_on       1           /* Automatic EQP is on */
1074#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1075#define AUTOEQP_full     3           /* Show full EXPLAIN */
1076
1077/* Allowed values for ShellState.openMode
1078*/
1079#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1080#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1081#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1082#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1083#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1084#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1085#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1086
1087/* Allowed values for ShellState.eTraceType
1088*/
1089#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1090#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1091#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1092
1093/* Bits in the ShellState.flgProgress variable */
1094#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1095#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1096                                   ** callback limit is reached, and for each
1097                                   ** top-level SQL statement */
1098#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1099
1100/*
1101** These are the allowed shellFlgs values
1102*/
1103#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1104#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1105#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1106#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1107#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1108#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1109#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1110
1111/*
1112** Macros for testing and setting shellFlgs
1113*/
1114#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1115#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1116#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1117
1118/*
1119** These are the allowed modes.
1120*/
1121#define MODE_Line     0  /* One column per line.  Blank line between records */
1122#define MODE_Column   1  /* One record per line in neat columns */
1123#define MODE_List     2  /* One record per line with a separator */
1124#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1125#define MODE_Html     4  /* Generate an XHTML table */
1126#define MODE_Insert   5  /* Generate SQL "insert" statements */
1127#define MODE_Quote    6  /* Quote values as for SQL */
1128#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1129#define MODE_Csv      8  /* Quote strings, numbers are plain */
1130#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1131#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1132#define MODE_Pretty  11  /* Pretty-print schemas */
1133#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1134
1135static const char *modeDescr[] = {
1136  "line",
1137  "column",
1138  "list",
1139  "semi",
1140  "html",
1141  "insert",
1142  "quote",
1143  "tcl",
1144  "csv",
1145  "explain",
1146  "ascii",
1147  "prettyprint",
1148  "eqp"
1149};
1150
1151/*
1152** These are the column/row/line separators used by the various
1153** import/export modes.
1154*/
1155#define SEP_Column    "|"
1156#define SEP_Row       "\n"
1157#define SEP_Tab       "\t"
1158#define SEP_Space     " "
1159#define SEP_Comma     ","
1160#define SEP_CrLf      "\r\n"
1161#define SEP_Unit      "\x1F"
1162#define SEP_Record    "\x1E"
1163
1164/*
1165** A callback for the sqlite3_log() interface.
1166*/
1167static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1168  ShellState *p = (ShellState*)pArg;
1169  if( p->pLog==0 ) return;
1170  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1171  fflush(p->pLog);
1172}
1173
1174/*
1175** SQL function:  shell_putsnl(X)
1176**
1177** Write the text X to the screen (or whatever output is being directed)
1178** adding a newline at the end, and then return X.
1179*/
1180static void shellPutsFunc(
1181  sqlite3_context *pCtx,
1182  int nVal,
1183  sqlite3_value **apVal
1184){
1185  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1186  (void)nVal;
1187  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1188  sqlite3_result_value(pCtx, apVal[0]);
1189}
1190
1191/*
1192** SQL function:   edit(VALUE)
1193**                 edit(VALUE,EDITOR)
1194**
1195** These steps:
1196**
1197**     (1) Write VALUE into a temporary file.
1198**     (2) Run program EDITOR on that temporary file.
1199**     (3) Read the temporary file back and return its content as the result.
1200**     (4) Delete the temporary file
1201**
1202** If the EDITOR argument is omitted, use the value in the VISUAL
1203** environment variable.  If still there is no EDITOR, through an error.
1204**
1205** Also throw an error if the EDITOR program returns a non-zero exit code.
1206*/
1207#ifndef SQLITE_NOHAVE_SYSTEM
1208static void editFunc(
1209  sqlite3_context *context,
1210  int argc,
1211  sqlite3_value **argv
1212){
1213  const char *zEditor;
1214  char *zTempFile = 0;
1215  sqlite3 *db;
1216  char *zCmd = 0;
1217  int bBin;
1218  int rc;
1219  int hasCRNL = 0;
1220  FILE *f = 0;
1221  sqlite3_int64 sz;
1222  sqlite3_int64 x;
1223  unsigned char *p = 0;
1224
1225  if( argc==2 ){
1226    zEditor = (const char*)sqlite3_value_text(argv[1]);
1227  }else{
1228    zEditor = getenv("VISUAL");
1229  }
1230  if( zEditor==0 ){
1231    sqlite3_result_error(context, "no editor for edit()", -1);
1232    return;
1233  }
1234  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1235    sqlite3_result_error(context, "NULL input to edit()", -1);
1236    return;
1237  }
1238  db = sqlite3_context_db_handle(context);
1239  zTempFile = 0;
1240  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1241  if( zTempFile==0 ){
1242    sqlite3_uint64 r = 0;
1243    sqlite3_randomness(sizeof(r), &r);
1244    zTempFile = sqlite3_mprintf("temp%llx", r);
1245    if( zTempFile==0 ){
1246      sqlite3_result_error_nomem(context);
1247      return;
1248    }
1249  }
1250  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1251  /* When writing the file to be edited, do \n to \r\n conversions on systems
1252  ** that want \r\n line endings */
1253  f = fopen(zTempFile, bBin ? "wb" : "w");
1254  if( f==0 ){
1255    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1256    goto edit_func_end;
1257  }
1258  sz = sqlite3_value_bytes(argv[0]);
1259  if( bBin ){
1260    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1261  }else{
1262    const char *z = (const char*)sqlite3_value_text(argv[0]);
1263    /* Remember whether or not the value originally contained \r\n */
1264    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1265    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1266  }
1267  fclose(f);
1268  f = 0;
1269  if( x!=sz ){
1270    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1271    goto edit_func_end;
1272  }
1273  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1274  if( zCmd==0 ){
1275    sqlite3_result_error_nomem(context);
1276    goto edit_func_end;
1277  }
1278  rc = system(zCmd);
1279  sqlite3_free(zCmd);
1280  if( rc ){
1281    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1282    goto edit_func_end;
1283  }
1284  f = fopen(zTempFile, "rb");
1285  if( f==0 ){
1286    sqlite3_result_error(context,
1287      "edit() cannot reopen temp file after edit", -1);
1288    goto edit_func_end;
1289  }
1290  fseek(f, 0, SEEK_END);
1291  sz = ftell(f);
1292  rewind(f);
1293  p = sqlite3_malloc64( sz+1 );
1294  if( p==0 ){
1295    sqlite3_result_error_nomem(context);
1296    goto edit_func_end;
1297  }
1298  x = fread(p, 1, (size_t)sz, f);
1299  fclose(f);
1300  f = 0;
1301  if( x!=sz ){
1302    sqlite3_result_error(context, "could not read back the whole file", -1);
1303    goto edit_func_end;
1304  }
1305  if( bBin ){
1306    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1307  }else{
1308    sqlite3_int64 i, j;
1309    if( hasCRNL ){
1310      /* If the original contains \r\n then do no conversions back to \n */
1311      j = sz;
1312    }else{
1313      /* If the file did not originally contain \r\n then convert any new
1314      ** \r\n back into \n */
1315      for(i=j=0; i<sz; i++){
1316        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1317        p[j++] = p[i];
1318      }
1319      sz = j;
1320      p[sz] = 0;
1321    }
1322    sqlite3_result_text64(context, (const char*)p, sz,
1323                          sqlite3_free, SQLITE_UTF8);
1324  }
1325  p = 0;
1326
1327edit_func_end:
1328  if( f ) fclose(f);
1329  unlink(zTempFile);
1330  sqlite3_free(zTempFile);
1331  sqlite3_free(p);
1332}
1333#endif /* SQLITE_NOHAVE_SYSTEM */
1334
1335/*
1336** Save or restore the current output mode
1337*/
1338static void outputModePush(ShellState *p){
1339  p->modePrior = p->mode;
1340  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1341  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1342}
1343static void outputModePop(ShellState *p){
1344  p->mode = p->modePrior;
1345  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1346  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1347}
1348
1349/*
1350** Output the given string as a hex-encoded blob (eg. X'1234' )
1351*/
1352static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1353  int i;
1354  char *zBlob = (char *)pBlob;
1355  raw_printf(out,"X'");
1356  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1357  raw_printf(out,"'");
1358}
1359
1360/*
1361** Find a string that is not found anywhere in z[].  Return a pointer
1362** to that string.
1363**
1364** Try to use zA and zB first.  If both of those are already found in z[]
1365** then make up some string and store it in the buffer zBuf.
1366*/
1367static const char *unused_string(
1368  const char *z,                    /* Result must not appear anywhere in z */
1369  const char *zA, const char *zB,   /* Try these first */
1370  char *zBuf                        /* Space to store a generated string */
1371){
1372  unsigned i = 0;
1373  if( strstr(z, zA)==0 ) return zA;
1374  if( strstr(z, zB)==0 ) return zB;
1375  do{
1376    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1377  }while( strstr(z,zBuf)!=0 );
1378  return zBuf;
1379}
1380
1381/*
1382** Output the given string as a quoted string using SQL quoting conventions.
1383**
1384** See also: output_quoted_escaped_string()
1385*/
1386static void output_quoted_string(FILE *out, const char *z){
1387  int i;
1388  char c;
1389  setBinaryMode(out, 1);
1390  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1391  if( c==0 ){
1392    utf8_printf(out,"'%s'",z);
1393  }else{
1394    raw_printf(out, "'");
1395    while( *z ){
1396      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1397      if( c=='\'' ) i++;
1398      if( i ){
1399        utf8_printf(out, "%.*s", i, z);
1400        z += i;
1401      }
1402      if( c=='\'' ){
1403        raw_printf(out, "'");
1404        continue;
1405      }
1406      if( c==0 ){
1407        break;
1408      }
1409      z++;
1410    }
1411    raw_printf(out, "'");
1412  }
1413  setTextMode(out, 1);
1414}
1415
1416/*
1417** Output the given string as a quoted string using SQL quoting conventions.
1418** Additionallly , escape the "\n" and "\r" characters so that they do not
1419** get corrupted by end-of-line translation facilities in some operating
1420** systems.
1421**
1422** This is like output_quoted_string() but with the addition of the \r\n
1423** escape mechanism.
1424*/
1425static void output_quoted_escaped_string(FILE *out, const char *z){
1426  int i;
1427  char c;
1428  setBinaryMode(out, 1);
1429  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1430  if( c==0 ){
1431    utf8_printf(out,"'%s'",z);
1432  }else{
1433    const char *zNL = 0;
1434    const char *zCR = 0;
1435    int nNL = 0;
1436    int nCR = 0;
1437    char zBuf1[20], zBuf2[20];
1438    for(i=0; z[i]; i++){
1439      if( z[i]=='\n' ) nNL++;
1440      if( z[i]=='\r' ) nCR++;
1441    }
1442    if( nNL ){
1443      raw_printf(out, "replace(");
1444      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1445    }
1446    if( nCR ){
1447      raw_printf(out, "replace(");
1448      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1449    }
1450    raw_printf(out, "'");
1451    while( *z ){
1452      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1453      if( c=='\'' ) i++;
1454      if( i ){
1455        utf8_printf(out, "%.*s", i, z);
1456        z += i;
1457      }
1458      if( c=='\'' ){
1459        raw_printf(out, "'");
1460        continue;
1461      }
1462      if( c==0 ){
1463        break;
1464      }
1465      z++;
1466      if( c=='\n' ){
1467        raw_printf(out, "%s", zNL);
1468        continue;
1469      }
1470      raw_printf(out, "%s", zCR);
1471    }
1472    raw_printf(out, "'");
1473    if( nCR ){
1474      raw_printf(out, ",'%s',char(13))", zCR);
1475    }
1476    if( nNL ){
1477      raw_printf(out, ",'%s',char(10))", zNL);
1478    }
1479  }
1480  setTextMode(out, 1);
1481}
1482
1483/*
1484** Output the given string as a quoted according to C or TCL quoting rules.
1485*/
1486static void output_c_string(FILE *out, const char *z){
1487  unsigned int c;
1488  fputc('"', out);
1489  while( (c = *(z++))!=0 ){
1490    if( c=='\\' ){
1491      fputc(c, out);
1492      fputc(c, out);
1493    }else if( c=='"' ){
1494      fputc('\\', out);
1495      fputc('"', out);
1496    }else if( c=='\t' ){
1497      fputc('\\', out);
1498      fputc('t', out);
1499    }else if( c=='\n' ){
1500      fputc('\\', out);
1501      fputc('n', out);
1502    }else if( c=='\r' ){
1503      fputc('\\', out);
1504      fputc('r', out);
1505    }else if( !isprint(c&0xff) ){
1506      raw_printf(out, "\\%03o", c&0xff);
1507    }else{
1508      fputc(c, out);
1509    }
1510  }
1511  fputc('"', out);
1512}
1513
1514/*
1515** Output the given string with characters that are special to
1516** HTML escaped.
1517*/
1518static void output_html_string(FILE *out, const char *z){
1519  int i;
1520  if( z==0 ) z = "";
1521  while( *z ){
1522    for(i=0;   z[i]
1523            && z[i]!='<'
1524            && z[i]!='&'
1525            && z[i]!='>'
1526            && z[i]!='\"'
1527            && z[i]!='\'';
1528        i++){}
1529    if( i>0 ){
1530      utf8_printf(out,"%.*s",i,z);
1531    }
1532    if( z[i]=='<' ){
1533      raw_printf(out,"&lt;");
1534    }else if( z[i]=='&' ){
1535      raw_printf(out,"&amp;");
1536    }else if( z[i]=='>' ){
1537      raw_printf(out,"&gt;");
1538    }else if( z[i]=='\"' ){
1539      raw_printf(out,"&quot;");
1540    }else if( z[i]=='\'' ){
1541      raw_printf(out,"&#39;");
1542    }else{
1543      break;
1544    }
1545    z += i + 1;
1546  }
1547}
1548
1549/*
1550** If a field contains any character identified by a 1 in the following
1551** array, then the string must be quoted for CSV.
1552*/
1553static const char needCsvQuote[] = {
1554  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1555  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1556  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1557  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1558  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1559  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1560  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1561  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1562  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1563  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1564  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1565  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1566  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1567  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1568  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1569  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1570};
1571
1572/*
1573** Output a single term of CSV.  Actually, p->colSeparator is used for
1574** the separator, which may or may not be a comma.  p->nullValue is
1575** the null value.  Strings are quoted if necessary.  The separator
1576** is only issued if bSep is true.
1577*/
1578static void output_csv(ShellState *p, const char *z, int bSep){
1579  FILE *out = p->out;
1580  if( z==0 ){
1581    utf8_printf(out,"%s",p->nullValue);
1582  }else{
1583    int i;
1584    int nSep = strlen30(p->colSeparator);
1585    for(i=0; z[i]; i++){
1586      if( needCsvQuote[((unsigned char*)z)[i]]
1587         || (z[i]==p->colSeparator[0] &&
1588             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1589        i = 0;
1590        break;
1591      }
1592    }
1593    if( i==0 ){
1594      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1595      utf8_printf(out, "%s", zQuoted);
1596      sqlite3_free(zQuoted);
1597    }else{
1598      utf8_printf(out, "%s", z);
1599    }
1600  }
1601  if( bSep ){
1602    utf8_printf(p->out, "%s", p->colSeparator);
1603  }
1604}
1605
1606/*
1607** This routine runs when the user presses Ctrl-C
1608*/
1609static void interrupt_handler(int NotUsed){
1610  UNUSED_PARAMETER(NotUsed);
1611  seenInterrupt++;
1612  if( seenInterrupt>2 ) exit(1);
1613  if( globalDb ) sqlite3_interrupt(globalDb);
1614}
1615
1616#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1617/*
1618** This routine runs for console events (e.g. Ctrl-C) on Win32
1619*/
1620static BOOL WINAPI ConsoleCtrlHandler(
1621  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1622){
1623  if( dwCtrlType==CTRL_C_EVENT ){
1624    interrupt_handler(0);
1625    return TRUE;
1626  }
1627  return FALSE;
1628}
1629#endif
1630
1631#ifndef SQLITE_OMIT_AUTHORIZATION
1632/*
1633** When the ".auth ON" is set, the following authorizer callback is
1634** invoked.  It always returns SQLITE_OK.
1635*/
1636static int shellAuth(
1637  void *pClientData,
1638  int op,
1639  const char *zA1,
1640  const char *zA2,
1641  const char *zA3,
1642  const char *zA4
1643){
1644  ShellState *p = (ShellState*)pClientData;
1645  static const char *azAction[] = { 0,
1646     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1647     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1648     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1649     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1650     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1651     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1652     "PRAGMA",               "READ",                 "SELECT",
1653     "TRANSACTION",          "UPDATE",               "ATTACH",
1654     "DETACH",               "ALTER_TABLE",          "REINDEX",
1655     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1656     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1657  };
1658  int i;
1659  const char *az[4];
1660  az[0] = zA1;
1661  az[1] = zA2;
1662  az[2] = zA3;
1663  az[3] = zA4;
1664  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1665  for(i=0; i<4; i++){
1666    raw_printf(p->out, " ");
1667    if( az[i] ){
1668      output_c_string(p->out, az[i]);
1669    }else{
1670      raw_printf(p->out, "NULL");
1671    }
1672  }
1673  raw_printf(p->out, "\n");
1674  return SQLITE_OK;
1675}
1676#endif
1677
1678/*
1679** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1680**
1681** This routine converts some CREATE TABLE statements for shadow tables
1682** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1683*/
1684static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1685  if( z==0 ) return;
1686  if( zTail==0 ) return;
1687  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1688    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1689  }else{
1690    utf8_printf(out, "%s%s", z, zTail);
1691  }
1692}
1693static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1694  char c = z[n];
1695  z[n] = 0;
1696  printSchemaLine(out, z, zTail);
1697  z[n] = c;
1698}
1699
1700/*
1701** Return true if string z[] has nothing but whitespace and comments to the
1702** end of the first line.
1703*/
1704static int wsToEol(const char *z){
1705  int i;
1706  for(i=0; z[i]; i++){
1707    if( z[i]=='\n' ) return 1;
1708    if( IsSpace(z[i]) ) continue;
1709    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1710    return 0;
1711  }
1712  return 1;
1713}
1714
1715/*
1716** Add a new entry to the EXPLAIN QUERY PLAN data
1717*/
1718static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1719  EQPGraphRow *pNew;
1720  int nText = strlen30(zText);
1721  if( p->autoEQPtest ){
1722    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1723  }
1724  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1725  if( pNew==0 ) shell_out_of_memory();
1726  pNew->iEqpId = iEqpId;
1727  pNew->iParentId = p2;
1728  memcpy(pNew->zText, zText, nText+1);
1729  pNew->pNext = 0;
1730  if( p->sGraph.pLast ){
1731    p->sGraph.pLast->pNext = pNew;
1732  }else{
1733    p->sGraph.pRow = pNew;
1734  }
1735  p->sGraph.pLast = pNew;
1736}
1737
1738/*
1739** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1740** in p->sGraph.
1741*/
1742static void eqp_reset(ShellState *p){
1743  EQPGraphRow *pRow, *pNext;
1744  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1745    pNext = pRow->pNext;
1746    sqlite3_free(pRow);
1747  }
1748  memset(&p->sGraph, 0, sizeof(p->sGraph));
1749}
1750
1751/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1752** pOld, or return the first such line if pOld is NULL
1753*/
1754static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1755  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1756  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1757  return pRow;
1758}
1759
1760/* Render a single level of the graph that has iEqpId as its parent.  Called
1761** recursively to render sublevels.
1762*/
1763static void eqp_render_level(ShellState *p, int iEqpId){
1764  EQPGraphRow *pRow, *pNext;
1765  int n = strlen30(p->sGraph.zPrefix);
1766  char *z;
1767  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1768    pNext = eqp_next_row(p, iEqpId, pRow);
1769    z = pRow->zText;
1770    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1771                pNext ? "|--" : "`--", z);
1772    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1773      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1774      eqp_render_level(p, pRow->iEqpId);
1775      p->sGraph.zPrefix[n] = 0;
1776    }
1777  }
1778}
1779
1780/*
1781** Display and reset the EXPLAIN QUERY PLAN data
1782*/
1783static void eqp_render(ShellState *p){
1784  EQPGraphRow *pRow = p->sGraph.pRow;
1785  if( pRow ){
1786    if( pRow->zText[0]=='-' ){
1787      if( pRow->pNext==0 ){
1788        eqp_reset(p);
1789        return;
1790      }
1791      utf8_printf(p->out, "%s\n", pRow->zText+3);
1792      p->sGraph.pRow = pRow->pNext;
1793      sqlite3_free(pRow);
1794    }else{
1795      utf8_printf(p->out, "QUERY PLAN\n");
1796    }
1797    p->sGraph.zPrefix[0] = 0;
1798    eqp_render_level(p, 0);
1799    eqp_reset(p);
1800  }
1801}
1802
1803#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1804/*
1805** Progress handler callback.
1806*/
1807static int progress_handler(void *pClientData) {
1808  ShellState *p = (ShellState*)pClientData;
1809  p->nProgress++;
1810  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1811    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1812    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1813    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1814    return 1;
1815  }
1816  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1817    raw_printf(p->out, "Progress %u\n", p->nProgress);
1818  }
1819  return 0;
1820}
1821#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1822
1823/*
1824** This is the callback routine that the shell
1825** invokes for each row of a query result.
1826*/
1827static int shell_callback(
1828  void *pArg,
1829  int nArg,        /* Number of result columns */
1830  char **azArg,    /* Text of each result column */
1831  char **azCol,    /* Column names */
1832  int *aiType      /* Column types */
1833){
1834  int i;
1835  ShellState *p = (ShellState*)pArg;
1836
1837  if( azArg==0 ) return 0;
1838  switch( p->cMode ){
1839    case MODE_Line: {
1840      int w = 5;
1841      if( azArg==0 ) break;
1842      for(i=0; i<nArg; i++){
1843        int len = strlen30(azCol[i] ? azCol[i] : "");
1844        if( len>w ) w = len;
1845      }
1846      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
1847      for(i=0; i<nArg; i++){
1848        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
1849                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
1850      }
1851      break;
1852    }
1853    case MODE_Explain:
1854    case MODE_Column: {
1855      static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
1856      const int *colWidth;
1857      int showHdr;
1858      char *rowSep;
1859      int nWidth;
1860      if( p->cMode==MODE_Column ){
1861        colWidth = p->colWidth;
1862        nWidth = ArraySize(p->colWidth);
1863        showHdr = p->showHeader;
1864        rowSep = p->rowSeparator;
1865      }else{
1866        colWidth = aExplainWidths;
1867        nWidth = ArraySize(aExplainWidths);
1868        showHdr = 1;
1869        rowSep = SEP_Row;
1870      }
1871      if( p->cnt++==0 ){
1872        for(i=0; i<nArg; i++){
1873          int w, n;
1874          if( i<nWidth ){
1875            w = colWidth[i];
1876          }else{
1877            w = 0;
1878          }
1879          if( w==0 ){
1880            w = strlenChar(azCol[i] ? azCol[i] : "");
1881            if( w<10 ) w = 10;
1882            n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
1883            if( w<n ) w = n;
1884          }
1885          if( i<ArraySize(p->actualWidth) ){
1886            p->actualWidth[i] = w;
1887          }
1888          if( showHdr ){
1889            utf8_width_print(p->out, w, azCol[i]);
1890            utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
1891          }
1892        }
1893        if( showHdr ){
1894          for(i=0; i<nArg; i++){
1895            int w;
1896            if( i<ArraySize(p->actualWidth) ){
1897               w = p->actualWidth[i];
1898               if( w<0 ) w = -w;
1899            }else{
1900               w = 10;
1901            }
1902            utf8_printf(p->out,"%-*.*s%s",w,w,
1903                   "----------------------------------------------------------"
1904                   "----------------------------------------------------------",
1905                    i==nArg-1 ? rowSep : "  ");
1906          }
1907        }
1908      }
1909      if( azArg==0 ) break;
1910      for(i=0; i<nArg; i++){
1911        int w;
1912        if( i<ArraySize(p->actualWidth) ){
1913           w = p->actualWidth[i];
1914        }else{
1915           w = 10;
1916        }
1917        if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
1918          w = strlenChar(azArg[i]);
1919        }
1920        if( i==1 && p->aiIndent && p->pStmt ){
1921          if( p->iIndent<p->nIndent ){
1922            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
1923          }
1924          p->iIndent++;
1925        }
1926        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
1927        utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
1928      }
1929      break;
1930    }
1931    case MODE_Semi: {   /* .schema and .fullschema output */
1932      printSchemaLine(p->out, azArg[0], ";\n");
1933      break;
1934    }
1935    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
1936      char *z;
1937      int j;
1938      int nParen = 0;
1939      char cEnd = 0;
1940      char c;
1941      int nLine = 0;
1942      assert( nArg==1 );
1943      if( azArg[0]==0 ) break;
1944      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
1945       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
1946      ){
1947        utf8_printf(p->out, "%s;\n", azArg[0]);
1948        break;
1949      }
1950      z = sqlite3_mprintf("%s", azArg[0]);
1951      j = 0;
1952      for(i=0; IsSpace(z[i]); i++){}
1953      for(; (c = z[i])!=0; i++){
1954        if( IsSpace(c) ){
1955          if( z[j-1]=='\r' ) z[j-1] = '\n';
1956          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
1957        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
1958          j--;
1959        }
1960        z[j++] = c;
1961      }
1962      while( j>0 && IsSpace(z[j-1]) ){ j--; }
1963      z[j] = 0;
1964      if( strlen30(z)>=79 ){
1965        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
1966          if( c==cEnd ){
1967            cEnd = 0;
1968          }else if( c=='"' || c=='\'' || c=='`' ){
1969            cEnd = c;
1970          }else if( c=='[' ){
1971            cEnd = ']';
1972          }else if( c=='-' && z[i+1]=='-' ){
1973            cEnd = '\n';
1974          }else if( c=='(' ){
1975            nParen++;
1976          }else if( c==')' ){
1977            nParen--;
1978            if( nLine>0 && nParen==0 && j>0 ){
1979              printSchemaLineN(p->out, z, j, "\n");
1980              j = 0;
1981            }
1982          }
1983          z[j++] = c;
1984          if( nParen==1 && cEnd==0
1985           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
1986          ){
1987            if( c=='\n' ) j--;
1988            printSchemaLineN(p->out, z, j, "\n  ");
1989            j = 0;
1990            nLine++;
1991            while( IsSpace(z[i+1]) ){ i++; }
1992          }
1993        }
1994        z[j] = 0;
1995      }
1996      printSchemaLine(p->out, z, ";\n");
1997      sqlite3_free(z);
1998      break;
1999    }
2000    case MODE_List: {
2001      if( p->cnt++==0 && p->showHeader ){
2002        for(i=0; i<nArg; i++){
2003          utf8_printf(p->out,"%s%s",azCol[i],
2004                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2005        }
2006      }
2007      if( azArg==0 ) break;
2008      for(i=0; i<nArg; i++){
2009        char *z = azArg[i];
2010        if( z==0 ) z = p->nullValue;
2011        utf8_printf(p->out, "%s", z);
2012        if( i<nArg-1 ){
2013          utf8_printf(p->out, "%s", p->colSeparator);
2014        }else{
2015          utf8_printf(p->out, "%s", p->rowSeparator);
2016        }
2017      }
2018      break;
2019    }
2020    case MODE_Html: {
2021      if( p->cnt++==0 && p->showHeader ){
2022        raw_printf(p->out,"<TR>");
2023        for(i=0; i<nArg; i++){
2024          raw_printf(p->out,"<TH>");
2025          output_html_string(p->out, azCol[i]);
2026          raw_printf(p->out,"</TH>\n");
2027        }
2028        raw_printf(p->out,"</TR>\n");
2029      }
2030      if( azArg==0 ) break;
2031      raw_printf(p->out,"<TR>");
2032      for(i=0; i<nArg; i++){
2033        raw_printf(p->out,"<TD>");
2034        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2035        raw_printf(p->out,"</TD>\n");
2036      }
2037      raw_printf(p->out,"</TR>\n");
2038      break;
2039    }
2040    case MODE_Tcl: {
2041      if( p->cnt++==0 && p->showHeader ){
2042        for(i=0; i<nArg; i++){
2043          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2044          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2045        }
2046        utf8_printf(p->out, "%s", p->rowSeparator);
2047      }
2048      if( azArg==0 ) break;
2049      for(i=0; i<nArg; i++){
2050        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2051        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2052      }
2053      utf8_printf(p->out, "%s", p->rowSeparator);
2054      break;
2055    }
2056    case MODE_Csv: {
2057      setBinaryMode(p->out, 1);
2058      if( p->cnt++==0 && p->showHeader ){
2059        for(i=0; i<nArg; i++){
2060          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2061        }
2062        utf8_printf(p->out, "%s", p->rowSeparator);
2063      }
2064      if( nArg>0 ){
2065        for(i=0; i<nArg; i++){
2066          output_csv(p, azArg[i], i<nArg-1);
2067        }
2068        utf8_printf(p->out, "%s", p->rowSeparator);
2069      }
2070      setTextMode(p->out, 1);
2071      break;
2072    }
2073    case MODE_Insert: {
2074      if( azArg==0 ) break;
2075      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2076      if( p->showHeader ){
2077        raw_printf(p->out,"(");
2078        for(i=0; i<nArg; i++){
2079          if( i>0 ) raw_printf(p->out, ",");
2080          if( quoteChar(azCol[i]) ){
2081            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2082            utf8_printf(p->out, "%s", z);
2083            sqlite3_free(z);
2084          }else{
2085            raw_printf(p->out, "%s", azCol[i]);
2086          }
2087        }
2088        raw_printf(p->out,")");
2089      }
2090      p->cnt++;
2091      for(i=0; i<nArg; i++){
2092        raw_printf(p->out, i>0 ? "," : " VALUES(");
2093        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2094          utf8_printf(p->out,"NULL");
2095        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2096          if( ShellHasFlag(p, SHFLG_Newlines) ){
2097            output_quoted_string(p->out, azArg[i]);
2098          }else{
2099            output_quoted_escaped_string(p->out, azArg[i]);
2100          }
2101        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2102          utf8_printf(p->out,"%s", azArg[i]);
2103        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2104          char z[50];
2105          double r = sqlite3_column_double(p->pStmt, i);
2106          sqlite3_uint64 ur;
2107          memcpy(&ur,&r,sizeof(r));
2108          if( ur==0x7ff0000000000000LL ){
2109            raw_printf(p->out, "1e999");
2110          }else if( ur==0xfff0000000000000LL ){
2111            raw_printf(p->out, "-1e999");
2112          }else{
2113            sqlite3_snprintf(50,z,"%!.20g", r);
2114            raw_printf(p->out, "%s", z);
2115          }
2116        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2117          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2118          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2119          output_hex_blob(p->out, pBlob, nBlob);
2120        }else if( isNumber(azArg[i], 0) ){
2121          utf8_printf(p->out,"%s", azArg[i]);
2122        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2123          output_quoted_string(p->out, azArg[i]);
2124        }else{
2125          output_quoted_escaped_string(p->out, azArg[i]);
2126        }
2127      }
2128      raw_printf(p->out,");\n");
2129      break;
2130    }
2131    case MODE_Quote: {
2132      if( azArg==0 ) break;
2133      if( p->cnt==0 && p->showHeader ){
2134        for(i=0; i<nArg; i++){
2135          if( i>0 ) raw_printf(p->out, ",");
2136          output_quoted_string(p->out, azCol[i]);
2137        }
2138        raw_printf(p->out,"\n");
2139      }
2140      p->cnt++;
2141      for(i=0; i<nArg; i++){
2142        if( i>0 ) raw_printf(p->out, ",");
2143        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2144          utf8_printf(p->out,"NULL");
2145        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2146          output_quoted_string(p->out, azArg[i]);
2147        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2148          utf8_printf(p->out,"%s", azArg[i]);
2149        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2150          char z[50];
2151          double r = sqlite3_column_double(p->pStmt, i);
2152          sqlite3_snprintf(50,z,"%!.20g", r);
2153          raw_printf(p->out, "%s", z);
2154        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2155          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2156          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2157          output_hex_blob(p->out, pBlob, nBlob);
2158        }else if( isNumber(azArg[i], 0) ){
2159          utf8_printf(p->out,"%s", azArg[i]);
2160        }else{
2161          output_quoted_string(p->out, azArg[i]);
2162        }
2163      }
2164      raw_printf(p->out,"\n");
2165      break;
2166    }
2167    case MODE_Ascii: {
2168      if( p->cnt++==0 && p->showHeader ){
2169        for(i=0; i<nArg; i++){
2170          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2171          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2172        }
2173        utf8_printf(p->out, "%s", p->rowSeparator);
2174      }
2175      if( azArg==0 ) break;
2176      for(i=0; i<nArg; i++){
2177        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2178        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2179      }
2180      utf8_printf(p->out, "%s", p->rowSeparator);
2181      break;
2182    }
2183    case MODE_EQP: {
2184      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2185      break;
2186    }
2187  }
2188  return 0;
2189}
2190
2191/*
2192** This is the callback routine that the SQLite library
2193** invokes for each row of a query result.
2194*/
2195static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2196  /* since we don't have type info, call the shell_callback with a NULL value */
2197  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2198}
2199
2200/*
2201** This is the callback routine from sqlite3_exec() that appends all
2202** output onto the end of a ShellText object.
2203*/
2204static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2205  ShellText *p = (ShellText*)pArg;
2206  int i;
2207  UNUSED_PARAMETER(az);
2208  if( azArg==0 ) return 0;
2209  if( p->n ) appendText(p, "|", 0);
2210  for(i=0; i<nArg; i++){
2211    if( i ) appendText(p, ",", 0);
2212    if( azArg[i] ) appendText(p, azArg[i], 0);
2213  }
2214  return 0;
2215}
2216
2217/*
2218** Generate an appropriate SELFTEST table in the main database.
2219*/
2220static void createSelftestTable(ShellState *p){
2221  char *zErrMsg = 0;
2222  sqlite3_exec(p->db,
2223    "SAVEPOINT selftest_init;\n"
2224    "CREATE TABLE IF NOT EXISTS selftest(\n"
2225    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2226    "  op TEXT,\n"                   /* Operator:  memo run */
2227    "  cmd TEXT,\n"                  /* Command text */
2228    "  ans TEXT\n"                   /* Desired answer */
2229    ");"
2230    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2231    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2232    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2233    "         'memo','Tests generated by --init');\n"
2234    "INSERT INTO [_shell$self]\n"
2235    "  SELECT 'run',\n"
2236    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2237                                 "FROM sqlite_master ORDER BY 2'',224))',\n"
2238    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2239                          "FROM sqlite_master ORDER BY 2',224));\n"
2240    "INSERT INTO [_shell$self]\n"
2241    "  SELECT 'run',"
2242    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2243    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2244    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2245    "  FROM (\n"
2246    "    SELECT name FROM sqlite_master\n"
2247    "     WHERE type='table'\n"
2248    "       AND name<>'selftest'\n"
2249    "       AND coalesce(rootpage,0)>0\n"
2250    "  )\n"
2251    " ORDER BY name;\n"
2252    "INSERT INTO [_shell$self]\n"
2253    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2254    "INSERT INTO selftest(tno,op,cmd,ans)"
2255    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2256    "DROP TABLE [_shell$self];"
2257    ,0,0,&zErrMsg);
2258  if( zErrMsg ){
2259    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2260    sqlite3_free(zErrMsg);
2261  }
2262  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2263}
2264
2265
2266/*
2267** Set the destination table field of the ShellState structure to
2268** the name of the table given.  Escape any quote characters in the
2269** table name.
2270*/
2271static void set_table_name(ShellState *p, const char *zName){
2272  int i, n;
2273  char cQuote;
2274  char *z;
2275
2276  if( p->zDestTable ){
2277    free(p->zDestTable);
2278    p->zDestTable = 0;
2279  }
2280  if( zName==0 ) return;
2281  cQuote = quoteChar(zName);
2282  n = strlen30(zName);
2283  if( cQuote ) n += n+2;
2284  z = p->zDestTable = malloc( n+1 );
2285  if( z==0 ) shell_out_of_memory();
2286  n = 0;
2287  if( cQuote ) z[n++] = cQuote;
2288  for(i=0; zName[i]; i++){
2289    z[n++] = zName[i];
2290    if( zName[i]==cQuote ) z[n++] = cQuote;
2291  }
2292  if( cQuote ) z[n++] = cQuote;
2293  z[n] = 0;
2294}
2295
2296
2297/*
2298** Execute a query statement that will generate SQL output.  Print
2299** the result columns, comma-separated, on a line and then add a
2300** semicolon terminator to the end of that line.
2301**
2302** If the number of columns is 1 and that column contains text "--"
2303** then write the semicolon on a separate line.  That way, if a
2304** "--" comment occurs at the end of the statement, the comment
2305** won't consume the semicolon terminator.
2306*/
2307static int run_table_dump_query(
2308  ShellState *p,           /* Query context */
2309  const char *zSelect,     /* SELECT statement to extract content */
2310  const char *zFirstRow    /* Print before first row, if not NULL */
2311){
2312  sqlite3_stmt *pSelect;
2313  int rc;
2314  int nResult;
2315  int i;
2316  const char *z;
2317  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2318  if( rc!=SQLITE_OK || !pSelect ){
2319    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2320                sqlite3_errmsg(p->db));
2321    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2322    return rc;
2323  }
2324  rc = sqlite3_step(pSelect);
2325  nResult = sqlite3_column_count(pSelect);
2326  while( rc==SQLITE_ROW ){
2327    if( zFirstRow ){
2328      utf8_printf(p->out, "%s", zFirstRow);
2329      zFirstRow = 0;
2330    }
2331    z = (const char*)sqlite3_column_text(pSelect, 0);
2332    utf8_printf(p->out, "%s", z);
2333    for(i=1; i<nResult; i++){
2334      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2335    }
2336    if( z==0 ) z = "";
2337    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2338    if( z[0] ){
2339      raw_printf(p->out, "\n;\n");
2340    }else{
2341      raw_printf(p->out, ";\n");
2342    }
2343    rc = sqlite3_step(pSelect);
2344  }
2345  rc = sqlite3_finalize(pSelect);
2346  if( rc!=SQLITE_OK ){
2347    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2348                sqlite3_errmsg(p->db));
2349    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2350  }
2351  return rc;
2352}
2353
2354/*
2355** Allocate space and save off current error string.
2356*/
2357static char *save_err_msg(
2358  sqlite3 *db            /* Database to query */
2359){
2360  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2361  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2362  if( zErrMsg ){
2363    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2364  }
2365  return zErrMsg;
2366}
2367
2368#ifdef __linux__
2369/*
2370** Attempt to display I/O stats on Linux using /proc/PID/io
2371*/
2372static void displayLinuxIoStats(FILE *out){
2373  FILE *in;
2374  char z[200];
2375  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2376  in = fopen(z, "rb");
2377  if( in==0 ) return;
2378  while( fgets(z, sizeof(z), in)!=0 ){
2379    static const struct {
2380      const char *zPattern;
2381      const char *zDesc;
2382    } aTrans[] = {
2383      { "rchar: ",                  "Bytes received by read():" },
2384      { "wchar: ",                  "Bytes sent to write():"    },
2385      { "syscr: ",                  "Read() system calls:"      },
2386      { "syscw: ",                  "Write() system calls:"     },
2387      { "read_bytes: ",             "Bytes read from storage:"  },
2388      { "write_bytes: ",            "Bytes written to storage:" },
2389      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2390    };
2391    int i;
2392    for(i=0; i<ArraySize(aTrans); i++){
2393      int n = strlen30(aTrans[i].zPattern);
2394      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2395        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2396        break;
2397      }
2398    }
2399  }
2400  fclose(in);
2401}
2402#endif
2403
2404/*
2405** Display a single line of status using 64-bit values.
2406*/
2407static void displayStatLine(
2408  ShellState *p,            /* The shell context */
2409  char *zLabel,             /* Label for this one line */
2410  char *zFormat,            /* Format for the result */
2411  int iStatusCtrl,          /* Which status to display */
2412  int bReset                /* True to reset the stats */
2413){
2414  sqlite3_int64 iCur = -1;
2415  sqlite3_int64 iHiwtr = -1;
2416  int i, nPercent;
2417  char zLine[200];
2418  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2419  for(i=0, nPercent=0; zFormat[i]; i++){
2420    if( zFormat[i]=='%' ) nPercent++;
2421  }
2422  if( nPercent>1 ){
2423    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2424  }else{
2425    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2426  }
2427  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2428}
2429
2430/*
2431** Display memory stats.
2432*/
2433static int display_stats(
2434  sqlite3 *db,                /* Database to query */
2435  ShellState *pArg,           /* Pointer to ShellState */
2436  int bReset                  /* True to reset the stats */
2437){
2438  int iCur;
2439  int iHiwtr;
2440  FILE *out;
2441  if( pArg==0 || pArg->out==0 ) return 0;
2442  out = pArg->out;
2443
2444  if( pArg->pStmt && (pArg->statsOn & 2) ){
2445    int nCol, i, x;
2446    sqlite3_stmt *pStmt = pArg->pStmt;
2447    char z[100];
2448    nCol = sqlite3_column_count(pStmt);
2449    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2450    for(i=0; i<nCol; i++){
2451      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2452      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2453#ifndef SQLITE_OMIT_DECLTYPE
2454      sqlite3_snprintf(30, z+x, "declared type:");
2455      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2456#endif
2457#ifdef SQLITE_ENABLE_COLUMN_METADATA
2458      sqlite3_snprintf(30, z+x, "database name:");
2459      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2460      sqlite3_snprintf(30, z+x, "table name:");
2461      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2462      sqlite3_snprintf(30, z+x, "origin name:");
2463      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2464#endif
2465    }
2466  }
2467
2468  displayStatLine(pArg, "Memory Used:",
2469     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2470  displayStatLine(pArg, "Number of Outstanding Allocations:",
2471     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2472  if( pArg->shellFlgs & SHFLG_Pagecache ){
2473    displayStatLine(pArg, "Number of Pcache Pages Used:",
2474       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2475  }
2476  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2477     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2478  displayStatLine(pArg, "Largest Allocation:",
2479     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2480  displayStatLine(pArg, "Largest Pcache Allocation:",
2481     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2482#ifdef YYTRACKMAXSTACKDEPTH
2483  displayStatLine(pArg, "Deepest Parser Stack:",
2484     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2485#endif
2486
2487  if( db ){
2488    if( pArg->shellFlgs & SHFLG_Lookaside ){
2489      iHiwtr = iCur = -1;
2490      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2491                        &iCur, &iHiwtr, bReset);
2492      raw_printf(pArg->out,
2493              "Lookaside Slots Used:                %d (max %d)\n",
2494              iCur, iHiwtr);
2495      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2496                        &iCur, &iHiwtr, bReset);
2497      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2498              iHiwtr);
2499      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2500                        &iCur, &iHiwtr, bReset);
2501      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2502              iHiwtr);
2503      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2504                        &iCur, &iHiwtr, bReset);
2505      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2506              iHiwtr);
2507    }
2508    iHiwtr = iCur = -1;
2509    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2510    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2511            iCur);
2512    iHiwtr = iCur = -1;
2513    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2514    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2515    iHiwtr = iCur = -1;
2516    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2517    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2518    iHiwtr = iCur = -1;
2519    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2520    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2521    iHiwtr = iCur = -1;
2522    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2523    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2524    iHiwtr = iCur = -1;
2525    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2526    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2527            iCur);
2528    iHiwtr = iCur = -1;
2529    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2530    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2531            iCur);
2532  }
2533
2534  if( pArg->pStmt ){
2535    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2536                               bReset);
2537    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2538    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2539    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2540    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2541    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2542    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2543    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2544    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2545    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2546    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2547    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2548    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2549    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2550  }
2551
2552#ifdef __linux__
2553  displayLinuxIoStats(pArg->out);
2554#endif
2555
2556  /* Do not remove this machine readable comment: extra-stats-output-here */
2557
2558  return 0;
2559}
2560
2561/*
2562** Display scan stats.
2563*/
2564static void display_scanstats(
2565  sqlite3 *db,                    /* Database to query */
2566  ShellState *pArg                /* Pointer to ShellState */
2567){
2568#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2569  UNUSED_PARAMETER(db);
2570  UNUSED_PARAMETER(pArg);
2571#else
2572  int i, k, n, mx;
2573  raw_printf(pArg->out, "-------- scanstats --------\n");
2574  mx = 0;
2575  for(k=0; k<=mx; k++){
2576    double rEstLoop = 1.0;
2577    for(i=n=0; 1; i++){
2578      sqlite3_stmt *p = pArg->pStmt;
2579      sqlite3_int64 nLoop, nVisit;
2580      double rEst;
2581      int iSid;
2582      const char *zExplain;
2583      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2584        break;
2585      }
2586      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2587      if( iSid>mx ) mx = iSid;
2588      if( iSid!=k ) continue;
2589      if( n==0 ){
2590        rEstLoop = (double)nLoop;
2591        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2592      }
2593      n++;
2594      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2595      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2596      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2597      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2598      rEstLoop *= rEst;
2599      raw_printf(pArg->out,
2600          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2601          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2602      );
2603    }
2604  }
2605  raw_printf(pArg->out, "---------------------------\n");
2606#endif
2607}
2608
2609/*
2610** Parameter azArray points to a zero-terminated array of strings. zStr
2611** points to a single nul-terminated string. Return non-zero if zStr
2612** is equal, according to strcmp(), to any of the strings in the array.
2613** Otherwise, return zero.
2614*/
2615static int str_in_array(const char *zStr, const char **azArray){
2616  int i;
2617  for(i=0; azArray[i]; i++){
2618    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2619  }
2620  return 0;
2621}
2622
2623/*
2624** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2625** and populate the ShellState.aiIndent[] array with the number of
2626** spaces each opcode should be indented before it is output.
2627**
2628** The indenting rules are:
2629**
2630**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2631**       all opcodes that occur between the p2 jump destination and the opcode
2632**       itself by 2 spaces.
2633**
2634**     * For each "Goto", if the jump destination is earlier in the program
2635**       and ends on one of:
2636**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2637**       or if the P1 parameter is one instead of zero,
2638**       then indent all opcodes between the earlier instruction
2639**       and "Goto" by 2 spaces.
2640*/
2641static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2642  const char *zSql;               /* The text of the SQL statement */
2643  const char *z;                  /* Used to check if this is an EXPLAIN */
2644  int *abYield = 0;               /* True if op is an OP_Yield */
2645  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2646  int iOp;                        /* Index of operation in p->aiIndent[] */
2647
2648  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2649  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2650                            "Rewind", 0 };
2651  const char *azGoto[] = { "Goto", 0 };
2652
2653  /* Try to figure out if this is really an EXPLAIN statement. If this
2654  ** cannot be verified, return early.  */
2655  if( sqlite3_column_count(pSql)!=8 ){
2656    p->cMode = p->mode;
2657    return;
2658  }
2659  zSql = sqlite3_sql(pSql);
2660  if( zSql==0 ) return;
2661  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2662  if( sqlite3_strnicmp(z, "explain", 7) ){
2663    p->cMode = p->mode;
2664    return;
2665  }
2666
2667  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2668    int i;
2669    int iAddr = sqlite3_column_int(pSql, 0);
2670    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2671
2672    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2673    ** p2 is an instruction address, set variable p2op to the index of that
2674    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2675    ** the current instruction is part of a sub-program generated by an
2676    ** SQL trigger or foreign key.  */
2677    int p2 = sqlite3_column_int(pSql, 3);
2678    int p2op = (p2 + (iOp-iAddr));
2679
2680    /* Grow the p->aiIndent array as required */
2681    if( iOp>=nAlloc ){
2682      if( iOp==0 ){
2683        /* Do further verfication that this is explain output.  Abort if
2684        ** it is not */
2685        static const char *explainCols[] = {
2686           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2687        int jj;
2688        for(jj=0; jj<ArraySize(explainCols); jj++){
2689          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2690            p->cMode = p->mode;
2691            sqlite3_reset(pSql);
2692            return;
2693          }
2694        }
2695      }
2696      nAlloc += 100;
2697      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2698      if( p->aiIndent==0 ) shell_out_of_memory();
2699      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2700      if( abYield==0 ) shell_out_of_memory();
2701    }
2702    abYield[iOp] = str_in_array(zOp, azYield);
2703    p->aiIndent[iOp] = 0;
2704    p->nIndent = iOp+1;
2705
2706    if( str_in_array(zOp, azNext) ){
2707      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2708    }
2709    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2710     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2711    ){
2712      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2713    }
2714  }
2715
2716  p->iIndent = 0;
2717  sqlite3_free(abYield);
2718  sqlite3_reset(pSql);
2719}
2720
2721/*
2722** Free the array allocated by explain_data_prepare().
2723*/
2724static void explain_data_delete(ShellState *p){
2725  sqlite3_free(p->aiIndent);
2726  p->aiIndent = 0;
2727  p->nIndent = 0;
2728  p->iIndent = 0;
2729}
2730
2731/*
2732** Disable and restore .wheretrace and .selecttrace settings.
2733*/
2734#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2735extern int sqlite3SelectTrace;
2736static int savedSelectTrace;
2737#endif
2738#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2739extern int sqlite3WhereTrace;
2740static int savedWhereTrace;
2741#endif
2742static void disable_debug_trace_modes(void){
2743#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2744  savedSelectTrace = sqlite3SelectTrace;
2745  sqlite3SelectTrace = 0;
2746#endif
2747#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2748  savedWhereTrace = sqlite3WhereTrace;
2749  sqlite3WhereTrace = 0;
2750#endif
2751}
2752static void restore_debug_trace_modes(void){
2753#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2754  sqlite3SelectTrace = savedSelectTrace;
2755#endif
2756#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2757  sqlite3WhereTrace = savedWhereTrace;
2758#endif
2759}
2760
2761/* Create the TEMP table used to store parameter bindings */
2762static void bind_table_init(ShellState *p){
2763  int wrSchema = 0;
2764  int defensiveMode = 0;
2765  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2766  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2767  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2768  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2769  sqlite3_exec(p->db,
2770    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2771    "  key TEXT PRIMARY KEY,\n"
2772    "  value ANY\n"
2773    ") WITHOUT ROWID;",
2774    0, 0, 0);
2775  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2776  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2777}
2778
2779/*
2780** Bind parameters on a prepared statement.
2781**
2782** Parameter bindings are taken from a TEMP table of the form:
2783**
2784**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2785**    WITHOUT ROWID;
2786**
2787** No bindings occur if this table does not exist.  The special character '$'
2788** is included in the table name to help prevent collisions with actual tables.
2789** The table must be in the TEMP schema.
2790*/
2791static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2792  int nVar;
2793  int i;
2794  int rc;
2795  sqlite3_stmt *pQ = 0;
2796
2797  nVar = sqlite3_bind_parameter_count(pStmt);
2798  if( nVar==0 ) return;  /* Nothing to do */
2799  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2800                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2801    return; /* Parameter table does not exist */
2802  }
2803  rc = sqlite3_prepare_v2(pArg->db,
2804          "SELECT value FROM temp.sqlite_parameters"
2805          " WHERE key=?1", -1, &pQ, 0);
2806  if( rc || pQ==0 ) return;
2807  for(i=1; i<=nVar; i++){
2808    char zNum[30];
2809    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2810    if( zVar==0 ){
2811      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2812      zVar = zNum;
2813    }
2814    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2815    if( sqlite3_step(pQ)==SQLITE_ROW ){
2816      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2817    }else{
2818      sqlite3_bind_null(pStmt, i);
2819    }
2820    sqlite3_reset(pQ);
2821  }
2822  sqlite3_finalize(pQ);
2823}
2824
2825/*
2826** Run a prepared statement
2827*/
2828static void exec_prepared_stmt(
2829  ShellState *pArg,                                /* Pointer to ShellState */
2830  sqlite3_stmt *pStmt                              /* Statment to run */
2831){
2832  int rc;
2833
2834  /* perform the first step.  this will tell us if we
2835  ** have a result set or not and how wide it is.
2836  */
2837  rc = sqlite3_step(pStmt);
2838  /* if we have a result set... */
2839  if( SQLITE_ROW == rc ){
2840    /* allocate space for col name ptr, value ptr, and type */
2841    int nCol = sqlite3_column_count(pStmt);
2842    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
2843    if( !pData ){
2844      rc = SQLITE_NOMEM;
2845    }else{
2846      char **azCols = (char **)pData;      /* Names of result columns */
2847      char **azVals = &azCols[nCol];       /* Results */
2848      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
2849      int i, x;
2850      assert(sizeof(int) <= sizeof(char *));
2851      /* save off ptrs to column names */
2852      for(i=0; i<nCol; i++){
2853        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
2854      }
2855      do{
2856        /* extract the data and data types */
2857        for(i=0; i<nCol; i++){
2858          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
2859          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
2860            azVals[i] = "";
2861          }else{
2862            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
2863          }
2864          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
2865            rc = SQLITE_NOMEM;
2866            break; /* from for */
2867          }
2868        } /* end for */
2869
2870        /* if data and types extracted successfully... */
2871        if( SQLITE_ROW == rc ){
2872          /* call the supplied callback with the result row data */
2873          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
2874            rc = SQLITE_ABORT;
2875          }else{
2876            rc = sqlite3_step(pStmt);
2877          }
2878        }
2879      } while( SQLITE_ROW == rc );
2880      sqlite3_free(pData);
2881    }
2882  }
2883}
2884
2885#ifndef SQLITE_OMIT_VIRTUALTABLE
2886/*
2887** This function is called to process SQL if the previous shell command
2888** was ".expert". It passes the SQL in the second argument directly to
2889** the sqlite3expert object.
2890**
2891** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2892** code. In this case, (*pzErr) may be set to point to a buffer containing
2893** an English language error message. It is the responsibility of the
2894** caller to eventually free this buffer using sqlite3_free().
2895*/
2896static int expertHandleSQL(
2897  ShellState *pState,
2898  const char *zSql,
2899  char **pzErr
2900){
2901  assert( pState->expert.pExpert );
2902  assert( pzErr==0 || *pzErr==0 );
2903  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
2904}
2905
2906/*
2907** This function is called either to silently clean up the object
2908** created by the ".expert" command (if bCancel==1), or to generate a
2909** report from it and then clean it up (if bCancel==0).
2910**
2911** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2912** code. In this case, (*pzErr) may be set to point to a buffer containing
2913** an English language error message. It is the responsibility of the
2914** caller to eventually free this buffer using sqlite3_free().
2915*/
2916static int expertFinish(
2917  ShellState *pState,
2918  int bCancel,
2919  char **pzErr
2920){
2921  int rc = SQLITE_OK;
2922  sqlite3expert *p = pState->expert.pExpert;
2923  assert( p );
2924  assert( bCancel || pzErr==0 || *pzErr==0 );
2925  if( bCancel==0 ){
2926    FILE *out = pState->out;
2927    int bVerbose = pState->expert.bVerbose;
2928
2929    rc = sqlite3_expert_analyze(p, pzErr);
2930    if( rc==SQLITE_OK ){
2931      int nQuery = sqlite3_expert_count(p);
2932      int i;
2933
2934      if( bVerbose ){
2935        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
2936        raw_printf(out, "-- Candidates -----------------------------\n");
2937        raw_printf(out, "%s\n", zCand);
2938      }
2939      for(i=0; i<nQuery; i++){
2940        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
2941        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
2942        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
2943        if( zIdx==0 ) zIdx = "(no new indexes)\n";
2944        if( bVerbose ){
2945          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
2946          raw_printf(out, "%s\n\n", zSql);
2947        }
2948        raw_printf(out, "%s\n", zIdx);
2949        raw_printf(out, "%s\n", zEQP);
2950      }
2951    }
2952  }
2953  sqlite3_expert_destroy(p);
2954  pState->expert.pExpert = 0;
2955  return rc;
2956}
2957
2958/*
2959** Implementation of ".expert" dot command.
2960*/
2961static int expertDotCommand(
2962  ShellState *pState,             /* Current shell tool state */
2963  char **azArg,                   /* Array of arguments passed to dot command */
2964  int nArg                        /* Number of entries in azArg[] */
2965){
2966  int rc = SQLITE_OK;
2967  char *zErr = 0;
2968  int i;
2969  int iSample = 0;
2970
2971  assert( pState->expert.pExpert==0 );
2972  memset(&pState->expert, 0, sizeof(ExpertInfo));
2973
2974  for(i=1; rc==SQLITE_OK && i<nArg; i++){
2975    char *z = azArg[i];
2976    int n;
2977    if( z[0]=='-' && z[1]=='-' ) z++;
2978    n = strlen30(z);
2979    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
2980      pState->expert.bVerbose = 1;
2981    }
2982    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
2983      if( i==(nArg-1) ){
2984        raw_printf(stderr, "option requires an argument: %s\n", z);
2985        rc = SQLITE_ERROR;
2986      }else{
2987        iSample = (int)integerValue(azArg[++i]);
2988        if( iSample<0 || iSample>100 ){
2989          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
2990          rc = SQLITE_ERROR;
2991        }
2992      }
2993    }
2994    else{
2995      raw_printf(stderr, "unknown option: %s\n", z);
2996      rc = SQLITE_ERROR;
2997    }
2998  }
2999
3000  if( rc==SQLITE_OK ){
3001    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3002    if( pState->expert.pExpert==0 ){
3003      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3004      rc = SQLITE_ERROR;
3005    }else{
3006      sqlite3_expert_config(
3007          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3008      );
3009    }
3010  }
3011
3012  return rc;
3013}
3014#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3015
3016/*
3017** Execute a statement or set of statements.  Print
3018** any result rows/columns depending on the current mode
3019** set via the supplied callback.
3020**
3021** This is very similar to SQLite's built-in sqlite3_exec()
3022** function except it takes a slightly different callback
3023** and callback data argument.
3024*/
3025static int shell_exec(
3026  ShellState *pArg,                         /* Pointer to ShellState */
3027  const char *zSql,                         /* SQL to be evaluated */
3028  char **pzErrMsg                           /* Error msg written here */
3029){
3030  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3031  int rc = SQLITE_OK;             /* Return Code */
3032  int rc2;
3033  const char *zLeftover;          /* Tail of unprocessed SQL */
3034  sqlite3 *db = pArg->db;
3035
3036  if( pzErrMsg ){
3037    *pzErrMsg = NULL;
3038  }
3039
3040#ifndef SQLITE_OMIT_VIRTUALTABLE
3041  if( pArg->expert.pExpert ){
3042    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3043    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3044  }
3045#endif
3046
3047  while( zSql[0] && (SQLITE_OK == rc) ){
3048    static const char *zStmtSql;
3049    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3050    if( SQLITE_OK != rc ){
3051      if( pzErrMsg ){
3052        *pzErrMsg = save_err_msg(db);
3053      }
3054    }else{
3055      if( !pStmt ){
3056        /* this happens for a comment or white-space */
3057        zSql = zLeftover;
3058        while( IsSpace(zSql[0]) ) zSql++;
3059        continue;
3060      }
3061      zStmtSql = sqlite3_sql(pStmt);
3062      if( zStmtSql==0 ) zStmtSql = "";
3063      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3064
3065      /* save off the prepared statment handle and reset row count */
3066      if( pArg ){
3067        pArg->pStmt = pStmt;
3068        pArg->cnt = 0;
3069      }
3070
3071      /* echo the sql statement if echo on */
3072      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3073        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3074      }
3075
3076      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3077      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3078        sqlite3_stmt *pExplain;
3079        char *zEQP;
3080        int triggerEQP = 0;
3081        disable_debug_trace_modes();
3082        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3083        if( pArg->autoEQP>=AUTOEQP_trigger ){
3084          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3085        }
3086        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3087        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3088        if( rc==SQLITE_OK ){
3089          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3090            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3091            int iEqpId = sqlite3_column_int(pExplain, 0);
3092            int iParentId = sqlite3_column_int(pExplain, 1);
3093            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3094            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3095          }
3096          eqp_render(pArg);
3097        }
3098        sqlite3_finalize(pExplain);
3099        sqlite3_free(zEQP);
3100        if( pArg->autoEQP>=AUTOEQP_full ){
3101          /* Also do an EXPLAIN for ".eqp full" mode */
3102          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3103          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3104          if( rc==SQLITE_OK ){
3105            pArg->cMode = MODE_Explain;
3106            explain_data_prepare(pArg, pExplain);
3107            exec_prepared_stmt(pArg, pExplain);
3108            explain_data_delete(pArg);
3109          }
3110          sqlite3_finalize(pExplain);
3111          sqlite3_free(zEQP);
3112        }
3113        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3114          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3115          /* Reprepare pStmt before reactiving trace modes */
3116          sqlite3_finalize(pStmt);
3117          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3118          if( pArg ) pArg->pStmt = pStmt;
3119        }
3120        restore_debug_trace_modes();
3121      }
3122
3123      if( pArg ){
3124        pArg->cMode = pArg->mode;
3125        if( pArg->autoExplain ){
3126          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3127            pArg->cMode = MODE_Explain;
3128          }
3129          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3130            pArg->cMode = MODE_EQP;
3131          }
3132        }
3133
3134        /* If the shell is currently in ".explain" mode, gather the extra
3135        ** data required to add indents to the output.*/
3136        if( pArg->cMode==MODE_Explain ){
3137          explain_data_prepare(pArg, pStmt);
3138        }
3139      }
3140
3141      bind_prepared_stmt(pArg, pStmt);
3142      exec_prepared_stmt(pArg, pStmt);
3143      explain_data_delete(pArg);
3144      eqp_render(pArg);
3145
3146      /* print usage stats if stats on */
3147      if( pArg && pArg->statsOn ){
3148        display_stats(db, pArg, 0);
3149      }
3150
3151      /* print loop-counters if required */
3152      if( pArg && pArg->scanstatsOn ){
3153        display_scanstats(db, pArg);
3154      }
3155
3156      /* Finalize the statement just executed. If this fails, save a
3157      ** copy of the error message. Otherwise, set zSql to point to the
3158      ** next statement to execute. */
3159      rc2 = sqlite3_finalize(pStmt);
3160      if( rc!=SQLITE_NOMEM ) rc = rc2;
3161      if( rc==SQLITE_OK ){
3162        zSql = zLeftover;
3163        while( IsSpace(zSql[0]) ) zSql++;
3164      }else if( pzErrMsg ){
3165        *pzErrMsg = save_err_msg(db);
3166      }
3167
3168      /* clear saved stmt handle */
3169      if( pArg ){
3170        pArg->pStmt = NULL;
3171      }
3172    }
3173  } /* end while */
3174
3175  return rc;
3176}
3177
3178/*
3179** Release memory previously allocated by tableColumnList().
3180*/
3181static void freeColumnList(char **azCol){
3182  int i;
3183  for(i=1; azCol[i]; i++){
3184    sqlite3_free(azCol[i]);
3185  }
3186  /* azCol[0] is a static string */
3187  sqlite3_free(azCol);
3188}
3189
3190/*
3191** Return a list of pointers to strings which are the names of all
3192** columns in table zTab.   The memory to hold the names is dynamically
3193** allocated and must be released by the caller using a subsequent call
3194** to freeColumnList().
3195**
3196** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3197** value that needs to be preserved, then azCol[0] is filled in with the
3198** name of the rowid column.
3199**
3200** The first regular column in the table is azCol[1].  The list is terminated
3201** by an entry with azCol[i]==0.
3202*/
3203static char **tableColumnList(ShellState *p, const char *zTab){
3204  char **azCol = 0;
3205  sqlite3_stmt *pStmt;
3206  char *zSql;
3207  int nCol = 0;
3208  int nAlloc = 0;
3209  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3210  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3211  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3212  int rc;
3213
3214  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3215  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3216  sqlite3_free(zSql);
3217  if( rc ) return 0;
3218  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3219    if( nCol>=nAlloc-2 ){
3220      nAlloc = nAlloc*2 + nCol + 10;
3221      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3222      if( azCol==0 ) shell_out_of_memory();
3223    }
3224    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3225    if( sqlite3_column_int(pStmt, 5) ){
3226      nPK++;
3227      if( nPK==1
3228       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3229                          "INTEGER")==0
3230      ){
3231        isIPK = 1;
3232      }else{
3233        isIPK = 0;
3234      }
3235    }
3236  }
3237  sqlite3_finalize(pStmt);
3238  if( azCol==0 ) return 0;
3239  azCol[0] = 0;
3240  azCol[nCol+1] = 0;
3241
3242  /* The decision of whether or not a rowid really needs to be preserved
3243  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3244  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3245  ** rowids on tables where the rowid is inaccessible because there are other
3246  ** columns in the table named "rowid", "_rowid_", and "oid".
3247  */
3248  if( preserveRowid && isIPK ){
3249    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3250    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3251    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3252    ** ROWID aliases.  To distinguish these cases, check to see if
3253    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3254    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3255    */
3256    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3257                           " WHERE origin='pk'", zTab);
3258    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3259    sqlite3_free(zSql);
3260    if( rc ){
3261      freeColumnList(azCol);
3262      return 0;
3263    }
3264    rc = sqlite3_step(pStmt);
3265    sqlite3_finalize(pStmt);
3266    preserveRowid = rc==SQLITE_ROW;
3267  }
3268  if( preserveRowid ){
3269    /* Only preserve the rowid if we can find a name to use for the
3270    ** rowid */
3271    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3272    int i, j;
3273    for(j=0; j<3; j++){
3274      for(i=1; i<=nCol; i++){
3275        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3276      }
3277      if( i>nCol ){
3278        /* At this point, we know that azRowid[j] is not the name of any
3279        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3280        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3281        ** tables will fail this last check */
3282        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3283        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3284        break;
3285      }
3286    }
3287  }
3288  return azCol;
3289}
3290
3291/*
3292** Toggle the reverse_unordered_selects setting.
3293*/
3294static void toggleSelectOrder(sqlite3 *db){
3295  sqlite3_stmt *pStmt = 0;
3296  int iSetting = 0;
3297  char zStmt[100];
3298  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3299  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3300    iSetting = sqlite3_column_int(pStmt, 0);
3301  }
3302  sqlite3_finalize(pStmt);
3303  sqlite3_snprintf(sizeof(zStmt), zStmt,
3304       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3305  sqlite3_exec(db, zStmt, 0, 0, 0);
3306}
3307
3308/*
3309** This is a different callback routine used for dumping the database.
3310** Each row received by this callback consists of a table name,
3311** the table type ("index" or "table") and SQL to create the table.
3312** This routine should print text sufficient to recreate the table.
3313*/
3314static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3315  int rc;
3316  const char *zTable;
3317  const char *zType;
3318  const char *zSql;
3319  ShellState *p = (ShellState *)pArg;
3320
3321  UNUSED_PARAMETER(azNotUsed);
3322  if( nArg!=3 || azArg==0 ) return 0;
3323  zTable = azArg[0];
3324  zType = azArg[1];
3325  zSql = azArg[2];
3326
3327  if( strcmp(zTable, "sqlite_sequence")==0 ){
3328    raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3329  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
3330    raw_printf(p->out, "ANALYZE sqlite_master;\n");
3331  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3332    return 0;
3333  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3334    char *zIns;
3335    if( !p->writableSchema ){
3336      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3337      p->writableSchema = 1;
3338    }
3339    zIns = sqlite3_mprintf(
3340       "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
3341       "VALUES('table','%q','%q',0,'%q');",
3342       zTable, zTable, zSql);
3343    utf8_printf(p->out, "%s\n", zIns);
3344    sqlite3_free(zIns);
3345    return 0;
3346  }else{
3347    printSchemaLine(p->out, zSql, ";\n");
3348  }
3349
3350  if( strcmp(zType, "table")==0 ){
3351    ShellText sSelect;
3352    ShellText sTable;
3353    char **azCol;
3354    int i;
3355    char *savedDestTable;
3356    int savedMode;
3357
3358    azCol = tableColumnList(p, zTable);
3359    if( azCol==0 ){
3360      p->nErr++;
3361      return 0;
3362    }
3363
3364    /* Always quote the table name, even if it appears to be pure ascii,
3365    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3366    initText(&sTable);
3367    appendText(&sTable, zTable, quoteChar(zTable));
3368    /* If preserving the rowid, add a column list after the table name.
3369    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3370    ** instead of the usual "INSERT INTO tab VALUES(...)".
3371    */
3372    if( azCol[0] ){
3373      appendText(&sTable, "(", 0);
3374      appendText(&sTable, azCol[0], 0);
3375      for(i=1; azCol[i]; i++){
3376        appendText(&sTable, ",", 0);
3377        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3378      }
3379      appendText(&sTable, ")", 0);
3380    }
3381
3382    /* Build an appropriate SELECT statement */
3383    initText(&sSelect);
3384    appendText(&sSelect, "SELECT ", 0);
3385    if( azCol[0] ){
3386      appendText(&sSelect, azCol[0], 0);
3387      appendText(&sSelect, ",", 0);
3388    }
3389    for(i=1; azCol[i]; i++){
3390      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3391      if( azCol[i+1] ){
3392        appendText(&sSelect, ",", 0);
3393      }
3394    }
3395    freeColumnList(azCol);
3396    appendText(&sSelect, " FROM ", 0);
3397    appendText(&sSelect, zTable, quoteChar(zTable));
3398
3399    savedDestTable = p->zDestTable;
3400    savedMode = p->mode;
3401    p->zDestTable = sTable.z;
3402    p->mode = p->cMode = MODE_Insert;
3403    rc = shell_exec(p, sSelect.z, 0);
3404    if( (rc&0xff)==SQLITE_CORRUPT ){
3405      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3406      toggleSelectOrder(p->db);
3407      shell_exec(p, sSelect.z, 0);
3408      toggleSelectOrder(p->db);
3409    }
3410    p->zDestTable = savedDestTable;
3411    p->mode = savedMode;
3412    freeText(&sTable);
3413    freeText(&sSelect);
3414    if( rc ) p->nErr++;
3415  }
3416  return 0;
3417}
3418
3419/*
3420** Run zQuery.  Use dump_callback() as the callback routine so that
3421** the contents of the query are output as SQL statements.
3422**
3423** If we get a SQLITE_CORRUPT error, rerun the query after appending
3424** "ORDER BY rowid DESC" to the end.
3425*/
3426static int run_schema_dump_query(
3427  ShellState *p,
3428  const char *zQuery
3429){
3430  int rc;
3431  char *zErr = 0;
3432  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3433  if( rc==SQLITE_CORRUPT ){
3434    char *zQ2;
3435    int len = strlen30(zQuery);
3436    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3437    if( zErr ){
3438      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3439      sqlite3_free(zErr);
3440      zErr = 0;
3441    }
3442    zQ2 = malloc( len+100 );
3443    if( zQ2==0 ) return rc;
3444    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3445    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3446    if( rc ){
3447      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3448    }else{
3449      rc = SQLITE_CORRUPT;
3450    }
3451    sqlite3_free(zErr);
3452    free(zQ2);
3453  }
3454  return rc;
3455}
3456
3457/*
3458** Text of help messages.
3459**
3460** The help text for each individual command begins with a line that starts
3461** with ".".  Subsequent lines are supplimental information.
3462**
3463** There must be two or more spaces between the end of the command and the
3464** start of the description of what that command does.
3465*/
3466static const char *(azHelp[]) = {
3467#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3468  ".archive ...             Manage SQL archives",
3469  "   Each command must have exactly one of the following options:",
3470  "     -c, --create               Create a new archive",
3471  "     -u, --update               Add or update files with changed mtime",
3472  "     -i, --insert               Like -u but always add even if unchanged",
3473  "     -t, --list                 List contents of archive",
3474  "     -x, --extract              Extract files from archive",
3475  "   Optional arguments:",
3476  "     -v, --verbose              Print each filename as it is processed",
3477  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3478  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3479  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3480  "     -n, --dryrun               Show the SQL that would have occurred",
3481  "   Examples:",
3482  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3483  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3484  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3485  "   See also:",
3486  "      http://sqlite.org/cli.html#sqlar_archive_support",
3487#endif
3488#ifndef SQLITE_OMIT_AUTHORIZATION
3489  ".auth ON|OFF             Show authorizer callbacks",
3490#endif
3491  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3492  "       --append            Use the appendvfs",
3493  "       --async             Write to FILE without journal and fsync()",
3494  ".bail on|off             Stop after hitting an error.  Default OFF",
3495  ".binary on|off           Turn binary output on or off.  Default OFF",
3496  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3497  ".changes on|off          Show number of rows changed by SQL",
3498  ".check GLOB              Fail if output since .testcase does not match",
3499  ".clone NEWDB             Clone data into NEWDB from the existing database",
3500  ".databases               List names and files of attached databases",
3501  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3502  ".dbinfo ?DB?             Show status information about the database",
3503  ".dump ?TABLE? ...        Render all database content as SQL",
3504  "   Options:",
3505  "     --preserve-rowids      Include ROWID values in the output",
3506  "     --newlines             Allow unescaped newline characters in output",
3507  "   TABLE is a LIKE pattern for the tables to dump",
3508  ".echo on|off             Turn command echo on or off",
3509  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3510  "   Other Modes:",
3511#ifdef SQLITE_DEBUG
3512  "      test                  Show raw EXPLAIN QUERY PLAN output",
3513  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3514#endif
3515  "      trigger               Like \"full\" but also show trigger bytecode",
3516  ".excel                   Display the output of next command in spreadsheet",
3517  ".exit ?CODE?             Exit this program with return-code CODE",
3518  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3519  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3520  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3521  "                           Run \".filectrl\" with no arguments for details",
3522  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3523  ".headers on|off          Turn display of headers on or off",
3524  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3525  ".import FILE TABLE       Import data from FILE into TABLE",
3526#ifndef SQLITE_OMIT_TEST_CONTROL
3527  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3528#endif
3529  ".indexes ?TABLE?         Show names of indexes",
3530  "                           If TABLE is specified, only show indexes for",
3531  "                           tables matching TABLE using the LIKE operator.",
3532#ifdef SQLITE_ENABLE_IOTRACE
3533  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3534#endif
3535  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3536  ".lint OPTIONS            Report potential schema issues.",
3537  "     Options:",
3538  "        fkey-indexes     Find missing foreign key indexes",
3539#ifndef SQLITE_OMIT_LOAD_EXTENSION
3540  ".load FILE ?ENTRY?       Load an extension library",
3541#endif
3542  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3543  ".mode MODE ?TABLE?       Set output mode",
3544  "   MODE is one of:",
3545  "     ascii    Columns/rows delimited by 0x1F and 0x1E",
3546  "     csv      Comma-separated values",
3547  "     column   Left-aligned columns.  (See .width)",
3548  "     html     HTML <table> code",
3549  "     insert   SQL insert statements for TABLE",
3550  "     line     One value per line",
3551  "     list     Values delimited by \"|\"",
3552  "     quote    Escape answers as for SQL",
3553  "     tabs     Tab-separated values",
3554  "     tcl      TCL list elements",
3555  ".nullvalue STRING        Use STRING in place of NULL values",
3556  ".once (-e|-x|FILE)       Output for the next SQL command only to FILE",
3557  "     If FILE begins with '|' then open as a pipe",
3558  "     Other options:",
3559  "       -e    Invoke system text editor",
3560  "       -x    Open in a spreadsheet",
3561  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3562  "     Options:",
3563  "        --append        Use appendvfs to append database to the end of FILE",
3564#ifdef SQLITE_ENABLE_DESERIALIZE
3565  "        --deserialize   Load into memory useing sqlite3_deserialize()",
3566  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3567  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3568#endif
3569  "        --new           Initialize FILE to an empty database",
3570  "        --nofollow      Do not follow symbolic links",
3571  "        --readonly      Open FILE readonly",
3572  "        --zip           FILE is a ZIP archive",
3573  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
3574  "     If FILE begins with '|' then open it as a pipe.",
3575  ".parameter CMD ...       Manage SQL parameter bindings",
3576  "   clear                   Erase all bindings",
3577  "   init                    Initialize the TEMP table that holds bindings",
3578  "   list                    List the current parameter bindings",
3579  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
3580  "                           PARAMETER should start with one of: $ : @ ?",
3581  "   unset PARAMETER         Remove PARAMETER from the binding table",
3582  ".print STRING...         Print literal STRING",
3583#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
3584  ".progress N              Invoke progress handler after every N opcodes",
3585  "   --limit N                 Interrupt after N progress callbacks",
3586  "   --once                    Do no more than one progress interrupt",
3587  "   --quiet|-q                No output except at interrupts",
3588  "   --reset                   Reset the count for each input and interrupt",
3589#endif
3590  ".prompt MAIN CONTINUE    Replace the standard prompts",
3591  ".quit                    Exit this program",
3592  ".read FILE               Read input from FILE",
3593#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
3594  ".recover                 Recover as much data as possible from corrupt db.",
3595  "   --freelist-corrupt       Assume the freelist is corrupt",
3596  "   --recovery-db NAME       Store recovery metadata in database file NAME",
3597  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
3598  "   --no-rowids              Do not attempt to recover rowid values",
3599  "                            that are not also INTEGER PRIMARY KEYs",
3600#endif
3601  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
3602  ".save FILE               Write in-memory database into FILE",
3603  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
3604  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
3605  "     Options:",
3606  "         --indent            Try to pretty-print the schema",
3607  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
3608  "    Options:",
3609  "       --init               Create a new SELFTEST table",
3610  "       -v                   Verbose output",
3611  ".separator COL ?ROW?     Change the column and row separators",
3612#if defined(SQLITE_ENABLE_SESSION)
3613  ".session ?NAME? CMD ...  Create or control sessions",
3614  "   Subcommands:",
3615  "     attach TABLE             Attach TABLE",
3616  "     changeset FILE           Write a changeset into FILE",
3617  "     close                    Close one session",
3618  "     enable ?BOOLEAN?         Set or query the enable bit",
3619  "     filter GLOB...           Reject tables matching GLOBs",
3620  "     indirect ?BOOLEAN?       Mark or query the indirect status",
3621  "     isempty                  Query whether the session is empty",
3622  "     list                     List currently open session names",
3623  "     open DB NAME             Open a new session on DB",
3624  "     patchset FILE            Write a patchset into FILE",
3625  "   If ?NAME? is omitted, the first defined session is used.",
3626#endif
3627  ".sha3sum ...             Compute a SHA3 hash of database content",
3628  "    Options:",
3629  "      --schema              Also hash the sqlite_master table",
3630  "      --sha3-224            Use the sha3-224 algorithm",
3631  "      --sha3-256            Use the sha3-256 algorithm (default)",
3632  "      --sha3-384            Use the sha3-384 algorithm",
3633  "      --sha3-512            Use the sha3-512 algorithm",
3634  "    Any other argument is a LIKE pattern for tables to hash",
3635#ifndef SQLITE_NOHAVE_SYSTEM
3636  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
3637#endif
3638  ".show                    Show the current values for various settings",
3639  ".stats ?on|off?          Show stats or turn stats on or off",
3640#ifndef SQLITE_NOHAVE_SYSTEM
3641  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
3642#endif
3643  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
3644  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
3645  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
3646  "                           Run \".testctrl\" with no arguments for details",
3647  ".timeout MS              Try opening locked tables for MS milliseconds",
3648  ".timer on|off            Turn SQL timer on or off",
3649#ifndef SQLITE_OMIT_TRACE
3650  ".trace ?OPTIONS?         Output each SQL statement as it is run",
3651  "    FILE                    Send output to FILE",
3652  "    stdout                  Send output to stdout",
3653  "    stderr                  Send output to stderr",
3654  "    off                     Disable tracing",
3655  "    --expanded              Expand query parameters",
3656#ifdef SQLITE_ENABLE_NORMALIZE
3657  "    --normalized            Normal the SQL statements",
3658#endif
3659  "    --plain                 Show SQL as it is input",
3660  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
3661  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
3662  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
3663  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
3664#endif /* SQLITE_OMIT_TRACE */
3665#ifdef SQLITE_DEBUG
3666  ".unmodule NAME ...       Unregister virtual table modules",
3667  "    --allexcept             Unregister everything except those named",
3668#endif
3669  ".vfsinfo ?AUX?           Information about the top-level VFS",
3670  ".vfslist                 List all available VFSes",
3671  ".vfsname ?AUX?           Print the name of the VFS stack",
3672  ".width NUM1 NUM2 ...     Set column widths for \"column\" mode",
3673  "     Negative values right-justify",
3674};
3675
3676/*
3677** Output help text.
3678**
3679** zPattern describes the set of commands for which help text is provided.
3680** If zPattern is NULL, then show all commands, but only give a one-line
3681** description of each.
3682**
3683** Return the number of matches.
3684*/
3685static int showHelp(FILE *out, const char *zPattern){
3686  int i = 0;
3687  int j = 0;
3688  int n = 0;
3689  char *zPat;
3690  if( zPattern==0
3691   || zPattern[0]=='0'
3692   || strcmp(zPattern,"-a")==0
3693   || strcmp(zPattern,"-all")==0
3694  ){
3695    /* Show all commands, but only one line per command */
3696    if( zPattern==0 ) zPattern = "";
3697    for(i=0; i<ArraySize(azHelp); i++){
3698      if( azHelp[i][0]=='.' || zPattern[0] ){
3699        utf8_printf(out, "%s\n", azHelp[i]);
3700        n++;
3701      }
3702    }
3703  }else{
3704    /* Look for commands that for which zPattern is an exact prefix */
3705    zPat = sqlite3_mprintf(".%s*", zPattern);
3706    for(i=0; i<ArraySize(azHelp); i++){
3707      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
3708        utf8_printf(out, "%s\n", azHelp[i]);
3709        j = i+1;
3710        n++;
3711      }
3712    }
3713    sqlite3_free(zPat);
3714    if( n ){
3715      if( n==1 ){
3716        /* when zPattern is a prefix of exactly one command, then include the
3717        ** details of that command, which should begin at offset j */
3718        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
3719          utf8_printf(out, "%s\n", azHelp[j]);
3720          j++;
3721        }
3722      }
3723      return n;
3724    }
3725    /* Look for commands that contain zPattern anywhere.  Show the complete
3726    ** text of all commands that match. */
3727    zPat = sqlite3_mprintf("%%%s%%", zPattern);
3728    for(i=0; i<ArraySize(azHelp); i++){
3729      if( azHelp[i][0]=='.' ) j = i;
3730      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
3731        utf8_printf(out, "%s\n", azHelp[j]);
3732        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
3733          j++;
3734          utf8_printf(out, "%s\n", azHelp[j]);
3735        }
3736        i = j;
3737        n++;
3738      }
3739    }
3740    sqlite3_free(zPat);
3741  }
3742  return n;
3743}
3744
3745/* Forward reference */
3746static int process_input(ShellState *p);
3747
3748/*
3749** Read the content of file zName into memory obtained from sqlite3_malloc64()
3750** and return a pointer to the buffer. The caller is responsible for freeing
3751** the memory.
3752**
3753** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
3754** read.
3755**
3756** For convenience, a nul-terminator byte is always appended to the data read
3757** from the file before the buffer is returned. This byte is not included in
3758** the final value of (*pnByte), if applicable.
3759**
3760** NULL is returned if any error is encountered. The final value of *pnByte
3761** is undefined in this case.
3762*/
3763static char *readFile(const char *zName, int *pnByte){
3764  FILE *in = fopen(zName, "rb");
3765  long nIn;
3766  size_t nRead;
3767  char *pBuf;
3768  if( in==0 ) return 0;
3769  fseek(in, 0, SEEK_END);
3770  nIn = ftell(in);
3771  rewind(in);
3772  pBuf = sqlite3_malloc64( nIn+1 );
3773  if( pBuf==0 ){ fclose(in); return 0; }
3774  nRead = fread(pBuf, nIn, 1, in);
3775  fclose(in);
3776  if( nRead!=1 ){
3777    sqlite3_free(pBuf);
3778    return 0;
3779  }
3780  pBuf[nIn] = 0;
3781  if( pnByte ) *pnByte = nIn;
3782  return pBuf;
3783}
3784
3785#if defined(SQLITE_ENABLE_SESSION)
3786/*
3787** Close a single OpenSession object and release all of its associated
3788** resources.
3789*/
3790static void session_close(OpenSession *pSession){
3791  int i;
3792  sqlite3session_delete(pSession->p);
3793  sqlite3_free(pSession->zName);
3794  for(i=0; i<pSession->nFilter; i++){
3795    sqlite3_free(pSession->azFilter[i]);
3796  }
3797  sqlite3_free(pSession->azFilter);
3798  memset(pSession, 0, sizeof(OpenSession));
3799}
3800#endif
3801
3802/*
3803** Close all OpenSession objects and release all associated resources.
3804*/
3805#if defined(SQLITE_ENABLE_SESSION)
3806static void session_close_all(ShellState *p){
3807  int i;
3808  for(i=0; i<p->nSession; i++){
3809    session_close(&p->aSession[i]);
3810  }
3811  p->nSession = 0;
3812}
3813#else
3814# define session_close_all(X)
3815#endif
3816
3817/*
3818** Implementation of the xFilter function for an open session.  Omit
3819** any tables named by ".session filter" but let all other table through.
3820*/
3821#if defined(SQLITE_ENABLE_SESSION)
3822static int session_filter(void *pCtx, const char *zTab){
3823  OpenSession *pSession = (OpenSession*)pCtx;
3824  int i;
3825  for(i=0; i<pSession->nFilter; i++){
3826    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
3827  }
3828  return 1;
3829}
3830#endif
3831
3832/*
3833** Try to deduce the type of file for zName based on its content.  Return
3834** one of the SHELL_OPEN_* constants.
3835**
3836** If the file does not exist or is empty but its name looks like a ZIP
3837** archive and the dfltZip flag is true, then assume it is a ZIP archive.
3838** Otherwise, assume an ordinary database regardless of the filename if
3839** the type cannot be determined from content.
3840*/
3841int deduceDatabaseType(const char *zName, int dfltZip){
3842  FILE *f = fopen(zName, "rb");
3843  size_t n;
3844  int rc = SHELL_OPEN_UNSPEC;
3845  char zBuf[100];
3846  if( f==0 ){
3847    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
3848       return SHELL_OPEN_ZIPFILE;
3849    }else{
3850       return SHELL_OPEN_NORMAL;
3851    }
3852  }
3853  n = fread(zBuf, 16, 1, f);
3854  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
3855    fclose(f);
3856    return SHELL_OPEN_NORMAL;
3857  }
3858  fseek(f, -25, SEEK_END);
3859  n = fread(zBuf, 25, 1, f);
3860  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
3861    rc = SHELL_OPEN_APPENDVFS;
3862  }else{
3863    fseek(f, -22, SEEK_END);
3864    n = fread(zBuf, 22, 1, f);
3865    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
3866       && zBuf[3]==0x06 ){
3867      rc = SHELL_OPEN_ZIPFILE;
3868    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
3869      rc = SHELL_OPEN_ZIPFILE;
3870    }
3871  }
3872  fclose(f);
3873  return rc;
3874}
3875
3876#ifdef SQLITE_ENABLE_DESERIALIZE
3877/*
3878** Reconstruct an in-memory database using the output from the "dbtotxt"
3879** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
3880** is 0, then read from standard input.
3881*/
3882static unsigned char *readHexDb(ShellState *p, int *pnData){
3883  unsigned char *a = 0;
3884  int nLine;
3885  int n = 0;
3886  int pgsz = 0;
3887  int iOffset = 0;
3888  int j, k;
3889  int rc;
3890  FILE *in;
3891  unsigned int x[16];
3892  char zLine[1000];
3893  if( p->zDbFilename ){
3894    in = fopen(p->zDbFilename, "r");
3895    if( in==0 ){
3896      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
3897      return 0;
3898    }
3899    nLine = 0;
3900  }else{
3901    in = p->in;
3902    nLine = p->lineno;
3903    if( in==0 ) in = stdin;
3904  }
3905  *pnData = 0;
3906  nLine++;
3907  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
3908  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
3909  if( rc!=2 ) goto readHexDb_error;
3910  if( n<0 ) goto readHexDb_error;
3911  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
3912  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
3913  a = sqlite3_malloc( n ? n : 1 );
3914  if( a==0 ){
3915    utf8_printf(stderr, "Out of memory!\n");
3916    goto readHexDb_error;
3917  }
3918  memset(a, 0, n);
3919  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
3920    utf8_printf(stderr, "invalid pagesize\n");
3921    goto readHexDb_error;
3922  }
3923  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
3924    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
3925    if( rc==2 ){
3926      iOffset = k;
3927      continue;
3928    }
3929    if( strncmp(zLine, "| end ", 6)==0 ){
3930      break;
3931    }
3932    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
3933                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
3934                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
3935    if( rc==17 ){
3936      k = iOffset+j;
3937      if( k+16<=n ){
3938        int ii;
3939        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
3940      }
3941    }
3942  }
3943  *pnData = n;
3944  if( in!=p->in ){
3945    fclose(in);
3946  }else{
3947    p->lineno = nLine;
3948  }
3949  return a;
3950
3951readHexDb_error:
3952  if( in!=p->in ){
3953    fclose(in);
3954  }else{
3955    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
3956      nLine++;
3957      if(strncmp(zLine, "| end ", 6)==0 ) break;
3958    }
3959    p->lineno = nLine;
3960  }
3961  sqlite3_free(a);
3962  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
3963  return 0;
3964}
3965#endif /* SQLITE_ENABLE_DESERIALIZE */
3966
3967/*
3968** Scalar function "shell_int32". The first argument to this function
3969** must be a blob. The second a non-negative integer. This function
3970** reads and returns a 32-bit big-endian integer from byte
3971** offset (4*<arg2>) of the blob.
3972*/
3973static void shellInt32(
3974  sqlite3_context *context,
3975  int argc,
3976  sqlite3_value **argv
3977){
3978  const unsigned char *pBlob;
3979  int nBlob;
3980  int iInt;
3981
3982  UNUSED_PARAMETER(argc);
3983  nBlob = sqlite3_value_bytes(argv[0]);
3984  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
3985  iInt = sqlite3_value_int(argv[1]);
3986
3987  if( iInt>=0 && (iInt+1)*4<=nBlob ){
3988    const unsigned char *a = &pBlob[iInt*4];
3989    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
3990                       + ((sqlite3_int64)a[1]<<16)
3991                       + ((sqlite3_int64)a[2]<< 8)
3992                       + ((sqlite3_int64)a[3]<< 0);
3993    sqlite3_result_int64(context, iVal);
3994  }
3995}
3996
3997/*
3998** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
3999** using "..." with internal double-quote characters doubled.
4000*/
4001static void shellIdQuote(
4002  sqlite3_context *context,
4003  int argc,
4004  sqlite3_value **argv
4005){
4006  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4007  UNUSED_PARAMETER(argc);
4008  if( zName ){
4009    char *z = sqlite3_mprintf("\"%w\"", zName);
4010    sqlite3_result_text(context, z, -1, sqlite3_free);
4011  }
4012}
4013
4014/*
4015** Scalar function "shell_escape_crnl" used by the .recover command.
4016** The argument passed to this function is the output of built-in
4017** function quote(). If the first character of the input is "'",
4018** indicating that the value passed to quote() was a text value,
4019** then this function searches the input for "\n" and "\r" characters
4020** and adds a wrapper similar to the following:
4021**
4022**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4023**
4024** Or, if the first character of the input is not "'", then a copy
4025** of the input is returned.
4026*/
4027static void shellEscapeCrnl(
4028  sqlite3_context *context,
4029  int argc,
4030  sqlite3_value **argv
4031){
4032  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4033  UNUSED_PARAMETER(argc);
4034  if( zText[0]=='\'' ){
4035    int nText = sqlite3_value_bytes(argv[0]);
4036    int i;
4037    char zBuf1[20];
4038    char zBuf2[20];
4039    const char *zNL = 0;
4040    const char *zCR = 0;
4041    int nCR = 0;
4042    int nNL = 0;
4043
4044    for(i=0; zText[i]; i++){
4045      if( zNL==0 && zText[i]=='\n' ){
4046        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4047        nNL = (int)strlen(zNL);
4048      }
4049      if( zCR==0 && zText[i]=='\r' ){
4050        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4051        nCR = (int)strlen(zCR);
4052      }
4053    }
4054
4055    if( zNL || zCR ){
4056      int iOut = 0;
4057      i64 nMax = (nNL > nCR) ? nNL : nCR;
4058      i64 nAlloc = nMax * nText + (nMax+64)*2;
4059      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4060      if( zOut==0 ){
4061        sqlite3_result_error_nomem(context);
4062        return;
4063      }
4064
4065      if( zNL && zCR ){
4066        memcpy(&zOut[iOut], "replace(replace(", 16);
4067        iOut += 16;
4068      }else{
4069        memcpy(&zOut[iOut], "replace(", 8);
4070        iOut += 8;
4071      }
4072      for(i=0; zText[i]; i++){
4073        if( zText[i]=='\n' ){
4074          memcpy(&zOut[iOut], zNL, nNL);
4075          iOut += nNL;
4076        }else if( zText[i]=='\r' ){
4077          memcpy(&zOut[iOut], zCR, nCR);
4078          iOut += nCR;
4079        }else{
4080          zOut[iOut] = zText[i];
4081          iOut++;
4082        }
4083      }
4084
4085      if( zNL ){
4086        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4087        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4088        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4089      }
4090      if( zCR ){
4091        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4092        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4093        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4094      }
4095
4096      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4097      sqlite3_free(zOut);
4098      return;
4099    }
4100  }
4101
4102  sqlite3_result_value(context, argv[0]);
4103}
4104
4105/* Flags for open_db().
4106**
4107** The default behavior of open_db() is to exit(1) if the database fails to
4108** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4109** but still returns without calling exit.
4110**
4111** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4112** ZIP archive if the file does not exist or is empty and its name matches
4113** the *.zip pattern.
4114*/
4115#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4116#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4117
4118/*
4119** Make sure the database is open.  If it is not, then open it.  If
4120** the database fails to open, print an error message and exit.
4121*/
4122static void open_db(ShellState *p, int openFlags){
4123  if( p->db==0 ){
4124    if( p->openMode==SHELL_OPEN_UNSPEC ){
4125      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4126        p->openMode = SHELL_OPEN_NORMAL;
4127      }else{
4128        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4129                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4130      }
4131    }
4132    switch( p->openMode ){
4133      case SHELL_OPEN_APPENDVFS: {
4134        sqlite3_open_v2(p->zDbFilename, &p->db,
4135           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4136        break;
4137      }
4138      case SHELL_OPEN_HEXDB:
4139      case SHELL_OPEN_DESERIALIZE: {
4140        sqlite3_open(0, &p->db);
4141        break;
4142      }
4143      case SHELL_OPEN_ZIPFILE: {
4144        sqlite3_open(":memory:", &p->db);
4145        break;
4146      }
4147      case SHELL_OPEN_READONLY: {
4148        sqlite3_open_v2(p->zDbFilename, &p->db,
4149            SQLITE_OPEN_READONLY|p->openFlags, 0);
4150        break;
4151      }
4152      case SHELL_OPEN_UNSPEC:
4153      case SHELL_OPEN_NORMAL: {
4154        sqlite3_open_v2(p->zDbFilename, &p->db,
4155           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4156        break;
4157      }
4158    }
4159    globalDb = p->db;
4160    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4161      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4162          p->zDbFilename, sqlite3_errmsg(p->db));
4163      if( openFlags & OPEN_DB_KEEPALIVE ){
4164        sqlite3_open(":memory:", &p->db);
4165        return;
4166      }
4167      exit(1);
4168    }
4169#ifndef SQLITE_OMIT_LOAD_EXTENSION
4170    sqlite3_enable_load_extension(p->db, 1);
4171#endif
4172    sqlite3_fileio_init(p->db, 0, 0);
4173    sqlite3_shathree_init(p->db, 0, 0);
4174    sqlite3_completion_init(p->db, 0, 0);
4175#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4176    sqlite3_dbdata_init(p->db, 0, 0);
4177#endif
4178#ifdef SQLITE_HAVE_ZLIB
4179    sqlite3_zipfile_init(p->db, 0, 0);
4180    sqlite3_sqlar_init(p->db, 0, 0);
4181#endif
4182    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4183                            shellAddSchemaName, 0, 0);
4184    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4185                            shellModuleSchema, 0, 0);
4186    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4187                            shellPutsFunc, 0, 0);
4188    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4189                            shellEscapeCrnl, 0, 0);
4190    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4191                            shellInt32, 0, 0);
4192    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4193                            shellIdQuote, 0, 0);
4194#ifndef SQLITE_NOHAVE_SYSTEM
4195    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4196                            editFunc, 0, 0);
4197    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4198                            editFunc, 0, 0);
4199#endif
4200    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4201      char *zSql = sqlite3_mprintf(
4202         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4203      sqlite3_exec(p->db, zSql, 0, 0, 0);
4204      sqlite3_free(zSql);
4205    }
4206#ifdef SQLITE_ENABLE_DESERIALIZE
4207    else
4208    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4209      int rc;
4210      int nData = 0;
4211      unsigned char *aData;
4212      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4213        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4214      }else{
4215        aData = readHexDb(p, &nData);
4216        if( aData==0 ){
4217          return;
4218        }
4219      }
4220      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4221                   SQLITE_DESERIALIZE_RESIZEABLE |
4222                   SQLITE_DESERIALIZE_FREEONCLOSE);
4223      if( rc ){
4224        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4225      }
4226      if( p->szMax>0 ){
4227        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4228      }
4229    }
4230#endif
4231  }
4232}
4233
4234/*
4235** Attempt to close the databaes connection.  Report errors.
4236*/
4237void close_db(sqlite3 *db){
4238  int rc = sqlite3_close(db);
4239  if( rc ){
4240    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4241        rc, sqlite3_errmsg(db));
4242  }
4243}
4244
4245#if HAVE_READLINE || HAVE_EDITLINE
4246/*
4247** Readline completion callbacks
4248*/
4249static char *readline_completion_generator(const char *text, int state){
4250  static sqlite3_stmt *pStmt = 0;
4251  char *zRet;
4252  if( state==0 ){
4253    char *zSql;
4254    sqlite3_finalize(pStmt);
4255    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4256                           "  FROM completion(%Q) ORDER BY 1", text);
4257    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4258    sqlite3_free(zSql);
4259  }
4260  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4261    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4262  }else{
4263    sqlite3_finalize(pStmt);
4264    pStmt = 0;
4265    zRet = 0;
4266  }
4267  return zRet;
4268}
4269static char **readline_completion(const char *zText, int iStart, int iEnd){
4270  rl_attempted_completion_over = 1;
4271  return rl_completion_matches(zText, readline_completion_generator);
4272}
4273
4274#elif HAVE_LINENOISE
4275/*
4276** Linenoise completion callback
4277*/
4278static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4279  int nLine = strlen30(zLine);
4280  int i, iStart;
4281  sqlite3_stmt *pStmt = 0;
4282  char *zSql;
4283  char zBuf[1000];
4284
4285  if( nLine>sizeof(zBuf)-30 ) return;
4286  if( zLine[0]=='.' || zLine[0]=='#') return;
4287  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4288  if( i==nLine-1 ) return;
4289  iStart = i+1;
4290  memcpy(zBuf, zLine, iStart);
4291  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4292                         "  FROM completion(%Q,%Q) ORDER BY 1",
4293                         &zLine[iStart], zLine);
4294  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4295  sqlite3_free(zSql);
4296  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4297  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4298    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4299    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4300    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4301      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4302      linenoiseAddCompletion(lc, zBuf);
4303    }
4304  }
4305  sqlite3_finalize(pStmt);
4306}
4307#endif
4308
4309/*
4310** Do C-language style dequoting.
4311**
4312**    \a    -> alarm
4313**    \b    -> backspace
4314**    \t    -> tab
4315**    \n    -> newline
4316**    \v    -> vertical tab
4317**    \f    -> form feed
4318**    \r    -> carriage return
4319**    \s    -> space
4320**    \"    -> "
4321**    \'    -> '
4322**    \\    -> backslash
4323**    \NNN  -> ascii character NNN in octal
4324*/
4325static void resolve_backslashes(char *z){
4326  int i, j;
4327  char c;
4328  while( *z && *z!='\\' ) z++;
4329  for(i=j=0; (c = z[i])!=0; i++, j++){
4330    if( c=='\\' && z[i+1]!=0 ){
4331      c = z[++i];
4332      if( c=='a' ){
4333        c = '\a';
4334      }else if( c=='b' ){
4335        c = '\b';
4336      }else if( c=='t' ){
4337        c = '\t';
4338      }else if( c=='n' ){
4339        c = '\n';
4340      }else if( c=='v' ){
4341        c = '\v';
4342      }else if( c=='f' ){
4343        c = '\f';
4344      }else if( c=='r' ){
4345        c = '\r';
4346      }else if( c=='"' ){
4347        c = '"';
4348      }else if( c=='\'' ){
4349        c = '\'';
4350      }else if( c=='\\' ){
4351        c = '\\';
4352      }else if( c>='0' && c<='7' ){
4353        c -= '0';
4354        if( z[i+1]>='0' && z[i+1]<='7' ){
4355          i++;
4356          c = (c<<3) + z[i] - '0';
4357          if( z[i+1]>='0' && z[i+1]<='7' ){
4358            i++;
4359            c = (c<<3) + z[i] - '0';
4360          }
4361        }
4362      }
4363    }
4364    z[j] = c;
4365  }
4366  if( j<i ) z[j] = 0;
4367}
4368
4369/*
4370** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4371** for TRUE and FALSE.  Return the integer value if appropriate.
4372*/
4373static int booleanValue(const char *zArg){
4374  int i;
4375  if( zArg[0]=='0' && zArg[1]=='x' ){
4376    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4377  }else{
4378    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4379  }
4380  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4381  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4382    return 1;
4383  }
4384  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4385    return 0;
4386  }
4387  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4388          zArg);
4389  return 0;
4390}
4391
4392/*
4393** Set or clear a shell flag according to a boolean value.
4394*/
4395static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4396  if( booleanValue(zArg) ){
4397    ShellSetFlag(p, mFlag);
4398  }else{
4399    ShellClearFlag(p, mFlag);
4400  }
4401}
4402
4403/*
4404** Close an output file, assuming it is not stderr or stdout
4405*/
4406static void output_file_close(FILE *f){
4407  if( f && f!=stdout && f!=stderr ) fclose(f);
4408}
4409
4410/*
4411** Try to open an output file.   The names "stdout" and "stderr" are
4412** recognized and do the right thing.  NULL is returned if the output
4413** filename is "off".
4414*/
4415static FILE *output_file_open(const char *zFile, int bTextMode){
4416  FILE *f;
4417  if( strcmp(zFile,"stdout")==0 ){
4418    f = stdout;
4419  }else if( strcmp(zFile, "stderr")==0 ){
4420    f = stderr;
4421  }else if( strcmp(zFile, "off")==0 ){
4422    f = 0;
4423  }else{
4424    f = fopen(zFile, bTextMode ? "w" : "wb");
4425    if( f==0 ){
4426      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4427    }
4428  }
4429  return f;
4430}
4431
4432#ifndef SQLITE_OMIT_TRACE
4433/*
4434** A routine for handling output from sqlite3_trace().
4435*/
4436static int sql_trace_callback(
4437  unsigned mType,         /* The trace type */
4438  void *pArg,             /* The ShellState pointer */
4439  void *pP,               /* Usually a pointer to sqlite_stmt */
4440  void *pX                /* Auxiliary output */
4441){
4442  ShellState *p = (ShellState*)pArg;
4443  sqlite3_stmt *pStmt;
4444  const char *zSql;
4445  int nSql;
4446  if( p->traceOut==0 ) return 0;
4447  if( mType==SQLITE_TRACE_CLOSE ){
4448    utf8_printf(p->traceOut, "-- closing database connection\n");
4449    return 0;
4450  }
4451  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4452    zSql = (const char*)pX;
4453  }else{
4454    pStmt = (sqlite3_stmt*)pP;
4455    switch( p->eTraceType ){
4456      case SHELL_TRACE_EXPANDED: {
4457        zSql = sqlite3_expanded_sql(pStmt);
4458        break;
4459      }
4460#ifdef SQLITE_ENABLE_NORMALIZE
4461      case SHELL_TRACE_NORMALIZED: {
4462        zSql = sqlite3_normalized_sql(pStmt);
4463        break;
4464      }
4465#endif
4466      default: {
4467        zSql = sqlite3_sql(pStmt);
4468        break;
4469      }
4470    }
4471  }
4472  if( zSql==0 ) return 0;
4473  nSql = strlen30(zSql);
4474  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4475  switch( mType ){
4476    case SQLITE_TRACE_ROW:
4477    case SQLITE_TRACE_STMT: {
4478      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4479      break;
4480    }
4481    case SQLITE_TRACE_PROFILE: {
4482      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4483      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4484      break;
4485    }
4486  }
4487  return 0;
4488}
4489#endif
4490
4491/*
4492** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4493** a useful spot to set a debugger breakpoint.
4494*/
4495static void test_breakpoint(void){
4496  static int nCall = 0;
4497  nCall++;
4498}
4499
4500/*
4501** An object used to read a CSV and other files for import.
4502*/
4503typedef struct ImportCtx ImportCtx;
4504struct ImportCtx {
4505  const char *zFile;  /* Name of the input file */
4506  FILE *in;           /* Read the CSV text from this input stream */
4507  char *z;            /* Accumulated text for a field */
4508  int n;              /* Number of bytes in z */
4509  int nAlloc;         /* Space allocated for z[] */
4510  int nLine;          /* Current line number */
4511  int bNotFirst;      /* True if one or more bytes already read */
4512  int cTerm;          /* Character that terminated the most recent field */
4513  int cColSep;        /* The column separator character.  (Usually ",") */
4514  int cRowSep;        /* The row separator character.  (Usually "\n") */
4515};
4516
4517/* Append a single byte to z[] */
4518static void import_append_char(ImportCtx *p, int c){
4519  if( p->n+1>=p->nAlloc ){
4520    p->nAlloc += p->nAlloc + 100;
4521    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4522    if( p->z==0 ) shell_out_of_memory();
4523  }
4524  p->z[p->n++] = (char)c;
4525}
4526
4527/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4528** with the option of having a separator other than ",".
4529**
4530**   +  Input comes from p->in.
4531**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4532**      from sqlite3_malloc64().
4533**   +  Use p->cSep as the column separator.  The default is ",".
4534**   +  Use p->rSep as the row separator.  The default is "\n".
4535**   +  Keep track of the line number in p->nLine.
4536**   +  Store the character that terminates the field in p->cTerm.  Store
4537**      EOF on end-of-file.
4538**   +  Report syntax errors on stderr
4539*/
4540static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4541  int c;
4542  int cSep = p->cColSep;
4543  int rSep = p->cRowSep;
4544  p->n = 0;
4545  c = fgetc(p->in);
4546  if( c==EOF || seenInterrupt ){
4547    p->cTerm = EOF;
4548    return 0;
4549  }
4550  if( c=='"' ){
4551    int pc, ppc;
4552    int startLine = p->nLine;
4553    int cQuote = c;
4554    pc = ppc = 0;
4555    while( 1 ){
4556      c = fgetc(p->in);
4557      if( c==rSep ) p->nLine++;
4558      if( c==cQuote ){
4559        if( pc==cQuote ){
4560          pc = 0;
4561          continue;
4562        }
4563      }
4564      if( (c==cSep && pc==cQuote)
4565       || (c==rSep && pc==cQuote)
4566       || (c==rSep && pc=='\r' && ppc==cQuote)
4567       || (c==EOF && pc==cQuote)
4568      ){
4569        do{ p->n--; }while( p->z[p->n]!=cQuote );
4570        p->cTerm = c;
4571        break;
4572      }
4573      if( pc==cQuote && c!='\r' ){
4574        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
4575                p->zFile, p->nLine, cQuote);
4576      }
4577      if( c==EOF ){
4578        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
4579                p->zFile, startLine, cQuote);
4580        p->cTerm = c;
4581        break;
4582      }
4583      import_append_char(p, c);
4584      ppc = pc;
4585      pc = c;
4586    }
4587  }else{
4588    /* If this is the first field being parsed and it begins with the
4589    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
4590    if( (c&0xff)==0xef && p->bNotFirst==0 ){
4591      import_append_char(p, c);
4592      c = fgetc(p->in);
4593      if( (c&0xff)==0xbb ){
4594        import_append_char(p, c);
4595        c = fgetc(p->in);
4596        if( (c&0xff)==0xbf ){
4597          p->bNotFirst = 1;
4598          p->n = 0;
4599          return csv_read_one_field(p);
4600        }
4601      }
4602    }
4603    while( c!=EOF && c!=cSep && c!=rSep ){
4604      import_append_char(p, c);
4605      c = fgetc(p->in);
4606    }
4607    if( c==rSep ){
4608      p->nLine++;
4609      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
4610    }
4611    p->cTerm = c;
4612  }
4613  if( p->z ) p->z[p->n] = 0;
4614  p->bNotFirst = 1;
4615  return p->z;
4616}
4617
4618/* Read a single field of ASCII delimited text.
4619**
4620**   +  Input comes from p->in.
4621**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4622**      from sqlite3_malloc64().
4623**   +  Use p->cSep as the column separator.  The default is "\x1F".
4624**   +  Use p->rSep as the row separator.  The default is "\x1E".
4625**   +  Keep track of the row number in p->nLine.
4626**   +  Store the character that terminates the field in p->cTerm.  Store
4627**      EOF on end-of-file.
4628**   +  Report syntax errors on stderr
4629*/
4630static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
4631  int c;
4632  int cSep = p->cColSep;
4633  int rSep = p->cRowSep;
4634  p->n = 0;
4635  c = fgetc(p->in);
4636  if( c==EOF || seenInterrupt ){
4637    p->cTerm = EOF;
4638    return 0;
4639  }
4640  while( c!=EOF && c!=cSep && c!=rSep ){
4641    import_append_char(p, c);
4642    c = fgetc(p->in);
4643  }
4644  if( c==rSep ){
4645    p->nLine++;
4646  }
4647  p->cTerm = c;
4648  if( p->z ) p->z[p->n] = 0;
4649  return p->z;
4650}
4651
4652/*
4653** Try to transfer data for table zTable.  If an error is seen while
4654** moving forward, try to go backwards.  The backwards movement won't
4655** work for WITHOUT ROWID tables.
4656*/
4657static void tryToCloneData(
4658  ShellState *p,
4659  sqlite3 *newDb,
4660  const char *zTable
4661){
4662  sqlite3_stmt *pQuery = 0;
4663  sqlite3_stmt *pInsert = 0;
4664  char *zQuery = 0;
4665  char *zInsert = 0;
4666  int rc;
4667  int i, j, n;
4668  int nTable = strlen30(zTable);
4669  int k = 0;
4670  int cnt = 0;
4671  const int spinRate = 10000;
4672
4673  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
4674  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4675  if( rc ){
4676    utf8_printf(stderr, "Error %d: %s on [%s]\n",
4677            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4678            zQuery);
4679    goto end_data_xfer;
4680  }
4681  n = sqlite3_column_count(pQuery);
4682  zInsert = sqlite3_malloc64(200 + nTable + n*3);
4683  if( zInsert==0 ) shell_out_of_memory();
4684  sqlite3_snprintf(200+nTable,zInsert,
4685                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
4686  i = strlen30(zInsert);
4687  for(j=1; j<n; j++){
4688    memcpy(zInsert+i, ",?", 2);
4689    i += 2;
4690  }
4691  memcpy(zInsert+i, ");", 3);
4692  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
4693  if( rc ){
4694    utf8_printf(stderr, "Error %d: %s on [%s]\n",
4695            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
4696            zQuery);
4697    goto end_data_xfer;
4698  }
4699  for(k=0; k<2; k++){
4700    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4701      for(i=0; i<n; i++){
4702        switch( sqlite3_column_type(pQuery, i) ){
4703          case SQLITE_NULL: {
4704            sqlite3_bind_null(pInsert, i+1);
4705            break;
4706          }
4707          case SQLITE_INTEGER: {
4708            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
4709            break;
4710          }
4711          case SQLITE_FLOAT: {
4712            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
4713            break;
4714          }
4715          case SQLITE_TEXT: {
4716            sqlite3_bind_text(pInsert, i+1,
4717                             (const char*)sqlite3_column_text(pQuery,i),
4718                             -1, SQLITE_STATIC);
4719            break;
4720          }
4721          case SQLITE_BLOB: {
4722            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
4723                                            sqlite3_column_bytes(pQuery,i),
4724                                            SQLITE_STATIC);
4725            break;
4726          }
4727        }
4728      } /* End for */
4729      rc = sqlite3_step(pInsert);
4730      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
4731        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
4732                        sqlite3_errmsg(newDb));
4733      }
4734      sqlite3_reset(pInsert);
4735      cnt++;
4736      if( (cnt%spinRate)==0 ){
4737        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
4738        fflush(stdout);
4739      }
4740    } /* End while */
4741    if( rc==SQLITE_DONE ) break;
4742    sqlite3_finalize(pQuery);
4743    sqlite3_free(zQuery);
4744    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
4745                             zTable);
4746    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4747    if( rc ){
4748      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
4749      break;
4750    }
4751  } /* End for(k=0...) */
4752
4753end_data_xfer:
4754  sqlite3_finalize(pQuery);
4755  sqlite3_finalize(pInsert);
4756  sqlite3_free(zQuery);
4757  sqlite3_free(zInsert);
4758}
4759
4760
4761/*
4762** Try to transfer all rows of the schema that match zWhere.  For
4763** each row, invoke xForEach() on the object defined by that row.
4764** If an error is encountered while moving forward through the
4765** sqlite_master table, try again moving backwards.
4766*/
4767static void tryToCloneSchema(
4768  ShellState *p,
4769  sqlite3 *newDb,
4770  const char *zWhere,
4771  void (*xForEach)(ShellState*,sqlite3*,const char*)
4772){
4773  sqlite3_stmt *pQuery = 0;
4774  char *zQuery = 0;
4775  int rc;
4776  const unsigned char *zName;
4777  const unsigned char *zSql;
4778  char *zErrMsg = 0;
4779
4780  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4781                           " WHERE %s", zWhere);
4782  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4783  if( rc ){
4784    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4785                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4786                    zQuery);
4787    goto end_schema_xfer;
4788  }
4789  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4790    zName = sqlite3_column_text(pQuery, 0);
4791    zSql = sqlite3_column_text(pQuery, 1);
4792    printf("%s... ", zName); fflush(stdout);
4793    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4794    if( zErrMsg ){
4795      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4796      sqlite3_free(zErrMsg);
4797      zErrMsg = 0;
4798    }
4799    if( xForEach ){
4800      xForEach(p, newDb, (const char*)zName);
4801    }
4802    printf("done\n");
4803  }
4804  if( rc!=SQLITE_DONE ){
4805    sqlite3_finalize(pQuery);
4806    sqlite3_free(zQuery);
4807    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4808                             " WHERE %s ORDER BY rowid DESC", zWhere);
4809    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4810    if( rc ){
4811      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4812                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4813                      zQuery);
4814      goto end_schema_xfer;
4815    }
4816    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4817      zName = sqlite3_column_text(pQuery, 0);
4818      zSql = sqlite3_column_text(pQuery, 1);
4819      printf("%s... ", zName); fflush(stdout);
4820      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4821      if( zErrMsg ){
4822        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4823        sqlite3_free(zErrMsg);
4824        zErrMsg = 0;
4825      }
4826      if( xForEach ){
4827        xForEach(p, newDb, (const char*)zName);
4828      }
4829      printf("done\n");
4830    }
4831  }
4832end_schema_xfer:
4833  sqlite3_finalize(pQuery);
4834  sqlite3_free(zQuery);
4835}
4836
4837/*
4838** Open a new database file named "zNewDb".  Try to recover as much information
4839** as possible out of the main database (which might be corrupt) and write it
4840** into zNewDb.
4841*/
4842static void tryToClone(ShellState *p, const char *zNewDb){
4843  int rc;
4844  sqlite3 *newDb = 0;
4845  if( access(zNewDb,0)==0 ){
4846    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
4847    return;
4848  }
4849  rc = sqlite3_open(zNewDb, &newDb);
4850  if( rc ){
4851    utf8_printf(stderr, "Cannot create output database: %s\n",
4852            sqlite3_errmsg(newDb));
4853  }else{
4854    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
4855    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
4856    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
4857    tryToCloneSchema(p, newDb, "type!='table'", 0);
4858    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
4859    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4860  }
4861  close_db(newDb);
4862}
4863
4864/*
4865** Change the output file back to stdout.
4866**
4867** If the p->doXdgOpen flag is set, that means the output was being
4868** redirected to a temporary file named by p->zTempFile.  In that case,
4869** launch start/open/xdg-open on that temporary file.
4870*/
4871static void output_reset(ShellState *p){
4872  if( p->outfile[0]=='|' ){
4873#ifndef SQLITE_OMIT_POPEN
4874    pclose(p->out);
4875#endif
4876  }else{
4877    output_file_close(p->out);
4878#ifndef SQLITE_NOHAVE_SYSTEM
4879    if( p->doXdgOpen ){
4880      const char *zXdgOpenCmd =
4881#if defined(_WIN32)
4882      "start";
4883#elif defined(__APPLE__)
4884      "open";
4885#else
4886      "xdg-open";
4887#endif
4888      char *zCmd;
4889      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
4890      if( system(zCmd) ){
4891        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
4892      }
4893      sqlite3_free(zCmd);
4894      outputModePop(p);
4895      p->doXdgOpen = 0;
4896      sqlite3_sleep(100);
4897    }
4898#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
4899  }
4900  p->outfile[0] = 0;
4901  p->out = stdout;
4902}
4903
4904/*
4905** Run an SQL command and return the single integer result.
4906*/
4907static int db_int(ShellState *p, const char *zSql){
4908  sqlite3_stmt *pStmt;
4909  int res = 0;
4910  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4911  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
4912    res = sqlite3_column_int(pStmt,0);
4913  }
4914  sqlite3_finalize(pStmt);
4915  return res;
4916}
4917
4918/*
4919** Convert a 2-byte or 4-byte big-endian integer into a native integer
4920*/
4921static unsigned int get2byteInt(unsigned char *a){
4922  return (a[0]<<8) + a[1];
4923}
4924static unsigned int get4byteInt(unsigned char *a){
4925  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
4926}
4927
4928/*
4929** Implementation of the ".dbinfo" command.
4930**
4931** Return 1 on error, 2 to exit, and 0 otherwise.
4932*/
4933static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
4934  static const struct { const char *zName; int ofst; } aField[] = {
4935     { "file change counter:",  24  },
4936     { "database page count:",  28  },
4937     { "freelist page count:",  36  },
4938     { "schema cookie:",        40  },
4939     { "schema format:",        44  },
4940     { "default cache size:",   48  },
4941     { "autovacuum top root:",  52  },
4942     { "incremental vacuum:",   64  },
4943     { "text encoding:",        56  },
4944     { "user version:",         60  },
4945     { "application id:",       68  },
4946     { "software version:",     96  },
4947  };
4948  static const struct { const char *zName; const char *zSql; } aQuery[] = {
4949     { "number of tables:",
4950       "SELECT count(*) FROM %s WHERE type='table'" },
4951     { "number of indexes:",
4952       "SELECT count(*) FROM %s WHERE type='index'" },
4953     { "number of triggers:",
4954       "SELECT count(*) FROM %s WHERE type='trigger'" },
4955     { "number of views:",
4956       "SELECT count(*) FROM %s WHERE type='view'" },
4957     { "schema size:",
4958       "SELECT total(length(sql)) FROM %s" },
4959  };
4960  int i, rc;
4961  unsigned iDataVersion;
4962  char *zSchemaTab;
4963  char *zDb = nArg>=2 ? azArg[1] : "main";
4964  sqlite3_stmt *pStmt = 0;
4965  unsigned char aHdr[100];
4966  open_db(p, 0);
4967  if( p->db==0 ) return 1;
4968  rc = sqlite3_prepare_v2(p->db,
4969             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
4970             -1, &pStmt, 0);
4971  if( rc ){
4972    if( !sqlite3_compileoption_used("ENABLE_DBPAGE_VTAB") ){
4973      utf8_printf(stderr, "the \".dbinfo\" command requires the "
4974                          "-DSQLITE_ENABLE_DBPAGE_VTAB compile-time options\n");
4975    }else{
4976      utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
4977    }
4978    sqlite3_finalize(pStmt);
4979    return 1;
4980  }
4981  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
4982  if( sqlite3_step(pStmt)==SQLITE_ROW
4983   && sqlite3_column_bytes(pStmt,0)>100
4984  ){
4985    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
4986    sqlite3_finalize(pStmt);
4987  }else{
4988    raw_printf(stderr, "unable to read database header\n");
4989    sqlite3_finalize(pStmt);
4990    return 1;
4991  }
4992  i = get2byteInt(aHdr+16);
4993  if( i==1 ) i = 65536;
4994  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
4995  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
4996  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
4997  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
4998  for(i=0; i<ArraySize(aField); i++){
4999    int ofst = aField[i].ofst;
5000    unsigned int val = get4byteInt(aHdr + ofst);
5001    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5002    switch( ofst ){
5003      case 56: {
5004        if( val==1 ) raw_printf(p->out, " (utf8)");
5005        if( val==2 ) raw_printf(p->out, " (utf16le)");
5006        if( val==3 ) raw_printf(p->out, " (utf16be)");
5007      }
5008    }
5009    raw_printf(p->out, "\n");
5010  }
5011  if( zDb==0 ){
5012    zSchemaTab = sqlite3_mprintf("main.sqlite_master");
5013  }else if( strcmp(zDb,"temp")==0 ){
5014    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
5015  }else{
5016    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
5017  }
5018  for(i=0; i<ArraySize(aQuery); i++){
5019    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5020    int val = db_int(p, zSql);
5021    sqlite3_free(zSql);
5022    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5023  }
5024  sqlite3_free(zSchemaTab);
5025  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5026  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5027  return 0;
5028}
5029
5030/*
5031** Print the current sqlite3_errmsg() value to stderr and return 1.
5032*/
5033static int shellDatabaseError(sqlite3 *db){
5034  const char *zErr = sqlite3_errmsg(db);
5035  utf8_printf(stderr, "Error: %s\n", zErr);
5036  return 1;
5037}
5038
5039/*
5040** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5041** if they match and FALSE (0) if they do not match.
5042**
5043** Globbing rules:
5044**
5045**      '*'       Matches any sequence of zero or more characters.
5046**
5047**      '?'       Matches exactly one character.
5048**
5049**     [...]      Matches one character from the enclosed list of
5050**                characters.
5051**
5052**     [^...]     Matches one character not in the enclosed list.
5053**
5054**      '#'       Matches any sequence of one or more digits with an
5055**                optional + or - sign in front
5056**
5057**      ' '       Any span of whitespace matches any other span of
5058**                whitespace.
5059**
5060** Extra whitespace at the end of z[] is ignored.
5061*/
5062static int testcase_glob(const char *zGlob, const char *z){
5063  int c, c2;
5064  int invert;
5065  int seen;
5066
5067  while( (c = (*(zGlob++)))!=0 ){
5068    if( IsSpace(c) ){
5069      if( !IsSpace(*z) ) return 0;
5070      while( IsSpace(*zGlob) ) zGlob++;
5071      while( IsSpace(*z) ) z++;
5072    }else if( c=='*' ){
5073      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5074        if( c=='?' && (*(z++))==0 ) return 0;
5075      }
5076      if( c==0 ){
5077        return 1;
5078      }else if( c=='[' ){
5079        while( *z && testcase_glob(zGlob-1,z)==0 ){
5080          z++;
5081        }
5082        return (*z)!=0;
5083      }
5084      while( (c2 = (*(z++)))!=0 ){
5085        while( c2!=c ){
5086          c2 = *(z++);
5087          if( c2==0 ) return 0;
5088        }
5089        if( testcase_glob(zGlob,z) ) return 1;
5090      }
5091      return 0;
5092    }else if( c=='?' ){
5093      if( (*(z++))==0 ) return 0;
5094    }else if( c=='[' ){
5095      int prior_c = 0;
5096      seen = 0;
5097      invert = 0;
5098      c = *(z++);
5099      if( c==0 ) return 0;
5100      c2 = *(zGlob++);
5101      if( c2=='^' ){
5102        invert = 1;
5103        c2 = *(zGlob++);
5104      }
5105      if( c2==']' ){
5106        if( c==']' ) seen = 1;
5107        c2 = *(zGlob++);
5108      }
5109      while( c2 && c2!=']' ){
5110        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5111          c2 = *(zGlob++);
5112          if( c>=prior_c && c<=c2 ) seen = 1;
5113          prior_c = 0;
5114        }else{
5115          if( c==c2 ){
5116            seen = 1;
5117          }
5118          prior_c = c2;
5119        }
5120        c2 = *(zGlob++);
5121      }
5122      if( c2==0 || (seen ^ invert)==0 ) return 0;
5123    }else if( c=='#' ){
5124      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5125      if( !IsDigit(z[0]) ) return 0;
5126      z++;
5127      while( IsDigit(z[0]) ){ z++; }
5128    }else{
5129      if( c!=(*(z++)) ) return 0;
5130    }
5131  }
5132  while( IsSpace(*z) ){ z++; }
5133  return *z==0;
5134}
5135
5136
5137/*
5138** Compare the string as a command-line option with either one or two
5139** initial "-" characters.
5140*/
5141static int optionMatch(const char *zStr, const char *zOpt){
5142  if( zStr[0]!='-' ) return 0;
5143  zStr++;
5144  if( zStr[0]=='-' ) zStr++;
5145  return strcmp(zStr, zOpt)==0;
5146}
5147
5148/*
5149** Delete a file.
5150*/
5151int shellDeleteFile(const char *zFilename){
5152  int rc;
5153#ifdef _WIN32
5154  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5155  rc = _wunlink(z);
5156  sqlite3_free(z);
5157#else
5158  rc = unlink(zFilename);
5159#endif
5160  return rc;
5161}
5162
5163/*
5164** Try to delete the temporary file (if there is one) and free the
5165** memory used to hold the name of the temp file.
5166*/
5167static void clearTempFile(ShellState *p){
5168  if( p->zTempFile==0 ) return;
5169  if( p->doXdgOpen ) return;
5170  if( shellDeleteFile(p->zTempFile) ) return;
5171  sqlite3_free(p->zTempFile);
5172  p->zTempFile = 0;
5173}
5174
5175/*
5176** Create a new temp file name with the given suffix.
5177*/
5178static void newTempFile(ShellState *p, const char *zSuffix){
5179  clearTempFile(p);
5180  sqlite3_free(p->zTempFile);
5181  p->zTempFile = 0;
5182  if( p->db ){
5183    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5184  }
5185  if( p->zTempFile==0 ){
5186    sqlite3_uint64 r;
5187    sqlite3_randomness(sizeof(r), &r);
5188    p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
5189  }else{
5190    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5191  }
5192  if( p->zTempFile==0 ){
5193    raw_printf(stderr, "out of memory\n");
5194    exit(1);
5195  }
5196}
5197
5198
5199/*
5200** The implementation of SQL scalar function fkey_collate_clause(), used
5201** by the ".lint fkey-indexes" command. This scalar function is always
5202** called with four arguments - the parent table name, the parent column name,
5203** the child table name and the child column name.
5204**
5205**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5206**
5207** If either of the named tables or columns do not exist, this function
5208** returns an empty string. An empty string is also returned if both tables
5209** and columns exist but have the same default collation sequence. Or,
5210** if both exist but the default collation sequences are different, this
5211** function returns the string " COLLATE <parent-collation>", where
5212** <parent-collation> is the default collation sequence of the parent column.
5213*/
5214static void shellFkeyCollateClause(
5215  sqlite3_context *pCtx,
5216  int nVal,
5217  sqlite3_value **apVal
5218){
5219  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5220  const char *zParent;
5221  const char *zParentCol;
5222  const char *zParentSeq;
5223  const char *zChild;
5224  const char *zChildCol;
5225  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5226  int rc;
5227
5228  assert( nVal==4 );
5229  zParent = (const char*)sqlite3_value_text(apVal[0]);
5230  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5231  zChild = (const char*)sqlite3_value_text(apVal[2]);
5232  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5233
5234  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5235  rc = sqlite3_table_column_metadata(
5236      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5237  );
5238  if( rc==SQLITE_OK ){
5239    rc = sqlite3_table_column_metadata(
5240        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5241    );
5242  }
5243
5244  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5245    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5246    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5247    sqlite3_free(z);
5248  }
5249}
5250
5251
5252/*
5253** The implementation of dot-command ".lint fkey-indexes".
5254*/
5255static int lintFkeyIndexes(
5256  ShellState *pState,             /* Current shell tool state */
5257  char **azArg,                   /* Array of arguments passed to dot command */
5258  int nArg                        /* Number of entries in azArg[] */
5259){
5260  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5261  FILE *out = pState->out;        /* Stream to write non-error output to */
5262  int bVerbose = 0;               /* If -verbose is present */
5263  int bGroupByParent = 0;         /* If -groupbyparent is present */
5264  int i;                          /* To iterate through azArg[] */
5265  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5266  int rc;                         /* Return code */
5267  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5268
5269  /*
5270  ** This SELECT statement returns one row for each foreign key constraint
5271  ** in the schema of the main database. The column values are:
5272  **
5273  ** 0. The text of an SQL statement similar to:
5274  **
5275  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5276  **
5277  **    This SELECT is similar to the one that the foreign keys implementation
5278  **    needs to run internally on child tables. If there is an index that can
5279  **    be used to optimize this query, then it can also be used by the FK
5280  **    implementation to optimize DELETE or UPDATE statements on the parent
5281  **    table.
5282  **
5283  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5284  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5285  **    contains an index that can be used to optimize the query.
5286  **
5287  ** 2. Human readable text that describes the child table and columns. e.g.
5288  **
5289  **       "child_table(child_key1, child_key2)"
5290  **
5291  ** 3. Human readable text that describes the parent table and columns. e.g.
5292  **
5293  **       "parent_table(parent_key1, parent_key2)"
5294  **
5295  ** 4. A full CREATE INDEX statement for an index that could be used to
5296  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5297  **
5298  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5299  **
5300  ** 5. The name of the parent table.
5301  **
5302  ** These six values are used by the C logic below to generate the report.
5303  */
5304  const char *zSql =
5305  "SELECT "
5306    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5307    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5308    "  || fkey_collate_clause("
5309    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5310    ", "
5311    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5312    "  || group_concat('*=?', ' AND ') || ')'"
5313    ", "
5314    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5315    ", "
5316    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5317    ", "
5318    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5319    "  || ' ON ' || quote(s.name) || '('"
5320    "  || group_concat(quote(f.[from]) ||"
5321    "        fkey_collate_clause("
5322    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5323    "  || ');'"
5324    ", "
5325    "     f.[table] "
5326    "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
5327    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5328    "GROUP BY s.name, f.id "
5329    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5330  ;
5331  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5332
5333  for(i=2; i<nArg; i++){
5334    int n = strlen30(azArg[i]);
5335    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5336      bVerbose = 1;
5337    }
5338    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5339      bGroupByParent = 1;
5340      zIndent = "    ";
5341    }
5342    else{
5343      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5344          azArg[0], azArg[1]
5345      );
5346      return SQLITE_ERROR;
5347    }
5348  }
5349
5350  /* Register the fkey_collate_clause() SQL function */
5351  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5352      0, shellFkeyCollateClause, 0, 0
5353  );
5354
5355
5356  if( rc==SQLITE_OK ){
5357    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5358  }
5359  if( rc==SQLITE_OK ){
5360    sqlite3_bind_int(pSql, 1, bGroupByParent);
5361  }
5362
5363  if( rc==SQLITE_OK ){
5364    int rc2;
5365    char *zPrev = 0;
5366    while( SQLITE_ROW==sqlite3_step(pSql) ){
5367      int res = -1;
5368      sqlite3_stmt *pExplain = 0;
5369      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5370      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5371      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5372      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5373      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5374      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5375
5376      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5377      if( rc!=SQLITE_OK ) break;
5378      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5379        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5380        res = (
5381              0==sqlite3_strglob(zGlob, zPlan)
5382           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5383        );
5384      }
5385      rc = sqlite3_finalize(pExplain);
5386      if( rc!=SQLITE_OK ) break;
5387
5388      if( res<0 ){
5389        raw_printf(stderr, "Error: internal error");
5390        break;
5391      }else{
5392        if( bGroupByParent
5393        && (bVerbose || res==0)
5394        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5395        ){
5396          raw_printf(out, "-- Parent table %s\n", zParent);
5397          sqlite3_free(zPrev);
5398          zPrev = sqlite3_mprintf("%s", zParent);
5399        }
5400
5401        if( res==0 ){
5402          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5403        }else if( bVerbose ){
5404          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5405              zIndent, zFrom, zTarget
5406          );
5407        }
5408      }
5409    }
5410    sqlite3_free(zPrev);
5411
5412    if( rc!=SQLITE_OK ){
5413      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5414    }
5415
5416    rc2 = sqlite3_finalize(pSql);
5417    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5418      rc = rc2;
5419      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5420    }
5421  }else{
5422    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5423  }
5424
5425  return rc;
5426}
5427
5428/*
5429** Implementation of ".lint" dot command.
5430*/
5431static int lintDotCommand(
5432  ShellState *pState,             /* Current shell tool state */
5433  char **azArg,                   /* Array of arguments passed to dot command */
5434  int nArg                        /* Number of entries in azArg[] */
5435){
5436  int n;
5437  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5438  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5439  return lintFkeyIndexes(pState, azArg, nArg);
5440
5441 usage:
5442  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5443  raw_printf(stderr, "Where sub-commands are:\n");
5444  raw_printf(stderr, "    fkey-indexes\n");
5445  return SQLITE_ERROR;
5446}
5447
5448#if !defined SQLITE_OMIT_VIRTUALTABLE
5449static void shellPrepare(
5450  sqlite3 *db,
5451  int *pRc,
5452  const char *zSql,
5453  sqlite3_stmt **ppStmt
5454){
5455  *ppStmt = 0;
5456  if( *pRc==SQLITE_OK ){
5457    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5458    if( rc!=SQLITE_OK ){
5459      raw_printf(stderr, "sql error: %s (%d)\n",
5460          sqlite3_errmsg(db), sqlite3_errcode(db)
5461      );
5462      *pRc = rc;
5463    }
5464  }
5465}
5466
5467/*
5468** Create a prepared statement using printf-style arguments for the SQL.
5469**
5470** This routine is could be marked "static".  But it is not always used,
5471** depending on compile-time options.  By omitting the "static", we avoid
5472** nuisance compiler warnings about "defined but not used".
5473*/
5474void shellPreparePrintf(
5475  sqlite3 *db,
5476  int *pRc,
5477  sqlite3_stmt **ppStmt,
5478  const char *zFmt,
5479  ...
5480){
5481  *ppStmt = 0;
5482  if( *pRc==SQLITE_OK ){
5483    va_list ap;
5484    char *z;
5485    va_start(ap, zFmt);
5486    z = sqlite3_vmprintf(zFmt, ap);
5487    va_end(ap);
5488    if( z==0 ){
5489      *pRc = SQLITE_NOMEM;
5490    }else{
5491      shellPrepare(db, pRc, z, ppStmt);
5492      sqlite3_free(z);
5493    }
5494  }
5495}
5496
5497/* Finalize the prepared statement created using shellPreparePrintf().
5498**
5499** This routine is could be marked "static".  But it is not always used,
5500** depending on compile-time options.  By omitting the "static", we avoid
5501** nuisance compiler warnings about "defined but not used".
5502*/
5503void shellFinalize(
5504  int *pRc,
5505  sqlite3_stmt *pStmt
5506){
5507  if( pStmt ){
5508    sqlite3 *db = sqlite3_db_handle(pStmt);
5509    int rc = sqlite3_finalize(pStmt);
5510    if( *pRc==SQLITE_OK ){
5511      if( rc!=SQLITE_OK ){
5512        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5513      }
5514      *pRc = rc;
5515    }
5516  }
5517}
5518
5519/* Reset the prepared statement created using shellPreparePrintf().
5520**
5521** This routine is could be marked "static".  But it is not always used,
5522** depending on compile-time options.  By omitting the "static", we avoid
5523** nuisance compiler warnings about "defined but not used".
5524*/
5525void shellReset(
5526  int *pRc,
5527  sqlite3_stmt *pStmt
5528){
5529  int rc = sqlite3_reset(pStmt);
5530  if( *pRc==SQLITE_OK ){
5531    if( rc!=SQLITE_OK ){
5532      sqlite3 *db = sqlite3_db_handle(pStmt);
5533      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5534    }
5535    *pRc = rc;
5536  }
5537}
5538#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
5539
5540#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5541/******************************************************************************
5542** The ".archive" or ".ar" command.
5543*/
5544/*
5545** Structure representing a single ".ar" command.
5546*/
5547typedef struct ArCommand ArCommand;
5548struct ArCommand {
5549  u8 eCmd;                        /* An AR_CMD_* value */
5550  u8 bVerbose;                    /* True if --verbose */
5551  u8 bZip;                        /* True if the archive is a ZIP */
5552  u8 bDryRun;                     /* True if --dry-run */
5553  u8 bAppend;                     /* True if --append */
5554  u8 fromCmdLine;                 /* Run from -A instead of .archive */
5555  int nArg;                       /* Number of command arguments */
5556  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
5557  const char *zFile;              /* --file argument, or NULL */
5558  const char *zDir;               /* --directory argument, or NULL */
5559  char **azArg;                   /* Array of command arguments */
5560  ShellState *p;                  /* Shell state */
5561  sqlite3 *db;                    /* Database containing the archive */
5562};
5563
5564/*
5565** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
5566*/
5567static int arUsage(FILE *f){
5568  showHelp(f,"archive");
5569  return SQLITE_ERROR;
5570}
5571
5572/*
5573** Print an error message for the .ar command to stderr and return
5574** SQLITE_ERROR.
5575*/
5576static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
5577  va_list ap;
5578  char *z;
5579  va_start(ap, zFmt);
5580  z = sqlite3_vmprintf(zFmt, ap);
5581  va_end(ap);
5582  utf8_printf(stderr, "Error: %s\n", z);
5583  if( pAr->fromCmdLine ){
5584    utf8_printf(stderr, "Use \"-A\" for more help\n");
5585  }else{
5586    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
5587  }
5588  sqlite3_free(z);
5589  return SQLITE_ERROR;
5590}
5591
5592/*
5593** Values for ArCommand.eCmd.
5594*/
5595#define AR_CMD_CREATE       1
5596#define AR_CMD_UPDATE       2
5597#define AR_CMD_INSERT       3
5598#define AR_CMD_EXTRACT      4
5599#define AR_CMD_LIST         5
5600#define AR_CMD_HELP         6
5601
5602/*
5603** Other (non-command) switches.
5604*/
5605#define AR_SWITCH_VERBOSE     7
5606#define AR_SWITCH_FILE        8
5607#define AR_SWITCH_DIRECTORY   9
5608#define AR_SWITCH_APPEND     10
5609#define AR_SWITCH_DRYRUN     11
5610
5611static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
5612  switch( eSwitch ){
5613    case AR_CMD_CREATE:
5614    case AR_CMD_EXTRACT:
5615    case AR_CMD_LIST:
5616    case AR_CMD_UPDATE:
5617    case AR_CMD_INSERT:
5618    case AR_CMD_HELP:
5619      if( pAr->eCmd ){
5620        return arErrorMsg(pAr, "multiple command options");
5621      }
5622      pAr->eCmd = eSwitch;
5623      break;
5624
5625    case AR_SWITCH_DRYRUN:
5626      pAr->bDryRun = 1;
5627      break;
5628    case AR_SWITCH_VERBOSE:
5629      pAr->bVerbose = 1;
5630      break;
5631    case AR_SWITCH_APPEND:
5632      pAr->bAppend = 1;
5633      /* Fall thru into --file */
5634    case AR_SWITCH_FILE:
5635      pAr->zFile = zArg;
5636      break;
5637    case AR_SWITCH_DIRECTORY:
5638      pAr->zDir = zArg;
5639      break;
5640  }
5641
5642  return SQLITE_OK;
5643}
5644
5645/*
5646** Parse the command line for an ".ar" command. The results are written into
5647** structure (*pAr). SQLITE_OK is returned if the command line is parsed
5648** successfully, otherwise an error message is written to stderr and
5649** SQLITE_ERROR returned.
5650*/
5651static int arParseCommand(
5652  char **azArg,                   /* Array of arguments passed to dot command */
5653  int nArg,                       /* Number of entries in azArg[] */
5654  ArCommand *pAr                  /* Populate this object */
5655){
5656  struct ArSwitch {
5657    const char *zLong;
5658    char cShort;
5659    u8 eSwitch;
5660    u8 bArg;
5661  } aSwitch[] = {
5662    { "create",    'c', AR_CMD_CREATE,       0 },
5663    { "extract",   'x', AR_CMD_EXTRACT,      0 },
5664    { "insert",    'i', AR_CMD_INSERT,       0 },
5665    { "list",      't', AR_CMD_LIST,         0 },
5666    { "update",    'u', AR_CMD_UPDATE,       0 },
5667    { "help",      'h', AR_CMD_HELP,         0 },
5668    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
5669    { "file",      'f', AR_SWITCH_FILE,      1 },
5670    { "append",    'a', AR_SWITCH_APPEND,    1 },
5671    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
5672    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
5673  };
5674  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
5675  struct ArSwitch *pEnd = &aSwitch[nSwitch];
5676
5677  if( nArg<=1 ){
5678    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
5679    return arUsage(stderr);
5680  }else{
5681    char *z = azArg[1];
5682    if( z[0]!='-' ){
5683      /* Traditional style [tar] invocation */
5684      int i;
5685      int iArg = 2;
5686      for(i=0; z[i]; i++){
5687        const char *zArg = 0;
5688        struct ArSwitch *pOpt;
5689        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5690          if( z[i]==pOpt->cShort ) break;
5691        }
5692        if( pOpt==pEnd ){
5693          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
5694        }
5695        if( pOpt->bArg ){
5696          if( iArg>=nArg ){
5697            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
5698          }
5699          zArg = azArg[iArg++];
5700        }
5701        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
5702      }
5703      pAr->nArg = nArg-iArg;
5704      if( pAr->nArg>0 ){
5705        pAr->azArg = &azArg[iArg];
5706      }
5707    }else{
5708      /* Non-traditional invocation */
5709      int iArg;
5710      for(iArg=1; iArg<nArg; iArg++){
5711        int n;
5712        z = azArg[iArg];
5713        if( z[0]!='-' ){
5714          /* All remaining command line words are command arguments. */
5715          pAr->azArg = &azArg[iArg];
5716          pAr->nArg = nArg-iArg;
5717          break;
5718        }
5719        n = strlen30(z);
5720
5721        if( z[1]!='-' ){
5722          int i;
5723          /* One or more short options */
5724          for(i=1; i<n; i++){
5725            const char *zArg = 0;
5726            struct ArSwitch *pOpt;
5727            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5728              if( z[i]==pOpt->cShort ) break;
5729            }
5730            if( pOpt==pEnd ){
5731              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
5732            }
5733            if( pOpt->bArg ){
5734              if( i<(n-1) ){
5735                zArg = &z[i+1];
5736                i = n;
5737              }else{
5738                if( iArg>=(nArg-1) ){
5739                  return arErrorMsg(pAr, "option requires an argument: %c",
5740                                    z[i]);
5741                }
5742                zArg = azArg[++iArg];
5743              }
5744            }
5745            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
5746          }
5747        }else if( z[2]=='\0' ){
5748          /* A -- option, indicating that all remaining command line words
5749          ** are command arguments.  */
5750          pAr->azArg = &azArg[iArg+1];
5751          pAr->nArg = nArg-iArg-1;
5752          break;
5753        }else{
5754          /* A long option */
5755          const char *zArg = 0;             /* Argument for option, if any */
5756          struct ArSwitch *pMatch = 0;      /* Matching option */
5757          struct ArSwitch *pOpt;            /* Iterator */
5758          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5759            const char *zLong = pOpt->zLong;
5760            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
5761              if( pMatch ){
5762                return arErrorMsg(pAr, "ambiguous option: %s",z);
5763              }else{
5764                pMatch = pOpt;
5765              }
5766            }
5767          }
5768
5769          if( pMatch==0 ){
5770            return arErrorMsg(pAr, "unrecognized option: %s", z);
5771          }
5772          if( pMatch->bArg ){
5773            if( iArg>=(nArg-1) ){
5774              return arErrorMsg(pAr, "option requires an argument: %s", z);
5775            }
5776            zArg = azArg[++iArg];
5777          }
5778          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
5779        }
5780      }
5781    }
5782  }
5783
5784  return SQLITE_OK;
5785}
5786
5787/*
5788** This function assumes that all arguments within the ArCommand.azArg[]
5789** array refer to archive members, as for the --extract or --list commands.
5790** It checks that each of them are present. If any specified file is not
5791** present in the archive, an error is printed to stderr and an error
5792** code returned. Otherwise, if all specified arguments are present in
5793** the archive, SQLITE_OK is returned.
5794**
5795** This function strips any trailing '/' characters from each argument.
5796** This is consistent with the way the [tar] command seems to work on
5797** Linux.
5798*/
5799static int arCheckEntries(ArCommand *pAr){
5800  int rc = SQLITE_OK;
5801  if( pAr->nArg ){
5802    int i, j;
5803    sqlite3_stmt *pTest = 0;
5804
5805    shellPreparePrintf(pAr->db, &rc, &pTest,
5806        "SELECT name FROM %s WHERE name=$name",
5807        pAr->zSrcTable
5808    );
5809    j = sqlite3_bind_parameter_index(pTest, "$name");
5810    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
5811      char *z = pAr->azArg[i];
5812      int n = strlen30(z);
5813      int bOk = 0;
5814      while( n>0 && z[n-1]=='/' ) n--;
5815      z[n] = '\0';
5816      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
5817      if( SQLITE_ROW==sqlite3_step(pTest) ){
5818        bOk = 1;
5819      }
5820      shellReset(&rc, pTest);
5821      if( rc==SQLITE_OK && bOk==0 ){
5822        utf8_printf(stderr, "not found in archive: %s\n", z);
5823        rc = SQLITE_ERROR;
5824      }
5825    }
5826    shellFinalize(&rc, pTest);
5827  }
5828  return rc;
5829}
5830
5831/*
5832** Format a WHERE clause that can be used against the "sqlar" table to
5833** identify all archive members that match the command arguments held
5834** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
5835** The caller is responsible for eventually calling sqlite3_free() on
5836** any non-NULL (*pzWhere) value.
5837*/
5838static void arWhereClause(
5839  int *pRc,
5840  ArCommand *pAr,
5841  char **pzWhere                  /* OUT: New WHERE clause */
5842){
5843  char *zWhere = 0;
5844  if( *pRc==SQLITE_OK ){
5845    if( pAr->nArg==0 ){
5846      zWhere = sqlite3_mprintf("1");
5847    }else{
5848      int i;
5849      const char *zSep = "";
5850      for(i=0; i<pAr->nArg; i++){
5851        const char *z = pAr->azArg[i];
5852        zWhere = sqlite3_mprintf(
5853          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
5854          zWhere, zSep, z, strlen30(z)+1, z
5855        );
5856        if( zWhere==0 ){
5857          *pRc = SQLITE_NOMEM;
5858          break;
5859        }
5860        zSep = " OR ";
5861      }
5862    }
5863  }
5864  *pzWhere = zWhere;
5865}
5866
5867/*
5868** Implementation of .ar "lisT" command.
5869*/
5870static int arListCommand(ArCommand *pAr){
5871  const char *zSql = "SELECT %s FROM %s WHERE %s";
5872  const char *azCols[] = {
5873    "name",
5874    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
5875  };
5876
5877  char *zWhere = 0;
5878  sqlite3_stmt *pSql = 0;
5879  int rc;
5880
5881  rc = arCheckEntries(pAr);
5882  arWhereClause(&rc, pAr, &zWhere);
5883
5884  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
5885                     pAr->zSrcTable, zWhere);
5886  if( pAr->bDryRun ){
5887    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
5888  }else{
5889    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
5890      if( pAr->bVerbose ){
5891        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
5892            sqlite3_column_text(pSql, 0),
5893            sqlite3_column_int(pSql, 1),
5894            sqlite3_column_text(pSql, 2),
5895            sqlite3_column_text(pSql, 3)
5896        );
5897      }else{
5898        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
5899      }
5900    }
5901  }
5902  shellFinalize(&rc, pSql);
5903  sqlite3_free(zWhere);
5904  return rc;
5905}
5906
5907
5908/*
5909** Implementation of .ar "eXtract" command.
5910*/
5911static int arExtractCommand(ArCommand *pAr){
5912  const char *zSql1 =
5913    "SELECT "
5914    " ($dir || name),"
5915    " writefile(($dir || name), %s, mode, mtime) "
5916    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
5917    " AND name NOT GLOB '*..[/\\]*'";
5918
5919  const char *azExtraArg[] = {
5920    "sqlar_uncompress(data, sz)",
5921    "data"
5922  };
5923
5924  sqlite3_stmt *pSql = 0;
5925  int rc = SQLITE_OK;
5926  char *zDir = 0;
5927  char *zWhere = 0;
5928  int i, j;
5929
5930  /* If arguments are specified, check that they actually exist within
5931  ** the archive before proceeding. And formulate a WHERE clause to
5932  ** match them.  */
5933  rc = arCheckEntries(pAr);
5934  arWhereClause(&rc, pAr, &zWhere);
5935
5936  if( rc==SQLITE_OK ){
5937    if( pAr->zDir ){
5938      zDir = sqlite3_mprintf("%s/", pAr->zDir);
5939    }else{
5940      zDir = sqlite3_mprintf("");
5941    }
5942    if( zDir==0 ) rc = SQLITE_NOMEM;
5943  }
5944
5945  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
5946      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
5947  );
5948
5949  if( rc==SQLITE_OK ){
5950    j = sqlite3_bind_parameter_index(pSql, "$dir");
5951    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
5952
5953    /* Run the SELECT statement twice. The first time, writefile() is called
5954    ** for all archive members that should be extracted. The second time,
5955    ** only for the directories. This is because the timestamps for
5956    ** extracted directories must be reset after they are populated (as
5957    ** populating them changes the timestamp).  */
5958    for(i=0; i<2; i++){
5959      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
5960      sqlite3_bind_int(pSql, j, i);
5961      if( pAr->bDryRun ){
5962        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
5963      }else{
5964        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
5965          if( i==0 && pAr->bVerbose ){
5966            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
5967          }
5968        }
5969      }
5970      shellReset(&rc, pSql);
5971    }
5972    shellFinalize(&rc, pSql);
5973  }
5974
5975  sqlite3_free(zDir);
5976  sqlite3_free(zWhere);
5977  return rc;
5978}
5979
5980/*
5981** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
5982*/
5983static int arExecSql(ArCommand *pAr, const char *zSql){
5984  int rc;
5985  if( pAr->bDryRun ){
5986    utf8_printf(pAr->p->out, "%s\n", zSql);
5987    rc = SQLITE_OK;
5988  }else{
5989    char *zErr = 0;
5990    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
5991    if( zErr ){
5992      utf8_printf(stdout, "ERROR: %s\n", zErr);
5993      sqlite3_free(zErr);
5994    }
5995  }
5996  return rc;
5997}
5998
5999
6000/*
6001** Implementation of .ar "create", "insert", and "update" commands.
6002**
6003**     create    ->     Create a new SQL archive
6004**     insert    ->     Insert or reinsert all files listed
6005**     update    ->     Insert files that have changed or that were not
6006**                      previously in the archive
6007**
6008** Create the "sqlar" table in the database if it does not already exist.
6009** Then add each file in the azFile[] array to the archive. Directories
6010** are added recursively. If argument bVerbose is non-zero, a message is
6011** printed on stdout for each file archived.
6012**
6013** The create command is the same as update, except that it drops
6014** any existing "sqlar" table before beginning.  The "insert" command
6015** always overwrites every file named on the command-line, where as
6016** "update" only overwrites if the size or mtime or mode has changed.
6017*/
6018static int arCreateOrUpdateCommand(
6019  ArCommand *pAr,                 /* Command arguments and options */
6020  int bUpdate,                    /* true for a --create. */
6021  int bOnlyIfChanged              /* Only update if file has changed */
6022){
6023  const char *zCreate =
6024      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6025      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6026      "  mode INT,               -- access permissions\n"
6027      "  mtime INT,              -- last modification time\n"
6028      "  sz INT,                 -- original file size\n"
6029      "  data BLOB               -- compressed content\n"
6030      ")";
6031  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6032  const char *zInsertFmt[2] = {
6033     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6034     "  SELECT\n"
6035     "    %s,\n"
6036     "    mode,\n"
6037     "    mtime,\n"
6038     "    CASE substr(lsmode(mode),1,1)\n"
6039     "      WHEN '-' THEN length(data)\n"
6040     "      WHEN 'd' THEN 0\n"
6041     "      ELSE -1 END,\n"
6042     "    sqlar_compress(data)\n"
6043     "  FROM fsdir(%Q,%Q) AS disk\n"
6044     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6045     ,
6046     "REPLACE INTO %s(name,mode,mtime,data)\n"
6047     "  SELECT\n"
6048     "    %s,\n"
6049     "    mode,\n"
6050     "    mtime,\n"
6051     "    data\n"
6052     "  FROM fsdir(%Q,%Q) AS disk\n"
6053     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6054  };
6055  int i;                          /* For iterating through azFile[] */
6056  int rc;                         /* Return code */
6057  const char *zTab = 0;           /* SQL table into which to insert */
6058  char *zSql;
6059  char zTemp[50];
6060  char *zExists = 0;
6061
6062  arExecSql(pAr, "PRAGMA page_size=512");
6063  rc = arExecSql(pAr, "SAVEPOINT ar;");
6064  if( rc!=SQLITE_OK ) return rc;
6065  zTemp[0] = 0;
6066  if( pAr->bZip ){
6067    /* Initialize the zipfile virtual table, if necessary */
6068    if( pAr->zFile ){
6069      sqlite3_uint64 r;
6070      sqlite3_randomness(sizeof(r),&r);
6071      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6072      zTab = zTemp;
6073      zSql = sqlite3_mprintf(
6074         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6075         zTab, pAr->zFile
6076      );
6077      rc = arExecSql(pAr, zSql);
6078      sqlite3_free(zSql);
6079    }else{
6080      zTab = "zip";
6081    }
6082  }else{
6083    /* Initialize the table for an SQLAR */
6084    zTab = "sqlar";
6085    if( bUpdate==0 ){
6086      rc = arExecSql(pAr, zDrop);
6087      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6088    }
6089    rc = arExecSql(pAr, zCreate);
6090  }
6091  if( bOnlyIfChanged ){
6092    zExists = sqlite3_mprintf(
6093      " AND NOT EXISTS("
6094          "SELECT 1 FROM %s AS mem"
6095          " WHERE mem.name=disk.name"
6096          " AND mem.mtime=disk.mtime"
6097          " AND mem.mode=disk.mode)", zTab);
6098  }else{
6099    zExists = sqlite3_mprintf("");
6100  }
6101  if( zExists==0 ) rc = SQLITE_NOMEM;
6102  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6103    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6104        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6105        pAr->azArg[i], pAr->zDir, zExists);
6106    rc = arExecSql(pAr, zSql2);
6107    sqlite3_free(zSql2);
6108  }
6109end_ar_transaction:
6110  if( rc!=SQLITE_OK ){
6111    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6112  }else{
6113    rc = arExecSql(pAr, "RELEASE ar;");
6114    if( pAr->bZip && pAr->zFile ){
6115      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6116      arExecSql(pAr, zSql);
6117      sqlite3_free(zSql);
6118    }
6119  }
6120  sqlite3_free(zExists);
6121  return rc;
6122}
6123
6124/*
6125** Implementation of ".ar" dot command.
6126*/
6127static int arDotCommand(
6128  ShellState *pState,          /* Current shell tool state */
6129  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6130  char **azArg,                /* Array of arguments passed to dot command */
6131  int nArg                     /* Number of entries in azArg[] */
6132){
6133  ArCommand cmd;
6134  int rc;
6135  memset(&cmd, 0, sizeof(cmd));
6136  cmd.fromCmdLine = fromCmdLine;
6137  rc = arParseCommand(azArg, nArg, &cmd);
6138  if( rc==SQLITE_OK ){
6139    int eDbType = SHELL_OPEN_UNSPEC;
6140    cmd.p = pState;
6141    cmd.db = pState->db;
6142    if( cmd.zFile ){
6143      eDbType = deduceDatabaseType(cmd.zFile, 1);
6144    }else{
6145      eDbType = pState->openMode;
6146    }
6147    if( eDbType==SHELL_OPEN_ZIPFILE ){
6148      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6149        if( cmd.zFile==0 ){
6150          cmd.zSrcTable = sqlite3_mprintf("zip");
6151        }else{
6152          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6153        }
6154      }
6155      cmd.bZip = 1;
6156    }else if( cmd.zFile ){
6157      int flags;
6158      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6159      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6160           || cmd.eCmd==AR_CMD_UPDATE ){
6161        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6162      }else{
6163        flags = SQLITE_OPEN_READONLY;
6164      }
6165      cmd.db = 0;
6166      if( cmd.bDryRun ){
6167        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6168             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6169      }
6170      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6171             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6172      if( rc!=SQLITE_OK ){
6173        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6174            cmd.zFile, sqlite3_errmsg(cmd.db)
6175        );
6176        goto end_ar_command;
6177      }
6178      sqlite3_fileio_init(cmd.db, 0, 0);
6179      sqlite3_sqlar_init(cmd.db, 0, 0);
6180      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6181                              shellPutsFunc, 0, 0);
6182
6183    }
6184    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6185      if( cmd.eCmd!=AR_CMD_CREATE
6186       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6187      ){
6188        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6189        rc = SQLITE_ERROR;
6190        goto end_ar_command;
6191      }
6192      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6193    }
6194
6195    switch( cmd.eCmd ){
6196      case AR_CMD_CREATE:
6197        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6198        break;
6199
6200      case AR_CMD_EXTRACT:
6201        rc = arExtractCommand(&cmd);
6202        break;
6203
6204      case AR_CMD_LIST:
6205        rc = arListCommand(&cmd);
6206        break;
6207
6208      case AR_CMD_HELP:
6209        arUsage(pState->out);
6210        break;
6211
6212      case AR_CMD_INSERT:
6213        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6214        break;
6215
6216      default:
6217        assert( cmd.eCmd==AR_CMD_UPDATE );
6218        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6219        break;
6220    }
6221  }
6222end_ar_command:
6223  if( cmd.db!=pState->db ){
6224    close_db(cmd.db);
6225  }
6226  sqlite3_free(cmd.zSrcTable);
6227
6228  return rc;
6229}
6230/* End of the ".archive" or ".ar" command logic
6231*******************************************************************************/
6232#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6233
6234#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6235/*
6236** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6237** Otherwise, the SQL statement or statements in zSql are executed using
6238** database connection db and the error code written to *pRc before
6239** this function returns.
6240*/
6241static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6242  int rc = *pRc;
6243  if( rc==SQLITE_OK ){
6244    char *zErr = 0;
6245    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6246    if( rc!=SQLITE_OK ){
6247      raw_printf(stderr, "SQL error: %s\n", zErr);
6248    }
6249    *pRc = rc;
6250  }
6251}
6252
6253/*
6254** Like shellExec(), except that zFmt is a printf() style format string.
6255*/
6256static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6257  char *z = 0;
6258  if( *pRc==SQLITE_OK ){
6259    va_list ap;
6260    va_start(ap, zFmt);
6261    z = sqlite3_vmprintf(zFmt, ap);
6262    va_end(ap);
6263    if( z==0 ){
6264      *pRc = SQLITE_NOMEM;
6265    }else{
6266      shellExec(db, pRc, z);
6267    }
6268    sqlite3_free(z);
6269  }
6270}
6271
6272/*
6273** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6274** Otherwise, an attempt is made to allocate, zero and return a pointer
6275** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6276** to SQLITE_NOMEM and NULL returned.
6277*/
6278static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6279  void *pRet = 0;
6280  if( *pRc==SQLITE_OK ){
6281    pRet = sqlite3_malloc64(nByte);
6282    if( pRet==0 ){
6283      *pRc = SQLITE_NOMEM;
6284    }else{
6285      memset(pRet, 0, nByte);
6286    }
6287  }
6288  return pRet;
6289}
6290
6291/*
6292** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6293** Otherwise, zFmt is treated as a printf() style string. The result of
6294** formatting it along with any trailing arguments is written into a
6295** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6296** It is the responsibility of the caller to eventually free this buffer
6297** using a call to sqlite3_free().
6298**
6299** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6300** pointer returned.
6301*/
6302static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6303  char *z = 0;
6304  if( *pRc==SQLITE_OK ){
6305    va_list ap;
6306    va_start(ap, zFmt);
6307    z = sqlite3_vmprintf(zFmt, ap);
6308    va_end(ap);
6309    if( z==0 ){
6310      *pRc = SQLITE_NOMEM;
6311    }
6312  }
6313  return z;
6314}
6315
6316/*
6317** When running the ".recover" command, each output table, and the special
6318** orphaned row table if it is required, is represented by an instance
6319** of the following struct.
6320*/
6321typedef struct RecoverTable RecoverTable;
6322struct RecoverTable {
6323  char *zQuoted;                  /* Quoted version of table name */
6324  int nCol;                       /* Number of columns in table */
6325  char **azlCol;                  /* Array of column lists */
6326  int iPk;                        /* Index of IPK column */
6327};
6328
6329/*
6330** Free a RecoverTable object allocated by recoverFindTable() or
6331** recoverOrphanTable().
6332*/
6333static void recoverFreeTable(RecoverTable *pTab){
6334  if( pTab ){
6335    sqlite3_free(pTab->zQuoted);
6336    if( pTab->azlCol ){
6337      int i;
6338      for(i=0; i<=pTab->nCol; i++){
6339        sqlite3_free(pTab->azlCol[i]);
6340      }
6341      sqlite3_free(pTab->azlCol);
6342    }
6343    sqlite3_free(pTab);
6344  }
6345}
6346
6347/*
6348** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6349** Otherwise, it allocates and returns a RecoverTable object based on the
6350** final four arguments passed to this function. It is the responsibility
6351** of the caller to eventually free the returned object using
6352** recoverFreeTable().
6353*/
6354static RecoverTable *recoverNewTable(
6355  int *pRc,                       /* IN/OUT: Error code */
6356  const char *zName,              /* Name of table */
6357  const char *zSql,               /* CREATE TABLE statement */
6358  int bIntkey,
6359  int nCol
6360){
6361  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6362  int rc = *pRc;
6363  RecoverTable *pTab = 0;
6364
6365  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6366  if( rc==SQLITE_OK ){
6367    int nSqlCol = 0;
6368    int bSqlIntkey = 0;
6369    sqlite3_stmt *pStmt = 0;
6370
6371    rc = sqlite3_open("", &dbtmp);
6372    if( rc==SQLITE_OK ){
6373      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6374                              shellIdQuote, 0, 0);
6375    }
6376    if( rc==SQLITE_OK ){
6377      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6378    }
6379    if( rc==SQLITE_OK ){
6380      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6381      if( rc==SQLITE_ERROR ){
6382        rc = SQLITE_OK;
6383        goto finished;
6384      }
6385    }
6386    shellPreparePrintf(dbtmp, &rc, &pStmt,
6387        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6388    );
6389    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6390      nSqlCol = sqlite3_column_int(pStmt, 0);
6391    }
6392    shellFinalize(&rc, pStmt);
6393
6394    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6395      goto finished;
6396    }
6397
6398    shellPreparePrintf(dbtmp, &rc, &pStmt,
6399      "SELECT ("
6400      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6401      ") FROM sqlite_master WHERE name = %Q", zName
6402    );
6403    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6404      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6405    }
6406    shellFinalize(&rc, pStmt);
6407
6408    if( bIntkey==bSqlIntkey ){
6409      int i;
6410      const char *zPk = "_rowid_";
6411      sqlite3_stmt *pPkFinder = 0;
6412
6413      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6414      ** set zPk to the name of the PK column, and pTab->iPk to the index
6415      ** of the column, where columns are 0-numbered from left to right.
6416      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6417      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6418      pTab->iPk = -2;
6419      if( bIntkey ){
6420        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6421          "SELECT cid, name FROM pragma_table_info(%Q) "
6422          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6423          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6424          , zName, zName
6425        );
6426        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6427          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6428          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6429        }
6430      }
6431
6432      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6433      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6434      pTab->nCol = nSqlCol;
6435
6436      if( bIntkey ){
6437        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6438      }else{
6439        pTab->azlCol[0] = shellMPrintf(&rc, "");
6440      }
6441      i = 1;
6442      shellPreparePrintf(dbtmp, &rc, &pStmt,
6443          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6444          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6445          "FROM pragma_table_info(%Q)",
6446          bIntkey ? ", " : "", pTab->iPk,
6447          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6448          zName
6449      );
6450      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6451        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6452        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6453        i++;
6454      }
6455      shellFinalize(&rc, pStmt);
6456
6457      shellFinalize(&rc, pPkFinder);
6458    }
6459  }
6460
6461 finished:
6462  sqlite3_close(dbtmp);
6463  *pRc = rc;
6464  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6465    recoverFreeTable(pTab);
6466    pTab = 0;
6467  }
6468  return pTab;
6469}
6470
6471/*
6472** This function is called to search the schema recovered from the
6473** sqlite_master table of the (possibly) corrupt database as part
6474** of a ".recover" command. Specifically, for a table with root page
6475** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6476** table must be a WITHOUT ROWID table, or if non-zero, not one of
6477** those.
6478**
6479** If a table is found, a (RecoverTable*) object is returned. Or, if
6480** no such table is found, but bIntkey is false and iRoot is the
6481** root page of an index in the recovered schema, then (*pbNoop) is
6482** set to true and NULL returned. Or, if there is no such table or
6483** index, NULL is returned and (*pbNoop) set to 0, indicating that
6484** the caller should write data to the orphans table.
6485*/
6486static RecoverTable *recoverFindTable(
6487  ShellState *pState,             /* Shell state object */
6488  int *pRc,                       /* IN/OUT: Error code */
6489  int iRoot,                      /* Root page of table */
6490  int bIntkey,                    /* True for an intkey table */
6491  int nCol,                       /* Number of columns in table */
6492  int *pbNoop                     /* OUT: True if iRoot is root of index */
6493){
6494  sqlite3_stmt *pStmt = 0;
6495  RecoverTable *pRet = 0;
6496  int bNoop = 0;
6497  const char *zSql = 0;
6498  const char *zName = 0;
6499
6500  /* Search the recovered schema for an object with root page iRoot. */
6501  shellPreparePrintf(pState->db, pRc, &pStmt,
6502      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6503  );
6504  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6505    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6506    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6507      bNoop = 1;
6508      break;
6509    }
6510    if( sqlite3_stricmp(zType, "table")==0 ){
6511      zName = (const char*)sqlite3_column_text(pStmt, 1);
6512      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6513      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6514      break;
6515    }
6516  }
6517
6518  shellFinalize(pRc, pStmt);
6519  *pbNoop = bNoop;
6520  return pRet;
6521}
6522
6523/*
6524** Return a RecoverTable object representing the orphans table.
6525*/
6526static RecoverTable *recoverOrphanTable(
6527  ShellState *pState,             /* Shell state object */
6528  int *pRc,                       /* IN/OUT: Error code */
6529  const char *zLostAndFound,      /* Base name for orphans table */
6530  int nCol                        /* Number of user data columns */
6531){
6532  RecoverTable *pTab = 0;
6533  if( nCol>=0 && *pRc==SQLITE_OK ){
6534    int i;
6535
6536    /* This block determines the name of the orphan table. The prefered
6537    ** name is zLostAndFound. But if that clashes with another name
6538    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
6539    ** and so on until a non-clashing name is found.  */
6540    int iTab = 0;
6541    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
6542    sqlite3_stmt *pTest = 0;
6543    shellPrepare(pState->db, pRc,
6544        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
6545    );
6546    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6547    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
6548      shellReset(pRc, pTest);
6549      sqlite3_free(zTab);
6550      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
6551      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6552    }
6553    shellFinalize(pRc, pTest);
6554
6555    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
6556    if( pTab ){
6557      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
6558      pTab->nCol = nCol;
6559      pTab->iPk = -2;
6560      if( nCol>0 ){
6561        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
6562        if( pTab->azlCol ){
6563          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
6564          for(i=nCol-1; i>=0; i--){
6565            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
6566          }
6567        }
6568      }
6569
6570      if( *pRc!=SQLITE_OK ){
6571        recoverFreeTable(pTab);
6572        pTab = 0;
6573      }else{
6574        raw_printf(pState->out,
6575            "CREATE TABLE %s(rootpgno INTEGER, "
6576            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
6577        );
6578        for(i=0; i<nCol; i++){
6579          raw_printf(pState->out, ", c%d", i);
6580        }
6581        raw_printf(pState->out, ");\n");
6582      }
6583    }
6584    sqlite3_free(zTab);
6585  }
6586  return pTab;
6587}
6588
6589/*
6590** This function is called to recover data from the database. A script
6591** to construct a new database containing all recovered data is output
6592** on stream pState->out.
6593*/
6594static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
6595  int rc = SQLITE_OK;
6596  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
6597  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
6598  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
6599  const char *zRecoveryDb = "";   /* Name of "recovery" database */
6600  const char *zLostAndFound = "lost_and_found";
6601  int i;
6602  int nOrphan = -1;
6603  RecoverTable *pOrphan = 0;
6604
6605  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
6606  int bRowids = 1;                /* 0 if --no-rowids */
6607  for(i=1; i<nArg; i++){
6608    char *z = azArg[i];
6609    int n;
6610    if( z[0]=='-' && z[1]=='-' ) z++;
6611    n = strlen30(z);
6612    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
6613      bFreelist = 0;
6614    }else
6615    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
6616      i++;
6617      zRecoveryDb = azArg[i];
6618    }else
6619    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
6620      i++;
6621      zLostAndFound = azArg[i];
6622    }else
6623    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
6624      bRowids = 0;
6625    }
6626    else{
6627      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
6628      showHelp(pState->out, azArg[0]);
6629      return 1;
6630    }
6631  }
6632
6633  shellExecPrintf(pState->db, &rc,
6634    /* Attach an in-memory database named 'recovery'. Create an indexed
6635    ** cache of the sqlite_dbptr virtual table. */
6636    "PRAGMA writable_schema = on;"
6637    "ATTACH %Q AS recovery;"
6638    "DROP TABLE IF EXISTS recovery.dbptr;"
6639    "DROP TABLE IF EXISTS recovery.freelist;"
6640    "DROP TABLE IF EXISTS recovery.map;"
6641    "DROP TABLE IF EXISTS recovery.schema;"
6642    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
6643  );
6644
6645  if( bFreelist ){
6646    shellExec(pState->db, &rc,
6647      "WITH trunk(pgno) AS ("
6648      "  SELECT shell_int32("
6649      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
6650      "      WHERE x>0"
6651      "    UNION"
6652      "  SELECT shell_int32("
6653      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
6654      "      FROM trunk WHERE x>0"
6655      "),"
6656      "freelist(data, n, freepgno) AS ("
6657      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
6658      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
6659      "    UNION ALL"
6660      "  SELECT data, n-1, shell_int32(data, 2+n) "
6661      "      FROM freelist WHERE n>=0"
6662      ")"
6663      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
6664    );
6665  }
6666
6667  /* If this is an auto-vacuum database, add all pointer-map pages to
6668  ** the freelist table. Do this regardless of whether or not
6669  ** --freelist-corrupt was specified.  */
6670  shellExec(pState->db, &rc,
6671    "WITH ptrmap(pgno) AS ("
6672    "  SELECT 2 WHERE shell_int32("
6673    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
6674    "  )"
6675    "    UNION ALL "
6676    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
6677    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
6678    ")"
6679    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
6680  );
6681
6682  shellExec(pState->db, &rc,
6683    "CREATE TABLE recovery.dbptr("
6684    "      pgno, child, PRIMARY KEY(child, pgno)"
6685    ") WITHOUT ROWID;"
6686    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
6687    "    SELECT * FROM sqlite_dbptr"
6688    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
6689
6690    /* Delete any pointer to page 1. This ensures that page 1 is considered
6691    ** a root page, regardless of how corrupt the db is. */
6692    "DELETE FROM recovery.dbptr WHERE child = 1;"
6693
6694    /* Delete all pointers to any pages that have more than one pointer
6695    ** to them. Such pages will be treated as root pages when recovering
6696    ** data.  */
6697    "DELETE FROM recovery.dbptr WHERE child IN ("
6698    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
6699    ");"
6700
6701    /* Create the "map" table that will (eventually) contain instructions
6702    ** for dealing with each page in the db that contains one or more
6703    ** records. */
6704    "CREATE TABLE recovery.map("
6705      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
6706    ");"
6707
6708    /* Populate table [map]. If there are circular loops of pages in the
6709    ** database, the following adds all pages in such a loop to the map
6710    ** as individual root pages. This could be handled better.  */
6711    "WITH pages(i, maxlen) AS ("
6712    "  SELECT page_count, ("
6713    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
6714    "  ) FROM pragma_page_count WHERE page_count>0"
6715    "    UNION ALL"
6716    "  SELECT i-1, ("
6717    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
6718    "  ) FROM pages WHERE i>=2"
6719    ")"
6720    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
6721    "  SELECT i, maxlen, NULL, ("
6722    "    WITH p(orig, pgno, parent) AS ("
6723    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
6724    "        UNION "
6725    "      SELECT i, p.parent, "
6726    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
6727    "    )"
6728    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
6729    ") "
6730    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
6731    "UPDATE recovery.map AS o SET intkey = ("
6732    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
6733    ");"
6734
6735    /* Extract data from page 1 and any linked pages into table
6736    ** recovery.schema. With the same schema as an sqlite_master table.  */
6737    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
6738    "INSERT INTO recovery.schema SELECT "
6739    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
6740    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
6741    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
6742    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
6743    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
6744    "FROM sqlite_dbdata WHERE pgno IN ("
6745    "  SELECT pgno FROM recovery.map WHERE root=1"
6746    ")"
6747    "GROUP BY pgno, cell;"
6748    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
6749  );
6750
6751  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
6752  ** CREATE TABLE statements that extracted from the existing schema.  */
6753  if( rc==SQLITE_OK ){
6754    sqlite3_stmt *pStmt = 0;
6755    /* ".recover" might output content in an order which causes immediate
6756    ** foreign key constraints to be violated. So disable foreign-key
6757    ** constraint enforcement to prevent problems when running the output
6758    ** script. */
6759    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
6760    raw_printf(pState->out, "BEGIN;\n");
6761    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
6762    shellPrepare(pState->db, &rc,
6763        "SELECT sql FROM recovery.schema "
6764        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
6765    );
6766    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6767      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
6768      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
6769          &zCreateTable[12]
6770      );
6771    }
6772    shellFinalize(&rc, pStmt);
6773  }
6774
6775  /* Figure out if an orphan table will be required. And if so, how many
6776  ** user columns it should contain */
6777  shellPrepare(pState->db, &rc,
6778      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
6779      , &pLoop
6780  );
6781  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
6782    nOrphan = sqlite3_column_int(pLoop, 0);
6783  }
6784  shellFinalize(&rc, pLoop);
6785  pLoop = 0;
6786
6787  shellPrepare(pState->db, &rc,
6788      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
6789  );
6790
6791  shellPrepare(pState->db, &rc,
6792      "SELECT max(field), group_concat(shell_escape_crnl(quote"
6793      "(case when (? AND field<0) then NULL else value end)"
6794      "), ', ')"
6795      ", min(field) "
6796      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
6797      "GROUP BY cell", &pCells
6798  );
6799
6800  /* Loop through each root page. */
6801  shellPrepare(pState->db, &rc,
6802      "SELECT root, intkey, max(maxlen) FROM recovery.map"
6803      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
6804      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
6805      ")", &pLoop
6806  );
6807  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
6808    int iRoot = sqlite3_column_int(pLoop, 0);
6809    int bIntkey = sqlite3_column_int(pLoop, 1);
6810    int nCol = sqlite3_column_int(pLoop, 2);
6811    int bNoop = 0;
6812    RecoverTable *pTab;
6813
6814    assert( bIntkey==0 || bIntkey==1 );
6815    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
6816    if( bNoop || rc ) continue;
6817    if( pTab==0 ){
6818      if( pOrphan==0 ){
6819        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
6820      }
6821      pTab = pOrphan;
6822      if( pTab==0 ) break;
6823    }
6824
6825    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
6826      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
6827    }
6828    sqlite3_bind_int(pPages, 1, iRoot);
6829    if( bRowids==0 && pTab->iPk<0 ){
6830      sqlite3_bind_int(pCells, 1, 1);
6831    }else{
6832      sqlite3_bind_int(pCells, 1, 0);
6833    }
6834    sqlite3_bind_int(pCells, 3, pTab->iPk);
6835
6836    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
6837      int iPgno = sqlite3_column_int(pPages, 0);
6838      sqlite3_bind_int(pCells, 2, iPgno);
6839      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
6840        int nField = sqlite3_column_int(pCells, 0);
6841        int iMin = sqlite3_column_int(pCells, 2);
6842        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
6843
6844        RecoverTable *pTab2 = pTab;
6845        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
6846          if( pOrphan==0 ){
6847            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
6848          }
6849          pTab2 = pOrphan;
6850          if( pTab2==0 ) break;
6851        }
6852
6853        nField = nField+1;
6854        if( pTab2==pOrphan ){
6855          raw_printf(pState->out,
6856              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
6857              pTab2->zQuoted, iRoot, iPgno, nField,
6858              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
6859          );
6860        }else{
6861          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
6862              pTab2->zQuoted, pTab2->azlCol[nField], zVal
6863          );
6864        }
6865      }
6866      shellReset(&rc, pCells);
6867    }
6868    shellReset(&rc, pPages);
6869    if( pTab!=pOrphan ) recoverFreeTable(pTab);
6870  }
6871  shellFinalize(&rc, pLoop);
6872  shellFinalize(&rc, pPages);
6873  shellFinalize(&rc, pCells);
6874  recoverFreeTable(pOrphan);
6875
6876  /* The rest of the schema */
6877  if( rc==SQLITE_OK ){
6878    sqlite3_stmt *pStmt = 0;
6879    shellPrepare(pState->db, &rc,
6880        "SELECT sql, name FROM recovery.schema "
6881        "WHERE sql NOT LIKE 'create table%'", &pStmt
6882    );
6883    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6884      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
6885      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
6886        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
6887        char *zPrint = shellMPrintf(&rc,
6888          "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)",
6889          zName, zName, zSql
6890        );
6891        raw_printf(pState->out, "%s;\n", zPrint);
6892        sqlite3_free(zPrint);
6893      }else{
6894        raw_printf(pState->out, "%s;\n", zSql);
6895      }
6896    }
6897    shellFinalize(&rc, pStmt);
6898  }
6899
6900  if( rc==SQLITE_OK ){
6901    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
6902    raw_printf(pState->out, "COMMIT;\n");
6903  }
6904  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
6905  return rc;
6906}
6907#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
6908
6909
6910/*
6911** If an input line begins with "." then invoke this routine to
6912** process that line.
6913**
6914** Return 1 on error, 2 to exit, and 0 otherwise.
6915*/
6916static int do_meta_command(char *zLine, ShellState *p){
6917  int h = 1;
6918  int nArg = 0;
6919  int n, c;
6920  int rc = 0;
6921  char *azArg[52];
6922
6923#ifndef SQLITE_OMIT_VIRTUALTABLE
6924  if( p->expert.pExpert ){
6925    expertFinish(p, 1, 0);
6926  }
6927#endif
6928
6929  /* Parse the input line into tokens.
6930  */
6931  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
6932    while( IsSpace(zLine[h]) ){ h++; }
6933    if( zLine[h]==0 ) break;
6934    if( zLine[h]=='\'' || zLine[h]=='"' ){
6935      int delim = zLine[h++];
6936      azArg[nArg++] = &zLine[h];
6937      while( zLine[h] && zLine[h]!=delim ){
6938        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
6939        h++;
6940      }
6941      if( zLine[h]==delim ){
6942        zLine[h++] = 0;
6943      }
6944      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
6945    }else{
6946      azArg[nArg++] = &zLine[h];
6947      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
6948      if( zLine[h] ) zLine[h++] = 0;
6949      resolve_backslashes(azArg[nArg-1]);
6950    }
6951  }
6952  azArg[nArg] = 0;
6953
6954  /* Process the input line.
6955  */
6956  if( nArg==0 ) return 0; /* no tokens, no error */
6957  n = strlen30(azArg[0]);
6958  c = azArg[0][0];
6959  clearTempFile(p);
6960
6961#ifndef SQLITE_OMIT_AUTHORIZATION
6962  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
6963    if( nArg!=2 ){
6964      raw_printf(stderr, "Usage: .auth ON|OFF\n");
6965      rc = 1;
6966      goto meta_command_exit;
6967    }
6968    open_db(p, 0);
6969    if( booleanValue(azArg[1]) ){
6970      sqlite3_set_authorizer(p->db, shellAuth, p);
6971    }else{
6972      sqlite3_set_authorizer(p->db, 0, 0);
6973    }
6974  }else
6975#endif
6976
6977#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6978  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
6979    open_db(p, 0);
6980    rc = arDotCommand(p, 0, azArg, nArg);
6981  }else
6982#endif
6983
6984  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
6985   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
6986  ){
6987    const char *zDestFile = 0;
6988    const char *zDb = 0;
6989    sqlite3 *pDest;
6990    sqlite3_backup *pBackup;
6991    int j;
6992    int bAsync = 0;
6993    const char *zVfs = 0;
6994    for(j=1; j<nArg; j++){
6995      const char *z = azArg[j];
6996      if( z[0]=='-' ){
6997        if( z[1]=='-' ) z++;
6998        if( strcmp(z, "-append")==0 ){
6999          zVfs = "apndvfs";
7000        }else
7001        if( strcmp(z, "-async")==0 ){
7002          bAsync = 1;
7003        }else
7004        {
7005          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7006          return 1;
7007        }
7008      }else if( zDestFile==0 ){
7009        zDestFile = azArg[j];
7010      }else if( zDb==0 ){
7011        zDb = zDestFile;
7012        zDestFile = azArg[j];
7013      }else{
7014        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7015        return 1;
7016      }
7017    }
7018    if( zDestFile==0 ){
7019      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7020      return 1;
7021    }
7022    if( zDb==0 ) zDb = "main";
7023    rc = sqlite3_open_v2(zDestFile, &pDest,
7024                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7025    if( rc!=SQLITE_OK ){
7026      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7027      close_db(pDest);
7028      return 1;
7029    }
7030    if( bAsync ){
7031      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7032                   0, 0, 0);
7033    }
7034    open_db(p, 0);
7035    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7036    if( pBackup==0 ){
7037      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7038      close_db(pDest);
7039      return 1;
7040    }
7041    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7042    sqlite3_backup_finish(pBackup);
7043    if( rc==SQLITE_DONE ){
7044      rc = 0;
7045    }else{
7046      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7047      rc = 1;
7048    }
7049    close_db(pDest);
7050  }else
7051
7052  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7053    if( nArg==2 ){
7054      bail_on_error = booleanValue(azArg[1]);
7055    }else{
7056      raw_printf(stderr, "Usage: .bail on|off\n");
7057      rc = 1;
7058    }
7059  }else
7060
7061  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7062    if( nArg==2 ){
7063      if( booleanValue(azArg[1]) ){
7064        setBinaryMode(p->out, 1);
7065      }else{
7066        setTextMode(p->out, 1);
7067      }
7068    }else{
7069      raw_printf(stderr, "Usage: .binary on|off\n");
7070      rc = 1;
7071    }
7072  }else
7073
7074  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7075    if( nArg==2 ){
7076#if defined(_WIN32) || defined(WIN32)
7077      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7078      rc = !SetCurrentDirectoryW(z);
7079      sqlite3_free(z);
7080#else
7081      rc = chdir(azArg[1]);
7082#endif
7083      if( rc ){
7084        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7085        rc = 1;
7086      }
7087    }else{
7088      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7089      rc = 1;
7090    }
7091  }else
7092
7093  /* The undocumented ".breakpoint" command causes a call to the no-op
7094  ** routine named test_breakpoint().
7095  */
7096  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7097    test_breakpoint();
7098  }else
7099
7100  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7101    if( nArg==2 ){
7102      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7103    }else{
7104      raw_printf(stderr, "Usage: .changes on|off\n");
7105      rc = 1;
7106    }
7107  }else
7108
7109  /* Cancel output redirection, if it is currently set (by .testcase)
7110  ** Then read the content of the testcase-out.txt file and compare against
7111  ** azArg[1].  If there are differences, report an error and exit.
7112  */
7113  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7114    char *zRes = 0;
7115    output_reset(p);
7116    if( nArg!=2 ){
7117      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7118      rc = 2;
7119    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7120      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7121      rc = 2;
7122    }else if( testcase_glob(azArg[1],zRes)==0 ){
7123      utf8_printf(stderr,
7124                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7125                 p->zTestcase, azArg[1], zRes);
7126      rc = 1;
7127    }else{
7128      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7129      p->nCheck++;
7130    }
7131    sqlite3_free(zRes);
7132  }else
7133
7134  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7135    if( nArg==2 ){
7136      tryToClone(p, azArg[1]);
7137    }else{
7138      raw_printf(stderr, "Usage: .clone FILENAME\n");
7139      rc = 1;
7140    }
7141  }else
7142
7143  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7144    ShellState data;
7145    char *zErrMsg = 0;
7146    open_db(p, 0);
7147    memcpy(&data, p, sizeof(data));
7148    data.showHeader = 0;
7149    data.cMode = data.mode = MODE_List;
7150    sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
7151    data.cnt = 0;
7152    sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
7153                 callback, &data, &zErrMsg);
7154    if( zErrMsg ){
7155      utf8_printf(stderr,"Error: %s\n", zErrMsg);
7156      sqlite3_free(zErrMsg);
7157      rc = 1;
7158    }
7159  }else
7160
7161  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7162    static const struct DbConfigChoices {
7163      const char *zName;
7164      int op;
7165    } aDbConfig[] = {
7166        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7167        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7168        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7169        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7170        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7171        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7172        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7173        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7174        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7175        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7176        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7177        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7178        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7179        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7180        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7181        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7182    };
7183    int ii, v;
7184    open_db(p, 0);
7185    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7186      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7187      if( nArg>=3 ){
7188        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7189      }
7190      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7191      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7192      if( nArg>1 ) break;
7193    }
7194    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7195      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7196      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7197    }
7198  }else
7199
7200  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7201    rc = shell_dbinfo_command(p, nArg, azArg);
7202  }else
7203
7204#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7205  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7206    open_db(p, 0);
7207    rc = recoverDatabaseCmd(p, nArg, azArg);
7208  }else
7209#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7210
7211  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7212    const char *zLike = 0;
7213    int i;
7214    int savedShowHeader = p->showHeader;
7215    int savedShellFlags = p->shellFlgs;
7216    ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
7217    for(i=1; i<nArg; i++){
7218      if( azArg[i][0]=='-' ){
7219        const char *z = azArg[i]+1;
7220        if( z[0]=='-' ) z++;
7221        if( strcmp(z,"preserve-rowids")==0 ){
7222#ifdef SQLITE_OMIT_VIRTUALTABLE
7223          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7224                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7225          rc = 1;
7226          goto meta_command_exit;
7227#else
7228          ShellSetFlag(p, SHFLG_PreserveRowid);
7229#endif
7230        }else
7231        if( strcmp(z,"newlines")==0 ){
7232          ShellSetFlag(p, SHFLG_Newlines);
7233        }else
7234        {
7235          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7236          rc = 1;
7237          goto meta_command_exit;
7238        }
7239      }else if( zLike ){
7240        raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
7241                           "?--newlines? ?LIKE-PATTERN?\n");
7242        rc = 1;
7243        goto meta_command_exit;
7244      }else{
7245        zLike = azArg[i];
7246      }
7247    }
7248
7249    open_db(p, 0);
7250
7251    /* When playing back a "dump", the content might appear in an order
7252    ** which causes immediate foreign key constraints to be violated.
7253    ** So disable foreign-key constraint enforcement to prevent problems. */
7254    raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7255    raw_printf(p->out, "BEGIN TRANSACTION;\n");
7256    p->writableSchema = 0;
7257    p->showHeader = 0;
7258    /* Set writable_schema=ON since doing so forces SQLite to initialize
7259    ** as much of the schema as it can even if the sqlite_master table is
7260    ** corrupt. */
7261    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7262    p->nErr = 0;
7263    if( zLike==0 ){
7264      run_schema_dump_query(p,
7265        "SELECT name, type, sql FROM sqlite_master "
7266        "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
7267      );
7268      run_schema_dump_query(p,
7269        "SELECT name, type, sql FROM sqlite_master "
7270        "WHERE name=='sqlite_sequence'"
7271      );
7272      run_table_dump_query(p,
7273        "SELECT sql FROM sqlite_master "
7274        "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
7275      );
7276    }else{
7277      char *zSql;
7278      zSql = sqlite3_mprintf(
7279        "SELECT name, type, sql FROM sqlite_master "
7280        "WHERE tbl_name LIKE %Q AND type=='table'"
7281        "  AND sql NOT NULL", zLike);
7282      run_schema_dump_query(p,zSql);
7283      sqlite3_free(zSql);
7284      zSql = sqlite3_mprintf(
7285        "SELECT sql FROM sqlite_master "
7286        "WHERE sql NOT NULL"
7287        "  AND type IN ('index','trigger','view')"
7288        "  AND tbl_name LIKE %Q", zLike);
7289      run_table_dump_query(p, zSql, 0);
7290      sqlite3_free(zSql);
7291    }
7292    if( p->writableSchema ){
7293      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7294      p->writableSchema = 0;
7295    }
7296    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7297    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7298    raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7299    p->showHeader = savedShowHeader;
7300    p->shellFlgs = savedShellFlags;
7301  }else
7302
7303  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7304    if( nArg==2 ){
7305      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7306    }else{
7307      raw_printf(stderr, "Usage: .echo on|off\n");
7308      rc = 1;
7309    }
7310  }else
7311
7312  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7313    if( nArg==2 ){
7314      p->autoEQPtest = 0;
7315      if( p->autoEQPtrace ){
7316        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7317        p->autoEQPtrace = 0;
7318      }
7319      if( strcmp(azArg[1],"full")==0 ){
7320        p->autoEQP = AUTOEQP_full;
7321      }else if( strcmp(azArg[1],"trigger")==0 ){
7322        p->autoEQP = AUTOEQP_trigger;
7323#ifdef SQLITE_DEBUG
7324      }else if( strcmp(azArg[1],"test")==0 ){
7325        p->autoEQP = AUTOEQP_on;
7326        p->autoEQPtest = 1;
7327      }else if( strcmp(azArg[1],"trace")==0 ){
7328        p->autoEQP = AUTOEQP_full;
7329        p->autoEQPtrace = 1;
7330        open_db(p, 0);
7331        sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
7332        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7333#endif
7334      }else{
7335        p->autoEQP = (u8)booleanValue(azArg[1]);
7336      }
7337    }else{
7338      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7339      rc = 1;
7340    }
7341  }else
7342
7343  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7344    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7345    rc = 2;
7346  }else
7347
7348  /* The ".explain" command is automatic now.  It is largely pointless.  It
7349  ** retained purely for backwards compatibility */
7350  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7351    int val = 1;
7352    if( nArg>=2 ){
7353      if( strcmp(azArg[1],"auto")==0 ){
7354        val = 99;
7355      }else{
7356        val =  booleanValue(azArg[1]);
7357      }
7358    }
7359    if( val==1 && p->mode!=MODE_Explain ){
7360      p->normalMode = p->mode;
7361      p->mode = MODE_Explain;
7362      p->autoExplain = 0;
7363    }else if( val==0 ){
7364      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7365      p->autoExplain = 0;
7366    }else if( val==99 ){
7367      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7368      p->autoExplain = 1;
7369    }
7370  }else
7371
7372#ifndef SQLITE_OMIT_VIRTUALTABLE
7373  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7374    open_db(p, 0);
7375    expertDotCommand(p, azArg, nArg);
7376  }else
7377#endif
7378
7379  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7380    static const struct {
7381       const char *zCtrlName;   /* Name of a test-control option */
7382       int ctrlCode;            /* Integer code for that option */
7383       const char *zUsage;      /* Usage notes */
7384    } aCtrl[] = {
7385      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7386      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7387   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7388      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7389      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7390   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7391      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7392      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7393      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7394    };
7395    int filectrl = -1;
7396    int iCtrl = -1;
7397    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7398    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7399    int n2, i;
7400    const char *zCmd = 0;
7401
7402    open_db(p, 0);
7403    zCmd = nArg>=2 ? azArg[1] : "help";
7404
7405    /* The argument can optionally begin with "-" or "--" */
7406    if( zCmd[0]=='-' && zCmd[1] ){
7407      zCmd++;
7408      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7409    }
7410
7411    /* --help lists all file-controls */
7412    if( strcmp(zCmd,"help")==0 ){
7413      utf8_printf(p->out, "Available file-controls:\n");
7414      for(i=0; i<ArraySize(aCtrl); i++){
7415        utf8_printf(p->out, "  .filectrl %s %s\n",
7416                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7417      }
7418      rc = 1;
7419      goto meta_command_exit;
7420    }
7421
7422    /* convert filectrl text option to value. allow any unique prefix
7423    ** of the option name, or a numerical value. */
7424    n2 = strlen30(zCmd);
7425    for(i=0; i<ArraySize(aCtrl); i++){
7426      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7427        if( filectrl<0 ){
7428          filectrl = aCtrl[i].ctrlCode;
7429          iCtrl = i;
7430        }else{
7431          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7432                              "Use \".filectrl --help\" for help\n", zCmd);
7433          rc = 1;
7434          goto meta_command_exit;
7435        }
7436      }
7437    }
7438    if( filectrl<0 ){
7439      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7440                         "Use \".filectrl --help\" for help\n", zCmd);
7441    }else{
7442      switch(filectrl){
7443        case SQLITE_FCNTL_SIZE_LIMIT: {
7444          if( nArg!=2 && nArg!=3 ) break;
7445          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7446          sqlite3_file_control(p->db, 0, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7447          isOk = 1;
7448          break;
7449        }
7450        case SQLITE_FCNTL_LOCK_TIMEOUT:
7451        case SQLITE_FCNTL_CHUNK_SIZE: {
7452          int x;
7453          if( nArg!=3 ) break;
7454          x = (int)integerValue(azArg[2]);
7455          sqlite3_file_control(p->db, 0, filectrl, &x);
7456          isOk = 2;
7457          break;
7458        }
7459        case SQLITE_FCNTL_PERSIST_WAL:
7460        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7461          int x;
7462          if( nArg!=2 && nArg!=3 ) break;
7463          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7464          sqlite3_file_control(p->db, 0, filectrl, &x);
7465          iRes = x;
7466          isOk = 1;
7467          break;
7468        }
7469        case SQLITE_FCNTL_HAS_MOVED: {
7470          int x;
7471          if( nArg!=2 ) break;
7472          sqlite3_file_control(p->db, 0, filectrl, &x);
7473          iRes = x;
7474          isOk = 1;
7475          break;
7476        }
7477        case SQLITE_FCNTL_TEMPFILENAME: {
7478          char *z = 0;
7479          if( nArg!=2 ) break;
7480          sqlite3_file_control(p->db, 0, filectrl, &z);
7481          if( z ){
7482            utf8_printf(p->out, "%s\n", z);
7483            sqlite3_free(z);
7484          }
7485          isOk = 2;
7486          break;
7487        }
7488      }
7489    }
7490    if( isOk==0 && iCtrl>=0 ){
7491      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
7492      rc = 1;
7493    }else if( isOk==1 ){
7494      char zBuf[100];
7495      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
7496      raw_printf(p->out, "%s\n", zBuf);
7497    }
7498  }else
7499
7500  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
7501    ShellState data;
7502    char *zErrMsg = 0;
7503    int doStats = 0;
7504    memcpy(&data, p, sizeof(data));
7505    data.showHeader = 0;
7506    data.cMode = data.mode = MODE_Semi;
7507    if( nArg==2 && optionMatch(azArg[1], "indent") ){
7508      data.cMode = data.mode = MODE_Pretty;
7509      nArg = 1;
7510    }
7511    if( nArg!=1 ){
7512      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
7513      rc = 1;
7514      goto meta_command_exit;
7515    }
7516    open_db(p, 0);
7517    rc = sqlite3_exec(p->db,
7518       "SELECT sql FROM"
7519       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
7520       "     FROM sqlite_master UNION ALL"
7521       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
7522       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
7523       "ORDER BY rowid",
7524       callback, &data, &zErrMsg
7525    );
7526    if( rc==SQLITE_OK ){
7527      sqlite3_stmt *pStmt;
7528      rc = sqlite3_prepare_v2(p->db,
7529               "SELECT rowid FROM sqlite_master"
7530               " WHERE name GLOB 'sqlite_stat[134]'",
7531               -1, &pStmt, 0);
7532      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
7533      sqlite3_finalize(pStmt);
7534    }
7535    if( doStats==0 ){
7536      raw_printf(p->out, "/* No STAT tables available */\n");
7537    }else{
7538      raw_printf(p->out, "ANALYZE sqlite_master;\n");
7539      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
7540                   callback, &data, &zErrMsg);
7541      data.cMode = data.mode = MODE_Insert;
7542      data.zDestTable = "sqlite_stat1";
7543      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
7544      data.zDestTable = "sqlite_stat4";
7545      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
7546      raw_printf(p->out, "ANALYZE sqlite_master;\n");
7547    }
7548  }else
7549
7550  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
7551    if( nArg==2 ){
7552      p->showHeader = booleanValue(azArg[1]);
7553    }else{
7554      raw_printf(stderr, "Usage: .headers on|off\n");
7555      rc = 1;
7556    }
7557  }else
7558
7559  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
7560    if( nArg>=2 ){
7561      n = showHelp(p->out, azArg[1]);
7562      if( n==0 ){
7563        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
7564      }
7565    }else{
7566      showHelp(p->out, 0);
7567    }
7568  }else
7569
7570  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
7571    char *zTable;               /* Insert data into this table */
7572    char *zFile;                /* Name of file to extra content from */
7573    sqlite3_stmt *pStmt = NULL; /* A statement */
7574    int nCol;                   /* Number of columns in the table */
7575    int nByte;                  /* Number of bytes in an SQL string */
7576    int i, j;                   /* Loop counters */
7577    int needCommit;             /* True to COMMIT or ROLLBACK at end */
7578    int nSep;                   /* Number of bytes in p->colSeparator[] */
7579    char *zSql;                 /* An SQL statement */
7580    ImportCtx sCtx;             /* Reader context */
7581    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
7582    int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
7583
7584    if( nArg!=3 ){
7585      raw_printf(stderr, "Usage: .import FILE TABLE\n");
7586      goto meta_command_exit;
7587    }
7588    zFile = azArg[1];
7589    zTable = azArg[2];
7590    seenInterrupt = 0;
7591    memset(&sCtx, 0, sizeof(sCtx));
7592    open_db(p, 0);
7593    nSep = strlen30(p->colSeparator);
7594    if( nSep==0 ){
7595      raw_printf(stderr,
7596                 "Error: non-null column separator required for import\n");
7597      return 1;
7598    }
7599    if( nSep>1 ){
7600      raw_printf(stderr, "Error: multi-character column separators not allowed"
7601                      " for import\n");
7602      return 1;
7603    }
7604    nSep = strlen30(p->rowSeparator);
7605    if( nSep==0 ){
7606      raw_printf(stderr, "Error: non-null row separator required for import\n");
7607      return 1;
7608    }
7609    if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
7610      /* When importing CSV (only), if the row separator is set to the
7611      ** default output row separator, change it to the default input
7612      ** row separator.  This avoids having to maintain different input
7613      ** and output row separators. */
7614      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
7615      nSep = strlen30(p->rowSeparator);
7616    }
7617    if( nSep>1 ){
7618      raw_printf(stderr, "Error: multi-character row separators not allowed"
7619                      " for import\n");
7620      return 1;
7621    }
7622    sCtx.zFile = zFile;
7623    sCtx.nLine = 1;
7624    if( sCtx.zFile[0]=='|' ){
7625#ifdef SQLITE_OMIT_POPEN
7626      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
7627      return 1;
7628#else
7629      sCtx.in = popen(sCtx.zFile+1, "r");
7630      sCtx.zFile = "<pipe>";
7631      xCloser = pclose;
7632#endif
7633    }else{
7634      sCtx.in = fopen(sCtx.zFile, "rb");
7635      xCloser = fclose;
7636    }
7637    if( p->mode==MODE_Ascii ){
7638      xRead = ascii_read_one_field;
7639    }else{
7640      xRead = csv_read_one_field;
7641    }
7642    if( sCtx.in==0 ){
7643      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
7644      return 1;
7645    }
7646    sCtx.cColSep = p->colSeparator[0];
7647    sCtx.cRowSep = p->rowSeparator[0];
7648    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
7649    if( zSql==0 ){
7650      xCloser(sCtx.in);
7651      shell_out_of_memory();
7652    }
7653    nByte = strlen30(zSql);
7654    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7655    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
7656    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
7657      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
7658      char cSep = '(';
7659      while( xRead(&sCtx) ){
7660        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
7661        cSep = ',';
7662        if( sCtx.cTerm!=sCtx.cColSep ) break;
7663      }
7664      if( cSep=='(' ){
7665        sqlite3_free(zCreate);
7666        sqlite3_free(sCtx.z);
7667        xCloser(sCtx.in);
7668        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
7669        return 1;
7670      }
7671      zCreate = sqlite3_mprintf("%z\n)", zCreate);
7672      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
7673      sqlite3_free(zCreate);
7674      if( rc ){
7675        utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
7676                sqlite3_errmsg(p->db));
7677        sqlite3_free(sCtx.z);
7678        xCloser(sCtx.in);
7679        return 1;
7680      }
7681      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7682    }
7683    sqlite3_free(zSql);
7684    if( rc ){
7685      if (pStmt) sqlite3_finalize(pStmt);
7686      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
7687      xCloser(sCtx.in);
7688      return 1;
7689    }
7690    nCol = sqlite3_column_count(pStmt);
7691    sqlite3_finalize(pStmt);
7692    pStmt = 0;
7693    if( nCol==0 ) return 0; /* no columns, no error */
7694    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
7695    if( zSql==0 ){
7696      xCloser(sCtx.in);
7697      shell_out_of_memory();
7698    }
7699    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
7700    j = strlen30(zSql);
7701    for(i=1; i<nCol; i++){
7702      zSql[j++] = ',';
7703      zSql[j++] = '?';
7704    }
7705    zSql[j++] = ')';
7706    zSql[j] = 0;
7707    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7708    sqlite3_free(zSql);
7709    if( rc ){
7710      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7711      if (pStmt) sqlite3_finalize(pStmt);
7712      xCloser(sCtx.in);
7713      return 1;
7714    }
7715    needCommit = sqlite3_get_autocommit(p->db);
7716    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
7717    do{
7718      int startLine = sCtx.nLine;
7719      for(i=0; i<nCol; i++){
7720        char *z = xRead(&sCtx);
7721        /*
7722        ** Did we reach end-of-file before finding any columns?
7723        ** If so, stop instead of NULL filling the remaining columns.
7724        */
7725        if( z==0 && i==0 ) break;
7726        /*
7727        ** Did we reach end-of-file OR end-of-line before finding any
7728        ** columns in ASCII mode?  If so, stop instead of NULL filling
7729        ** the remaining columns.
7730        */
7731        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
7732        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
7733        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
7734          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
7735                          "filling the rest with NULL\n",
7736                          sCtx.zFile, startLine, nCol, i+1);
7737          i += 2;
7738          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
7739        }
7740      }
7741      if( sCtx.cTerm==sCtx.cColSep ){
7742        do{
7743          xRead(&sCtx);
7744          i++;
7745        }while( sCtx.cTerm==sCtx.cColSep );
7746        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
7747                        "extras ignored\n",
7748                        sCtx.zFile, startLine, nCol, i);
7749      }
7750      if( i>=nCol ){
7751        sqlite3_step(pStmt);
7752        rc = sqlite3_reset(pStmt);
7753        if( rc!=SQLITE_OK ){
7754          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
7755                      startLine, sqlite3_errmsg(p->db));
7756        }
7757      }
7758    }while( sCtx.cTerm!=EOF );
7759
7760    xCloser(sCtx.in);
7761    sqlite3_free(sCtx.z);
7762    sqlite3_finalize(pStmt);
7763    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
7764  }else
7765
7766#ifndef SQLITE_UNTESTABLE
7767  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
7768    char *zSql;
7769    char *zCollist = 0;
7770    sqlite3_stmt *pStmt;
7771    int tnum = 0;
7772    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
7773    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
7774    int i;
7775    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
7776      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
7777                          "       .imposter off\n");
7778      /* Also allowed, but not documented:
7779      **
7780      **    .imposter TABLE IMPOSTER
7781      **
7782      ** where TABLE is a WITHOUT ROWID table.  In that case, the
7783      ** imposter is another WITHOUT ROWID table with the columns in
7784      ** storage order. */
7785      rc = 1;
7786      goto meta_command_exit;
7787    }
7788    open_db(p, 0);
7789    if( nArg==2 ){
7790      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
7791      goto meta_command_exit;
7792    }
7793    zSql = sqlite3_mprintf(
7794      "SELECT rootpage, 0 FROM sqlite_master"
7795      " WHERE name='%q' AND type='index'"
7796      "UNION ALL "
7797      "SELECT rootpage, 1 FROM sqlite_master"
7798      " WHERE name='%q' AND type='table'"
7799      "   AND sql LIKE '%%without%%rowid%%'",
7800      azArg[1], azArg[1]
7801    );
7802    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7803    sqlite3_free(zSql);
7804    if( sqlite3_step(pStmt)==SQLITE_ROW ){
7805      tnum = sqlite3_column_int(pStmt, 0);
7806      isWO = sqlite3_column_int(pStmt, 1);
7807    }
7808    sqlite3_finalize(pStmt);
7809    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
7810    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7811    sqlite3_free(zSql);
7812    i = 0;
7813    while( sqlite3_step(pStmt)==SQLITE_ROW ){
7814      char zLabel[20];
7815      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
7816      i++;
7817      if( zCol==0 ){
7818        if( sqlite3_column_int(pStmt,1)==-1 ){
7819          zCol = "_ROWID_";
7820        }else{
7821          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
7822          zCol = zLabel;
7823        }
7824      }
7825      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
7826        lenPK = (int)strlen(zCollist);
7827      }
7828      if( zCollist==0 ){
7829        zCollist = sqlite3_mprintf("\"%w\"", zCol);
7830      }else{
7831        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
7832      }
7833    }
7834    sqlite3_finalize(pStmt);
7835    if( i==0 || tnum==0 ){
7836      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
7837      rc = 1;
7838      sqlite3_free(zCollist);
7839      goto meta_command_exit;
7840    }
7841    if( lenPK==0 ) lenPK = 100000;
7842    zSql = sqlite3_mprintf(
7843          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
7844          azArg[2], zCollist, lenPK, zCollist);
7845    sqlite3_free(zCollist);
7846    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
7847    if( rc==SQLITE_OK ){
7848      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
7849      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
7850      if( rc ){
7851        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
7852      }else{
7853        utf8_printf(stdout, "%s;\n", zSql);
7854        raw_printf(stdout,
7855          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
7856          azArg[1], isWO ? "table" : "index"
7857        );
7858      }
7859    }else{
7860      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
7861      rc = 1;
7862    }
7863    sqlite3_free(zSql);
7864  }else
7865#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
7866
7867#ifdef SQLITE_ENABLE_IOTRACE
7868  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
7869    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
7870    if( iotrace && iotrace!=stdout ) fclose(iotrace);
7871    iotrace = 0;
7872    if( nArg<2 ){
7873      sqlite3IoTrace = 0;
7874    }else if( strcmp(azArg[1], "-")==0 ){
7875      sqlite3IoTrace = iotracePrintf;
7876      iotrace = stdout;
7877    }else{
7878      iotrace = fopen(azArg[1], "w");
7879      if( iotrace==0 ){
7880        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
7881        sqlite3IoTrace = 0;
7882        rc = 1;
7883      }else{
7884        sqlite3IoTrace = iotracePrintf;
7885      }
7886    }
7887  }else
7888#endif
7889
7890  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
7891    static const struct {
7892       const char *zLimitName;   /* Name of a limit */
7893       int limitCode;            /* Integer code for that limit */
7894    } aLimit[] = {
7895      { "length",                SQLITE_LIMIT_LENGTH                    },
7896      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
7897      { "column",                SQLITE_LIMIT_COLUMN                    },
7898      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
7899      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
7900      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
7901      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
7902      { "attached",              SQLITE_LIMIT_ATTACHED                  },
7903      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
7904      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
7905      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
7906      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
7907    };
7908    int i, n2;
7909    open_db(p, 0);
7910    if( nArg==1 ){
7911      for(i=0; i<ArraySize(aLimit); i++){
7912        printf("%20s %d\n", aLimit[i].zLimitName,
7913               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
7914      }
7915    }else if( nArg>3 ){
7916      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
7917      rc = 1;
7918      goto meta_command_exit;
7919    }else{
7920      int iLimit = -1;
7921      n2 = strlen30(azArg[1]);
7922      for(i=0; i<ArraySize(aLimit); i++){
7923        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
7924          if( iLimit<0 ){
7925            iLimit = i;
7926          }else{
7927            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
7928            rc = 1;
7929            goto meta_command_exit;
7930          }
7931        }
7932      }
7933      if( iLimit<0 ){
7934        utf8_printf(stderr, "unknown limit: \"%s\"\n"
7935                        "enter \".limits\" with no arguments for a list.\n",
7936                         azArg[1]);
7937        rc = 1;
7938        goto meta_command_exit;
7939      }
7940      if( nArg==3 ){
7941        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
7942                      (int)integerValue(azArg[2]));
7943      }
7944      printf("%20s %d\n", aLimit[iLimit].zLimitName,
7945             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
7946    }
7947  }else
7948
7949  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
7950    open_db(p, 0);
7951    lintDotCommand(p, azArg, nArg);
7952  }else
7953
7954#ifndef SQLITE_OMIT_LOAD_EXTENSION
7955  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
7956    const char *zFile, *zProc;
7957    char *zErrMsg = 0;
7958    if( nArg<2 ){
7959      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
7960      rc = 1;
7961      goto meta_command_exit;
7962    }
7963    zFile = azArg[1];
7964    zProc = nArg>=3 ? azArg[2] : 0;
7965    open_db(p, 0);
7966    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
7967    if( rc!=SQLITE_OK ){
7968      utf8_printf(stderr, "Error: %s\n", zErrMsg);
7969      sqlite3_free(zErrMsg);
7970      rc = 1;
7971    }
7972  }else
7973#endif
7974
7975  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
7976    if( nArg!=2 ){
7977      raw_printf(stderr, "Usage: .log FILENAME\n");
7978      rc = 1;
7979    }else{
7980      const char *zFile = azArg[1];
7981      output_file_close(p->pLog);
7982      p->pLog = output_file_open(zFile, 0);
7983    }
7984  }else
7985
7986  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
7987    const char *zMode = nArg>=2 ? azArg[1] : "";
7988    int n2 = strlen30(zMode);
7989    int c2 = zMode[0];
7990    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
7991      p->mode = MODE_Line;
7992      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
7993    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
7994      p->mode = MODE_Column;
7995      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
7996    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
7997      p->mode = MODE_List;
7998      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
7999      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8000    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8001      p->mode = MODE_Html;
8002    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8003      p->mode = MODE_Tcl;
8004      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8005      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8006    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8007      p->mode = MODE_Csv;
8008      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8009      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8010    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8011      p->mode = MODE_List;
8012      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8013    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8014      p->mode = MODE_Insert;
8015      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8016    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8017      p->mode = MODE_Quote;
8018    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8019      p->mode = MODE_Ascii;
8020      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8021      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8022    }else if( nArg==1 ){
8023      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8024    }else{
8025      raw_printf(stderr, "Error: mode should be one of: "
8026         "ascii column csv html insert line list quote tabs tcl\n");
8027      rc = 1;
8028    }
8029    p->cMode = p->mode;
8030  }else
8031
8032  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8033    if( nArg==2 ){
8034      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8035                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8036    }else{
8037      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8038      rc = 1;
8039    }
8040  }else
8041
8042  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8043    char *zNewFilename;  /* Name of the database file to open */
8044    int iName = 1;       /* Index in azArg[] of the filename */
8045    int newFlag = 0;     /* True to delete file before opening */
8046    /* Close the existing database */
8047    session_close_all(p);
8048    close_db(p->db);
8049    p->db = 0;
8050    p->zDbFilename = 0;
8051    sqlite3_free(p->zFreeOnClose);
8052    p->zFreeOnClose = 0;
8053    p->openMode = SHELL_OPEN_UNSPEC;
8054    p->openFlags = 0;
8055    p->szMax = 0;
8056    /* Check for command-line arguments */
8057    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
8058      const char *z = azArg[iName];
8059      if( optionMatch(z,"new") ){
8060        newFlag = 1;
8061#ifdef SQLITE_HAVE_ZLIB
8062      }else if( optionMatch(z, "zip") ){
8063        p->openMode = SHELL_OPEN_ZIPFILE;
8064#endif
8065      }else if( optionMatch(z, "append") ){
8066        p->openMode = SHELL_OPEN_APPENDVFS;
8067      }else if( optionMatch(z, "readonly") ){
8068        p->openMode = SHELL_OPEN_READONLY;
8069      }else if( optionMatch(z, "nofollow") ){
8070        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8071#ifdef SQLITE_ENABLE_DESERIALIZE
8072      }else if( optionMatch(z, "deserialize") ){
8073        p->openMode = SHELL_OPEN_DESERIALIZE;
8074      }else if( optionMatch(z, "hexdb") ){
8075        p->openMode = SHELL_OPEN_HEXDB;
8076      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8077        p->szMax = integerValue(azArg[++iName]);
8078#endif /* SQLITE_ENABLE_DESERIALIZE */
8079      }else if( z[0]=='-' ){
8080        utf8_printf(stderr, "unknown option: %s\n", z);
8081        rc = 1;
8082        goto meta_command_exit;
8083      }
8084    }
8085    /* If a filename is specified, try to open it first */
8086    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
8087    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8088      if( newFlag ) shellDeleteFile(zNewFilename);
8089      p->zDbFilename = zNewFilename;
8090      open_db(p, OPEN_DB_KEEPALIVE);
8091      if( p->db==0 ){
8092        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8093        sqlite3_free(zNewFilename);
8094      }else{
8095        p->zFreeOnClose = zNewFilename;
8096      }
8097    }
8098    if( p->db==0 ){
8099      /* As a fall-back open a TEMP database */
8100      p->zDbFilename = 0;
8101      open_db(p, 0);
8102    }
8103  }else
8104
8105  if( (c=='o'
8106        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8107   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8108  ){
8109    const char *zFile = nArg>=2 ? azArg[1] : "stdout";
8110    int bTxtMode = 0;
8111    if( azArg[0][0]=='e' ){
8112      /* Transform the ".excel" command into ".once -x" */
8113      nArg = 2;
8114      azArg[0] = "once";
8115      zFile = azArg[1] = "-x";
8116      n = 4;
8117    }
8118    if( nArg>2 ){
8119      utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
8120      rc = 1;
8121      goto meta_command_exit;
8122    }
8123    if( n>1 && strncmp(azArg[0], "once", n)==0 ){
8124      if( nArg<2 ){
8125        raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
8126        rc = 1;
8127        goto meta_command_exit;
8128      }
8129      p->outCount = 2;
8130    }else{
8131      p->outCount = 0;
8132    }
8133    output_reset(p);
8134    if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
8135#ifndef SQLITE_NOHAVE_SYSTEM
8136    if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
8137      p->doXdgOpen = 1;
8138      outputModePush(p);
8139      if( zFile[1]=='x' ){
8140        newTempFile(p, "csv");
8141        p->mode = MODE_Csv;
8142        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8143        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8144      }else{
8145        newTempFile(p, "txt");
8146        bTxtMode = 1;
8147      }
8148      zFile = p->zTempFile;
8149    }
8150#endif /* SQLITE_NOHAVE_SYSTEM */
8151    if( zFile[0]=='|' ){
8152#ifdef SQLITE_OMIT_POPEN
8153      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8154      rc = 1;
8155      p->out = stdout;
8156#else
8157      p->out = popen(zFile + 1, "w");
8158      if( p->out==0 ){
8159        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8160        p->out = stdout;
8161        rc = 1;
8162      }else{
8163        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8164      }
8165#endif
8166    }else{
8167      p->out = output_file_open(zFile, bTxtMode);
8168      if( p->out==0 ){
8169        if( strcmp(zFile,"off")!=0 ){
8170          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8171        }
8172        p->out = stdout;
8173        rc = 1;
8174      } else {
8175        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8176      }
8177    }
8178  }else
8179
8180  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8181    open_db(p,0);
8182    if( nArg<=1 ) goto parameter_syntax_error;
8183
8184    /* .parameter clear
8185    ** Clear all bind parameters by dropping the TEMP table that holds them.
8186    */
8187    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8188      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8189                   0, 0, 0);
8190    }else
8191
8192    /* .parameter list
8193    ** List all bind parameters.
8194    */
8195    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8196      sqlite3_stmt *pStmt = 0;
8197      int rx;
8198      int len = 0;
8199      rx = sqlite3_prepare_v2(p->db,
8200             "SELECT max(length(key)) "
8201             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8202      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8203        len = sqlite3_column_int(pStmt, 0);
8204        if( len>40 ) len = 40;
8205      }
8206      sqlite3_finalize(pStmt);
8207      pStmt = 0;
8208      if( len ){
8209        rx = sqlite3_prepare_v2(p->db,
8210             "SELECT key, quote(value) "
8211             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8212        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8213          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8214                      sqlite3_column_text(pStmt,1));
8215        }
8216        sqlite3_finalize(pStmt);
8217      }
8218    }else
8219
8220    /* .parameter init
8221    ** Make sure the TEMP table used to hold bind parameters exists.
8222    ** Create it if necessary.
8223    */
8224    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8225      bind_table_init(p);
8226    }else
8227
8228    /* .parameter set NAME VALUE
8229    ** Set or reset a bind parameter.  NAME should be the full parameter
8230    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8231    ** VALUE can be in either SQL literal notation, or if not it will be
8232    ** understood to be a text string.
8233    */
8234    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8235      int rx;
8236      char *zSql;
8237      sqlite3_stmt *pStmt;
8238      const char *zKey = azArg[2];
8239      const char *zValue = azArg[3];
8240      bind_table_init(p);
8241      zSql = sqlite3_mprintf(
8242                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8243                  "VALUES(%Q,%s);", zKey, zValue);
8244      if( zSql==0 ) shell_out_of_memory();
8245      pStmt = 0;
8246      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8247      sqlite3_free(zSql);
8248      if( rx!=SQLITE_OK ){
8249        sqlite3_finalize(pStmt);
8250        pStmt = 0;
8251        zSql = sqlite3_mprintf(
8252                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8253                   "VALUES(%Q,%Q);", zKey, zValue);
8254        if( zSql==0 ) shell_out_of_memory();
8255        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8256        sqlite3_free(zSql);
8257        if( rx!=SQLITE_OK ){
8258          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8259          sqlite3_finalize(pStmt);
8260          pStmt = 0;
8261          rc = 1;
8262        }
8263      }
8264      sqlite3_step(pStmt);
8265      sqlite3_finalize(pStmt);
8266    }else
8267
8268    /* .parameter unset NAME
8269    ** Remove the NAME binding from the parameter binding table, if it
8270    ** exists.
8271    */
8272    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8273      char *zSql = sqlite3_mprintf(
8274          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8275      if( zSql==0 ) shell_out_of_memory();
8276      sqlite3_exec(p->db, zSql, 0, 0, 0);
8277      sqlite3_free(zSql);
8278    }else
8279    /* If no command name matches, show a syntax error */
8280    parameter_syntax_error:
8281    showHelp(p->out, "parameter");
8282  }else
8283
8284  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8285    int i;
8286    for(i=1; i<nArg; i++){
8287      if( i>1 ) raw_printf(p->out, " ");
8288      utf8_printf(p->out, "%s", azArg[i]);
8289    }
8290    raw_printf(p->out, "\n");
8291  }else
8292
8293#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8294  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8295    int i;
8296    int nn = 0;
8297    p->flgProgress = 0;
8298    p->mxProgress = 0;
8299    p->nProgress = 0;
8300    for(i=1; i<nArg; i++){
8301      const char *z = azArg[i];
8302      if( z[0]=='-' ){
8303        z++;
8304        if( z[0]=='-' ) z++;
8305        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
8306          p->flgProgress |= SHELL_PROGRESS_QUIET;
8307          continue;
8308        }
8309        if( strcmp(z,"reset")==0 ){
8310          p->flgProgress |= SHELL_PROGRESS_RESET;
8311          continue;
8312        }
8313        if( strcmp(z,"once")==0 ){
8314          p->flgProgress |= SHELL_PROGRESS_ONCE;
8315          continue;
8316        }
8317        if( strcmp(z,"limit")==0 ){
8318          if( i+1>=nArg ){
8319            utf8_printf(stderr, "Error: missing argument on --limit\n");
8320            rc = 1;
8321            goto meta_command_exit;
8322          }else{
8323            p->mxProgress = (int)integerValue(azArg[++i]);
8324          }
8325          continue;
8326        }
8327        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
8328        rc = 1;
8329        goto meta_command_exit;
8330      }else{
8331        nn = (int)integerValue(z);
8332      }
8333    }
8334    open_db(p, 0);
8335    sqlite3_progress_handler(p->db, nn, progress_handler, p);
8336  }else
8337#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
8338
8339  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
8340    if( nArg >= 2) {
8341      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
8342    }
8343    if( nArg >= 3) {
8344      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
8345    }
8346  }else
8347
8348  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
8349    rc = 2;
8350  }else
8351
8352  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
8353    FILE *inSaved = p->in;
8354    int savedLineno = p->lineno;
8355    if( nArg!=2 ){
8356      raw_printf(stderr, "Usage: .read FILE\n");
8357      rc = 1;
8358      goto meta_command_exit;
8359    }
8360    p->in = fopen(azArg[1], "rb");
8361    if( p->in==0 ){
8362      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
8363      rc = 1;
8364    }else{
8365      rc = process_input(p);
8366      fclose(p->in);
8367    }
8368    p->in = inSaved;
8369    p->lineno = savedLineno;
8370  }else
8371
8372  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
8373    const char *zSrcFile;
8374    const char *zDb;
8375    sqlite3 *pSrc;
8376    sqlite3_backup *pBackup;
8377    int nTimeout = 0;
8378
8379    if( nArg==2 ){
8380      zSrcFile = azArg[1];
8381      zDb = "main";
8382    }else if( nArg==3 ){
8383      zSrcFile = azArg[2];
8384      zDb = azArg[1];
8385    }else{
8386      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
8387      rc = 1;
8388      goto meta_command_exit;
8389    }
8390    rc = sqlite3_open(zSrcFile, &pSrc);
8391    if( rc!=SQLITE_OK ){
8392      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
8393      close_db(pSrc);
8394      return 1;
8395    }
8396    open_db(p, 0);
8397    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
8398    if( pBackup==0 ){
8399      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8400      close_db(pSrc);
8401      return 1;
8402    }
8403    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
8404          || rc==SQLITE_BUSY  ){
8405      if( rc==SQLITE_BUSY ){
8406        if( nTimeout++ >= 3 ) break;
8407        sqlite3_sleep(100);
8408      }
8409    }
8410    sqlite3_backup_finish(pBackup);
8411    if( rc==SQLITE_DONE ){
8412      rc = 0;
8413    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
8414      raw_printf(stderr, "Error: source database is busy\n");
8415      rc = 1;
8416    }else{
8417      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8418      rc = 1;
8419    }
8420    close_db(pSrc);
8421  }else
8422
8423  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
8424    if( nArg==2 ){
8425      p->scanstatsOn = (u8)booleanValue(azArg[1]);
8426#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
8427      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
8428#endif
8429    }else{
8430      raw_printf(stderr, "Usage: .scanstats on|off\n");
8431      rc = 1;
8432    }
8433  }else
8434
8435  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
8436    ShellText sSelect;
8437    ShellState data;
8438    char *zErrMsg = 0;
8439    const char *zDiv = "(";
8440    const char *zName = 0;
8441    int iSchema = 0;
8442    int bDebug = 0;
8443    int ii;
8444
8445    open_db(p, 0);
8446    memcpy(&data, p, sizeof(data));
8447    data.showHeader = 0;
8448    data.cMode = data.mode = MODE_Semi;
8449    initText(&sSelect);
8450    for(ii=1; ii<nArg; ii++){
8451      if( optionMatch(azArg[ii],"indent") ){
8452        data.cMode = data.mode = MODE_Pretty;
8453      }else if( optionMatch(azArg[ii],"debug") ){
8454        bDebug = 1;
8455      }else if( zName==0 ){
8456        zName = azArg[ii];
8457      }else{
8458        raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
8459        rc = 1;
8460        goto meta_command_exit;
8461      }
8462    }
8463    if( zName!=0 ){
8464      int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
8465      if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
8466        char *new_argv[2], *new_colv[2];
8467        new_argv[0] = sqlite3_mprintf(
8468                      "CREATE TABLE %s (\n"
8469                      "  type text,\n"
8470                      "  name text,\n"
8471                      "  tbl_name text,\n"
8472                      "  rootpage integer,\n"
8473                      "  sql text\n"
8474                      ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
8475        new_argv[1] = 0;
8476        new_colv[0] = "sql";
8477        new_colv[1] = 0;
8478        callback(&data, 1, new_argv, new_colv);
8479        sqlite3_free(new_argv[0]);
8480      }
8481    }
8482    if( zDiv ){
8483      sqlite3_stmt *pStmt = 0;
8484      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
8485                              -1, &pStmt, 0);
8486      if( rc ){
8487        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8488        sqlite3_finalize(pStmt);
8489        rc = 1;
8490        goto meta_command_exit;
8491      }
8492      appendText(&sSelect, "SELECT sql FROM", 0);
8493      iSchema = 0;
8494      while( sqlite3_step(pStmt)==SQLITE_ROW ){
8495        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
8496        char zScNum[30];
8497        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
8498        appendText(&sSelect, zDiv, 0);
8499        zDiv = " UNION ALL ";
8500        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
8501        if( sqlite3_stricmp(zDb, "main")!=0 ){
8502          appendText(&sSelect, zDb, '\'');
8503        }else{
8504          appendText(&sSelect, "NULL", 0);
8505        }
8506        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
8507        appendText(&sSelect, zScNum, 0);
8508        appendText(&sSelect, " AS snum, ", 0);
8509        appendText(&sSelect, zDb, '\'');
8510        appendText(&sSelect, " AS sname FROM ", 0);
8511        appendText(&sSelect, zDb, quoteChar(zDb));
8512        appendText(&sSelect, ".sqlite_master", 0);
8513      }
8514      sqlite3_finalize(pStmt);
8515#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
8516      if( zName ){
8517        appendText(&sSelect,
8518           " UNION ALL SELECT shell_module_schema(name),"
8519           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
8520        0);
8521      }
8522#endif
8523      appendText(&sSelect, ") WHERE ", 0);
8524      if( zName ){
8525        char *zQarg = sqlite3_mprintf("%Q", zName);
8526        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
8527                    strchr(zName, '[') != 0;
8528        if( strchr(zName, '.') ){
8529          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
8530        }else{
8531          appendText(&sSelect, "lower(tbl_name)", 0);
8532        }
8533        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
8534        appendText(&sSelect, zQarg, 0);
8535        if( !bGlob ){
8536          appendText(&sSelect, " ESCAPE '\\' ", 0);
8537        }
8538        appendText(&sSelect, " AND ", 0);
8539        sqlite3_free(zQarg);
8540      }
8541      appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
8542                           " ORDER BY snum, rowid", 0);
8543      if( bDebug ){
8544        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
8545      }else{
8546        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
8547      }
8548      freeText(&sSelect);
8549    }
8550    if( zErrMsg ){
8551      utf8_printf(stderr,"Error: %s\n", zErrMsg);
8552      sqlite3_free(zErrMsg);
8553      rc = 1;
8554    }else if( rc != SQLITE_OK ){
8555      raw_printf(stderr,"Error: querying schema information\n");
8556      rc = 1;
8557    }else{
8558      rc = 0;
8559    }
8560  }else
8561
8562#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
8563  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
8564    sqlite3SelectTrace = (int)integerValue(azArg[1]);
8565  }else
8566#endif
8567
8568#if defined(SQLITE_ENABLE_SESSION)
8569  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
8570    OpenSession *pSession = &p->aSession[0];
8571    char **azCmd = &azArg[1];
8572    int iSes = 0;
8573    int nCmd = nArg - 1;
8574    int i;
8575    if( nArg<=1 ) goto session_syntax_error;
8576    open_db(p, 0);
8577    if( nArg>=3 ){
8578      for(iSes=0; iSes<p->nSession; iSes++){
8579        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
8580      }
8581      if( iSes<p->nSession ){
8582        pSession = &p->aSession[iSes];
8583        azCmd++;
8584        nCmd--;
8585      }else{
8586        pSession = &p->aSession[0];
8587        iSes = 0;
8588      }
8589    }
8590
8591    /* .session attach TABLE
8592    ** Invoke the sqlite3session_attach() interface to attach a particular
8593    ** table so that it is never filtered.
8594    */
8595    if( strcmp(azCmd[0],"attach")==0 ){
8596      if( nCmd!=2 ) goto session_syntax_error;
8597      if( pSession->p==0 ){
8598        session_not_open:
8599        raw_printf(stderr, "ERROR: No sessions are open\n");
8600      }else{
8601        rc = sqlite3session_attach(pSession->p, azCmd[1]);
8602        if( rc ){
8603          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
8604          rc = 0;
8605        }
8606      }
8607    }else
8608
8609    /* .session changeset FILE
8610    ** .session patchset FILE
8611    ** Write a changeset or patchset into a file.  The file is overwritten.
8612    */
8613    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
8614      FILE *out = 0;
8615      if( nCmd!=2 ) goto session_syntax_error;
8616      if( pSession->p==0 ) goto session_not_open;
8617      out = fopen(azCmd[1], "wb");
8618      if( out==0 ){
8619        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
8620                    azCmd[1]);
8621      }else{
8622        int szChng;
8623        void *pChng;
8624        if( azCmd[0][0]=='c' ){
8625          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
8626        }else{
8627          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
8628        }
8629        if( rc ){
8630          printf("Error: error code %d\n", rc);
8631          rc = 0;
8632        }
8633        if( pChng
8634          && fwrite(pChng, szChng, 1, out)!=1 ){
8635          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
8636                  szChng);
8637        }
8638        sqlite3_free(pChng);
8639        fclose(out);
8640      }
8641    }else
8642
8643    /* .session close
8644    ** Close the identified session
8645    */
8646    if( strcmp(azCmd[0], "close")==0 ){
8647      if( nCmd!=1 ) goto session_syntax_error;
8648      if( p->nSession ){
8649        session_close(pSession);
8650        p->aSession[iSes] = p->aSession[--p->nSession];
8651      }
8652    }else
8653
8654    /* .session enable ?BOOLEAN?
8655    ** Query or set the enable flag
8656    */
8657    if( strcmp(azCmd[0], "enable")==0 ){
8658      int ii;
8659      if( nCmd>2 ) goto session_syntax_error;
8660      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
8661      if( p->nSession ){
8662        ii = sqlite3session_enable(pSession->p, ii);
8663        utf8_printf(p->out, "session %s enable flag = %d\n",
8664                    pSession->zName, ii);
8665      }
8666    }else
8667
8668    /* .session filter GLOB ....
8669    ** Set a list of GLOB patterns of table names to be excluded.
8670    */
8671    if( strcmp(azCmd[0], "filter")==0 ){
8672      int ii, nByte;
8673      if( nCmd<2 ) goto session_syntax_error;
8674      if( p->nSession ){
8675        for(ii=0; ii<pSession->nFilter; ii++){
8676          sqlite3_free(pSession->azFilter[ii]);
8677        }
8678        sqlite3_free(pSession->azFilter);
8679        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
8680        pSession->azFilter = sqlite3_malloc( nByte );
8681        if( pSession->azFilter==0 ){
8682          raw_printf(stderr, "Error: out or memory\n");
8683          exit(1);
8684        }
8685        for(ii=1; ii<nCmd; ii++){
8686          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
8687        }
8688        pSession->nFilter = ii-1;
8689      }
8690    }else
8691
8692    /* .session indirect ?BOOLEAN?
8693    ** Query or set the indirect flag
8694    */
8695    if( strcmp(azCmd[0], "indirect")==0 ){
8696      int ii;
8697      if( nCmd>2 ) goto session_syntax_error;
8698      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
8699      if( p->nSession ){
8700        ii = sqlite3session_indirect(pSession->p, ii);
8701        utf8_printf(p->out, "session %s indirect flag = %d\n",
8702                    pSession->zName, ii);
8703      }
8704    }else
8705
8706    /* .session isempty
8707    ** Determine if the session is empty
8708    */
8709    if( strcmp(azCmd[0], "isempty")==0 ){
8710      int ii;
8711      if( nCmd!=1 ) goto session_syntax_error;
8712      if( p->nSession ){
8713        ii = sqlite3session_isempty(pSession->p);
8714        utf8_printf(p->out, "session %s isempty flag = %d\n",
8715                    pSession->zName, ii);
8716      }
8717    }else
8718
8719    /* .session list
8720    ** List all currently open sessions
8721    */
8722    if( strcmp(azCmd[0],"list")==0 ){
8723      for(i=0; i<p->nSession; i++){
8724        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
8725      }
8726    }else
8727
8728    /* .session open DB NAME
8729    ** Open a new session called NAME on the attached database DB.
8730    ** DB is normally "main".
8731    */
8732    if( strcmp(azCmd[0],"open")==0 ){
8733      char *zName;
8734      if( nCmd!=3 ) goto session_syntax_error;
8735      zName = azCmd[2];
8736      if( zName[0]==0 ) goto session_syntax_error;
8737      for(i=0; i<p->nSession; i++){
8738        if( strcmp(p->aSession[i].zName,zName)==0 ){
8739          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
8740          goto meta_command_exit;
8741        }
8742      }
8743      if( p->nSession>=ArraySize(p->aSession) ){
8744        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
8745        goto meta_command_exit;
8746      }
8747      pSession = &p->aSession[p->nSession];
8748      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
8749      if( rc ){
8750        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
8751        rc = 0;
8752        goto meta_command_exit;
8753      }
8754      pSession->nFilter = 0;
8755      sqlite3session_table_filter(pSession->p, session_filter, pSession);
8756      p->nSession++;
8757      pSession->zName = sqlite3_mprintf("%s", zName);
8758    }else
8759    /* If no command name matches, show a syntax error */
8760    session_syntax_error:
8761    showHelp(p->out, "session");
8762  }else
8763#endif
8764
8765#ifdef SQLITE_DEBUG
8766  /* Undocumented commands for internal testing.  Subject to change
8767  ** without notice. */
8768  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
8769    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
8770      int i, v;
8771      for(i=1; i<nArg; i++){
8772        v = booleanValue(azArg[i]);
8773        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
8774      }
8775    }
8776    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
8777      int i; sqlite3_int64 v;
8778      for(i=1; i<nArg; i++){
8779        char zBuf[200];
8780        v = integerValue(azArg[i]);
8781        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
8782        utf8_printf(p->out, "%s", zBuf);
8783      }
8784    }
8785  }else
8786#endif
8787
8788  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
8789    int bIsInit = 0;         /* True to initialize the SELFTEST table */
8790    int bVerbose = 0;        /* Verbose output */
8791    int bSelftestExists;     /* True if SELFTEST already exists */
8792    int i, k;                /* Loop counters */
8793    int nTest = 0;           /* Number of tests runs */
8794    int nErr = 0;            /* Number of errors seen */
8795    ShellText str;           /* Answer for a query */
8796    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
8797
8798    open_db(p,0);
8799    for(i=1; i<nArg; i++){
8800      const char *z = azArg[i];
8801      if( z[0]=='-' && z[1]=='-' ) z++;
8802      if( strcmp(z,"-init")==0 ){
8803        bIsInit = 1;
8804      }else
8805      if( strcmp(z,"-v")==0 ){
8806        bVerbose++;
8807      }else
8808      {
8809        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
8810                    azArg[i], azArg[0]);
8811        raw_printf(stderr, "Should be one of: --init -v\n");
8812        rc = 1;
8813        goto meta_command_exit;
8814      }
8815    }
8816    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
8817           != SQLITE_OK ){
8818      bSelftestExists = 0;
8819    }else{
8820      bSelftestExists = 1;
8821    }
8822    if( bIsInit ){
8823      createSelftestTable(p);
8824      bSelftestExists = 1;
8825    }
8826    initText(&str);
8827    appendText(&str, "x", 0);
8828    for(k=bSelftestExists; k>=0; k--){
8829      if( k==1 ){
8830        rc = sqlite3_prepare_v2(p->db,
8831            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
8832            -1, &pStmt, 0);
8833      }else{
8834        rc = sqlite3_prepare_v2(p->db,
8835          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
8836          "      (1,'run','PRAGMA integrity_check','ok')",
8837          -1, &pStmt, 0);
8838      }
8839      if( rc ){
8840        raw_printf(stderr, "Error querying the selftest table\n");
8841        rc = 1;
8842        sqlite3_finalize(pStmt);
8843        goto meta_command_exit;
8844      }
8845      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
8846        int tno = sqlite3_column_int(pStmt, 0);
8847        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
8848        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
8849        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
8850
8851        k = 0;
8852        if( bVerbose>0 ){
8853          char *zQuote = sqlite3_mprintf("%q", zSql);
8854          printf("%d: %s %s\n", tno, zOp, zSql);
8855          sqlite3_free(zQuote);
8856        }
8857        if( strcmp(zOp,"memo")==0 ){
8858          utf8_printf(p->out, "%s\n", zSql);
8859        }else
8860        if( strcmp(zOp,"run")==0 ){
8861          char *zErrMsg = 0;
8862          str.n = 0;
8863          str.z[0] = 0;
8864          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
8865          nTest++;
8866          if( bVerbose ){
8867            utf8_printf(p->out, "Result: %s\n", str.z);
8868          }
8869          if( rc || zErrMsg ){
8870            nErr++;
8871            rc = 1;
8872            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
8873            sqlite3_free(zErrMsg);
8874          }else if( strcmp(zAns,str.z)!=0 ){
8875            nErr++;
8876            rc = 1;
8877            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
8878            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
8879          }
8880        }else
8881        {
8882          utf8_printf(stderr,
8883            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
8884          rc = 1;
8885          break;
8886        }
8887      } /* End loop over rows of content from SELFTEST */
8888      sqlite3_finalize(pStmt);
8889    } /* End loop over k */
8890    freeText(&str);
8891    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
8892  }else
8893
8894  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
8895    if( nArg<2 || nArg>3 ){
8896      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
8897      rc = 1;
8898    }
8899    if( nArg>=2 ){
8900      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
8901                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
8902    }
8903    if( nArg>=3 ){
8904      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
8905                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
8906    }
8907  }else
8908
8909  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
8910    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
8911    int i;                   /* Loop counter */
8912    int bSchema = 0;         /* Also hash the schema */
8913    int bSeparate = 0;       /* Hash each table separately */
8914    int iSize = 224;         /* Hash algorithm to use */
8915    int bDebug = 0;          /* Only show the query that would have run */
8916    sqlite3_stmt *pStmt;     /* For querying tables names */
8917    char *zSql;              /* SQL to be run */
8918    char *zSep;              /* Separator */
8919    ShellText sSql;          /* Complete SQL for the query to run the hash */
8920    ShellText sQuery;        /* Set of queries used to read all content */
8921    open_db(p, 0);
8922    for(i=1; i<nArg; i++){
8923      const char *z = azArg[i];
8924      if( z[0]=='-' ){
8925        z++;
8926        if( z[0]=='-' ) z++;
8927        if( strcmp(z,"schema")==0 ){
8928          bSchema = 1;
8929        }else
8930        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
8931         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
8932        ){
8933          iSize = atoi(&z[5]);
8934        }else
8935        if( strcmp(z,"debug")==0 ){
8936          bDebug = 1;
8937        }else
8938        {
8939          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
8940                      azArg[i], azArg[0]);
8941          showHelp(p->out, azArg[0]);
8942          rc = 1;
8943          goto meta_command_exit;
8944        }
8945      }else if( zLike ){
8946        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
8947        rc = 1;
8948        goto meta_command_exit;
8949      }else{
8950        zLike = z;
8951        bSeparate = 1;
8952        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
8953      }
8954    }
8955    if( bSchema ){
8956      zSql = "SELECT lower(name) FROM sqlite_master"
8957             " WHERE type='table' AND coalesce(rootpage,0)>1"
8958             " UNION ALL SELECT 'sqlite_master'"
8959             " ORDER BY 1 collate nocase";
8960    }else{
8961      zSql = "SELECT lower(name) FROM sqlite_master"
8962             " WHERE type='table' AND coalesce(rootpage,0)>1"
8963             " AND name NOT LIKE 'sqlite_%'"
8964             " ORDER BY 1 collate nocase";
8965    }
8966    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8967    initText(&sQuery);
8968    initText(&sSql);
8969    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
8970    zSep = "VALUES(";
8971    while( SQLITE_ROW==sqlite3_step(pStmt) ){
8972      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
8973      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
8974      if( strncmp(zTab, "sqlite_",7)!=0 ){
8975        appendText(&sQuery,"SELECT * FROM ", 0);
8976        appendText(&sQuery,zTab,'"');
8977        appendText(&sQuery," NOT INDEXED;", 0);
8978      }else if( strcmp(zTab, "sqlite_master")==0 ){
8979        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
8980                           " ORDER BY name;", 0);
8981      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
8982        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
8983                           " ORDER BY name;", 0);
8984      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
8985        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
8986                           " ORDER BY tbl,idx;", 0);
8987      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
8988        appendText(&sQuery, "SELECT * FROM ", 0);
8989        appendText(&sQuery, zTab, 0);
8990        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
8991      }
8992      appendText(&sSql, zSep, 0);
8993      appendText(&sSql, sQuery.z, '\'');
8994      sQuery.n = 0;
8995      appendText(&sSql, ",", 0);
8996      appendText(&sSql, zTab, '\'');
8997      zSep = "),(";
8998    }
8999    sqlite3_finalize(pStmt);
9000    if( bSeparate ){
9001      zSql = sqlite3_mprintf(
9002          "%s))"
9003          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9004          "   FROM [sha3sum$query]",
9005          sSql.z, iSize);
9006    }else{
9007      zSql = sqlite3_mprintf(
9008          "%s))"
9009          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9010          "   FROM [sha3sum$query]",
9011          sSql.z, iSize);
9012    }
9013    freeText(&sQuery);
9014    freeText(&sSql);
9015    if( bDebug ){
9016      utf8_printf(p->out, "%s\n", zSql);
9017    }else{
9018      shell_exec(p, zSql, 0);
9019    }
9020    sqlite3_free(zSql);
9021  }else
9022
9023#ifndef SQLITE_NOHAVE_SYSTEM
9024  if( c=='s'
9025   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9026  ){
9027    char *zCmd;
9028    int i, x;
9029    if( nArg<2 ){
9030      raw_printf(stderr, "Usage: .system COMMAND\n");
9031      rc = 1;
9032      goto meta_command_exit;
9033    }
9034    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9035    for(i=2; i<nArg; i++){
9036      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9037                             zCmd, azArg[i]);
9038    }
9039    x = system(zCmd);
9040    sqlite3_free(zCmd);
9041    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9042  }else
9043#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9044
9045  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9046    static const char *azBool[] = { "off", "on", "trigger", "full"};
9047    int i;
9048    if( nArg!=1 ){
9049      raw_printf(stderr, "Usage: .show\n");
9050      rc = 1;
9051      goto meta_command_exit;
9052    }
9053    utf8_printf(p->out, "%12.12s: %s\n","echo",
9054                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9055    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9056    utf8_printf(p->out, "%12.12s: %s\n","explain",
9057         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9058    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9059    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9060    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9061      output_c_string(p->out, p->nullValue);
9062      raw_printf(p->out, "\n");
9063    utf8_printf(p->out,"%12.12s: %s\n","output",
9064            strlen30(p->outfile) ? p->outfile : "stdout");
9065    utf8_printf(p->out,"%12.12s: ", "colseparator");
9066      output_c_string(p->out, p->colSeparator);
9067      raw_printf(p->out, "\n");
9068    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9069      output_c_string(p->out, p->rowSeparator);
9070      raw_printf(p->out, "\n");
9071    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
9072    utf8_printf(p->out, "%12.12s: ", "width");
9073    for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
9074      raw_printf(p->out, "%d ", p->colWidth[i]);
9075    }
9076    raw_printf(p->out, "\n");
9077    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9078                p->zDbFilename ? p->zDbFilename : "");
9079  }else
9080
9081  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9082    if( nArg==2 ){
9083      p->statsOn = (u8)booleanValue(azArg[1]);
9084    }else if( nArg==1 ){
9085      display_stats(p->db, p, 0);
9086    }else{
9087      raw_printf(stderr, "Usage: .stats ?on|off?\n");
9088      rc = 1;
9089    }
9090  }else
9091
9092  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9093   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9094                 || strncmp(azArg[0], "indexes", n)==0) )
9095  ){
9096    sqlite3_stmt *pStmt;
9097    char **azResult;
9098    int nRow, nAlloc;
9099    int ii;
9100    ShellText s;
9101    initText(&s);
9102    open_db(p, 0);
9103    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9104    if( rc ){
9105      sqlite3_finalize(pStmt);
9106      return shellDatabaseError(p->db);
9107    }
9108
9109    if( nArg>2 && c=='i' ){
9110      /* It is an historical accident that the .indexes command shows an error
9111      ** when called with the wrong number of arguments whereas the .tables
9112      ** command does not. */
9113      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9114      rc = 1;
9115      sqlite3_finalize(pStmt);
9116      goto meta_command_exit;
9117    }
9118    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9119      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9120      if( zDbName==0 ) continue;
9121      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9122      if( sqlite3_stricmp(zDbName, "main")==0 ){
9123        appendText(&s, "SELECT name FROM ", 0);
9124      }else{
9125        appendText(&s, "SELECT ", 0);
9126        appendText(&s, zDbName, '\'');
9127        appendText(&s, "||'.'||name FROM ", 0);
9128      }
9129      appendText(&s, zDbName, '"');
9130      appendText(&s, ".sqlite_master ", 0);
9131      if( c=='t' ){
9132        appendText(&s," WHERE type IN ('table','view')"
9133                      "   AND name NOT LIKE 'sqlite_%'"
9134                      "   AND name LIKE ?1", 0);
9135      }else{
9136        appendText(&s," WHERE type='index'"
9137                      "   AND tbl_name LIKE ?1", 0);
9138      }
9139    }
9140    rc = sqlite3_finalize(pStmt);
9141    appendText(&s, " ORDER BY 1", 0);
9142    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9143    freeText(&s);
9144    if( rc ) return shellDatabaseError(p->db);
9145
9146    /* Run the SQL statement prepared by the above block. Store the results
9147    ** as an array of nul-terminated strings in azResult[].  */
9148    nRow = nAlloc = 0;
9149    azResult = 0;
9150    if( nArg>1 ){
9151      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9152    }else{
9153      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9154    }
9155    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9156      if( nRow>=nAlloc ){
9157        char **azNew;
9158        int n2 = nAlloc*2 + 10;
9159        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9160        if( azNew==0 ) shell_out_of_memory();
9161        nAlloc = n2;
9162        azResult = azNew;
9163      }
9164      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9165      if( 0==azResult[nRow] ) shell_out_of_memory();
9166      nRow++;
9167    }
9168    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9169      rc = shellDatabaseError(p->db);
9170    }
9171
9172    /* Pretty-print the contents of array azResult[] to the output */
9173    if( rc==0 && nRow>0 ){
9174      int len, maxlen = 0;
9175      int i, j;
9176      int nPrintCol, nPrintRow;
9177      for(i=0; i<nRow; i++){
9178        len = strlen30(azResult[i]);
9179        if( len>maxlen ) maxlen = len;
9180      }
9181      nPrintCol = 80/(maxlen+2);
9182      if( nPrintCol<1 ) nPrintCol = 1;
9183      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9184      for(i=0; i<nPrintRow; i++){
9185        for(j=i; j<nRow; j+=nPrintRow){
9186          char *zSp = j<nPrintRow ? "" : "  ";
9187          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9188                      azResult[j] ? azResult[j]:"");
9189        }
9190        raw_printf(p->out, "\n");
9191      }
9192    }
9193
9194    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9195    sqlite3_free(azResult);
9196  }else
9197
9198  /* Begin redirecting output to the file "testcase-out.txt" */
9199  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9200    output_reset(p);
9201    p->out = output_file_open("testcase-out.txt", 0);
9202    if( p->out==0 ){
9203      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9204    }
9205    if( nArg>=2 ){
9206      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9207    }else{
9208      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9209    }
9210  }else
9211
9212#ifndef SQLITE_UNTESTABLE
9213  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9214    static const struct {
9215       const char *zCtrlName;   /* Name of a test-control option */
9216       int ctrlCode;            /* Integer code for that option */
9217       const char *zUsage;      /* Usage notes */
9218    } aCtrl[] = {
9219      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9220      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9221    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9222    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9223      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9224      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9225    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9226      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9227      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9228      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9229      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9230      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9231#ifdef YYCOVERAGE
9232      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9233#endif
9234      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9235      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9236      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9237      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9238      { "reserve",            SQLITE_TESTCTRL_RESERVE,      "BYTES-OF-RESERVE"},
9239    };
9240    int testctrl = -1;
9241    int iCtrl = -1;
9242    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9243    int isOk = 0;
9244    int i, n2;
9245    const char *zCmd = 0;
9246
9247    open_db(p, 0);
9248    zCmd = nArg>=2 ? azArg[1] : "help";
9249
9250    /* The argument can optionally begin with "-" or "--" */
9251    if( zCmd[0]=='-' && zCmd[1] ){
9252      zCmd++;
9253      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9254    }
9255
9256    /* --help lists all test-controls */
9257    if( strcmp(zCmd,"help")==0 ){
9258      utf8_printf(p->out, "Available test-controls:\n");
9259      for(i=0; i<ArraySize(aCtrl); i++){
9260        utf8_printf(p->out, "  .testctrl %s %s\n",
9261                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
9262      }
9263      rc = 1;
9264      goto meta_command_exit;
9265    }
9266
9267    /* convert testctrl text option to value. allow any unique prefix
9268    ** of the option name, or a numerical value. */
9269    n2 = strlen30(zCmd);
9270    for(i=0; i<ArraySize(aCtrl); i++){
9271      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
9272        if( testctrl<0 ){
9273          testctrl = aCtrl[i].ctrlCode;
9274          iCtrl = i;
9275        }else{
9276          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
9277                              "Use \".testctrl --help\" for help\n", zCmd);
9278          rc = 1;
9279          goto meta_command_exit;
9280        }
9281      }
9282    }
9283    if( testctrl<0 ){
9284      utf8_printf(stderr,"Error: unknown test-control: %s\n"
9285                         "Use \".testctrl --help\" for help\n", zCmd);
9286    }else{
9287      switch(testctrl){
9288
9289        /* sqlite3_test_control(int, db, int) */
9290        case SQLITE_TESTCTRL_OPTIMIZATIONS:
9291        case SQLITE_TESTCTRL_RESERVE:
9292          if( nArg==3 ){
9293            int opt = (int)strtol(azArg[2], 0, 0);
9294            rc2 = sqlite3_test_control(testctrl, p->db, opt);
9295            isOk = 3;
9296          }
9297          break;
9298
9299        /* sqlite3_test_control(int) */
9300        case SQLITE_TESTCTRL_PRNG_SAVE:
9301        case SQLITE_TESTCTRL_PRNG_RESTORE:
9302        case SQLITE_TESTCTRL_PRNG_RESET:
9303        case SQLITE_TESTCTRL_BYTEORDER:
9304          if( nArg==2 ){
9305            rc2 = sqlite3_test_control(testctrl);
9306            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
9307          }
9308          break;
9309
9310        /* sqlite3_test_control(int, uint) */
9311        case SQLITE_TESTCTRL_PENDING_BYTE:
9312          if( nArg==3 ){
9313            unsigned int opt = (unsigned int)integerValue(azArg[2]);
9314            rc2 = sqlite3_test_control(testctrl, opt);
9315            isOk = 3;
9316          }
9317          break;
9318
9319        /* sqlite3_test_control(int, int, sqlite3*) */
9320        case SQLITE_TESTCTRL_PRNG_SEED:
9321          if( nArg==3 || nArg==4 ){
9322            int ii = (int)integerValue(azArg[2]);
9323            sqlite3 *db;
9324            if( ii==0 && strcmp(azArg[2],"random")==0 ){
9325              sqlite3_randomness(sizeof(ii),&ii);
9326              printf("-- random seed: %d\n", ii);
9327            }
9328            if( nArg==3 ){
9329              db = 0;
9330            }else{
9331              db = p->db;
9332              /* Make sure the schema has been loaded */
9333              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
9334            }
9335            rc2 = sqlite3_test_control(testctrl, ii, db);
9336            isOk = 3;
9337          }
9338          break;
9339
9340        /* sqlite3_test_control(int, int) */
9341        case SQLITE_TESTCTRL_ASSERT:
9342        case SQLITE_TESTCTRL_ALWAYS:
9343          if( nArg==3 ){
9344            int opt = booleanValue(azArg[2]);
9345            rc2 = sqlite3_test_control(testctrl, opt);
9346            isOk = 1;
9347          }
9348          break;
9349
9350        /* sqlite3_test_control(int, int) */
9351        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
9352        case SQLITE_TESTCTRL_NEVER_CORRUPT:
9353          if( nArg==3 ){
9354            int opt = booleanValue(azArg[2]);
9355            rc2 = sqlite3_test_control(testctrl, opt);
9356            isOk = 3;
9357          }
9358          break;
9359
9360        /* sqlite3_test_control(sqlite3*) */
9361        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
9362          rc2 = sqlite3_test_control(testctrl, p->db);
9363          isOk = 3;
9364          break;
9365
9366        case SQLITE_TESTCTRL_IMPOSTER:
9367          if( nArg==5 ){
9368            rc2 = sqlite3_test_control(testctrl, p->db,
9369                          azArg[2],
9370                          integerValue(azArg[3]),
9371                          integerValue(azArg[4]));
9372            isOk = 3;
9373          }
9374          break;
9375
9376#ifdef YYCOVERAGE
9377        case SQLITE_TESTCTRL_PARSER_COVERAGE:
9378          if( nArg==2 ){
9379            sqlite3_test_control(testctrl, p->out);
9380            isOk = 3;
9381          }
9382#endif
9383      }
9384    }
9385    if( isOk==0 && iCtrl>=0 ){
9386      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
9387      rc = 1;
9388    }else if( isOk==1 ){
9389      raw_printf(p->out, "%d\n", rc2);
9390    }else if( isOk==2 ){
9391      raw_printf(p->out, "0x%08x\n", rc2);
9392    }
9393  }else
9394#endif /* !defined(SQLITE_UNTESTABLE) */
9395
9396  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
9397    open_db(p, 0);
9398    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
9399  }else
9400
9401  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
9402    if( nArg==2 ){
9403      enableTimer = booleanValue(azArg[1]);
9404      if( enableTimer && !HAS_TIMER ){
9405        raw_printf(stderr, "Error: timer not available on this system.\n");
9406        enableTimer = 0;
9407      }
9408    }else{
9409      raw_printf(stderr, "Usage: .timer on|off\n");
9410      rc = 1;
9411    }
9412  }else
9413
9414#ifndef SQLITE_OMIT_TRACE
9415  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
9416    int mType = 0;
9417    int jj;
9418    open_db(p, 0);
9419    for(jj=1; jj<nArg; jj++){
9420      const char *z = azArg[jj];
9421      if( z[0]=='-' ){
9422        if( optionMatch(z, "expanded") ){
9423          p->eTraceType = SHELL_TRACE_EXPANDED;
9424        }
9425#ifdef SQLITE_ENABLE_NORMALIZE
9426        else if( optionMatch(z, "normalized") ){
9427          p->eTraceType = SHELL_TRACE_NORMALIZED;
9428        }
9429#endif
9430        else if( optionMatch(z, "plain") ){
9431          p->eTraceType = SHELL_TRACE_PLAIN;
9432        }
9433        else if( optionMatch(z, "profile") ){
9434          mType |= SQLITE_TRACE_PROFILE;
9435        }
9436        else if( optionMatch(z, "row") ){
9437          mType |= SQLITE_TRACE_ROW;
9438        }
9439        else if( optionMatch(z, "stmt") ){
9440          mType |= SQLITE_TRACE_STMT;
9441        }
9442        else if( optionMatch(z, "close") ){
9443          mType |= SQLITE_TRACE_CLOSE;
9444        }
9445        else {
9446          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
9447          rc = 1;
9448          goto meta_command_exit;
9449        }
9450      }else{
9451        output_file_close(p->traceOut);
9452        p->traceOut = output_file_open(azArg[1], 0);
9453      }
9454    }
9455    if( p->traceOut==0 ){
9456      sqlite3_trace_v2(p->db, 0, 0, 0);
9457    }else{
9458      if( mType==0 ) mType = SQLITE_TRACE_STMT;
9459      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
9460    }
9461  }else
9462#endif /* !defined(SQLITE_OMIT_TRACE) */
9463
9464#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
9465  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
9466    int ii;
9467    int lenOpt;
9468    char *zOpt;
9469    if( nArg<2 ){
9470      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
9471      rc = 1;
9472      goto meta_command_exit;
9473    }
9474    open_db(p, 0);
9475    zOpt = azArg[1];
9476    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
9477    lenOpt = (int)strlen(zOpt);
9478    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
9479      assert( azArg[nArg]==0 );
9480      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
9481    }else{
9482      for(ii=1; ii<nArg; ii++){
9483        sqlite3_create_module(p->db, azArg[ii], 0, 0);
9484      }
9485    }
9486  }else
9487#endif
9488
9489#if SQLITE_USER_AUTHENTICATION
9490  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
9491    if( nArg<2 ){
9492      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
9493      rc = 1;
9494      goto meta_command_exit;
9495    }
9496    open_db(p, 0);
9497    if( strcmp(azArg[1],"login")==0 ){
9498      if( nArg!=4 ){
9499        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
9500        rc = 1;
9501        goto meta_command_exit;
9502      }
9503      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
9504                                     strlen30(azArg[3]));
9505      if( rc ){
9506        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
9507        rc = 1;
9508      }
9509    }else if( strcmp(azArg[1],"add")==0 ){
9510      if( nArg!=5 ){
9511        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
9512        rc = 1;
9513        goto meta_command_exit;
9514      }
9515      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
9516                            booleanValue(azArg[4]));
9517      if( rc ){
9518        raw_printf(stderr, "User-Add failed: %d\n", rc);
9519        rc = 1;
9520      }
9521    }else if( strcmp(azArg[1],"edit")==0 ){
9522      if( nArg!=5 ){
9523        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
9524        rc = 1;
9525        goto meta_command_exit;
9526      }
9527      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
9528                              booleanValue(azArg[4]));
9529      if( rc ){
9530        raw_printf(stderr, "User-Edit failed: %d\n", rc);
9531        rc = 1;
9532      }
9533    }else if( strcmp(azArg[1],"delete")==0 ){
9534      if( nArg!=3 ){
9535        raw_printf(stderr, "Usage: .user delete USER\n");
9536        rc = 1;
9537        goto meta_command_exit;
9538      }
9539      rc = sqlite3_user_delete(p->db, azArg[2]);
9540      if( rc ){
9541        raw_printf(stderr, "User-Delete failed: %d\n", rc);
9542        rc = 1;
9543      }
9544    }else{
9545      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
9546      rc = 1;
9547      goto meta_command_exit;
9548    }
9549  }else
9550#endif /* SQLITE_USER_AUTHENTICATION */
9551
9552  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
9553    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
9554        sqlite3_libversion(), sqlite3_sourceid());
9555#if SQLITE_HAVE_ZLIB
9556    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
9557#endif
9558#define CTIMEOPT_VAL_(opt) #opt
9559#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
9560#if defined(__clang__) && defined(__clang_major__)
9561    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
9562                    CTIMEOPT_VAL(__clang_minor__) "."
9563                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
9564#elif defined(_MSC_VER)
9565    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
9566#elif defined(__GNUC__) && defined(__VERSION__)
9567    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
9568#endif
9569  }else
9570
9571  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
9572    const char *zDbName = nArg==2 ? azArg[1] : "main";
9573    sqlite3_vfs *pVfs = 0;
9574    if( p->db ){
9575      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
9576      if( pVfs ){
9577        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
9578        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
9579        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
9580        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
9581      }
9582    }
9583  }else
9584
9585  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
9586    sqlite3_vfs *pVfs;
9587    sqlite3_vfs *pCurrent = 0;
9588    if( p->db ){
9589      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
9590    }
9591    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
9592      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
9593           pVfs==pCurrent ? "  <--- CURRENT" : "");
9594      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
9595      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
9596      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
9597      if( pVfs->pNext ){
9598        raw_printf(p->out, "-----------------------------------\n");
9599      }
9600    }
9601  }else
9602
9603  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
9604    const char *zDbName = nArg==2 ? azArg[1] : "main";
9605    char *zVfsName = 0;
9606    if( p->db ){
9607      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
9608      if( zVfsName ){
9609        utf8_printf(p->out, "%s\n", zVfsName);
9610        sqlite3_free(zVfsName);
9611      }
9612    }
9613  }else
9614
9615#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
9616  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
9617    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
9618  }else
9619#endif
9620
9621  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
9622    int j;
9623    assert( nArg<=ArraySize(azArg) );
9624    for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
9625      p->colWidth[j-1] = (int)integerValue(azArg[j]);
9626    }
9627  }else
9628
9629  {
9630    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
9631      " \"%s\". Enter \".help\" for help\n", azArg[0]);
9632    rc = 1;
9633  }
9634
9635meta_command_exit:
9636  if( p->outCount ){
9637    p->outCount--;
9638    if( p->outCount==0 ) output_reset(p);
9639  }
9640  return rc;
9641}
9642
9643/*
9644** Return TRUE if a semicolon occurs anywhere in the first N characters
9645** of string z[].
9646*/
9647static int line_contains_semicolon(const char *z, int N){
9648  int i;
9649  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
9650  return 0;
9651}
9652
9653/*
9654** Test to see if a line consists entirely of whitespace.
9655*/
9656static int _all_whitespace(const char *z){
9657  for(; *z; z++){
9658    if( IsSpace(z[0]) ) continue;
9659    if( *z=='/' && z[1]=='*' ){
9660      z += 2;
9661      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
9662      if( *z==0 ) return 0;
9663      z++;
9664      continue;
9665    }
9666    if( *z=='-' && z[1]=='-' ){
9667      z += 2;
9668      while( *z && *z!='\n' ){ z++; }
9669      if( *z==0 ) return 1;
9670      continue;
9671    }
9672    return 0;
9673  }
9674  return 1;
9675}
9676
9677/*
9678** Return TRUE if the line typed in is an SQL command terminator other
9679** than a semi-colon.  The SQL Server style "go" command is understood
9680** as is the Oracle "/".
9681*/
9682static int line_is_command_terminator(const char *zLine){
9683  while( IsSpace(zLine[0]) ){ zLine++; };
9684  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
9685    return 1;  /* Oracle */
9686  }
9687  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
9688         && _all_whitespace(&zLine[2]) ){
9689    return 1;  /* SQL Server */
9690  }
9691  return 0;
9692}
9693
9694/*
9695** We need a default sqlite3_complete() implementation to use in case
9696** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
9697** any arbitrary text is a complete SQL statement.  This is not very
9698** user-friendly, but it does seem to work.
9699*/
9700#ifdef SQLITE_OMIT_COMPLETE
9701#define sqlite3_complete(x) 1
9702#endif
9703
9704/*
9705** Return true if zSql is a complete SQL statement.  Return false if it
9706** ends in the middle of a string literal or C-style comment.
9707*/
9708static int line_is_complete(char *zSql, int nSql){
9709  int rc;
9710  if( zSql==0 ) return 1;
9711  zSql[nSql] = ';';
9712  zSql[nSql+1] = 0;
9713  rc = sqlite3_complete(zSql);
9714  zSql[nSql] = 0;
9715  return rc;
9716}
9717
9718/*
9719** Run a single line of SQL.  Return the number of errors.
9720*/
9721static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
9722  int rc;
9723  char *zErrMsg = 0;
9724
9725  open_db(p, 0);
9726  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
9727  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
9728  BEGIN_TIMER;
9729  rc = shell_exec(p, zSql, &zErrMsg);
9730  END_TIMER;
9731  if( rc || zErrMsg ){
9732    char zPrefix[100];
9733    if( in!=0 || !stdin_is_interactive ){
9734      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
9735                       "Error: near line %d:", startline);
9736    }else{
9737      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
9738    }
9739    if( zErrMsg!=0 ){
9740      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
9741      sqlite3_free(zErrMsg);
9742      zErrMsg = 0;
9743    }else{
9744      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
9745    }
9746    return 1;
9747  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
9748    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
9749            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
9750  }
9751  return 0;
9752}
9753
9754
9755/*
9756** Read input from *in and process it.  If *in==0 then input
9757** is interactive - the user is typing it it.  Otherwise, input
9758** is coming from a file or device.  A prompt is issued and history
9759** is saved only if input is interactive.  An interrupt signal will
9760** cause this routine to exit immediately, unless input is interactive.
9761**
9762** Return the number of errors.
9763*/
9764static int process_input(ShellState *p){
9765  char *zLine = 0;          /* A single input line */
9766  char *zSql = 0;           /* Accumulated SQL text */
9767  int nLine;                /* Length of current line */
9768  int nSql = 0;             /* Bytes of zSql[] used */
9769  int nAlloc = 0;           /* Allocated zSql[] space */
9770  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
9771  int rc;                   /* Error code */
9772  int errCnt = 0;           /* Number of errors seen */
9773  int startline = 0;        /* Line number for start of current input */
9774
9775  p->lineno = 0;
9776  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
9777    fflush(p->out);
9778    zLine = one_input_line(p->in, zLine, nSql>0);
9779    if( zLine==0 ){
9780      /* End of input */
9781      if( p->in==0 && stdin_is_interactive ) printf("\n");
9782      break;
9783    }
9784    if( seenInterrupt ){
9785      if( p->in!=0 ) break;
9786      seenInterrupt = 0;
9787    }
9788    p->lineno++;
9789    if( nSql==0 && _all_whitespace(zLine) ){
9790      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
9791      continue;
9792    }
9793    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
9794      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
9795      if( zLine[0]=='.' ){
9796        rc = do_meta_command(zLine, p);
9797        if( rc==2 ){ /* exit requested */
9798          break;
9799        }else if( rc ){
9800          errCnt++;
9801        }
9802      }
9803      continue;
9804    }
9805    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
9806      memcpy(zLine,";",2);
9807    }
9808    nLine = strlen30(zLine);
9809    if( nSql+nLine+2>=nAlloc ){
9810      nAlloc = nSql+nLine+100;
9811      zSql = realloc(zSql, nAlloc);
9812      if( zSql==0 ) shell_out_of_memory();
9813    }
9814    nSqlPrior = nSql;
9815    if( nSql==0 ){
9816      int i;
9817      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
9818      assert( nAlloc>0 && zSql!=0 );
9819      memcpy(zSql, zLine+i, nLine+1-i);
9820      startline = p->lineno;
9821      nSql = nLine-i;
9822    }else{
9823      zSql[nSql++] = '\n';
9824      memcpy(zSql+nSql, zLine, nLine+1);
9825      nSql += nLine;
9826    }
9827    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
9828                && sqlite3_complete(zSql) ){
9829      errCnt += runOneSqlLine(p, zSql, p->in, startline);
9830      nSql = 0;
9831      if( p->outCount ){
9832        output_reset(p);
9833        p->outCount = 0;
9834      }else{
9835        clearTempFile(p);
9836      }
9837    }else if( nSql && _all_whitespace(zSql) ){
9838      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
9839      nSql = 0;
9840    }
9841  }
9842  if( nSql && !_all_whitespace(zSql) ){
9843    errCnt += runOneSqlLine(p, zSql, p->in, startline);
9844  }
9845  free(zSql);
9846  free(zLine);
9847  return errCnt>0;
9848}
9849
9850/*
9851** Return a pathname which is the user's home directory.  A
9852** 0 return indicates an error of some kind.
9853*/
9854static char *find_home_dir(int clearFlag){
9855  static char *home_dir = NULL;
9856  if( clearFlag ){
9857    free(home_dir);
9858    home_dir = 0;
9859    return 0;
9860  }
9861  if( home_dir ) return home_dir;
9862
9863#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
9864     && !defined(__RTP__) && !defined(_WRS_KERNEL)
9865  {
9866    struct passwd *pwent;
9867    uid_t uid = getuid();
9868    if( (pwent=getpwuid(uid)) != NULL) {
9869      home_dir = pwent->pw_dir;
9870    }
9871  }
9872#endif
9873
9874#if defined(_WIN32_WCE)
9875  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
9876   */
9877  home_dir = "/";
9878#else
9879
9880#if defined(_WIN32) || defined(WIN32)
9881  if (!home_dir) {
9882    home_dir = getenv("USERPROFILE");
9883  }
9884#endif
9885
9886  if (!home_dir) {
9887    home_dir = getenv("HOME");
9888  }
9889
9890#if defined(_WIN32) || defined(WIN32)
9891  if (!home_dir) {
9892    char *zDrive, *zPath;
9893    int n;
9894    zDrive = getenv("HOMEDRIVE");
9895    zPath = getenv("HOMEPATH");
9896    if( zDrive && zPath ){
9897      n = strlen30(zDrive) + strlen30(zPath) + 1;
9898      home_dir = malloc( n );
9899      if( home_dir==0 ) return 0;
9900      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
9901      return home_dir;
9902    }
9903    home_dir = "c:\\";
9904  }
9905#endif
9906
9907#endif /* !_WIN32_WCE */
9908
9909  if( home_dir ){
9910    int n = strlen30(home_dir) + 1;
9911    char *z = malloc( n );
9912    if( z ) memcpy(z, home_dir, n);
9913    home_dir = z;
9914  }
9915
9916  return home_dir;
9917}
9918
9919/*
9920** Read input from the file given by sqliterc_override.  Or if that
9921** parameter is NULL, take input from ~/.sqliterc
9922**
9923** Returns the number of errors.
9924*/
9925static void process_sqliterc(
9926  ShellState *p,                  /* Configuration data */
9927  const char *sqliterc_override   /* Name of config file. NULL to use default */
9928){
9929  char *home_dir = NULL;
9930  const char *sqliterc = sqliterc_override;
9931  char *zBuf = 0;
9932  FILE *inSaved = p->in;
9933  int savedLineno = p->lineno;
9934
9935  if (sqliterc == NULL) {
9936    home_dir = find_home_dir(0);
9937    if( home_dir==0 ){
9938      raw_printf(stderr, "-- warning: cannot find home directory;"
9939                      " cannot read ~/.sqliterc\n");
9940      return;
9941    }
9942    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
9943    sqliterc = zBuf;
9944  }
9945  p->in = fopen(sqliterc,"rb");
9946  if( p->in ){
9947    if( stdin_is_interactive ){
9948      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
9949    }
9950    process_input(p);
9951    fclose(p->in);
9952  }
9953  p->in = inSaved;
9954  p->lineno = savedLineno;
9955  sqlite3_free(zBuf);
9956}
9957
9958/*
9959** Show available command line options
9960*/
9961static const char zOptions[] =
9962#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
9963  "   -A ARGS...           run \".archive ARGS\" and exit\n"
9964#endif
9965  "   -append              append the database to the end of the file\n"
9966  "   -ascii               set output mode to 'ascii'\n"
9967  "   -bail                stop after hitting an error\n"
9968  "   -batch               force batch I/O\n"
9969  "   -column              set output mode to 'column'\n"
9970  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
9971  "   -csv                 set output mode to 'csv'\n"
9972#if defined(SQLITE_ENABLE_DESERIALIZE)
9973  "   -deserialize         open the database using sqlite3_deserialize()\n"
9974#endif
9975  "   -echo                print commands before execution\n"
9976  "   -init FILENAME       read/process named file\n"
9977  "   -[no]header          turn headers on or off\n"
9978#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
9979  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
9980#endif
9981  "   -help                show this message\n"
9982  "   -html                set output mode to HTML\n"
9983  "   -interactive         force interactive I/O\n"
9984  "   -line                set output mode to 'line'\n"
9985  "   -list                set output mode to 'list'\n"
9986  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
9987#if defined(SQLITE_ENABLE_DESERIALIZE)
9988  "   -maxsize N           maximum size for a --deserialize database\n"
9989#endif
9990  "   -memtrace            trace all memory allocations and deallocations\n"
9991  "   -mmap N              default mmap size set to N\n"
9992#ifdef SQLITE_ENABLE_MULTIPLEX
9993  "   -multiplex           enable the multiplexor VFS\n"
9994#endif
9995  "   -newline SEP         set output row separator. Default: '\\n'\n"
9996  "   -nofollow            refuse to open symbolic links to database files\n"
9997  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
9998  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
9999  "   -quote               set output mode to 'quote'\n"
10000  "   -readonly            open the database read-only\n"
10001  "   -separator SEP       set output column separator. Default: '|'\n"
10002#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10003  "   -sorterref SIZE      sorter references threshold size\n"
10004#endif
10005  "   -stats               print memory stats before each finalize\n"
10006  "   -version             show SQLite version\n"
10007  "   -vfs NAME            use NAME as the default VFS\n"
10008#ifdef SQLITE_ENABLE_VFSTRACE
10009  "   -vfstrace            enable tracing of all VFS calls\n"
10010#endif
10011#ifdef SQLITE_HAVE_ZLIB
10012  "   -zip                 open the file as a ZIP Archive\n"
10013#endif
10014;
10015static void usage(int showDetail){
10016  utf8_printf(stderr,
10017      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10018      "FILENAME is the name of an SQLite database. A new database is created\n"
10019      "if the file does not previously exist.\n", Argv0);
10020  if( showDetail ){
10021    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10022  }else{
10023    raw_printf(stderr, "Use the -help option for additional information\n");
10024  }
10025  exit(1);
10026}
10027
10028/*
10029** Internal check:  Verify that the SQLite is uninitialized.  Print a
10030** error message if it is initialized.
10031*/
10032static void verify_uninitialized(void){
10033  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10034    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10035                        " initialization.\n");
10036  }
10037}
10038
10039/*
10040** Initialize the state information in data
10041*/
10042static void main_init(ShellState *data) {
10043  memset(data, 0, sizeof(*data));
10044  data->normalMode = data->cMode = data->mode = MODE_List;
10045  data->autoExplain = 1;
10046  memcpy(data->colSeparator,SEP_Column, 2);
10047  memcpy(data->rowSeparator,SEP_Row, 2);
10048  data->showHeader = 0;
10049  data->shellFlgs = SHFLG_Lookaside;
10050  verify_uninitialized();
10051  sqlite3_config(SQLITE_CONFIG_URI, 1);
10052  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10053  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10054  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10055  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10056}
10057
10058/*
10059** Output text to the console in a font that attracts extra attention.
10060*/
10061#ifdef _WIN32
10062static void printBold(const char *zText){
10063  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10064  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10065  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10066  SetConsoleTextAttribute(out,
10067         FOREGROUND_RED|FOREGROUND_INTENSITY
10068  );
10069  printf("%s", zText);
10070  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10071}
10072#else
10073static void printBold(const char *zText){
10074  printf("\033[1m%s\033[0m", zText);
10075}
10076#endif
10077
10078/*
10079** Get the argument to an --option.  Throw an error and die if no argument
10080** is available.
10081*/
10082static char *cmdline_option_value(int argc, char **argv, int i){
10083  if( i==argc ){
10084    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10085            argv[0], argv[argc-1]);
10086    exit(1);
10087  }
10088  return argv[i];
10089}
10090
10091#ifndef SQLITE_SHELL_IS_UTF8
10092#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
10093#    define SQLITE_SHELL_IS_UTF8          (0)
10094#  else
10095#    define SQLITE_SHELL_IS_UTF8          (1)
10096#  endif
10097#endif
10098
10099#if SQLITE_SHELL_IS_UTF8
10100int SQLITE_CDECL main(int argc, char **argv){
10101#else
10102int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10103  char **argv;
10104#endif
10105  char *zErrMsg = 0;
10106  ShellState data;
10107  const char *zInitFile = 0;
10108  int i;
10109  int rc = 0;
10110  int warnInmemoryDb = 0;
10111  int readStdin = 1;
10112  int nCmd = 0;
10113  char **azCmd = 0;
10114  const char *zVfs = 0;           /* Value of -vfs command-line option */
10115#if !SQLITE_SHELL_IS_UTF8
10116  char **argvToFree = 0;
10117  int argcToFree = 0;
10118#endif
10119
10120  setBinaryMode(stdin, 0);
10121  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10122  stdin_is_interactive = isatty(0);
10123  stdout_is_console = isatty(1);
10124
10125#if !defined(_WIN32_WCE)
10126  if( getenv("SQLITE_DEBUG_BREAK") ){
10127    if( isatty(0) && isatty(2) ){
10128      fprintf(stderr,
10129          "attach debugger to process %d and press any key to continue.\n",
10130          GETPID());
10131      fgetc(stdin);
10132    }else{
10133#if defined(_WIN32) || defined(WIN32)
10134      DebugBreak();
10135#elif defined(SIGTRAP)
10136      raise(SIGTRAP);
10137#endif
10138    }
10139  }
10140#endif
10141
10142#if USE_SYSTEM_SQLITE+0!=1
10143  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10144    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10145            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10146    exit(1);
10147  }
10148#endif
10149  main_init(&data);
10150
10151  /* On Windows, we must translate command-line arguments into UTF-8.
10152  ** The SQLite memory allocator subsystem has to be enabled in order to
10153  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10154  ** subsequent sqlite3_config() calls will work.  So copy all results into
10155  ** memory that does not come from the SQLite memory allocator.
10156  */
10157#if !SQLITE_SHELL_IS_UTF8
10158  sqlite3_initialize();
10159  argvToFree = malloc(sizeof(argv[0])*argc*2);
10160  argcToFree = argc;
10161  argv = argvToFree + argc;
10162  if( argv==0 ) shell_out_of_memory();
10163  for(i=0; i<argc; i++){
10164    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10165    int n;
10166    if( z==0 ) shell_out_of_memory();
10167    n = (int)strlen(z);
10168    argv[i] = malloc( n+1 );
10169    if( argv[i]==0 ) shell_out_of_memory();
10170    memcpy(argv[i], z, n+1);
10171    argvToFree[i] = argv[i];
10172    sqlite3_free(z);
10173  }
10174  sqlite3_shutdown();
10175#endif
10176
10177  assert( argc>=1 && argv && argv[0] );
10178  Argv0 = argv[0];
10179
10180  /* Make sure we have a valid signal handler early, before anything
10181  ** else is done.
10182  */
10183#ifdef SIGINT
10184  signal(SIGINT, interrupt_handler);
10185#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10186  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10187#endif
10188
10189#ifdef SQLITE_SHELL_DBNAME_PROC
10190  {
10191    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10192    ** of a C-function that will provide the name of the database file.  Use
10193    ** this compile-time option to embed this shell program in larger
10194    ** applications. */
10195    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10196    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10197    warnInmemoryDb = 0;
10198  }
10199#endif
10200
10201  /* Do an initial pass through the command-line argument to locate
10202  ** the name of the database file, the name of the initialization file,
10203  ** the size of the alternative malloc heap,
10204  ** and the first command to execute.
10205  */
10206  verify_uninitialized();
10207  for(i=1; i<argc; i++){
10208    char *z;
10209    z = argv[i];
10210    if( z[0]!='-' ){
10211      if( data.zDbFilename==0 ){
10212        data.zDbFilename = z;
10213      }else{
10214        /* Excesss arguments are interpreted as SQL (or dot-commands) and
10215        ** mean that nothing is read from stdin */
10216        readStdin = 0;
10217        nCmd++;
10218        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
10219        if( azCmd==0 ) shell_out_of_memory();
10220        azCmd[nCmd-1] = z;
10221      }
10222    }
10223    if( z[1]=='-' ) z++;
10224    if( strcmp(z,"-separator")==0
10225     || strcmp(z,"-nullvalue")==0
10226     || strcmp(z,"-newline")==0
10227     || strcmp(z,"-cmd")==0
10228    ){
10229      (void)cmdline_option_value(argc, argv, ++i);
10230    }else if( strcmp(z,"-init")==0 ){
10231      zInitFile = cmdline_option_value(argc, argv, ++i);
10232    }else if( strcmp(z,"-batch")==0 ){
10233      /* Need to check for batch mode here to so we can avoid printing
10234      ** informational messages (like from process_sqliterc) before
10235      ** we do the actual processing of arguments later in a second pass.
10236      */
10237      stdin_is_interactive = 0;
10238    }else if( strcmp(z,"-heap")==0 ){
10239#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10240      const char *zSize;
10241      sqlite3_int64 szHeap;
10242
10243      zSize = cmdline_option_value(argc, argv, ++i);
10244      szHeap = integerValue(zSize);
10245      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
10246      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
10247#else
10248      (void)cmdline_option_value(argc, argv, ++i);
10249#endif
10250    }else if( strcmp(z,"-pagecache")==0 ){
10251      int n, sz;
10252      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10253      if( sz>70000 ) sz = 70000;
10254      if( sz<0 ) sz = 0;
10255      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10256      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
10257                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
10258      data.shellFlgs |= SHFLG_Pagecache;
10259    }else if( strcmp(z,"-lookaside")==0 ){
10260      int n, sz;
10261      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10262      if( sz<0 ) sz = 0;
10263      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10264      if( n<0 ) n = 0;
10265      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
10266      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
10267#ifdef SQLITE_ENABLE_VFSTRACE
10268    }else if( strcmp(z,"-vfstrace")==0 ){
10269      extern int vfstrace_register(
10270         const char *zTraceName,
10271         const char *zOldVfsName,
10272         int (*xOut)(const char*,void*),
10273         void *pOutArg,
10274         int makeDefault
10275      );
10276      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
10277#endif
10278#ifdef SQLITE_ENABLE_MULTIPLEX
10279    }else if( strcmp(z,"-multiplex")==0 ){
10280      extern int sqlite3_multiple_initialize(const char*,int);
10281      sqlite3_multiplex_initialize(0, 1);
10282#endif
10283    }else if( strcmp(z,"-mmap")==0 ){
10284      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10285      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
10286#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10287    }else if( strcmp(z,"-sorterref")==0 ){
10288      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10289      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
10290#endif
10291    }else if( strcmp(z,"-vfs")==0 ){
10292      zVfs = cmdline_option_value(argc, argv, ++i);
10293#ifdef SQLITE_HAVE_ZLIB
10294    }else if( strcmp(z,"-zip")==0 ){
10295      data.openMode = SHELL_OPEN_ZIPFILE;
10296#endif
10297    }else if( strcmp(z,"-append")==0 ){
10298      data.openMode = SHELL_OPEN_APPENDVFS;
10299#ifdef SQLITE_ENABLE_DESERIALIZE
10300    }else if( strcmp(z,"-deserialize")==0 ){
10301      data.openMode = SHELL_OPEN_DESERIALIZE;
10302    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10303      data.szMax = integerValue(argv[++i]);
10304#endif
10305    }else if( strcmp(z,"-readonly")==0 ){
10306      data.openMode = SHELL_OPEN_READONLY;
10307    }else if( strcmp(z,"-nofollow")==0 ){
10308      data.openFlags = SQLITE_OPEN_NOFOLLOW;
10309#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
10310    }else if( strncmp(z, "-A",2)==0 ){
10311      /* All remaining command-line arguments are passed to the ".archive"
10312      ** command, so ignore them */
10313      break;
10314#endif
10315    }else if( strcmp(z, "-memtrace")==0 ){
10316      sqlite3MemTraceActivate(stderr);
10317    }
10318  }
10319  verify_uninitialized();
10320
10321
10322#ifdef SQLITE_SHELL_INIT_PROC
10323  {
10324    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
10325    ** of a C-function that will perform initialization actions on SQLite that
10326    ** occur just before or after sqlite3_initialize(). Use this compile-time
10327    ** option to embed this shell program in larger applications. */
10328    extern void SQLITE_SHELL_INIT_PROC(void);
10329    SQLITE_SHELL_INIT_PROC();
10330  }
10331#else
10332  /* All the sqlite3_config() calls have now been made. So it is safe
10333  ** to call sqlite3_initialize() and process any command line -vfs option. */
10334  sqlite3_initialize();
10335#endif
10336
10337  if( zVfs ){
10338    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
10339    if( pVfs ){
10340      sqlite3_vfs_register(pVfs, 1);
10341    }else{
10342      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
10343      exit(1);
10344    }
10345  }
10346
10347  if( data.zDbFilename==0 ){
10348#ifndef SQLITE_OMIT_MEMORYDB
10349    data.zDbFilename = ":memory:";
10350    warnInmemoryDb = argc==1;
10351#else
10352    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
10353    return 1;
10354#endif
10355  }
10356  data.out = stdout;
10357  sqlite3_appendvfs_init(0,0,0);
10358
10359  /* Go ahead and open the database file if it already exists.  If the
10360  ** file does not exist, delay opening it.  This prevents empty database
10361  ** files from being created if a user mistypes the database name argument
10362  ** to the sqlite command-line tool.
10363  */
10364  if( access(data.zDbFilename, 0)==0 ){
10365    open_db(&data, 0);
10366  }
10367
10368  /* Process the initialization file if there is one.  If no -init option
10369  ** is given on the command line, look for a file named ~/.sqliterc and
10370  ** try to process it.
10371  */
10372  process_sqliterc(&data,zInitFile);
10373
10374  /* Make a second pass through the command-line argument and set
10375  ** options.  This second pass is delayed until after the initialization
10376  ** file is processed so that the command-line arguments will override
10377  ** settings in the initialization file.
10378  */
10379  for(i=1; i<argc; i++){
10380    char *z = argv[i];
10381    if( z[0]!='-' ) continue;
10382    if( z[1]=='-' ){ z++; }
10383    if( strcmp(z,"-init")==0 ){
10384      i++;
10385    }else if( strcmp(z,"-html")==0 ){
10386      data.mode = MODE_Html;
10387    }else if( strcmp(z,"-list")==0 ){
10388      data.mode = MODE_List;
10389    }else if( strcmp(z,"-quote")==0 ){
10390      data.mode = MODE_Quote;
10391    }else if( strcmp(z,"-line")==0 ){
10392      data.mode = MODE_Line;
10393    }else if( strcmp(z,"-column")==0 ){
10394      data.mode = MODE_Column;
10395    }else if( strcmp(z,"-csv")==0 ){
10396      data.mode = MODE_Csv;
10397      memcpy(data.colSeparator,",",2);
10398#ifdef SQLITE_HAVE_ZLIB
10399    }else if( strcmp(z,"-zip")==0 ){
10400      data.openMode = SHELL_OPEN_ZIPFILE;
10401#endif
10402    }else if( strcmp(z,"-append")==0 ){
10403      data.openMode = SHELL_OPEN_APPENDVFS;
10404#ifdef SQLITE_ENABLE_DESERIALIZE
10405    }else if( strcmp(z,"-deserialize")==0 ){
10406      data.openMode = SHELL_OPEN_DESERIALIZE;
10407    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10408      data.szMax = integerValue(argv[++i]);
10409#endif
10410    }else if( strcmp(z,"-readonly")==0 ){
10411      data.openMode = SHELL_OPEN_READONLY;
10412    }else if( strcmp(z,"-nofollow")==0 ){
10413      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
10414    }else if( strcmp(z,"-ascii")==0 ){
10415      data.mode = MODE_Ascii;
10416      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
10417                       SEP_Unit);
10418      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
10419                       SEP_Record);
10420    }else if( strcmp(z,"-separator")==0 ){
10421      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
10422                       "%s",cmdline_option_value(argc,argv,++i));
10423    }else if( strcmp(z,"-newline")==0 ){
10424      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
10425                       "%s",cmdline_option_value(argc,argv,++i));
10426    }else if( strcmp(z,"-nullvalue")==0 ){
10427      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
10428                       "%s",cmdline_option_value(argc,argv,++i));
10429    }else if( strcmp(z,"-header")==0 ){
10430      data.showHeader = 1;
10431    }else if( strcmp(z,"-noheader")==0 ){
10432      data.showHeader = 0;
10433    }else if( strcmp(z,"-echo")==0 ){
10434      ShellSetFlag(&data, SHFLG_Echo);
10435    }else if( strcmp(z,"-eqp")==0 ){
10436      data.autoEQP = AUTOEQP_on;
10437    }else if( strcmp(z,"-eqpfull")==0 ){
10438      data.autoEQP = AUTOEQP_full;
10439    }else if( strcmp(z,"-stats")==0 ){
10440      data.statsOn = 1;
10441    }else if( strcmp(z,"-scanstats")==0 ){
10442      data.scanstatsOn = 1;
10443    }else if( strcmp(z,"-backslash")==0 ){
10444      /* Undocumented command-line option: -backslash
10445      ** Causes C-style backslash escapes to be evaluated in SQL statements
10446      ** prior to sending the SQL into SQLite.  Useful for injecting
10447      ** crazy bytes in the middle of SQL statements for testing and debugging.
10448      */
10449      ShellSetFlag(&data, SHFLG_Backslash);
10450    }else if( strcmp(z,"-bail")==0 ){
10451      bail_on_error = 1;
10452    }else if( strcmp(z,"-version")==0 ){
10453      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
10454      return 0;
10455    }else if( strcmp(z,"-interactive")==0 ){
10456      stdin_is_interactive = 1;
10457    }else if( strcmp(z,"-batch")==0 ){
10458      stdin_is_interactive = 0;
10459    }else if( strcmp(z,"-heap")==0 ){
10460      i++;
10461    }else if( strcmp(z,"-pagecache")==0 ){
10462      i+=2;
10463    }else if( strcmp(z,"-lookaside")==0 ){
10464      i+=2;
10465    }else if( strcmp(z,"-mmap")==0 ){
10466      i++;
10467    }else if( strcmp(z,"-memtrace")==0 ){
10468      i++;
10469#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10470    }else if( strcmp(z,"-sorterref")==0 ){
10471      i++;
10472#endif
10473    }else if( strcmp(z,"-vfs")==0 ){
10474      i++;
10475#ifdef SQLITE_ENABLE_VFSTRACE
10476    }else if( strcmp(z,"-vfstrace")==0 ){
10477      i++;
10478#endif
10479#ifdef SQLITE_ENABLE_MULTIPLEX
10480    }else if( strcmp(z,"-multiplex")==0 ){
10481      i++;
10482#endif
10483    }else if( strcmp(z,"-help")==0 ){
10484      usage(1);
10485    }else if( strcmp(z,"-cmd")==0 ){
10486      /* Run commands that follow -cmd first and separately from commands
10487      ** that simply appear on the command-line.  This seems goofy.  It would
10488      ** be better if all commands ran in the order that they appear.  But
10489      ** we retain the goofy behavior for historical compatibility. */
10490      if( i==argc-1 ) break;
10491      z = cmdline_option_value(argc,argv,++i);
10492      if( z[0]=='.' ){
10493        rc = do_meta_command(z, &data);
10494        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
10495      }else{
10496        open_db(&data, 0);
10497        rc = shell_exec(&data, z, &zErrMsg);
10498        if( zErrMsg!=0 ){
10499          utf8_printf(stderr,"Error: %s\n", zErrMsg);
10500          if( bail_on_error ) return rc!=0 ? rc : 1;
10501        }else if( rc!=0 ){
10502          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
10503          if( bail_on_error ) return rc;
10504        }
10505      }
10506#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
10507    }else if( strncmp(z, "-A", 2)==0 ){
10508      if( nCmd>0 ){
10509        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
10510                            " with \"%s\"\n", z);
10511        return 1;
10512      }
10513      open_db(&data, OPEN_DB_ZIPFILE);
10514      if( z[2] ){
10515        argv[i] = &z[2];
10516        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
10517      }else{
10518        arDotCommand(&data, 1, argv+i, argc-i);
10519      }
10520      readStdin = 0;
10521      break;
10522#endif
10523    }else{
10524      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
10525      raw_printf(stderr,"Use -help for a list of options.\n");
10526      return 1;
10527    }
10528    data.cMode = data.mode;
10529  }
10530
10531  if( !readStdin ){
10532    /* Run all arguments that do not begin with '-' as if they were separate
10533    ** command-line inputs, except for the argToSkip argument which contains
10534    ** the database filename.
10535    */
10536    for(i=0; i<nCmd; i++){
10537      if( azCmd[i][0]=='.' ){
10538        rc = do_meta_command(azCmd[i], &data);
10539        if( rc ) return rc==2 ? 0 : rc;
10540      }else{
10541        open_db(&data, 0);
10542        rc = shell_exec(&data, azCmd[i], &zErrMsg);
10543        if( zErrMsg!=0 ){
10544          utf8_printf(stderr,"Error: %s\n", zErrMsg);
10545          return rc!=0 ? rc : 1;
10546        }else if( rc!=0 ){
10547          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
10548          return rc;
10549        }
10550      }
10551    }
10552    free(azCmd);
10553  }else{
10554    /* Run commands received from standard input
10555    */
10556    if( stdin_is_interactive ){
10557      char *zHome;
10558      char *zHistory;
10559      int nHistory;
10560      printf(
10561        "SQLite version %s %.19s\n" /*extra-version-info*/
10562        "Enter \".help\" for usage hints.\n",
10563        sqlite3_libversion(), sqlite3_sourceid()
10564      );
10565      if( warnInmemoryDb ){
10566        printf("Connected to a ");
10567        printBold("transient in-memory database");
10568        printf(".\nUse \".open FILENAME\" to reopen on a "
10569               "persistent database.\n");
10570      }
10571      zHistory = getenv("SQLITE_HISTORY");
10572      if( zHistory ){
10573        zHistory = strdup(zHistory);
10574      }else if( (zHome = find_home_dir(0))!=0 ){
10575        nHistory = strlen30(zHome) + 20;
10576        if( (zHistory = malloc(nHistory))!=0 ){
10577          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
10578        }
10579      }
10580      if( zHistory ){ shell_read_history(zHistory); }
10581#if HAVE_READLINE || HAVE_EDITLINE
10582      rl_attempted_completion_function = readline_completion;
10583#elif HAVE_LINENOISE
10584      linenoiseSetCompletionCallback(linenoise_completion);
10585#endif
10586      data.in = 0;
10587      rc = process_input(&data);
10588      if( zHistory ){
10589        shell_stifle_history(2000);
10590        shell_write_history(zHistory);
10591        free(zHistory);
10592      }
10593    }else{
10594      data.in = stdin;
10595      rc = process_input(&data);
10596    }
10597  }
10598  set_table_name(&data, 0);
10599  if( data.db ){
10600    session_close_all(&data);
10601    close_db(data.db);
10602  }
10603  sqlite3_free(data.zFreeOnClose);
10604  find_home_dir(1);
10605  output_reset(&data);
10606  data.doXdgOpen = 0;
10607  clearTempFile(&data);
10608#if !SQLITE_SHELL_IS_UTF8
10609  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
10610  free(argvToFree);
10611#endif
10612  /* Clear the global data structure so that valgrind will detect memory
10613  ** leaks */
10614  memset(&data, 0, sizeof(data));
10615  return rc;
10616}
10617