xref: /sqlite-3.40.0/src/shell.c.in (revision b88eaf16)
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  FILE *in;              /* Read commands from this stream */
1025  FILE *out;             /* Write results here */
1026  FILE *traceOut;        /* Output for sqlite3_trace() */
1027  int nErr;              /* Number of errors seen */
1028  int mode;              /* An output mode setting */
1029  int modePrior;         /* Saved mode */
1030  int cMode;             /* temporary output mode for the current query */
1031  int normalMode;        /* Output mode before ".explain on" */
1032  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1033  int showHeader;        /* True to show column names in List or Column mode */
1034  int nCheck;            /* Number of ".check" commands run */
1035  unsigned nProgress;    /* Number of progress callbacks encountered */
1036  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1037  unsigned flgProgress;  /* Flags for the progress callback */
1038  unsigned shellFlgs;    /* Various flags */
1039  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1040  char *zDestTable;      /* Name of destination table when MODE_Insert */
1041  char *zTempFile;       /* Temporary file that might need deleting */
1042  char zTestcase[30];    /* Name of current test case */
1043  char colSeparator[20]; /* Column separator character for several modes */
1044  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1045  char colSepPrior[20];  /* Saved column separator */
1046  char rowSepPrior[20];  /* Saved row separator */
1047  int colWidth[100];     /* Requested width of each column when in column mode*/
1048  int actualWidth[100];  /* Actual width of each column */
1049  char nullValue[20];    /* The text to print when a NULL comes back from
1050                         ** the database */
1051  char outfile[FILENAME_MAX]; /* Filename for *out */
1052  const char *zDbFilename;    /* name of the database file */
1053  char *zFreeOnClose;         /* Filename to free when closing */
1054  const char *zVfs;           /* Name of VFS to use */
1055  sqlite3_stmt *pStmt;   /* Current statement if any. */
1056  FILE *pLog;            /* Write log output here */
1057  int *aiIndent;         /* Array of indents used in MODE_Explain */
1058  int nIndent;           /* Size of array aiIndent[] */
1059  int iIndent;           /* Index of current op in aiIndent[] */
1060  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1061#if defined(SQLITE_ENABLE_SESSION)
1062  int nSession;             /* Number of active sessions */
1063  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1064#endif
1065  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1066};
1067
1068
1069/* Allowed values for ShellState.autoEQP
1070*/
1071#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1072#define AUTOEQP_on       1           /* Automatic EQP is on */
1073#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1074#define AUTOEQP_full     3           /* Show full EXPLAIN */
1075
1076/* Allowed values for ShellState.openMode
1077*/
1078#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1079#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1080#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1081#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1082#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1083#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1084#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1085
1086/* Allowed values for ShellState.eTraceType
1087*/
1088#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1089#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1090#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1091
1092/* Bits in the ShellState.flgProgress variable */
1093#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1094#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1095                                   ** callback limit is reached, and for each
1096                                   ** top-level SQL statement */
1097#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1098
1099/*
1100** These are the allowed shellFlgs values
1101*/
1102#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1103#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1104#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1105#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1106#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1107#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1108#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1109
1110/*
1111** Macros for testing and setting shellFlgs
1112*/
1113#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1114#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1115#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1116
1117/*
1118** These are the allowed modes.
1119*/
1120#define MODE_Line     0  /* One column per line.  Blank line between records */
1121#define MODE_Column   1  /* One record per line in neat columns */
1122#define MODE_List     2  /* One record per line with a separator */
1123#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1124#define MODE_Html     4  /* Generate an XHTML table */
1125#define MODE_Insert   5  /* Generate SQL "insert" statements */
1126#define MODE_Quote    6  /* Quote values as for SQL */
1127#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1128#define MODE_Csv      8  /* Quote strings, numbers are plain */
1129#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1130#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1131#define MODE_Pretty  11  /* Pretty-print schemas */
1132#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1133
1134static const char *modeDescr[] = {
1135  "line",
1136  "column",
1137  "list",
1138  "semi",
1139  "html",
1140  "insert",
1141  "quote",
1142  "tcl",
1143  "csv",
1144  "explain",
1145  "ascii",
1146  "prettyprint",
1147  "eqp"
1148};
1149
1150/*
1151** These are the column/row/line separators used by the various
1152** import/export modes.
1153*/
1154#define SEP_Column    "|"
1155#define SEP_Row       "\n"
1156#define SEP_Tab       "\t"
1157#define SEP_Space     " "
1158#define SEP_Comma     ","
1159#define SEP_CrLf      "\r\n"
1160#define SEP_Unit      "\x1F"
1161#define SEP_Record    "\x1E"
1162
1163/*
1164** A callback for the sqlite3_log() interface.
1165*/
1166static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1167  ShellState *p = (ShellState*)pArg;
1168  if( p->pLog==0 ) return;
1169  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1170  fflush(p->pLog);
1171}
1172
1173/*
1174** SQL function:  shell_putsnl(X)
1175**
1176** Write the text X to the screen (or whatever output is being directed)
1177** adding a newline at the end, and then return X.
1178*/
1179static void shellPutsFunc(
1180  sqlite3_context *pCtx,
1181  int nVal,
1182  sqlite3_value **apVal
1183){
1184  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1185  (void)nVal;
1186  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1187  sqlite3_result_value(pCtx, apVal[0]);
1188}
1189
1190/*
1191** SQL function:   edit(VALUE)
1192**                 edit(VALUE,EDITOR)
1193**
1194** These steps:
1195**
1196**     (1) Write VALUE into a temporary file.
1197**     (2) Run program EDITOR on that temporary file.
1198**     (3) Read the temporary file back and return its content as the result.
1199**     (4) Delete the temporary file
1200**
1201** If the EDITOR argument is omitted, use the value in the VISUAL
1202** environment variable.  If still there is no EDITOR, through an error.
1203**
1204** Also throw an error if the EDITOR program returns a non-zero exit code.
1205*/
1206#ifndef SQLITE_NOHAVE_SYSTEM
1207static void editFunc(
1208  sqlite3_context *context,
1209  int argc,
1210  sqlite3_value **argv
1211){
1212  const char *zEditor;
1213  char *zTempFile = 0;
1214  sqlite3 *db;
1215  char *zCmd = 0;
1216  int bBin;
1217  int rc;
1218  int hasCRNL = 0;
1219  FILE *f = 0;
1220  sqlite3_int64 sz;
1221  sqlite3_int64 x;
1222  unsigned char *p = 0;
1223
1224  if( argc==2 ){
1225    zEditor = (const char*)sqlite3_value_text(argv[1]);
1226  }else{
1227    zEditor = getenv("VISUAL");
1228  }
1229  if( zEditor==0 ){
1230    sqlite3_result_error(context, "no editor for edit()", -1);
1231    return;
1232  }
1233  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1234    sqlite3_result_error(context, "NULL input to edit()", -1);
1235    return;
1236  }
1237  db = sqlite3_context_db_handle(context);
1238  zTempFile = 0;
1239  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1240  if( zTempFile==0 ){
1241    sqlite3_uint64 r = 0;
1242    sqlite3_randomness(sizeof(r), &r);
1243    zTempFile = sqlite3_mprintf("temp%llx", r);
1244    if( zTempFile==0 ){
1245      sqlite3_result_error_nomem(context);
1246      return;
1247    }
1248  }
1249  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1250  /* When writing the file to be edited, do \n to \r\n conversions on systems
1251  ** that want \r\n line endings */
1252  f = fopen(zTempFile, bBin ? "wb" : "w");
1253  if( f==0 ){
1254    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1255    goto edit_func_end;
1256  }
1257  sz = sqlite3_value_bytes(argv[0]);
1258  if( bBin ){
1259    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1260  }else{
1261    const char *z = (const char*)sqlite3_value_text(argv[0]);
1262    /* Remember whether or not the value originally contained \r\n */
1263    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1264    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1265  }
1266  fclose(f);
1267  f = 0;
1268  if( x!=sz ){
1269    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1270    goto edit_func_end;
1271  }
1272  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1273  if( zCmd==0 ){
1274    sqlite3_result_error_nomem(context);
1275    goto edit_func_end;
1276  }
1277  rc = system(zCmd);
1278  sqlite3_free(zCmd);
1279  if( rc ){
1280    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1281    goto edit_func_end;
1282  }
1283  f = fopen(zTempFile, "rb");
1284  if( f==0 ){
1285    sqlite3_result_error(context,
1286      "edit() cannot reopen temp file after edit", -1);
1287    goto edit_func_end;
1288  }
1289  fseek(f, 0, SEEK_END);
1290  sz = ftell(f);
1291  rewind(f);
1292  p = sqlite3_malloc64( sz+1 );
1293  if( p==0 ){
1294    sqlite3_result_error_nomem(context);
1295    goto edit_func_end;
1296  }
1297  x = fread(p, 1, (size_t)sz, f);
1298  fclose(f);
1299  f = 0;
1300  if( x!=sz ){
1301    sqlite3_result_error(context, "could not read back the whole file", -1);
1302    goto edit_func_end;
1303  }
1304  if( bBin ){
1305    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1306  }else{
1307    sqlite3_int64 i, j;
1308    if( hasCRNL ){
1309      /* If the original contains \r\n then do no conversions back to \n */
1310      j = sz;
1311    }else{
1312      /* If the file did not originally contain \r\n then convert any new
1313      ** \r\n back into \n */
1314      for(i=j=0; i<sz; i++){
1315        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1316        p[j++] = p[i];
1317      }
1318      sz = j;
1319      p[sz] = 0;
1320    }
1321    sqlite3_result_text64(context, (const char*)p, sz,
1322                          sqlite3_free, SQLITE_UTF8);
1323  }
1324  p = 0;
1325
1326edit_func_end:
1327  if( f ) fclose(f);
1328  unlink(zTempFile);
1329  sqlite3_free(zTempFile);
1330  sqlite3_free(p);
1331}
1332#endif /* SQLITE_NOHAVE_SYSTEM */
1333
1334/*
1335** Save or restore the current output mode
1336*/
1337static void outputModePush(ShellState *p){
1338  p->modePrior = p->mode;
1339  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1340  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1341}
1342static void outputModePop(ShellState *p){
1343  p->mode = p->modePrior;
1344  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1345  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1346}
1347
1348/*
1349** Output the given string as a hex-encoded blob (eg. X'1234' )
1350*/
1351static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1352  int i;
1353  char *zBlob = (char *)pBlob;
1354  raw_printf(out,"X'");
1355  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1356  raw_printf(out,"'");
1357}
1358
1359/*
1360** Find a string that is not found anywhere in z[].  Return a pointer
1361** to that string.
1362**
1363** Try to use zA and zB first.  If both of those are already found in z[]
1364** then make up some string and store it in the buffer zBuf.
1365*/
1366static const char *unused_string(
1367  const char *z,                    /* Result must not appear anywhere in z */
1368  const char *zA, const char *zB,   /* Try these first */
1369  char *zBuf                        /* Space to store a generated string */
1370){
1371  unsigned i = 0;
1372  if( strstr(z, zA)==0 ) return zA;
1373  if( strstr(z, zB)==0 ) return zB;
1374  do{
1375    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1376  }while( strstr(z,zBuf)!=0 );
1377  return zBuf;
1378}
1379
1380/*
1381** Output the given string as a quoted string using SQL quoting conventions.
1382**
1383** See also: output_quoted_escaped_string()
1384*/
1385static void output_quoted_string(FILE *out, const char *z){
1386  int i;
1387  char c;
1388  setBinaryMode(out, 1);
1389  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1390  if( c==0 ){
1391    utf8_printf(out,"'%s'",z);
1392  }else{
1393    raw_printf(out, "'");
1394    while( *z ){
1395      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1396      if( c=='\'' ) i++;
1397      if( i ){
1398        utf8_printf(out, "%.*s", i, z);
1399        z += i;
1400      }
1401      if( c=='\'' ){
1402        raw_printf(out, "'");
1403        continue;
1404      }
1405      if( c==0 ){
1406        break;
1407      }
1408      z++;
1409    }
1410    raw_printf(out, "'");
1411  }
1412  setTextMode(out, 1);
1413}
1414
1415/*
1416** Output the given string as a quoted string using SQL quoting conventions.
1417** Additionallly , escape the "\n" and "\r" characters so that they do not
1418** get corrupted by end-of-line translation facilities in some operating
1419** systems.
1420**
1421** This is like output_quoted_string() but with the addition of the \r\n
1422** escape mechanism.
1423*/
1424static void output_quoted_escaped_string(FILE *out, const char *z){
1425  int i;
1426  char c;
1427  setBinaryMode(out, 1);
1428  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1429  if( c==0 ){
1430    utf8_printf(out,"'%s'",z);
1431  }else{
1432    const char *zNL = 0;
1433    const char *zCR = 0;
1434    int nNL = 0;
1435    int nCR = 0;
1436    char zBuf1[20], zBuf2[20];
1437    for(i=0; z[i]; i++){
1438      if( z[i]=='\n' ) nNL++;
1439      if( z[i]=='\r' ) nCR++;
1440    }
1441    if( nNL ){
1442      raw_printf(out, "replace(");
1443      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1444    }
1445    if( nCR ){
1446      raw_printf(out, "replace(");
1447      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1448    }
1449    raw_printf(out, "'");
1450    while( *z ){
1451      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1452      if( c=='\'' ) i++;
1453      if( i ){
1454        utf8_printf(out, "%.*s", i, z);
1455        z += i;
1456      }
1457      if( c=='\'' ){
1458        raw_printf(out, "'");
1459        continue;
1460      }
1461      if( c==0 ){
1462        break;
1463      }
1464      z++;
1465      if( c=='\n' ){
1466        raw_printf(out, "%s", zNL);
1467        continue;
1468      }
1469      raw_printf(out, "%s", zCR);
1470    }
1471    raw_printf(out, "'");
1472    if( nCR ){
1473      raw_printf(out, ",'%s',char(13))", zCR);
1474    }
1475    if( nNL ){
1476      raw_printf(out, ",'%s',char(10))", zNL);
1477    }
1478  }
1479  setTextMode(out, 1);
1480}
1481
1482/*
1483** Output the given string as a quoted according to C or TCL quoting rules.
1484*/
1485static void output_c_string(FILE *out, const char *z){
1486  unsigned int c;
1487  fputc('"', out);
1488  while( (c = *(z++))!=0 ){
1489    if( c=='\\' ){
1490      fputc(c, out);
1491      fputc(c, out);
1492    }else if( c=='"' ){
1493      fputc('\\', out);
1494      fputc('"', out);
1495    }else if( c=='\t' ){
1496      fputc('\\', out);
1497      fputc('t', out);
1498    }else if( c=='\n' ){
1499      fputc('\\', out);
1500      fputc('n', out);
1501    }else if( c=='\r' ){
1502      fputc('\\', out);
1503      fputc('r', out);
1504    }else if( !isprint(c&0xff) ){
1505      raw_printf(out, "\\%03o", c&0xff);
1506    }else{
1507      fputc(c, out);
1508    }
1509  }
1510  fputc('"', out);
1511}
1512
1513/*
1514** Output the given string with characters that are special to
1515** HTML escaped.
1516*/
1517static void output_html_string(FILE *out, const char *z){
1518  int i;
1519  if( z==0 ) z = "";
1520  while( *z ){
1521    for(i=0;   z[i]
1522            && z[i]!='<'
1523            && z[i]!='&'
1524            && z[i]!='>'
1525            && z[i]!='\"'
1526            && z[i]!='\'';
1527        i++){}
1528    if( i>0 ){
1529      utf8_printf(out,"%.*s",i,z);
1530    }
1531    if( z[i]=='<' ){
1532      raw_printf(out,"&lt;");
1533    }else if( z[i]=='&' ){
1534      raw_printf(out,"&amp;");
1535    }else if( z[i]=='>' ){
1536      raw_printf(out,"&gt;");
1537    }else if( z[i]=='\"' ){
1538      raw_printf(out,"&quot;");
1539    }else if( z[i]=='\'' ){
1540      raw_printf(out,"&#39;");
1541    }else{
1542      break;
1543    }
1544    z += i + 1;
1545  }
1546}
1547
1548/*
1549** If a field contains any character identified by a 1 in the following
1550** array, then the string must be quoted for CSV.
1551*/
1552static const char needCsvQuote[] = {
1553  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1554  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1555  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1556  0, 0, 0, 0, 0, 0, 0, 0,   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, 1,
1561  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 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};
1570
1571/*
1572** Output a single term of CSV.  Actually, p->colSeparator is used for
1573** the separator, which may or may not be a comma.  p->nullValue is
1574** the null value.  Strings are quoted if necessary.  The separator
1575** is only issued if bSep is true.
1576*/
1577static void output_csv(ShellState *p, const char *z, int bSep){
1578  FILE *out = p->out;
1579  if( z==0 ){
1580    utf8_printf(out,"%s",p->nullValue);
1581  }else{
1582    int i;
1583    int nSep = strlen30(p->colSeparator);
1584    for(i=0; z[i]; i++){
1585      if( needCsvQuote[((unsigned char*)z)[i]]
1586         || (z[i]==p->colSeparator[0] &&
1587             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1588        i = 0;
1589        break;
1590      }
1591    }
1592    if( i==0 ){
1593      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1594      utf8_printf(out, "%s", zQuoted);
1595      sqlite3_free(zQuoted);
1596    }else{
1597      utf8_printf(out, "%s", z);
1598    }
1599  }
1600  if( bSep ){
1601    utf8_printf(p->out, "%s", p->colSeparator);
1602  }
1603}
1604
1605/*
1606** This routine runs when the user presses Ctrl-C
1607*/
1608static void interrupt_handler(int NotUsed){
1609  UNUSED_PARAMETER(NotUsed);
1610  seenInterrupt++;
1611  if( seenInterrupt>2 ) exit(1);
1612  if( globalDb ) sqlite3_interrupt(globalDb);
1613}
1614
1615#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1616/*
1617** This routine runs for console events (e.g. Ctrl-C) on Win32
1618*/
1619static BOOL WINAPI ConsoleCtrlHandler(
1620  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1621){
1622  if( dwCtrlType==CTRL_C_EVENT ){
1623    interrupt_handler(0);
1624    return TRUE;
1625  }
1626  return FALSE;
1627}
1628#endif
1629
1630#ifndef SQLITE_OMIT_AUTHORIZATION
1631/*
1632** When the ".auth ON" is set, the following authorizer callback is
1633** invoked.  It always returns SQLITE_OK.
1634*/
1635static int shellAuth(
1636  void *pClientData,
1637  int op,
1638  const char *zA1,
1639  const char *zA2,
1640  const char *zA3,
1641  const char *zA4
1642){
1643  ShellState *p = (ShellState*)pClientData;
1644  static const char *azAction[] = { 0,
1645     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1646     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1647     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1648     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1649     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1650     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1651     "PRAGMA",               "READ",                 "SELECT",
1652     "TRANSACTION",          "UPDATE",               "ATTACH",
1653     "DETACH",               "ALTER_TABLE",          "REINDEX",
1654     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1655     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1656  };
1657  int i;
1658  const char *az[4];
1659  az[0] = zA1;
1660  az[1] = zA2;
1661  az[2] = zA3;
1662  az[3] = zA4;
1663  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1664  for(i=0; i<4; i++){
1665    raw_printf(p->out, " ");
1666    if( az[i] ){
1667      output_c_string(p->out, az[i]);
1668    }else{
1669      raw_printf(p->out, "NULL");
1670    }
1671  }
1672  raw_printf(p->out, "\n");
1673  return SQLITE_OK;
1674}
1675#endif
1676
1677/*
1678** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1679**
1680** This routine converts some CREATE TABLE statements for shadow tables
1681** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1682*/
1683static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1684  if( z==0 ) return;
1685  if( zTail==0 ) return;
1686  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1687    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1688  }else{
1689    utf8_printf(out, "%s%s", z, zTail);
1690  }
1691}
1692static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1693  char c = z[n];
1694  z[n] = 0;
1695  printSchemaLine(out, z, zTail);
1696  z[n] = c;
1697}
1698
1699/*
1700** Return true if string z[] has nothing but whitespace and comments to the
1701** end of the first line.
1702*/
1703static int wsToEol(const char *z){
1704  int i;
1705  for(i=0; z[i]; i++){
1706    if( z[i]=='\n' ) return 1;
1707    if( IsSpace(z[i]) ) continue;
1708    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1709    return 0;
1710  }
1711  return 1;
1712}
1713
1714/*
1715** Add a new entry to the EXPLAIN QUERY PLAN data
1716*/
1717static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1718  EQPGraphRow *pNew;
1719  int nText = strlen30(zText);
1720  if( p->autoEQPtest ){
1721    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1722  }
1723  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1724  if( pNew==0 ) shell_out_of_memory();
1725  pNew->iEqpId = iEqpId;
1726  pNew->iParentId = p2;
1727  memcpy(pNew->zText, zText, nText+1);
1728  pNew->pNext = 0;
1729  if( p->sGraph.pLast ){
1730    p->sGraph.pLast->pNext = pNew;
1731  }else{
1732    p->sGraph.pRow = pNew;
1733  }
1734  p->sGraph.pLast = pNew;
1735}
1736
1737/*
1738** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1739** in p->sGraph.
1740*/
1741static void eqp_reset(ShellState *p){
1742  EQPGraphRow *pRow, *pNext;
1743  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1744    pNext = pRow->pNext;
1745    sqlite3_free(pRow);
1746  }
1747  memset(&p->sGraph, 0, sizeof(p->sGraph));
1748}
1749
1750/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1751** pOld, or return the first such line if pOld is NULL
1752*/
1753static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1754  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1755  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1756  return pRow;
1757}
1758
1759/* Render a single level of the graph that has iEqpId as its parent.  Called
1760** recursively to render sublevels.
1761*/
1762static void eqp_render_level(ShellState *p, int iEqpId){
1763  EQPGraphRow *pRow, *pNext;
1764  int n = strlen30(p->sGraph.zPrefix);
1765  char *z;
1766  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1767    pNext = eqp_next_row(p, iEqpId, pRow);
1768    z = pRow->zText;
1769    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1770                pNext ? "|--" : "`--", z);
1771    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1772      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1773      eqp_render_level(p, pRow->iEqpId);
1774      p->sGraph.zPrefix[n] = 0;
1775    }
1776  }
1777}
1778
1779/*
1780** Display and reset the EXPLAIN QUERY PLAN data
1781*/
1782static void eqp_render(ShellState *p){
1783  EQPGraphRow *pRow = p->sGraph.pRow;
1784  if( pRow ){
1785    if( pRow->zText[0]=='-' ){
1786      if( pRow->pNext==0 ){
1787        eqp_reset(p);
1788        return;
1789      }
1790      utf8_printf(p->out, "%s\n", pRow->zText+3);
1791      p->sGraph.pRow = pRow->pNext;
1792      sqlite3_free(pRow);
1793    }else{
1794      utf8_printf(p->out, "QUERY PLAN\n");
1795    }
1796    p->sGraph.zPrefix[0] = 0;
1797    eqp_render_level(p, 0);
1798    eqp_reset(p);
1799  }
1800}
1801
1802#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1803/*
1804** Progress handler callback.
1805*/
1806static int progress_handler(void *pClientData) {
1807  ShellState *p = (ShellState*)pClientData;
1808  p->nProgress++;
1809  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1810    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1811    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1812    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1813    return 1;
1814  }
1815  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1816    raw_printf(p->out, "Progress %u\n", p->nProgress);
1817  }
1818  return 0;
1819}
1820#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1821
1822/*
1823** This is the callback routine that the shell
1824** invokes for each row of a query result.
1825*/
1826static int shell_callback(
1827  void *pArg,
1828  int nArg,        /* Number of result columns */
1829  char **azArg,    /* Text of each result column */
1830  char **azCol,    /* Column names */
1831  int *aiType      /* Column types */
1832){
1833  int i;
1834  ShellState *p = (ShellState*)pArg;
1835
1836  if( azArg==0 ) return 0;
1837  switch( p->cMode ){
1838    case MODE_Line: {
1839      int w = 5;
1840      if( azArg==0 ) break;
1841      for(i=0; i<nArg; i++){
1842        int len = strlen30(azCol[i] ? azCol[i] : "");
1843        if( len>w ) w = len;
1844      }
1845      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
1846      for(i=0; i<nArg; i++){
1847        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
1848                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
1849      }
1850      break;
1851    }
1852    case MODE_Explain:
1853    case MODE_Column: {
1854      static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
1855      const int *colWidth;
1856      int showHdr;
1857      char *rowSep;
1858      int nWidth;
1859      if( p->cMode==MODE_Column ){
1860        colWidth = p->colWidth;
1861        nWidth = ArraySize(p->colWidth);
1862        showHdr = p->showHeader;
1863        rowSep = p->rowSeparator;
1864      }else{
1865        colWidth = aExplainWidths;
1866        nWidth = ArraySize(aExplainWidths);
1867        showHdr = 1;
1868        rowSep = SEP_Row;
1869      }
1870      if( p->cnt++==0 ){
1871        for(i=0; i<nArg; i++){
1872          int w, n;
1873          if( i<nWidth ){
1874            w = colWidth[i];
1875          }else{
1876            w = 0;
1877          }
1878          if( w==0 ){
1879            w = strlenChar(azCol[i] ? azCol[i] : "");
1880            if( w<10 ) w = 10;
1881            n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
1882            if( w<n ) w = n;
1883          }
1884          if( i<ArraySize(p->actualWidth) ){
1885            p->actualWidth[i] = w;
1886          }
1887          if( showHdr ){
1888            utf8_width_print(p->out, w, azCol[i]);
1889            utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
1890          }
1891        }
1892        if( showHdr ){
1893          for(i=0; i<nArg; i++){
1894            int w;
1895            if( i<ArraySize(p->actualWidth) ){
1896               w = p->actualWidth[i];
1897               if( w<0 ) w = -w;
1898            }else{
1899               w = 10;
1900            }
1901            utf8_printf(p->out,"%-*.*s%s",w,w,
1902                   "----------------------------------------------------------"
1903                   "----------------------------------------------------------",
1904                    i==nArg-1 ? rowSep : "  ");
1905          }
1906        }
1907      }
1908      if( azArg==0 ) break;
1909      for(i=0; i<nArg; i++){
1910        int w;
1911        if( i<ArraySize(p->actualWidth) ){
1912           w = p->actualWidth[i];
1913        }else{
1914           w = 10;
1915        }
1916        if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
1917          w = strlenChar(azArg[i]);
1918        }
1919        if( i==1 && p->aiIndent && p->pStmt ){
1920          if( p->iIndent<p->nIndent ){
1921            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
1922          }
1923          p->iIndent++;
1924        }
1925        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
1926        utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : "  ");
1927      }
1928      break;
1929    }
1930    case MODE_Semi: {   /* .schema and .fullschema output */
1931      printSchemaLine(p->out, azArg[0], ";\n");
1932      break;
1933    }
1934    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
1935      char *z;
1936      int j;
1937      int nParen = 0;
1938      char cEnd = 0;
1939      char c;
1940      int nLine = 0;
1941      assert( nArg==1 );
1942      if( azArg[0]==0 ) break;
1943      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
1944       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
1945      ){
1946        utf8_printf(p->out, "%s;\n", azArg[0]);
1947        break;
1948      }
1949      z = sqlite3_mprintf("%s", azArg[0]);
1950      j = 0;
1951      for(i=0; IsSpace(z[i]); i++){}
1952      for(; (c = z[i])!=0; i++){
1953        if( IsSpace(c) ){
1954          if( z[j-1]=='\r' ) z[j-1] = '\n';
1955          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
1956        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
1957          j--;
1958        }
1959        z[j++] = c;
1960      }
1961      while( j>0 && IsSpace(z[j-1]) ){ j--; }
1962      z[j] = 0;
1963      if( strlen30(z)>=79 ){
1964        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
1965          if( c==cEnd ){
1966            cEnd = 0;
1967          }else if( c=='"' || c=='\'' || c=='`' ){
1968            cEnd = c;
1969          }else if( c=='[' ){
1970            cEnd = ']';
1971          }else if( c=='-' && z[i+1]=='-' ){
1972            cEnd = '\n';
1973          }else if( c=='(' ){
1974            nParen++;
1975          }else if( c==')' ){
1976            nParen--;
1977            if( nLine>0 && nParen==0 && j>0 ){
1978              printSchemaLineN(p->out, z, j, "\n");
1979              j = 0;
1980            }
1981          }
1982          z[j++] = c;
1983          if( nParen==1 && cEnd==0
1984           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
1985          ){
1986            if( c=='\n' ) j--;
1987            printSchemaLineN(p->out, z, j, "\n  ");
1988            j = 0;
1989            nLine++;
1990            while( IsSpace(z[i+1]) ){ i++; }
1991          }
1992        }
1993        z[j] = 0;
1994      }
1995      printSchemaLine(p->out, z, ";\n");
1996      sqlite3_free(z);
1997      break;
1998    }
1999    case MODE_List: {
2000      if( p->cnt++==0 && p->showHeader ){
2001        for(i=0; i<nArg; i++){
2002          utf8_printf(p->out,"%s%s",azCol[i],
2003                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2004        }
2005      }
2006      if( azArg==0 ) break;
2007      for(i=0; i<nArg; i++){
2008        char *z = azArg[i];
2009        if( z==0 ) z = p->nullValue;
2010        utf8_printf(p->out, "%s", z);
2011        if( i<nArg-1 ){
2012          utf8_printf(p->out, "%s", p->colSeparator);
2013        }else{
2014          utf8_printf(p->out, "%s", p->rowSeparator);
2015        }
2016      }
2017      break;
2018    }
2019    case MODE_Html: {
2020      if( p->cnt++==0 && p->showHeader ){
2021        raw_printf(p->out,"<TR>");
2022        for(i=0; i<nArg; i++){
2023          raw_printf(p->out,"<TH>");
2024          output_html_string(p->out, azCol[i]);
2025          raw_printf(p->out,"</TH>\n");
2026        }
2027        raw_printf(p->out,"</TR>\n");
2028      }
2029      if( azArg==0 ) break;
2030      raw_printf(p->out,"<TR>");
2031      for(i=0; i<nArg; i++){
2032        raw_printf(p->out,"<TD>");
2033        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2034        raw_printf(p->out,"</TD>\n");
2035      }
2036      raw_printf(p->out,"</TR>\n");
2037      break;
2038    }
2039    case MODE_Tcl: {
2040      if( p->cnt++==0 && p->showHeader ){
2041        for(i=0; i<nArg; i++){
2042          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2043          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2044        }
2045        utf8_printf(p->out, "%s", p->rowSeparator);
2046      }
2047      if( azArg==0 ) break;
2048      for(i=0; i<nArg; i++){
2049        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2050        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2051      }
2052      utf8_printf(p->out, "%s", p->rowSeparator);
2053      break;
2054    }
2055    case MODE_Csv: {
2056      setBinaryMode(p->out, 1);
2057      if( p->cnt++==0 && p->showHeader ){
2058        for(i=0; i<nArg; i++){
2059          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2060        }
2061        utf8_printf(p->out, "%s", p->rowSeparator);
2062      }
2063      if( nArg>0 ){
2064        for(i=0; i<nArg; i++){
2065          output_csv(p, azArg[i], i<nArg-1);
2066        }
2067        utf8_printf(p->out, "%s", p->rowSeparator);
2068      }
2069      setTextMode(p->out, 1);
2070      break;
2071    }
2072    case MODE_Insert: {
2073      if( azArg==0 ) break;
2074      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2075      if( p->showHeader ){
2076        raw_printf(p->out,"(");
2077        for(i=0; i<nArg; i++){
2078          if( i>0 ) raw_printf(p->out, ",");
2079          if( quoteChar(azCol[i]) ){
2080            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2081            utf8_printf(p->out, "%s", z);
2082            sqlite3_free(z);
2083          }else{
2084            raw_printf(p->out, "%s", azCol[i]);
2085          }
2086        }
2087        raw_printf(p->out,")");
2088      }
2089      p->cnt++;
2090      for(i=0; i<nArg; i++){
2091        raw_printf(p->out, i>0 ? "," : " VALUES(");
2092        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2093          utf8_printf(p->out,"NULL");
2094        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2095          if( ShellHasFlag(p, SHFLG_Newlines) ){
2096            output_quoted_string(p->out, azArg[i]);
2097          }else{
2098            output_quoted_escaped_string(p->out, azArg[i]);
2099          }
2100        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2101          utf8_printf(p->out,"%s", azArg[i]);
2102        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2103          char z[50];
2104          double r = sqlite3_column_double(p->pStmt, i);
2105          sqlite3_uint64 ur;
2106          memcpy(&ur,&r,sizeof(r));
2107          if( ur==0x7ff0000000000000LL ){
2108            raw_printf(p->out, "1e999");
2109          }else if( ur==0xfff0000000000000LL ){
2110            raw_printf(p->out, "-1e999");
2111          }else{
2112            sqlite3_snprintf(50,z,"%!.20g", r);
2113            raw_printf(p->out, "%s", z);
2114          }
2115        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2116          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2117          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2118          output_hex_blob(p->out, pBlob, nBlob);
2119        }else if( isNumber(azArg[i], 0) ){
2120          utf8_printf(p->out,"%s", azArg[i]);
2121        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2122          output_quoted_string(p->out, azArg[i]);
2123        }else{
2124          output_quoted_escaped_string(p->out, azArg[i]);
2125        }
2126      }
2127      raw_printf(p->out,");\n");
2128      break;
2129    }
2130    case MODE_Quote: {
2131      if( azArg==0 ) break;
2132      if( p->cnt==0 && p->showHeader ){
2133        for(i=0; i<nArg; i++){
2134          if( i>0 ) raw_printf(p->out, ",");
2135          output_quoted_string(p->out, azCol[i]);
2136        }
2137        raw_printf(p->out,"\n");
2138      }
2139      p->cnt++;
2140      for(i=0; i<nArg; i++){
2141        if( i>0 ) raw_printf(p->out, ",");
2142        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2143          utf8_printf(p->out,"NULL");
2144        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2145          output_quoted_string(p->out, azArg[i]);
2146        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2147          utf8_printf(p->out,"%s", azArg[i]);
2148        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2149          char z[50];
2150          double r = sqlite3_column_double(p->pStmt, i);
2151          sqlite3_snprintf(50,z,"%!.20g", r);
2152          raw_printf(p->out, "%s", z);
2153        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2154          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2155          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2156          output_hex_blob(p->out, pBlob, nBlob);
2157        }else if( isNumber(azArg[i], 0) ){
2158          utf8_printf(p->out,"%s", azArg[i]);
2159        }else{
2160          output_quoted_string(p->out, azArg[i]);
2161        }
2162      }
2163      raw_printf(p->out,"\n");
2164      break;
2165    }
2166    case MODE_Ascii: {
2167      if( p->cnt++==0 && p->showHeader ){
2168        for(i=0; i<nArg; i++){
2169          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2170          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2171        }
2172        utf8_printf(p->out, "%s", p->rowSeparator);
2173      }
2174      if( azArg==0 ) break;
2175      for(i=0; i<nArg; i++){
2176        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2177        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2178      }
2179      utf8_printf(p->out, "%s", p->rowSeparator);
2180      break;
2181    }
2182    case MODE_EQP: {
2183      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2184      break;
2185    }
2186  }
2187  return 0;
2188}
2189
2190/*
2191** This is the callback routine that the SQLite library
2192** invokes for each row of a query result.
2193*/
2194static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2195  /* since we don't have type info, call the shell_callback with a NULL value */
2196  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2197}
2198
2199/*
2200** This is the callback routine from sqlite3_exec() that appends all
2201** output onto the end of a ShellText object.
2202*/
2203static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2204  ShellText *p = (ShellText*)pArg;
2205  int i;
2206  UNUSED_PARAMETER(az);
2207  if( azArg==0 ) return 0;
2208  if( p->n ) appendText(p, "|", 0);
2209  for(i=0; i<nArg; i++){
2210    if( i ) appendText(p, ",", 0);
2211    if( azArg[i] ) appendText(p, azArg[i], 0);
2212  }
2213  return 0;
2214}
2215
2216/*
2217** Generate an appropriate SELFTEST table in the main database.
2218*/
2219static void createSelftestTable(ShellState *p){
2220  char *zErrMsg = 0;
2221  sqlite3_exec(p->db,
2222    "SAVEPOINT selftest_init;\n"
2223    "CREATE TABLE IF NOT EXISTS selftest(\n"
2224    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2225    "  op TEXT,\n"                   /* Operator:  memo run */
2226    "  cmd TEXT,\n"                  /* Command text */
2227    "  ans TEXT\n"                   /* Desired answer */
2228    ");"
2229    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2230    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2231    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2232    "         'memo','Tests generated by --init');\n"
2233    "INSERT INTO [_shell$self]\n"
2234    "  SELECT 'run',\n"
2235    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2236                                 "FROM sqlite_master ORDER BY 2'',224))',\n"
2237    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2238                          "FROM sqlite_master ORDER BY 2',224));\n"
2239    "INSERT INTO [_shell$self]\n"
2240    "  SELECT 'run',"
2241    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2242    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2243    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2244    "  FROM (\n"
2245    "    SELECT name FROM sqlite_master\n"
2246    "     WHERE type='table'\n"
2247    "       AND name<>'selftest'\n"
2248    "       AND coalesce(rootpage,0)>0\n"
2249    "  )\n"
2250    " ORDER BY name;\n"
2251    "INSERT INTO [_shell$self]\n"
2252    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2253    "INSERT INTO selftest(tno,op,cmd,ans)"
2254    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2255    "DROP TABLE [_shell$self];"
2256    ,0,0,&zErrMsg);
2257  if( zErrMsg ){
2258    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2259    sqlite3_free(zErrMsg);
2260  }
2261  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2262}
2263
2264
2265/*
2266** Set the destination table field of the ShellState structure to
2267** the name of the table given.  Escape any quote characters in the
2268** table name.
2269*/
2270static void set_table_name(ShellState *p, const char *zName){
2271  int i, n;
2272  char cQuote;
2273  char *z;
2274
2275  if( p->zDestTable ){
2276    free(p->zDestTable);
2277    p->zDestTable = 0;
2278  }
2279  if( zName==0 ) return;
2280  cQuote = quoteChar(zName);
2281  n = strlen30(zName);
2282  if( cQuote ) n += n+2;
2283  z = p->zDestTable = malloc( n+1 );
2284  if( z==0 ) shell_out_of_memory();
2285  n = 0;
2286  if( cQuote ) z[n++] = cQuote;
2287  for(i=0; zName[i]; i++){
2288    z[n++] = zName[i];
2289    if( zName[i]==cQuote ) z[n++] = cQuote;
2290  }
2291  if( cQuote ) z[n++] = cQuote;
2292  z[n] = 0;
2293}
2294
2295
2296/*
2297** Execute a query statement that will generate SQL output.  Print
2298** the result columns, comma-separated, on a line and then add a
2299** semicolon terminator to the end of that line.
2300**
2301** If the number of columns is 1 and that column contains text "--"
2302** then write the semicolon on a separate line.  That way, if a
2303** "--" comment occurs at the end of the statement, the comment
2304** won't consume the semicolon terminator.
2305*/
2306static int run_table_dump_query(
2307  ShellState *p,           /* Query context */
2308  const char *zSelect,     /* SELECT statement to extract content */
2309  const char *zFirstRow    /* Print before first row, if not NULL */
2310){
2311  sqlite3_stmt *pSelect;
2312  int rc;
2313  int nResult;
2314  int i;
2315  const char *z;
2316  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2317  if( rc!=SQLITE_OK || !pSelect ){
2318    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2319                sqlite3_errmsg(p->db));
2320    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2321    return rc;
2322  }
2323  rc = sqlite3_step(pSelect);
2324  nResult = sqlite3_column_count(pSelect);
2325  while( rc==SQLITE_ROW ){
2326    if( zFirstRow ){
2327      utf8_printf(p->out, "%s", zFirstRow);
2328      zFirstRow = 0;
2329    }
2330    z = (const char*)sqlite3_column_text(pSelect, 0);
2331    utf8_printf(p->out, "%s", z);
2332    for(i=1; i<nResult; i++){
2333      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2334    }
2335    if( z==0 ) z = "";
2336    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2337    if( z[0] ){
2338      raw_printf(p->out, "\n;\n");
2339    }else{
2340      raw_printf(p->out, ";\n");
2341    }
2342    rc = sqlite3_step(pSelect);
2343  }
2344  rc = sqlite3_finalize(pSelect);
2345  if( rc!=SQLITE_OK ){
2346    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2347                sqlite3_errmsg(p->db));
2348    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2349  }
2350  return rc;
2351}
2352
2353/*
2354** Allocate space and save off current error string.
2355*/
2356static char *save_err_msg(
2357  sqlite3 *db            /* Database to query */
2358){
2359  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2360  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2361  if( zErrMsg ){
2362    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2363  }
2364  return zErrMsg;
2365}
2366
2367#ifdef __linux__
2368/*
2369** Attempt to display I/O stats on Linux using /proc/PID/io
2370*/
2371static void displayLinuxIoStats(FILE *out){
2372  FILE *in;
2373  char z[200];
2374  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2375  in = fopen(z, "rb");
2376  if( in==0 ) return;
2377  while( fgets(z, sizeof(z), in)!=0 ){
2378    static const struct {
2379      const char *zPattern;
2380      const char *zDesc;
2381    } aTrans[] = {
2382      { "rchar: ",                  "Bytes received by read():" },
2383      { "wchar: ",                  "Bytes sent to write():"    },
2384      { "syscr: ",                  "Read() system calls:"      },
2385      { "syscw: ",                  "Write() system calls:"     },
2386      { "read_bytes: ",             "Bytes read from storage:"  },
2387      { "write_bytes: ",            "Bytes written to storage:" },
2388      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2389    };
2390    int i;
2391    for(i=0; i<ArraySize(aTrans); i++){
2392      int n = strlen30(aTrans[i].zPattern);
2393      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2394        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2395        break;
2396      }
2397    }
2398  }
2399  fclose(in);
2400}
2401#endif
2402
2403/*
2404** Display a single line of status using 64-bit values.
2405*/
2406static void displayStatLine(
2407  ShellState *p,            /* The shell context */
2408  char *zLabel,             /* Label for this one line */
2409  char *zFormat,            /* Format for the result */
2410  int iStatusCtrl,          /* Which status to display */
2411  int bReset                /* True to reset the stats */
2412){
2413  sqlite3_int64 iCur = -1;
2414  sqlite3_int64 iHiwtr = -1;
2415  int i, nPercent;
2416  char zLine[200];
2417  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2418  for(i=0, nPercent=0; zFormat[i]; i++){
2419    if( zFormat[i]=='%' ) nPercent++;
2420  }
2421  if( nPercent>1 ){
2422    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2423  }else{
2424    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2425  }
2426  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2427}
2428
2429/*
2430** Display memory stats.
2431*/
2432static int display_stats(
2433  sqlite3 *db,                /* Database to query */
2434  ShellState *pArg,           /* Pointer to ShellState */
2435  int bReset                  /* True to reset the stats */
2436){
2437  int iCur;
2438  int iHiwtr;
2439  FILE *out;
2440  if( pArg==0 || pArg->out==0 ) return 0;
2441  out = pArg->out;
2442
2443  if( pArg->pStmt && (pArg->statsOn & 2) ){
2444    int nCol, i, x;
2445    sqlite3_stmt *pStmt = pArg->pStmt;
2446    char z[100];
2447    nCol = sqlite3_column_count(pStmt);
2448    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2449    for(i=0; i<nCol; i++){
2450      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2451      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2452#ifndef SQLITE_OMIT_DECLTYPE
2453      sqlite3_snprintf(30, z+x, "declared type:");
2454      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2455#endif
2456#ifdef SQLITE_ENABLE_COLUMN_METADATA
2457      sqlite3_snprintf(30, z+x, "database name:");
2458      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2459      sqlite3_snprintf(30, z+x, "table name:");
2460      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2461      sqlite3_snprintf(30, z+x, "origin name:");
2462      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2463#endif
2464    }
2465  }
2466
2467  displayStatLine(pArg, "Memory Used:",
2468     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2469  displayStatLine(pArg, "Number of Outstanding Allocations:",
2470     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2471  if( pArg->shellFlgs & SHFLG_Pagecache ){
2472    displayStatLine(pArg, "Number of Pcache Pages Used:",
2473       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2474  }
2475  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2476     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2477  displayStatLine(pArg, "Largest Allocation:",
2478     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2479  displayStatLine(pArg, "Largest Pcache Allocation:",
2480     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2481#ifdef YYTRACKMAXSTACKDEPTH
2482  displayStatLine(pArg, "Deepest Parser Stack:",
2483     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2484#endif
2485
2486  if( db ){
2487    if( pArg->shellFlgs & SHFLG_Lookaside ){
2488      iHiwtr = iCur = -1;
2489      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2490                        &iCur, &iHiwtr, bReset);
2491      raw_printf(pArg->out,
2492              "Lookaside Slots Used:                %d (max %d)\n",
2493              iCur, iHiwtr);
2494      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2495                        &iCur, &iHiwtr, bReset);
2496      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2497              iHiwtr);
2498      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2499                        &iCur, &iHiwtr, bReset);
2500      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2501              iHiwtr);
2502      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2503                        &iCur, &iHiwtr, bReset);
2504      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2505              iHiwtr);
2506    }
2507    iHiwtr = iCur = -1;
2508    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2509    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2510            iCur);
2511    iHiwtr = iCur = -1;
2512    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2513    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2514    iHiwtr = iCur = -1;
2515    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2516    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2517    iHiwtr = iCur = -1;
2518    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2519    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2520    iHiwtr = iCur = -1;
2521    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2522    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2523    iHiwtr = iCur = -1;
2524    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2525    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2526            iCur);
2527    iHiwtr = iCur = -1;
2528    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2529    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2530            iCur);
2531  }
2532
2533  if( pArg->pStmt ){
2534    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2535                               bReset);
2536    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2537    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2538    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2539    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2540    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2541    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2542    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2543    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2544    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2545    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2546    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2547    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2548    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2549  }
2550
2551#ifdef __linux__
2552  displayLinuxIoStats(pArg->out);
2553#endif
2554
2555  /* Do not remove this machine readable comment: extra-stats-output-here */
2556
2557  return 0;
2558}
2559
2560/*
2561** Display scan stats.
2562*/
2563static void display_scanstats(
2564  sqlite3 *db,                    /* Database to query */
2565  ShellState *pArg                /* Pointer to ShellState */
2566){
2567#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2568  UNUSED_PARAMETER(db);
2569  UNUSED_PARAMETER(pArg);
2570#else
2571  int i, k, n, mx;
2572  raw_printf(pArg->out, "-------- scanstats --------\n");
2573  mx = 0;
2574  for(k=0; k<=mx; k++){
2575    double rEstLoop = 1.0;
2576    for(i=n=0; 1; i++){
2577      sqlite3_stmt *p = pArg->pStmt;
2578      sqlite3_int64 nLoop, nVisit;
2579      double rEst;
2580      int iSid;
2581      const char *zExplain;
2582      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2583        break;
2584      }
2585      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2586      if( iSid>mx ) mx = iSid;
2587      if( iSid!=k ) continue;
2588      if( n==0 ){
2589        rEstLoop = (double)nLoop;
2590        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2591      }
2592      n++;
2593      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2594      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2595      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2596      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2597      rEstLoop *= rEst;
2598      raw_printf(pArg->out,
2599          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2600          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2601      );
2602    }
2603  }
2604  raw_printf(pArg->out, "---------------------------\n");
2605#endif
2606}
2607
2608/*
2609** Parameter azArray points to a zero-terminated array of strings. zStr
2610** points to a single nul-terminated string. Return non-zero if zStr
2611** is equal, according to strcmp(), to any of the strings in the array.
2612** Otherwise, return zero.
2613*/
2614static int str_in_array(const char *zStr, const char **azArray){
2615  int i;
2616  for(i=0; azArray[i]; i++){
2617    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2618  }
2619  return 0;
2620}
2621
2622/*
2623** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2624** and populate the ShellState.aiIndent[] array with the number of
2625** spaces each opcode should be indented before it is output.
2626**
2627** The indenting rules are:
2628**
2629**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2630**       all opcodes that occur between the p2 jump destination and the opcode
2631**       itself by 2 spaces.
2632**
2633**     * For each "Goto", if the jump destination is earlier in the program
2634**       and ends on one of:
2635**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2636**       or if the P1 parameter is one instead of zero,
2637**       then indent all opcodes between the earlier instruction
2638**       and "Goto" by 2 spaces.
2639*/
2640static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2641  const char *zSql;               /* The text of the SQL statement */
2642  const char *z;                  /* Used to check if this is an EXPLAIN */
2643  int *abYield = 0;               /* True if op is an OP_Yield */
2644  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2645  int iOp;                        /* Index of operation in p->aiIndent[] */
2646
2647  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2648  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2649                            "Rewind", 0 };
2650  const char *azGoto[] = { "Goto", 0 };
2651
2652  /* Try to figure out if this is really an EXPLAIN statement. If this
2653  ** cannot be verified, return early.  */
2654  if( sqlite3_column_count(pSql)!=8 ){
2655    p->cMode = p->mode;
2656    return;
2657  }
2658  zSql = sqlite3_sql(pSql);
2659  if( zSql==0 ) return;
2660  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2661  if( sqlite3_strnicmp(z, "explain", 7) ){
2662    p->cMode = p->mode;
2663    return;
2664  }
2665
2666  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2667    int i;
2668    int iAddr = sqlite3_column_int(pSql, 0);
2669    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2670
2671    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2672    ** p2 is an instruction address, set variable p2op to the index of that
2673    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2674    ** the current instruction is part of a sub-program generated by an
2675    ** SQL trigger or foreign key.  */
2676    int p2 = sqlite3_column_int(pSql, 3);
2677    int p2op = (p2 + (iOp-iAddr));
2678
2679    /* Grow the p->aiIndent array as required */
2680    if( iOp>=nAlloc ){
2681      if( iOp==0 ){
2682        /* Do further verfication that this is explain output.  Abort if
2683        ** it is not */
2684        static const char *explainCols[] = {
2685           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2686        int jj;
2687        for(jj=0; jj<ArraySize(explainCols); jj++){
2688          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2689            p->cMode = p->mode;
2690            sqlite3_reset(pSql);
2691            return;
2692          }
2693        }
2694      }
2695      nAlloc += 100;
2696      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2697      if( p->aiIndent==0 ) shell_out_of_memory();
2698      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2699      if( abYield==0 ) shell_out_of_memory();
2700    }
2701    abYield[iOp] = str_in_array(zOp, azYield);
2702    p->aiIndent[iOp] = 0;
2703    p->nIndent = iOp+1;
2704
2705    if( str_in_array(zOp, azNext) ){
2706      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2707    }
2708    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2709     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2710    ){
2711      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2712    }
2713  }
2714
2715  p->iIndent = 0;
2716  sqlite3_free(abYield);
2717  sqlite3_reset(pSql);
2718}
2719
2720/*
2721** Free the array allocated by explain_data_prepare().
2722*/
2723static void explain_data_delete(ShellState *p){
2724  sqlite3_free(p->aiIndent);
2725  p->aiIndent = 0;
2726  p->nIndent = 0;
2727  p->iIndent = 0;
2728}
2729
2730/*
2731** Disable and restore .wheretrace and .selecttrace settings.
2732*/
2733#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2734extern int sqlite3SelectTrace;
2735static int savedSelectTrace;
2736#endif
2737#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2738extern int sqlite3WhereTrace;
2739static int savedWhereTrace;
2740#endif
2741static void disable_debug_trace_modes(void){
2742#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2743  savedSelectTrace = sqlite3SelectTrace;
2744  sqlite3SelectTrace = 0;
2745#endif
2746#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2747  savedWhereTrace = sqlite3WhereTrace;
2748  sqlite3WhereTrace = 0;
2749#endif
2750}
2751static void restore_debug_trace_modes(void){
2752#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2753  sqlite3SelectTrace = savedSelectTrace;
2754#endif
2755#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2756  sqlite3WhereTrace = savedWhereTrace;
2757#endif
2758}
2759
2760/* Create the TEMP table used to store parameter bindings */
2761static void bind_table_init(ShellState *p){
2762  int wrSchema = 0;
2763  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2764  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2765  sqlite3_exec(p->db,
2766    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2767    "  key TEXT PRIMARY KEY,\n"
2768    "  value ANY\n"
2769    ") WITHOUT ROWID;",
2770    0, 0, 0);
2771  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2772}
2773
2774/*
2775** Bind parameters on a prepared statement.
2776**
2777** Parameter bindings are taken from a TEMP table of the form:
2778**
2779**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2780**    WITHOUT ROWID;
2781**
2782** No bindings occur if this table does not exist.  The special character '$'
2783** is included in the table name to help prevent collisions with actual tables.
2784** The table must be in the TEMP schema.
2785*/
2786static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2787  int nVar;
2788  int i;
2789  int rc;
2790  sqlite3_stmt *pQ = 0;
2791
2792  nVar = sqlite3_bind_parameter_count(pStmt);
2793  if( nVar==0 ) return;  /* Nothing to do */
2794  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2795                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2796    return; /* Parameter table does not exist */
2797  }
2798  rc = sqlite3_prepare_v2(pArg->db,
2799          "SELECT value FROM temp.sqlite_parameters"
2800          " WHERE key=?1", -1, &pQ, 0);
2801  if( rc || pQ==0 ) return;
2802  for(i=1; i<=nVar; i++){
2803    char zNum[30];
2804    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2805    if( zVar==0 ){
2806      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2807      zVar = zNum;
2808    }
2809    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2810    if( sqlite3_step(pQ)==SQLITE_ROW ){
2811      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2812    }else{
2813      sqlite3_bind_null(pStmt, i);
2814    }
2815    sqlite3_reset(pQ);
2816  }
2817  sqlite3_finalize(pQ);
2818}
2819
2820/*
2821** Run a prepared statement
2822*/
2823static void exec_prepared_stmt(
2824  ShellState *pArg,                                /* Pointer to ShellState */
2825  sqlite3_stmt *pStmt                              /* Statment to run */
2826){
2827  int rc;
2828
2829  /* perform the first step.  this will tell us if we
2830  ** have a result set or not and how wide it is.
2831  */
2832  rc = sqlite3_step(pStmt);
2833  /* if we have a result set... */
2834  if( SQLITE_ROW == rc ){
2835    /* allocate space for col name ptr, value ptr, and type */
2836    int nCol = sqlite3_column_count(pStmt);
2837    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
2838    if( !pData ){
2839      rc = SQLITE_NOMEM;
2840    }else{
2841      char **azCols = (char **)pData;      /* Names of result columns */
2842      char **azVals = &azCols[nCol];       /* Results */
2843      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
2844      int i, x;
2845      assert(sizeof(int) <= sizeof(char *));
2846      /* save off ptrs to column names */
2847      for(i=0; i<nCol; i++){
2848        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
2849      }
2850      do{
2851        /* extract the data and data types */
2852        for(i=0; i<nCol; i++){
2853          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
2854          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
2855            azVals[i] = "";
2856          }else{
2857            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
2858          }
2859          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
2860            rc = SQLITE_NOMEM;
2861            break; /* from for */
2862          }
2863        } /* end for */
2864
2865        /* if data and types extracted successfully... */
2866        if( SQLITE_ROW == rc ){
2867          /* call the supplied callback with the result row data */
2868          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
2869            rc = SQLITE_ABORT;
2870          }else{
2871            rc = sqlite3_step(pStmt);
2872          }
2873        }
2874      } while( SQLITE_ROW == rc );
2875      sqlite3_free(pData);
2876    }
2877  }
2878}
2879
2880#ifndef SQLITE_OMIT_VIRTUALTABLE
2881/*
2882** This function is called to process SQL if the previous shell command
2883** was ".expert". It passes the SQL in the second argument directly to
2884** the sqlite3expert object.
2885**
2886** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2887** code. In this case, (*pzErr) may be set to point to a buffer containing
2888** an English language error message. It is the responsibility of the
2889** caller to eventually free this buffer using sqlite3_free().
2890*/
2891static int expertHandleSQL(
2892  ShellState *pState,
2893  const char *zSql,
2894  char **pzErr
2895){
2896  assert( pState->expert.pExpert );
2897  assert( pzErr==0 || *pzErr==0 );
2898  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
2899}
2900
2901/*
2902** This function is called either to silently clean up the object
2903** created by the ".expert" command (if bCancel==1), or to generate a
2904** report from it and then clean it up (if bCancel==0).
2905**
2906** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2907** code. In this case, (*pzErr) may be set to point to a buffer containing
2908** an English language error message. It is the responsibility of the
2909** caller to eventually free this buffer using sqlite3_free().
2910*/
2911static int expertFinish(
2912  ShellState *pState,
2913  int bCancel,
2914  char **pzErr
2915){
2916  int rc = SQLITE_OK;
2917  sqlite3expert *p = pState->expert.pExpert;
2918  assert( p );
2919  assert( bCancel || pzErr==0 || *pzErr==0 );
2920  if( bCancel==0 ){
2921    FILE *out = pState->out;
2922    int bVerbose = pState->expert.bVerbose;
2923
2924    rc = sqlite3_expert_analyze(p, pzErr);
2925    if( rc==SQLITE_OK ){
2926      int nQuery = sqlite3_expert_count(p);
2927      int i;
2928
2929      if( bVerbose ){
2930        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
2931        raw_printf(out, "-- Candidates -----------------------------\n");
2932        raw_printf(out, "%s\n", zCand);
2933      }
2934      for(i=0; i<nQuery; i++){
2935        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
2936        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
2937        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
2938        if( zIdx==0 ) zIdx = "(no new indexes)\n";
2939        if( bVerbose ){
2940          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
2941          raw_printf(out, "%s\n\n", zSql);
2942        }
2943        raw_printf(out, "%s\n", zIdx);
2944        raw_printf(out, "%s\n", zEQP);
2945      }
2946    }
2947  }
2948  sqlite3_expert_destroy(p);
2949  pState->expert.pExpert = 0;
2950  return rc;
2951}
2952
2953/*
2954** Implementation of ".expert" dot command.
2955*/
2956static int expertDotCommand(
2957  ShellState *pState,             /* Current shell tool state */
2958  char **azArg,                   /* Array of arguments passed to dot command */
2959  int nArg                        /* Number of entries in azArg[] */
2960){
2961  int rc = SQLITE_OK;
2962  char *zErr = 0;
2963  int i;
2964  int iSample = 0;
2965
2966  assert( pState->expert.pExpert==0 );
2967  memset(&pState->expert, 0, sizeof(ExpertInfo));
2968
2969  for(i=1; rc==SQLITE_OK && i<nArg; i++){
2970    char *z = azArg[i];
2971    int n;
2972    if( z[0]=='-' && z[1]=='-' ) z++;
2973    n = strlen30(z);
2974    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
2975      pState->expert.bVerbose = 1;
2976    }
2977    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
2978      if( i==(nArg-1) ){
2979        raw_printf(stderr, "option requires an argument: %s\n", z);
2980        rc = SQLITE_ERROR;
2981      }else{
2982        iSample = (int)integerValue(azArg[++i]);
2983        if( iSample<0 || iSample>100 ){
2984          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
2985          rc = SQLITE_ERROR;
2986        }
2987      }
2988    }
2989    else{
2990      raw_printf(stderr, "unknown option: %s\n", z);
2991      rc = SQLITE_ERROR;
2992    }
2993  }
2994
2995  if( rc==SQLITE_OK ){
2996    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
2997    if( pState->expert.pExpert==0 ){
2998      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
2999      rc = SQLITE_ERROR;
3000    }else{
3001      sqlite3_expert_config(
3002          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3003      );
3004    }
3005  }
3006
3007  return rc;
3008}
3009#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3010
3011/*
3012** Execute a statement or set of statements.  Print
3013** any result rows/columns depending on the current mode
3014** set via the supplied callback.
3015**
3016** This is very similar to SQLite's built-in sqlite3_exec()
3017** function except it takes a slightly different callback
3018** and callback data argument.
3019*/
3020static int shell_exec(
3021  ShellState *pArg,                         /* Pointer to ShellState */
3022  const char *zSql,                         /* SQL to be evaluated */
3023  char **pzErrMsg                           /* Error msg written here */
3024){
3025  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3026  int rc = SQLITE_OK;             /* Return Code */
3027  int rc2;
3028  const char *zLeftover;          /* Tail of unprocessed SQL */
3029  sqlite3 *db = pArg->db;
3030
3031  if( pzErrMsg ){
3032    *pzErrMsg = NULL;
3033  }
3034
3035#ifndef SQLITE_OMIT_VIRTUALTABLE
3036  if( pArg->expert.pExpert ){
3037    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3038    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3039  }
3040#endif
3041
3042  while( zSql[0] && (SQLITE_OK == rc) ){
3043    static const char *zStmtSql;
3044    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3045    if( SQLITE_OK != rc ){
3046      if( pzErrMsg ){
3047        *pzErrMsg = save_err_msg(db);
3048      }
3049    }else{
3050      if( !pStmt ){
3051        /* this happens for a comment or white-space */
3052        zSql = zLeftover;
3053        while( IsSpace(zSql[0]) ) zSql++;
3054        continue;
3055      }
3056      zStmtSql = sqlite3_sql(pStmt);
3057      if( zStmtSql==0 ) zStmtSql = "";
3058      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3059
3060      /* save off the prepared statment handle and reset row count */
3061      if( pArg ){
3062        pArg->pStmt = pStmt;
3063        pArg->cnt = 0;
3064      }
3065
3066      /* echo the sql statement if echo on */
3067      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3068        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3069      }
3070
3071      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3072      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3073        sqlite3_stmt *pExplain;
3074        char *zEQP;
3075        int triggerEQP = 0;
3076        disable_debug_trace_modes();
3077        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3078        if( pArg->autoEQP>=AUTOEQP_trigger ){
3079          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3080        }
3081        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3082        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3083        if( rc==SQLITE_OK ){
3084          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3085            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3086            int iEqpId = sqlite3_column_int(pExplain, 0);
3087            int iParentId = sqlite3_column_int(pExplain, 1);
3088            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3089            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3090          }
3091          eqp_render(pArg);
3092        }
3093        sqlite3_finalize(pExplain);
3094        sqlite3_free(zEQP);
3095        if( pArg->autoEQP>=AUTOEQP_full ){
3096          /* Also do an EXPLAIN for ".eqp full" mode */
3097          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3098          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3099          if( rc==SQLITE_OK ){
3100            pArg->cMode = MODE_Explain;
3101            explain_data_prepare(pArg, pExplain);
3102            exec_prepared_stmt(pArg, pExplain);
3103            explain_data_delete(pArg);
3104          }
3105          sqlite3_finalize(pExplain);
3106          sqlite3_free(zEQP);
3107        }
3108        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3109          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3110          /* Reprepare pStmt before reactiving trace modes */
3111          sqlite3_finalize(pStmt);
3112          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3113          if( pArg ) pArg->pStmt = pStmt;
3114        }
3115        restore_debug_trace_modes();
3116      }
3117
3118      if( pArg ){
3119        pArg->cMode = pArg->mode;
3120        if( pArg->autoExplain ){
3121          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3122            pArg->cMode = MODE_Explain;
3123          }
3124          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3125            pArg->cMode = MODE_EQP;
3126          }
3127        }
3128
3129        /* If the shell is currently in ".explain" mode, gather the extra
3130        ** data required to add indents to the output.*/
3131        if( pArg->cMode==MODE_Explain ){
3132          explain_data_prepare(pArg, pStmt);
3133        }
3134      }
3135
3136      bind_prepared_stmt(pArg, pStmt);
3137      exec_prepared_stmt(pArg, pStmt);
3138      explain_data_delete(pArg);
3139      eqp_render(pArg);
3140
3141      /* print usage stats if stats on */
3142      if( pArg && pArg->statsOn ){
3143        display_stats(db, pArg, 0);
3144      }
3145
3146      /* print loop-counters if required */
3147      if( pArg && pArg->scanstatsOn ){
3148        display_scanstats(db, pArg);
3149      }
3150
3151      /* Finalize the statement just executed. If this fails, save a
3152      ** copy of the error message. Otherwise, set zSql to point to the
3153      ** next statement to execute. */
3154      rc2 = sqlite3_finalize(pStmt);
3155      if( rc!=SQLITE_NOMEM ) rc = rc2;
3156      if( rc==SQLITE_OK ){
3157        zSql = zLeftover;
3158        while( IsSpace(zSql[0]) ) zSql++;
3159      }else if( pzErrMsg ){
3160        *pzErrMsg = save_err_msg(db);
3161      }
3162
3163      /* clear saved stmt handle */
3164      if( pArg ){
3165        pArg->pStmt = NULL;
3166      }
3167    }
3168  } /* end while */
3169
3170  return rc;
3171}
3172
3173/*
3174** Release memory previously allocated by tableColumnList().
3175*/
3176static void freeColumnList(char **azCol){
3177  int i;
3178  for(i=1; azCol[i]; i++){
3179    sqlite3_free(azCol[i]);
3180  }
3181  /* azCol[0] is a static string */
3182  sqlite3_free(azCol);
3183}
3184
3185/*
3186** Return a list of pointers to strings which are the names of all
3187** columns in table zTab.   The memory to hold the names is dynamically
3188** allocated and must be released by the caller using a subsequent call
3189** to freeColumnList().
3190**
3191** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3192** value that needs to be preserved, then azCol[0] is filled in with the
3193** name of the rowid column.
3194**
3195** The first regular column in the table is azCol[1].  The list is terminated
3196** by an entry with azCol[i]==0.
3197*/
3198static char **tableColumnList(ShellState *p, const char *zTab){
3199  char **azCol = 0;
3200  sqlite3_stmt *pStmt;
3201  char *zSql;
3202  int nCol = 0;
3203  int nAlloc = 0;
3204  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3205  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3206  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3207  int rc;
3208
3209  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3210  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3211  sqlite3_free(zSql);
3212  if( rc ) return 0;
3213  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3214    if( nCol>=nAlloc-2 ){
3215      nAlloc = nAlloc*2 + nCol + 10;
3216      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3217      if( azCol==0 ) shell_out_of_memory();
3218    }
3219    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3220    if( sqlite3_column_int(pStmt, 5) ){
3221      nPK++;
3222      if( nPK==1
3223       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3224                          "INTEGER")==0
3225      ){
3226        isIPK = 1;
3227      }else{
3228        isIPK = 0;
3229      }
3230    }
3231  }
3232  sqlite3_finalize(pStmt);
3233  if( azCol==0 ) return 0;
3234  azCol[0] = 0;
3235  azCol[nCol+1] = 0;
3236
3237  /* The decision of whether or not a rowid really needs to be preserved
3238  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3239  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3240  ** rowids on tables where the rowid is inaccessible because there are other
3241  ** columns in the table named "rowid", "_rowid_", and "oid".
3242  */
3243  if( preserveRowid && isIPK ){
3244    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3245    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3246    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3247    ** ROWID aliases.  To distinguish these cases, check to see if
3248    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3249    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3250    */
3251    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3252                           " WHERE origin='pk'", zTab);
3253    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3254    sqlite3_free(zSql);
3255    if( rc ){
3256      freeColumnList(azCol);
3257      return 0;
3258    }
3259    rc = sqlite3_step(pStmt);
3260    sqlite3_finalize(pStmt);
3261    preserveRowid = rc==SQLITE_ROW;
3262  }
3263  if( preserveRowid ){
3264    /* Only preserve the rowid if we can find a name to use for the
3265    ** rowid */
3266    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3267    int i, j;
3268    for(j=0; j<3; j++){
3269      for(i=1; i<=nCol; i++){
3270        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3271      }
3272      if( i>nCol ){
3273        /* At this point, we know that azRowid[j] is not the name of any
3274        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3275        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3276        ** tables will fail this last check */
3277        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3278        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3279        break;
3280      }
3281    }
3282  }
3283  return azCol;
3284}
3285
3286/*
3287** Toggle the reverse_unordered_selects setting.
3288*/
3289static void toggleSelectOrder(sqlite3 *db){
3290  sqlite3_stmt *pStmt = 0;
3291  int iSetting = 0;
3292  char zStmt[100];
3293  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3294  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3295    iSetting = sqlite3_column_int(pStmt, 0);
3296  }
3297  sqlite3_finalize(pStmt);
3298  sqlite3_snprintf(sizeof(zStmt), zStmt,
3299       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3300  sqlite3_exec(db, zStmt, 0, 0, 0);
3301}
3302
3303/*
3304** This is a different callback routine used for dumping the database.
3305** Each row received by this callback consists of a table name,
3306** the table type ("index" or "table") and SQL to create the table.
3307** This routine should print text sufficient to recreate the table.
3308*/
3309static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3310  int rc;
3311  const char *zTable;
3312  const char *zType;
3313  const char *zSql;
3314  ShellState *p = (ShellState *)pArg;
3315
3316  UNUSED_PARAMETER(azNotUsed);
3317  if( nArg!=3 || azArg==0 ) return 0;
3318  zTable = azArg[0];
3319  zType = azArg[1];
3320  zSql = azArg[2];
3321
3322  if( strcmp(zTable, "sqlite_sequence")==0 ){
3323    raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3324  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
3325    raw_printf(p->out, "ANALYZE sqlite_master;\n");
3326  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3327    return 0;
3328  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3329    char *zIns;
3330    if( !p->writableSchema ){
3331      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3332      p->writableSchema = 1;
3333    }
3334    zIns = sqlite3_mprintf(
3335       "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
3336       "VALUES('table','%q','%q',0,'%q');",
3337       zTable, zTable, zSql);
3338    utf8_printf(p->out, "%s\n", zIns);
3339    sqlite3_free(zIns);
3340    return 0;
3341  }else{
3342    printSchemaLine(p->out, zSql, ";\n");
3343  }
3344
3345  if( strcmp(zType, "table")==0 ){
3346    ShellText sSelect;
3347    ShellText sTable;
3348    char **azCol;
3349    int i;
3350    char *savedDestTable;
3351    int savedMode;
3352
3353    azCol = tableColumnList(p, zTable);
3354    if( azCol==0 ){
3355      p->nErr++;
3356      return 0;
3357    }
3358
3359    /* Always quote the table name, even if it appears to be pure ascii,
3360    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3361    initText(&sTable);
3362    appendText(&sTable, zTable, quoteChar(zTable));
3363    /* If preserving the rowid, add a column list after the table name.
3364    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3365    ** instead of the usual "INSERT INTO tab VALUES(...)".
3366    */
3367    if( azCol[0] ){
3368      appendText(&sTable, "(", 0);
3369      appendText(&sTable, azCol[0], 0);
3370      for(i=1; azCol[i]; i++){
3371        appendText(&sTable, ",", 0);
3372        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3373      }
3374      appendText(&sTable, ")", 0);
3375    }
3376
3377    /* Build an appropriate SELECT statement */
3378    initText(&sSelect);
3379    appendText(&sSelect, "SELECT ", 0);
3380    if( azCol[0] ){
3381      appendText(&sSelect, azCol[0], 0);
3382      appendText(&sSelect, ",", 0);
3383    }
3384    for(i=1; azCol[i]; i++){
3385      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3386      if( azCol[i+1] ){
3387        appendText(&sSelect, ",", 0);
3388      }
3389    }
3390    freeColumnList(azCol);
3391    appendText(&sSelect, " FROM ", 0);
3392    appendText(&sSelect, zTable, quoteChar(zTable));
3393
3394    savedDestTable = p->zDestTable;
3395    savedMode = p->mode;
3396    p->zDestTable = sTable.z;
3397    p->mode = p->cMode = MODE_Insert;
3398    rc = shell_exec(p, sSelect.z, 0);
3399    if( (rc&0xff)==SQLITE_CORRUPT ){
3400      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3401      toggleSelectOrder(p->db);
3402      shell_exec(p, sSelect.z, 0);
3403      toggleSelectOrder(p->db);
3404    }
3405    p->zDestTable = savedDestTable;
3406    p->mode = savedMode;
3407    freeText(&sTable);
3408    freeText(&sSelect);
3409    if( rc ) p->nErr++;
3410  }
3411  return 0;
3412}
3413
3414/*
3415** Run zQuery.  Use dump_callback() as the callback routine so that
3416** the contents of the query are output as SQL statements.
3417**
3418** If we get a SQLITE_CORRUPT error, rerun the query after appending
3419** "ORDER BY rowid DESC" to the end.
3420*/
3421static int run_schema_dump_query(
3422  ShellState *p,
3423  const char *zQuery
3424){
3425  int rc;
3426  char *zErr = 0;
3427  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3428  if( rc==SQLITE_CORRUPT ){
3429    char *zQ2;
3430    int len = strlen30(zQuery);
3431    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3432    if( zErr ){
3433      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3434      sqlite3_free(zErr);
3435      zErr = 0;
3436    }
3437    zQ2 = malloc( len+100 );
3438    if( zQ2==0 ) return rc;
3439    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3440    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3441    if( rc ){
3442      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3443    }else{
3444      rc = SQLITE_CORRUPT;
3445    }
3446    sqlite3_free(zErr);
3447    free(zQ2);
3448  }
3449  return rc;
3450}
3451
3452/*
3453** Text of help messages.
3454**
3455** The help text for each individual command begins with a line that starts
3456** with ".".  Subsequent lines are supplimental information.
3457**
3458** There must be two or more spaces between the end of the command and the
3459** start of the description of what that command does.
3460*/
3461static const char *(azHelp[]) = {
3462#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3463  ".archive ...             Manage SQL archives",
3464  "   Each command must have exactly one of the following options:",
3465  "     -c, --create               Create a new archive",
3466  "     -u, --update               Add or update files with changed mtime",
3467  "     -i, --insert               Like -u but always add even if unchanged",
3468  "     -t, --list                 List contents of archive",
3469  "     -x, --extract              Extract files from archive",
3470  "   Optional arguments:",
3471  "     -v, --verbose              Print each filename as it is processed",
3472  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3473  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3474  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3475  "     -n, --dryrun               Show the SQL that would have occurred",
3476  "   Examples:",
3477  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3478  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3479  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3480  "   See also:",
3481  "      http://sqlite.org/cli.html#sqlar_archive_support",
3482#endif
3483#ifndef SQLITE_OMIT_AUTHORIZATION
3484  ".auth ON|OFF             Show authorizer callbacks",
3485#endif
3486  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3487  "       --append            Use the appendvfs",
3488  "       --async             Write to FILE without journal and fsync()",
3489  ".bail on|off             Stop after hitting an error.  Default OFF",
3490  ".binary on|off           Turn binary output on or off.  Default OFF",
3491  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3492  ".changes on|off          Show number of rows changed by SQL",
3493  ".check GLOB              Fail if output since .testcase does not match",
3494  ".clone NEWDB             Clone data into NEWDB from the existing database",
3495  ".databases               List names and files of attached databases",
3496  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3497  ".dbinfo ?DB?             Show status information about the database",
3498  ".dump ?TABLE? ...        Render all database content as SQL",
3499  "   Options:",
3500  "     --preserve-rowids      Include ROWID values in the output",
3501  "     --newlines             Allow unescaped newline characters in output",
3502  "   TABLE is a LIKE pattern for the tables to dump",
3503  ".echo on|off             Turn command echo on or off",
3504  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3505  "   Other Modes:",
3506#ifdef SQLITE_DEBUG
3507  "      test                  Show raw EXPLAIN QUERY PLAN output",
3508  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3509#endif
3510  "      trigger               Like \"full\" but also show trigger bytecode",
3511  ".excel                   Display the output of next command in spreadsheet",
3512  ".exit ?CODE?             Exit this program with return-code CODE",
3513  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3514  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3515  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3516  "                           Run \".filectrl\" with no arguments for details",
3517  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3518  ".headers on|off          Turn display of headers on or off",
3519  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3520  ".import FILE TABLE       Import data from FILE into TABLE",
3521#ifndef SQLITE_OMIT_TEST_CONTROL
3522  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3523#endif
3524  ".indexes ?TABLE?         Show names of indexes",
3525  "                           If TABLE is specified, only show indexes for",
3526  "                           tables matching TABLE using the LIKE operator.",
3527#ifdef SQLITE_ENABLE_IOTRACE
3528  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3529#endif
3530  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3531  ".lint OPTIONS            Report potential schema issues.",
3532  "     Options:",
3533  "        fkey-indexes     Find missing foreign key indexes",
3534#ifndef SQLITE_OMIT_LOAD_EXTENSION
3535  ".load FILE ?ENTRY?       Load an extension library",
3536#endif
3537  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3538  ".mode MODE ?TABLE?       Set output mode",
3539  "   MODE is one of:",
3540  "     ascii    Columns/rows delimited by 0x1F and 0x1E",
3541  "     csv      Comma-separated values",
3542  "     column   Left-aligned columns.  (See .width)",
3543  "     html     HTML <table> code",
3544  "     insert   SQL insert statements for TABLE",
3545  "     line     One value per line",
3546  "     list     Values delimited by \"|\"",
3547  "     quote    Escape answers as for SQL",
3548  "     tabs     Tab-separated values",
3549  "     tcl      TCL list elements",
3550  ".nullvalue STRING        Use STRING in place of NULL values",
3551  ".once (-e|-x|FILE)       Output for the next SQL command only to FILE",
3552  "     If FILE begins with '|' then open as a pipe",
3553  "     Other options:",
3554  "       -e    Invoke system text editor",
3555  "       -x    Open in a spreadsheet",
3556  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3557  "     Options:",
3558  "        --append        Use appendvfs to append database to the end of FILE",
3559#ifdef SQLITE_ENABLE_DESERIALIZE
3560  "        --deserialize   Load into memory useing sqlite3_deserialize()",
3561  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3562  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3563#endif
3564  "        --new           Initialize FILE to an empty database",
3565  "        --readonly      Open FILE readonly",
3566  "        --zip           FILE is a ZIP archive",
3567  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
3568  "     If FILE begins with '|' then open it as a pipe.",
3569  ".parameter CMD ...       Manage SQL parameter bindings",
3570  "   clear                   Erase all bindings",
3571  "   init                    Initialize the TEMP table that holds bindings",
3572  "   list                    List the current parameter bindings",
3573  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
3574  "                           PARAMETER should start with one of: $ : @ ?",
3575  "   unset PARAMETER         Remove PARAMETER from the binding table",
3576  ".print STRING...         Print literal STRING",
3577#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
3578  ".progress N              Invoke progress handler after every N opcodes",
3579  "   --limit N                 Interrupt after N progress callbacks",
3580  "   --once                    Do no more than one progress interrupt",
3581  "   --quiet|-q                No output except at interrupts",
3582  "   --reset                   Reset the count for each input and interrupt",
3583#endif
3584  ".prompt MAIN CONTINUE    Replace the standard prompts",
3585  ".quit                    Exit this program",
3586  ".read FILE               Read input from FILE",
3587#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
3588  ".recover                 Recover as much data as possible from corrupt db.",
3589  "   --freelist-corrupt       Assume the freelist is corrupt",
3590  "   --recovery-db NAME       Store recovery metadata in database file NAME",
3591  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
3592  "   --no-rowids              Do not attempt to recover rowid values",
3593  "                            that are not also INTEGER PRIMARY KEYs",
3594#endif
3595  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
3596  ".save FILE               Write in-memory database into FILE",
3597  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
3598  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
3599  "     Options:",
3600  "         --indent            Try to pretty-print the schema",
3601  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
3602  "    Options:",
3603  "       --init               Create a new SELFTEST table",
3604  "       -v                   Verbose output",
3605  ".separator COL ?ROW?     Change the column and row separators",
3606#if defined(SQLITE_ENABLE_SESSION)
3607  ".session ?NAME? CMD ...  Create or control sessions",
3608  "   Subcommands:",
3609  "     attach TABLE             Attach TABLE",
3610  "     changeset FILE           Write a changeset into FILE",
3611  "     close                    Close one session",
3612  "     enable ?BOOLEAN?         Set or query the enable bit",
3613  "     filter GLOB...           Reject tables matching GLOBs",
3614  "     indirect ?BOOLEAN?       Mark or query the indirect status",
3615  "     isempty                  Query whether the session is empty",
3616  "     list                     List currently open session names",
3617  "     open DB NAME             Open a new session on DB",
3618  "     patchset FILE            Write a patchset into FILE",
3619  "   If ?NAME? is omitted, the first defined session is used.",
3620#endif
3621  ".sha3sum ...             Compute a SHA3 hash of database content",
3622  "    Options:",
3623  "      --schema              Also hash the sqlite_master table",
3624  "      --sha3-224            Use the sha3-224 algorithm",
3625  "      --sha3-256            Use the sha3-256 algorithm (default)",
3626  "      --sha3-384            Use the sha3-384 algorithm",
3627  "      --sha3-512            Use the sha3-512 algorithm",
3628  "    Any other argument is a LIKE pattern for tables to hash",
3629#ifndef SQLITE_NOHAVE_SYSTEM
3630  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
3631#endif
3632  ".show                    Show the current values for various settings",
3633  ".stats ?on|off?          Show stats or turn stats on or off",
3634#ifndef SQLITE_NOHAVE_SYSTEM
3635  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
3636#endif
3637  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
3638  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
3639  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
3640  "                           Run \".testctrl\" with no arguments for details",
3641  ".timeout MS              Try opening locked tables for MS milliseconds",
3642  ".timer on|off            Turn SQL timer on or off",
3643#ifndef SQLITE_OMIT_TRACE
3644  ".trace ?OPTIONS?         Output each SQL statement as it is run",
3645  "    FILE                    Send output to FILE",
3646  "    stdout                  Send output to stdout",
3647  "    stderr                  Send output to stderr",
3648  "    off                     Disable tracing",
3649  "    --expanded              Expand query parameters",
3650#ifdef SQLITE_ENABLE_NORMALIZE
3651  "    --normalized            Normal the SQL statements",
3652#endif
3653  "    --plain                 Show SQL as it is input",
3654  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
3655  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
3656  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
3657  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
3658#endif /* SQLITE_OMIT_TRACE */
3659#ifdef SQLITE_DEBUG
3660  ".unmodule NAME ...       Unregister virtual table modules",
3661  "    --allexcept             Unregister everything except those named",
3662#endif
3663  ".vfsinfo ?AUX?           Information about the top-level VFS",
3664  ".vfslist                 List all available VFSes",
3665  ".vfsname ?AUX?           Print the name of the VFS stack",
3666  ".width NUM1 NUM2 ...     Set column widths for \"column\" mode",
3667  "     Negative values right-justify",
3668};
3669
3670/*
3671** Output help text.
3672**
3673** zPattern describes the set of commands for which help text is provided.
3674** If zPattern is NULL, then show all commands, but only give a one-line
3675** description of each.
3676**
3677** Return the number of matches.
3678*/
3679static int showHelp(FILE *out, const char *zPattern){
3680  int i = 0;
3681  int j = 0;
3682  int n = 0;
3683  char *zPat;
3684  if( zPattern==0
3685   || zPattern[0]=='0'
3686   || strcmp(zPattern,"-a")==0
3687   || strcmp(zPattern,"-all")==0
3688  ){
3689    /* Show all commands, but only one line per command */
3690    if( zPattern==0 ) zPattern = "";
3691    for(i=0; i<ArraySize(azHelp); i++){
3692      if( azHelp[i][0]=='.' || zPattern[0] ){
3693        utf8_printf(out, "%s\n", azHelp[i]);
3694        n++;
3695      }
3696    }
3697  }else{
3698    /* Look for commands that for which zPattern is an exact prefix */
3699    zPat = sqlite3_mprintf(".%s*", zPattern);
3700    for(i=0; i<ArraySize(azHelp); i++){
3701      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
3702        utf8_printf(out, "%s\n", azHelp[i]);
3703        j = i+1;
3704        n++;
3705      }
3706    }
3707    sqlite3_free(zPat);
3708    if( n ){
3709      if( n==1 ){
3710        /* when zPattern is a prefix of exactly one command, then include the
3711        ** details of that command, which should begin at offset j */
3712        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
3713          utf8_printf(out, "%s\n", azHelp[j]);
3714          j++;
3715        }
3716      }
3717      return n;
3718    }
3719    /* Look for commands that contain zPattern anywhere.  Show the complete
3720    ** text of all commands that match. */
3721    zPat = sqlite3_mprintf("%%%s%%", zPattern);
3722    for(i=0; i<ArraySize(azHelp); i++){
3723      if( azHelp[i][0]=='.' ) j = i;
3724      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
3725        utf8_printf(out, "%s\n", azHelp[j]);
3726        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
3727          j++;
3728          utf8_printf(out, "%s\n", azHelp[j]);
3729        }
3730        i = j;
3731        n++;
3732      }
3733    }
3734    sqlite3_free(zPat);
3735  }
3736  return n;
3737}
3738
3739/* Forward reference */
3740static int process_input(ShellState *p);
3741
3742/*
3743** Read the content of file zName into memory obtained from sqlite3_malloc64()
3744** and return a pointer to the buffer. The caller is responsible for freeing
3745** the memory.
3746**
3747** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
3748** read.
3749**
3750** For convenience, a nul-terminator byte is always appended to the data read
3751** from the file before the buffer is returned. This byte is not included in
3752** the final value of (*pnByte), if applicable.
3753**
3754** NULL is returned if any error is encountered. The final value of *pnByte
3755** is undefined in this case.
3756*/
3757static char *readFile(const char *zName, int *pnByte){
3758  FILE *in = fopen(zName, "rb");
3759  long nIn;
3760  size_t nRead;
3761  char *pBuf;
3762  if( in==0 ) return 0;
3763  fseek(in, 0, SEEK_END);
3764  nIn = ftell(in);
3765  rewind(in);
3766  pBuf = sqlite3_malloc64( nIn+1 );
3767  if( pBuf==0 ){ fclose(in); return 0; }
3768  nRead = fread(pBuf, nIn, 1, in);
3769  fclose(in);
3770  if( nRead!=1 ){
3771    sqlite3_free(pBuf);
3772    return 0;
3773  }
3774  pBuf[nIn] = 0;
3775  if( pnByte ) *pnByte = nIn;
3776  return pBuf;
3777}
3778
3779#if defined(SQLITE_ENABLE_SESSION)
3780/*
3781** Close a single OpenSession object and release all of its associated
3782** resources.
3783*/
3784static void session_close(OpenSession *pSession){
3785  int i;
3786  sqlite3session_delete(pSession->p);
3787  sqlite3_free(pSession->zName);
3788  for(i=0; i<pSession->nFilter; i++){
3789    sqlite3_free(pSession->azFilter[i]);
3790  }
3791  sqlite3_free(pSession->azFilter);
3792  memset(pSession, 0, sizeof(OpenSession));
3793}
3794#endif
3795
3796/*
3797** Close all OpenSession objects and release all associated resources.
3798*/
3799#if defined(SQLITE_ENABLE_SESSION)
3800static void session_close_all(ShellState *p){
3801  int i;
3802  for(i=0; i<p->nSession; i++){
3803    session_close(&p->aSession[i]);
3804  }
3805  p->nSession = 0;
3806}
3807#else
3808# define session_close_all(X)
3809#endif
3810
3811/*
3812** Implementation of the xFilter function for an open session.  Omit
3813** any tables named by ".session filter" but let all other table through.
3814*/
3815#if defined(SQLITE_ENABLE_SESSION)
3816static int session_filter(void *pCtx, const char *zTab){
3817  OpenSession *pSession = (OpenSession*)pCtx;
3818  int i;
3819  for(i=0; i<pSession->nFilter; i++){
3820    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
3821  }
3822  return 1;
3823}
3824#endif
3825
3826/*
3827** Try to deduce the type of file for zName based on its content.  Return
3828** one of the SHELL_OPEN_* constants.
3829**
3830** If the file does not exist or is empty but its name looks like a ZIP
3831** archive and the dfltZip flag is true, then assume it is a ZIP archive.
3832** Otherwise, assume an ordinary database regardless of the filename if
3833** the type cannot be determined from content.
3834*/
3835int deduceDatabaseType(const char *zName, int dfltZip){
3836  FILE *f = fopen(zName, "rb");
3837  size_t n;
3838  int rc = SHELL_OPEN_UNSPEC;
3839  char zBuf[100];
3840  if( f==0 ){
3841    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
3842       return SHELL_OPEN_ZIPFILE;
3843    }else{
3844       return SHELL_OPEN_NORMAL;
3845    }
3846  }
3847  n = fread(zBuf, 16, 1, f);
3848  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
3849    fclose(f);
3850    return SHELL_OPEN_NORMAL;
3851  }
3852  fseek(f, -25, SEEK_END);
3853  n = fread(zBuf, 25, 1, f);
3854  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
3855    rc = SHELL_OPEN_APPENDVFS;
3856  }else{
3857    fseek(f, -22, SEEK_END);
3858    n = fread(zBuf, 22, 1, f);
3859    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
3860       && zBuf[3]==0x06 ){
3861      rc = SHELL_OPEN_ZIPFILE;
3862    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
3863      rc = SHELL_OPEN_ZIPFILE;
3864    }
3865  }
3866  fclose(f);
3867  return rc;
3868}
3869
3870#ifdef SQLITE_ENABLE_DESERIALIZE
3871/*
3872** Reconstruct an in-memory database using the output from the "dbtotxt"
3873** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
3874** is 0, then read from standard input.
3875*/
3876static unsigned char *readHexDb(ShellState *p, int *pnData){
3877  unsigned char *a = 0;
3878  int nLine;
3879  int n = 0;
3880  int pgsz = 0;
3881  int iOffset = 0;
3882  int j, k;
3883  int rc;
3884  FILE *in;
3885  unsigned int x[16];
3886  char zLine[1000];
3887  if( p->zDbFilename ){
3888    in = fopen(p->zDbFilename, "r");
3889    if( in==0 ){
3890      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
3891      return 0;
3892    }
3893    nLine = 0;
3894  }else{
3895    in = p->in;
3896    nLine = p->lineno;
3897    if( in==0 ) in = stdin;
3898  }
3899  *pnData = 0;
3900  nLine++;
3901  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
3902  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
3903  if( rc!=2 ) goto readHexDb_error;
3904  if( n<0 ) goto readHexDb_error;
3905  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
3906  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
3907  a = sqlite3_malloc( n ? n : 1 );
3908  if( a==0 ){
3909    utf8_printf(stderr, "Out of memory!\n");
3910    goto readHexDb_error;
3911  }
3912  memset(a, 0, n);
3913  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
3914    utf8_printf(stderr, "invalid pagesize\n");
3915    goto readHexDb_error;
3916  }
3917  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
3918    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
3919    if( rc==2 ){
3920      iOffset = k;
3921      continue;
3922    }
3923    if( strncmp(zLine, "| end ", 6)==0 ){
3924      break;
3925    }
3926    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
3927                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
3928                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
3929    if( rc==17 ){
3930      k = iOffset+j;
3931      if( k+16<=n ){
3932        int ii;
3933        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
3934      }
3935    }
3936  }
3937  *pnData = n;
3938  if( in!=p->in ){
3939    fclose(in);
3940  }else{
3941    p->lineno = nLine;
3942  }
3943  return a;
3944
3945readHexDb_error:
3946  if( in!=p->in ){
3947    fclose(in);
3948  }else{
3949    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
3950      nLine++;
3951      if(strncmp(zLine, "| end ", 6)==0 ) break;
3952    }
3953    p->lineno = nLine;
3954  }
3955  sqlite3_free(a);
3956  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
3957  return 0;
3958}
3959#endif /* SQLITE_ENABLE_DESERIALIZE */
3960
3961/*
3962** Scalar function "shell_int32". The first argument to this function
3963** must be a blob. The second a non-negative integer. This function
3964** reads and returns a 32-bit big-endian integer from byte
3965** offset (4*<arg2>) of the blob.
3966*/
3967static void shellInt32(
3968  sqlite3_context *context,
3969  int argc,
3970  sqlite3_value **argv
3971){
3972  const unsigned char *pBlob;
3973  int nBlob;
3974  int iInt;
3975
3976  UNUSED_PARAMETER(argc);
3977  nBlob = sqlite3_value_bytes(argv[0]);
3978  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
3979  iInt = sqlite3_value_int(argv[1]);
3980
3981  if( iInt>=0 && (iInt+1)*4<=nBlob ){
3982    const unsigned char *a = &pBlob[iInt*4];
3983    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
3984                       + ((sqlite3_int64)a[1]<<16)
3985                       + ((sqlite3_int64)a[2]<< 8)
3986                       + ((sqlite3_int64)a[3]<< 0);
3987    sqlite3_result_int64(context, iVal);
3988  }
3989}
3990
3991/*
3992** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
3993** using "..." with internal double-quote characters doubled.
3994*/
3995static void shellIdQuote(
3996  sqlite3_context *context,
3997  int argc,
3998  sqlite3_value **argv
3999){
4000  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4001  UNUSED_PARAMETER(argc);
4002  if( zName ){
4003    char *z = sqlite3_mprintf("\"%w\"", zName);
4004    sqlite3_result_text(context, z, -1, sqlite3_free);
4005  }
4006}
4007
4008/*
4009** Scalar function "shell_escape_crnl" used by the .recover command.
4010** The argument passed to this function is the output of built-in
4011** function quote(). If the first character of the input is "'",
4012** indicating that the value passed to quote() was a text value,
4013** then this function searches the input for "\n" and "\r" characters
4014** and adds a wrapper similar to the following:
4015**
4016**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4017**
4018** Or, if the first character of the input is not "'", then a copy
4019** of the input is returned.
4020*/
4021static void shellEscapeCrnl(
4022  sqlite3_context *context,
4023  int argc,
4024  sqlite3_value **argv
4025){
4026  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4027  UNUSED_PARAMETER(argc);
4028  if( zText[0]=='\'' ){
4029    int nText = sqlite3_value_bytes(argv[0]);
4030    int i;
4031    char zBuf1[20];
4032    char zBuf2[20];
4033    const char *zNL = 0;
4034    const char *zCR = 0;
4035    int nCR = 0;
4036    int nNL = 0;
4037
4038    for(i=0; zText[i]; i++){
4039      if( zNL==0 && zText[i]=='\n' ){
4040        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4041        nNL = (int)strlen(zNL);
4042      }
4043      if( zCR==0 && zText[i]=='\r' ){
4044        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4045        nCR = (int)strlen(zCR);
4046      }
4047    }
4048
4049    if( zNL || zCR ){
4050      int iOut = 0;
4051      i64 nMax = (nNL > nCR) ? nNL : nCR;
4052      i64 nAlloc = nMax * nText + (nMax+64)*2;
4053      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4054      if( zOut==0 ){
4055        sqlite3_result_error_nomem(context);
4056        return;
4057      }
4058
4059      if( zNL && zCR ){
4060        memcpy(&zOut[iOut], "replace(replace(", 16);
4061        iOut += 16;
4062      }else{
4063        memcpy(&zOut[iOut], "replace(", 8);
4064        iOut += 8;
4065      }
4066      for(i=0; zText[i]; i++){
4067        if( zText[i]=='\n' ){
4068          memcpy(&zOut[iOut], zNL, nNL);
4069          iOut += nNL;
4070        }else if( zText[i]=='\r' ){
4071          memcpy(&zOut[iOut], zCR, nCR);
4072          iOut += nCR;
4073        }else{
4074          zOut[iOut] = zText[i];
4075          iOut++;
4076        }
4077      }
4078
4079      if( zNL ){
4080        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4081        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4082        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4083      }
4084      if( zCR ){
4085        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4086        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4087        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4088      }
4089
4090      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4091      sqlite3_free(zOut);
4092      return;
4093    }
4094  }
4095
4096  sqlite3_result_value(context, argv[0]);
4097}
4098
4099/* Flags for open_db().
4100**
4101** The default behavior of open_db() is to exit(1) if the database fails to
4102** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4103** but still returns without calling exit.
4104**
4105** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4106** ZIP archive if the file does not exist or is empty and its name matches
4107** the *.zip pattern.
4108*/
4109#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4110#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4111
4112/*
4113** Make sure the database is open.  If it is not, then open it.  If
4114** the database fails to open, print an error message and exit.
4115*/
4116static void open_db(ShellState *p, int openFlags){
4117  if( p->db==0 ){
4118    if( p->openMode==SHELL_OPEN_UNSPEC ){
4119      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4120        p->openMode = SHELL_OPEN_NORMAL;
4121      }else{
4122        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4123                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4124      }
4125    }
4126    switch( p->openMode ){
4127      case SHELL_OPEN_APPENDVFS: {
4128        sqlite3_open_v2(p->zDbFilename, &p->db,
4129           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
4130        break;
4131      }
4132      case SHELL_OPEN_HEXDB:
4133      case SHELL_OPEN_DESERIALIZE: {
4134        sqlite3_open(0, &p->db);
4135        break;
4136      }
4137      case SHELL_OPEN_ZIPFILE: {
4138        sqlite3_open(":memory:", &p->db);
4139        break;
4140      }
4141      case SHELL_OPEN_READONLY: {
4142        sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
4143        break;
4144      }
4145      case SHELL_OPEN_UNSPEC:
4146      case SHELL_OPEN_NORMAL: {
4147        sqlite3_open(p->zDbFilename, &p->db);
4148        break;
4149      }
4150    }
4151    globalDb = p->db;
4152    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4153      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4154          p->zDbFilename, sqlite3_errmsg(p->db));
4155      if( openFlags & OPEN_DB_KEEPALIVE ){
4156        sqlite3_open(":memory:", &p->db);
4157        return;
4158      }
4159      exit(1);
4160    }
4161#ifndef SQLITE_OMIT_LOAD_EXTENSION
4162    sqlite3_enable_load_extension(p->db, 1);
4163#endif
4164    sqlite3_fileio_init(p->db, 0, 0);
4165    sqlite3_shathree_init(p->db, 0, 0);
4166    sqlite3_completion_init(p->db, 0, 0);
4167#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4168    sqlite3_dbdata_init(p->db, 0, 0);
4169#endif
4170#ifdef SQLITE_HAVE_ZLIB
4171    sqlite3_zipfile_init(p->db, 0, 0);
4172    sqlite3_sqlar_init(p->db, 0, 0);
4173#endif
4174    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4175                            shellAddSchemaName, 0, 0);
4176    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4177                            shellModuleSchema, 0, 0);
4178    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4179                            shellPutsFunc, 0, 0);
4180    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4181                            shellEscapeCrnl, 0, 0);
4182    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4183                            shellInt32, 0, 0);
4184    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4185                            shellIdQuote, 0, 0);
4186#ifndef SQLITE_NOHAVE_SYSTEM
4187    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4188                            editFunc, 0, 0);
4189    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4190                            editFunc, 0, 0);
4191#endif
4192    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4193      char *zSql = sqlite3_mprintf(
4194         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4195      sqlite3_exec(p->db, zSql, 0, 0, 0);
4196      sqlite3_free(zSql);
4197    }
4198#ifdef SQLITE_ENABLE_DESERIALIZE
4199    else
4200    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4201      int rc;
4202      int nData = 0;
4203      unsigned char *aData;
4204      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4205        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4206      }else{
4207        aData = readHexDb(p, &nData);
4208        if( aData==0 ){
4209          return;
4210        }
4211      }
4212      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4213                   SQLITE_DESERIALIZE_RESIZEABLE |
4214                   SQLITE_DESERIALIZE_FREEONCLOSE);
4215      if( rc ){
4216        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4217      }
4218      if( p->szMax>0 ){
4219        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4220      }
4221    }
4222#endif
4223  }
4224}
4225
4226/*
4227** Attempt to close the databaes connection.  Report errors.
4228*/
4229void close_db(sqlite3 *db){
4230  int rc = sqlite3_close(db);
4231  if( rc ){
4232    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4233        rc, sqlite3_errmsg(db));
4234  }
4235}
4236
4237#if HAVE_READLINE || HAVE_EDITLINE
4238/*
4239** Readline completion callbacks
4240*/
4241static char *readline_completion_generator(const char *text, int state){
4242  static sqlite3_stmt *pStmt = 0;
4243  char *zRet;
4244  if( state==0 ){
4245    char *zSql;
4246    sqlite3_finalize(pStmt);
4247    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4248                           "  FROM completion(%Q) ORDER BY 1", text);
4249    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4250    sqlite3_free(zSql);
4251  }
4252  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4253    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4254  }else{
4255    sqlite3_finalize(pStmt);
4256    pStmt = 0;
4257    zRet = 0;
4258  }
4259  return zRet;
4260}
4261static char **readline_completion(const char *zText, int iStart, int iEnd){
4262  rl_attempted_completion_over = 1;
4263  return rl_completion_matches(zText, readline_completion_generator);
4264}
4265
4266#elif HAVE_LINENOISE
4267/*
4268** Linenoise completion callback
4269*/
4270static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4271  int nLine = strlen30(zLine);
4272  int i, iStart;
4273  sqlite3_stmt *pStmt = 0;
4274  char *zSql;
4275  char zBuf[1000];
4276
4277  if( nLine>sizeof(zBuf)-30 ) return;
4278  if( zLine[0]=='.' || zLine[0]=='#') return;
4279  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4280  if( i==nLine-1 ) return;
4281  iStart = i+1;
4282  memcpy(zBuf, zLine, iStart);
4283  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4284                         "  FROM completion(%Q,%Q) ORDER BY 1",
4285                         &zLine[iStart], zLine);
4286  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4287  sqlite3_free(zSql);
4288  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4289  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4290    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4291    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4292    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4293      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4294      linenoiseAddCompletion(lc, zBuf);
4295    }
4296  }
4297  sqlite3_finalize(pStmt);
4298}
4299#endif
4300
4301/*
4302** Do C-language style dequoting.
4303**
4304**    \a    -> alarm
4305**    \b    -> backspace
4306**    \t    -> tab
4307**    \n    -> newline
4308**    \v    -> vertical tab
4309**    \f    -> form feed
4310**    \r    -> carriage return
4311**    \s    -> space
4312**    \"    -> "
4313**    \'    -> '
4314**    \\    -> backslash
4315**    \NNN  -> ascii character NNN in octal
4316*/
4317static void resolve_backslashes(char *z){
4318  int i, j;
4319  char c;
4320  while( *z && *z!='\\' ) z++;
4321  for(i=j=0; (c = z[i])!=0; i++, j++){
4322    if( c=='\\' && z[i+1]!=0 ){
4323      c = z[++i];
4324      if( c=='a' ){
4325        c = '\a';
4326      }else if( c=='b' ){
4327        c = '\b';
4328      }else if( c=='t' ){
4329        c = '\t';
4330      }else if( c=='n' ){
4331        c = '\n';
4332      }else if( c=='v' ){
4333        c = '\v';
4334      }else if( c=='f' ){
4335        c = '\f';
4336      }else if( c=='r' ){
4337        c = '\r';
4338      }else if( c=='"' ){
4339        c = '"';
4340      }else if( c=='\'' ){
4341        c = '\'';
4342      }else if( c=='\\' ){
4343        c = '\\';
4344      }else if( c>='0' && c<='7' ){
4345        c -= '0';
4346        if( z[i+1]>='0' && z[i+1]<='7' ){
4347          i++;
4348          c = (c<<3) + z[i] - '0';
4349          if( z[i+1]>='0' && z[i+1]<='7' ){
4350            i++;
4351            c = (c<<3) + z[i] - '0';
4352          }
4353        }
4354      }
4355    }
4356    z[j] = c;
4357  }
4358  if( j<i ) z[j] = 0;
4359}
4360
4361/*
4362** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4363** for TRUE and FALSE.  Return the integer value if appropriate.
4364*/
4365static int booleanValue(const char *zArg){
4366  int i;
4367  if( zArg[0]=='0' && zArg[1]=='x' ){
4368    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4369  }else{
4370    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4371  }
4372  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4373  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4374    return 1;
4375  }
4376  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4377    return 0;
4378  }
4379  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4380          zArg);
4381  return 0;
4382}
4383
4384/*
4385** Set or clear a shell flag according to a boolean value.
4386*/
4387static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4388  if( booleanValue(zArg) ){
4389    ShellSetFlag(p, mFlag);
4390  }else{
4391    ShellClearFlag(p, mFlag);
4392  }
4393}
4394
4395/*
4396** Close an output file, assuming it is not stderr or stdout
4397*/
4398static void output_file_close(FILE *f){
4399  if( f && f!=stdout && f!=stderr ) fclose(f);
4400}
4401
4402/*
4403** Try to open an output file.   The names "stdout" and "stderr" are
4404** recognized and do the right thing.  NULL is returned if the output
4405** filename is "off".
4406*/
4407static FILE *output_file_open(const char *zFile, int bTextMode){
4408  FILE *f;
4409  if( strcmp(zFile,"stdout")==0 ){
4410    f = stdout;
4411  }else if( strcmp(zFile, "stderr")==0 ){
4412    f = stderr;
4413  }else if( strcmp(zFile, "off")==0 ){
4414    f = 0;
4415  }else{
4416    f = fopen(zFile, bTextMode ? "w" : "wb");
4417    if( f==0 ){
4418      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4419    }
4420  }
4421  return f;
4422}
4423
4424#ifndef SQLITE_OMIT_TRACE
4425/*
4426** A routine for handling output from sqlite3_trace().
4427*/
4428static int sql_trace_callback(
4429  unsigned mType,         /* The trace type */
4430  void *pArg,             /* The ShellState pointer */
4431  void *pP,               /* Usually a pointer to sqlite_stmt */
4432  void *pX                /* Auxiliary output */
4433){
4434  ShellState *p = (ShellState*)pArg;
4435  sqlite3_stmt *pStmt;
4436  const char *zSql;
4437  int nSql;
4438  if( p->traceOut==0 ) return 0;
4439  if( mType==SQLITE_TRACE_CLOSE ){
4440    utf8_printf(p->traceOut, "-- closing database connection\n");
4441    return 0;
4442  }
4443  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4444    zSql = (const char*)pX;
4445  }else{
4446    pStmt = (sqlite3_stmt*)pP;
4447    switch( p->eTraceType ){
4448      case SHELL_TRACE_EXPANDED: {
4449        zSql = sqlite3_expanded_sql(pStmt);
4450        break;
4451      }
4452#ifdef SQLITE_ENABLE_NORMALIZE
4453      case SHELL_TRACE_NORMALIZED: {
4454        zSql = sqlite3_normalized_sql(pStmt);
4455        break;
4456      }
4457#endif
4458      default: {
4459        zSql = sqlite3_sql(pStmt);
4460        break;
4461      }
4462    }
4463  }
4464  if( zSql==0 ) return 0;
4465  nSql = strlen30(zSql);
4466  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4467  switch( mType ){
4468    case SQLITE_TRACE_ROW:
4469    case SQLITE_TRACE_STMT: {
4470      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4471      break;
4472    }
4473    case SQLITE_TRACE_PROFILE: {
4474      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4475      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4476      break;
4477    }
4478  }
4479  return 0;
4480}
4481#endif
4482
4483/*
4484** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4485** a useful spot to set a debugger breakpoint.
4486*/
4487static void test_breakpoint(void){
4488  static int nCall = 0;
4489  nCall++;
4490}
4491
4492/*
4493** An object used to read a CSV and other files for import.
4494*/
4495typedef struct ImportCtx ImportCtx;
4496struct ImportCtx {
4497  const char *zFile;  /* Name of the input file */
4498  FILE *in;           /* Read the CSV text from this input stream */
4499  char *z;            /* Accumulated text for a field */
4500  int n;              /* Number of bytes in z */
4501  int nAlloc;         /* Space allocated for z[] */
4502  int nLine;          /* Current line number */
4503  int bNotFirst;      /* True if one or more bytes already read */
4504  int cTerm;          /* Character that terminated the most recent field */
4505  int cColSep;        /* The column separator character.  (Usually ",") */
4506  int cRowSep;        /* The row separator character.  (Usually "\n") */
4507};
4508
4509/* Append a single byte to z[] */
4510static void import_append_char(ImportCtx *p, int c){
4511  if( p->n+1>=p->nAlloc ){
4512    p->nAlloc += p->nAlloc + 100;
4513    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4514    if( p->z==0 ) shell_out_of_memory();
4515  }
4516  p->z[p->n++] = (char)c;
4517}
4518
4519/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4520** with the option of having a separator other than ",".
4521**
4522**   +  Input comes from p->in.
4523**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4524**      from sqlite3_malloc64().
4525**   +  Use p->cSep as the column separator.  The default is ",".
4526**   +  Use p->rSep as the row separator.  The default is "\n".
4527**   +  Keep track of the line number in p->nLine.
4528**   +  Store the character that terminates the field in p->cTerm.  Store
4529**      EOF on end-of-file.
4530**   +  Report syntax errors on stderr
4531*/
4532static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4533  int c;
4534  int cSep = p->cColSep;
4535  int rSep = p->cRowSep;
4536  p->n = 0;
4537  c = fgetc(p->in);
4538  if( c==EOF || seenInterrupt ){
4539    p->cTerm = EOF;
4540    return 0;
4541  }
4542  if( c=='"' ){
4543    int pc, ppc;
4544    int startLine = p->nLine;
4545    int cQuote = c;
4546    pc = ppc = 0;
4547    while( 1 ){
4548      c = fgetc(p->in);
4549      if( c==rSep ) p->nLine++;
4550      if( c==cQuote ){
4551        if( pc==cQuote ){
4552          pc = 0;
4553          continue;
4554        }
4555      }
4556      if( (c==cSep && pc==cQuote)
4557       || (c==rSep && pc==cQuote)
4558       || (c==rSep && pc=='\r' && ppc==cQuote)
4559       || (c==EOF && pc==cQuote)
4560      ){
4561        do{ p->n--; }while( p->z[p->n]!=cQuote );
4562        p->cTerm = c;
4563        break;
4564      }
4565      if( pc==cQuote && c!='\r' ){
4566        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
4567                p->zFile, p->nLine, cQuote);
4568      }
4569      if( c==EOF ){
4570        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
4571                p->zFile, startLine, cQuote);
4572        p->cTerm = c;
4573        break;
4574      }
4575      import_append_char(p, c);
4576      ppc = pc;
4577      pc = c;
4578    }
4579  }else{
4580    /* If this is the first field being parsed and it begins with the
4581    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
4582    if( (c&0xff)==0xef && p->bNotFirst==0 ){
4583      import_append_char(p, c);
4584      c = fgetc(p->in);
4585      if( (c&0xff)==0xbb ){
4586        import_append_char(p, c);
4587        c = fgetc(p->in);
4588        if( (c&0xff)==0xbf ){
4589          p->bNotFirst = 1;
4590          p->n = 0;
4591          return csv_read_one_field(p);
4592        }
4593      }
4594    }
4595    while( c!=EOF && c!=cSep && c!=rSep ){
4596      import_append_char(p, c);
4597      c = fgetc(p->in);
4598    }
4599    if( c==rSep ){
4600      p->nLine++;
4601      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
4602    }
4603    p->cTerm = c;
4604  }
4605  if( p->z ) p->z[p->n] = 0;
4606  p->bNotFirst = 1;
4607  return p->z;
4608}
4609
4610/* Read a single field of ASCII delimited text.
4611**
4612**   +  Input comes from p->in.
4613**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4614**      from sqlite3_malloc64().
4615**   +  Use p->cSep as the column separator.  The default is "\x1F".
4616**   +  Use p->rSep as the row separator.  The default is "\x1E".
4617**   +  Keep track of the row number in p->nLine.
4618**   +  Store the character that terminates the field in p->cTerm.  Store
4619**      EOF on end-of-file.
4620**   +  Report syntax errors on stderr
4621*/
4622static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
4623  int c;
4624  int cSep = p->cColSep;
4625  int rSep = p->cRowSep;
4626  p->n = 0;
4627  c = fgetc(p->in);
4628  if( c==EOF || seenInterrupt ){
4629    p->cTerm = EOF;
4630    return 0;
4631  }
4632  while( c!=EOF && c!=cSep && c!=rSep ){
4633    import_append_char(p, c);
4634    c = fgetc(p->in);
4635  }
4636  if( c==rSep ){
4637    p->nLine++;
4638  }
4639  p->cTerm = c;
4640  if( p->z ) p->z[p->n] = 0;
4641  return p->z;
4642}
4643
4644/*
4645** Try to transfer data for table zTable.  If an error is seen while
4646** moving forward, try to go backwards.  The backwards movement won't
4647** work for WITHOUT ROWID tables.
4648*/
4649static void tryToCloneData(
4650  ShellState *p,
4651  sqlite3 *newDb,
4652  const char *zTable
4653){
4654  sqlite3_stmt *pQuery = 0;
4655  sqlite3_stmt *pInsert = 0;
4656  char *zQuery = 0;
4657  char *zInsert = 0;
4658  int rc;
4659  int i, j, n;
4660  int nTable = strlen30(zTable);
4661  int k = 0;
4662  int cnt = 0;
4663  const int spinRate = 10000;
4664
4665  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
4666  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4667  if( rc ){
4668    utf8_printf(stderr, "Error %d: %s on [%s]\n",
4669            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4670            zQuery);
4671    goto end_data_xfer;
4672  }
4673  n = sqlite3_column_count(pQuery);
4674  zInsert = sqlite3_malloc64(200 + nTable + n*3);
4675  if( zInsert==0 ) shell_out_of_memory();
4676  sqlite3_snprintf(200+nTable,zInsert,
4677                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
4678  i = strlen30(zInsert);
4679  for(j=1; j<n; j++){
4680    memcpy(zInsert+i, ",?", 2);
4681    i += 2;
4682  }
4683  memcpy(zInsert+i, ");", 3);
4684  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
4685  if( rc ){
4686    utf8_printf(stderr, "Error %d: %s on [%s]\n",
4687            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
4688            zQuery);
4689    goto end_data_xfer;
4690  }
4691  for(k=0; k<2; k++){
4692    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4693      for(i=0; i<n; i++){
4694        switch( sqlite3_column_type(pQuery, i) ){
4695          case SQLITE_NULL: {
4696            sqlite3_bind_null(pInsert, i+1);
4697            break;
4698          }
4699          case SQLITE_INTEGER: {
4700            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
4701            break;
4702          }
4703          case SQLITE_FLOAT: {
4704            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
4705            break;
4706          }
4707          case SQLITE_TEXT: {
4708            sqlite3_bind_text(pInsert, i+1,
4709                             (const char*)sqlite3_column_text(pQuery,i),
4710                             -1, SQLITE_STATIC);
4711            break;
4712          }
4713          case SQLITE_BLOB: {
4714            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
4715                                            sqlite3_column_bytes(pQuery,i),
4716                                            SQLITE_STATIC);
4717            break;
4718          }
4719        }
4720      } /* End for */
4721      rc = sqlite3_step(pInsert);
4722      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
4723        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
4724                        sqlite3_errmsg(newDb));
4725      }
4726      sqlite3_reset(pInsert);
4727      cnt++;
4728      if( (cnt%spinRate)==0 ){
4729        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
4730        fflush(stdout);
4731      }
4732    } /* End while */
4733    if( rc==SQLITE_DONE ) break;
4734    sqlite3_finalize(pQuery);
4735    sqlite3_free(zQuery);
4736    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
4737                             zTable);
4738    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4739    if( rc ){
4740      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
4741      break;
4742    }
4743  } /* End for(k=0...) */
4744
4745end_data_xfer:
4746  sqlite3_finalize(pQuery);
4747  sqlite3_finalize(pInsert);
4748  sqlite3_free(zQuery);
4749  sqlite3_free(zInsert);
4750}
4751
4752
4753/*
4754** Try to transfer all rows of the schema that match zWhere.  For
4755** each row, invoke xForEach() on the object defined by that row.
4756** If an error is encountered while moving forward through the
4757** sqlite_master table, try again moving backwards.
4758*/
4759static void tryToCloneSchema(
4760  ShellState *p,
4761  sqlite3 *newDb,
4762  const char *zWhere,
4763  void (*xForEach)(ShellState*,sqlite3*,const char*)
4764){
4765  sqlite3_stmt *pQuery = 0;
4766  char *zQuery = 0;
4767  int rc;
4768  const unsigned char *zName;
4769  const unsigned char *zSql;
4770  char *zErrMsg = 0;
4771
4772  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4773                           " WHERE %s", zWhere);
4774  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4775  if( rc ){
4776    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4777                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4778                    zQuery);
4779    goto end_schema_xfer;
4780  }
4781  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4782    zName = sqlite3_column_text(pQuery, 0);
4783    zSql = sqlite3_column_text(pQuery, 1);
4784    printf("%s... ", zName); fflush(stdout);
4785    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4786    if( zErrMsg ){
4787      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4788      sqlite3_free(zErrMsg);
4789      zErrMsg = 0;
4790    }
4791    if( xForEach ){
4792      xForEach(p, newDb, (const char*)zName);
4793    }
4794    printf("done\n");
4795  }
4796  if( rc!=SQLITE_DONE ){
4797    sqlite3_finalize(pQuery);
4798    sqlite3_free(zQuery);
4799    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4800                             " WHERE %s ORDER BY rowid DESC", zWhere);
4801    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4802    if( rc ){
4803      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4804                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4805                      zQuery);
4806      goto end_schema_xfer;
4807    }
4808    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4809      zName = sqlite3_column_text(pQuery, 0);
4810      zSql = sqlite3_column_text(pQuery, 1);
4811      printf("%s... ", zName); fflush(stdout);
4812      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4813      if( zErrMsg ){
4814        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4815        sqlite3_free(zErrMsg);
4816        zErrMsg = 0;
4817      }
4818      if( xForEach ){
4819        xForEach(p, newDb, (const char*)zName);
4820      }
4821      printf("done\n");
4822    }
4823  }
4824end_schema_xfer:
4825  sqlite3_finalize(pQuery);
4826  sqlite3_free(zQuery);
4827}
4828
4829/*
4830** Open a new database file named "zNewDb".  Try to recover as much information
4831** as possible out of the main database (which might be corrupt) and write it
4832** into zNewDb.
4833*/
4834static void tryToClone(ShellState *p, const char *zNewDb){
4835  int rc;
4836  sqlite3 *newDb = 0;
4837  if( access(zNewDb,0)==0 ){
4838    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
4839    return;
4840  }
4841  rc = sqlite3_open(zNewDb, &newDb);
4842  if( rc ){
4843    utf8_printf(stderr, "Cannot create output database: %s\n",
4844            sqlite3_errmsg(newDb));
4845  }else{
4846    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
4847    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
4848    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
4849    tryToCloneSchema(p, newDb, "type!='table'", 0);
4850    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
4851    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4852  }
4853  close_db(newDb);
4854}
4855
4856/*
4857** Change the output file back to stdout.
4858**
4859** If the p->doXdgOpen flag is set, that means the output was being
4860** redirected to a temporary file named by p->zTempFile.  In that case,
4861** launch start/open/xdg-open on that temporary file.
4862*/
4863static void output_reset(ShellState *p){
4864  if( p->outfile[0]=='|' ){
4865#ifndef SQLITE_OMIT_POPEN
4866    pclose(p->out);
4867#endif
4868  }else{
4869    output_file_close(p->out);
4870#ifndef SQLITE_NOHAVE_SYSTEM
4871    if( p->doXdgOpen ){
4872      const char *zXdgOpenCmd =
4873#if defined(_WIN32)
4874      "start";
4875#elif defined(__APPLE__)
4876      "open";
4877#else
4878      "xdg-open";
4879#endif
4880      char *zCmd;
4881      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
4882      if( system(zCmd) ){
4883        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
4884      }
4885      sqlite3_free(zCmd);
4886      outputModePop(p);
4887      p->doXdgOpen = 0;
4888      sqlite3_sleep(100);
4889    }
4890#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
4891  }
4892  p->outfile[0] = 0;
4893  p->out = stdout;
4894}
4895
4896/*
4897** Run an SQL command and return the single integer result.
4898*/
4899static int db_int(ShellState *p, const char *zSql){
4900  sqlite3_stmt *pStmt;
4901  int res = 0;
4902  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4903  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
4904    res = sqlite3_column_int(pStmt,0);
4905  }
4906  sqlite3_finalize(pStmt);
4907  return res;
4908}
4909
4910/*
4911** Convert a 2-byte or 4-byte big-endian integer into a native integer
4912*/
4913static unsigned int get2byteInt(unsigned char *a){
4914  return (a[0]<<8) + a[1];
4915}
4916static unsigned int get4byteInt(unsigned char *a){
4917  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
4918}
4919
4920/*
4921** Implementation of the ".info" command.
4922**
4923** Return 1 on error, 2 to exit, and 0 otherwise.
4924*/
4925static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
4926  static const struct { const char *zName; int ofst; } aField[] = {
4927     { "file change counter:",  24  },
4928     { "database page count:",  28  },
4929     { "freelist page count:",  36  },
4930     { "schema cookie:",        40  },
4931     { "schema format:",        44  },
4932     { "default cache size:",   48  },
4933     { "autovacuum top root:",  52  },
4934     { "incremental vacuum:",   64  },
4935     { "text encoding:",        56  },
4936     { "user version:",         60  },
4937     { "application id:",       68  },
4938     { "software version:",     96  },
4939  };
4940  static const struct { const char *zName; const char *zSql; } aQuery[] = {
4941     { "number of tables:",
4942       "SELECT count(*) FROM %s WHERE type='table'" },
4943     { "number of indexes:",
4944       "SELECT count(*) FROM %s WHERE type='index'" },
4945     { "number of triggers:",
4946       "SELECT count(*) FROM %s WHERE type='trigger'" },
4947     { "number of views:",
4948       "SELECT count(*) FROM %s WHERE type='view'" },
4949     { "schema size:",
4950       "SELECT total(length(sql)) FROM %s" },
4951  };
4952  int i, rc;
4953  unsigned iDataVersion;
4954  char *zSchemaTab;
4955  char *zDb = nArg>=2 ? azArg[1] : "main";
4956  sqlite3_stmt *pStmt = 0;
4957  unsigned char aHdr[100];
4958  open_db(p, 0);
4959  if( p->db==0 ) return 1;
4960  rc = sqlite3_prepare_v2(p->db,
4961             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
4962             -1, &pStmt, 0);
4963  if( rc ){
4964    if( !sqlite3_compileoption_used("ENABLE_DBPAGE_VTAB") ){
4965      utf8_printf(stderr, "the \".dbinfo\" command requires the "
4966                          "-DSQLITE_ENABLE_DBPAGE_VTAB compile-time options\n");
4967    }else{
4968      utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
4969    }
4970    sqlite3_finalize(pStmt);
4971    return 1;
4972  }
4973  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
4974  if( sqlite3_step(pStmt)==SQLITE_ROW
4975   && sqlite3_column_bytes(pStmt,0)>100
4976  ){
4977    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
4978    sqlite3_finalize(pStmt);
4979  }else{
4980    raw_printf(stderr, "unable to read database header\n");
4981    sqlite3_finalize(pStmt);
4982    return 1;
4983  }
4984  i = get2byteInt(aHdr+16);
4985  if( i==1 ) i = 65536;
4986  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
4987  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
4988  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
4989  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
4990  for(i=0; i<ArraySize(aField); i++){
4991    int ofst = aField[i].ofst;
4992    unsigned int val = get4byteInt(aHdr + ofst);
4993    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
4994    switch( ofst ){
4995      case 56: {
4996        if( val==1 ) raw_printf(p->out, " (utf8)");
4997        if( val==2 ) raw_printf(p->out, " (utf16le)");
4998        if( val==3 ) raw_printf(p->out, " (utf16be)");
4999      }
5000    }
5001    raw_printf(p->out, "\n");
5002  }
5003  if( zDb==0 ){
5004    zSchemaTab = sqlite3_mprintf("main.sqlite_master");
5005  }else if( strcmp(zDb,"temp")==0 ){
5006    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
5007  }else{
5008    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
5009  }
5010  for(i=0; i<ArraySize(aQuery); i++){
5011    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5012    int val = db_int(p, zSql);
5013    sqlite3_free(zSql);
5014    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5015  }
5016  sqlite3_free(zSchemaTab);
5017  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5018  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5019  return 0;
5020}
5021
5022/*
5023** Print the current sqlite3_errmsg() value to stderr and return 1.
5024*/
5025static int shellDatabaseError(sqlite3 *db){
5026  const char *zErr = sqlite3_errmsg(db);
5027  utf8_printf(stderr, "Error: %s\n", zErr);
5028  return 1;
5029}
5030
5031/*
5032** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5033** if they match and FALSE (0) if they do not match.
5034**
5035** Globbing rules:
5036**
5037**      '*'       Matches any sequence of zero or more characters.
5038**
5039**      '?'       Matches exactly one character.
5040**
5041**     [...]      Matches one character from the enclosed list of
5042**                characters.
5043**
5044**     [^...]     Matches one character not in the enclosed list.
5045**
5046**      '#'       Matches any sequence of one or more digits with an
5047**                optional + or - sign in front
5048**
5049**      ' '       Any span of whitespace matches any other span of
5050**                whitespace.
5051**
5052** Extra whitespace at the end of z[] is ignored.
5053*/
5054static int testcase_glob(const char *zGlob, const char *z){
5055  int c, c2;
5056  int invert;
5057  int seen;
5058
5059  while( (c = (*(zGlob++)))!=0 ){
5060    if( IsSpace(c) ){
5061      if( !IsSpace(*z) ) return 0;
5062      while( IsSpace(*zGlob) ) zGlob++;
5063      while( IsSpace(*z) ) z++;
5064    }else if( c=='*' ){
5065      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5066        if( c=='?' && (*(z++))==0 ) return 0;
5067      }
5068      if( c==0 ){
5069        return 1;
5070      }else if( c=='[' ){
5071        while( *z && testcase_glob(zGlob-1,z)==0 ){
5072          z++;
5073        }
5074        return (*z)!=0;
5075      }
5076      while( (c2 = (*(z++)))!=0 ){
5077        while( c2!=c ){
5078          c2 = *(z++);
5079          if( c2==0 ) return 0;
5080        }
5081        if( testcase_glob(zGlob,z) ) return 1;
5082      }
5083      return 0;
5084    }else if( c=='?' ){
5085      if( (*(z++))==0 ) return 0;
5086    }else if( c=='[' ){
5087      int prior_c = 0;
5088      seen = 0;
5089      invert = 0;
5090      c = *(z++);
5091      if( c==0 ) return 0;
5092      c2 = *(zGlob++);
5093      if( c2=='^' ){
5094        invert = 1;
5095        c2 = *(zGlob++);
5096      }
5097      if( c2==']' ){
5098        if( c==']' ) seen = 1;
5099        c2 = *(zGlob++);
5100      }
5101      while( c2 && c2!=']' ){
5102        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5103          c2 = *(zGlob++);
5104          if( c>=prior_c && c<=c2 ) seen = 1;
5105          prior_c = 0;
5106        }else{
5107          if( c==c2 ){
5108            seen = 1;
5109          }
5110          prior_c = c2;
5111        }
5112        c2 = *(zGlob++);
5113      }
5114      if( c2==0 || (seen ^ invert)==0 ) return 0;
5115    }else if( c=='#' ){
5116      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5117      if( !IsDigit(z[0]) ) return 0;
5118      z++;
5119      while( IsDigit(z[0]) ){ z++; }
5120    }else{
5121      if( c!=(*(z++)) ) return 0;
5122    }
5123  }
5124  while( IsSpace(*z) ){ z++; }
5125  return *z==0;
5126}
5127
5128
5129/*
5130** Compare the string as a command-line option with either one or two
5131** initial "-" characters.
5132*/
5133static int optionMatch(const char *zStr, const char *zOpt){
5134  if( zStr[0]!='-' ) return 0;
5135  zStr++;
5136  if( zStr[0]=='-' ) zStr++;
5137  return strcmp(zStr, zOpt)==0;
5138}
5139
5140/*
5141** Delete a file.
5142*/
5143int shellDeleteFile(const char *zFilename){
5144  int rc;
5145#ifdef _WIN32
5146  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5147  rc = _wunlink(z);
5148  sqlite3_free(z);
5149#else
5150  rc = unlink(zFilename);
5151#endif
5152  return rc;
5153}
5154
5155/*
5156** Try to delete the temporary file (if there is one) and free the
5157** memory used to hold the name of the temp file.
5158*/
5159static void clearTempFile(ShellState *p){
5160  if( p->zTempFile==0 ) return;
5161  if( p->doXdgOpen ) return;
5162  if( shellDeleteFile(p->zTempFile) ) return;
5163  sqlite3_free(p->zTempFile);
5164  p->zTempFile = 0;
5165}
5166
5167/*
5168** Create a new temp file name with the given suffix.
5169*/
5170static void newTempFile(ShellState *p, const char *zSuffix){
5171  clearTempFile(p);
5172  sqlite3_free(p->zTempFile);
5173  p->zTempFile = 0;
5174  if( p->db ){
5175    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5176  }
5177  if( p->zTempFile==0 ){
5178    sqlite3_uint64 r;
5179    sqlite3_randomness(sizeof(r), &r);
5180    p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
5181  }else{
5182    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5183  }
5184  if( p->zTempFile==0 ){
5185    raw_printf(stderr, "out of memory\n");
5186    exit(1);
5187  }
5188}
5189
5190
5191/*
5192** The implementation of SQL scalar function fkey_collate_clause(), used
5193** by the ".lint fkey-indexes" command. This scalar function is always
5194** called with four arguments - the parent table name, the parent column name,
5195** the child table name and the child column name.
5196**
5197**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5198**
5199** If either of the named tables or columns do not exist, this function
5200** returns an empty string. An empty string is also returned if both tables
5201** and columns exist but have the same default collation sequence. Or,
5202** if both exist but the default collation sequences are different, this
5203** function returns the string " COLLATE <parent-collation>", where
5204** <parent-collation> is the default collation sequence of the parent column.
5205*/
5206static void shellFkeyCollateClause(
5207  sqlite3_context *pCtx,
5208  int nVal,
5209  sqlite3_value **apVal
5210){
5211  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5212  const char *zParent;
5213  const char *zParentCol;
5214  const char *zParentSeq;
5215  const char *zChild;
5216  const char *zChildCol;
5217  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5218  int rc;
5219
5220  assert( nVal==4 );
5221  zParent = (const char*)sqlite3_value_text(apVal[0]);
5222  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5223  zChild = (const char*)sqlite3_value_text(apVal[2]);
5224  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5225
5226  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5227  rc = sqlite3_table_column_metadata(
5228      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5229  );
5230  if( rc==SQLITE_OK ){
5231    rc = sqlite3_table_column_metadata(
5232        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5233    );
5234  }
5235
5236  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5237    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5238    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5239    sqlite3_free(z);
5240  }
5241}
5242
5243
5244/*
5245** The implementation of dot-command ".lint fkey-indexes".
5246*/
5247static int lintFkeyIndexes(
5248  ShellState *pState,             /* Current shell tool state */
5249  char **azArg,                   /* Array of arguments passed to dot command */
5250  int nArg                        /* Number of entries in azArg[] */
5251){
5252  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5253  FILE *out = pState->out;        /* Stream to write non-error output to */
5254  int bVerbose = 0;               /* If -verbose is present */
5255  int bGroupByParent = 0;         /* If -groupbyparent is present */
5256  int i;                          /* To iterate through azArg[] */
5257  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5258  int rc;                         /* Return code */
5259  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5260
5261  /*
5262  ** This SELECT statement returns one row for each foreign key constraint
5263  ** in the schema of the main database. The column values are:
5264  **
5265  ** 0. The text of an SQL statement similar to:
5266  **
5267  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5268  **
5269  **    This SELECT is similar to the one that the foreign keys implementation
5270  **    needs to run internally on child tables. If there is an index that can
5271  **    be used to optimize this query, then it can also be used by the FK
5272  **    implementation to optimize DELETE or UPDATE statements on the parent
5273  **    table.
5274  **
5275  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5276  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5277  **    contains an index that can be used to optimize the query.
5278  **
5279  ** 2. Human readable text that describes the child table and columns. e.g.
5280  **
5281  **       "child_table(child_key1, child_key2)"
5282  **
5283  ** 3. Human readable text that describes the parent table and columns. e.g.
5284  **
5285  **       "parent_table(parent_key1, parent_key2)"
5286  **
5287  ** 4. A full CREATE INDEX statement for an index that could be used to
5288  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5289  **
5290  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5291  **
5292  ** 5. The name of the parent table.
5293  **
5294  ** These six values are used by the C logic below to generate the report.
5295  */
5296  const char *zSql =
5297  "SELECT "
5298    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5299    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5300    "  || fkey_collate_clause("
5301    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5302    ", "
5303    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5304    "  || group_concat('*=?', ' AND ') || ')'"
5305    ", "
5306    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5307    ", "
5308    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5309    ", "
5310    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5311    "  || ' ON ' || quote(s.name) || '('"
5312    "  || group_concat(quote(f.[from]) ||"
5313    "        fkey_collate_clause("
5314    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5315    "  || ');'"
5316    ", "
5317    "     f.[table] "
5318    "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
5319    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5320    "GROUP BY s.name, f.id "
5321    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5322  ;
5323  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5324
5325  for(i=2; i<nArg; i++){
5326    int n = strlen30(azArg[i]);
5327    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5328      bVerbose = 1;
5329    }
5330    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5331      bGroupByParent = 1;
5332      zIndent = "    ";
5333    }
5334    else{
5335      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5336          azArg[0], azArg[1]
5337      );
5338      return SQLITE_ERROR;
5339    }
5340  }
5341
5342  /* Register the fkey_collate_clause() SQL function */
5343  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5344      0, shellFkeyCollateClause, 0, 0
5345  );
5346
5347
5348  if( rc==SQLITE_OK ){
5349    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5350  }
5351  if( rc==SQLITE_OK ){
5352    sqlite3_bind_int(pSql, 1, bGroupByParent);
5353  }
5354
5355  if( rc==SQLITE_OK ){
5356    int rc2;
5357    char *zPrev = 0;
5358    while( SQLITE_ROW==sqlite3_step(pSql) ){
5359      int res = -1;
5360      sqlite3_stmt *pExplain = 0;
5361      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5362      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5363      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5364      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5365      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5366      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5367
5368      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5369      if( rc!=SQLITE_OK ) break;
5370      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5371        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5372        res = (
5373              0==sqlite3_strglob(zGlob, zPlan)
5374           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5375        );
5376      }
5377      rc = sqlite3_finalize(pExplain);
5378      if( rc!=SQLITE_OK ) break;
5379
5380      if( res<0 ){
5381        raw_printf(stderr, "Error: internal error");
5382        break;
5383      }else{
5384        if( bGroupByParent
5385        && (bVerbose || res==0)
5386        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5387        ){
5388          raw_printf(out, "-- Parent table %s\n", zParent);
5389          sqlite3_free(zPrev);
5390          zPrev = sqlite3_mprintf("%s", zParent);
5391        }
5392
5393        if( res==0 ){
5394          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5395        }else if( bVerbose ){
5396          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5397              zIndent, zFrom, zTarget
5398          );
5399        }
5400      }
5401    }
5402    sqlite3_free(zPrev);
5403
5404    if( rc!=SQLITE_OK ){
5405      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5406    }
5407
5408    rc2 = sqlite3_finalize(pSql);
5409    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5410      rc = rc2;
5411      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5412    }
5413  }else{
5414    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5415  }
5416
5417  return rc;
5418}
5419
5420/*
5421** Implementation of ".lint" dot command.
5422*/
5423static int lintDotCommand(
5424  ShellState *pState,             /* Current shell tool state */
5425  char **azArg,                   /* Array of arguments passed to dot command */
5426  int nArg                        /* Number of entries in azArg[] */
5427){
5428  int n;
5429  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5430  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5431  return lintFkeyIndexes(pState, azArg, nArg);
5432
5433 usage:
5434  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5435  raw_printf(stderr, "Where sub-commands are:\n");
5436  raw_printf(stderr, "    fkey-indexes\n");
5437  return SQLITE_ERROR;
5438}
5439
5440#if !defined SQLITE_OMIT_VIRTUALTABLE
5441static void shellPrepare(
5442  sqlite3 *db,
5443  int *pRc,
5444  const char *zSql,
5445  sqlite3_stmt **ppStmt
5446){
5447  *ppStmt = 0;
5448  if( *pRc==SQLITE_OK ){
5449    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5450    if( rc!=SQLITE_OK ){
5451      raw_printf(stderr, "sql error: %s (%d)\n",
5452          sqlite3_errmsg(db), sqlite3_errcode(db)
5453      );
5454      *pRc = rc;
5455    }
5456  }
5457}
5458
5459/*
5460** Create a prepared statement using printf-style arguments for the SQL.
5461**
5462** This routine is could be marked "static".  But it is not always used,
5463** depending on compile-time options.  By omitting the "static", we avoid
5464** nuisance compiler warnings about "defined but not used".
5465*/
5466void shellPreparePrintf(
5467  sqlite3 *db,
5468  int *pRc,
5469  sqlite3_stmt **ppStmt,
5470  const char *zFmt,
5471  ...
5472){
5473  *ppStmt = 0;
5474  if( *pRc==SQLITE_OK ){
5475    va_list ap;
5476    char *z;
5477    va_start(ap, zFmt);
5478    z = sqlite3_vmprintf(zFmt, ap);
5479    va_end(ap);
5480    if( z==0 ){
5481      *pRc = SQLITE_NOMEM;
5482    }else{
5483      shellPrepare(db, pRc, z, ppStmt);
5484      sqlite3_free(z);
5485    }
5486  }
5487}
5488
5489/* Finalize the prepared statement created using shellPreparePrintf().
5490**
5491** This routine is could be marked "static".  But it is not always used,
5492** depending on compile-time options.  By omitting the "static", we avoid
5493** nuisance compiler warnings about "defined but not used".
5494*/
5495void shellFinalize(
5496  int *pRc,
5497  sqlite3_stmt *pStmt
5498){
5499  if( pStmt ){
5500    sqlite3 *db = sqlite3_db_handle(pStmt);
5501    int rc = sqlite3_finalize(pStmt);
5502    if( *pRc==SQLITE_OK ){
5503      if( rc!=SQLITE_OK ){
5504        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5505      }
5506      *pRc = rc;
5507    }
5508  }
5509}
5510
5511/* Reset the prepared statement created using shellPreparePrintf().
5512**
5513** This routine is could be marked "static".  But it is not always used,
5514** depending on compile-time options.  By omitting the "static", we avoid
5515** nuisance compiler warnings about "defined but not used".
5516*/
5517void shellReset(
5518  int *pRc,
5519  sqlite3_stmt *pStmt
5520){
5521  int rc = sqlite3_reset(pStmt);
5522  if( *pRc==SQLITE_OK ){
5523    if( rc!=SQLITE_OK ){
5524      sqlite3 *db = sqlite3_db_handle(pStmt);
5525      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5526    }
5527    *pRc = rc;
5528  }
5529}
5530#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
5531
5532#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5533/******************************************************************************
5534** The ".archive" or ".ar" command.
5535*/
5536/*
5537** Structure representing a single ".ar" command.
5538*/
5539typedef struct ArCommand ArCommand;
5540struct ArCommand {
5541  u8 eCmd;                        /* An AR_CMD_* value */
5542  u8 bVerbose;                    /* True if --verbose */
5543  u8 bZip;                        /* True if the archive is a ZIP */
5544  u8 bDryRun;                     /* True if --dry-run */
5545  u8 bAppend;                     /* True if --append */
5546  u8 fromCmdLine;                 /* Run from -A instead of .archive */
5547  int nArg;                       /* Number of command arguments */
5548  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
5549  const char *zFile;              /* --file argument, or NULL */
5550  const char *zDir;               /* --directory argument, or NULL */
5551  char **azArg;                   /* Array of command arguments */
5552  ShellState *p;                  /* Shell state */
5553  sqlite3 *db;                    /* Database containing the archive */
5554};
5555
5556/*
5557** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
5558*/
5559static int arUsage(FILE *f){
5560  showHelp(f,"archive");
5561  return SQLITE_ERROR;
5562}
5563
5564/*
5565** Print an error message for the .ar command to stderr and return
5566** SQLITE_ERROR.
5567*/
5568static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
5569  va_list ap;
5570  char *z;
5571  va_start(ap, zFmt);
5572  z = sqlite3_vmprintf(zFmt, ap);
5573  va_end(ap);
5574  utf8_printf(stderr, "Error: %s\n", z);
5575  if( pAr->fromCmdLine ){
5576    utf8_printf(stderr, "Use \"-A\" for more help\n");
5577  }else{
5578    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
5579  }
5580  sqlite3_free(z);
5581  return SQLITE_ERROR;
5582}
5583
5584/*
5585** Values for ArCommand.eCmd.
5586*/
5587#define AR_CMD_CREATE       1
5588#define AR_CMD_UPDATE       2
5589#define AR_CMD_INSERT       3
5590#define AR_CMD_EXTRACT      4
5591#define AR_CMD_LIST         5
5592#define AR_CMD_HELP         6
5593
5594/*
5595** Other (non-command) switches.
5596*/
5597#define AR_SWITCH_VERBOSE     7
5598#define AR_SWITCH_FILE        8
5599#define AR_SWITCH_DIRECTORY   9
5600#define AR_SWITCH_APPEND     10
5601#define AR_SWITCH_DRYRUN     11
5602
5603static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
5604  switch( eSwitch ){
5605    case AR_CMD_CREATE:
5606    case AR_CMD_EXTRACT:
5607    case AR_CMD_LIST:
5608    case AR_CMD_UPDATE:
5609    case AR_CMD_INSERT:
5610    case AR_CMD_HELP:
5611      if( pAr->eCmd ){
5612        return arErrorMsg(pAr, "multiple command options");
5613      }
5614      pAr->eCmd = eSwitch;
5615      break;
5616
5617    case AR_SWITCH_DRYRUN:
5618      pAr->bDryRun = 1;
5619      break;
5620    case AR_SWITCH_VERBOSE:
5621      pAr->bVerbose = 1;
5622      break;
5623    case AR_SWITCH_APPEND:
5624      pAr->bAppend = 1;
5625      /* Fall thru into --file */
5626    case AR_SWITCH_FILE:
5627      pAr->zFile = zArg;
5628      break;
5629    case AR_SWITCH_DIRECTORY:
5630      pAr->zDir = zArg;
5631      break;
5632  }
5633
5634  return SQLITE_OK;
5635}
5636
5637/*
5638** Parse the command line for an ".ar" command. The results are written into
5639** structure (*pAr). SQLITE_OK is returned if the command line is parsed
5640** successfully, otherwise an error message is written to stderr and
5641** SQLITE_ERROR returned.
5642*/
5643static int arParseCommand(
5644  char **azArg,                   /* Array of arguments passed to dot command */
5645  int nArg,                       /* Number of entries in azArg[] */
5646  ArCommand *pAr                  /* Populate this object */
5647){
5648  struct ArSwitch {
5649    const char *zLong;
5650    char cShort;
5651    u8 eSwitch;
5652    u8 bArg;
5653  } aSwitch[] = {
5654    { "create",    'c', AR_CMD_CREATE,       0 },
5655    { "extract",   'x', AR_CMD_EXTRACT,      0 },
5656    { "insert",    'i', AR_CMD_INSERT,       0 },
5657    { "list",      't', AR_CMD_LIST,         0 },
5658    { "update",    'u', AR_CMD_UPDATE,       0 },
5659    { "help",      'h', AR_CMD_HELP,         0 },
5660    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
5661    { "file",      'f', AR_SWITCH_FILE,      1 },
5662    { "append",    'a', AR_SWITCH_APPEND,    1 },
5663    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
5664    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
5665  };
5666  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
5667  struct ArSwitch *pEnd = &aSwitch[nSwitch];
5668
5669  if( nArg<=1 ){
5670    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
5671    return arUsage(stderr);
5672  }else{
5673    char *z = azArg[1];
5674    if( z[0]!='-' ){
5675      /* Traditional style [tar] invocation */
5676      int i;
5677      int iArg = 2;
5678      for(i=0; z[i]; i++){
5679        const char *zArg = 0;
5680        struct ArSwitch *pOpt;
5681        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5682          if( z[i]==pOpt->cShort ) break;
5683        }
5684        if( pOpt==pEnd ){
5685          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
5686        }
5687        if( pOpt->bArg ){
5688          if( iArg>=nArg ){
5689            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
5690          }
5691          zArg = azArg[iArg++];
5692        }
5693        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
5694      }
5695      pAr->nArg = nArg-iArg;
5696      if( pAr->nArg>0 ){
5697        pAr->azArg = &azArg[iArg];
5698      }
5699    }else{
5700      /* Non-traditional invocation */
5701      int iArg;
5702      for(iArg=1; iArg<nArg; iArg++){
5703        int n;
5704        z = azArg[iArg];
5705        if( z[0]!='-' ){
5706          /* All remaining command line words are command arguments. */
5707          pAr->azArg = &azArg[iArg];
5708          pAr->nArg = nArg-iArg;
5709          break;
5710        }
5711        n = strlen30(z);
5712
5713        if( z[1]!='-' ){
5714          int i;
5715          /* One or more short options */
5716          for(i=1; i<n; i++){
5717            const char *zArg = 0;
5718            struct ArSwitch *pOpt;
5719            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5720              if( z[i]==pOpt->cShort ) break;
5721            }
5722            if( pOpt==pEnd ){
5723              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
5724            }
5725            if( pOpt->bArg ){
5726              if( i<(n-1) ){
5727                zArg = &z[i+1];
5728                i = n;
5729              }else{
5730                if( iArg>=(nArg-1) ){
5731                  return arErrorMsg(pAr, "option requires an argument: %c",
5732                                    z[i]);
5733                }
5734                zArg = azArg[++iArg];
5735              }
5736            }
5737            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
5738          }
5739        }else if( z[2]=='\0' ){
5740          /* A -- option, indicating that all remaining command line words
5741          ** are command arguments.  */
5742          pAr->azArg = &azArg[iArg+1];
5743          pAr->nArg = nArg-iArg-1;
5744          break;
5745        }else{
5746          /* A long option */
5747          const char *zArg = 0;             /* Argument for option, if any */
5748          struct ArSwitch *pMatch = 0;      /* Matching option */
5749          struct ArSwitch *pOpt;            /* Iterator */
5750          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5751            const char *zLong = pOpt->zLong;
5752            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
5753              if( pMatch ){
5754                return arErrorMsg(pAr, "ambiguous option: %s",z);
5755              }else{
5756                pMatch = pOpt;
5757              }
5758            }
5759          }
5760
5761          if( pMatch==0 ){
5762            return arErrorMsg(pAr, "unrecognized option: %s", z);
5763          }
5764          if( pMatch->bArg ){
5765            if( iArg>=(nArg-1) ){
5766              return arErrorMsg(pAr, "option requires an argument: %s", z);
5767            }
5768            zArg = azArg[++iArg];
5769          }
5770          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
5771        }
5772      }
5773    }
5774  }
5775
5776  return SQLITE_OK;
5777}
5778
5779/*
5780** This function assumes that all arguments within the ArCommand.azArg[]
5781** array refer to archive members, as for the --extract or --list commands.
5782** It checks that each of them are present. If any specified file is not
5783** present in the archive, an error is printed to stderr and an error
5784** code returned. Otherwise, if all specified arguments are present in
5785** the archive, SQLITE_OK is returned.
5786**
5787** This function strips any trailing '/' characters from each argument.
5788** This is consistent with the way the [tar] command seems to work on
5789** Linux.
5790*/
5791static int arCheckEntries(ArCommand *pAr){
5792  int rc = SQLITE_OK;
5793  if( pAr->nArg ){
5794    int i, j;
5795    sqlite3_stmt *pTest = 0;
5796
5797    shellPreparePrintf(pAr->db, &rc, &pTest,
5798        "SELECT name FROM %s WHERE name=$name",
5799        pAr->zSrcTable
5800    );
5801    j = sqlite3_bind_parameter_index(pTest, "$name");
5802    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
5803      char *z = pAr->azArg[i];
5804      int n = strlen30(z);
5805      int bOk = 0;
5806      while( n>0 && z[n-1]=='/' ) n--;
5807      z[n] = '\0';
5808      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
5809      if( SQLITE_ROW==sqlite3_step(pTest) ){
5810        bOk = 1;
5811      }
5812      shellReset(&rc, pTest);
5813      if( rc==SQLITE_OK && bOk==0 ){
5814        utf8_printf(stderr, "not found in archive: %s\n", z);
5815        rc = SQLITE_ERROR;
5816      }
5817    }
5818    shellFinalize(&rc, pTest);
5819  }
5820  return rc;
5821}
5822
5823/*
5824** Format a WHERE clause that can be used against the "sqlar" table to
5825** identify all archive members that match the command arguments held
5826** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
5827** The caller is responsible for eventually calling sqlite3_free() on
5828** any non-NULL (*pzWhere) value.
5829*/
5830static void arWhereClause(
5831  int *pRc,
5832  ArCommand *pAr,
5833  char **pzWhere                  /* OUT: New WHERE clause */
5834){
5835  char *zWhere = 0;
5836  if( *pRc==SQLITE_OK ){
5837    if( pAr->nArg==0 ){
5838      zWhere = sqlite3_mprintf("1");
5839    }else{
5840      int i;
5841      const char *zSep = "";
5842      for(i=0; i<pAr->nArg; i++){
5843        const char *z = pAr->azArg[i];
5844        zWhere = sqlite3_mprintf(
5845          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
5846          zWhere, zSep, z, strlen30(z)+1, z
5847        );
5848        if( zWhere==0 ){
5849          *pRc = SQLITE_NOMEM;
5850          break;
5851        }
5852        zSep = " OR ";
5853      }
5854    }
5855  }
5856  *pzWhere = zWhere;
5857}
5858
5859/*
5860** Implementation of .ar "lisT" command.
5861*/
5862static int arListCommand(ArCommand *pAr){
5863  const char *zSql = "SELECT %s FROM %s WHERE %s";
5864  const char *azCols[] = {
5865    "name",
5866    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
5867  };
5868
5869  char *zWhere = 0;
5870  sqlite3_stmt *pSql = 0;
5871  int rc;
5872
5873  rc = arCheckEntries(pAr);
5874  arWhereClause(&rc, pAr, &zWhere);
5875
5876  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
5877                     pAr->zSrcTable, zWhere);
5878  if( pAr->bDryRun ){
5879    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
5880  }else{
5881    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
5882      if( pAr->bVerbose ){
5883        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
5884            sqlite3_column_text(pSql, 0),
5885            sqlite3_column_int(pSql, 1),
5886            sqlite3_column_text(pSql, 2),
5887            sqlite3_column_text(pSql, 3)
5888        );
5889      }else{
5890        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
5891      }
5892    }
5893  }
5894  shellFinalize(&rc, pSql);
5895  sqlite3_free(zWhere);
5896  return rc;
5897}
5898
5899
5900/*
5901** Implementation of .ar "eXtract" command.
5902*/
5903static int arExtractCommand(ArCommand *pAr){
5904  const char *zSql1 =
5905    "SELECT "
5906    " ($dir || name),"
5907    " writefile(($dir || name), %s, mode, mtime) "
5908    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
5909    " AND name NOT GLOB '*..[/\\]*'";
5910
5911  const char *azExtraArg[] = {
5912    "sqlar_uncompress(data, sz)",
5913    "data"
5914  };
5915
5916  sqlite3_stmt *pSql = 0;
5917  int rc = SQLITE_OK;
5918  char *zDir = 0;
5919  char *zWhere = 0;
5920  int i, j;
5921
5922  /* If arguments are specified, check that they actually exist within
5923  ** the archive before proceeding. And formulate a WHERE clause to
5924  ** match them.  */
5925  rc = arCheckEntries(pAr);
5926  arWhereClause(&rc, pAr, &zWhere);
5927
5928  if( rc==SQLITE_OK ){
5929    if( pAr->zDir ){
5930      zDir = sqlite3_mprintf("%s/", pAr->zDir);
5931    }else{
5932      zDir = sqlite3_mprintf("");
5933    }
5934    if( zDir==0 ) rc = SQLITE_NOMEM;
5935  }
5936
5937  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
5938      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
5939  );
5940
5941  if( rc==SQLITE_OK ){
5942    j = sqlite3_bind_parameter_index(pSql, "$dir");
5943    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
5944
5945    /* Run the SELECT statement twice. The first time, writefile() is called
5946    ** for all archive members that should be extracted. The second time,
5947    ** only for the directories. This is because the timestamps for
5948    ** extracted directories must be reset after they are populated (as
5949    ** populating them changes the timestamp).  */
5950    for(i=0; i<2; i++){
5951      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
5952      sqlite3_bind_int(pSql, j, i);
5953      if( pAr->bDryRun ){
5954        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
5955      }else{
5956        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
5957          if( i==0 && pAr->bVerbose ){
5958            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
5959          }
5960        }
5961      }
5962      shellReset(&rc, pSql);
5963    }
5964    shellFinalize(&rc, pSql);
5965  }
5966
5967  sqlite3_free(zDir);
5968  sqlite3_free(zWhere);
5969  return rc;
5970}
5971
5972/*
5973** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
5974*/
5975static int arExecSql(ArCommand *pAr, const char *zSql){
5976  int rc;
5977  if( pAr->bDryRun ){
5978    utf8_printf(pAr->p->out, "%s\n", zSql);
5979    rc = SQLITE_OK;
5980  }else{
5981    char *zErr = 0;
5982    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
5983    if( zErr ){
5984      utf8_printf(stdout, "ERROR: %s\n", zErr);
5985      sqlite3_free(zErr);
5986    }
5987  }
5988  return rc;
5989}
5990
5991
5992/*
5993** Implementation of .ar "create", "insert", and "update" commands.
5994**
5995**     create    ->     Create a new SQL archive
5996**     insert    ->     Insert or reinsert all files listed
5997**     update    ->     Insert files that have changed or that were not
5998**                      previously in the archive
5999**
6000** Create the "sqlar" table in the database if it does not already exist.
6001** Then add each file in the azFile[] array to the archive. Directories
6002** are added recursively. If argument bVerbose is non-zero, a message is
6003** printed on stdout for each file archived.
6004**
6005** The create command is the same as update, except that it drops
6006** any existing "sqlar" table before beginning.  The "insert" command
6007** always overwrites every file named on the command-line, where as
6008** "update" only overwrites if the size or mtime or mode has changed.
6009*/
6010static int arCreateOrUpdateCommand(
6011  ArCommand *pAr,                 /* Command arguments and options */
6012  int bUpdate,                    /* true for a --create. */
6013  int bOnlyIfChanged              /* Only update if file has changed */
6014){
6015  const char *zCreate =
6016      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6017      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6018      "  mode INT,               -- access permissions\n"
6019      "  mtime INT,              -- last modification time\n"
6020      "  sz INT,                 -- original file size\n"
6021      "  data BLOB               -- compressed content\n"
6022      ")";
6023  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6024  const char *zInsertFmt[2] = {
6025     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6026     "  SELECT\n"
6027     "    %s,\n"
6028     "    mode,\n"
6029     "    mtime,\n"
6030     "    CASE substr(lsmode(mode),1,1)\n"
6031     "      WHEN '-' THEN length(data)\n"
6032     "      WHEN 'd' THEN 0\n"
6033     "      ELSE -1 END,\n"
6034     "    sqlar_compress(data)\n"
6035     "  FROM fsdir(%Q,%Q) AS disk\n"
6036     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6037     ,
6038     "REPLACE INTO %s(name,mode,mtime,data)\n"
6039     "  SELECT\n"
6040     "    %s,\n"
6041     "    mode,\n"
6042     "    mtime,\n"
6043     "    data\n"
6044     "  FROM fsdir(%Q,%Q) AS disk\n"
6045     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6046  };
6047  int i;                          /* For iterating through azFile[] */
6048  int rc;                         /* Return code */
6049  const char *zTab = 0;           /* SQL table into which to insert */
6050  char *zSql;
6051  char zTemp[50];
6052  char *zExists = 0;
6053
6054  arExecSql(pAr, "PRAGMA page_size=512");
6055  rc = arExecSql(pAr, "SAVEPOINT ar;");
6056  if( rc!=SQLITE_OK ) return rc;
6057  zTemp[0] = 0;
6058  if( pAr->bZip ){
6059    /* Initialize the zipfile virtual table, if necessary */
6060    if( pAr->zFile ){
6061      sqlite3_uint64 r;
6062      sqlite3_randomness(sizeof(r),&r);
6063      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6064      zTab = zTemp;
6065      zSql = sqlite3_mprintf(
6066         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6067         zTab, pAr->zFile
6068      );
6069      rc = arExecSql(pAr, zSql);
6070      sqlite3_free(zSql);
6071    }else{
6072      zTab = "zip";
6073    }
6074  }else{
6075    /* Initialize the table for an SQLAR */
6076    zTab = "sqlar";
6077    if( bUpdate==0 ){
6078      rc = arExecSql(pAr, zDrop);
6079      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6080    }
6081    rc = arExecSql(pAr, zCreate);
6082  }
6083  if( bOnlyIfChanged ){
6084    zExists = sqlite3_mprintf(
6085      " AND NOT EXISTS("
6086          "SELECT 1 FROM %s AS mem"
6087          " WHERE mem.name=disk.name"
6088          " AND mem.mtime=disk.mtime"
6089          " AND mem.mode=disk.mode)", zTab);
6090  }else{
6091    zExists = sqlite3_mprintf("");
6092  }
6093  if( zExists==0 ) rc = SQLITE_NOMEM;
6094  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6095    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6096        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6097        pAr->azArg[i], pAr->zDir, zExists);
6098    rc = arExecSql(pAr, zSql2);
6099    sqlite3_free(zSql2);
6100  }
6101end_ar_transaction:
6102  if( rc!=SQLITE_OK ){
6103    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6104  }else{
6105    rc = arExecSql(pAr, "RELEASE ar;");
6106    if( pAr->bZip && pAr->zFile ){
6107      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6108      arExecSql(pAr, zSql);
6109      sqlite3_free(zSql);
6110    }
6111  }
6112  sqlite3_free(zExists);
6113  return rc;
6114}
6115
6116/*
6117** Implementation of ".ar" dot command.
6118*/
6119static int arDotCommand(
6120  ShellState *pState,          /* Current shell tool state */
6121  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6122  char **azArg,                /* Array of arguments passed to dot command */
6123  int nArg                     /* Number of entries in azArg[] */
6124){
6125  ArCommand cmd;
6126  int rc;
6127  memset(&cmd, 0, sizeof(cmd));
6128  cmd.fromCmdLine = fromCmdLine;
6129  rc = arParseCommand(azArg, nArg, &cmd);
6130  if( rc==SQLITE_OK ){
6131    int eDbType = SHELL_OPEN_UNSPEC;
6132    cmd.p = pState;
6133    cmd.db = pState->db;
6134    if( cmd.zFile ){
6135      eDbType = deduceDatabaseType(cmd.zFile, 1);
6136    }else{
6137      eDbType = pState->openMode;
6138    }
6139    if( eDbType==SHELL_OPEN_ZIPFILE ){
6140      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6141        if( cmd.zFile==0 ){
6142          cmd.zSrcTable = sqlite3_mprintf("zip");
6143        }else{
6144          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6145        }
6146      }
6147      cmd.bZip = 1;
6148    }else if( cmd.zFile ){
6149      int flags;
6150      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6151      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6152           || cmd.eCmd==AR_CMD_UPDATE ){
6153        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6154      }else{
6155        flags = SQLITE_OPEN_READONLY;
6156      }
6157      cmd.db = 0;
6158      if( cmd.bDryRun ){
6159        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6160             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6161      }
6162      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6163             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6164      if( rc!=SQLITE_OK ){
6165        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6166            cmd.zFile, sqlite3_errmsg(cmd.db)
6167        );
6168        goto end_ar_command;
6169      }
6170      sqlite3_fileio_init(cmd.db, 0, 0);
6171      sqlite3_sqlar_init(cmd.db, 0, 0);
6172      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6173                              shellPutsFunc, 0, 0);
6174
6175    }
6176    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6177      if( cmd.eCmd!=AR_CMD_CREATE
6178       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6179      ){
6180        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6181        rc = SQLITE_ERROR;
6182        goto end_ar_command;
6183      }
6184      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6185    }
6186
6187    switch( cmd.eCmd ){
6188      case AR_CMD_CREATE:
6189        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6190        break;
6191
6192      case AR_CMD_EXTRACT:
6193        rc = arExtractCommand(&cmd);
6194        break;
6195
6196      case AR_CMD_LIST:
6197        rc = arListCommand(&cmd);
6198        break;
6199
6200      case AR_CMD_HELP:
6201        arUsage(pState->out);
6202        break;
6203
6204      case AR_CMD_INSERT:
6205        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6206        break;
6207
6208      default:
6209        assert( cmd.eCmd==AR_CMD_UPDATE );
6210        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6211        break;
6212    }
6213  }
6214end_ar_command:
6215  if( cmd.db!=pState->db ){
6216    close_db(cmd.db);
6217  }
6218  sqlite3_free(cmd.zSrcTable);
6219
6220  return rc;
6221}
6222/* End of the ".archive" or ".ar" command logic
6223*******************************************************************************/
6224#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6225
6226#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6227/*
6228** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6229** Otherwise, the SQL statement or statements in zSql are executed using
6230** database connection db and the error code written to *pRc before
6231** this function returns.
6232*/
6233static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6234  int rc = *pRc;
6235  if( rc==SQLITE_OK ){
6236    char *zErr = 0;
6237    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6238    if( rc!=SQLITE_OK ){
6239      raw_printf(stderr, "SQL error: %s\n", zErr);
6240    }
6241    *pRc = rc;
6242  }
6243}
6244
6245/*
6246** Like shellExec(), except that zFmt is a printf() style format string.
6247*/
6248static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6249  char *z = 0;
6250  if( *pRc==SQLITE_OK ){
6251    va_list ap;
6252    va_start(ap, zFmt);
6253    z = sqlite3_vmprintf(zFmt, ap);
6254    va_end(ap);
6255    if( z==0 ){
6256      *pRc = SQLITE_NOMEM;
6257    }else{
6258      shellExec(db, pRc, z);
6259    }
6260    sqlite3_free(z);
6261  }
6262}
6263
6264/*
6265** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6266** Otherwise, an attempt is made to allocate, zero and return a pointer
6267** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6268** to SQLITE_NOMEM and NULL returned.
6269*/
6270static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6271  void *pRet = 0;
6272  if( *pRc==SQLITE_OK ){
6273    pRet = sqlite3_malloc64(nByte);
6274    if( pRet==0 ){
6275      *pRc = SQLITE_NOMEM;
6276    }else{
6277      memset(pRet, 0, nByte);
6278    }
6279  }
6280  return pRet;
6281}
6282
6283/*
6284** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6285** Otherwise, zFmt is treated as a printf() style string. The result of
6286** formatting it along with any trailing arguments is written into a
6287** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6288** It is the responsibility of the caller to eventually free this buffer
6289** using a call to sqlite3_free().
6290**
6291** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6292** pointer returned.
6293*/
6294static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6295  char *z = 0;
6296  if( *pRc==SQLITE_OK ){
6297    va_list ap;
6298    va_start(ap, zFmt);
6299    z = sqlite3_vmprintf(zFmt, ap);
6300    va_end(ap);
6301    if( z==0 ){
6302      *pRc = SQLITE_NOMEM;
6303    }
6304  }
6305  return z;
6306}
6307
6308/*
6309** When running the ".recover" command, each output table, and the special
6310** orphaned row table if it is required, is represented by an instance
6311** of the following struct.
6312*/
6313typedef struct RecoverTable RecoverTable;
6314struct RecoverTable {
6315  char *zQuoted;                  /* Quoted version of table name */
6316  int nCol;                       /* Number of columns in table */
6317  char **azlCol;                  /* Array of column lists */
6318  int iPk;                        /* Index of IPK column */
6319};
6320
6321/*
6322** Free a RecoverTable object allocated by recoverFindTable() or
6323** recoverOrphanTable().
6324*/
6325static void recoverFreeTable(RecoverTable *pTab){
6326  if( pTab ){
6327    sqlite3_free(pTab->zQuoted);
6328    if( pTab->azlCol ){
6329      int i;
6330      for(i=0; i<=pTab->nCol; i++){
6331        sqlite3_free(pTab->azlCol[i]);
6332      }
6333      sqlite3_free(pTab->azlCol);
6334    }
6335    sqlite3_free(pTab);
6336  }
6337}
6338
6339/*
6340** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6341** Otherwise, it allocates and returns a RecoverTable object based on the
6342** final four arguments passed to this function. It is the responsibility
6343** of the caller to eventually free the returned object using
6344** recoverFreeTable().
6345*/
6346static RecoverTable *recoverNewTable(
6347  int *pRc,                       /* IN/OUT: Error code */
6348  const char *zName,              /* Name of table */
6349  const char *zSql,               /* CREATE TABLE statement */
6350  int bIntkey,
6351  int nCol
6352){
6353  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6354  int rc = *pRc;
6355  RecoverTable *pTab = 0;
6356
6357  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6358  if( rc==SQLITE_OK ){
6359    int nSqlCol = 0;
6360    int bSqlIntkey = 0;
6361    sqlite3_stmt *pStmt = 0;
6362
6363    rc = sqlite3_open("", &dbtmp);
6364    if( rc==SQLITE_OK ){
6365      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6366                              shellIdQuote, 0, 0);
6367    }
6368    if( rc==SQLITE_OK ){
6369      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6370    }
6371    if( rc==SQLITE_OK ){
6372      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6373      if( rc==SQLITE_ERROR ){
6374        rc = SQLITE_OK;
6375        goto finished;
6376      }
6377    }
6378    shellPreparePrintf(dbtmp, &rc, &pStmt,
6379        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6380    );
6381    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6382      nSqlCol = sqlite3_column_int(pStmt, 0);
6383    }
6384    shellFinalize(&rc, pStmt);
6385
6386    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6387      goto finished;
6388    }
6389
6390    shellPreparePrintf(dbtmp, &rc, &pStmt,
6391      "SELECT ("
6392      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6393      ") FROM sqlite_master WHERE name = %Q", zName
6394    );
6395    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6396      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6397    }
6398    shellFinalize(&rc, pStmt);
6399
6400    if( bIntkey==bSqlIntkey ){
6401      int i;
6402      const char *zPk = "_rowid_";
6403      sqlite3_stmt *pPkFinder = 0;
6404
6405      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6406      ** set zPk to the name of the PK column, and pTab->iPk to the index
6407      ** of the column, where columns are 0-numbered from left to right.
6408      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6409      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6410      pTab->iPk = -2;
6411      if( bIntkey ){
6412        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6413          "SELECT cid, name FROM pragma_table_info(%Q) "
6414          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6415          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6416          , zName, zName
6417        );
6418        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6419          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6420          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6421        }
6422      }
6423
6424      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6425      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6426      pTab->nCol = nSqlCol;
6427
6428      if( bIntkey ){
6429        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6430      }else{
6431        pTab->azlCol[0] = shellMPrintf(&rc, "");
6432      }
6433      i = 1;
6434      shellPreparePrintf(dbtmp, &rc, &pStmt,
6435          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6436          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6437          "FROM pragma_table_info(%Q)",
6438          bIntkey ? ", " : "", pTab->iPk,
6439          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6440          zName
6441      );
6442      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6443        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6444        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6445        i++;
6446      }
6447      shellFinalize(&rc, pStmt);
6448
6449      shellFinalize(&rc, pPkFinder);
6450    }
6451  }
6452
6453 finished:
6454  sqlite3_close(dbtmp);
6455  *pRc = rc;
6456  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6457    recoverFreeTable(pTab);
6458    pTab = 0;
6459  }
6460  return pTab;
6461}
6462
6463/*
6464** This function is called to search the schema recovered from the
6465** sqlite_master table of the (possibly) corrupt database as part
6466** of a ".recover" command. Specifically, for a table with root page
6467** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6468** table must be a WITHOUT ROWID table, or if non-zero, not one of
6469** those.
6470**
6471** If a table is found, a (RecoverTable*) object is returned. Or, if
6472** no such table is found, but bIntkey is false and iRoot is the
6473** root page of an index in the recovered schema, then (*pbNoop) is
6474** set to true and NULL returned. Or, if there is no such table or
6475** index, NULL is returned and (*pbNoop) set to 0, indicating that
6476** the caller should write data to the orphans table.
6477*/
6478static RecoverTable *recoverFindTable(
6479  ShellState *pState,             /* Shell state object */
6480  int *pRc,                       /* IN/OUT: Error code */
6481  int iRoot,                      /* Root page of table */
6482  int bIntkey,                    /* True for an intkey table */
6483  int nCol,                       /* Number of columns in table */
6484  int *pbNoop                     /* OUT: True if iRoot is root of index */
6485){
6486  sqlite3_stmt *pStmt = 0;
6487  RecoverTable *pRet = 0;
6488  int bNoop = 0;
6489  const char *zSql = 0;
6490  const char *zName = 0;
6491
6492  /* Search the recovered schema for an object with root page iRoot. */
6493  shellPreparePrintf(pState->db, pRc, &pStmt,
6494      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6495  );
6496  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6497    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6498    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6499      bNoop = 1;
6500      break;
6501    }
6502    if( sqlite3_stricmp(zType, "table")==0 ){
6503      zName = (const char*)sqlite3_column_text(pStmt, 1);
6504      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6505      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6506      break;
6507    }
6508  }
6509
6510  shellFinalize(pRc, pStmt);
6511  *pbNoop = bNoop;
6512  return pRet;
6513}
6514
6515/*
6516** Return a RecoverTable object representing the orphans table.
6517*/
6518static RecoverTable *recoverOrphanTable(
6519  ShellState *pState,             /* Shell state object */
6520  int *pRc,                       /* IN/OUT: Error code */
6521  const char *zLostAndFound,      /* Base name for orphans table */
6522  int nCol                        /* Number of user data columns */
6523){
6524  RecoverTable *pTab = 0;
6525  if( nCol>=0 && *pRc==SQLITE_OK ){
6526    int i;
6527
6528    /* This block determines the name of the orphan table. The prefered
6529    ** name is zLostAndFound. But if that clashes with another name
6530    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
6531    ** and so on until a non-clashing name is found.  */
6532    int iTab = 0;
6533    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
6534    sqlite3_stmt *pTest = 0;
6535    shellPrepare(pState->db, pRc,
6536        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
6537    );
6538    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6539    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
6540      shellReset(pRc, pTest);
6541      sqlite3_free(zTab);
6542      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
6543      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6544    }
6545    shellFinalize(pRc, pTest);
6546
6547    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
6548    if( pTab ){
6549      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
6550      pTab->nCol = nCol;
6551      pTab->iPk = -2;
6552      if( nCol>0 ){
6553        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
6554        if( pTab->azlCol ){
6555          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
6556          for(i=nCol-1; i>=0; i--){
6557            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
6558          }
6559        }
6560      }
6561
6562      if( *pRc!=SQLITE_OK ){
6563        recoverFreeTable(pTab);
6564        pTab = 0;
6565      }else{
6566        raw_printf(pState->out,
6567            "CREATE TABLE %s(rootpgno INTEGER, "
6568            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
6569        );
6570        for(i=0; i<nCol; i++){
6571          raw_printf(pState->out, ", c%d", i);
6572        }
6573        raw_printf(pState->out, ");\n");
6574      }
6575    }
6576    sqlite3_free(zTab);
6577  }
6578  return pTab;
6579}
6580
6581/*
6582** This function is called to recover data from the database. A script
6583** to construct a new database containing all recovered data is output
6584** on stream pState->out.
6585*/
6586static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
6587  int rc = SQLITE_OK;
6588  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
6589  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
6590  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
6591  const char *zRecoveryDb = "";   /* Name of "recovery" database */
6592  const char *zLostAndFound = "lost_and_found";
6593  int i;
6594  int nOrphan = -1;
6595  RecoverTable *pOrphan = 0;
6596
6597  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
6598  int bRowids = 1;                /* 0 if --no-rowids */
6599  for(i=1; i<nArg; i++){
6600    char *z = azArg[i];
6601    int n;
6602    if( z[0]=='-' && z[1]=='-' ) z++;
6603    n = strlen30(z);
6604    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
6605      bFreelist = 0;
6606    }else
6607    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
6608      i++;
6609      zRecoveryDb = azArg[i];
6610    }else
6611    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
6612      i++;
6613      zLostAndFound = azArg[i];
6614    }else
6615    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
6616      bRowids = 0;
6617    }
6618    else{
6619      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
6620      showHelp(pState->out, azArg[0]);
6621      return 1;
6622    }
6623  }
6624
6625  shellExecPrintf(pState->db, &rc,
6626    /* Attach an in-memory database named 'recovery'. Create an indexed
6627    ** cache of the sqlite_dbptr virtual table. */
6628    "PRAGMA writable_schema = on;"
6629    "ATTACH %Q AS recovery;"
6630    "DROP TABLE IF EXISTS recovery.dbptr;"
6631    "DROP TABLE IF EXISTS recovery.freelist;"
6632    "DROP TABLE IF EXISTS recovery.map;"
6633    "DROP TABLE IF EXISTS recovery.schema;"
6634    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
6635  );
6636
6637  if( bFreelist ){
6638    shellExec(pState->db, &rc,
6639      "WITH trunk(pgno) AS ("
6640      "  SELECT shell_int32("
6641      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
6642      "      WHERE x>0"
6643      "    UNION"
6644      "  SELECT shell_int32("
6645      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
6646      "      FROM trunk WHERE x>0"
6647      "),"
6648      "freelist(data, n, freepgno) AS ("
6649      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
6650      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
6651      "    UNION ALL"
6652      "  SELECT data, n-1, shell_int32(data, 2+n) "
6653      "      FROM freelist WHERE n>=0"
6654      ")"
6655      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
6656    );
6657  }
6658
6659  /* If this is an auto-vacuum database, add all pointer-map pages to
6660  ** the freelist table. Do this regardless of whether or not
6661  ** --freelist-corrupt was specified.  */
6662  shellExec(pState->db, &rc,
6663    "WITH ptrmap(pgno) AS ("
6664    "  SELECT 2 WHERE shell_int32("
6665    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
6666    "  )"
6667    "    UNION ALL "
6668    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
6669    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
6670    ")"
6671    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
6672  );
6673
6674  shellExec(pState->db, &rc,
6675    "CREATE TABLE recovery.dbptr("
6676    "      pgno, child, PRIMARY KEY(child, pgno)"
6677    ") WITHOUT ROWID;"
6678    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
6679    "    SELECT * FROM sqlite_dbptr"
6680    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
6681
6682    /* Delete any pointer to page 1. This ensures that page 1 is considered
6683    ** a root page, regardless of how corrupt the db is. */
6684    "DELETE FROM recovery.dbptr WHERE child = 1;"
6685
6686    /* Delete all pointers to any pages that have more than one pointer
6687    ** to them. Such pages will be treated as root pages when recovering
6688    ** data.  */
6689    "DELETE FROM recovery.dbptr WHERE child IN ("
6690    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
6691    ");"
6692
6693    /* Create the "map" table that will (eventually) contain instructions
6694    ** for dealing with each page in the db that contains one or more
6695    ** records. */
6696    "CREATE TABLE recovery.map("
6697      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
6698    ");"
6699
6700    /* Populate table [map]. If there are circular loops of pages in the
6701    ** database, the following adds all pages in such a loop to the map
6702    ** as individual root pages. This could be handled better.  */
6703    "WITH pages(i, maxlen) AS ("
6704    "  SELECT page_count, ("
6705    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
6706    "  ) FROM pragma_page_count WHERE page_count>0"
6707    "    UNION ALL"
6708    "  SELECT i-1, ("
6709    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
6710    "  ) FROM pages WHERE i>=2"
6711    ")"
6712    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
6713    "  SELECT i, maxlen, NULL, ("
6714    "    WITH p(orig, pgno, parent) AS ("
6715    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
6716    "        UNION "
6717    "      SELECT i, p.parent, "
6718    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
6719    "    )"
6720    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
6721    ") "
6722    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
6723    "UPDATE recovery.map AS o SET intkey = ("
6724    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
6725    ");"
6726
6727    /* Extract data from page 1 and any linked pages into table
6728    ** recovery.schema. With the same schema as an sqlite_master table.  */
6729    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
6730    "INSERT INTO recovery.schema SELECT "
6731    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
6732    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
6733    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
6734    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
6735    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
6736    "FROM sqlite_dbdata WHERE pgno IN ("
6737    "  SELECT pgno FROM recovery.map WHERE root=1"
6738    ")"
6739    "GROUP BY pgno, cell;"
6740    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
6741  );
6742
6743  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
6744  ** CREATE TABLE statements that extracted from the existing schema.  */
6745  if( rc==SQLITE_OK ){
6746    sqlite3_stmt *pStmt = 0;
6747    /* ".recover" might output content in an order which causes immediate
6748    ** foreign key constraints to be violated. So disable foreign-key
6749    ** constraint enforcement to prevent problems when running the output
6750    ** script. */
6751    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
6752    raw_printf(pState->out, "BEGIN;\n");
6753    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
6754    shellPrepare(pState->db, &rc,
6755        "SELECT sql FROM recovery.schema "
6756        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
6757    );
6758    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6759      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
6760      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
6761          &zCreateTable[12]
6762      );
6763    }
6764    shellFinalize(&rc, pStmt);
6765  }
6766
6767  /* Figure out if an orphan table will be required. And if so, how many
6768  ** user columns it should contain */
6769  shellPrepare(pState->db, &rc,
6770      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
6771      , &pLoop
6772  );
6773  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
6774    nOrphan = sqlite3_column_int(pLoop, 0);
6775  }
6776  shellFinalize(&rc, pLoop);
6777  pLoop = 0;
6778
6779  shellPrepare(pState->db, &rc,
6780      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
6781  );
6782
6783  shellPrepare(pState->db, &rc,
6784      "SELECT max(field), group_concat(shell_escape_crnl(quote"
6785      "(case when (? AND field<0) then NULL else value end)"
6786      "), ', ')"
6787      ", min(field) "
6788      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
6789      "GROUP BY cell", &pCells
6790  );
6791
6792  /* Loop through each root page. */
6793  shellPrepare(pState->db, &rc,
6794      "SELECT root, intkey, max(maxlen) FROM recovery.map"
6795      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
6796      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
6797      ")", &pLoop
6798  );
6799  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
6800    int iRoot = sqlite3_column_int(pLoop, 0);
6801    int bIntkey = sqlite3_column_int(pLoop, 1);
6802    int nCol = sqlite3_column_int(pLoop, 2);
6803    int bNoop = 0;
6804    RecoverTable *pTab;
6805
6806    assert( bIntkey==0 || bIntkey==1 );
6807    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
6808    if( bNoop || rc ) continue;
6809    if( pTab==0 ){
6810      if( pOrphan==0 ){
6811        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
6812      }
6813      pTab = pOrphan;
6814      if( pTab==0 ) break;
6815    }
6816
6817    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
6818      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
6819    }
6820    sqlite3_bind_int(pPages, 1, iRoot);
6821    if( bRowids==0 && pTab->iPk<0 ){
6822      sqlite3_bind_int(pCells, 1, 1);
6823    }else{
6824      sqlite3_bind_int(pCells, 1, 0);
6825    }
6826    sqlite3_bind_int(pCells, 3, pTab->iPk);
6827
6828    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
6829      int iPgno = sqlite3_column_int(pPages, 0);
6830      sqlite3_bind_int(pCells, 2, iPgno);
6831      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
6832        int nField = sqlite3_column_int(pCells, 0);
6833        int iMin = sqlite3_column_int(pCells, 2);
6834        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
6835
6836        RecoverTable *pTab2 = pTab;
6837        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
6838          if( pOrphan==0 ){
6839            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
6840          }
6841          pTab2 = pOrphan;
6842          if( pTab2==0 ) break;
6843        }
6844
6845        nField = nField+1;
6846        if( pTab2==pOrphan ){
6847          raw_printf(pState->out,
6848              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
6849              pTab2->zQuoted, iRoot, iPgno, nField,
6850              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
6851          );
6852        }else{
6853          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
6854              pTab2->zQuoted, pTab2->azlCol[nField], zVal
6855          );
6856        }
6857      }
6858      shellReset(&rc, pCells);
6859    }
6860    shellReset(&rc, pPages);
6861    if( pTab!=pOrphan ) recoverFreeTable(pTab);
6862  }
6863  shellFinalize(&rc, pLoop);
6864  shellFinalize(&rc, pPages);
6865  shellFinalize(&rc, pCells);
6866  recoverFreeTable(pOrphan);
6867
6868  /* The rest of the schema */
6869  if( rc==SQLITE_OK ){
6870    sqlite3_stmt *pStmt = 0;
6871    shellPrepare(pState->db, &rc,
6872        "SELECT sql, name FROM recovery.schema "
6873        "WHERE sql NOT LIKE 'create table%'", &pStmt
6874    );
6875    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6876      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
6877      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
6878        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
6879        char *zPrint = shellMPrintf(&rc,
6880          "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)",
6881          zName, zName, zSql
6882        );
6883        raw_printf(pState->out, "%s;\n", zPrint);
6884        sqlite3_free(zPrint);
6885      }else{
6886        raw_printf(pState->out, "%s;\n", zSql);
6887      }
6888    }
6889    shellFinalize(&rc, pStmt);
6890  }
6891
6892  if( rc==SQLITE_OK ){
6893    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
6894    raw_printf(pState->out, "COMMIT;\n");
6895  }
6896  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
6897  return rc;
6898}
6899#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
6900
6901
6902/*
6903** If an input line begins with "." then invoke this routine to
6904** process that line.
6905**
6906** Return 1 on error, 2 to exit, and 0 otherwise.
6907*/
6908static int do_meta_command(char *zLine, ShellState *p){
6909  int h = 1;
6910  int nArg = 0;
6911  int n, c;
6912  int rc = 0;
6913  char *azArg[52];
6914
6915#ifndef SQLITE_OMIT_VIRTUALTABLE
6916  if( p->expert.pExpert ){
6917    expertFinish(p, 1, 0);
6918  }
6919#endif
6920
6921  /* Parse the input line into tokens.
6922  */
6923  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
6924    while( IsSpace(zLine[h]) ){ h++; }
6925    if( zLine[h]==0 ) break;
6926    if( zLine[h]=='\'' || zLine[h]=='"' ){
6927      int delim = zLine[h++];
6928      azArg[nArg++] = &zLine[h];
6929      while( zLine[h] && zLine[h]!=delim ){
6930        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
6931        h++;
6932      }
6933      if( zLine[h]==delim ){
6934        zLine[h++] = 0;
6935      }
6936      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
6937    }else{
6938      azArg[nArg++] = &zLine[h];
6939      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
6940      if( zLine[h] ) zLine[h++] = 0;
6941      resolve_backslashes(azArg[nArg-1]);
6942    }
6943  }
6944  azArg[nArg] = 0;
6945
6946  /* Process the input line.
6947  */
6948  if( nArg==0 ) return 0; /* no tokens, no error */
6949  n = strlen30(azArg[0]);
6950  c = azArg[0][0];
6951  clearTempFile(p);
6952
6953#ifndef SQLITE_OMIT_AUTHORIZATION
6954  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
6955    if( nArg!=2 ){
6956      raw_printf(stderr, "Usage: .auth ON|OFF\n");
6957      rc = 1;
6958      goto meta_command_exit;
6959    }
6960    open_db(p, 0);
6961    if( booleanValue(azArg[1]) ){
6962      sqlite3_set_authorizer(p->db, shellAuth, p);
6963    }else{
6964      sqlite3_set_authorizer(p->db, 0, 0);
6965    }
6966  }else
6967#endif
6968
6969#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6970  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
6971    open_db(p, 0);
6972    rc = arDotCommand(p, 0, azArg, nArg);
6973  }else
6974#endif
6975
6976  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
6977   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
6978  ){
6979    const char *zDestFile = 0;
6980    const char *zDb = 0;
6981    sqlite3 *pDest;
6982    sqlite3_backup *pBackup;
6983    int j;
6984    int bAsync = 0;
6985    const char *zVfs = 0;
6986    for(j=1; j<nArg; j++){
6987      const char *z = azArg[j];
6988      if( z[0]=='-' ){
6989        if( z[1]=='-' ) z++;
6990        if( strcmp(z, "-append")==0 ){
6991          zVfs = "apndvfs";
6992        }else
6993        if( strcmp(z, "-async")==0 ){
6994          bAsync = 1;
6995        }else
6996        {
6997          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
6998          return 1;
6999        }
7000      }else if( zDestFile==0 ){
7001        zDestFile = azArg[j];
7002      }else if( zDb==0 ){
7003        zDb = zDestFile;
7004        zDestFile = azArg[j];
7005      }else{
7006        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7007        return 1;
7008      }
7009    }
7010    if( zDestFile==0 ){
7011      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7012      return 1;
7013    }
7014    if( zDb==0 ) zDb = "main";
7015    rc = sqlite3_open_v2(zDestFile, &pDest,
7016                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7017    if( rc!=SQLITE_OK ){
7018      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7019      close_db(pDest);
7020      return 1;
7021    }
7022    if( bAsync ){
7023      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7024                   0, 0, 0);
7025    }
7026    open_db(p, 0);
7027    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7028    if( pBackup==0 ){
7029      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7030      close_db(pDest);
7031      return 1;
7032    }
7033    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7034    sqlite3_backup_finish(pBackup);
7035    if( rc==SQLITE_DONE ){
7036      rc = 0;
7037    }else{
7038      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7039      rc = 1;
7040    }
7041    close_db(pDest);
7042  }else
7043
7044  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7045    if( nArg==2 ){
7046      bail_on_error = booleanValue(azArg[1]);
7047    }else{
7048      raw_printf(stderr, "Usage: .bail on|off\n");
7049      rc = 1;
7050    }
7051  }else
7052
7053  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7054    if( nArg==2 ){
7055      if( booleanValue(azArg[1]) ){
7056        setBinaryMode(p->out, 1);
7057      }else{
7058        setTextMode(p->out, 1);
7059      }
7060    }else{
7061      raw_printf(stderr, "Usage: .binary on|off\n");
7062      rc = 1;
7063    }
7064  }else
7065
7066  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7067    if( nArg==2 ){
7068#if defined(_WIN32) || defined(WIN32)
7069      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7070      rc = !SetCurrentDirectoryW(z);
7071      sqlite3_free(z);
7072#else
7073      rc = chdir(azArg[1]);
7074#endif
7075      if( rc ){
7076        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7077        rc = 1;
7078      }
7079    }else{
7080      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7081      rc = 1;
7082    }
7083  }else
7084
7085  /* The undocumented ".breakpoint" command causes a call to the no-op
7086  ** routine named test_breakpoint().
7087  */
7088  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7089    test_breakpoint();
7090  }else
7091
7092  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7093    if( nArg==2 ){
7094      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7095    }else{
7096      raw_printf(stderr, "Usage: .changes on|off\n");
7097      rc = 1;
7098    }
7099  }else
7100
7101  /* Cancel output redirection, if it is currently set (by .testcase)
7102  ** Then read the content of the testcase-out.txt file and compare against
7103  ** azArg[1].  If there are differences, report an error and exit.
7104  */
7105  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7106    char *zRes = 0;
7107    output_reset(p);
7108    if( nArg!=2 ){
7109      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7110      rc = 2;
7111    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7112      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7113      rc = 2;
7114    }else if( testcase_glob(azArg[1],zRes)==0 ){
7115      utf8_printf(stderr,
7116                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7117                 p->zTestcase, azArg[1], zRes);
7118      rc = 1;
7119    }else{
7120      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7121      p->nCheck++;
7122    }
7123    sqlite3_free(zRes);
7124  }else
7125
7126  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7127    if( nArg==2 ){
7128      tryToClone(p, azArg[1]);
7129    }else{
7130      raw_printf(stderr, "Usage: .clone FILENAME\n");
7131      rc = 1;
7132    }
7133  }else
7134
7135  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7136    ShellState data;
7137    char *zErrMsg = 0;
7138    open_db(p, 0);
7139    memcpy(&data, p, sizeof(data));
7140    data.showHeader = 0;
7141    data.cMode = data.mode = MODE_List;
7142    sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
7143    data.cnt = 0;
7144    sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
7145                 callback, &data, &zErrMsg);
7146    if( zErrMsg ){
7147      utf8_printf(stderr,"Error: %s\n", zErrMsg);
7148      sqlite3_free(zErrMsg);
7149      rc = 1;
7150    }
7151  }else
7152
7153  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7154    static const struct DbConfigChoices {
7155      const char *zName;
7156      int op;
7157    } aDbConfig[] = {
7158        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7159        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7160        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7161        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7162        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7163        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7164        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7165        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7166        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7167        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7168        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7169        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7170        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7171        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7172        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7173    };
7174    int ii, v;
7175    open_db(p, 0);
7176    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7177      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7178      if( nArg>=3 ){
7179        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7180      }
7181      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7182      utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7183      if( nArg>1 ) break;
7184    }
7185    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7186      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7187      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7188    }
7189  }else
7190
7191  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7192    rc = shell_dbinfo_command(p, nArg, azArg);
7193  }else
7194
7195#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7196  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7197    open_db(p, 0);
7198    rc = recoverDatabaseCmd(p, nArg, azArg);
7199  }else
7200#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7201
7202  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7203    const char *zLike = 0;
7204    int i;
7205    int savedShowHeader = p->showHeader;
7206    int savedShellFlags = p->shellFlgs;
7207    ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
7208    for(i=1; i<nArg; i++){
7209      if( azArg[i][0]=='-' ){
7210        const char *z = azArg[i]+1;
7211        if( z[0]=='-' ) z++;
7212        if( strcmp(z,"preserve-rowids")==0 ){
7213#ifdef SQLITE_OMIT_VIRTUALTABLE
7214          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7215                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7216          rc = 1;
7217          goto meta_command_exit;
7218#else
7219          ShellSetFlag(p, SHFLG_PreserveRowid);
7220#endif
7221        }else
7222        if( strcmp(z,"newlines")==0 ){
7223          ShellSetFlag(p, SHFLG_Newlines);
7224        }else
7225        {
7226          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7227          rc = 1;
7228          goto meta_command_exit;
7229        }
7230      }else if( zLike ){
7231        raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
7232                           "?--newlines? ?LIKE-PATTERN?\n");
7233        rc = 1;
7234        goto meta_command_exit;
7235      }else{
7236        zLike = azArg[i];
7237      }
7238    }
7239
7240    open_db(p, 0);
7241
7242    /* When playing back a "dump", the content might appear in an order
7243    ** which causes immediate foreign key constraints to be violated.
7244    ** So disable foreign-key constraint enforcement to prevent problems. */
7245    raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7246    raw_printf(p->out, "BEGIN TRANSACTION;\n");
7247    p->writableSchema = 0;
7248    p->showHeader = 0;
7249    /* Set writable_schema=ON since doing so forces SQLite to initialize
7250    ** as much of the schema as it can even if the sqlite_master table is
7251    ** corrupt. */
7252    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7253    p->nErr = 0;
7254    if( zLike==0 ){
7255      run_schema_dump_query(p,
7256        "SELECT name, type, sql FROM sqlite_master "
7257        "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
7258      );
7259      run_schema_dump_query(p,
7260        "SELECT name, type, sql FROM sqlite_master "
7261        "WHERE name=='sqlite_sequence'"
7262      );
7263      run_table_dump_query(p,
7264        "SELECT sql FROM sqlite_master "
7265        "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
7266      );
7267    }else{
7268      char *zSql;
7269      zSql = sqlite3_mprintf(
7270        "SELECT name, type, sql FROM sqlite_master "
7271        "WHERE tbl_name LIKE %Q AND type=='table'"
7272        "  AND sql NOT NULL", zLike);
7273      run_schema_dump_query(p,zSql);
7274      sqlite3_free(zSql);
7275      zSql = sqlite3_mprintf(
7276        "SELECT sql FROM sqlite_master "
7277        "WHERE sql NOT NULL"
7278        "  AND type IN ('index','trigger','view')"
7279        "  AND tbl_name LIKE %Q", zLike);
7280      run_table_dump_query(p, zSql, 0);
7281      sqlite3_free(zSql);
7282    }
7283    if( p->writableSchema ){
7284      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7285      p->writableSchema = 0;
7286    }
7287    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7288    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7289    raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7290    p->showHeader = savedShowHeader;
7291    p->shellFlgs = savedShellFlags;
7292  }else
7293
7294  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7295    if( nArg==2 ){
7296      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7297    }else{
7298      raw_printf(stderr, "Usage: .echo on|off\n");
7299      rc = 1;
7300    }
7301  }else
7302
7303  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7304    if( nArg==2 ){
7305      p->autoEQPtest = 0;
7306      if( p->autoEQPtrace ){
7307        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7308        p->autoEQPtrace = 0;
7309      }
7310      if( strcmp(azArg[1],"full")==0 ){
7311        p->autoEQP = AUTOEQP_full;
7312      }else if( strcmp(azArg[1],"trigger")==0 ){
7313        p->autoEQP = AUTOEQP_trigger;
7314#ifdef SQLITE_DEBUG
7315      }else if( strcmp(azArg[1],"test")==0 ){
7316        p->autoEQP = AUTOEQP_on;
7317        p->autoEQPtest = 1;
7318      }else if( strcmp(azArg[1],"trace")==0 ){
7319        p->autoEQP = AUTOEQP_full;
7320        p->autoEQPtrace = 1;
7321        open_db(p, 0);
7322        sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
7323        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7324#endif
7325      }else{
7326        p->autoEQP = (u8)booleanValue(azArg[1]);
7327      }
7328    }else{
7329      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7330      rc = 1;
7331    }
7332  }else
7333
7334  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7335    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7336    rc = 2;
7337  }else
7338
7339  /* The ".explain" command is automatic now.  It is largely pointless.  It
7340  ** retained purely for backwards compatibility */
7341  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7342    int val = 1;
7343    if( nArg>=2 ){
7344      if( strcmp(azArg[1],"auto")==0 ){
7345        val = 99;
7346      }else{
7347        val =  booleanValue(azArg[1]);
7348      }
7349    }
7350    if( val==1 && p->mode!=MODE_Explain ){
7351      p->normalMode = p->mode;
7352      p->mode = MODE_Explain;
7353      p->autoExplain = 0;
7354    }else if( val==0 ){
7355      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7356      p->autoExplain = 0;
7357    }else if( val==99 ){
7358      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7359      p->autoExplain = 1;
7360    }
7361  }else
7362
7363#ifndef SQLITE_OMIT_VIRTUALTABLE
7364  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7365    open_db(p, 0);
7366    expertDotCommand(p, azArg, nArg);
7367  }else
7368#endif
7369
7370  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7371    static const struct {
7372       const char *zCtrlName;   /* Name of a test-control option */
7373       int ctrlCode;            /* Integer code for that option */
7374       const char *zUsage;      /* Usage notes */
7375    } aCtrl[] = {
7376      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7377      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7378   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7379      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7380      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7381   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7382      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7383      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7384      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7385    };
7386    int filectrl = -1;
7387    int iCtrl = -1;
7388    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7389    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7390    int n2, i;
7391    const char *zCmd = 0;
7392
7393    open_db(p, 0);
7394    zCmd = nArg>=2 ? azArg[1] : "help";
7395
7396    /* The argument can optionally begin with "-" or "--" */
7397    if( zCmd[0]=='-' && zCmd[1] ){
7398      zCmd++;
7399      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7400    }
7401
7402    /* --help lists all file-controls */
7403    if( strcmp(zCmd,"help")==0 ){
7404      utf8_printf(p->out, "Available file-controls:\n");
7405      for(i=0; i<ArraySize(aCtrl); i++){
7406        utf8_printf(p->out, "  .filectrl %s %s\n",
7407                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7408      }
7409      rc = 1;
7410      goto meta_command_exit;
7411    }
7412
7413    /* convert filectrl text option to value. allow any unique prefix
7414    ** of the option name, or a numerical value. */
7415    n2 = strlen30(zCmd);
7416    for(i=0; i<ArraySize(aCtrl); i++){
7417      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7418        if( filectrl<0 ){
7419          filectrl = aCtrl[i].ctrlCode;
7420          iCtrl = i;
7421        }else{
7422          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7423                              "Use \".filectrl --help\" for help\n", zCmd);
7424          rc = 1;
7425          goto meta_command_exit;
7426        }
7427      }
7428    }
7429    if( filectrl<0 ){
7430      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7431                         "Use \".filectrl --help\" for help\n", zCmd);
7432    }else{
7433      switch(filectrl){
7434        case SQLITE_FCNTL_SIZE_LIMIT: {
7435          if( nArg!=2 && nArg!=3 ) break;
7436          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7437          sqlite3_file_control(p->db, 0, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7438          isOk = 1;
7439          break;
7440        }
7441        case SQLITE_FCNTL_LOCK_TIMEOUT:
7442        case SQLITE_FCNTL_CHUNK_SIZE: {
7443          int x;
7444          if( nArg!=3 ) break;
7445          x = (int)integerValue(azArg[2]);
7446          sqlite3_file_control(p->db, 0, filectrl, &x);
7447          isOk = 2;
7448          break;
7449        }
7450        case SQLITE_FCNTL_PERSIST_WAL:
7451        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7452          int x;
7453          if( nArg!=2 && nArg!=3 ) break;
7454          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7455          sqlite3_file_control(p->db, 0, filectrl, &x);
7456          iRes = x;
7457          isOk = 1;
7458          break;
7459        }
7460        case SQLITE_FCNTL_HAS_MOVED: {
7461          int x;
7462          if( nArg!=2 ) break;
7463          sqlite3_file_control(p->db, 0, filectrl, &x);
7464          iRes = x;
7465          isOk = 1;
7466          break;
7467        }
7468        case SQLITE_FCNTL_TEMPFILENAME: {
7469          char *z = 0;
7470          if( nArg!=2 ) break;
7471          sqlite3_file_control(p->db, 0, filectrl, &z);
7472          if( z ){
7473            utf8_printf(p->out, "%s\n", z);
7474            sqlite3_free(z);
7475          }
7476          isOk = 2;
7477          break;
7478        }
7479      }
7480    }
7481    if( isOk==0 && iCtrl>=0 ){
7482      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
7483      rc = 1;
7484    }else if( isOk==1 ){
7485      char zBuf[100];
7486      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
7487      raw_printf(p->out, "%s\n", zBuf);
7488    }
7489  }else
7490
7491  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
7492    ShellState data;
7493    char *zErrMsg = 0;
7494    int doStats = 0;
7495    memcpy(&data, p, sizeof(data));
7496    data.showHeader = 0;
7497    data.cMode = data.mode = MODE_Semi;
7498    if( nArg==2 && optionMatch(azArg[1], "indent") ){
7499      data.cMode = data.mode = MODE_Pretty;
7500      nArg = 1;
7501    }
7502    if( nArg!=1 ){
7503      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
7504      rc = 1;
7505      goto meta_command_exit;
7506    }
7507    open_db(p, 0);
7508    rc = sqlite3_exec(p->db,
7509       "SELECT sql FROM"
7510       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
7511       "     FROM sqlite_master UNION ALL"
7512       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
7513       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
7514       "ORDER BY rowid",
7515       callback, &data, &zErrMsg
7516    );
7517    if( rc==SQLITE_OK ){
7518      sqlite3_stmt *pStmt;
7519      rc = sqlite3_prepare_v2(p->db,
7520               "SELECT rowid FROM sqlite_master"
7521               " WHERE name GLOB 'sqlite_stat[134]'",
7522               -1, &pStmt, 0);
7523      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
7524      sqlite3_finalize(pStmt);
7525    }
7526    if( doStats==0 ){
7527      raw_printf(p->out, "/* No STAT tables available */\n");
7528    }else{
7529      raw_printf(p->out, "ANALYZE sqlite_master;\n");
7530      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
7531                   callback, &data, &zErrMsg);
7532      data.cMode = data.mode = MODE_Insert;
7533      data.zDestTable = "sqlite_stat1";
7534      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
7535      data.zDestTable = "sqlite_stat4";
7536      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
7537      raw_printf(p->out, "ANALYZE sqlite_master;\n");
7538    }
7539  }else
7540
7541  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
7542    if( nArg==2 ){
7543      p->showHeader = booleanValue(azArg[1]);
7544    }else{
7545      raw_printf(stderr, "Usage: .headers on|off\n");
7546      rc = 1;
7547    }
7548  }else
7549
7550  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
7551    if( nArg>=2 ){
7552      n = showHelp(p->out, azArg[1]);
7553      if( n==0 ){
7554        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
7555      }
7556    }else{
7557      showHelp(p->out, 0);
7558    }
7559  }else
7560
7561  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
7562    char *zTable;               /* Insert data into this table */
7563    char *zFile;                /* Name of file to extra content from */
7564    sqlite3_stmt *pStmt = NULL; /* A statement */
7565    int nCol;                   /* Number of columns in the table */
7566    int nByte;                  /* Number of bytes in an SQL string */
7567    int i, j;                   /* Loop counters */
7568    int needCommit;             /* True to COMMIT or ROLLBACK at end */
7569    int nSep;                   /* Number of bytes in p->colSeparator[] */
7570    char *zSql;                 /* An SQL statement */
7571    ImportCtx sCtx;             /* Reader context */
7572    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
7573    int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close file */
7574
7575    if( nArg!=3 ){
7576      raw_printf(stderr, "Usage: .import FILE TABLE\n");
7577      goto meta_command_exit;
7578    }
7579    zFile = azArg[1];
7580    zTable = azArg[2];
7581    seenInterrupt = 0;
7582    memset(&sCtx, 0, sizeof(sCtx));
7583    open_db(p, 0);
7584    nSep = strlen30(p->colSeparator);
7585    if( nSep==0 ){
7586      raw_printf(stderr,
7587                 "Error: non-null column separator required for import\n");
7588      return 1;
7589    }
7590    if( nSep>1 ){
7591      raw_printf(stderr, "Error: multi-character column separators not allowed"
7592                      " for import\n");
7593      return 1;
7594    }
7595    nSep = strlen30(p->rowSeparator);
7596    if( nSep==0 ){
7597      raw_printf(stderr, "Error: non-null row separator required for import\n");
7598      return 1;
7599    }
7600    if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
7601      /* When importing CSV (only), if the row separator is set to the
7602      ** default output row separator, change it to the default input
7603      ** row separator.  This avoids having to maintain different input
7604      ** and output row separators. */
7605      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
7606      nSep = strlen30(p->rowSeparator);
7607    }
7608    if( nSep>1 ){
7609      raw_printf(stderr, "Error: multi-character row separators not allowed"
7610                      " for import\n");
7611      return 1;
7612    }
7613    sCtx.zFile = zFile;
7614    sCtx.nLine = 1;
7615    if( sCtx.zFile[0]=='|' ){
7616#ifdef SQLITE_OMIT_POPEN
7617      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
7618      return 1;
7619#else
7620      sCtx.in = popen(sCtx.zFile+1, "r");
7621      sCtx.zFile = "<pipe>";
7622      xCloser = pclose;
7623#endif
7624    }else{
7625      sCtx.in = fopen(sCtx.zFile, "rb");
7626      xCloser = fclose;
7627    }
7628    if( p->mode==MODE_Ascii ){
7629      xRead = ascii_read_one_field;
7630    }else{
7631      xRead = csv_read_one_field;
7632    }
7633    if( sCtx.in==0 ){
7634      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
7635      return 1;
7636    }
7637    sCtx.cColSep = p->colSeparator[0];
7638    sCtx.cRowSep = p->rowSeparator[0];
7639    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
7640    if( zSql==0 ){
7641      xCloser(sCtx.in);
7642      shell_out_of_memory();
7643    }
7644    nByte = strlen30(zSql);
7645    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7646    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
7647    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
7648      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
7649      char cSep = '(';
7650      while( xRead(&sCtx) ){
7651        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
7652        cSep = ',';
7653        if( sCtx.cTerm!=sCtx.cColSep ) break;
7654      }
7655      if( cSep=='(' ){
7656        sqlite3_free(zCreate);
7657        sqlite3_free(sCtx.z);
7658        xCloser(sCtx.in);
7659        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
7660        return 1;
7661      }
7662      zCreate = sqlite3_mprintf("%z\n)", zCreate);
7663      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
7664      sqlite3_free(zCreate);
7665      if( rc ){
7666        utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
7667                sqlite3_errmsg(p->db));
7668        sqlite3_free(sCtx.z);
7669        xCloser(sCtx.in);
7670        return 1;
7671      }
7672      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7673    }
7674    sqlite3_free(zSql);
7675    if( rc ){
7676      if (pStmt) sqlite3_finalize(pStmt);
7677      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
7678      xCloser(sCtx.in);
7679      return 1;
7680    }
7681    nCol = sqlite3_column_count(pStmt);
7682    sqlite3_finalize(pStmt);
7683    pStmt = 0;
7684    if( nCol==0 ) return 0; /* no columns, no error */
7685    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
7686    if( zSql==0 ){
7687      xCloser(sCtx.in);
7688      shell_out_of_memory();
7689    }
7690    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
7691    j = strlen30(zSql);
7692    for(i=1; i<nCol; i++){
7693      zSql[j++] = ',';
7694      zSql[j++] = '?';
7695    }
7696    zSql[j++] = ')';
7697    zSql[j] = 0;
7698    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7699    sqlite3_free(zSql);
7700    if( rc ){
7701      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7702      if (pStmt) sqlite3_finalize(pStmt);
7703      xCloser(sCtx.in);
7704      return 1;
7705    }
7706    needCommit = sqlite3_get_autocommit(p->db);
7707    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
7708    do{
7709      int startLine = sCtx.nLine;
7710      for(i=0; i<nCol; i++){
7711        char *z = xRead(&sCtx);
7712        /*
7713        ** Did we reach end-of-file before finding any columns?
7714        ** If so, stop instead of NULL filling the remaining columns.
7715        */
7716        if( z==0 && i==0 ) break;
7717        /*
7718        ** Did we reach end-of-file OR end-of-line before finding any
7719        ** columns in ASCII mode?  If so, stop instead of NULL filling
7720        ** the remaining columns.
7721        */
7722        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
7723        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
7724        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
7725          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
7726                          "filling the rest with NULL\n",
7727                          sCtx.zFile, startLine, nCol, i+1);
7728          i += 2;
7729          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
7730        }
7731      }
7732      if( sCtx.cTerm==sCtx.cColSep ){
7733        do{
7734          xRead(&sCtx);
7735          i++;
7736        }while( sCtx.cTerm==sCtx.cColSep );
7737        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
7738                        "extras ignored\n",
7739                        sCtx.zFile, startLine, nCol, i);
7740      }
7741      if( i>=nCol ){
7742        sqlite3_step(pStmt);
7743        rc = sqlite3_reset(pStmt);
7744        if( rc!=SQLITE_OK ){
7745          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
7746                      startLine, sqlite3_errmsg(p->db));
7747        }
7748      }
7749    }while( sCtx.cTerm!=EOF );
7750
7751    xCloser(sCtx.in);
7752    sqlite3_free(sCtx.z);
7753    sqlite3_finalize(pStmt);
7754    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
7755  }else
7756
7757#ifndef SQLITE_UNTESTABLE
7758  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
7759    char *zSql;
7760    char *zCollist = 0;
7761    sqlite3_stmt *pStmt;
7762    int tnum = 0;
7763    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
7764    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
7765    int i;
7766    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
7767      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
7768                          "       .imposter off\n");
7769      /* Also allowed, but not documented:
7770      **
7771      **    .imposter TABLE IMPOSTER
7772      **
7773      ** where TABLE is a WITHOUT ROWID table.  In that case, the
7774      ** imposter is another WITHOUT ROWID table with the columns in
7775      ** storage order. */
7776      rc = 1;
7777      goto meta_command_exit;
7778    }
7779    open_db(p, 0);
7780    if( nArg==2 ){
7781      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
7782      goto meta_command_exit;
7783    }
7784    zSql = sqlite3_mprintf(
7785      "SELECT rootpage, 0 FROM sqlite_master"
7786      " WHERE name='%q' AND type='index'"
7787      "UNION ALL "
7788      "SELECT rootpage, 1 FROM sqlite_master"
7789      " WHERE name='%q' AND type='table'"
7790      "   AND sql LIKE '%%without%%rowid%%'",
7791      azArg[1], azArg[1]
7792    );
7793    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7794    sqlite3_free(zSql);
7795    if( sqlite3_step(pStmt)==SQLITE_ROW ){
7796      tnum = sqlite3_column_int(pStmt, 0);
7797      isWO = sqlite3_column_int(pStmt, 1);
7798    }
7799    sqlite3_finalize(pStmt);
7800    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
7801    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7802    sqlite3_free(zSql);
7803    i = 0;
7804    while( sqlite3_step(pStmt)==SQLITE_ROW ){
7805      char zLabel[20];
7806      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
7807      i++;
7808      if( zCol==0 ){
7809        if( sqlite3_column_int(pStmt,1)==-1 ){
7810          zCol = "_ROWID_";
7811        }else{
7812          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
7813          zCol = zLabel;
7814        }
7815      }
7816      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
7817        lenPK = (int)strlen(zCollist);
7818      }
7819      if( zCollist==0 ){
7820        zCollist = sqlite3_mprintf("\"%w\"", zCol);
7821      }else{
7822        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
7823      }
7824    }
7825    sqlite3_finalize(pStmt);
7826    if( i==0 || tnum==0 ){
7827      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
7828      rc = 1;
7829      sqlite3_free(zCollist);
7830      goto meta_command_exit;
7831    }
7832    if( lenPK==0 ) lenPK = 100000;
7833    zSql = sqlite3_mprintf(
7834          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
7835          azArg[2], zCollist, lenPK, zCollist);
7836    sqlite3_free(zCollist);
7837    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
7838    if( rc==SQLITE_OK ){
7839      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
7840      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
7841      if( rc ){
7842        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
7843      }else{
7844        utf8_printf(stdout, "%s;\n", zSql);
7845        raw_printf(stdout,
7846          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
7847          azArg[1], isWO ? "table" : "index"
7848        );
7849      }
7850    }else{
7851      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
7852      rc = 1;
7853    }
7854    sqlite3_free(zSql);
7855  }else
7856#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
7857
7858#ifdef SQLITE_ENABLE_IOTRACE
7859  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
7860    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
7861    if( iotrace && iotrace!=stdout ) fclose(iotrace);
7862    iotrace = 0;
7863    if( nArg<2 ){
7864      sqlite3IoTrace = 0;
7865    }else if( strcmp(azArg[1], "-")==0 ){
7866      sqlite3IoTrace = iotracePrintf;
7867      iotrace = stdout;
7868    }else{
7869      iotrace = fopen(azArg[1], "w");
7870      if( iotrace==0 ){
7871        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
7872        sqlite3IoTrace = 0;
7873        rc = 1;
7874      }else{
7875        sqlite3IoTrace = iotracePrintf;
7876      }
7877    }
7878  }else
7879#endif
7880
7881  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
7882    static const struct {
7883       const char *zLimitName;   /* Name of a limit */
7884       int limitCode;            /* Integer code for that limit */
7885    } aLimit[] = {
7886      { "length",                SQLITE_LIMIT_LENGTH                    },
7887      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
7888      { "column",                SQLITE_LIMIT_COLUMN                    },
7889      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
7890      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
7891      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
7892      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
7893      { "attached",              SQLITE_LIMIT_ATTACHED                  },
7894      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
7895      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
7896      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
7897      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
7898    };
7899    int i, n2;
7900    open_db(p, 0);
7901    if( nArg==1 ){
7902      for(i=0; i<ArraySize(aLimit); i++){
7903        printf("%20s %d\n", aLimit[i].zLimitName,
7904               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
7905      }
7906    }else if( nArg>3 ){
7907      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
7908      rc = 1;
7909      goto meta_command_exit;
7910    }else{
7911      int iLimit = -1;
7912      n2 = strlen30(azArg[1]);
7913      for(i=0; i<ArraySize(aLimit); i++){
7914        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
7915          if( iLimit<0 ){
7916            iLimit = i;
7917          }else{
7918            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
7919            rc = 1;
7920            goto meta_command_exit;
7921          }
7922        }
7923      }
7924      if( iLimit<0 ){
7925        utf8_printf(stderr, "unknown limit: \"%s\"\n"
7926                        "enter \".limits\" with no arguments for a list.\n",
7927                         azArg[1]);
7928        rc = 1;
7929        goto meta_command_exit;
7930      }
7931      if( nArg==3 ){
7932        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
7933                      (int)integerValue(azArg[2]));
7934      }
7935      printf("%20s %d\n", aLimit[iLimit].zLimitName,
7936             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
7937    }
7938  }else
7939
7940  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
7941    open_db(p, 0);
7942    lintDotCommand(p, azArg, nArg);
7943  }else
7944
7945#ifndef SQLITE_OMIT_LOAD_EXTENSION
7946  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
7947    const char *zFile, *zProc;
7948    char *zErrMsg = 0;
7949    if( nArg<2 ){
7950      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
7951      rc = 1;
7952      goto meta_command_exit;
7953    }
7954    zFile = azArg[1];
7955    zProc = nArg>=3 ? azArg[2] : 0;
7956    open_db(p, 0);
7957    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
7958    if( rc!=SQLITE_OK ){
7959      utf8_printf(stderr, "Error: %s\n", zErrMsg);
7960      sqlite3_free(zErrMsg);
7961      rc = 1;
7962    }
7963  }else
7964#endif
7965
7966  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
7967    if( nArg!=2 ){
7968      raw_printf(stderr, "Usage: .log FILENAME\n");
7969      rc = 1;
7970    }else{
7971      const char *zFile = azArg[1];
7972      output_file_close(p->pLog);
7973      p->pLog = output_file_open(zFile, 0);
7974    }
7975  }else
7976
7977  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
7978    const char *zMode = nArg>=2 ? azArg[1] : "";
7979    int n2 = strlen30(zMode);
7980    int c2 = zMode[0];
7981    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
7982      p->mode = MODE_Line;
7983      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
7984    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
7985      p->mode = MODE_Column;
7986      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
7987    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
7988      p->mode = MODE_List;
7989      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
7990      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
7991    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
7992      p->mode = MODE_Html;
7993    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
7994      p->mode = MODE_Tcl;
7995      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
7996      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
7997    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
7998      p->mode = MODE_Csv;
7999      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8000      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8001    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8002      p->mode = MODE_List;
8003      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8004    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8005      p->mode = MODE_Insert;
8006      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8007    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8008      p->mode = MODE_Quote;
8009    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8010      p->mode = MODE_Ascii;
8011      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8012      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8013    }else if( nArg==1 ){
8014      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8015    }else{
8016      raw_printf(stderr, "Error: mode should be one of: "
8017         "ascii column csv html insert line list quote tabs tcl\n");
8018      rc = 1;
8019    }
8020    p->cMode = p->mode;
8021  }else
8022
8023  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8024    if( nArg==2 ){
8025      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8026                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8027    }else{
8028      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8029      rc = 1;
8030    }
8031  }else
8032
8033  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8034    char *zNewFilename;  /* Name of the database file to open */
8035    int iName = 1;       /* Index in azArg[] of the filename */
8036    int newFlag = 0;     /* True to delete file before opening */
8037    /* Close the existing database */
8038    session_close_all(p);
8039    close_db(p->db);
8040    p->db = 0;
8041    p->zDbFilename = 0;
8042    sqlite3_free(p->zFreeOnClose);
8043    p->zFreeOnClose = 0;
8044    p->openMode = SHELL_OPEN_UNSPEC;
8045    p->szMax = 0;
8046    /* Check for command-line arguments */
8047    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
8048      const char *z = azArg[iName];
8049      if( optionMatch(z,"new") ){
8050        newFlag = 1;
8051#ifdef SQLITE_HAVE_ZLIB
8052      }else if( optionMatch(z, "zip") ){
8053        p->openMode = SHELL_OPEN_ZIPFILE;
8054#endif
8055      }else if( optionMatch(z, "append") ){
8056        p->openMode = SHELL_OPEN_APPENDVFS;
8057      }else if( optionMatch(z, "readonly") ){
8058        p->openMode = SHELL_OPEN_READONLY;
8059#ifdef SQLITE_ENABLE_DESERIALIZE
8060      }else if( optionMatch(z, "deserialize") ){
8061        p->openMode = SHELL_OPEN_DESERIALIZE;
8062      }else if( optionMatch(z, "hexdb") ){
8063        p->openMode = SHELL_OPEN_HEXDB;
8064      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8065        p->szMax = integerValue(azArg[++iName]);
8066#endif /* SQLITE_ENABLE_DESERIALIZE */
8067      }else if( z[0]=='-' ){
8068        utf8_printf(stderr, "unknown option: %s\n", z);
8069        rc = 1;
8070        goto meta_command_exit;
8071      }
8072    }
8073    /* If a filename is specified, try to open it first */
8074    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
8075    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8076      if( newFlag ) shellDeleteFile(zNewFilename);
8077      p->zDbFilename = zNewFilename;
8078      open_db(p, OPEN_DB_KEEPALIVE);
8079      if( p->db==0 ){
8080        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8081        sqlite3_free(zNewFilename);
8082      }else{
8083        p->zFreeOnClose = zNewFilename;
8084      }
8085    }
8086    if( p->db==0 ){
8087      /* As a fall-back open a TEMP database */
8088      p->zDbFilename = 0;
8089      open_db(p, 0);
8090    }
8091  }else
8092
8093  if( (c=='o'
8094        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8095   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8096  ){
8097    const char *zFile = nArg>=2 ? azArg[1] : "stdout";
8098    int bTxtMode = 0;
8099    if( azArg[0][0]=='e' ){
8100      /* Transform the ".excel" command into ".once -x" */
8101      nArg = 2;
8102      azArg[0] = "once";
8103      zFile = azArg[1] = "-x";
8104      n = 4;
8105    }
8106    if( nArg>2 ){
8107      utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
8108      rc = 1;
8109      goto meta_command_exit;
8110    }
8111    if( n>1 && strncmp(azArg[0], "once", n)==0 ){
8112      if( nArg<2 ){
8113        raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
8114        rc = 1;
8115        goto meta_command_exit;
8116      }
8117      p->outCount = 2;
8118    }else{
8119      p->outCount = 0;
8120    }
8121    output_reset(p);
8122    if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
8123#ifndef SQLITE_NOHAVE_SYSTEM
8124    if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
8125      p->doXdgOpen = 1;
8126      outputModePush(p);
8127      if( zFile[1]=='x' ){
8128        newTempFile(p, "csv");
8129        p->mode = MODE_Csv;
8130        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8131        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8132      }else{
8133        newTempFile(p, "txt");
8134        bTxtMode = 1;
8135      }
8136      zFile = p->zTempFile;
8137    }
8138#endif /* SQLITE_NOHAVE_SYSTEM */
8139    if( zFile[0]=='|' ){
8140#ifdef SQLITE_OMIT_POPEN
8141      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8142      rc = 1;
8143      p->out = stdout;
8144#else
8145      p->out = popen(zFile + 1, "w");
8146      if( p->out==0 ){
8147        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8148        p->out = stdout;
8149        rc = 1;
8150      }else{
8151        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8152      }
8153#endif
8154    }else{
8155      p->out = output_file_open(zFile, bTxtMode);
8156      if( p->out==0 ){
8157        if( strcmp(zFile,"off")!=0 ){
8158          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8159        }
8160        p->out = stdout;
8161        rc = 1;
8162      } else {
8163        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8164      }
8165    }
8166  }else
8167
8168  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8169    open_db(p,0);
8170    if( nArg<=1 ) goto parameter_syntax_error;
8171
8172    /* .parameter clear
8173    ** Clear all bind parameters by dropping the TEMP table that holds them.
8174    */
8175    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8176      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8177                   0, 0, 0);
8178    }else
8179
8180    /* .parameter list
8181    ** List all bind parameters.
8182    */
8183    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8184      sqlite3_stmt *pStmt = 0;
8185      int rx;
8186      int len = 0;
8187      rx = sqlite3_prepare_v2(p->db,
8188             "SELECT max(length(key)) "
8189             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8190      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8191        len = sqlite3_column_int(pStmt, 0);
8192        if( len>40 ) len = 40;
8193      }
8194      sqlite3_finalize(pStmt);
8195      pStmt = 0;
8196      if( len ){
8197        rx = sqlite3_prepare_v2(p->db,
8198             "SELECT key, quote(value) "
8199             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8200        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8201          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8202                      sqlite3_column_text(pStmt,1));
8203        }
8204        sqlite3_finalize(pStmt);
8205      }
8206    }else
8207
8208    /* .parameter init
8209    ** Make sure the TEMP table used to hold bind parameters exists.
8210    ** Create it if necessary.
8211    */
8212    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8213      bind_table_init(p);
8214    }else
8215
8216    /* .parameter set NAME VALUE
8217    ** Set or reset a bind parameter.  NAME should be the full parameter
8218    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8219    ** VALUE can be in either SQL literal notation, or if not it will be
8220    ** understood to be a text string.
8221    */
8222    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8223      int rx;
8224      char *zSql;
8225      sqlite3_stmt *pStmt;
8226      const char *zKey = azArg[2];
8227      const char *zValue = azArg[3];
8228      bind_table_init(p);
8229      zSql = sqlite3_mprintf(
8230                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8231                  "VALUES(%Q,%s);", zKey, zValue);
8232      if( zSql==0 ) shell_out_of_memory();
8233      pStmt = 0;
8234      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8235      sqlite3_free(zSql);
8236      if( rx!=SQLITE_OK ){
8237        sqlite3_finalize(pStmt);
8238        pStmt = 0;
8239        zSql = sqlite3_mprintf(
8240                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8241                   "VALUES(%Q,%Q);", zKey, zValue);
8242        if( zSql==0 ) shell_out_of_memory();
8243        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8244        sqlite3_free(zSql);
8245        if( rx!=SQLITE_OK ){
8246          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8247          sqlite3_finalize(pStmt);
8248          pStmt = 0;
8249          rc = 1;
8250        }
8251      }
8252      sqlite3_step(pStmt);
8253      sqlite3_finalize(pStmt);
8254    }else
8255
8256    /* .parameter unset NAME
8257    ** Remove the NAME binding from the parameter binding table, if it
8258    ** exists.
8259    */
8260    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8261      char *zSql = sqlite3_mprintf(
8262          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8263      if( zSql==0 ) shell_out_of_memory();
8264      sqlite3_exec(p->db, zSql, 0, 0, 0);
8265      sqlite3_free(zSql);
8266    }else
8267    /* If no command name matches, show a syntax error */
8268    parameter_syntax_error:
8269    showHelp(p->out, "parameter");
8270  }else
8271
8272  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8273    int i;
8274    for(i=1; i<nArg; i++){
8275      if( i>1 ) raw_printf(p->out, " ");
8276      utf8_printf(p->out, "%s", azArg[i]);
8277    }
8278    raw_printf(p->out, "\n");
8279  }else
8280
8281#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8282  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8283    int i;
8284    int nn = 0;
8285    p->flgProgress = 0;
8286    p->mxProgress = 0;
8287    p->nProgress = 0;
8288    for(i=1; i<nArg; i++){
8289      const char *z = azArg[i];
8290      if( z[0]=='-' ){
8291        z++;
8292        if( z[0]=='-' ) z++;
8293        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
8294          p->flgProgress |= SHELL_PROGRESS_QUIET;
8295          continue;
8296        }
8297        if( strcmp(z,"reset")==0 ){
8298          p->flgProgress |= SHELL_PROGRESS_RESET;
8299          continue;
8300        }
8301        if( strcmp(z,"once")==0 ){
8302          p->flgProgress |= SHELL_PROGRESS_ONCE;
8303          continue;
8304        }
8305        if( strcmp(z,"limit")==0 ){
8306          if( i+1>=nArg ){
8307            utf8_printf(stderr, "Error: missing argument on --limit\n");
8308            rc = 1;
8309            goto meta_command_exit;
8310          }else{
8311            p->mxProgress = (int)integerValue(azArg[++i]);
8312          }
8313          continue;
8314        }
8315        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
8316        rc = 1;
8317        goto meta_command_exit;
8318      }else{
8319        nn = (int)integerValue(z);
8320      }
8321    }
8322    open_db(p, 0);
8323    sqlite3_progress_handler(p->db, nn, progress_handler, p);
8324  }else
8325#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
8326
8327  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
8328    if( nArg >= 2) {
8329      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
8330    }
8331    if( nArg >= 3) {
8332      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
8333    }
8334  }else
8335
8336  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
8337    rc = 2;
8338  }else
8339
8340  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
8341    FILE *inSaved = p->in;
8342    int savedLineno = p->lineno;
8343    if( nArg!=2 ){
8344      raw_printf(stderr, "Usage: .read FILE\n");
8345      rc = 1;
8346      goto meta_command_exit;
8347    }
8348    p->in = fopen(azArg[1], "rb");
8349    if( p->in==0 ){
8350      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
8351      rc = 1;
8352    }else{
8353      rc = process_input(p);
8354      fclose(p->in);
8355    }
8356    p->in = inSaved;
8357    p->lineno = savedLineno;
8358  }else
8359
8360  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
8361    const char *zSrcFile;
8362    const char *zDb;
8363    sqlite3 *pSrc;
8364    sqlite3_backup *pBackup;
8365    int nTimeout = 0;
8366
8367    if( nArg==2 ){
8368      zSrcFile = azArg[1];
8369      zDb = "main";
8370    }else if( nArg==3 ){
8371      zSrcFile = azArg[2];
8372      zDb = azArg[1];
8373    }else{
8374      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
8375      rc = 1;
8376      goto meta_command_exit;
8377    }
8378    rc = sqlite3_open(zSrcFile, &pSrc);
8379    if( rc!=SQLITE_OK ){
8380      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
8381      close_db(pSrc);
8382      return 1;
8383    }
8384    open_db(p, 0);
8385    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
8386    if( pBackup==0 ){
8387      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8388      close_db(pSrc);
8389      return 1;
8390    }
8391    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
8392          || rc==SQLITE_BUSY  ){
8393      if( rc==SQLITE_BUSY ){
8394        if( nTimeout++ >= 3 ) break;
8395        sqlite3_sleep(100);
8396      }
8397    }
8398    sqlite3_backup_finish(pBackup);
8399    if( rc==SQLITE_DONE ){
8400      rc = 0;
8401    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
8402      raw_printf(stderr, "Error: source database is busy\n");
8403      rc = 1;
8404    }else{
8405      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8406      rc = 1;
8407    }
8408    close_db(pSrc);
8409  }else
8410
8411  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
8412    if( nArg==2 ){
8413      p->scanstatsOn = (u8)booleanValue(azArg[1]);
8414#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
8415      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
8416#endif
8417    }else{
8418      raw_printf(stderr, "Usage: .scanstats on|off\n");
8419      rc = 1;
8420    }
8421  }else
8422
8423  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
8424    ShellText sSelect;
8425    ShellState data;
8426    char *zErrMsg = 0;
8427    const char *zDiv = "(";
8428    const char *zName = 0;
8429    int iSchema = 0;
8430    int bDebug = 0;
8431    int ii;
8432
8433    open_db(p, 0);
8434    memcpy(&data, p, sizeof(data));
8435    data.showHeader = 0;
8436    data.cMode = data.mode = MODE_Semi;
8437    initText(&sSelect);
8438    for(ii=1; ii<nArg; ii++){
8439      if( optionMatch(azArg[ii],"indent") ){
8440        data.cMode = data.mode = MODE_Pretty;
8441      }else if( optionMatch(azArg[ii],"debug") ){
8442        bDebug = 1;
8443      }else if( zName==0 ){
8444        zName = azArg[ii];
8445      }else{
8446        raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
8447        rc = 1;
8448        goto meta_command_exit;
8449      }
8450    }
8451    if( zName!=0 ){
8452      int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
8453      if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
8454        char *new_argv[2], *new_colv[2];
8455        new_argv[0] = sqlite3_mprintf(
8456                      "CREATE TABLE %s (\n"
8457                      "  type text,\n"
8458                      "  name text,\n"
8459                      "  tbl_name text,\n"
8460                      "  rootpage integer,\n"
8461                      "  sql text\n"
8462                      ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
8463        new_argv[1] = 0;
8464        new_colv[0] = "sql";
8465        new_colv[1] = 0;
8466        callback(&data, 1, new_argv, new_colv);
8467        sqlite3_free(new_argv[0]);
8468      }
8469    }
8470    if( zDiv ){
8471      sqlite3_stmt *pStmt = 0;
8472      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
8473                              -1, &pStmt, 0);
8474      if( rc ){
8475        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8476        sqlite3_finalize(pStmt);
8477        rc = 1;
8478        goto meta_command_exit;
8479      }
8480      appendText(&sSelect, "SELECT sql FROM", 0);
8481      iSchema = 0;
8482      while( sqlite3_step(pStmt)==SQLITE_ROW ){
8483        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
8484        char zScNum[30];
8485        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
8486        appendText(&sSelect, zDiv, 0);
8487        zDiv = " UNION ALL ";
8488        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
8489        if( sqlite3_stricmp(zDb, "main")!=0 ){
8490          appendText(&sSelect, zDb, '\'');
8491        }else{
8492          appendText(&sSelect, "NULL", 0);
8493        }
8494        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
8495        appendText(&sSelect, zScNum, 0);
8496        appendText(&sSelect, " AS snum, ", 0);
8497        appendText(&sSelect, zDb, '\'');
8498        appendText(&sSelect, " AS sname FROM ", 0);
8499        appendText(&sSelect, zDb, quoteChar(zDb));
8500        appendText(&sSelect, ".sqlite_master", 0);
8501      }
8502      sqlite3_finalize(pStmt);
8503#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
8504      if( zName ){
8505        appendText(&sSelect,
8506           " UNION ALL SELECT shell_module_schema(name),"
8507           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
8508        0);
8509      }
8510#endif
8511      appendText(&sSelect, ") WHERE ", 0);
8512      if( zName ){
8513        char *zQarg = sqlite3_mprintf("%Q", zName);
8514        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
8515                    strchr(zName, '[') != 0;
8516        if( strchr(zName, '.') ){
8517          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
8518        }else{
8519          appendText(&sSelect, "lower(tbl_name)", 0);
8520        }
8521        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
8522        appendText(&sSelect, zQarg, 0);
8523        if( !bGlob ){
8524          appendText(&sSelect, " ESCAPE '\\' ", 0);
8525        }
8526        appendText(&sSelect, " AND ", 0);
8527        sqlite3_free(zQarg);
8528      }
8529      appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
8530                           " ORDER BY snum, rowid", 0);
8531      if( bDebug ){
8532        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
8533      }else{
8534        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
8535      }
8536      freeText(&sSelect);
8537    }
8538    if( zErrMsg ){
8539      utf8_printf(stderr,"Error: %s\n", zErrMsg);
8540      sqlite3_free(zErrMsg);
8541      rc = 1;
8542    }else if( rc != SQLITE_OK ){
8543      raw_printf(stderr,"Error: querying schema information\n");
8544      rc = 1;
8545    }else{
8546      rc = 0;
8547    }
8548  }else
8549
8550#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
8551  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
8552    sqlite3SelectTrace = (int)integerValue(azArg[1]);
8553  }else
8554#endif
8555
8556#if defined(SQLITE_ENABLE_SESSION)
8557  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
8558    OpenSession *pSession = &p->aSession[0];
8559    char **azCmd = &azArg[1];
8560    int iSes = 0;
8561    int nCmd = nArg - 1;
8562    int i;
8563    if( nArg<=1 ) goto session_syntax_error;
8564    open_db(p, 0);
8565    if( nArg>=3 ){
8566      for(iSes=0; iSes<p->nSession; iSes++){
8567        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
8568      }
8569      if( iSes<p->nSession ){
8570        pSession = &p->aSession[iSes];
8571        azCmd++;
8572        nCmd--;
8573      }else{
8574        pSession = &p->aSession[0];
8575        iSes = 0;
8576      }
8577    }
8578
8579    /* .session attach TABLE
8580    ** Invoke the sqlite3session_attach() interface to attach a particular
8581    ** table so that it is never filtered.
8582    */
8583    if( strcmp(azCmd[0],"attach")==0 ){
8584      if( nCmd!=2 ) goto session_syntax_error;
8585      if( pSession->p==0 ){
8586        session_not_open:
8587        raw_printf(stderr, "ERROR: No sessions are open\n");
8588      }else{
8589        rc = sqlite3session_attach(pSession->p, azCmd[1]);
8590        if( rc ){
8591          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
8592          rc = 0;
8593        }
8594      }
8595    }else
8596
8597    /* .session changeset FILE
8598    ** .session patchset FILE
8599    ** Write a changeset or patchset into a file.  The file is overwritten.
8600    */
8601    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
8602      FILE *out = 0;
8603      if( nCmd!=2 ) goto session_syntax_error;
8604      if( pSession->p==0 ) goto session_not_open;
8605      out = fopen(azCmd[1], "wb");
8606      if( out==0 ){
8607        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
8608                    azCmd[1]);
8609      }else{
8610        int szChng;
8611        void *pChng;
8612        if( azCmd[0][0]=='c' ){
8613          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
8614        }else{
8615          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
8616        }
8617        if( rc ){
8618          printf("Error: error code %d\n", rc);
8619          rc = 0;
8620        }
8621        if( pChng
8622          && fwrite(pChng, szChng, 1, out)!=1 ){
8623          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
8624                  szChng);
8625        }
8626        sqlite3_free(pChng);
8627        fclose(out);
8628      }
8629    }else
8630
8631    /* .session close
8632    ** Close the identified session
8633    */
8634    if( strcmp(azCmd[0], "close")==0 ){
8635      if( nCmd!=1 ) goto session_syntax_error;
8636      if( p->nSession ){
8637        session_close(pSession);
8638        p->aSession[iSes] = p->aSession[--p->nSession];
8639      }
8640    }else
8641
8642    /* .session enable ?BOOLEAN?
8643    ** Query or set the enable flag
8644    */
8645    if( strcmp(azCmd[0], "enable")==0 ){
8646      int ii;
8647      if( nCmd>2 ) goto session_syntax_error;
8648      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
8649      if( p->nSession ){
8650        ii = sqlite3session_enable(pSession->p, ii);
8651        utf8_printf(p->out, "session %s enable flag = %d\n",
8652                    pSession->zName, ii);
8653      }
8654    }else
8655
8656    /* .session filter GLOB ....
8657    ** Set a list of GLOB patterns of table names to be excluded.
8658    */
8659    if( strcmp(azCmd[0], "filter")==0 ){
8660      int ii, nByte;
8661      if( nCmd<2 ) goto session_syntax_error;
8662      if( p->nSession ){
8663        for(ii=0; ii<pSession->nFilter; ii++){
8664          sqlite3_free(pSession->azFilter[ii]);
8665        }
8666        sqlite3_free(pSession->azFilter);
8667        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
8668        pSession->azFilter = sqlite3_malloc( nByte );
8669        if( pSession->azFilter==0 ){
8670          raw_printf(stderr, "Error: out or memory\n");
8671          exit(1);
8672        }
8673        for(ii=1; ii<nCmd; ii++){
8674          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
8675        }
8676        pSession->nFilter = ii-1;
8677      }
8678    }else
8679
8680    /* .session indirect ?BOOLEAN?
8681    ** Query or set the indirect flag
8682    */
8683    if( strcmp(azCmd[0], "indirect")==0 ){
8684      int ii;
8685      if( nCmd>2 ) goto session_syntax_error;
8686      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
8687      if( p->nSession ){
8688        ii = sqlite3session_indirect(pSession->p, ii);
8689        utf8_printf(p->out, "session %s indirect flag = %d\n",
8690                    pSession->zName, ii);
8691      }
8692    }else
8693
8694    /* .session isempty
8695    ** Determine if the session is empty
8696    */
8697    if( strcmp(azCmd[0], "isempty")==0 ){
8698      int ii;
8699      if( nCmd!=1 ) goto session_syntax_error;
8700      if( p->nSession ){
8701        ii = sqlite3session_isempty(pSession->p);
8702        utf8_printf(p->out, "session %s isempty flag = %d\n",
8703                    pSession->zName, ii);
8704      }
8705    }else
8706
8707    /* .session list
8708    ** List all currently open sessions
8709    */
8710    if( strcmp(azCmd[0],"list")==0 ){
8711      for(i=0; i<p->nSession; i++){
8712        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
8713      }
8714    }else
8715
8716    /* .session open DB NAME
8717    ** Open a new session called NAME on the attached database DB.
8718    ** DB is normally "main".
8719    */
8720    if( strcmp(azCmd[0],"open")==0 ){
8721      char *zName;
8722      if( nCmd!=3 ) goto session_syntax_error;
8723      zName = azCmd[2];
8724      if( zName[0]==0 ) goto session_syntax_error;
8725      for(i=0; i<p->nSession; i++){
8726        if( strcmp(p->aSession[i].zName,zName)==0 ){
8727          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
8728          goto meta_command_exit;
8729        }
8730      }
8731      if( p->nSession>=ArraySize(p->aSession) ){
8732        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
8733        goto meta_command_exit;
8734      }
8735      pSession = &p->aSession[p->nSession];
8736      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
8737      if( rc ){
8738        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
8739        rc = 0;
8740        goto meta_command_exit;
8741      }
8742      pSession->nFilter = 0;
8743      sqlite3session_table_filter(pSession->p, session_filter, pSession);
8744      p->nSession++;
8745      pSession->zName = sqlite3_mprintf("%s", zName);
8746    }else
8747    /* If no command name matches, show a syntax error */
8748    session_syntax_error:
8749    showHelp(p->out, "session");
8750  }else
8751#endif
8752
8753#ifdef SQLITE_DEBUG
8754  /* Undocumented commands for internal testing.  Subject to change
8755  ** without notice. */
8756  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
8757    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
8758      int i, v;
8759      for(i=1; i<nArg; i++){
8760        v = booleanValue(azArg[i]);
8761        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
8762      }
8763    }
8764    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
8765      int i; sqlite3_int64 v;
8766      for(i=1; i<nArg; i++){
8767        char zBuf[200];
8768        v = integerValue(azArg[i]);
8769        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
8770        utf8_printf(p->out, "%s", zBuf);
8771      }
8772    }
8773  }else
8774#endif
8775
8776  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
8777    int bIsInit = 0;         /* True to initialize the SELFTEST table */
8778    int bVerbose = 0;        /* Verbose output */
8779    int bSelftestExists;     /* True if SELFTEST already exists */
8780    int i, k;                /* Loop counters */
8781    int nTest = 0;           /* Number of tests runs */
8782    int nErr = 0;            /* Number of errors seen */
8783    ShellText str;           /* Answer for a query */
8784    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
8785
8786    open_db(p,0);
8787    for(i=1; i<nArg; i++){
8788      const char *z = azArg[i];
8789      if( z[0]=='-' && z[1]=='-' ) z++;
8790      if( strcmp(z,"-init")==0 ){
8791        bIsInit = 1;
8792      }else
8793      if( strcmp(z,"-v")==0 ){
8794        bVerbose++;
8795      }else
8796      {
8797        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
8798                    azArg[i], azArg[0]);
8799        raw_printf(stderr, "Should be one of: --init -v\n");
8800        rc = 1;
8801        goto meta_command_exit;
8802      }
8803    }
8804    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
8805           != SQLITE_OK ){
8806      bSelftestExists = 0;
8807    }else{
8808      bSelftestExists = 1;
8809    }
8810    if( bIsInit ){
8811      createSelftestTable(p);
8812      bSelftestExists = 1;
8813    }
8814    initText(&str);
8815    appendText(&str, "x", 0);
8816    for(k=bSelftestExists; k>=0; k--){
8817      if( k==1 ){
8818        rc = sqlite3_prepare_v2(p->db,
8819            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
8820            -1, &pStmt, 0);
8821      }else{
8822        rc = sqlite3_prepare_v2(p->db,
8823          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
8824          "      (1,'run','PRAGMA integrity_check','ok')",
8825          -1, &pStmt, 0);
8826      }
8827      if( rc ){
8828        raw_printf(stderr, "Error querying the selftest table\n");
8829        rc = 1;
8830        sqlite3_finalize(pStmt);
8831        goto meta_command_exit;
8832      }
8833      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
8834        int tno = sqlite3_column_int(pStmt, 0);
8835        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
8836        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
8837        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
8838
8839        k = 0;
8840        if( bVerbose>0 ){
8841          char *zQuote = sqlite3_mprintf("%q", zSql);
8842          printf("%d: %s %s\n", tno, zOp, zSql);
8843          sqlite3_free(zQuote);
8844        }
8845        if( strcmp(zOp,"memo")==0 ){
8846          utf8_printf(p->out, "%s\n", zSql);
8847        }else
8848        if( strcmp(zOp,"run")==0 ){
8849          char *zErrMsg = 0;
8850          str.n = 0;
8851          str.z[0] = 0;
8852          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
8853          nTest++;
8854          if( bVerbose ){
8855            utf8_printf(p->out, "Result: %s\n", str.z);
8856          }
8857          if( rc || zErrMsg ){
8858            nErr++;
8859            rc = 1;
8860            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
8861            sqlite3_free(zErrMsg);
8862          }else if( strcmp(zAns,str.z)!=0 ){
8863            nErr++;
8864            rc = 1;
8865            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
8866            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
8867          }
8868        }else
8869        {
8870          utf8_printf(stderr,
8871            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
8872          rc = 1;
8873          break;
8874        }
8875      } /* End loop over rows of content from SELFTEST */
8876      sqlite3_finalize(pStmt);
8877    } /* End loop over k */
8878    freeText(&str);
8879    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
8880  }else
8881
8882  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
8883    if( nArg<2 || nArg>3 ){
8884      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
8885      rc = 1;
8886    }
8887    if( nArg>=2 ){
8888      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
8889                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
8890    }
8891    if( nArg>=3 ){
8892      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
8893                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
8894    }
8895  }else
8896
8897  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
8898    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
8899    int i;                   /* Loop counter */
8900    int bSchema = 0;         /* Also hash the schema */
8901    int bSeparate = 0;       /* Hash each table separately */
8902    int iSize = 224;         /* Hash algorithm to use */
8903    int bDebug = 0;          /* Only show the query that would have run */
8904    sqlite3_stmt *pStmt;     /* For querying tables names */
8905    char *zSql;              /* SQL to be run */
8906    char *zSep;              /* Separator */
8907    ShellText sSql;          /* Complete SQL for the query to run the hash */
8908    ShellText sQuery;        /* Set of queries used to read all content */
8909    open_db(p, 0);
8910    for(i=1; i<nArg; i++){
8911      const char *z = azArg[i];
8912      if( z[0]=='-' ){
8913        z++;
8914        if( z[0]=='-' ) z++;
8915        if( strcmp(z,"schema")==0 ){
8916          bSchema = 1;
8917        }else
8918        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
8919         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
8920        ){
8921          iSize = atoi(&z[5]);
8922        }else
8923        if( strcmp(z,"debug")==0 ){
8924          bDebug = 1;
8925        }else
8926        {
8927          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
8928                      azArg[i], azArg[0]);
8929          showHelp(p->out, azArg[0]);
8930          rc = 1;
8931          goto meta_command_exit;
8932        }
8933      }else if( zLike ){
8934        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
8935        rc = 1;
8936        goto meta_command_exit;
8937      }else{
8938        zLike = z;
8939        bSeparate = 1;
8940        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
8941      }
8942    }
8943    if( bSchema ){
8944      zSql = "SELECT lower(name) FROM sqlite_master"
8945             " WHERE type='table' AND coalesce(rootpage,0)>1"
8946             " UNION ALL SELECT 'sqlite_master'"
8947             " ORDER BY 1 collate nocase";
8948    }else{
8949      zSql = "SELECT lower(name) FROM sqlite_master"
8950             " WHERE type='table' AND coalesce(rootpage,0)>1"
8951             " AND name NOT LIKE 'sqlite_%'"
8952             " ORDER BY 1 collate nocase";
8953    }
8954    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8955    initText(&sQuery);
8956    initText(&sSql);
8957    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
8958    zSep = "VALUES(";
8959    while( SQLITE_ROW==sqlite3_step(pStmt) ){
8960      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
8961      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
8962      if( strncmp(zTab, "sqlite_",7)!=0 ){
8963        appendText(&sQuery,"SELECT * FROM ", 0);
8964        appendText(&sQuery,zTab,'"');
8965        appendText(&sQuery," NOT INDEXED;", 0);
8966      }else if( strcmp(zTab, "sqlite_master")==0 ){
8967        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
8968                           " ORDER BY name;", 0);
8969      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
8970        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
8971                           " ORDER BY name;", 0);
8972      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
8973        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
8974                           " ORDER BY tbl,idx;", 0);
8975      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
8976        appendText(&sQuery, "SELECT * FROM ", 0);
8977        appendText(&sQuery, zTab, 0);
8978        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
8979      }
8980      appendText(&sSql, zSep, 0);
8981      appendText(&sSql, sQuery.z, '\'');
8982      sQuery.n = 0;
8983      appendText(&sSql, ",", 0);
8984      appendText(&sSql, zTab, '\'');
8985      zSep = "),(";
8986    }
8987    sqlite3_finalize(pStmt);
8988    if( bSeparate ){
8989      zSql = sqlite3_mprintf(
8990          "%s))"
8991          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
8992          "   FROM [sha3sum$query]",
8993          sSql.z, iSize);
8994    }else{
8995      zSql = sqlite3_mprintf(
8996          "%s))"
8997          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
8998          "   FROM [sha3sum$query]",
8999          sSql.z, iSize);
9000    }
9001    freeText(&sQuery);
9002    freeText(&sSql);
9003    if( bDebug ){
9004      utf8_printf(p->out, "%s\n", zSql);
9005    }else{
9006      shell_exec(p, zSql, 0);
9007    }
9008    sqlite3_free(zSql);
9009  }else
9010
9011#ifndef SQLITE_NOHAVE_SYSTEM
9012  if( c=='s'
9013   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9014  ){
9015    char *zCmd;
9016    int i, x;
9017    if( nArg<2 ){
9018      raw_printf(stderr, "Usage: .system COMMAND\n");
9019      rc = 1;
9020      goto meta_command_exit;
9021    }
9022    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9023    for(i=2; i<nArg; i++){
9024      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9025                             zCmd, azArg[i]);
9026    }
9027    x = system(zCmd);
9028    sqlite3_free(zCmd);
9029    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9030  }else
9031#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9032
9033  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9034    static const char *azBool[] = { "off", "on", "trigger", "full"};
9035    int i;
9036    if( nArg!=1 ){
9037      raw_printf(stderr, "Usage: .show\n");
9038      rc = 1;
9039      goto meta_command_exit;
9040    }
9041    utf8_printf(p->out, "%12.12s: %s\n","echo",
9042                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9043    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9044    utf8_printf(p->out, "%12.12s: %s\n","explain",
9045         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9046    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9047    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9048    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9049      output_c_string(p->out, p->nullValue);
9050      raw_printf(p->out, "\n");
9051    utf8_printf(p->out,"%12.12s: %s\n","output",
9052            strlen30(p->outfile) ? p->outfile : "stdout");
9053    utf8_printf(p->out,"%12.12s: ", "colseparator");
9054      output_c_string(p->out, p->colSeparator);
9055      raw_printf(p->out, "\n");
9056    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9057      output_c_string(p->out, p->rowSeparator);
9058      raw_printf(p->out, "\n");
9059    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
9060    utf8_printf(p->out, "%12.12s: ", "width");
9061    for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
9062      raw_printf(p->out, "%d ", p->colWidth[i]);
9063    }
9064    raw_printf(p->out, "\n");
9065    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9066                p->zDbFilename ? p->zDbFilename : "");
9067  }else
9068
9069  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9070    if( nArg==2 ){
9071      p->statsOn = (u8)booleanValue(azArg[1]);
9072    }else if( nArg==1 ){
9073      display_stats(p->db, p, 0);
9074    }else{
9075      raw_printf(stderr, "Usage: .stats ?on|off?\n");
9076      rc = 1;
9077    }
9078  }else
9079
9080  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9081   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9082                 || strncmp(azArg[0], "indexes", n)==0) )
9083  ){
9084    sqlite3_stmt *pStmt;
9085    char **azResult;
9086    int nRow, nAlloc;
9087    int ii;
9088    ShellText s;
9089    initText(&s);
9090    open_db(p, 0);
9091    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9092    if( rc ){
9093      sqlite3_finalize(pStmt);
9094      return shellDatabaseError(p->db);
9095    }
9096
9097    if( nArg>2 && c=='i' ){
9098      /* It is an historical accident that the .indexes command shows an error
9099      ** when called with the wrong number of arguments whereas the .tables
9100      ** command does not. */
9101      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9102      rc = 1;
9103      sqlite3_finalize(pStmt);
9104      goto meta_command_exit;
9105    }
9106    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9107      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9108      if( zDbName==0 ) continue;
9109      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9110      if( sqlite3_stricmp(zDbName, "main")==0 ){
9111        appendText(&s, "SELECT name FROM ", 0);
9112      }else{
9113        appendText(&s, "SELECT ", 0);
9114        appendText(&s, zDbName, '\'');
9115        appendText(&s, "||'.'||name FROM ", 0);
9116      }
9117      appendText(&s, zDbName, '"');
9118      appendText(&s, ".sqlite_master ", 0);
9119      if( c=='t' ){
9120        appendText(&s," WHERE type IN ('table','view')"
9121                      "   AND name NOT LIKE 'sqlite_%'"
9122                      "   AND name LIKE ?1", 0);
9123      }else{
9124        appendText(&s," WHERE type='index'"
9125                      "   AND tbl_name LIKE ?1", 0);
9126      }
9127    }
9128    rc = sqlite3_finalize(pStmt);
9129    appendText(&s, " ORDER BY 1", 0);
9130    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9131    freeText(&s);
9132    if( rc ) return shellDatabaseError(p->db);
9133
9134    /* Run the SQL statement prepared by the above block. Store the results
9135    ** as an array of nul-terminated strings in azResult[].  */
9136    nRow = nAlloc = 0;
9137    azResult = 0;
9138    if( nArg>1 ){
9139      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9140    }else{
9141      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9142    }
9143    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9144      if( nRow>=nAlloc ){
9145        char **azNew;
9146        int n2 = nAlloc*2 + 10;
9147        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9148        if( azNew==0 ) shell_out_of_memory();
9149        nAlloc = n2;
9150        azResult = azNew;
9151      }
9152      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9153      if( 0==azResult[nRow] ) shell_out_of_memory();
9154      nRow++;
9155    }
9156    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9157      rc = shellDatabaseError(p->db);
9158    }
9159
9160    /* Pretty-print the contents of array azResult[] to the output */
9161    if( rc==0 && nRow>0 ){
9162      int len, maxlen = 0;
9163      int i, j;
9164      int nPrintCol, nPrintRow;
9165      for(i=0; i<nRow; i++){
9166        len = strlen30(azResult[i]);
9167        if( len>maxlen ) maxlen = len;
9168      }
9169      nPrintCol = 80/(maxlen+2);
9170      if( nPrintCol<1 ) nPrintCol = 1;
9171      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9172      for(i=0; i<nPrintRow; i++){
9173        for(j=i; j<nRow; j+=nPrintRow){
9174          char *zSp = j<nPrintRow ? "" : "  ";
9175          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9176                      azResult[j] ? azResult[j]:"");
9177        }
9178        raw_printf(p->out, "\n");
9179      }
9180    }
9181
9182    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9183    sqlite3_free(azResult);
9184  }else
9185
9186  /* Begin redirecting output to the file "testcase-out.txt" */
9187  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9188    output_reset(p);
9189    p->out = output_file_open("testcase-out.txt", 0);
9190    if( p->out==0 ){
9191      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9192    }
9193    if( nArg>=2 ){
9194      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9195    }else{
9196      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9197    }
9198  }else
9199
9200#ifndef SQLITE_UNTESTABLE
9201  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9202    static const struct {
9203       const char *zCtrlName;   /* Name of a test-control option */
9204       int ctrlCode;            /* Integer code for that option */
9205       const char *zUsage;      /* Usage notes */
9206    } aCtrl[] = {
9207      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9208      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9209    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9210    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9211      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9212      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9213    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9214      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9215      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN"   },
9216      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9217      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9218      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9219#ifdef YYCOVERAGE
9220      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9221#endif
9222      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9223      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9224      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9225      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9226      { "reserve",            SQLITE_TESTCTRL_RESERVE,      "BYTES-OF-RESERVE"},
9227    };
9228    int testctrl = -1;
9229    int iCtrl = -1;
9230    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9231    int isOk = 0;
9232    int i, n2;
9233    const char *zCmd = 0;
9234
9235    open_db(p, 0);
9236    zCmd = nArg>=2 ? azArg[1] : "help";
9237
9238    /* The argument can optionally begin with "-" or "--" */
9239    if( zCmd[0]=='-' && zCmd[1] ){
9240      zCmd++;
9241      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9242    }
9243
9244    /* --help lists all test-controls */
9245    if( strcmp(zCmd,"help")==0 ){
9246      utf8_printf(p->out, "Available test-controls:\n");
9247      for(i=0; i<ArraySize(aCtrl); i++){
9248        utf8_printf(p->out, "  .testctrl %s %s\n",
9249                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
9250      }
9251      rc = 1;
9252      goto meta_command_exit;
9253    }
9254
9255    /* convert testctrl text option to value. allow any unique prefix
9256    ** of the option name, or a numerical value. */
9257    n2 = strlen30(zCmd);
9258    for(i=0; i<ArraySize(aCtrl); i++){
9259      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
9260        if( testctrl<0 ){
9261          testctrl = aCtrl[i].ctrlCode;
9262          iCtrl = i;
9263        }else{
9264          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
9265                              "Use \".testctrl --help\" for help\n", zCmd);
9266          rc = 1;
9267          goto meta_command_exit;
9268        }
9269      }
9270    }
9271    if( testctrl<0 ){
9272      utf8_printf(stderr,"Error: unknown test-control: %s\n"
9273                         "Use \".testctrl --help\" for help\n", zCmd);
9274    }else{
9275      switch(testctrl){
9276
9277        /* sqlite3_test_control(int, db, int) */
9278        case SQLITE_TESTCTRL_OPTIMIZATIONS:
9279        case SQLITE_TESTCTRL_RESERVE:
9280          if( nArg==3 ){
9281            int opt = (int)strtol(azArg[2], 0, 0);
9282            rc2 = sqlite3_test_control(testctrl, p->db, opt);
9283            isOk = 3;
9284          }
9285          break;
9286
9287        /* sqlite3_test_control(int) */
9288        case SQLITE_TESTCTRL_PRNG_SAVE:
9289        case SQLITE_TESTCTRL_PRNG_RESTORE:
9290        case SQLITE_TESTCTRL_PRNG_RESET:
9291        case SQLITE_TESTCTRL_BYTEORDER:
9292          if( nArg==2 ){
9293            rc2 = sqlite3_test_control(testctrl);
9294            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
9295          }
9296          break;
9297
9298        /* sqlite3_test_control(int, uint) */
9299        case SQLITE_TESTCTRL_PENDING_BYTE:
9300          if( nArg==3 ){
9301            unsigned int opt = (unsigned int)integerValue(azArg[2]);
9302            rc2 = sqlite3_test_control(testctrl, opt);
9303            isOk = 3;
9304          }
9305          break;
9306
9307        /* sqlite3_test_control(int, int, sqlite3*) */
9308        case SQLITE_TESTCTRL_PRNG_SEED:
9309          if( nArg==3 || nArg==4 ){
9310            int ii = (int)integerValue(azArg[2]);
9311            sqlite3 *db;
9312            if( ii==0 && strcmp(azArg[2],"random")==0 ){
9313              sqlite3_randomness(sizeof(ii),&ii);
9314              printf("-- random seed: %d\n", ii);
9315            }
9316            if( nArg==3 ){
9317              db = 0;
9318            }else{
9319              db = p->db;
9320              /* Make sure the schema has been loaded */
9321              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
9322            }
9323            rc2 = sqlite3_test_control(testctrl, ii, db);
9324            isOk = 3;
9325          }
9326          break;
9327
9328        /* sqlite3_test_control(int, int) */
9329        case SQLITE_TESTCTRL_ASSERT:
9330        case SQLITE_TESTCTRL_ALWAYS:
9331        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
9332          if( nArg==3 ){
9333            int opt = booleanValue(azArg[2]);
9334            rc2 = sqlite3_test_control(testctrl, opt);
9335            isOk = 1;
9336          }
9337          break;
9338
9339        /* sqlite3_test_control(int, int) */
9340        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
9341        case SQLITE_TESTCTRL_NEVER_CORRUPT:
9342          if( nArg==3 ){
9343            int opt = booleanValue(azArg[2]);
9344            rc2 = sqlite3_test_control(testctrl, opt);
9345            isOk = 3;
9346          }
9347          break;
9348
9349        case SQLITE_TESTCTRL_IMPOSTER:
9350          if( nArg==5 ){
9351            rc2 = sqlite3_test_control(testctrl, p->db,
9352                          azArg[2],
9353                          integerValue(azArg[3]),
9354                          integerValue(azArg[4]));
9355            isOk = 3;
9356          }
9357          break;
9358
9359#ifdef YYCOVERAGE
9360        case SQLITE_TESTCTRL_PARSER_COVERAGE:
9361          if( nArg==2 ){
9362            sqlite3_test_control(testctrl, p->out);
9363            isOk = 3;
9364          }
9365#endif
9366      }
9367    }
9368    if( isOk==0 && iCtrl>=0 ){
9369      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
9370      rc = 1;
9371    }else if( isOk==1 ){
9372      raw_printf(p->out, "%d\n", rc2);
9373    }else if( isOk==2 ){
9374      raw_printf(p->out, "0x%08x\n", rc2);
9375    }
9376  }else
9377#endif /* !defined(SQLITE_UNTESTABLE) */
9378
9379  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
9380    open_db(p, 0);
9381    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
9382  }else
9383
9384  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
9385    if( nArg==2 ){
9386      enableTimer = booleanValue(azArg[1]);
9387      if( enableTimer && !HAS_TIMER ){
9388        raw_printf(stderr, "Error: timer not available on this system.\n");
9389        enableTimer = 0;
9390      }
9391    }else{
9392      raw_printf(stderr, "Usage: .timer on|off\n");
9393      rc = 1;
9394    }
9395  }else
9396
9397#ifndef SQLITE_OMIT_TRACE
9398  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
9399    int mType = 0;
9400    int jj;
9401    open_db(p, 0);
9402    for(jj=1; jj<nArg; jj++){
9403      const char *z = azArg[jj];
9404      if( z[0]=='-' ){
9405        if( optionMatch(z, "expanded") ){
9406          p->eTraceType = SHELL_TRACE_EXPANDED;
9407        }
9408#ifdef SQLITE_ENABLE_NORMALIZE
9409        else if( optionMatch(z, "normalized") ){
9410          p->eTraceType = SHELL_TRACE_NORMALIZED;
9411        }
9412#endif
9413        else if( optionMatch(z, "plain") ){
9414          p->eTraceType = SHELL_TRACE_PLAIN;
9415        }
9416        else if( optionMatch(z, "profile") ){
9417          mType |= SQLITE_TRACE_PROFILE;
9418        }
9419        else if( optionMatch(z, "row") ){
9420          mType |= SQLITE_TRACE_ROW;
9421        }
9422        else if( optionMatch(z, "stmt") ){
9423          mType |= SQLITE_TRACE_STMT;
9424        }
9425        else if( optionMatch(z, "close") ){
9426          mType |= SQLITE_TRACE_CLOSE;
9427        }
9428        else {
9429          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
9430          rc = 1;
9431          goto meta_command_exit;
9432        }
9433      }else{
9434        output_file_close(p->traceOut);
9435        p->traceOut = output_file_open(azArg[1], 0);
9436      }
9437    }
9438    if( p->traceOut==0 ){
9439      sqlite3_trace_v2(p->db, 0, 0, 0);
9440    }else{
9441      if( mType==0 ) mType = SQLITE_TRACE_STMT;
9442      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
9443    }
9444  }else
9445#endif /* !defined(SQLITE_OMIT_TRACE) */
9446
9447#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
9448  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
9449    int ii;
9450    int lenOpt;
9451    char *zOpt;
9452    if( nArg<2 ){
9453      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
9454      rc = 1;
9455      goto meta_command_exit;
9456    }
9457    open_db(p, 0);
9458    zOpt = azArg[1];
9459    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
9460    lenOpt = (int)strlen(zOpt);
9461    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
9462      assert( azArg[nArg]==0 );
9463      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
9464    }else{
9465      for(ii=1; ii<nArg; ii++){
9466        sqlite3_create_module(p->db, azArg[ii], 0, 0);
9467      }
9468    }
9469  }else
9470#endif
9471
9472#if SQLITE_USER_AUTHENTICATION
9473  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
9474    if( nArg<2 ){
9475      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
9476      rc = 1;
9477      goto meta_command_exit;
9478    }
9479    open_db(p, 0);
9480    if( strcmp(azArg[1],"login")==0 ){
9481      if( nArg!=4 ){
9482        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
9483        rc = 1;
9484        goto meta_command_exit;
9485      }
9486      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
9487                                     strlen30(azArg[3]));
9488      if( rc ){
9489        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
9490        rc = 1;
9491      }
9492    }else if( strcmp(azArg[1],"add")==0 ){
9493      if( nArg!=5 ){
9494        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
9495        rc = 1;
9496        goto meta_command_exit;
9497      }
9498      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
9499                            booleanValue(azArg[4]));
9500      if( rc ){
9501        raw_printf(stderr, "User-Add failed: %d\n", rc);
9502        rc = 1;
9503      }
9504    }else if( strcmp(azArg[1],"edit")==0 ){
9505      if( nArg!=5 ){
9506        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
9507        rc = 1;
9508        goto meta_command_exit;
9509      }
9510      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
9511                              booleanValue(azArg[4]));
9512      if( rc ){
9513        raw_printf(stderr, "User-Edit failed: %d\n", rc);
9514        rc = 1;
9515      }
9516    }else if( strcmp(azArg[1],"delete")==0 ){
9517      if( nArg!=3 ){
9518        raw_printf(stderr, "Usage: .user delete USER\n");
9519        rc = 1;
9520        goto meta_command_exit;
9521      }
9522      rc = sqlite3_user_delete(p->db, azArg[2]);
9523      if( rc ){
9524        raw_printf(stderr, "User-Delete failed: %d\n", rc);
9525        rc = 1;
9526      }
9527    }else{
9528      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
9529      rc = 1;
9530      goto meta_command_exit;
9531    }
9532  }else
9533#endif /* SQLITE_USER_AUTHENTICATION */
9534
9535  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
9536    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
9537        sqlite3_libversion(), sqlite3_sourceid());
9538#if SQLITE_HAVE_ZLIB
9539    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
9540#endif
9541#define CTIMEOPT_VAL_(opt) #opt
9542#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
9543#if defined(__clang__) && defined(__clang_major__)
9544    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
9545                    CTIMEOPT_VAL(__clang_minor__) "."
9546                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
9547#elif defined(_MSC_VER)
9548    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
9549#elif defined(__GNUC__) && defined(__VERSION__)
9550    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
9551#endif
9552  }else
9553
9554  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
9555    const char *zDbName = nArg==2 ? azArg[1] : "main";
9556    sqlite3_vfs *pVfs = 0;
9557    if( p->db ){
9558      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
9559      if( pVfs ){
9560        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
9561        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
9562        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
9563        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
9564      }
9565    }
9566  }else
9567
9568  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
9569    sqlite3_vfs *pVfs;
9570    sqlite3_vfs *pCurrent = 0;
9571    if( p->db ){
9572      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
9573    }
9574    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
9575      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
9576           pVfs==pCurrent ? "  <--- CURRENT" : "");
9577      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
9578      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
9579      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
9580      if( pVfs->pNext ){
9581        raw_printf(p->out, "-----------------------------------\n");
9582      }
9583    }
9584  }else
9585
9586  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
9587    const char *zDbName = nArg==2 ? azArg[1] : "main";
9588    char *zVfsName = 0;
9589    if( p->db ){
9590      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
9591      if( zVfsName ){
9592        utf8_printf(p->out, "%s\n", zVfsName);
9593        sqlite3_free(zVfsName);
9594      }
9595    }
9596  }else
9597
9598#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
9599  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
9600    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
9601  }else
9602#endif
9603
9604  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
9605    int j;
9606    assert( nArg<=ArraySize(azArg) );
9607    for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
9608      p->colWidth[j-1] = (int)integerValue(azArg[j]);
9609    }
9610  }else
9611
9612  {
9613    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
9614      " \"%s\". Enter \".help\" for help\n", azArg[0]);
9615    rc = 1;
9616  }
9617
9618meta_command_exit:
9619  if( p->outCount ){
9620    p->outCount--;
9621    if( p->outCount==0 ) output_reset(p);
9622  }
9623  return rc;
9624}
9625
9626/*
9627** Return TRUE if a semicolon occurs anywhere in the first N characters
9628** of string z[].
9629*/
9630static int line_contains_semicolon(const char *z, int N){
9631  int i;
9632  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
9633  return 0;
9634}
9635
9636/*
9637** Test to see if a line consists entirely of whitespace.
9638*/
9639static int _all_whitespace(const char *z){
9640  for(; *z; z++){
9641    if( IsSpace(z[0]) ) continue;
9642    if( *z=='/' && z[1]=='*' ){
9643      z += 2;
9644      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
9645      if( *z==0 ) return 0;
9646      z++;
9647      continue;
9648    }
9649    if( *z=='-' && z[1]=='-' ){
9650      z += 2;
9651      while( *z && *z!='\n' ){ z++; }
9652      if( *z==0 ) return 1;
9653      continue;
9654    }
9655    return 0;
9656  }
9657  return 1;
9658}
9659
9660/*
9661** Return TRUE if the line typed in is an SQL command terminator other
9662** than a semi-colon.  The SQL Server style "go" command is understood
9663** as is the Oracle "/".
9664*/
9665static int line_is_command_terminator(const char *zLine){
9666  while( IsSpace(zLine[0]) ){ zLine++; };
9667  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
9668    return 1;  /* Oracle */
9669  }
9670  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
9671         && _all_whitespace(&zLine[2]) ){
9672    return 1;  /* SQL Server */
9673  }
9674  return 0;
9675}
9676
9677/*
9678** We need a default sqlite3_complete() implementation to use in case
9679** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
9680** any arbitrary text is a complete SQL statement.  This is not very
9681** user-friendly, but it does seem to work.
9682*/
9683#ifdef SQLITE_OMIT_COMPLETE
9684#define sqlite3_complete(x) 1
9685#endif
9686
9687/*
9688** Return true if zSql is a complete SQL statement.  Return false if it
9689** ends in the middle of a string literal or C-style comment.
9690*/
9691static int line_is_complete(char *zSql, int nSql){
9692  int rc;
9693  if( zSql==0 ) return 1;
9694  zSql[nSql] = ';';
9695  zSql[nSql+1] = 0;
9696  rc = sqlite3_complete(zSql);
9697  zSql[nSql] = 0;
9698  return rc;
9699}
9700
9701/*
9702** Run a single line of SQL.  Return the number of errors.
9703*/
9704static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
9705  int rc;
9706  char *zErrMsg = 0;
9707
9708  open_db(p, 0);
9709  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
9710  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
9711  BEGIN_TIMER;
9712  rc = shell_exec(p, zSql, &zErrMsg);
9713  END_TIMER;
9714  if( rc || zErrMsg ){
9715    char zPrefix[100];
9716    if( in!=0 || !stdin_is_interactive ){
9717      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
9718                       "Error: near line %d:", startline);
9719    }else{
9720      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
9721    }
9722    if( zErrMsg!=0 ){
9723      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
9724      sqlite3_free(zErrMsg);
9725      zErrMsg = 0;
9726    }else{
9727      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
9728    }
9729    return 1;
9730  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
9731    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
9732            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
9733  }
9734  return 0;
9735}
9736
9737
9738/*
9739** Read input from *in and process it.  If *in==0 then input
9740** is interactive - the user is typing it it.  Otherwise, input
9741** is coming from a file or device.  A prompt is issued and history
9742** is saved only if input is interactive.  An interrupt signal will
9743** cause this routine to exit immediately, unless input is interactive.
9744**
9745** Return the number of errors.
9746*/
9747static int process_input(ShellState *p){
9748  char *zLine = 0;          /* A single input line */
9749  char *zSql = 0;           /* Accumulated SQL text */
9750  int nLine;                /* Length of current line */
9751  int nSql = 0;             /* Bytes of zSql[] used */
9752  int nAlloc = 0;           /* Allocated zSql[] space */
9753  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
9754  int rc;                   /* Error code */
9755  int errCnt = 0;           /* Number of errors seen */
9756  int startline = 0;        /* Line number for start of current input */
9757
9758  p->lineno = 0;
9759  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
9760    fflush(p->out);
9761    zLine = one_input_line(p->in, zLine, nSql>0);
9762    if( zLine==0 ){
9763      /* End of input */
9764      if( p->in==0 && stdin_is_interactive ) printf("\n");
9765      break;
9766    }
9767    if( seenInterrupt ){
9768      if( p->in!=0 ) break;
9769      seenInterrupt = 0;
9770    }
9771    p->lineno++;
9772    if( nSql==0 && _all_whitespace(zLine) ){
9773      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
9774      continue;
9775    }
9776    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
9777      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
9778      if( zLine[0]=='.' ){
9779        rc = do_meta_command(zLine, p);
9780        if( rc==2 ){ /* exit requested */
9781          break;
9782        }else if( rc ){
9783          errCnt++;
9784        }
9785      }
9786      continue;
9787    }
9788    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
9789      memcpy(zLine,";",2);
9790    }
9791    nLine = strlen30(zLine);
9792    if( nSql+nLine+2>=nAlloc ){
9793      nAlloc = nSql+nLine+100;
9794      zSql = realloc(zSql, nAlloc);
9795      if( zSql==0 ) shell_out_of_memory();
9796    }
9797    nSqlPrior = nSql;
9798    if( nSql==0 ){
9799      int i;
9800      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
9801      assert( nAlloc>0 && zSql!=0 );
9802      memcpy(zSql, zLine+i, nLine+1-i);
9803      startline = p->lineno;
9804      nSql = nLine-i;
9805    }else{
9806      zSql[nSql++] = '\n';
9807      memcpy(zSql+nSql, zLine, nLine+1);
9808      nSql += nLine;
9809    }
9810    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
9811                && sqlite3_complete(zSql) ){
9812      errCnt += runOneSqlLine(p, zSql, p->in, startline);
9813      nSql = 0;
9814      if( p->outCount ){
9815        output_reset(p);
9816        p->outCount = 0;
9817      }else{
9818        clearTempFile(p);
9819      }
9820    }else if( nSql && _all_whitespace(zSql) ){
9821      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
9822      nSql = 0;
9823    }
9824  }
9825  if( nSql && !_all_whitespace(zSql) ){
9826    errCnt += runOneSqlLine(p, zSql, p->in, startline);
9827  }
9828  free(zSql);
9829  free(zLine);
9830  return errCnt>0;
9831}
9832
9833/*
9834** Return a pathname which is the user's home directory.  A
9835** 0 return indicates an error of some kind.
9836*/
9837static char *find_home_dir(int clearFlag){
9838  static char *home_dir = NULL;
9839  if( clearFlag ){
9840    free(home_dir);
9841    home_dir = 0;
9842    return 0;
9843  }
9844  if( home_dir ) return home_dir;
9845
9846#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
9847     && !defined(__RTP__) && !defined(_WRS_KERNEL)
9848  {
9849    struct passwd *pwent;
9850    uid_t uid = getuid();
9851    if( (pwent=getpwuid(uid)) != NULL) {
9852      home_dir = pwent->pw_dir;
9853    }
9854  }
9855#endif
9856
9857#if defined(_WIN32_WCE)
9858  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
9859   */
9860  home_dir = "/";
9861#else
9862
9863#if defined(_WIN32) || defined(WIN32)
9864  if (!home_dir) {
9865    home_dir = getenv("USERPROFILE");
9866  }
9867#endif
9868
9869  if (!home_dir) {
9870    home_dir = getenv("HOME");
9871  }
9872
9873#if defined(_WIN32) || defined(WIN32)
9874  if (!home_dir) {
9875    char *zDrive, *zPath;
9876    int n;
9877    zDrive = getenv("HOMEDRIVE");
9878    zPath = getenv("HOMEPATH");
9879    if( zDrive && zPath ){
9880      n = strlen30(zDrive) + strlen30(zPath) + 1;
9881      home_dir = malloc( n );
9882      if( home_dir==0 ) return 0;
9883      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
9884      return home_dir;
9885    }
9886    home_dir = "c:\\";
9887  }
9888#endif
9889
9890#endif /* !_WIN32_WCE */
9891
9892  if( home_dir ){
9893    int n = strlen30(home_dir) + 1;
9894    char *z = malloc( n );
9895    if( z ) memcpy(z, home_dir, n);
9896    home_dir = z;
9897  }
9898
9899  return home_dir;
9900}
9901
9902/*
9903** Read input from the file given by sqliterc_override.  Or if that
9904** parameter is NULL, take input from ~/.sqliterc
9905**
9906** Returns the number of errors.
9907*/
9908static void process_sqliterc(
9909  ShellState *p,                  /* Configuration data */
9910  const char *sqliterc_override   /* Name of config file. NULL to use default */
9911){
9912  char *home_dir = NULL;
9913  const char *sqliterc = sqliterc_override;
9914  char *zBuf = 0;
9915  FILE *inSaved = p->in;
9916  int savedLineno = p->lineno;
9917
9918  if (sqliterc == NULL) {
9919    home_dir = find_home_dir(0);
9920    if( home_dir==0 ){
9921      raw_printf(stderr, "-- warning: cannot find home directory;"
9922                      " cannot read ~/.sqliterc\n");
9923      return;
9924    }
9925    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
9926    sqliterc = zBuf;
9927  }
9928  p->in = fopen(sqliterc,"rb");
9929  if( p->in ){
9930    if( stdin_is_interactive ){
9931      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
9932    }
9933    process_input(p);
9934    fclose(p->in);
9935  }
9936  p->in = inSaved;
9937  p->lineno = savedLineno;
9938  sqlite3_free(zBuf);
9939}
9940
9941/*
9942** Show available command line options
9943*/
9944static const char zOptions[] =
9945#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
9946  "   -A ARGS...           run \".archive ARGS\" and exit\n"
9947#endif
9948  "   -append              append the database to the end of the file\n"
9949  "   -ascii               set output mode to 'ascii'\n"
9950  "   -bail                stop after hitting an error\n"
9951  "   -batch               force batch I/O\n"
9952  "   -column              set output mode to 'column'\n"
9953  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
9954  "   -csv                 set output mode to 'csv'\n"
9955#if defined(SQLITE_ENABLE_DESERIALIZE)
9956  "   -deserialize         open the database using sqlite3_deserialize()\n"
9957#endif
9958  "   -echo                print commands before execution\n"
9959  "   -init FILENAME       read/process named file\n"
9960  "   -[no]header          turn headers on or off\n"
9961#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
9962  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
9963#endif
9964  "   -help                show this message\n"
9965  "   -html                set output mode to HTML\n"
9966  "   -interactive         force interactive I/O\n"
9967  "   -line                set output mode to 'line'\n"
9968  "   -list                set output mode to 'list'\n"
9969  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
9970#if defined(SQLITE_ENABLE_DESERIALIZE)
9971  "   -maxsize N           maximum size for a --deserialize database\n"
9972#endif
9973  "   -memtrace            trace all memory allocations and deallocations\n"
9974  "   -mmap N              default mmap size set to N\n"
9975#ifdef SQLITE_ENABLE_MULTIPLEX
9976  "   -multiplex           enable the multiplexor VFS\n"
9977#endif
9978  "   -newline SEP         set output row separator. Default: '\\n'\n"
9979  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
9980  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
9981  "   -quote               set output mode to 'quote'\n"
9982  "   -readonly            open the database read-only\n"
9983  "   -separator SEP       set output column separator. Default: '|'\n"
9984#ifdef SQLITE_ENABLE_SORTER_REFERENCES
9985  "   -sorterref SIZE      sorter references threshold size\n"
9986#endif
9987  "   -stats               print memory stats before each finalize\n"
9988  "   -version             show SQLite version\n"
9989  "   -vfs NAME            use NAME as the default VFS\n"
9990#ifdef SQLITE_ENABLE_VFSTRACE
9991  "   -vfstrace            enable tracing of all VFS calls\n"
9992#endif
9993#ifdef SQLITE_HAVE_ZLIB
9994  "   -zip                 open the file as a ZIP Archive\n"
9995#endif
9996;
9997static void usage(int showDetail){
9998  utf8_printf(stderr,
9999      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10000      "FILENAME is the name of an SQLite database. A new database is created\n"
10001      "if the file does not previously exist.\n", Argv0);
10002  if( showDetail ){
10003    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10004  }else{
10005    raw_printf(stderr, "Use the -help option for additional information\n");
10006  }
10007  exit(1);
10008}
10009
10010/*
10011** Internal check:  Verify that the SQLite is uninitialized.  Print a
10012** error message if it is initialized.
10013*/
10014static void verify_uninitialized(void){
10015  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10016    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10017                        " initialization.\n");
10018  }
10019}
10020
10021/*
10022** Initialize the state information in data
10023*/
10024static void main_init(ShellState *data) {
10025  memset(data, 0, sizeof(*data));
10026  data->normalMode = data->cMode = data->mode = MODE_List;
10027  data->autoExplain = 1;
10028  memcpy(data->colSeparator,SEP_Column, 2);
10029  memcpy(data->rowSeparator,SEP_Row, 2);
10030  data->showHeader = 0;
10031  data->shellFlgs = SHFLG_Lookaside;
10032  verify_uninitialized();
10033  sqlite3_config(SQLITE_CONFIG_URI, 1);
10034  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10035  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10036  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10037  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10038}
10039
10040/*
10041** Output text to the console in a font that attracts extra attention.
10042*/
10043#ifdef _WIN32
10044static void printBold(const char *zText){
10045  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10046  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10047  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10048  SetConsoleTextAttribute(out,
10049         FOREGROUND_RED|FOREGROUND_INTENSITY
10050  );
10051  printf("%s", zText);
10052  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10053}
10054#else
10055static void printBold(const char *zText){
10056  printf("\033[1m%s\033[0m", zText);
10057}
10058#endif
10059
10060/*
10061** Get the argument to an --option.  Throw an error and die if no argument
10062** is available.
10063*/
10064static char *cmdline_option_value(int argc, char **argv, int i){
10065  if( i==argc ){
10066    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10067            argv[0], argv[argc-1]);
10068    exit(1);
10069  }
10070  return argv[i];
10071}
10072
10073#ifndef SQLITE_SHELL_IS_UTF8
10074#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
10075#    define SQLITE_SHELL_IS_UTF8          (0)
10076#  else
10077#    define SQLITE_SHELL_IS_UTF8          (1)
10078#  endif
10079#endif
10080
10081#if SQLITE_SHELL_IS_UTF8
10082int SQLITE_CDECL main(int argc, char **argv){
10083#else
10084int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10085  char **argv;
10086#endif
10087  char *zErrMsg = 0;
10088  ShellState data;
10089  const char *zInitFile = 0;
10090  int i;
10091  int rc = 0;
10092  int warnInmemoryDb = 0;
10093  int readStdin = 1;
10094  int nCmd = 0;
10095  char **azCmd = 0;
10096  const char *zVfs = 0;           /* Value of -vfs command-line option */
10097#if !SQLITE_SHELL_IS_UTF8
10098  char **argvToFree = 0;
10099  int argcToFree = 0;
10100#endif
10101
10102  setBinaryMode(stdin, 0);
10103  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10104  stdin_is_interactive = isatty(0);
10105  stdout_is_console = isatty(1);
10106
10107#if !defined(_WIN32_WCE)
10108  if( getenv("SQLITE_DEBUG_BREAK") ){
10109    if( isatty(0) && isatty(2) ){
10110      fprintf(stderr,
10111          "attach debugger to process %d and press any key to continue.\n",
10112          GETPID());
10113      fgetc(stdin);
10114    }else{
10115#if defined(_WIN32) || defined(WIN32)
10116      DebugBreak();
10117#elif defined(SIGTRAP)
10118      raise(SIGTRAP);
10119#endif
10120    }
10121  }
10122#endif
10123
10124#if USE_SYSTEM_SQLITE+0!=1
10125  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10126    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10127            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10128    exit(1);
10129  }
10130#endif
10131  main_init(&data);
10132
10133  /* On Windows, we must translate command-line arguments into UTF-8.
10134  ** The SQLite memory allocator subsystem has to be enabled in order to
10135  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10136  ** subsequent sqlite3_config() calls will work.  So copy all results into
10137  ** memory that does not come from the SQLite memory allocator.
10138  */
10139#if !SQLITE_SHELL_IS_UTF8
10140  sqlite3_initialize();
10141  argvToFree = malloc(sizeof(argv[0])*argc*2);
10142  argcToFree = argc;
10143  argv = argvToFree + argc;
10144  if( argv==0 ) shell_out_of_memory();
10145  for(i=0; i<argc; i++){
10146    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10147    int n;
10148    if( z==0 ) shell_out_of_memory();
10149    n = (int)strlen(z);
10150    argv[i] = malloc( n+1 );
10151    if( argv[i]==0 ) shell_out_of_memory();
10152    memcpy(argv[i], z, n+1);
10153    argvToFree[i] = argv[i];
10154    sqlite3_free(z);
10155  }
10156  sqlite3_shutdown();
10157#endif
10158
10159  assert( argc>=1 && argv && argv[0] );
10160  Argv0 = argv[0];
10161
10162  /* Make sure we have a valid signal handler early, before anything
10163  ** else is done.
10164  */
10165#ifdef SIGINT
10166  signal(SIGINT, interrupt_handler);
10167#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10168  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10169#endif
10170
10171#ifdef SQLITE_SHELL_DBNAME_PROC
10172  {
10173    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10174    ** of a C-function that will provide the name of the database file.  Use
10175    ** this compile-time option to embed this shell program in larger
10176    ** applications. */
10177    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10178    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10179    warnInmemoryDb = 0;
10180  }
10181#endif
10182
10183  /* Do an initial pass through the command-line argument to locate
10184  ** the name of the database file, the name of the initialization file,
10185  ** the size of the alternative malloc heap,
10186  ** and the first command to execute.
10187  */
10188  verify_uninitialized();
10189  for(i=1; i<argc; i++){
10190    char *z;
10191    z = argv[i];
10192    if( z[0]!='-' ){
10193      if( data.zDbFilename==0 ){
10194        data.zDbFilename = z;
10195      }else{
10196        /* Excesss arguments are interpreted as SQL (or dot-commands) and
10197        ** mean that nothing is read from stdin */
10198        readStdin = 0;
10199        nCmd++;
10200        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
10201        if( azCmd==0 ) shell_out_of_memory();
10202        azCmd[nCmd-1] = z;
10203      }
10204    }
10205    if( z[1]=='-' ) z++;
10206    if( strcmp(z,"-separator")==0
10207     || strcmp(z,"-nullvalue")==0
10208     || strcmp(z,"-newline")==0
10209     || strcmp(z,"-cmd")==0
10210    ){
10211      (void)cmdline_option_value(argc, argv, ++i);
10212    }else if( strcmp(z,"-init")==0 ){
10213      zInitFile = cmdline_option_value(argc, argv, ++i);
10214    }else if( strcmp(z,"-batch")==0 ){
10215      /* Need to check for batch mode here to so we can avoid printing
10216      ** informational messages (like from process_sqliterc) before
10217      ** we do the actual processing of arguments later in a second pass.
10218      */
10219      stdin_is_interactive = 0;
10220    }else if( strcmp(z,"-heap")==0 ){
10221#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10222      const char *zSize;
10223      sqlite3_int64 szHeap;
10224
10225      zSize = cmdline_option_value(argc, argv, ++i);
10226      szHeap = integerValue(zSize);
10227      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
10228      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
10229#else
10230      (void)cmdline_option_value(argc, argv, ++i);
10231#endif
10232    }else if( strcmp(z,"-pagecache")==0 ){
10233      int n, sz;
10234      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10235      if( sz>70000 ) sz = 70000;
10236      if( sz<0 ) sz = 0;
10237      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10238      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
10239                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
10240      data.shellFlgs |= SHFLG_Pagecache;
10241    }else if( strcmp(z,"-lookaside")==0 ){
10242      int n, sz;
10243      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10244      if( sz<0 ) sz = 0;
10245      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10246      if( n<0 ) n = 0;
10247      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
10248      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
10249#ifdef SQLITE_ENABLE_VFSTRACE
10250    }else if( strcmp(z,"-vfstrace")==0 ){
10251      extern int vfstrace_register(
10252         const char *zTraceName,
10253         const char *zOldVfsName,
10254         int (*xOut)(const char*,void*),
10255         void *pOutArg,
10256         int makeDefault
10257      );
10258      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
10259#endif
10260#ifdef SQLITE_ENABLE_MULTIPLEX
10261    }else if( strcmp(z,"-multiplex")==0 ){
10262      extern int sqlite3_multiple_initialize(const char*,int);
10263      sqlite3_multiplex_initialize(0, 1);
10264#endif
10265    }else if( strcmp(z,"-mmap")==0 ){
10266      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10267      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
10268#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10269    }else if( strcmp(z,"-sorterref")==0 ){
10270      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10271      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
10272#endif
10273    }else if( strcmp(z,"-vfs")==0 ){
10274      zVfs = cmdline_option_value(argc, argv, ++i);
10275#ifdef SQLITE_HAVE_ZLIB
10276    }else if( strcmp(z,"-zip")==0 ){
10277      data.openMode = SHELL_OPEN_ZIPFILE;
10278#endif
10279    }else if( strcmp(z,"-append")==0 ){
10280      data.openMode = SHELL_OPEN_APPENDVFS;
10281#ifdef SQLITE_ENABLE_DESERIALIZE
10282    }else if( strcmp(z,"-deserialize")==0 ){
10283      data.openMode = SHELL_OPEN_DESERIALIZE;
10284    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10285      data.szMax = integerValue(argv[++i]);
10286#endif
10287    }else if( strcmp(z,"-readonly")==0 ){
10288      data.openMode = SHELL_OPEN_READONLY;
10289#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
10290    }else if( strncmp(z, "-A",2)==0 ){
10291      /* All remaining command-line arguments are passed to the ".archive"
10292      ** command, so ignore them */
10293      break;
10294#endif
10295    }else if( strcmp(z, "-memtrace")==0 ){
10296      sqlite3MemTraceActivate(stderr);
10297    }
10298  }
10299  verify_uninitialized();
10300
10301
10302#ifdef SQLITE_SHELL_INIT_PROC
10303  {
10304    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
10305    ** of a C-function that will perform initialization actions on SQLite that
10306    ** occur just before or after sqlite3_initialize(). Use this compile-time
10307    ** option to embed this shell program in larger applications. */
10308    extern void SQLITE_SHELL_INIT_PROC(void);
10309    SQLITE_SHELL_INIT_PROC();
10310  }
10311#else
10312  /* All the sqlite3_config() calls have now been made. So it is safe
10313  ** to call sqlite3_initialize() and process any command line -vfs option. */
10314  sqlite3_initialize();
10315#endif
10316
10317  if( zVfs ){
10318    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
10319    if( pVfs ){
10320      sqlite3_vfs_register(pVfs, 1);
10321    }else{
10322      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
10323      exit(1);
10324    }
10325  }
10326
10327  if( data.zDbFilename==0 ){
10328#ifndef SQLITE_OMIT_MEMORYDB
10329    data.zDbFilename = ":memory:";
10330    warnInmemoryDb = argc==1;
10331#else
10332    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
10333    return 1;
10334#endif
10335  }
10336  data.out = stdout;
10337  sqlite3_appendvfs_init(0,0,0);
10338
10339  /* Go ahead and open the database file if it already exists.  If the
10340  ** file does not exist, delay opening it.  This prevents empty database
10341  ** files from being created if a user mistypes the database name argument
10342  ** to the sqlite command-line tool.
10343  */
10344  if( access(data.zDbFilename, 0)==0 ){
10345    open_db(&data, 0);
10346  }
10347
10348  /* Process the initialization file if there is one.  If no -init option
10349  ** is given on the command line, look for a file named ~/.sqliterc and
10350  ** try to process it.
10351  */
10352  process_sqliterc(&data,zInitFile);
10353
10354  /* Make a second pass through the command-line argument and set
10355  ** options.  This second pass is delayed until after the initialization
10356  ** file is processed so that the command-line arguments will override
10357  ** settings in the initialization file.
10358  */
10359  for(i=1; i<argc; i++){
10360    char *z = argv[i];
10361    if( z[0]!='-' ) continue;
10362    if( z[1]=='-' ){ z++; }
10363    if( strcmp(z,"-init")==0 ){
10364      i++;
10365    }else if( strcmp(z,"-html")==0 ){
10366      data.mode = MODE_Html;
10367    }else if( strcmp(z,"-list")==0 ){
10368      data.mode = MODE_List;
10369    }else if( strcmp(z,"-quote")==0 ){
10370      data.mode = MODE_Quote;
10371    }else if( strcmp(z,"-line")==0 ){
10372      data.mode = MODE_Line;
10373    }else if( strcmp(z,"-column")==0 ){
10374      data.mode = MODE_Column;
10375    }else if( strcmp(z,"-csv")==0 ){
10376      data.mode = MODE_Csv;
10377      memcpy(data.colSeparator,",",2);
10378#ifdef SQLITE_HAVE_ZLIB
10379    }else if( strcmp(z,"-zip")==0 ){
10380      data.openMode = SHELL_OPEN_ZIPFILE;
10381#endif
10382    }else if( strcmp(z,"-append")==0 ){
10383      data.openMode = SHELL_OPEN_APPENDVFS;
10384#ifdef SQLITE_ENABLE_DESERIALIZE
10385    }else if( strcmp(z,"-deserialize")==0 ){
10386      data.openMode = SHELL_OPEN_DESERIALIZE;
10387    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10388      data.szMax = integerValue(argv[++i]);
10389#endif
10390    }else if( strcmp(z,"-readonly")==0 ){
10391      data.openMode = SHELL_OPEN_READONLY;
10392    }else if( strcmp(z,"-ascii")==0 ){
10393      data.mode = MODE_Ascii;
10394      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
10395                       SEP_Unit);
10396      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
10397                       SEP_Record);
10398    }else if( strcmp(z,"-separator")==0 ){
10399      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
10400                       "%s",cmdline_option_value(argc,argv,++i));
10401    }else if( strcmp(z,"-newline")==0 ){
10402      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
10403                       "%s",cmdline_option_value(argc,argv,++i));
10404    }else if( strcmp(z,"-nullvalue")==0 ){
10405      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
10406                       "%s",cmdline_option_value(argc,argv,++i));
10407    }else if( strcmp(z,"-header")==0 ){
10408      data.showHeader = 1;
10409    }else if( strcmp(z,"-noheader")==0 ){
10410      data.showHeader = 0;
10411    }else if( strcmp(z,"-echo")==0 ){
10412      ShellSetFlag(&data, SHFLG_Echo);
10413    }else if( strcmp(z,"-eqp")==0 ){
10414      data.autoEQP = AUTOEQP_on;
10415    }else if( strcmp(z,"-eqpfull")==0 ){
10416      data.autoEQP = AUTOEQP_full;
10417    }else if( strcmp(z,"-stats")==0 ){
10418      data.statsOn = 1;
10419    }else if( strcmp(z,"-scanstats")==0 ){
10420      data.scanstatsOn = 1;
10421    }else if( strcmp(z,"-backslash")==0 ){
10422      /* Undocumented command-line option: -backslash
10423      ** Causes C-style backslash escapes to be evaluated in SQL statements
10424      ** prior to sending the SQL into SQLite.  Useful for injecting
10425      ** crazy bytes in the middle of SQL statements for testing and debugging.
10426      */
10427      ShellSetFlag(&data, SHFLG_Backslash);
10428    }else if( strcmp(z,"-bail")==0 ){
10429      bail_on_error = 1;
10430    }else if( strcmp(z,"-version")==0 ){
10431      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
10432      return 0;
10433    }else if( strcmp(z,"-interactive")==0 ){
10434      stdin_is_interactive = 1;
10435    }else if( strcmp(z,"-batch")==0 ){
10436      stdin_is_interactive = 0;
10437    }else if( strcmp(z,"-heap")==0 ){
10438      i++;
10439    }else if( strcmp(z,"-pagecache")==0 ){
10440      i+=2;
10441    }else if( strcmp(z,"-lookaside")==0 ){
10442      i+=2;
10443    }else if( strcmp(z,"-mmap")==0 ){
10444      i++;
10445    }else if( strcmp(z,"-memtrace")==0 ){
10446      i++;
10447#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10448    }else if( strcmp(z,"-sorterref")==0 ){
10449      i++;
10450#endif
10451    }else if( strcmp(z,"-vfs")==0 ){
10452      i++;
10453#ifdef SQLITE_ENABLE_VFSTRACE
10454    }else if( strcmp(z,"-vfstrace")==0 ){
10455      i++;
10456#endif
10457#ifdef SQLITE_ENABLE_MULTIPLEX
10458    }else if( strcmp(z,"-multiplex")==0 ){
10459      i++;
10460#endif
10461    }else if( strcmp(z,"-help")==0 ){
10462      usage(1);
10463    }else if( strcmp(z,"-cmd")==0 ){
10464      /* Run commands that follow -cmd first and separately from commands
10465      ** that simply appear on the command-line.  This seems goofy.  It would
10466      ** be better if all commands ran in the order that they appear.  But
10467      ** we retain the goofy behavior for historical compatibility. */
10468      if( i==argc-1 ) break;
10469      z = cmdline_option_value(argc,argv,++i);
10470      if( z[0]=='.' ){
10471        rc = do_meta_command(z, &data);
10472        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
10473      }else{
10474        open_db(&data, 0);
10475        rc = shell_exec(&data, z, &zErrMsg);
10476        if( zErrMsg!=0 ){
10477          utf8_printf(stderr,"Error: %s\n", zErrMsg);
10478          if( bail_on_error ) return rc!=0 ? rc : 1;
10479        }else if( rc!=0 ){
10480          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
10481          if( bail_on_error ) return rc;
10482        }
10483      }
10484#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
10485    }else if( strncmp(z, "-A", 2)==0 ){
10486      if( nCmd>0 ){
10487        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
10488                            " with \"%s\"\n", z);
10489        return 1;
10490      }
10491      open_db(&data, OPEN_DB_ZIPFILE);
10492      if( z[2] ){
10493        argv[i] = &z[2];
10494        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
10495      }else{
10496        arDotCommand(&data, 1, argv+i, argc-i);
10497      }
10498      readStdin = 0;
10499      break;
10500#endif
10501    }else{
10502      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
10503      raw_printf(stderr,"Use -help for a list of options.\n");
10504      return 1;
10505    }
10506    data.cMode = data.mode;
10507  }
10508
10509  if( !readStdin ){
10510    /* Run all arguments that do not begin with '-' as if they were separate
10511    ** command-line inputs, except for the argToSkip argument which contains
10512    ** the database filename.
10513    */
10514    for(i=0; i<nCmd; i++){
10515      if( azCmd[i][0]=='.' ){
10516        rc = do_meta_command(azCmd[i], &data);
10517        if( rc ) return rc==2 ? 0 : rc;
10518      }else{
10519        open_db(&data, 0);
10520        rc = shell_exec(&data, azCmd[i], &zErrMsg);
10521        if( zErrMsg!=0 ){
10522          utf8_printf(stderr,"Error: %s\n", zErrMsg);
10523          return rc!=0 ? rc : 1;
10524        }else if( rc!=0 ){
10525          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
10526          return rc;
10527        }
10528      }
10529    }
10530    free(azCmd);
10531  }else{
10532    /* Run commands received from standard input
10533    */
10534    if( stdin_is_interactive ){
10535      char *zHome;
10536      char *zHistory;
10537      int nHistory;
10538      printf(
10539        "SQLite version %s %.19s\n" /*extra-version-info*/
10540        "Enter \".help\" for usage hints.\n",
10541        sqlite3_libversion(), sqlite3_sourceid()
10542      );
10543      if( warnInmemoryDb ){
10544        printf("Connected to a ");
10545        printBold("transient in-memory database");
10546        printf(".\nUse \".open FILENAME\" to reopen on a "
10547               "persistent database.\n");
10548      }
10549      zHistory = getenv("SQLITE_HISTORY");
10550      if( zHistory ){
10551        zHistory = strdup(zHistory);
10552      }else if( (zHome = find_home_dir(0))!=0 ){
10553        nHistory = strlen30(zHome) + 20;
10554        if( (zHistory = malloc(nHistory))!=0 ){
10555          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
10556        }
10557      }
10558      if( zHistory ){ shell_read_history(zHistory); }
10559#if HAVE_READLINE || HAVE_EDITLINE
10560      rl_attempted_completion_function = readline_completion;
10561#elif HAVE_LINENOISE
10562      linenoiseSetCompletionCallback(linenoise_completion);
10563#endif
10564      data.in = 0;
10565      rc = process_input(&data);
10566      if( zHistory ){
10567        shell_stifle_history(2000);
10568        shell_write_history(zHistory);
10569        free(zHistory);
10570      }
10571    }else{
10572      data.in = stdin;
10573      rc = process_input(&data);
10574    }
10575  }
10576  set_table_name(&data, 0);
10577  if( data.db ){
10578    session_close_all(&data);
10579    close_db(data.db);
10580  }
10581  sqlite3_free(data.zFreeOnClose);
10582  find_home_dir(1);
10583  output_reset(&data);
10584  data.doXdgOpen = 0;
10585  clearTempFile(&data);
10586#if !SQLITE_SHELL_IS_UTF8
10587  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
10588  free(argvToFree);
10589#endif
10590  /* Clear the global data structure so that valgrind will detect memory
10591  ** leaks */
10592  memset(&data, 0, sizeof(data));
10593  return rc;
10594}
10595