xref: /sqlite-3.40.0/src/shell.c.in (revision 3832ac9f)
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** Determine if we are dealing with WinRT, which provides only a subset of
22** the full Win32 API.
23*/
24#if !defined(SQLITE_OS_WINRT)
25# define SQLITE_OS_WINRT 0
26#endif
27
28/*
29** Warning pragmas copied from msvc.h in the core.
30*/
31#if defined(_MSC_VER)
32#pragma warning(disable : 4054)
33#pragma warning(disable : 4055)
34#pragma warning(disable : 4100)
35#pragma warning(disable : 4127)
36#pragma warning(disable : 4130)
37#pragma warning(disable : 4152)
38#pragma warning(disable : 4189)
39#pragma warning(disable : 4206)
40#pragma warning(disable : 4210)
41#pragma warning(disable : 4232)
42#pragma warning(disable : 4244)
43#pragma warning(disable : 4305)
44#pragma warning(disable : 4306)
45#pragma warning(disable : 4702)
46#pragma warning(disable : 4706)
47#endif /* defined(_MSC_VER) */
48
49/*
50** No support for loadable extensions in VxWorks.
51*/
52#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
53# define SQLITE_OMIT_LOAD_EXTENSION 1
54#endif
55
56/*
57** Enable large-file support for fopen() and friends on unix.
58*/
59#ifndef SQLITE_DISABLE_LFS
60# define _LARGE_FILE       1
61# ifndef _FILE_OFFSET_BITS
62#   define _FILE_OFFSET_BITS 64
63# endif
64# define _LARGEFILE_SOURCE 1
65#endif
66
67#include <stdlib.h>
68#include <string.h>
69#include <stdio.h>
70#include <assert.h>
71#include "sqlite3.h"
72typedef sqlite3_int64 i64;
73typedef sqlite3_uint64 u64;
74typedef unsigned char u8;
75#if SQLITE_USER_AUTHENTICATION
76# include "sqlite3userauth.h"
77#endif
78#include <ctype.h>
79#include <stdarg.h>
80
81#if !defined(_WIN32) && !defined(WIN32)
82# include <signal.h>
83# if !defined(__RTP__) && !defined(_WRS_KERNEL)
84#  include <pwd.h>
85# endif
86#endif
87#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
88# include <unistd.h>
89# include <dirent.h>
90# define GETPID getpid
91# if defined(__MINGW32__)
92#  define DIRENT dirent
93#  ifndef S_ISLNK
94#   define S_ISLNK(mode) (0)
95#  endif
96# endif
97#else
98# define GETPID (int)GetCurrentProcessId
99#endif
100#include <sys/types.h>
101#include <sys/stat.h>
102
103#if HAVE_READLINE
104# include <readline/readline.h>
105# include <readline/history.h>
106#endif
107
108#if HAVE_EDITLINE
109# include <editline/readline.h>
110#endif
111
112#if HAVE_EDITLINE || HAVE_READLINE
113
114# define shell_add_history(X) add_history(X)
115# define shell_read_history(X) read_history(X)
116# define shell_write_history(X) write_history(X)
117# define shell_stifle_history(X) stifle_history(X)
118# define shell_readline(X) readline(X)
119
120#elif HAVE_LINENOISE
121
122# include "linenoise.h"
123# define shell_add_history(X) linenoiseHistoryAdd(X)
124# define shell_read_history(X) linenoiseHistoryLoad(X)
125# define shell_write_history(X) linenoiseHistorySave(X)
126# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
127# define shell_readline(X) linenoise(X)
128
129#else
130
131# define shell_read_history(X)
132# define shell_write_history(X)
133# define shell_stifle_history(X)
134
135# define SHELL_USE_LOCAL_GETLINE 1
136#endif
137
138
139#if defined(_WIN32) || defined(WIN32)
140# if SQLITE_OS_WINRT
141#  define SQLITE_OMIT_POPEN 1
142# else
143#  include <io.h>
144#  include <fcntl.h>
145#  define isatty(h) _isatty(h)
146#  ifndef access
147#   define access(f,m) _access((f),(m))
148#  endif
149#  ifndef unlink
150#   define unlink _unlink
151#  endif
152#  ifndef strdup
153#   define strdup _strdup
154#  endif
155#  undef popen
156#  define popen _popen
157#  undef pclose
158#  define pclose _pclose
159# endif
160#else
161 /* Make sure isatty() has a prototype. */
162 extern int isatty(int);
163
164# if !defined(__RTP__) && !defined(_WRS_KERNEL)
165  /* popen and pclose are not C89 functions and so are
166  ** sometimes omitted from the <stdio.h> header */
167   extern FILE *popen(const char*,const char*);
168   extern int pclose(FILE*);
169# else
170#  define SQLITE_OMIT_POPEN 1
171# endif
172#endif
173
174#if defined(_WIN32_WCE)
175/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
176 * thus we always assume that we have a console. That can be
177 * overridden with the -batch command line option.
178 */
179#define isatty(x) 1
180#endif
181
182/* ctype macros that work with signed characters */
183#define IsSpace(X)  isspace((unsigned char)X)
184#define IsDigit(X)  isdigit((unsigned char)X)
185#define ToLower(X)  (char)tolower((unsigned char)X)
186
187#if defined(_WIN32) || defined(WIN32)
188#if SQLITE_OS_WINRT
189#include <intrin.h>
190#endif
191#include <windows.h>
192
193/* string conversion routines only needed on Win32 */
194extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
195extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
196extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
197extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
198#endif
199
200/* On Windows, we normally run with output mode of TEXT so that \n characters
201** are automatically translated into \r\n.  However, this behavior needs
202** to be disabled in some cases (ex: when generating CSV output and when
203** rendering quoted strings that contain \n characters).  The following
204** routines take care of that.
205*/
206#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
207static void setBinaryMode(FILE *file, int isOutput){
208  if( isOutput ) fflush(file);
209  _setmode(_fileno(file), _O_BINARY);
210}
211static void setTextMode(FILE *file, int isOutput){
212  if( isOutput ) fflush(file);
213  _setmode(_fileno(file), _O_TEXT);
214}
215#else
216# define setBinaryMode(X,Y)
217# define setTextMode(X,Y)
218#endif
219
220
221/* True if the timer is enabled */
222static int enableTimer = 0;
223
224/* Return the current wall-clock time */
225static sqlite3_int64 timeOfDay(void){
226  static sqlite3_vfs *clockVfs = 0;
227  sqlite3_int64 t;
228  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
229  if( clockVfs==0 ) return 0;  /* Never actually happens */
230  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
231    clockVfs->xCurrentTimeInt64(clockVfs, &t);
232  }else{
233    double r;
234    clockVfs->xCurrentTime(clockVfs, &r);
235    t = (sqlite3_int64)(r*86400000.0);
236  }
237  return t;
238}
239
240#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
241#include <sys/time.h>
242#include <sys/resource.h>
243
244/* VxWorks does not support getrusage() as far as we can determine */
245#if defined(_WRS_KERNEL) || defined(__RTP__)
246struct rusage {
247  struct timeval ru_utime; /* user CPU time used */
248  struct timeval ru_stime; /* system CPU time used */
249};
250#define getrusage(A,B) memset(B,0,sizeof(*B))
251#endif
252
253/* Saved resource information for the beginning of an operation */
254static struct rusage sBegin;  /* CPU time at start */
255static sqlite3_int64 iBegin;  /* Wall-clock time at start */
256
257/*
258** Begin timing an operation
259*/
260static void beginTimer(void){
261  if( enableTimer ){
262    getrusage(RUSAGE_SELF, &sBegin);
263    iBegin = timeOfDay();
264  }
265}
266
267/* Return the difference of two time_structs in seconds */
268static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
269  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
270         (double)(pEnd->tv_sec - pStart->tv_sec);
271}
272
273/*
274** Print the timing results.
275*/
276static void endTimer(void){
277  if( enableTimer ){
278    sqlite3_int64 iEnd = timeOfDay();
279    struct rusage sEnd;
280    getrusage(RUSAGE_SELF, &sEnd);
281    printf("Run Time: real %.3f user %f sys %f\n",
282       (iEnd - iBegin)*0.001,
283       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
284       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
285  }
286}
287
288#define BEGIN_TIMER beginTimer()
289#define END_TIMER endTimer()
290#define HAS_TIMER 1
291
292#elif (defined(_WIN32) || defined(WIN32))
293
294/* Saved resource information for the beginning of an operation */
295static HANDLE hProcess;
296static FILETIME ftKernelBegin;
297static FILETIME ftUserBegin;
298static sqlite3_int64 ftWallBegin;
299typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
300                                    LPFILETIME, LPFILETIME);
301static GETPROCTIMES getProcessTimesAddr = NULL;
302
303/*
304** Check to see if we have timer support.  Return 1 if necessary
305** support found (or found previously).
306*/
307static int hasTimer(void){
308  if( getProcessTimesAddr ){
309    return 1;
310  } else {
311#if !SQLITE_OS_WINRT
312    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
313    ** versions. See if the version we are running on has it, and if it
314    ** does, save off a pointer to it and the current process handle.
315    */
316    hProcess = GetCurrentProcess();
317    if( hProcess ){
318      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
319      if( NULL != hinstLib ){
320        getProcessTimesAddr =
321            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
322        if( NULL != getProcessTimesAddr ){
323          return 1;
324        }
325        FreeLibrary(hinstLib);
326      }
327    }
328#endif
329  }
330  return 0;
331}
332
333/*
334** Begin timing an operation
335*/
336static void beginTimer(void){
337  if( enableTimer && getProcessTimesAddr ){
338    FILETIME ftCreation, ftExit;
339    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
340                        &ftKernelBegin,&ftUserBegin);
341    ftWallBegin = timeOfDay();
342  }
343}
344
345/* Return the difference of two FILETIME structs in seconds */
346static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
347  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
348  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
349  return (double) ((i64End - i64Start) / 10000000.0);
350}
351
352/*
353** Print the timing results.
354*/
355static void endTimer(void){
356  if( enableTimer && getProcessTimesAddr){
357    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
358    sqlite3_int64 ftWallEnd = timeOfDay();
359    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
360    printf("Run Time: real %.3f user %f sys %f\n",
361       (ftWallEnd - ftWallBegin)*0.001,
362       timeDiff(&ftUserBegin, &ftUserEnd),
363       timeDiff(&ftKernelBegin, &ftKernelEnd));
364  }
365}
366
367#define BEGIN_TIMER beginTimer()
368#define END_TIMER endTimer()
369#define HAS_TIMER hasTimer()
370
371#else
372#define BEGIN_TIMER
373#define END_TIMER
374#define HAS_TIMER 0
375#endif
376
377/*
378** Used to prevent warnings about unused parameters
379*/
380#define UNUSED_PARAMETER(x) (void)(x)
381
382/*
383** Number of elements in an array
384*/
385#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
386
387/*
388** If the following flag is set, then command execution stops
389** at an error if we are not interactive.
390*/
391static int bail_on_error = 0;
392
393/*
394** Threat stdin as an interactive input if the following variable
395** is true.  Otherwise, assume stdin is connected to a file or pipe.
396*/
397static int stdin_is_interactive = 1;
398
399/*
400** On Windows systems we have to know if standard output is a console
401** in order to translate UTF-8 into MBCS.  The following variable is
402** true if translation is required.
403*/
404static int stdout_is_console = 1;
405
406/*
407** The following is the open SQLite database.  We make a pointer
408** to this database a static variable so that it can be accessed
409** by the SIGINT handler to interrupt database processing.
410*/
411static sqlite3 *globalDb = 0;
412
413/*
414** True if an interrupt (Control-C) has been received.
415*/
416static volatile int seenInterrupt = 0;
417
418#ifdef SQLITE_DEBUG
419/*
420** Out-of-memory simulator variables
421*/
422static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
423static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
424static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
425#endif /* SQLITE_DEBUG */
426
427/*
428** This is the name of our program. It is set in main(), used
429** in a number of other places, mostly for error messages.
430*/
431static char *Argv0;
432
433/*
434** Prompt strings. Initialized in main. Settable with
435**   .prompt main continue
436*/
437static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
438static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
439
440/*
441** Render output like fprintf().  Except, if the output is going to the
442** console and if this is running on a Windows machine, translate the
443** output from UTF-8 into MBCS.
444*/
445#if defined(_WIN32) || defined(WIN32)
446void utf8_printf(FILE *out, const char *zFormat, ...){
447  va_list ap;
448  va_start(ap, zFormat);
449  if( stdout_is_console && (out==stdout || out==stderr) ){
450    char *z1 = sqlite3_vmprintf(zFormat, ap);
451    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
452    sqlite3_free(z1);
453    fputs(z2, out);
454    sqlite3_free(z2);
455  }else{
456    vfprintf(out, zFormat, ap);
457  }
458  va_end(ap);
459}
460#elif !defined(utf8_printf)
461# define utf8_printf fprintf
462#endif
463
464/*
465** Render output like fprintf().  This should not be used on anything that
466** includes string formatting (e.g. "%s").
467*/
468#if !defined(raw_printf)
469# define raw_printf fprintf
470#endif
471
472/* Indicate out-of-memory and exit. */
473static void shell_out_of_memory(void){
474  raw_printf(stderr,"Error: out of memory\n");
475  exit(1);
476}
477
478#ifdef SQLITE_DEBUG
479/* This routine is called when a simulated OOM occurs.  It is broken
480** out as a separate routine to make it easy to set a breakpoint on
481** the OOM
482*/
483void shellOomFault(void){
484  if( oomRepeat>0 ){
485    oomRepeat--;
486  }else{
487    oomCounter--;
488  }
489}
490#endif /* SQLITE_DEBUG */
491
492#ifdef SQLITE_DEBUG
493/* This routine is a replacement malloc() that is used to simulate
494** Out-Of-Memory (OOM) errors for testing purposes.
495*/
496static void *oomMalloc(int nByte){
497  if( oomCounter ){
498    if( oomCounter==1 ){
499      shellOomFault();
500      return 0;
501    }else{
502      oomCounter--;
503    }
504  }
505  return defaultMalloc(nByte);
506}
507#endif /* SQLITE_DEBUG */
508
509#ifdef SQLITE_DEBUG
510/* Register the OOM simulator.  This must occur before any memory
511** allocations */
512static void registerOomSimulator(void){
513  sqlite3_mem_methods mem;
514  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
515  defaultMalloc = mem.xMalloc;
516  mem.xMalloc = oomMalloc;
517  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
518}
519#endif
520
521/*
522** Write I/O traces to the following stream.
523*/
524#ifdef SQLITE_ENABLE_IOTRACE
525static FILE *iotrace = 0;
526#endif
527
528/*
529** This routine works like printf in that its first argument is a
530** format string and subsequent arguments are values to be substituted
531** in place of % fields.  The result of formatting this string
532** is written to iotrace.
533*/
534#ifdef SQLITE_ENABLE_IOTRACE
535static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
536  va_list ap;
537  char *z;
538  if( iotrace==0 ) return;
539  va_start(ap, zFormat);
540  z = sqlite3_vmprintf(zFormat, ap);
541  va_end(ap);
542  utf8_printf(iotrace, "%s", z);
543  sqlite3_free(z);
544}
545#endif
546
547/*
548** Output string zUtf to stream pOut as w characters.  If w is negative,
549** then right-justify the text.  W is the width in UTF-8 characters, not
550** in bytes.  This is different from the %*.*s specification in printf
551** since with %*.*s the width is measured in bytes, not characters.
552*/
553static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
554  int i;
555  int n;
556  int aw = w<0 ? -w : w;
557  for(i=n=0; zUtf[i]; i++){
558    if( (zUtf[i]&0xc0)!=0x80 ){
559      n++;
560      if( n==aw ){
561        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
562        break;
563      }
564    }
565  }
566  if( n>=aw ){
567    utf8_printf(pOut, "%.*s", i, zUtf);
568  }else if( w<0 ){
569    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
570  }else{
571    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
572  }
573}
574
575
576/*
577** Determines if a string is a number of not.
578*/
579static int isNumber(const char *z, int *realnum){
580  if( *z=='-' || *z=='+' ) z++;
581  if( !IsDigit(*z) ){
582    return 0;
583  }
584  z++;
585  if( realnum ) *realnum = 0;
586  while( IsDigit(*z) ){ z++; }
587  if( *z=='.' ){
588    z++;
589    if( !IsDigit(*z) ) return 0;
590    while( IsDigit(*z) ){ z++; }
591    if( realnum ) *realnum = 1;
592  }
593  if( *z=='e' || *z=='E' ){
594    z++;
595    if( *z=='+' || *z=='-' ) z++;
596    if( !IsDigit(*z) ) return 0;
597    while( IsDigit(*z) ){ z++; }
598    if( realnum ) *realnum = 1;
599  }
600  return *z==0;
601}
602
603/*
604** Compute a string length that is limited to what can be stored in
605** lower 30 bits of a 32-bit signed integer.
606*/
607static int strlen30(const char *z){
608  const char *z2 = z;
609  while( *z2 ){ z2++; }
610  return 0x3fffffff & (int)(z2 - z);
611}
612
613/*
614** Return the length of a string in characters.  Multibyte UTF8 characters
615** count as a single character.
616*/
617static int strlenChar(const char *z){
618  int n = 0;
619  while( *z ){
620    if( (0xc0&*(z++))!=0x80 ) n++;
621  }
622  return n;
623}
624
625/*
626** Return true if zFile does not exist or if it is not an ordinary file.
627*/
628#ifdef _WIN32
629# define notNormalFile(X) 0
630#else
631static int notNormalFile(const char *zFile){
632  struct stat x;
633  int rc;
634  memset(&x, 0, sizeof(x));
635  rc = stat(zFile, &x);
636  return rc || !S_ISREG(x.st_mode);
637}
638#endif
639
640/*
641** This routine reads a line of text from FILE in, stores
642** the text in memory obtained from malloc() and returns a pointer
643** to the text.  NULL is returned at end of file, or if malloc()
644** fails.
645**
646** If zLine is not NULL then it is a malloced buffer returned from
647** a previous call to this routine that may be reused.
648*/
649static char *local_getline(char *zLine, FILE *in){
650  int nLine = zLine==0 ? 0 : 100;
651  int n = 0;
652
653  while( 1 ){
654    if( n+100>nLine ){
655      nLine = nLine*2 + 100;
656      zLine = realloc(zLine, nLine);
657      if( zLine==0 ) shell_out_of_memory();
658    }
659    if( fgets(&zLine[n], nLine - n, in)==0 ){
660      if( n==0 ){
661        free(zLine);
662        return 0;
663      }
664      zLine[n] = 0;
665      break;
666    }
667    while( zLine[n] ) n++;
668    if( n>0 && zLine[n-1]=='\n' ){
669      n--;
670      if( n>0 && zLine[n-1]=='\r' ) n--;
671      zLine[n] = 0;
672      break;
673    }
674  }
675#if defined(_WIN32) || defined(WIN32)
676  /* For interactive input on Windows systems, translate the
677  ** multi-byte characterset characters into UTF-8. */
678  if( stdin_is_interactive && in==stdin ){
679    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
680    if( zTrans ){
681      int nTrans = strlen30(zTrans)+1;
682      if( nTrans>nLine ){
683        zLine = realloc(zLine, nTrans);
684        if( zLine==0 ) shell_out_of_memory();
685      }
686      memcpy(zLine, zTrans, nTrans);
687      sqlite3_free(zTrans);
688    }
689  }
690#endif /* defined(_WIN32) || defined(WIN32) */
691  return zLine;
692}
693
694/*
695** Retrieve a single line of input text.
696**
697** If in==0 then read from standard input and prompt before each line.
698** If isContinuation is true, then a continuation prompt is appropriate.
699** If isContinuation is zero, then the main prompt should be used.
700**
701** If zPrior is not NULL then it is a buffer from a prior call to this
702** routine that can be reused.
703**
704** The result is stored in space obtained from malloc() and must either
705** be freed by the caller or else passed back into this routine via the
706** zPrior argument for reuse.
707*/
708static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
709  char *zPrompt;
710  char *zResult;
711  if( in!=0 ){
712    zResult = local_getline(zPrior, in);
713  }else{
714    zPrompt = isContinuation ? continuePrompt : mainPrompt;
715#if SHELL_USE_LOCAL_GETLINE
716    printf("%s", zPrompt);
717    fflush(stdout);
718    zResult = local_getline(zPrior, stdin);
719#else
720    free(zPrior);
721    zResult = shell_readline(zPrompt);
722    if( zResult && *zResult ) shell_add_history(zResult);
723#endif
724  }
725  return zResult;
726}
727
728
729/*
730** Return the value of a hexadecimal digit.  Return -1 if the input
731** is not a hex digit.
732*/
733static int hexDigitValue(char c){
734  if( c>='0' && c<='9' ) return c - '0';
735  if( c>='a' && c<='f' ) return c - 'a' + 10;
736  if( c>='A' && c<='F' ) return c - 'A' + 10;
737  return -1;
738}
739
740/*
741** Interpret zArg as an integer value, possibly with suffixes.
742*/
743static sqlite3_int64 integerValue(const char *zArg){
744  sqlite3_int64 v = 0;
745  static const struct { char *zSuffix; int iMult; } aMult[] = {
746    { "KiB", 1024 },
747    { "MiB", 1024*1024 },
748    { "GiB", 1024*1024*1024 },
749    { "KB",  1000 },
750    { "MB",  1000000 },
751    { "GB",  1000000000 },
752    { "K",   1000 },
753    { "M",   1000000 },
754    { "G",   1000000000 },
755  };
756  int i;
757  int isNeg = 0;
758  if( zArg[0]=='-' ){
759    isNeg = 1;
760    zArg++;
761  }else if( zArg[0]=='+' ){
762    zArg++;
763  }
764  if( zArg[0]=='0' && zArg[1]=='x' ){
765    int x;
766    zArg += 2;
767    while( (x = hexDigitValue(zArg[0]))>=0 ){
768      v = (v<<4) + x;
769      zArg++;
770    }
771  }else{
772    while( IsDigit(zArg[0]) ){
773      v = v*10 + zArg[0] - '0';
774      zArg++;
775    }
776  }
777  for(i=0; i<ArraySize(aMult); i++){
778    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
779      v *= aMult[i].iMult;
780      break;
781    }
782  }
783  return isNeg? -v : v;
784}
785
786/*
787** A variable length string to which one can append text.
788*/
789typedef struct ShellText ShellText;
790struct ShellText {
791  char *z;
792  int n;
793  int nAlloc;
794};
795
796/*
797** Initialize and destroy a ShellText object
798*/
799static void initText(ShellText *p){
800  memset(p, 0, sizeof(*p));
801}
802static void freeText(ShellText *p){
803  free(p->z);
804  initText(p);
805}
806
807/* zIn is either a pointer to a NULL-terminated string in memory obtained
808** from malloc(), or a NULL pointer. The string pointed to by zAppend is
809** added to zIn, and the result returned in memory obtained from malloc().
810** zIn, if it was not NULL, is freed.
811**
812** If the third argument, quote, is not '\0', then it is used as a
813** quote character for zAppend.
814*/
815static void appendText(ShellText *p, char const *zAppend, char quote){
816  int len;
817  int i;
818  int nAppend = strlen30(zAppend);
819
820  len = nAppend+p->n+1;
821  if( quote ){
822    len += 2;
823    for(i=0; i<nAppend; i++){
824      if( zAppend[i]==quote ) len++;
825    }
826  }
827
828  if( p->n+len>=p->nAlloc ){
829    p->nAlloc = p->nAlloc*2 + len + 20;
830    p->z = realloc(p->z, p->nAlloc);
831    if( p->z==0 ) shell_out_of_memory();
832  }
833
834  if( quote ){
835    char *zCsr = p->z+p->n;
836    *zCsr++ = quote;
837    for(i=0; i<nAppend; i++){
838      *zCsr++ = zAppend[i];
839      if( zAppend[i]==quote ) *zCsr++ = quote;
840    }
841    *zCsr++ = quote;
842    p->n = (int)(zCsr - p->z);
843    *zCsr = '\0';
844  }else{
845    memcpy(p->z+p->n, zAppend, nAppend);
846    p->n += nAppend;
847    p->z[p->n] = '\0';
848  }
849}
850
851/*
852** Attempt to determine if identifier zName needs to be quoted, either
853** because it contains non-alphanumeric characters, or because it is an
854** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
855** that quoting is required.
856**
857** Return '"' if quoting is required.  Return 0 if no quoting is required.
858*/
859static char quoteChar(const char *zName){
860  int i;
861  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
862  for(i=0; zName[i]; i++){
863    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
864  }
865  return sqlite3_keyword_check(zName, i) ? '"' : 0;
866}
867
868/*
869** Construct a fake object name and column list to describe the structure
870** of the view, virtual table, or table valued function zSchema.zName.
871*/
872static char *shellFakeSchema(
873  sqlite3 *db,            /* The database connection containing the vtab */
874  const char *zSchema,    /* Schema of the database holding the vtab */
875  const char *zName       /* The name of the virtual table */
876){
877  sqlite3_stmt *pStmt = 0;
878  char *zSql;
879  ShellText s;
880  char cQuote;
881  char *zDiv = "(";
882  int nRow = 0;
883
884  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
885                         zSchema ? zSchema : "main", zName);
886  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
887  sqlite3_free(zSql);
888  initText(&s);
889  if( zSchema ){
890    cQuote = quoteChar(zSchema);
891    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
892    appendText(&s, zSchema, cQuote);
893    appendText(&s, ".", 0);
894  }
895  cQuote = quoteChar(zName);
896  appendText(&s, zName, cQuote);
897  while( sqlite3_step(pStmt)==SQLITE_ROW ){
898    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
899    nRow++;
900    appendText(&s, zDiv, 0);
901    zDiv = ",";
902    cQuote = quoteChar(zCol);
903    appendText(&s, zCol, cQuote);
904  }
905  appendText(&s, ")", 0);
906  sqlite3_finalize(pStmt);
907  if( nRow==0 ){
908    freeText(&s);
909    s.z = 0;
910  }
911  return s.z;
912}
913
914/*
915** SQL function:  shell_module_schema(X)
916**
917** Return a fake schema for the table-valued function or eponymous virtual
918** table X.
919*/
920static void shellModuleSchema(
921  sqlite3_context *pCtx,
922  int nVal,
923  sqlite3_value **apVal
924){
925  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
926  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
927  UNUSED_PARAMETER(nVal);
928  if( zFake ){
929    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
930                        -1, sqlite3_free);
931    free(zFake);
932  }
933}
934
935/*
936** SQL function:  shell_add_schema(S,X)
937**
938** Add the schema name X to the CREATE statement in S and return the result.
939** Examples:
940**
941**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
942**
943** Also works on
944**
945**    CREATE INDEX
946**    CREATE UNIQUE INDEX
947**    CREATE VIEW
948**    CREATE TRIGGER
949**    CREATE VIRTUAL TABLE
950**
951** This UDF is used by the .schema command to insert the schema name of
952** attached databases into the middle of the sqlite_schema.sql field.
953*/
954static void shellAddSchemaName(
955  sqlite3_context *pCtx,
956  int nVal,
957  sqlite3_value **apVal
958){
959  static const char *aPrefix[] = {
960     "TABLE",
961     "INDEX",
962     "UNIQUE INDEX",
963     "VIEW",
964     "TRIGGER",
965     "VIRTUAL TABLE"
966  };
967  int i = 0;
968  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
969  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
970  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
971  sqlite3 *db = sqlite3_context_db_handle(pCtx);
972  UNUSED_PARAMETER(nVal);
973  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
974    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
975      int n = strlen30(aPrefix[i]);
976      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
977        char *z = 0;
978        char *zFake = 0;
979        if( zSchema ){
980          char cQuote = quoteChar(zSchema);
981          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
982            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
983          }else{
984            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
985          }
986        }
987        if( zName
988         && aPrefix[i][0]=='V'
989         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
990        ){
991          if( z==0 ){
992            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
993          }else{
994            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
995          }
996          free(zFake);
997        }
998        if( z ){
999          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1000          return;
1001        }
1002      }
1003    }
1004  }
1005  sqlite3_result_value(pCtx, apVal[0]);
1006}
1007
1008/*
1009** The source code for several run-time loadable extensions is inserted
1010** below by the ../tool/mkshellc.tcl script.  Before processing that included
1011** code, we need to override some macros to make the included program code
1012** work here in the middle of this regular program.
1013*/
1014#define SQLITE_EXTENSION_INIT1
1015#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1016
1017#if defined(_WIN32) && defined(_MSC_VER)
1018INCLUDE test_windirent.h
1019INCLUDE test_windirent.c
1020#define dirent DIRENT
1021#endif
1022INCLUDE ../ext/misc/shathree.c
1023INCLUDE ../ext/misc/fileio.c
1024INCLUDE ../ext/misc/completion.c
1025INCLUDE ../ext/misc/appendvfs.c
1026INCLUDE ../ext/misc/memtrace.c
1027INCLUDE ../ext/misc/uint.c
1028INCLUDE ../ext/misc/decimal.c
1029INCLUDE ../ext/misc/ieee754.c
1030INCLUDE ../ext/misc/series.c
1031INCLUDE ../ext/misc/regexp.c
1032#ifdef SQLITE_HAVE_ZLIB
1033INCLUDE ../ext/misc/zipfile.c
1034INCLUDE ../ext/misc/sqlar.c
1035#endif
1036INCLUDE ../ext/expert/sqlite3expert.h
1037INCLUDE ../ext/expert/sqlite3expert.c
1038
1039#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1040INCLUDE ../ext/misc/dbdata.c
1041#endif
1042
1043#if defined(SQLITE_ENABLE_SESSION)
1044/*
1045** State information for a single open session
1046*/
1047typedef struct OpenSession OpenSession;
1048struct OpenSession {
1049  char *zName;             /* Symbolic name for this session */
1050  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1051  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1052  sqlite3_session *p;      /* The open session */
1053};
1054#endif
1055
1056typedef struct ExpertInfo ExpertInfo;
1057struct ExpertInfo {
1058  sqlite3expert *pExpert;
1059  int bVerbose;
1060};
1061
1062/* A single line in the EQP output */
1063typedef struct EQPGraphRow EQPGraphRow;
1064struct EQPGraphRow {
1065  int iEqpId;           /* ID for this row */
1066  int iParentId;        /* ID of the parent row */
1067  EQPGraphRow *pNext;   /* Next row in sequence */
1068  char zText[1];        /* Text to display for this row */
1069};
1070
1071/* All EQP output is collected into an instance of the following */
1072typedef struct EQPGraph EQPGraph;
1073struct EQPGraph {
1074  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1075  EQPGraphRow *pLast;   /* Last element of the pRow list */
1076  char zPrefix[100];    /* Graph prefix */
1077};
1078
1079/*
1080** State information about the database connection is contained in an
1081** instance of the following structure.
1082*/
1083typedef struct ShellState ShellState;
1084struct ShellState {
1085  sqlite3 *db;           /* The database */
1086  u8 autoExplain;        /* Automatically turn on .explain mode */
1087  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1088  u8 autoEQPtest;        /* autoEQP is in test mode */
1089  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1090  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1091  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1092  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1093  u8 nEqpLevel;          /* Depth of the EQP output graph */
1094  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1095  unsigned statsOn;      /* True to display memory stats before each finalize */
1096  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1097  int outCount;          /* Revert to stdout when reaching zero */
1098  int cnt;               /* Number of records displayed so far */
1099  int lineno;            /* Line number of last line read from in */
1100  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1101  FILE *in;              /* Read commands from this stream */
1102  FILE *out;             /* Write results here */
1103  FILE *traceOut;        /* Output for sqlite3_trace() */
1104  int nErr;              /* Number of errors seen */
1105  int mode;              /* An output mode setting */
1106  int modePrior;         /* Saved mode */
1107  int cMode;             /* temporary output mode for the current query */
1108  int normalMode;        /* Output mode before ".explain on" */
1109  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1110  int showHeader;        /* True to show column names in List or Column mode */
1111  int nCheck;            /* Number of ".check" commands run */
1112  unsigned nProgress;    /* Number of progress callbacks encountered */
1113  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1114  unsigned flgProgress;  /* Flags for the progress callback */
1115  unsigned shellFlgs;    /* Various flags */
1116  unsigned priorShFlgs;  /* Saved copy of flags */
1117  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1118  char *zDestTable;      /* Name of destination table when MODE_Insert */
1119  char *zTempFile;       /* Temporary file that might need deleting */
1120  char zTestcase[30];    /* Name of current test case */
1121  char colSeparator[20]; /* Column separator character for several modes */
1122  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1123  char colSepPrior[20];  /* Saved column separator */
1124  char rowSepPrior[20];  /* Saved row separator */
1125  int *colWidth;         /* Requested width of each column in columnar modes */
1126  int *actualWidth;      /* Actual width of each column */
1127  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1128  char nullValue[20];    /* The text to print when a NULL comes back from
1129                         ** the database */
1130  char outfile[FILENAME_MAX]; /* Filename for *out */
1131  const char *zDbFilename;    /* name of the database file */
1132  char *zFreeOnClose;         /* Filename to free when closing */
1133  const char *zVfs;           /* Name of VFS to use */
1134  sqlite3_stmt *pStmt;   /* Current statement if any. */
1135  FILE *pLog;            /* Write log output here */
1136  int *aiIndent;         /* Array of indents used in MODE_Explain */
1137  int nIndent;           /* Size of array aiIndent[] */
1138  int iIndent;           /* Index of current op in aiIndent[] */
1139  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1140#if defined(SQLITE_ENABLE_SESSION)
1141  int nSession;             /* Number of active sessions */
1142  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1143#endif
1144  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1145};
1146
1147
1148/* Allowed values for ShellState.autoEQP
1149*/
1150#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1151#define AUTOEQP_on       1           /* Automatic EQP is on */
1152#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1153#define AUTOEQP_full     3           /* Show full EXPLAIN */
1154
1155/* Allowed values for ShellState.openMode
1156*/
1157#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1158#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1159#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1160#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1161#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1162#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1163#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1164
1165/* Allowed values for ShellState.eTraceType
1166*/
1167#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1168#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1169#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1170
1171/* Bits in the ShellState.flgProgress variable */
1172#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1173#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1174                                   ** callback limit is reached, and for each
1175                                   ** top-level SQL statement */
1176#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1177
1178/*
1179** These are the allowed shellFlgs values
1180*/
1181#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1182#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1183#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1184#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1185#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1186#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1187#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1188#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
1189#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1190#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1191
1192/*
1193** Macros for testing and setting shellFlgs
1194*/
1195#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1196#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1197#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1198
1199/*
1200** These are the allowed modes.
1201*/
1202#define MODE_Line     0  /* One column per line.  Blank line between records */
1203#define MODE_Column   1  /* One record per line in neat columns */
1204#define MODE_List     2  /* One record per line with a separator */
1205#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1206#define MODE_Html     4  /* Generate an XHTML table */
1207#define MODE_Insert   5  /* Generate SQL "insert" statements */
1208#define MODE_Quote    6  /* Quote values as for SQL */
1209#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1210#define MODE_Csv      8  /* Quote strings, numbers are plain */
1211#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1212#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1213#define MODE_Pretty  11  /* Pretty-print schemas */
1214#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1215#define MODE_Json    13  /* Output JSON */
1216#define MODE_Markdown 14 /* Markdown formatting */
1217#define MODE_Table   15  /* MySQL-style table formatting */
1218#define MODE_Box     16  /* Unicode box-drawing characters */
1219
1220static const char *modeDescr[] = {
1221  "line",
1222  "column",
1223  "list",
1224  "semi",
1225  "html",
1226  "insert",
1227  "quote",
1228  "tcl",
1229  "csv",
1230  "explain",
1231  "ascii",
1232  "prettyprint",
1233  "eqp",
1234  "json",
1235  "markdown",
1236  "table",
1237  "box"
1238};
1239
1240/*
1241** These are the column/row/line separators used by the various
1242** import/export modes.
1243*/
1244#define SEP_Column    "|"
1245#define SEP_Row       "\n"
1246#define SEP_Tab       "\t"
1247#define SEP_Space     " "
1248#define SEP_Comma     ","
1249#define SEP_CrLf      "\r\n"
1250#define SEP_Unit      "\x1F"
1251#define SEP_Record    "\x1E"
1252
1253/*
1254** A callback for the sqlite3_log() interface.
1255*/
1256static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1257  ShellState *p = (ShellState*)pArg;
1258  if( p->pLog==0 ) return;
1259  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1260  fflush(p->pLog);
1261}
1262
1263/*
1264** SQL function:  shell_putsnl(X)
1265**
1266** Write the text X to the screen (or whatever output is being directed)
1267** adding a newline at the end, and then return X.
1268*/
1269static void shellPutsFunc(
1270  sqlite3_context *pCtx,
1271  int nVal,
1272  sqlite3_value **apVal
1273){
1274  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1275  (void)nVal;
1276  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1277  sqlite3_result_value(pCtx, apVal[0]);
1278}
1279
1280/*
1281** SQL function:   edit(VALUE)
1282**                 edit(VALUE,EDITOR)
1283**
1284** These steps:
1285**
1286**     (1) Write VALUE into a temporary file.
1287**     (2) Run program EDITOR on that temporary file.
1288**     (3) Read the temporary file back and return its content as the result.
1289**     (4) Delete the temporary file
1290**
1291** If the EDITOR argument is omitted, use the value in the VISUAL
1292** environment variable.  If still there is no EDITOR, through an error.
1293**
1294** Also throw an error if the EDITOR program returns a non-zero exit code.
1295*/
1296#ifndef SQLITE_NOHAVE_SYSTEM
1297static void editFunc(
1298  sqlite3_context *context,
1299  int argc,
1300  sqlite3_value **argv
1301){
1302  const char *zEditor;
1303  char *zTempFile = 0;
1304  sqlite3 *db;
1305  char *zCmd = 0;
1306  int bBin;
1307  int rc;
1308  int hasCRNL = 0;
1309  FILE *f = 0;
1310  sqlite3_int64 sz;
1311  sqlite3_int64 x;
1312  unsigned char *p = 0;
1313
1314  if( argc==2 ){
1315    zEditor = (const char*)sqlite3_value_text(argv[1]);
1316  }else{
1317    zEditor = getenv("VISUAL");
1318  }
1319  if( zEditor==0 ){
1320    sqlite3_result_error(context, "no editor for edit()", -1);
1321    return;
1322  }
1323  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1324    sqlite3_result_error(context, "NULL input to edit()", -1);
1325    return;
1326  }
1327  db = sqlite3_context_db_handle(context);
1328  zTempFile = 0;
1329  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1330  if( zTempFile==0 ){
1331    sqlite3_uint64 r = 0;
1332    sqlite3_randomness(sizeof(r), &r);
1333    zTempFile = sqlite3_mprintf("temp%llx", r);
1334    if( zTempFile==0 ){
1335      sqlite3_result_error_nomem(context);
1336      return;
1337    }
1338  }
1339  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1340  /* When writing the file to be edited, do \n to \r\n conversions on systems
1341  ** that want \r\n line endings */
1342  f = fopen(zTempFile, bBin ? "wb" : "w");
1343  if( f==0 ){
1344    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1345    goto edit_func_end;
1346  }
1347  sz = sqlite3_value_bytes(argv[0]);
1348  if( bBin ){
1349    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1350  }else{
1351    const char *z = (const char*)sqlite3_value_text(argv[0]);
1352    /* Remember whether or not the value originally contained \r\n */
1353    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1354    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1355  }
1356  fclose(f);
1357  f = 0;
1358  if( x!=sz ){
1359    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1360    goto edit_func_end;
1361  }
1362  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1363  if( zCmd==0 ){
1364    sqlite3_result_error_nomem(context);
1365    goto edit_func_end;
1366  }
1367  rc = system(zCmd);
1368  sqlite3_free(zCmd);
1369  if( rc ){
1370    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1371    goto edit_func_end;
1372  }
1373  f = fopen(zTempFile, "rb");
1374  if( f==0 ){
1375    sqlite3_result_error(context,
1376      "edit() cannot reopen temp file after edit", -1);
1377    goto edit_func_end;
1378  }
1379  fseek(f, 0, SEEK_END);
1380  sz = ftell(f);
1381  rewind(f);
1382  p = sqlite3_malloc64( sz+1 );
1383  if( p==0 ){
1384    sqlite3_result_error_nomem(context);
1385    goto edit_func_end;
1386  }
1387  x = fread(p, 1, (size_t)sz, f);
1388  fclose(f);
1389  f = 0;
1390  if( x!=sz ){
1391    sqlite3_result_error(context, "could not read back the whole file", -1);
1392    goto edit_func_end;
1393  }
1394  if( bBin ){
1395    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1396  }else{
1397    sqlite3_int64 i, j;
1398    if( hasCRNL ){
1399      /* If the original contains \r\n then do no conversions back to \n */
1400      j = sz;
1401    }else{
1402      /* If the file did not originally contain \r\n then convert any new
1403      ** \r\n back into \n */
1404      for(i=j=0; i<sz; i++){
1405        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1406        p[j++] = p[i];
1407      }
1408      sz = j;
1409      p[sz] = 0;
1410    }
1411    sqlite3_result_text64(context, (const char*)p, sz,
1412                          sqlite3_free, SQLITE_UTF8);
1413  }
1414  p = 0;
1415
1416edit_func_end:
1417  if( f ) fclose(f);
1418  unlink(zTempFile);
1419  sqlite3_free(zTempFile);
1420  sqlite3_free(p);
1421}
1422#endif /* SQLITE_NOHAVE_SYSTEM */
1423
1424/*
1425** Save or restore the current output mode
1426*/
1427static void outputModePush(ShellState *p){
1428  p->modePrior = p->mode;
1429  p->priorShFlgs = p->shellFlgs;
1430  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1431  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1432}
1433static void outputModePop(ShellState *p){
1434  p->mode = p->modePrior;
1435  p->shellFlgs = p->priorShFlgs;
1436  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1437  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1438}
1439
1440/*
1441** Output the given string as a hex-encoded blob (eg. X'1234' )
1442*/
1443static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1444  int i;
1445  char *zBlob = (char *)pBlob;
1446  raw_printf(out,"X'");
1447  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1448  raw_printf(out,"'");
1449}
1450
1451/*
1452** Find a string that is not found anywhere in z[].  Return a pointer
1453** to that string.
1454**
1455** Try to use zA and zB first.  If both of those are already found in z[]
1456** then make up some string and store it in the buffer zBuf.
1457*/
1458static const char *unused_string(
1459  const char *z,                    /* Result must not appear anywhere in z */
1460  const char *zA, const char *zB,   /* Try these first */
1461  char *zBuf                        /* Space to store a generated string */
1462){
1463  unsigned i = 0;
1464  if( strstr(z, zA)==0 ) return zA;
1465  if( strstr(z, zB)==0 ) return zB;
1466  do{
1467    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1468  }while( strstr(z,zBuf)!=0 );
1469  return zBuf;
1470}
1471
1472/*
1473** Output the given string as a quoted string using SQL quoting conventions.
1474**
1475** See also: output_quoted_escaped_string()
1476*/
1477static void output_quoted_string(FILE *out, const char *z){
1478  int i;
1479  char c;
1480  setBinaryMode(out, 1);
1481  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1482  if( c==0 ){
1483    utf8_printf(out,"'%s'",z);
1484  }else{
1485    raw_printf(out, "'");
1486    while( *z ){
1487      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1488      if( c=='\'' ) i++;
1489      if( i ){
1490        utf8_printf(out, "%.*s", i, z);
1491        z += i;
1492      }
1493      if( c=='\'' ){
1494        raw_printf(out, "'");
1495        continue;
1496      }
1497      if( c==0 ){
1498        break;
1499      }
1500      z++;
1501    }
1502    raw_printf(out, "'");
1503  }
1504  setTextMode(out, 1);
1505}
1506
1507/*
1508** Output the given string as a quoted string using SQL quoting conventions.
1509** Additionallly , escape the "\n" and "\r" characters so that they do not
1510** get corrupted by end-of-line translation facilities in some operating
1511** systems.
1512**
1513** This is like output_quoted_string() but with the addition of the \r\n
1514** escape mechanism.
1515*/
1516static void output_quoted_escaped_string(FILE *out, const char *z){
1517  int i;
1518  char c;
1519  setBinaryMode(out, 1);
1520  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1521  if( c==0 ){
1522    utf8_printf(out,"'%s'",z);
1523  }else{
1524    const char *zNL = 0;
1525    const char *zCR = 0;
1526    int nNL = 0;
1527    int nCR = 0;
1528    char zBuf1[20], zBuf2[20];
1529    for(i=0; z[i]; i++){
1530      if( z[i]=='\n' ) nNL++;
1531      if( z[i]=='\r' ) nCR++;
1532    }
1533    if( nNL ){
1534      raw_printf(out, "replace(");
1535      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1536    }
1537    if( nCR ){
1538      raw_printf(out, "replace(");
1539      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1540    }
1541    raw_printf(out, "'");
1542    while( *z ){
1543      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1544      if( c=='\'' ) i++;
1545      if( i ){
1546        utf8_printf(out, "%.*s", i, z);
1547        z += i;
1548      }
1549      if( c=='\'' ){
1550        raw_printf(out, "'");
1551        continue;
1552      }
1553      if( c==0 ){
1554        break;
1555      }
1556      z++;
1557      if( c=='\n' ){
1558        raw_printf(out, "%s", zNL);
1559        continue;
1560      }
1561      raw_printf(out, "%s", zCR);
1562    }
1563    raw_printf(out, "'");
1564    if( nCR ){
1565      raw_printf(out, ",'%s',char(13))", zCR);
1566    }
1567    if( nNL ){
1568      raw_printf(out, ",'%s',char(10))", zNL);
1569    }
1570  }
1571  setTextMode(out, 1);
1572}
1573
1574/*
1575** Output the given string as a quoted according to C or TCL quoting rules.
1576*/
1577static void output_c_string(FILE *out, const char *z){
1578  unsigned int c;
1579  fputc('"', out);
1580  while( (c = *(z++))!=0 ){
1581    if( c=='\\' ){
1582      fputc(c, out);
1583      fputc(c, out);
1584    }else if( c=='"' ){
1585      fputc('\\', out);
1586      fputc('"', out);
1587    }else if( c=='\t' ){
1588      fputc('\\', out);
1589      fputc('t', out);
1590    }else if( c=='\n' ){
1591      fputc('\\', out);
1592      fputc('n', out);
1593    }else if( c=='\r' ){
1594      fputc('\\', out);
1595      fputc('r', out);
1596    }else if( !isprint(c&0xff) ){
1597      raw_printf(out, "\\%03o", c&0xff);
1598    }else{
1599      fputc(c, out);
1600    }
1601  }
1602  fputc('"', out);
1603}
1604
1605/*
1606** Output the given string as a quoted according to JSON quoting rules.
1607*/
1608static void output_json_string(FILE *out, const char *z, int n){
1609  unsigned int c;
1610  if( n<0 ) n = (int)strlen(z);
1611  fputc('"', out);
1612  while( n-- ){
1613    c = *(z++);
1614    if( c=='\\' || c=='"' ){
1615      fputc('\\', out);
1616      fputc(c, out);
1617    }else if( c<=0x1f ){
1618      fputc('\\', out);
1619      if( c=='\b' ){
1620        fputc('b', out);
1621      }else if( c=='\f' ){
1622        fputc('f', out);
1623      }else if( c=='\n' ){
1624        fputc('n', out);
1625      }else if( c=='\r' ){
1626        fputc('r', out);
1627      }else if( c=='\t' ){
1628        fputc('t', out);
1629      }else{
1630         raw_printf(out, "u%04x",c);
1631      }
1632    }else{
1633      fputc(c, out);
1634    }
1635  }
1636  fputc('"', out);
1637}
1638
1639/*
1640** Output the given string with characters that are special to
1641** HTML escaped.
1642*/
1643static void output_html_string(FILE *out, const char *z){
1644  int i;
1645  if( z==0 ) z = "";
1646  while( *z ){
1647    for(i=0;   z[i]
1648            && z[i]!='<'
1649            && z[i]!='&'
1650            && z[i]!='>'
1651            && z[i]!='\"'
1652            && z[i]!='\'';
1653        i++){}
1654    if( i>0 ){
1655      utf8_printf(out,"%.*s",i,z);
1656    }
1657    if( z[i]=='<' ){
1658      raw_printf(out,"&lt;");
1659    }else if( z[i]=='&' ){
1660      raw_printf(out,"&amp;");
1661    }else if( z[i]=='>' ){
1662      raw_printf(out,"&gt;");
1663    }else if( z[i]=='\"' ){
1664      raw_printf(out,"&quot;");
1665    }else if( z[i]=='\'' ){
1666      raw_printf(out,"&#39;");
1667    }else{
1668      break;
1669    }
1670    z += i + 1;
1671  }
1672}
1673
1674/*
1675** If a field contains any character identified by a 1 in the following
1676** array, then the string must be quoted for CSV.
1677*/
1678static const char needCsvQuote[] = {
1679  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1680  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1681  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1682  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1683  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1684  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1685  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1686  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1687  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1688  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1689  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1690  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1691  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1692  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1693  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1694  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1695};
1696
1697/*
1698** Output a single term of CSV.  Actually, p->colSeparator is used for
1699** the separator, which may or may not be a comma.  p->nullValue is
1700** the null value.  Strings are quoted if necessary.  The separator
1701** is only issued if bSep is true.
1702*/
1703static void output_csv(ShellState *p, const char *z, int bSep){
1704  FILE *out = p->out;
1705  if( z==0 ){
1706    utf8_printf(out,"%s",p->nullValue);
1707  }else{
1708    int i;
1709    int nSep = strlen30(p->colSeparator);
1710    for(i=0; z[i]; i++){
1711      if( needCsvQuote[((unsigned char*)z)[i]]
1712         || (z[i]==p->colSeparator[0] &&
1713             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1714        i = 0;
1715        break;
1716      }
1717    }
1718    if( i==0 ){
1719      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1720      utf8_printf(out, "%s", zQuoted);
1721      sqlite3_free(zQuoted);
1722    }else{
1723      utf8_printf(out, "%s", z);
1724    }
1725  }
1726  if( bSep ){
1727    utf8_printf(p->out, "%s", p->colSeparator);
1728  }
1729}
1730
1731/*
1732** This routine runs when the user presses Ctrl-C
1733*/
1734static void interrupt_handler(int NotUsed){
1735  UNUSED_PARAMETER(NotUsed);
1736  seenInterrupt++;
1737  if( seenInterrupt>2 ) exit(1);
1738  if( globalDb ) sqlite3_interrupt(globalDb);
1739}
1740
1741#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1742/*
1743** This routine runs for console events (e.g. Ctrl-C) on Win32
1744*/
1745static BOOL WINAPI ConsoleCtrlHandler(
1746  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1747){
1748  if( dwCtrlType==CTRL_C_EVENT ){
1749    interrupt_handler(0);
1750    return TRUE;
1751  }
1752  return FALSE;
1753}
1754#endif
1755
1756#ifndef SQLITE_OMIT_AUTHORIZATION
1757/*
1758** When the ".auth ON" is set, the following authorizer callback is
1759** invoked.  It always returns SQLITE_OK.
1760*/
1761static int shellAuth(
1762  void *pClientData,
1763  int op,
1764  const char *zA1,
1765  const char *zA2,
1766  const char *zA3,
1767  const char *zA4
1768){
1769  ShellState *p = (ShellState*)pClientData;
1770  static const char *azAction[] = { 0,
1771     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1772     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1773     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1774     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1775     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1776     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1777     "PRAGMA",               "READ",                 "SELECT",
1778     "TRANSACTION",          "UPDATE",               "ATTACH",
1779     "DETACH",               "ALTER_TABLE",          "REINDEX",
1780     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1781     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1782  };
1783  int i;
1784  const char *az[4];
1785  az[0] = zA1;
1786  az[1] = zA2;
1787  az[2] = zA3;
1788  az[3] = zA4;
1789  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1790  for(i=0; i<4; i++){
1791    raw_printf(p->out, " ");
1792    if( az[i] ){
1793      output_c_string(p->out, az[i]);
1794    }else{
1795      raw_printf(p->out, "NULL");
1796    }
1797  }
1798  raw_printf(p->out, "\n");
1799  return SQLITE_OK;
1800}
1801#endif
1802
1803/*
1804** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1805**
1806** This routine converts some CREATE TABLE statements for shadow tables
1807** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1808*/
1809static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1810  if( z==0 ) return;
1811  if( zTail==0 ) return;
1812  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1813    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1814  }else{
1815    utf8_printf(out, "%s%s", z, zTail);
1816  }
1817}
1818static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1819  char c = z[n];
1820  z[n] = 0;
1821  printSchemaLine(out, z, zTail);
1822  z[n] = c;
1823}
1824
1825/*
1826** Return true if string z[] has nothing but whitespace and comments to the
1827** end of the first line.
1828*/
1829static int wsToEol(const char *z){
1830  int i;
1831  for(i=0; z[i]; i++){
1832    if( z[i]=='\n' ) return 1;
1833    if( IsSpace(z[i]) ) continue;
1834    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1835    return 0;
1836  }
1837  return 1;
1838}
1839
1840/*
1841** Add a new entry to the EXPLAIN QUERY PLAN data
1842*/
1843static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1844  EQPGraphRow *pNew;
1845  int nText = strlen30(zText);
1846  if( p->autoEQPtest ){
1847    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1848  }
1849  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1850  if( pNew==0 ) shell_out_of_memory();
1851  pNew->iEqpId = iEqpId;
1852  pNew->iParentId = p2;
1853  memcpy(pNew->zText, zText, nText+1);
1854  pNew->pNext = 0;
1855  if( p->sGraph.pLast ){
1856    p->sGraph.pLast->pNext = pNew;
1857  }else{
1858    p->sGraph.pRow = pNew;
1859  }
1860  p->sGraph.pLast = pNew;
1861}
1862
1863/*
1864** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1865** in p->sGraph.
1866*/
1867static void eqp_reset(ShellState *p){
1868  EQPGraphRow *pRow, *pNext;
1869  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1870    pNext = pRow->pNext;
1871    sqlite3_free(pRow);
1872  }
1873  memset(&p->sGraph, 0, sizeof(p->sGraph));
1874}
1875
1876/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1877** pOld, or return the first such line if pOld is NULL
1878*/
1879static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1880  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1881  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1882  return pRow;
1883}
1884
1885/* Render a single level of the graph that has iEqpId as its parent.  Called
1886** recursively to render sublevels.
1887*/
1888static void eqp_render_level(ShellState *p, int iEqpId){
1889  EQPGraphRow *pRow, *pNext;
1890  int n = strlen30(p->sGraph.zPrefix);
1891  char *z;
1892  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1893    pNext = eqp_next_row(p, iEqpId, pRow);
1894    z = pRow->zText;
1895    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1896                pNext ? "|--" : "`--", z);
1897    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1898      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1899      eqp_render_level(p, pRow->iEqpId);
1900      p->sGraph.zPrefix[n] = 0;
1901    }
1902  }
1903}
1904
1905/*
1906** Display and reset the EXPLAIN QUERY PLAN data
1907*/
1908static void eqp_render(ShellState *p){
1909  EQPGraphRow *pRow = p->sGraph.pRow;
1910  if( pRow ){
1911    if( pRow->zText[0]=='-' ){
1912      if( pRow->pNext==0 ){
1913        eqp_reset(p);
1914        return;
1915      }
1916      utf8_printf(p->out, "%s\n", pRow->zText+3);
1917      p->sGraph.pRow = pRow->pNext;
1918      sqlite3_free(pRow);
1919    }else{
1920      utf8_printf(p->out, "QUERY PLAN\n");
1921    }
1922    p->sGraph.zPrefix[0] = 0;
1923    eqp_render_level(p, 0);
1924    eqp_reset(p);
1925  }
1926}
1927
1928#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1929/*
1930** Progress handler callback.
1931*/
1932static int progress_handler(void *pClientData) {
1933  ShellState *p = (ShellState*)pClientData;
1934  p->nProgress++;
1935  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1936    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1937    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1938    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1939    return 1;
1940  }
1941  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1942    raw_printf(p->out, "Progress %u\n", p->nProgress);
1943  }
1944  return 0;
1945}
1946#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1947
1948/*
1949** Print N dashes
1950*/
1951static void print_dashes(FILE *out, int N){
1952  const char zDash[] = "--------------------------------------------------";
1953  const int nDash = sizeof(zDash) - 1;
1954  while( N>nDash ){
1955    fputs(zDash, out);
1956    N -= nDash;
1957  }
1958  raw_printf(out, "%.*s", N, zDash);
1959}
1960
1961/*
1962** Print a markdown or table-style row separator using ascii-art
1963*/
1964static void print_row_separator(
1965  ShellState *p,
1966  int nArg,
1967  const char *zSep
1968){
1969  int i;
1970  if( nArg>0 ){
1971    fputs(zSep, p->out);
1972    print_dashes(p->out, p->actualWidth[0]+2);
1973    for(i=1; i<nArg; i++){
1974      fputs(zSep, p->out);
1975      print_dashes(p->out, p->actualWidth[i]+2);
1976    }
1977    fputs(zSep, p->out);
1978  }
1979  fputs("\n", p->out);
1980}
1981
1982/*
1983** This is the callback routine that the shell
1984** invokes for each row of a query result.
1985*/
1986static int shell_callback(
1987  void *pArg,
1988  int nArg,        /* Number of result columns */
1989  char **azArg,    /* Text of each result column */
1990  char **azCol,    /* Column names */
1991  int *aiType      /* Column types.  Might be NULL */
1992){
1993  int i;
1994  ShellState *p = (ShellState*)pArg;
1995
1996  if( azArg==0 ) return 0;
1997  switch( p->cMode ){
1998    case MODE_Line: {
1999      int w = 5;
2000      if( azArg==0 ) break;
2001      for(i=0; i<nArg; i++){
2002        int len = strlen30(azCol[i] ? azCol[i] : "");
2003        if( len>w ) w = len;
2004      }
2005      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2006      for(i=0; i<nArg; i++){
2007        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2008                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2009      }
2010      break;
2011    }
2012    case MODE_Explain: {
2013      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2014      if( nArg>ArraySize(aExplainWidth) ){
2015        nArg = ArraySize(aExplainWidth);
2016      }
2017      if( p->cnt++==0 ){
2018        for(i=0; i<nArg; i++){
2019          int w = aExplainWidth[i];
2020          utf8_width_print(p->out, w, azCol[i]);
2021          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2022        }
2023        for(i=0; i<nArg; i++){
2024          int w = aExplainWidth[i];
2025          print_dashes(p->out, w);
2026          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2027        }
2028      }
2029      if( azArg==0 ) break;
2030      for(i=0; i<nArg; i++){
2031        int w = aExplainWidth[i];
2032        if( i==nArg-1 ) w = 0;
2033        if( azArg[i] && strlenChar(azArg[i])>w ){
2034          w = strlenChar(azArg[i]);
2035        }
2036        if( i==1 && p->aiIndent && p->pStmt ){
2037          if( p->iIndent<p->nIndent ){
2038            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2039          }
2040          p->iIndent++;
2041        }
2042        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2043        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2044      }
2045      break;
2046    }
2047    case MODE_Semi: {   /* .schema and .fullschema output */
2048      printSchemaLine(p->out, azArg[0], ";\n");
2049      break;
2050    }
2051    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2052      char *z;
2053      int j;
2054      int nParen = 0;
2055      char cEnd = 0;
2056      char c;
2057      int nLine = 0;
2058      assert( nArg==1 );
2059      if( azArg[0]==0 ) break;
2060      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2061       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2062      ){
2063        utf8_printf(p->out, "%s;\n", azArg[0]);
2064        break;
2065      }
2066      z = sqlite3_mprintf("%s", azArg[0]);
2067      j = 0;
2068      for(i=0; IsSpace(z[i]); i++){}
2069      for(; (c = z[i])!=0; i++){
2070        if( IsSpace(c) ){
2071          if( z[j-1]=='\r' ) z[j-1] = '\n';
2072          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2073        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2074          j--;
2075        }
2076        z[j++] = c;
2077      }
2078      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2079      z[j] = 0;
2080      if( strlen30(z)>=79 ){
2081        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2082          if( c==cEnd ){
2083            cEnd = 0;
2084          }else if( c=='"' || c=='\'' || c=='`' ){
2085            cEnd = c;
2086          }else if( c=='[' ){
2087            cEnd = ']';
2088          }else if( c=='-' && z[i+1]=='-' ){
2089            cEnd = '\n';
2090          }else if( c=='(' ){
2091            nParen++;
2092          }else if( c==')' ){
2093            nParen--;
2094            if( nLine>0 && nParen==0 && j>0 ){
2095              printSchemaLineN(p->out, z, j, "\n");
2096              j = 0;
2097            }
2098          }
2099          z[j++] = c;
2100          if( nParen==1 && cEnd==0
2101           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2102          ){
2103            if( c=='\n' ) j--;
2104            printSchemaLineN(p->out, z, j, "\n  ");
2105            j = 0;
2106            nLine++;
2107            while( IsSpace(z[i+1]) ){ i++; }
2108          }
2109        }
2110        z[j] = 0;
2111      }
2112      printSchemaLine(p->out, z, ";\n");
2113      sqlite3_free(z);
2114      break;
2115    }
2116    case MODE_List: {
2117      if( p->cnt++==0 && p->showHeader ){
2118        for(i=0; i<nArg; i++){
2119          utf8_printf(p->out,"%s%s",azCol[i],
2120                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2121        }
2122      }
2123      if( azArg==0 ) break;
2124      for(i=0; i<nArg; i++){
2125        char *z = azArg[i];
2126        if( z==0 ) z = p->nullValue;
2127        utf8_printf(p->out, "%s", z);
2128        if( i<nArg-1 ){
2129          utf8_printf(p->out, "%s", p->colSeparator);
2130        }else{
2131          utf8_printf(p->out, "%s", p->rowSeparator);
2132        }
2133      }
2134      break;
2135    }
2136    case MODE_Html: {
2137      if( p->cnt++==0 && p->showHeader ){
2138        raw_printf(p->out,"<TR>");
2139        for(i=0; i<nArg; i++){
2140          raw_printf(p->out,"<TH>");
2141          output_html_string(p->out, azCol[i]);
2142          raw_printf(p->out,"</TH>\n");
2143        }
2144        raw_printf(p->out,"</TR>\n");
2145      }
2146      if( azArg==0 ) break;
2147      raw_printf(p->out,"<TR>");
2148      for(i=0; i<nArg; i++){
2149        raw_printf(p->out,"<TD>");
2150        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2151        raw_printf(p->out,"</TD>\n");
2152      }
2153      raw_printf(p->out,"</TR>\n");
2154      break;
2155    }
2156    case MODE_Tcl: {
2157      if( p->cnt++==0 && p->showHeader ){
2158        for(i=0; i<nArg; i++){
2159          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2160          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2161        }
2162        utf8_printf(p->out, "%s", p->rowSeparator);
2163      }
2164      if( azArg==0 ) break;
2165      for(i=0; i<nArg; i++){
2166        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2167        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2168      }
2169      utf8_printf(p->out, "%s", p->rowSeparator);
2170      break;
2171    }
2172    case MODE_Csv: {
2173      setBinaryMode(p->out, 1);
2174      if( p->cnt++==0 && p->showHeader ){
2175        for(i=0; i<nArg; i++){
2176          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2177        }
2178        utf8_printf(p->out, "%s", p->rowSeparator);
2179      }
2180      if( nArg>0 ){
2181        for(i=0; i<nArg; i++){
2182          output_csv(p, azArg[i], i<nArg-1);
2183        }
2184        utf8_printf(p->out, "%s", p->rowSeparator);
2185      }
2186      setTextMode(p->out, 1);
2187      break;
2188    }
2189    case MODE_Insert: {
2190      if( azArg==0 ) break;
2191      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2192      if( p->showHeader ){
2193        raw_printf(p->out,"(");
2194        for(i=0; i<nArg; i++){
2195          if( i>0 ) raw_printf(p->out, ",");
2196          if( quoteChar(azCol[i]) ){
2197            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2198            utf8_printf(p->out, "%s", z);
2199            sqlite3_free(z);
2200          }else{
2201            raw_printf(p->out, "%s", azCol[i]);
2202          }
2203        }
2204        raw_printf(p->out,")");
2205      }
2206      p->cnt++;
2207      for(i=0; i<nArg; i++){
2208        raw_printf(p->out, i>0 ? "," : " VALUES(");
2209        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2210          utf8_printf(p->out,"NULL");
2211        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2212          if( ShellHasFlag(p, SHFLG_Newlines) ){
2213            output_quoted_string(p->out, azArg[i]);
2214          }else{
2215            output_quoted_escaped_string(p->out, azArg[i]);
2216          }
2217        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2218          utf8_printf(p->out,"%s", azArg[i]);
2219        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2220          char z[50];
2221          double r = sqlite3_column_double(p->pStmt, i);
2222          sqlite3_uint64 ur;
2223          memcpy(&ur,&r,sizeof(r));
2224          if( ur==0x7ff0000000000000LL ){
2225            raw_printf(p->out, "1e999");
2226          }else if( ur==0xfff0000000000000LL ){
2227            raw_printf(p->out, "-1e999");
2228          }else{
2229            sqlite3_snprintf(50,z,"%!.20g", r);
2230            raw_printf(p->out, "%s", z);
2231          }
2232        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2233          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2234          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2235          output_hex_blob(p->out, pBlob, nBlob);
2236        }else if( isNumber(azArg[i], 0) ){
2237          utf8_printf(p->out,"%s", azArg[i]);
2238        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2239          output_quoted_string(p->out, azArg[i]);
2240        }else{
2241          output_quoted_escaped_string(p->out, azArg[i]);
2242        }
2243      }
2244      raw_printf(p->out,");\n");
2245      break;
2246    }
2247    case MODE_Json: {
2248      if( azArg==0 ) break;
2249      if( p->cnt==0 ){
2250        fputs("[{", p->out);
2251      }else{
2252        fputs(",\n{", p->out);
2253      }
2254      p->cnt++;
2255      for(i=0; i<nArg; i++){
2256        output_json_string(p->out, azCol[i], -1);
2257        putc(':', p->out);
2258        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2259          fputs("null",p->out);
2260        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2261          char z[50];
2262          double r = sqlite3_column_double(p->pStmt, i);
2263          sqlite3_uint64 ur;
2264          memcpy(&ur,&r,sizeof(r));
2265          if( ur==0x7ff0000000000000LL ){
2266            raw_printf(p->out, "1e999");
2267          }else if( ur==0xfff0000000000000LL ){
2268            raw_printf(p->out, "-1e999");
2269          }else{
2270            sqlite3_snprintf(50,z,"%!.20g", r);
2271            raw_printf(p->out, "%s", z);
2272          }
2273        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2274          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2275          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2276          output_json_string(p->out, pBlob, nBlob);
2277        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2278          output_json_string(p->out, azArg[i], -1);
2279        }else{
2280          utf8_printf(p->out,"%s", azArg[i]);
2281        }
2282        if( i<nArg-1 ){
2283          putc(',', p->out);
2284        }
2285      }
2286      putc('}', p->out);
2287      break;
2288    }
2289    case MODE_Quote: {
2290      if( azArg==0 ) break;
2291      if( p->cnt==0 && p->showHeader ){
2292        for(i=0; i<nArg; i++){
2293          if( i>0 ) fputs(p->colSeparator, p->out);
2294          output_quoted_string(p->out, azCol[i]);
2295        }
2296        fputs(p->rowSeparator, p->out);
2297      }
2298      p->cnt++;
2299      for(i=0; i<nArg; i++){
2300        if( i>0 ) fputs(p->colSeparator, p->out);
2301        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2302          utf8_printf(p->out,"NULL");
2303        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2304          output_quoted_string(p->out, azArg[i]);
2305        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2306          utf8_printf(p->out,"%s", azArg[i]);
2307        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2308          char z[50];
2309          double r = sqlite3_column_double(p->pStmt, i);
2310          sqlite3_snprintf(50,z,"%!.20g", r);
2311          raw_printf(p->out, "%s", z);
2312        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2313          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2314          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2315          output_hex_blob(p->out, pBlob, nBlob);
2316        }else if( isNumber(azArg[i], 0) ){
2317          utf8_printf(p->out,"%s", azArg[i]);
2318        }else{
2319          output_quoted_string(p->out, azArg[i]);
2320        }
2321      }
2322      fputs(p->rowSeparator, p->out);
2323      break;
2324    }
2325    case MODE_Ascii: {
2326      if( p->cnt++==0 && p->showHeader ){
2327        for(i=0; i<nArg; i++){
2328          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2329          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2330        }
2331        utf8_printf(p->out, "%s", p->rowSeparator);
2332      }
2333      if( azArg==0 ) break;
2334      for(i=0; i<nArg; i++){
2335        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2336        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2337      }
2338      utf8_printf(p->out, "%s", p->rowSeparator);
2339      break;
2340    }
2341    case MODE_EQP: {
2342      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2343      break;
2344    }
2345  }
2346  return 0;
2347}
2348
2349/*
2350** This is the callback routine that the SQLite library
2351** invokes for each row of a query result.
2352*/
2353static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2354  /* since we don't have type info, call the shell_callback with a NULL value */
2355  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2356}
2357
2358/*
2359** This is the callback routine from sqlite3_exec() that appends all
2360** output onto the end of a ShellText object.
2361*/
2362static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2363  ShellText *p = (ShellText*)pArg;
2364  int i;
2365  UNUSED_PARAMETER(az);
2366  if( azArg==0 ) return 0;
2367  if( p->n ) appendText(p, "|", 0);
2368  for(i=0; i<nArg; i++){
2369    if( i ) appendText(p, ",", 0);
2370    if( azArg[i] ) appendText(p, azArg[i], 0);
2371  }
2372  return 0;
2373}
2374
2375/*
2376** Generate an appropriate SELFTEST table in the main database.
2377*/
2378static void createSelftestTable(ShellState *p){
2379  char *zErrMsg = 0;
2380  sqlite3_exec(p->db,
2381    "SAVEPOINT selftest_init;\n"
2382    "CREATE TABLE IF NOT EXISTS selftest(\n"
2383    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2384    "  op TEXT,\n"                   /* Operator:  memo run */
2385    "  cmd TEXT,\n"                  /* Command text */
2386    "  ans TEXT\n"                   /* Desired answer */
2387    ");"
2388    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2389    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2390    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2391    "         'memo','Tests generated by --init');\n"
2392    "INSERT INTO [_shell$self]\n"
2393    "  SELECT 'run',\n"
2394    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2395                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2396    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2397                          "FROM sqlite_schema ORDER BY 2',224));\n"
2398    "INSERT INTO [_shell$self]\n"
2399    "  SELECT 'run',"
2400    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2401    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2402    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2403    "  FROM (\n"
2404    "    SELECT name FROM sqlite_schema\n"
2405    "     WHERE type='table'\n"
2406    "       AND name<>'selftest'\n"
2407    "       AND coalesce(rootpage,0)>0\n"
2408    "  )\n"
2409    " ORDER BY name;\n"
2410    "INSERT INTO [_shell$self]\n"
2411    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2412    "INSERT INTO selftest(tno,op,cmd,ans)"
2413    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2414    "DROP TABLE [_shell$self];"
2415    ,0,0,&zErrMsg);
2416  if( zErrMsg ){
2417    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2418    sqlite3_free(zErrMsg);
2419  }
2420  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2421}
2422
2423
2424/*
2425** Set the destination table field of the ShellState structure to
2426** the name of the table given.  Escape any quote characters in the
2427** table name.
2428*/
2429static void set_table_name(ShellState *p, const char *zName){
2430  int i, n;
2431  char cQuote;
2432  char *z;
2433
2434  if( p->zDestTable ){
2435    free(p->zDestTable);
2436    p->zDestTable = 0;
2437  }
2438  if( zName==0 ) return;
2439  cQuote = quoteChar(zName);
2440  n = strlen30(zName);
2441  if( cQuote ) n += n+2;
2442  z = p->zDestTable = malloc( n+1 );
2443  if( z==0 ) shell_out_of_memory();
2444  n = 0;
2445  if( cQuote ) z[n++] = cQuote;
2446  for(i=0; zName[i]; i++){
2447    z[n++] = zName[i];
2448    if( zName[i]==cQuote ) z[n++] = cQuote;
2449  }
2450  if( cQuote ) z[n++] = cQuote;
2451  z[n] = 0;
2452}
2453
2454
2455/*
2456** Execute a query statement that will generate SQL output.  Print
2457** the result columns, comma-separated, on a line and then add a
2458** semicolon terminator to the end of that line.
2459**
2460** If the number of columns is 1 and that column contains text "--"
2461** then write the semicolon on a separate line.  That way, if a
2462** "--" comment occurs at the end of the statement, the comment
2463** won't consume the semicolon terminator.
2464*/
2465static int run_table_dump_query(
2466  ShellState *p,           /* Query context */
2467  const char *zSelect      /* SELECT statement to extract content */
2468){
2469  sqlite3_stmt *pSelect;
2470  int rc;
2471  int nResult;
2472  int i;
2473  const char *z;
2474  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2475  if( rc!=SQLITE_OK || !pSelect ){
2476    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2477                sqlite3_errmsg(p->db));
2478    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2479    return rc;
2480  }
2481  rc = sqlite3_step(pSelect);
2482  nResult = sqlite3_column_count(pSelect);
2483  while( rc==SQLITE_ROW ){
2484    z = (const char*)sqlite3_column_text(pSelect, 0);
2485    utf8_printf(p->out, "%s", z);
2486    for(i=1; i<nResult; i++){
2487      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2488    }
2489    if( z==0 ) z = "";
2490    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2491    if( z[0] ){
2492      raw_printf(p->out, "\n;\n");
2493    }else{
2494      raw_printf(p->out, ";\n");
2495    }
2496    rc = sqlite3_step(pSelect);
2497  }
2498  rc = sqlite3_finalize(pSelect);
2499  if( rc!=SQLITE_OK ){
2500    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2501                sqlite3_errmsg(p->db));
2502    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2503  }
2504  return rc;
2505}
2506
2507/*
2508** Allocate space and save off current error string.
2509*/
2510static char *save_err_msg(
2511  sqlite3 *db            /* Database to query */
2512){
2513  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2514  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2515  if( zErrMsg ){
2516    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2517  }
2518  return zErrMsg;
2519}
2520
2521#ifdef __linux__
2522/*
2523** Attempt to display I/O stats on Linux using /proc/PID/io
2524*/
2525static void displayLinuxIoStats(FILE *out){
2526  FILE *in;
2527  char z[200];
2528  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2529  in = fopen(z, "rb");
2530  if( in==0 ) return;
2531  while( fgets(z, sizeof(z), in)!=0 ){
2532    static const struct {
2533      const char *zPattern;
2534      const char *zDesc;
2535    } aTrans[] = {
2536      { "rchar: ",                  "Bytes received by read():" },
2537      { "wchar: ",                  "Bytes sent to write():"    },
2538      { "syscr: ",                  "Read() system calls:"      },
2539      { "syscw: ",                  "Write() system calls:"     },
2540      { "read_bytes: ",             "Bytes read from storage:"  },
2541      { "write_bytes: ",            "Bytes written to storage:" },
2542      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2543    };
2544    int i;
2545    for(i=0; i<ArraySize(aTrans); i++){
2546      int n = strlen30(aTrans[i].zPattern);
2547      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2548        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2549        break;
2550      }
2551    }
2552  }
2553  fclose(in);
2554}
2555#endif
2556
2557/*
2558** Display a single line of status using 64-bit values.
2559*/
2560static void displayStatLine(
2561  ShellState *p,            /* The shell context */
2562  char *zLabel,             /* Label for this one line */
2563  char *zFormat,            /* Format for the result */
2564  int iStatusCtrl,          /* Which status to display */
2565  int bReset                /* True to reset the stats */
2566){
2567  sqlite3_int64 iCur = -1;
2568  sqlite3_int64 iHiwtr = -1;
2569  int i, nPercent;
2570  char zLine[200];
2571  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2572  for(i=0, nPercent=0; zFormat[i]; i++){
2573    if( zFormat[i]=='%' ) nPercent++;
2574  }
2575  if( nPercent>1 ){
2576    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2577  }else{
2578    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2579  }
2580  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2581}
2582
2583/*
2584** Display memory stats.
2585*/
2586static int display_stats(
2587  sqlite3 *db,                /* Database to query */
2588  ShellState *pArg,           /* Pointer to ShellState */
2589  int bReset                  /* True to reset the stats */
2590){
2591  int iCur;
2592  int iHiwtr;
2593  FILE *out;
2594  if( pArg==0 || pArg->out==0 ) return 0;
2595  out = pArg->out;
2596
2597  if( pArg->pStmt && pArg->statsOn==2 ){
2598    int nCol, i, x;
2599    sqlite3_stmt *pStmt = pArg->pStmt;
2600    char z[100];
2601    nCol = sqlite3_column_count(pStmt);
2602    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2603    for(i=0; i<nCol; i++){
2604      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2605      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2606#ifndef SQLITE_OMIT_DECLTYPE
2607      sqlite3_snprintf(30, z+x, "declared type:");
2608      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2609#endif
2610#ifdef SQLITE_ENABLE_COLUMN_METADATA
2611      sqlite3_snprintf(30, z+x, "database name:");
2612      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2613      sqlite3_snprintf(30, z+x, "table name:");
2614      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2615      sqlite3_snprintf(30, z+x, "origin name:");
2616      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2617#endif
2618    }
2619  }
2620
2621  if( pArg->statsOn==3 ){
2622    if( pArg->pStmt ){
2623      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2624      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2625    }
2626    return 0;
2627  }
2628
2629  displayStatLine(pArg, "Memory Used:",
2630     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2631  displayStatLine(pArg, "Number of Outstanding Allocations:",
2632     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2633  if( pArg->shellFlgs & SHFLG_Pagecache ){
2634    displayStatLine(pArg, "Number of Pcache Pages Used:",
2635       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2636  }
2637  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2638     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2639  displayStatLine(pArg, "Largest Allocation:",
2640     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2641  displayStatLine(pArg, "Largest Pcache Allocation:",
2642     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2643#ifdef YYTRACKMAXSTACKDEPTH
2644  displayStatLine(pArg, "Deepest Parser Stack:",
2645     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2646#endif
2647
2648  if( db ){
2649    if( pArg->shellFlgs & SHFLG_Lookaside ){
2650      iHiwtr = iCur = -1;
2651      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2652                        &iCur, &iHiwtr, bReset);
2653      raw_printf(pArg->out,
2654              "Lookaside Slots Used:                %d (max %d)\n",
2655              iCur, iHiwtr);
2656      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2657                        &iCur, &iHiwtr, bReset);
2658      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2659              iHiwtr);
2660      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2661                        &iCur, &iHiwtr, bReset);
2662      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2663              iHiwtr);
2664      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2665                        &iCur, &iHiwtr, bReset);
2666      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2667              iHiwtr);
2668    }
2669    iHiwtr = iCur = -1;
2670    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2671    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2672            iCur);
2673    iHiwtr = iCur = -1;
2674    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2675    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2676    iHiwtr = iCur = -1;
2677    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2678    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2679    iHiwtr = iCur = -1;
2680    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2681    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2682    iHiwtr = iCur = -1;
2683    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2684    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2685    iHiwtr = iCur = -1;
2686    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2687    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2688            iCur);
2689    iHiwtr = iCur = -1;
2690    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2691    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2692            iCur);
2693  }
2694
2695  if( pArg->pStmt ){
2696    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2697                               bReset);
2698    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2699    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2700    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2701    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2702    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2703    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2704    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2705    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2706    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2707    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2708    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2709    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2710    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2711  }
2712
2713#ifdef __linux__
2714  displayLinuxIoStats(pArg->out);
2715#endif
2716
2717  /* Do not remove this machine readable comment: extra-stats-output-here */
2718
2719  return 0;
2720}
2721
2722/*
2723** Display scan stats.
2724*/
2725static void display_scanstats(
2726  sqlite3 *db,                    /* Database to query */
2727  ShellState *pArg                /* Pointer to ShellState */
2728){
2729#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2730  UNUSED_PARAMETER(db);
2731  UNUSED_PARAMETER(pArg);
2732#else
2733  int i, k, n, mx;
2734  raw_printf(pArg->out, "-------- scanstats --------\n");
2735  mx = 0;
2736  for(k=0; k<=mx; k++){
2737    double rEstLoop = 1.0;
2738    for(i=n=0; 1; i++){
2739      sqlite3_stmt *p = pArg->pStmt;
2740      sqlite3_int64 nLoop, nVisit;
2741      double rEst;
2742      int iSid;
2743      const char *zExplain;
2744      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2745        break;
2746      }
2747      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2748      if( iSid>mx ) mx = iSid;
2749      if( iSid!=k ) continue;
2750      if( n==0 ){
2751        rEstLoop = (double)nLoop;
2752        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2753      }
2754      n++;
2755      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2756      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2757      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2758      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2759      rEstLoop *= rEst;
2760      raw_printf(pArg->out,
2761          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2762          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2763      );
2764    }
2765  }
2766  raw_printf(pArg->out, "---------------------------\n");
2767#endif
2768}
2769
2770/*
2771** Parameter azArray points to a zero-terminated array of strings. zStr
2772** points to a single nul-terminated string. Return non-zero if zStr
2773** is equal, according to strcmp(), to any of the strings in the array.
2774** Otherwise, return zero.
2775*/
2776static int str_in_array(const char *zStr, const char **azArray){
2777  int i;
2778  for(i=0; azArray[i]; i++){
2779    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2780  }
2781  return 0;
2782}
2783
2784/*
2785** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2786** and populate the ShellState.aiIndent[] array with the number of
2787** spaces each opcode should be indented before it is output.
2788**
2789** The indenting rules are:
2790**
2791**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2792**       all opcodes that occur between the p2 jump destination and the opcode
2793**       itself by 2 spaces.
2794**
2795**     * For each "Goto", if the jump destination is earlier in the program
2796**       and ends on one of:
2797**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2798**       or if the P1 parameter is one instead of zero,
2799**       then indent all opcodes between the earlier instruction
2800**       and "Goto" by 2 spaces.
2801*/
2802static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2803  const char *zSql;               /* The text of the SQL statement */
2804  const char *z;                  /* Used to check if this is an EXPLAIN */
2805  int *abYield = 0;               /* True if op is an OP_Yield */
2806  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2807  int iOp;                        /* Index of operation in p->aiIndent[] */
2808
2809  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2810  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2811                            "Rewind", 0 };
2812  const char *azGoto[] = { "Goto", 0 };
2813
2814  /* Try to figure out if this is really an EXPLAIN statement. If this
2815  ** cannot be verified, return early.  */
2816  if( sqlite3_column_count(pSql)!=8 ){
2817    p->cMode = p->mode;
2818    return;
2819  }
2820  zSql = sqlite3_sql(pSql);
2821  if( zSql==0 ) return;
2822  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2823  if( sqlite3_strnicmp(z, "explain", 7) ){
2824    p->cMode = p->mode;
2825    return;
2826  }
2827
2828  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2829    int i;
2830    int iAddr = sqlite3_column_int(pSql, 0);
2831    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2832
2833    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2834    ** p2 is an instruction address, set variable p2op to the index of that
2835    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2836    ** the current instruction is part of a sub-program generated by an
2837    ** SQL trigger or foreign key.  */
2838    int p2 = sqlite3_column_int(pSql, 3);
2839    int p2op = (p2 + (iOp-iAddr));
2840
2841    /* Grow the p->aiIndent array as required */
2842    if( iOp>=nAlloc ){
2843      if( iOp==0 ){
2844        /* Do further verfication that this is explain output.  Abort if
2845        ** it is not */
2846        static const char *explainCols[] = {
2847           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2848        int jj;
2849        for(jj=0; jj<ArraySize(explainCols); jj++){
2850          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2851            p->cMode = p->mode;
2852            sqlite3_reset(pSql);
2853            return;
2854          }
2855        }
2856      }
2857      nAlloc += 100;
2858      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2859      if( p->aiIndent==0 ) shell_out_of_memory();
2860      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2861      if( abYield==0 ) shell_out_of_memory();
2862    }
2863    abYield[iOp] = str_in_array(zOp, azYield);
2864    p->aiIndent[iOp] = 0;
2865    p->nIndent = iOp+1;
2866
2867    if( str_in_array(zOp, azNext) ){
2868      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2869    }
2870    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2871     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2872    ){
2873      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2874    }
2875  }
2876
2877  p->iIndent = 0;
2878  sqlite3_free(abYield);
2879  sqlite3_reset(pSql);
2880}
2881
2882/*
2883** Free the array allocated by explain_data_prepare().
2884*/
2885static void explain_data_delete(ShellState *p){
2886  sqlite3_free(p->aiIndent);
2887  p->aiIndent = 0;
2888  p->nIndent = 0;
2889  p->iIndent = 0;
2890}
2891
2892/*
2893** Disable and restore .wheretrace and .selecttrace settings.
2894*/
2895static unsigned int savedSelectTrace;
2896static unsigned int savedWhereTrace;
2897static void disable_debug_trace_modes(void){
2898  unsigned int zero = 0;
2899  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
2900  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
2901  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
2902  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
2903}
2904static void restore_debug_trace_modes(void){
2905  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
2906  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
2907}
2908
2909/* Create the TEMP table used to store parameter bindings */
2910static void bind_table_init(ShellState *p){
2911  int wrSchema = 0;
2912  int defensiveMode = 0;
2913  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2914  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2915  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2916  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2917  sqlite3_exec(p->db,
2918    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2919    "  key TEXT PRIMARY KEY,\n"
2920    "  value\n"
2921    ") WITHOUT ROWID;",
2922    0, 0, 0);
2923  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2924  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2925}
2926
2927/*
2928** Bind parameters on a prepared statement.
2929**
2930** Parameter bindings are taken from a TEMP table of the form:
2931**
2932**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2933**    WITHOUT ROWID;
2934**
2935** No bindings occur if this table does not exist.  The name of the table
2936** begins with "sqlite_" so that it will not collide with ordinary application
2937** tables.  The table must be in the TEMP schema.
2938*/
2939static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2940  int nVar;
2941  int i;
2942  int rc;
2943  sqlite3_stmt *pQ = 0;
2944
2945  nVar = sqlite3_bind_parameter_count(pStmt);
2946  if( nVar==0 ) return;  /* Nothing to do */
2947  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2948                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2949    return; /* Parameter table does not exist */
2950  }
2951  rc = sqlite3_prepare_v2(pArg->db,
2952          "SELECT value FROM temp.sqlite_parameters"
2953          " WHERE key=?1", -1, &pQ, 0);
2954  if( rc || pQ==0 ) return;
2955  for(i=1; i<=nVar; i++){
2956    char zNum[30];
2957    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2958    if( zVar==0 ){
2959      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2960      zVar = zNum;
2961    }
2962    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2963    if( sqlite3_step(pQ)==SQLITE_ROW ){
2964      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2965    }else{
2966      sqlite3_bind_null(pStmt, i);
2967    }
2968    sqlite3_reset(pQ);
2969  }
2970  sqlite3_finalize(pQ);
2971}
2972
2973/*
2974** UTF8 box-drawing characters.  Imagine box lines like this:
2975**
2976**           1
2977**           |
2978**       4 --+-- 2
2979**           |
2980**           3
2981**
2982** Each box characters has between 2 and 4 of the lines leading from
2983** the center.  The characters are here identified by the numbers of
2984** their corresponding lines.
2985*/
2986#define BOX_24   "\342\224\200"  /* U+2500 --- */
2987#define BOX_13   "\342\224\202"  /* U+2502  |  */
2988#define BOX_23   "\342\224\214"  /* U+250c  ,- */
2989#define BOX_34   "\342\224\220"  /* U+2510 -,  */
2990#define BOX_12   "\342\224\224"  /* U+2514  '- */
2991#define BOX_14   "\342\224\230"  /* U+2518 -'  */
2992#define BOX_123  "\342\224\234"  /* U+251c  |- */
2993#define BOX_134  "\342\224\244"  /* U+2524 -|  */
2994#define BOX_234  "\342\224\254"  /* U+252c -,- */
2995#define BOX_124  "\342\224\264"  /* U+2534 -'- */
2996#define BOX_1234 "\342\224\274"  /* U+253c -|- */
2997
2998/* Draw horizontal line N characters long using unicode box
2999** characters
3000*/
3001static void print_box_line(FILE *out, int N){
3002  const char zDash[] =
3003      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3004      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3005  const int nDash = sizeof(zDash) - 1;
3006  N *= 3;
3007  while( N>nDash ){
3008    utf8_printf(out, zDash);
3009    N -= nDash;
3010  }
3011  utf8_printf(out, "%.*s", N, zDash);
3012}
3013
3014/*
3015** Draw a horizontal separator for a MODE_Box table.
3016*/
3017static void print_box_row_separator(
3018  ShellState *p,
3019  int nArg,
3020  const char *zSep1,
3021  const char *zSep2,
3022  const char *zSep3
3023){
3024  int i;
3025  if( nArg>0 ){
3026    utf8_printf(p->out, "%s", zSep1);
3027    print_box_line(p->out, p->actualWidth[0]+2);
3028    for(i=1; i<nArg; i++){
3029      utf8_printf(p->out, "%s", zSep2);
3030      print_box_line(p->out, p->actualWidth[i]+2);
3031    }
3032    utf8_printf(p->out, "%s", zSep3);
3033  }
3034  fputs("\n", p->out);
3035}
3036
3037
3038
3039/*
3040** Run a prepared statement and output the result in one of the
3041** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3042** or MODE_Box.
3043**
3044** This is different from ordinary exec_prepared_stmt() in that
3045** it has to run the entire query and gather the results into memory
3046** first, in order to determine column widths, before providing
3047** any output.
3048*/
3049static void exec_prepared_stmt_columnar(
3050  ShellState *p,                        /* Pointer to ShellState */
3051  sqlite3_stmt *pStmt                   /* Statment to run */
3052){
3053  sqlite3_int64 nRow = 0;
3054  int nColumn = 0;
3055  char **azData = 0;
3056  sqlite3_int64 nAlloc = 0;
3057  const char *z;
3058  int rc;
3059  sqlite3_int64 i, nData;
3060  int j, nTotal, w, n;
3061  const char *colSep = 0;
3062  const char *rowSep = 0;
3063
3064  rc = sqlite3_step(pStmt);
3065  if( rc!=SQLITE_ROW ) return;
3066  nColumn = sqlite3_column_count(pStmt);
3067  nAlloc = nColumn*4;
3068  if( nAlloc<=0 ) nAlloc = 1;
3069  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3070  if( azData==0 ) shell_out_of_memory();
3071  for(i=0; i<nColumn; i++){
3072    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3073  }
3074  do{
3075    if( (nRow+2)*nColumn >= nAlloc ){
3076      nAlloc *= 2;
3077      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3078      if( azData==0 ) shell_out_of_memory();
3079    }
3080    nRow++;
3081    for(i=0; i<nColumn; i++){
3082      z = (const char*)sqlite3_column_text(pStmt,i);
3083      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3084    }
3085  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
3086  if( nColumn>p->nWidth ){
3087    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3088    if( p->colWidth==0 ) shell_out_of_memory();
3089    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3090    p->nWidth = nColumn;
3091    p->actualWidth = &p->colWidth[nColumn];
3092  }
3093  memset(p->actualWidth, 0, nColumn*sizeof(int));
3094  for(i=0; i<nColumn; i++){
3095    w = p->colWidth[i];
3096    if( w<0 ) w = -w;
3097    p->actualWidth[i] = w;
3098  }
3099  nTotal = nColumn*(nRow+1);
3100  for(i=0; i<nTotal; i++){
3101    z = azData[i];
3102    if( z==0 ) z = p->nullValue;
3103    n = strlenChar(z);
3104    j = i%nColumn;
3105    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3106  }
3107  if( seenInterrupt ) goto columnar_end;
3108  if( nColumn==0 ) goto columnar_end;
3109  switch( p->cMode ){
3110    case MODE_Column: {
3111      colSep = "  ";
3112      rowSep = "\n";
3113      if( p->showHeader ){
3114        for(i=0; i<nColumn; i++){
3115          w = p->actualWidth[i];
3116          if( p->colWidth[i]<0 ) w = -w;
3117          utf8_width_print(p->out, w, azData[i]);
3118          fputs(i==nColumn-1?"\n":"  ", p->out);
3119        }
3120        for(i=0; i<nColumn; i++){
3121          print_dashes(p->out, p->actualWidth[i]);
3122          fputs(i==nColumn-1?"\n":"  ", p->out);
3123        }
3124      }
3125      break;
3126    }
3127    case MODE_Table: {
3128      colSep = " | ";
3129      rowSep = " |\n";
3130      print_row_separator(p, nColumn, "+");
3131      fputs("| ", p->out);
3132      for(i=0; i<nColumn; i++){
3133        w = p->actualWidth[i];
3134        n = strlenChar(azData[i]);
3135        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3136        fputs(i==nColumn-1?" |\n":" | ", p->out);
3137      }
3138      print_row_separator(p, nColumn, "+");
3139      break;
3140    }
3141    case MODE_Markdown: {
3142      colSep = " | ";
3143      rowSep = " |\n";
3144      fputs("| ", p->out);
3145      for(i=0; i<nColumn; i++){
3146        w = p->actualWidth[i];
3147        n = strlenChar(azData[i]);
3148        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3149        fputs(i==nColumn-1?" |\n":" | ", p->out);
3150      }
3151      print_row_separator(p, nColumn, "|");
3152      break;
3153    }
3154    case MODE_Box: {
3155      colSep = " " BOX_13 " ";
3156      rowSep = " " BOX_13 "\n";
3157      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3158      utf8_printf(p->out, BOX_13 " ");
3159      for(i=0; i<nColumn; i++){
3160        w = p->actualWidth[i];
3161        n = strlenChar(azData[i]);
3162        utf8_printf(p->out, "%*s%s%*s%s",
3163            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3164            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3165      }
3166      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3167      break;
3168    }
3169  }
3170  for(i=nColumn, j=0; i<nTotal; i++, j++){
3171    if( j==0 && p->cMode!=MODE_Column ){
3172      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3173    }
3174    z = azData[i];
3175    if( z==0 ) z = p->nullValue;
3176    w = p->actualWidth[j];
3177    if( p->colWidth[j]<0 ) w = -w;
3178    utf8_width_print(p->out, w, z);
3179    if( j==nColumn-1 ){
3180      utf8_printf(p->out, "%s", rowSep);
3181      j = -1;
3182      if( seenInterrupt ) goto columnar_end;
3183    }else{
3184      utf8_printf(p->out, "%s", colSep);
3185    }
3186  }
3187  if( p->cMode==MODE_Table ){
3188    print_row_separator(p, nColumn, "+");
3189  }else if( p->cMode==MODE_Box ){
3190    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3191  }
3192columnar_end:
3193  if( seenInterrupt ){
3194    utf8_printf(p->out, "Interrupt\n");
3195  }
3196  nData = (nRow+1)*nColumn;
3197  for(i=0; i<nData; i++) free(azData[i]);
3198  sqlite3_free(azData);
3199}
3200
3201/*
3202** Run a prepared statement
3203*/
3204static void exec_prepared_stmt(
3205  ShellState *pArg,                                /* Pointer to ShellState */
3206  sqlite3_stmt *pStmt                              /* Statment to run */
3207){
3208  int rc;
3209
3210  if( pArg->cMode==MODE_Column
3211   || pArg->cMode==MODE_Table
3212   || pArg->cMode==MODE_Box
3213   || pArg->cMode==MODE_Markdown
3214  ){
3215    exec_prepared_stmt_columnar(pArg, pStmt);
3216    return;
3217  }
3218
3219  /* perform the first step.  this will tell us if we
3220  ** have a result set or not and how wide it is.
3221  */
3222  rc = sqlite3_step(pStmt);
3223  /* if we have a result set... */
3224  if( SQLITE_ROW == rc ){
3225    /* allocate space for col name ptr, value ptr, and type */
3226    int nCol = sqlite3_column_count(pStmt);
3227    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3228    if( !pData ){
3229      rc = SQLITE_NOMEM;
3230    }else{
3231      char **azCols = (char **)pData;      /* Names of result columns */
3232      char **azVals = &azCols[nCol];       /* Results */
3233      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3234      int i, x;
3235      assert(sizeof(int) <= sizeof(char *));
3236      /* save off ptrs to column names */
3237      for(i=0; i<nCol; i++){
3238        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3239      }
3240      do{
3241        /* extract the data and data types */
3242        for(i=0; i<nCol; i++){
3243          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3244          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3245            azVals[i] = "";
3246          }else{
3247            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3248          }
3249          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3250            rc = SQLITE_NOMEM;
3251            break; /* from for */
3252          }
3253        } /* end for */
3254
3255        /* if data and types extracted successfully... */
3256        if( SQLITE_ROW == rc ){
3257          /* call the supplied callback with the result row data */
3258          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3259            rc = SQLITE_ABORT;
3260          }else{
3261            rc = sqlite3_step(pStmt);
3262          }
3263        }
3264      } while( SQLITE_ROW == rc );
3265      sqlite3_free(pData);
3266      if( pArg->cMode==MODE_Json ){
3267        fputs("]\n", pArg->out);
3268      }
3269    }
3270  }
3271}
3272
3273#ifndef SQLITE_OMIT_VIRTUALTABLE
3274/*
3275** This function is called to process SQL if the previous shell command
3276** was ".expert". It passes the SQL in the second argument directly to
3277** the sqlite3expert object.
3278**
3279** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3280** code. In this case, (*pzErr) may be set to point to a buffer containing
3281** an English language error message. It is the responsibility of the
3282** caller to eventually free this buffer using sqlite3_free().
3283*/
3284static int expertHandleSQL(
3285  ShellState *pState,
3286  const char *zSql,
3287  char **pzErr
3288){
3289  assert( pState->expert.pExpert );
3290  assert( pzErr==0 || *pzErr==0 );
3291  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3292}
3293
3294/*
3295** This function is called either to silently clean up the object
3296** created by the ".expert" command (if bCancel==1), or to generate a
3297** report from it and then clean it up (if bCancel==0).
3298**
3299** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3300** code. In this case, (*pzErr) may be set to point to a buffer containing
3301** an English language error message. It is the responsibility of the
3302** caller to eventually free this buffer using sqlite3_free().
3303*/
3304static int expertFinish(
3305  ShellState *pState,
3306  int bCancel,
3307  char **pzErr
3308){
3309  int rc = SQLITE_OK;
3310  sqlite3expert *p = pState->expert.pExpert;
3311  assert( p );
3312  assert( bCancel || pzErr==0 || *pzErr==0 );
3313  if( bCancel==0 ){
3314    FILE *out = pState->out;
3315    int bVerbose = pState->expert.bVerbose;
3316
3317    rc = sqlite3_expert_analyze(p, pzErr);
3318    if( rc==SQLITE_OK ){
3319      int nQuery = sqlite3_expert_count(p);
3320      int i;
3321
3322      if( bVerbose ){
3323        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3324        raw_printf(out, "-- Candidates -----------------------------\n");
3325        raw_printf(out, "%s\n", zCand);
3326      }
3327      for(i=0; i<nQuery; i++){
3328        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3329        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3330        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3331        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3332        if( bVerbose ){
3333          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3334          raw_printf(out, "%s\n\n", zSql);
3335        }
3336        raw_printf(out, "%s\n", zIdx);
3337        raw_printf(out, "%s\n", zEQP);
3338      }
3339    }
3340  }
3341  sqlite3_expert_destroy(p);
3342  pState->expert.pExpert = 0;
3343  return rc;
3344}
3345
3346/*
3347** Implementation of ".expert" dot command.
3348*/
3349static int expertDotCommand(
3350  ShellState *pState,             /* Current shell tool state */
3351  char **azArg,                   /* Array of arguments passed to dot command */
3352  int nArg                        /* Number of entries in azArg[] */
3353){
3354  int rc = SQLITE_OK;
3355  char *zErr = 0;
3356  int i;
3357  int iSample = 0;
3358
3359  assert( pState->expert.pExpert==0 );
3360  memset(&pState->expert, 0, sizeof(ExpertInfo));
3361
3362  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3363    char *z = azArg[i];
3364    int n;
3365    if( z[0]=='-' && z[1]=='-' ) z++;
3366    n = strlen30(z);
3367    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3368      pState->expert.bVerbose = 1;
3369    }
3370    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3371      if( i==(nArg-1) ){
3372        raw_printf(stderr, "option requires an argument: %s\n", z);
3373        rc = SQLITE_ERROR;
3374      }else{
3375        iSample = (int)integerValue(azArg[++i]);
3376        if( iSample<0 || iSample>100 ){
3377          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3378          rc = SQLITE_ERROR;
3379        }
3380      }
3381    }
3382    else{
3383      raw_printf(stderr, "unknown option: %s\n", z);
3384      rc = SQLITE_ERROR;
3385    }
3386  }
3387
3388  if( rc==SQLITE_OK ){
3389    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3390    if( pState->expert.pExpert==0 ){
3391      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3392      rc = SQLITE_ERROR;
3393    }else{
3394      sqlite3_expert_config(
3395          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3396      );
3397    }
3398  }
3399
3400  return rc;
3401}
3402#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3403
3404/*
3405** Execute a statement or set of statements.  Print
3406** any result rows/columns depending on the current mode
3407** set via the supplied callback.
3408**
3409** This is very similar to SQLite's built-in sqlite3_exec()
3410** function except it takes a slightly different callback
3411** and callback data argument.
3412*/
3413static int shell_exec(
3414  ShellState *pArg,                         /* Pointer to ShellState */
3415  const char *zSql,                         /* SQL to be evaluated */
3416  char **pzErrMsg                           /* Error msg written here */
3417){
3418  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3419  int rc = SQLITE_OK;             /* Return Code */
3420  int rc2;
3421  const char *zLeftover;          /* Tail of unprocessed SQL */
3422  sqlite3 *db = pArg->db;
3423
3424  if( pzErrMsg ){
3425    *pzErrMsg = NULL;
3426  }
3427
3428#ifndef SQLITE_OMIT_VIRTUALTABLE
3429  if( pArg->expert.pExpert ){
3430    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3431    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3432  }
3433#endif
3434
3435  while( zSql[0] && (SQLITE_OK == rc) ){
3436    static const char *zStmtSql;
3437    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3438    if( SQLITE_OK != rc ){
3439      if( pzErrMsg ){
3440        *pzErrMsg = save_err_msg(db);
3441      }
3442    }else{
3443      if( !pStmt ){
3444        /* this happens for a comment or white-space */
3445        zSql = zLeftover;
3446        while( IsSpace(zSql[0]) ) zSql++;
3447        continue;
3448      }
3449      zStmtSql = sqlite3_sql(pStmt);
3450      if( zStmtSql==0 ) zStmtSql = "";
3451      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3452
3453      /* save off the prepared statment handle and reset row count */
3454      if( pArg ){
3455        pArg->pStmt = pStmt;
3456        pArg->cnt = 0;
3457      }
3458
3459      /* echo the sql statement if echo on */
3460      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3461        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3462      }
3463
3464      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3465      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3466        sqlite3_stmt *pExplain;
3467        char *zEQP;
3468        int triggerEQP = 0;
3469        disable_debug_trace_modes();
3470        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3471        if( pArg->autoEQP>=AUTOEQP_trigger ){
3472          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3473        }
3474        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3475        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3476        if( rc==SQLITE_OK ){
3477          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3478            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3479            int iEqpId = sqlite3_column_int(pExplain, 0);
3480            int iParentId = sqlite3_column_int(pExplain, 1);
3481            if( zEQPLine==0 ) zEQPLine = "";
3482            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3483            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3484          }
3485          eqp_render(pArg);
3486        }
3487        sqlite3_finalize(pExplain);
3488        sqlite3_free(zEQP);
3489        if( pArg->autoEQP>=AUTOEQP_full ){
3490          /* Also do an EXPLAIN for ".eqp full" mode */
3491          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3492          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3493          if( rc==SQLITE_OK ){
3494            pArg->cMode = MODE_Explain;
3495            explain_data_prepare(pArg, pExplain);
3496            exec_prepared_stmt(pArg, pExplain);
3497            explain_data_delete(pArg);
3498          }
3499          sqlite3_finalize(pExplain);
3500          sqlite3_free(zEQP);
3501        }
3502        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3503          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3504          /* Reprepare pStmt before reactiving trace modes */
3505          sqlite3_finalize(pStmt);
3506          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3507          if( pArg ) pArg->pStmt = pStmt;
3508        }
3509        restore_debug_trace_modes();
3510      }
3511
3512      if( pArg ){
3513        pArg->cMode = pArg->mode;
3514        if( pArg->autoExplain ){
3515          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3516            pArg->cMode = MODE_Explain;
3517          }
3518          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3519            pArg->cMode = MODE_EQP;
3520          }
3521        }
3522
3523        /* If the shell is currently in ".explain" mode, gather the extra
3524        ** data required to add indents to the output.*/
3525        if( pArg->cMode==MODE_Explain ){
3526          explain_data_prepare(pArg, pStmt);
3527        }
3528      }
3529
3530      bind_prepared_stmt(pArg, pStmt);
3531      exec_prepared_stmt(pArg, pStmt);
3532      explain_data_delete(pArg);
3533      eqp_render(pArg);
3534
3535      /* print usage stats if stats on */
3536      if( pArg && pArg->statsOn ){
3537        display_stats(db, pArg, 0);
3538      }
3539
3540      /* print loop-counters if required */
3541      if( pArg && pArg->scanstatsOn ){
3542        display_scanstats(db, pArg);
3543      }
3544
3545      /* Finalize the statement just executed. If this fails, save a
3546      ** copy of the error message. Otherwise, set zSql to point to the
3547      ** next statement to execute. */
3548      rc2 = sqlite3_finalize(pStmt);
3549      if( rc!=SQLITE_NOMEM ) rc = rc2;
3550      if( rc==SQLITE_OK ){
3551        zSql = zLeftover;
3552        while( IsSpace(zSql[0]) ) zSql++;
3553      }else if( pzErrMsg ){
3554        *pzErrMsg = save_err_msg(db);
3555      }
3556
3557      /* clear saved stmt handle */
3558      if( pArg ){
3559        pArg->pStmt = NULL;
3560      }
3561    }
3562  } /* end while */
3563
3564  return rc;
3565}
3566
3567/*
3568** Release memory previously allocated by tableColumnList().
3569*/
3570static void freeColumnList(char **azCol){
3571  int i;
3572  for(i=1; azCol[i]; i++){
3573    sqlite3_free(azCol[i]);
3574  }
3575  /* azCol[0] is a static string */
3576  sqlite3_free(azCol);
3577}
3578
3579/*
3580** Return a list of pointers to strings which are the names of all
3581** columns in table zTab.   The memory to hold the names is dynamically
3582** allocated and must be released by the caller using a subsequent call
3583** to freeColumnList().
3584**
3585** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3586** value that needs to be preserved, then azCol[0] is filled in with the
3587** name of the rowid column.
3588**
3589** The first regular column in the table is azCol[1].  The list is terminated
3590** by an entry with azCol[i]==0.
3591*/
3592static char **tableColumnList(ShellState *p, const char *zTab){
3593  char **azCol = 0;
3594  sqlite3_stmt *pStmt;
3595  char *zSql;
3596  int nCol = 0;
3597  int nAlloc = 0;
3598  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3599  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3600  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3601  int rc;
3602
3603  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3604  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3605  sqlite3_free(zSql);
3606  if( rc ) return 0;
3607  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3608    if( nCol>=nAlloc-2 ){
3609      nAlloc = nAlloc*2 + nCol + 10;
3610      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3611      if( azCol==0 ) shell_out_of_memory();
3612    }
3613    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3614    if( sqlite3_column_int(pStmt, 5) ){
3615      nPK++;
3616      if( nPK==1
3617       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3618                          "INTEGER")==0
3619      ){
3620        isIPK = 1;
3621      }else{
3622        isIPK = 0;
3623      }
3624    }
3625  }
3626  sqlite3_finalize(pStmt);
3627  if( azCol==0 ) return 0;
3628  azCol[0] = 0;
3629  azCol[nCol+1] = 0;
3630
3631  /* The decision of whether or not a rowid really needs to be preserved
3632  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3633  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3634  ** rowids on tables where the rowid is inaccessible because there are other
3635  ** columns in the table named "rowid", "_rowid_", and "oid".
3636  */
3637  if( preserveRowid && isIPK ){
3638    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3639    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3640    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3641    ** ROWID aliases.  To distinguish these cases, check to see if
3642    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3643    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3644    */
3645    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3646                           " WHERE origin='pk'", zTab);
3647    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3648    sqlite3_free(zSql);
3649    if( rc ){
3650      freeColumnList(azCol);
3651      return 0;
3652    }
3653    rc = sqlite3_step(pStmt);
3654    sqlite3_finalize(pStmt);
3655    preserveRowid = rc==SQLITE_ROW;
3656  }
3657  if( preserveRowid ){
3658    /* Only preserve the rowid if we can find a name to use for the
3659    ** rowid */
3660    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3661    int i, j;
3662    for(j=0; j<3; j++){
3663      for(i=1; i<=nCol; i++){
3664        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3665      }
3666      if( i>nCol ){
3667        /* At this point, we know that azRowid[j] is not the name of any
3668        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3669        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3670        ** tables will fail this last check */
3671        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3672        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3673        break;
3674      }
3675    }
3676  }
3677  return azCol;
3678}
3679
3680/*
3681** Toggle the reverse_unordered_selects setting.
3682*/
3683static void toggleSelectOrder(sqlite3 *db){
3684  sqlite3_stmt *pStmt = 0;
3685  int iSetting = 0;
3686  char zStmt[100];
3687  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3688  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3689    iSetting = sqlite3_column_int(pStmt, 0);
3690  }
3691  sqlite3_finalize(pStmt);
3692  sqlite3_snprintf(sizeof(zStmt), zStmt,
3693       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3694  sqlite3_exec(db, zStmt, 0, 0, 0);
3695}
3696
3697/*
3698** This is a different callback routine used for dumping the database.
3699** Each row received by this callback consists of a table name,
3700** the table type ("index" or "table") and SQL to create the table.
3701** This routine should print text sufficient to recreate the table.
3702*/
3703static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3704  int rc;
3705  const char *zTable;
3706  const char *zType;
3707  const char *zSql;
3708  ShellState *p = (ShellState *)pArg;
3709  int dataOnly;
3710  int noSys;
3711
3712  UNUSED_PARAMETER(azNotUsed);
3713  if( nArg!=3 || azArg==0 ) return 0;
3714  zTable = azArg[0];
3715  zType = azArg[1];
3716  zSql = azArg[2];
3717  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
3718  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
3719
3720  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
3721    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3722  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
3723    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3724  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3725    return 0;
3726  }else if( dataOnly ){
3727    /* no-op */
3728  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3729    char *zIns;
3730    if( !p->writableSchema ){
3731      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3732      p->writableSchema = 1;
3733    }
3734    zIns = sqlite3_mprintf(
3735       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3736       "VALUES('table','%q','%q',0,'%q');",
3737       zTable, zTable, zSql);
3738    utf8_printf(p->out, "%s\n", zIns);
3739    sqlite3_free(zIns);
3740    return 0;
3741  }else{
3742    printSchemaLine(p->out, zSql, ";\n");
3743  }
3744
3745  if( strcmp(zType, "table")==0 ){
3746    ShellText sSelect;
3747    ShellText sTable;
3748    char **azCol;
3749    int i;
3750    char *savedDestTable;
3751    int savedMode;
3752
3753    azCol = tableColumnList(p, zTable);
3754    if( azCol==0 ){
3755      p->nErr++;
3756      return 0;
3757    }
3758
3759    /* Always quote the table name, even if it appears to be pure ascii,
3760    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3761    initText(&sTable);
3762    appendText(&sTable, zTable, quoteChar(zTable));
3763    /* If preserving the rowid, add a column list after the table name.
3764    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3765    ** instead of the usual "INSERT INTO tab VALUES(...)".
3766    */
3767    if( azCol[0] ){
3768      appendText(&sTable, "(", 0);
3769      appendText(&sTable, azCol[0], 0);
3770      for(i=1; azCol[i]; i++){
3771        appendText(&sTable, ",", 0);
3772        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3773      }
3774      appendText(&sTable, ")", 0);
3775    }
3776
3777    /* Build an appropriate SELECT statement */
3778    initText(&sSelect);
3779    appendText(&sSelect, "SELECT ", 0);
3780    if( azCol[0] ){
3781      appendText(&sSelect, azCol[0], 0);
3782      appendText(&sSelect, ",", 0);
3783    }
3784    for(i=1; azCol[i]; i++){
3785      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3786      if( azCol[i+1] ){
3787        appendText(&sSelect, ",", 0);
3788      }
3789    }
3790    freeColumnList(azCol);
3791    appendText(&sSelect, " FROM ", 0);
3792    appendText(&sSelect, zTable, quoteChar(zTable));
3793
3794    savedDestTable = p->zDestTable;
3795    savedMode = p->mode;
3796    p->zDestTable = sTable.z;
3797    p->mode = p->cMode = MODE_Insert;
3798    rc = shell_exec(p, sSelect.z, 0);
3799    if( (rc&0xff)==SQLITE_CORRUPT ){
3800      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3801      toggleSelectOrder(p->db);
3802      shell_exec(p, sSelect.z, 0);
3803      toggleSelectOrder(p->db);
3804    }
3805    p->zDestTable = savedDestTable;
3806    p->mode = savedMode;
3807    freeText(&sTable);
3808    freeText(&sSelect);
3809    if( rc ) p->nErr++;
3810  }
3811  return 0;
3812}
3813
3814/*
3815** Run zQuery.  Use dump_callback() as the callback routine so that
3816** the contents of the query are output as SQL statements.
3817**
3818** If we get a SQLITE_CORRUPT error, rerun the query after appending
3819** "ORDER BY rowid DESC" to the end.
3820*/
3821static int run_schema_dump_query(
3822  ShellState *p,
3823  const char *zQuery
3824){
3825  int rc;
3826  char *zErr = 0;
3827  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3828  if( rc==SQLITE_CORRUPT ){
3829    char *zQ2;
3830    int len = strlen30(zQuery);
3831    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3832    if( zErr ){
3833      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3834      sqlite3_free(zErr);
3835      zErr = 0;
3836    }
3837    zQ2 = malloc( len+100 );
3838    if( zQ2==0 ) return rc;
3839    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3840    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3841    if( rc ){
3842      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3843    }else{
3844      rc = SQLITE_CORRUPT;
3845    }
3846    sqlite3_free(zErr);
3847    free(zQ2);
3848  }
3849  return rc;
3850}
3851
3852/*
3853** Text of help messages.
3854**
3855** The help text for each individual command begins with a line that starts
3856** with ".".  Subsequent lines are supplimental information.
3857**
3858** There must be two or more spaces between the end of the command and the
3859** start of the description of what that command does.
3860*/
3861static const char *(azHelp[]) = {
3862#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3863  ".archive ...             Manage SQL archives",
3864  "   Each command must have exactly one of the following options:",
3865  "     -c, --create               Create a new archive",
3866  "     -u, --update               Add or update files with changed mtime",
3867  "     -i, --insert               Like -u but always add even if unchanged",
3868  "     -t, --list                 List contents of archive",
3869  "     -x, --extract              Extract files from archive",
3870  "   Optional arguments:",
3871  "     -v, --verbose              Print each filename as it is processed",
3872  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3873  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3874  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3875  "     -n, --dryrun               Show the SQL that would have occurred",
3876  "   Examples:",
3877  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3878  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3879  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3880  "   See also:",
3881  "      http://sqlite.org/cli.html#sqlite_archive_support",
3882#endif
3883#ifndef SQLITE_OMIT_AUTHORIZATION
3884  ".auth ON|OFF             Show authorizer callbacks",
3885#endif
3886  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3887  "       --append            Use the appendvfs",
3888  "       --async             Write to FILE without journal and fsync()",
3889  ".bail on|off             Stop after hitting an error.  Default OFF",
3890  ".binary on|off           Turn binary output on or off.  Default OFF",
3891  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3892  ".changes on|off          Show number of rows changed by SQL",
3893  ".check GLOB              Fail if output since .testcase does not match",
3894  ".clone NEWDB             Clone data into NEWDB from the existing database",
3895  ".databases               List names and files of attached databases",
3896  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3897  ".dbinfo ?DB?             Show status information about the database",
3898  ".dump ?OBJECTS?          Render database content as SQL",
3899  "   Options:",
3900  "     --data-only            Output only INSERT statements",
3901  "     --newlines             Allow unescaped newline characters in output",
3902  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
3903  "     --preserve-rowids      Include ROWID values in the output",
3904  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
3905  "   Additional LIKE patterns can be given in subsequent arguments",
3906  ".echo on|off             Turn command echo on or off",
3907  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3908  "   Other Modes:",
3909#ifdef SQLITE_DEBUG
3910  "      test                  Show raw EXPLAIN QUERY PLAN output",
3911  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3912#endif
3913  "      trigger               Like \"full\" but also show trigger bytecode",
3914  ".excel                   Display the output of next command in spreadsheet",
3915  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
3916  ".exit ?CODE?             Exit this program with return-code CODE",
3917  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3918  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3919  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3920  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
3921  "   --help                  Show CMD details",
3922  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3923  ".headers on|off          Turn display of headers on or off",
3924  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3925  ".import FILE TABLE       Import data from FILE into TABLE",
3926  "   Options:",
3927  "     --ascii               Use \\037 and \\036 as column and row separators",
3928  "     --csv                 Use , and \\n as column and row separators",
3929  "     --skip N              Skip the first N rows of input",
3930  "     -v                    \"Verbose\" - increase auxiliary output",
3931  "   Notes:",
3932  "     *  If TABLE does not exist, it is created.  The first row of input",
3933  "        determines the column names.",
3934  "     *  If neither --csv or --ascii are used, the input mode is derived",
3935  "        from the \".mode\" output mode",
3936  "     *  If FILE begins with \"|\" then it is a command that generates the",
3937  "        input text.",
3938#ifndef SQLITE_OMIT_TEST_CONTROL
3939  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3940#endif
3941  ".indexes ?TABLE?         Show names of indexes",
3942  "                           If TABLE is specified, only show indexes for",
3943  "                           tables matching TABLE using the LIKE operator.",
3944#ifdef SQLITE_ENABLE_IOTRACE
3945  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3946#endif
3947  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3948  ".lint OPTIONS            Report potential schema issues.",
3949  "     Options:",
3950  "        fkey-indexes     Find missing foreign key indexes",
3951#ifndef SQLITE_OMIT_LOAD_EXTENSION
3952  ".load FILE ?ENTRY?       Load an extension library",
3953#endif
3954  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3955  ".mode MODE ?TABLE?       Set output mode",
3956  "   MODE is one of:",
3957  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
3958  "     box       Tables using unicode box-drawing characters",
3959  "     csv       Comma-separated values",
3960  "     column    Output in columns.  (See .width)",
3961  "     html      HTML <table> code",
3962  "     insert    SQL insert statements for TABLE",
3963  "     json      Results in a JSON array",
3964  "     line      One value per line",
3965  "     list      Values delimited by \"|\"",
3966  "     markdown  Markdown table format",
3967  "     quote     Escape answers as for SQL",
3968  "     table     ASCII-art table",
3969  "     tabs      Tab-separated values",
3970  "     tcl       TCL list elements",
3971  ".nullvalue STRING        Use STRING in place of NULL values",
3972  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
3973  "     If FILE begins with '|' then open as a pipe",
3974  "       --bom  Put a UTF8 byte-order mark at the beginning",
3975  "       -e     Send output to the system text editor",
3976  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
3977#ifdef SQLITE_DEBUG
3978  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
3979#endif
3980  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3981  "     Options:",
3982  "        --append        Use appendvfs to append database to the end of FILE",
3983#ifndef SQLITE_OMIT_DESERIALIZE
3984  "        --deserialize   Load into memory using sqlite3_deserialize()",
3985  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3986  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3987#endif
3988  "        --new           Initialize FILE to an empty database",
3989  "        --nofollow      Do not follow symbolic links",
3990  "        --readonly      Open FILE readonly",
3991  "        --zip           FILE is a ZIP archive",
3992  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
3993  "   If FILE begins with '|' then open it as a pipe.",
3994  "   Options:",
3995  "     --bom                 Prefix output with a UTF8 byte-order mark",
3996  "     -e                    Send output to the system text editor",
3997  "     -x                    Send output as CSV to a spreadsheet",
3998  ".parameter CMD ...       Manage SQL parameter bindings",
3999  "   clear                   Erase all bindings",
4000  "   init                    Initialize the TEMP table that holds bindings",
4001  "   list                    List the current parameter bindings",
4002  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4003  "                           PARAMETER should start with one of: $ : @ ?",
4004  "   unset PARAMETER         Remove PARAMETER from the binding table",
4005  ".print STRING...         Print literal STRING",
4006#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4007  ".progress N              Invoke progress handler after every N opcodes",
4008  "   --limit N                 Interrupt after N progress callbacks",
4009  "   --once                    Do no more than one progress interrupt",
4010  "   --quiet|-q                No output except at interrupts",
4011  "   --reset                   Reset the count for each input and interrupt",
4012#endif
4013  ".prompt MAIN CONTINUE    Replace the standard prompts",
4014  ".quit                    Exit this program",
4015  ".read FILE               Read input from FILE",
4016#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4017  ".recover                 Recover as much data as possible from corrupt db.",
4018  "   --freelist-corrupt       Assume the freelist is corrupt",
4019  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4020  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4021  "   --no-rowids              Do not attempt to recover rowid values",
4022  "                            that are not also INTEGER PRIMARY KEYs",
4023#endif
4024  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4025  ".save FILE               Write in-memory database into FILE",
4026  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4027  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4028  "   Options:",
4029  "      --indent             Try to pretty-print the schema",
4030  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4031  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4032  "    Options:",
4033  "       --init               Create a new SELFTEST table",
4034  "       -v                   Verbose output",
4035  ".separator COL ?ROW?     Change the column and row separators",
4036#if defined(SQLITE_ENABLE_SESSION)
4037  ".session ?NAME? CMD ...  Create or control sessions",
4038  "   Subcommands:",
4039  "     attach TABLE             Attach TABLE",
4040  "     changeset FILE           Write a changeset into FILE",
4041  "     close                    Close one session",
4042  "     enable ?BOOLEAN?         Set or query the enable bit",
4043  "     filter GLOB...           Reject tables matching GLOBs",
4044  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4045  "     isempty                  Query whether the session is empty",
4046  "     list                     List currently open session names",
4047  "     open DB NAME             Open a new session on DB",
4048  "     patchset FILE            Write a patchset into FILE",
4049  "   If ?NAME? is omitted, the first defined session is used.",
4050#endif
4051  ".sha3sum ...             Compute a SHA3 hash of database content",
4052  "    Options:",
4053  "      --schema              Also hash the sqlite_schema table",
4054  "      --sha3-224            Use the sha3-224 algorithm",
4055  "      --sha3-256            Use the sha3-256 algorithm (default)",
4056  "      --sha3-384            Use the sha3-384 algorithm",
4057  "      --sha3-512            Use the sha3-512 algorithm",
4058  "    Any other argument is a LIKE pattern for tables to hash",
4059#ifndef SQLITE_NOHAVE_SYSTEM
4060  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4061#endif
4062  ".show                    Show the current values for various settings",
4063  ".stats ?ARG?             Show stats or turn stats on or off",
4064  "   off                      Turn off automatic stat display",
4065  "   on                       Turn on automatic stat display",
4066  "   stmt                     Show statement stats",
4067  "   vmstep                   Show the virtual machine step count only",
4068#ifndef SQLITE_NOHAVE_SYSTEM
4069  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4070#endif
4071  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4072  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4073  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4074  "                           Run \".testctrl\" with no arguments for details",
4075  ".timeout MS              Try opening locked tables for MS milliseconds",
4076  ".timer on|off            Turn SQL timer on or off",
4077#ifndef SQLITE_OMIT_TRACE
4078  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4079  "    FILE                    Send output to FILE",
4080  "    stdout                  Send output to stdout",
4081  "    stderr                  Send output to stderr",
4082  "    off                     Disable tracing",
4083  "    --expanded              Expand query parameters",
4084#ifdef SQLITE_ENABLE_NORMALIZE
4085  "    --normalized            Normal the SQL statements",
4086#endif
4087  "    --plain                 Show SQL as it is input",
4088  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4089  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4090  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4091  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4092#endif /* SQLITE_OMIT_TRACE */
4093#ifdef SQLITE_DEBUG
4094  ".unmodule NAME ...       Unregister virtual table modules",
4095  "    --allexcept             Unregister everything except those named",
4096#endif
4097  ".vfsinfo ?AUX?           Information about the top-level VFS",
4098  ".vfslist                 List all available VFSes",
4099  ".vfsname ?AUX?           Print the name of the VFS stack",
4100  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4101  "     Negative values right-justify",
4102};
4103
4104/*
4105** Output help text.
4106**
4107** zPattern describes the set of commands for which help text is provided.
4108** If zPattern is NULL, then show all commands, but only give a one-line
4109** description of each.
4110**
4111** Return the number of matches.
4112*/
4113static int showHelp(FILE *out, const char *zPattern){
4114  int i = 0;
4115  int j = 0;
4116  int n = 0;
4117  char *zPat;
4118  if( zPattern==0
4119   || zPattern[0]=='0'
4120   || strcmp(zPattern,"-a")==0
4121   || strcmp(zPattern,"-all")==0
4122   || strcmp(zPattern,"--all")==0
4123  ){
4124    /* Show all commands, but only one line per command */
4125    if( zPattern==0 ) zPattern = "";
4126    for(i=0; i<ArraySize(azHelp); i++){
4127      if( azHelp[i][0]=='.' || zPattern[0] ){
4128        utf8_printf(out, "%s\n", azHelp[i]);
4129        n++;
4130      }
4131    }
4132  }else{
4133    /* Look for commands that for which zPattern is an exact prefix */
4134    zPat = sqlite3_mprintf(".%s*", zPattern);
4135    for(i=0; i<ArraySize(azHelp); i++){
4136      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4137        utf8_printf(out, "%s\n", azHelp[i]);
4138        j = i+1;
4139        n++;
4140      }
4141    }
4142    sqlite3_free(zPat);
4143    if( n ){
4144      if( n==1 ){
4145        /* when zPattern is a prefix of exactly one command, then include the
4146        ** details of that command, which should begin at offset j */
4147        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4148          utf8_printf(out, "%s\n", azHelp[j]);
4149          j++;
4150        }
4151      }
4152      return n;
4153    }
4154    /* Look for commands that contain zPattern anywhere.  Show the complete
4155    ** text of all commands that match. */
4156    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4157    for(i=0; i<ArraySize(azHelp); i++){
4158      if( azHelp[i][0]=='.' ) j = i;
4159      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4160        utf8_printf(out, "%s\n", azHelp[j]);
4161        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4162          j++;
4163          utf8_printf(out, "%s\n", azHelp[j]);
4164        }
4165        i = j;
4166        n++;
4167      }
4168    }
4169    sqlite3_free(zPat);
4170  }
4171  return n;
4172}
4173
4174/* Forward reference */
4175static int process_input(ShellState *p);
4176
4177/*
4178** Read the content of file zName into memory obtained from sqlite3_malloc64()
4179** and return a pointer to the buffer. The caller is responsible for freeing
4180** the memory.
4181**
4182** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4183** read.
4184**
4185** For convenience, a nul-terminator byte is always appended to the data read
4186** from the file before the buffer is returned. This byte is not included in
4187** the final value of (*pnByte), if applicable.
4188**
4189** NULL is returned if any error is encountered. The final value of *pnByte
4190** is undefined in this case.
4191*/
4192static char *readFile(const char *zName, int *pnByte){
4193  FILE *in = fopen(zName, "rb");
4194  long nIn;
4195  size_t nRead;
4196  char *pBuf;
4197  if( in==0 ) return 0;
4198  fseek(in, 0, SEEK_END);
4199  nIn = ftell(in);
4200  rewind(in);
4201  pBuf = sqlite3_malloc64( nIn+1 );
4202  if( pBuf==0 ){ fclose(in); return 0; }
4203  nRead = fread(pBuf, nIn, 1, in);
4204  fclose(in);
4205  if( nRead!=1 ){
4206    sqlite3_free(pBuf);
4207    return 0;
4208  }
4209  pBuf[nIn] = 0;
4210  if( pnByte ) *pnByte = nIn;
4211  return pBuf;
4212}
4213
4214#if defined(SQLITE_ENABLE_SESSION)
4215/*
4216** Close a single OpenSession object and release all of its associated
4217** resources.
4218*/
4219static void session_close(OpenSession *pSession){
4220  int i;
4221  sqlite3session_delete(pSession->p);
4222  sqlite3_free(pSession->zName);
4223  for(i=0; i<pSession->nFilter; i++){
4224    sqlite3_free(pSession->azFilter[i]);
4225  }
4226  sqlite3_free(pSession->azFilter);
4227  memset(pSession, 0, sizeof(OpenSession));
4228}
4229#endif
4230
4231/*
4232** Close all OpenSession objects and release all associated resources.
4233*/
4234#if defined(SQLITE_ENABLE_SESSION)
4235static void session_close_all(ShellState *p){
4236  int i;
4237  for(i=0; i<p->nSession; i++){
4238    session_close(&p->aSession[i]);
4239  }
4240  p->nSession = 0;
4241}
4242#else
4243# define session_close_all(X)
4244#endif
4245
4246/*
4247** Implementation of the xFilter function for an open session.  Omit
4248** any tables named by ".session filter" but let all other table through.
4249*/
4250#if defined(SQLITE_ENABLE_SESSION)
4251static int session_filter(void *pCtx, const char *zTab){
4252  OpenSession *pSession = (OpenSession*)pCtx;
4253  int i;
4254  for(i=0; i<pSession->nFilter; i++){
4255    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4256  }
4257  return 1;
4258}
4259#endif
4260
4261/*
4262** Try to deduce the type of file for zName based on its content.  Return
4263** one of the SHELL_OPEN_* constants.
4264**
4265** If the file does not exist or is empty but its name looks like a ZIP
4266** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4267** Otherwise, assume an ordinary database regardless of the filename if
4268** the type cannot be determined from content.
4269*/
4270int deduceDatabaseType(const char *zName, int dfltZip){
4271  FILE *f = fopen(zName, "rb");
4272  size_t n;
4273  int rc = SHELL_OPEN_UNSPEC;
4274  char zBuf[100];
4275  if( f==0 ){
4276    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4277       return SHELL_OPEN_ZIPFILE;
4278    }else{
4279       return SHELL_OPEN_NORMAL;
4280    }
4281  }
4282  n = fread(zBuf, 16, 1, f);
4283  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4284    fclose(f);
4285    return SHELL_OPEN_NORMAL;
4286  }
4287  fseek(f, -25, SEEK_END);
4288  n = fread(zBuf, 25, 1, f);
4289  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4290    rc = SHELL_OPEN_APPENDVFS;
4291  }else{
4292    fseek(f, -22, SEEK_END);
4293    n = fread(zBuf, 22, 1, f);
4294    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4295       && zBuf[3]==0x06 ){
4296      rc = SHELL_OPEN_ZIPFILE;
4297    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4298      rc = SHELL_OPEN_ZIPFILE;
4299    }
4300  }
4301  fclose(f);
4302  return rc;
4303}
4304
4305#ifndef SQLITE_OMIT_DESERIALIZE
4306/*
4307** Reconstruct an in-memory database using the output from the "dbtotxt"
4308** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
4309** is 0, then read from standard input.
4310*/
4311static unsigned char *readHexDb(ShellState *p, int *pnData){
4312  unsigned char *a = 0;
4313  int nLine;
4314  int n = 0;
4315  int pgsz = 0;
4316  int iOffset = 0;
4317  int j, k;
4318  int rc;
4319  FILE *in;
4320  unsigned int x[16];
4321  char zLine[1000];
4322  if( p->zDbFilename ){
4323    in = fopen(p->zDbFilename, "r");
4324    if( in==0 ){
4325      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
4326      return 0;
4327    }
4328    nLine = 0;
4329  }else{
4330    in = p->in;
4331    nLine = p->lineno;
4332    if( in==0 ) in = stdin;
4333  }
4334  *pnData = 0;
4335  nLine++;
4336  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4337  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4338  if( rc!=2 ) goto readHexDb_error;
4339  if( n<0 ) goto readHexDb_error;
4340  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4341  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4342  a = sqlite3_malloc( n ? n : 1 );
4343  if( a==0 ){
4344    utf8_printf(stderr, "Out of memory!\n");
4345    goto readHexDb_error;
4346  }
4347  memset(a, 0, n);
4348  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4349    utf8_printf(stderr, "invalid pagesize\n");
4350    goto readHexDb_error;
4351  }
4352  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4353    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4354    if( rc==2 ){
4355      iOffset = k;
4356      continue;
4357    }
4358    if( strncmp(zLine, "| end ", 6)==0 ){
4359      break;
4360    }
4361    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4362                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4363                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4364    if( rc==17 ){
4365      k = iOffset+j;
4366      if( k+16<=n ){
4367        int ii;
4368        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4369      }
4370    }
4371  }
4372  *pnData = n;
4373  if( in!=p->in ){
4374    fclose(in);
4375  }else{
4376    p->lineno = nLine;
4377  }
4378  return a;
4379
4380readHexDb_error:
4381  if( in!=p->in ){
4382    fclose(in);
4383  }else{
4384    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4385      nLine++;
4386      if(strncmp(zLine, "| end ", 6)==0 ) break;
4387    }
4388    p->lineno = nLine;
4389  }
4390  sqlite3_free(a);
4391  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4392  return 0;
4393}
4394#endif /* SQLITE_OMIT_DESERIALIZE */
4395
4396/*
4397** Scalar function "shell_int32". The first argument to this function
4398** must be a blob. The second a non-negative integer. This function
4399** reads and returns a 32-bit big-endian integer from byte
4400** offset (4*<arg2>) of the blob.
4401*/
4402static void shellInt32(
4403  sqlite3_context *context,
4404  int argc,
4405  sqlite3_value **argv
4406){
4407  const unsigned char *pBlob;
4408  int nBlob;
4409  int iInt;
4410
4411  UNUSED_PARAMETER(argc);
4412  nBlob = sqlite3_value_bytes(argv[0]);
4413  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4414  iInt = sqlite3_value_int(argv[1]);
4415
4416  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4417    const unsigned char *a = &pBlob[iInt*4];
4418    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4419                       + ((sqlite3_int64)a[1]<<16)
4420                       + ((sqlite3_int64)a[2]<< 8)
4421                       + ((sqlite3_int64)a[3]<< 0);
4422    sqlite3_result_int64(context, iVal);
4423  }
4424}
4425
4426/*
4427** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4428** using "..." with internal double-quote characters doubled.
4429*/
4430static void shellIdQuote(
4431  sqlite3_context *context,
4432  int argc,
4433  sqlite3_value **argv
4434){
4435  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4436  UNUSED_PARAMETER(argc);
4437  if( zName ){
4438    char *z = sqlite3_mprintf("\"%w\"", zName);
4439    sqlite3_result_text(context, z, -1, sqlite3_free);
4440  }
4441}
4442
4443/*
4444** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4445*/
4446static void shellUSleepFunc(
4447  sqlite3_context *context,
4448  int argcUnused,
4449  sqlite3_value **argv
4450){
4451  int sleep = sqlite3_value_int(argv[0]);
4452  (void)argcUnused;
4453  sqlite3_sleep(sleep/1000);
4454  sqlite3_result_int(context, sleep);
4455}
4456
4457/*
4458** Scalar function "shell_escape_crnl" used by the .recover command.
4459** The argument passed to this function is the output of built-in
4460** function quote(). If the first character of the input is "'",
4461** indicating that the value passed to quote() was a text value,
4462** then this function searches the input for "\n" and "\r" characters
4463** and adds a wrapper similar to the following:
4464**
4465**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4466**
4467** Or, if the first character of the input is not "'", then a copy
4468** of the input is returned.
4469*/
4470static void shellEscapeCrnl(
4471  sqlite3_context *context,
4472  int argc,
4473  sqlite3_value **argv
4474){
4475  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4476  UNUSED_PARAMETER(argc);
4477  if( zText[0]=='\'' ){
4478    int nText = sqlite3_value_bytes(argv[0]);
4479    int i;
4480    char zBuf1[20];
4481    char zBuf2[20];
4482    const char *zNL = 0;
4483    const char *zCR = 0;
4484    int nCR = 0;
4485    int nNL = 0;
4486
4487    for(i=0; zText[i]; i++){
4488      if( zNL==0 && zText[i]=='\n' ){
4489        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4490        nNL = (int)strlen(zNL);
4491      }
4492      if( zCR==0 && zText[i]=='\r' ){
4493        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4494        nCR = (int)strlen(zCR);
4495      }
4496    }
4497
4498    if( zNL || zCR ){
4499      int iOut = 0;
4500      i64 nMax = (nNL > nCR) ? nNL : nCR;
4501      i64 nAlloc = nMax * nText + (nMax+64)*2;
4502      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4503      if( zOut==0 ){
4504        sqlite3_result_error_nomem(context);
4505        return;
4506      }
4507
4508      if( zNL && zCR ){
4509        memcpy(&zOut[iOut], "replace(replace(", 16);
4510        iOut += 16;
4511      }else{
4512        memcpy(&zOut[iOut], "replace(", 8);
4513        iOut += 8;
4514      }
4515      for(i=0; zText[i]; i++){
4516        if( zText[i]=='\n' ){
4517          memcpy(&zOut[iOut], zNL, nNL);
4518          iOut += nNL;
4519        }else if( zText[i]=='\r' ){
4520          memcpy(&zOut[iOut], zCR, nCR);
4521          iOut += nCR;
4522        }else{
4523          zOut[iOut] = zText[i];
4524          iOut++;
4525        }
4526      }
4527
4528      if( zNL ){
4529        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4530        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4531        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4532      }
4533      if( zCR ){
4534        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4535        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4536        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4537      }
4538
4539      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4540      sqlite3_free(zOut);
4541      return;
4542    }
4543  }
4544
4545  sqlite3_result_value(context, argv[0]);
4546}
4547
4548/* Flags for open_db().
4549**
4550** The default behavior of open_db() is to exit(1) if the database fails to
4551** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4552** but still returns without calling exit.
4553**
4554** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4555** ZIP archive if the file does not exist or is empty and its name matches
4556** the *.zip pattern.
4557*/
4558#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4559#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4560
4561/*
4562** Make sure the database is open.  If it is not, then open it.  If
4563** the database fails to open, print an error message and exit.
4564*/
4565static void open_db(ShellState *p, int openFlags){
4566  if( p->db==0 ){
4567    if( p->openMode==SHELL_OPEN_UNSPEC ){
4568      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4569        p->openMode = SHELL_OPEN_NORMAL;
4570      }else{
4571        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4572                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4573      }
4574    }
4575    switch( p->openMode ){
4576      case SHELL_OPEN_APPENDVFS: {
4577        sqlite3_open_v2(p->zDbFilename, &p->db,
4578           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4579        break;
4580      }
4581      case SHELL_OPEN_HEXDB:
4582      case SHELL_OPEN_DESERIALIZE: {
4583        sqlite3_open(0, &p->db);
4584        break;
4585      }
4586      case SHELL_OPEN_ZIPFILE: {
4587        sqlite3_open(":memory:", &p->db);
4588        break;
4589      }
4590      case SHELL_OPEN_READONLY: {
4591        sqlite3_open_v2(p->zDbFilename, &p->db,
4592            SQLITE_OPEN_READONLY|p->openFlags, 0);
4593        break;
4594      }
4595      case SHELL_OPEN_UNSPEC:
4596      case SHELL_OPEN_NORMAL: {
4597        sqlite3_open_v2(p->zDbFilename, &p->db,
4598           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4599        break;
4600      }
4601    }
4602    globalDb = p->db;
4603    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4604      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4605          p->zDbFilename, sqlite3_errmsg(p->db));
4606      if( openFlags & OPEN_DB_KEEPALIVE ){
4607        sqlite3_open(":memory:", &p->db);
4608        return;
4609      }
4610      exit(1);
4611    }
4612#ifndef SQLITE_OMIT_LOAD_EXTENSION
4613    sqlite3_enable_load_extension(p->db, 1);
4614#endif
4615    sqlite3_fileio_init(p->db, 0, 0);
4616    sqlite3_shathree_init(p->db, 0, 0);
4617    sqlite3_completion_init(p->db, 0, 0);
4618    sqlite3_uint_init(p->db, 0, 0);
4619    sqlite3_decimal_init(p->db, 0, 0);
4620    sqlite3_regexp_init(p->db, 0, 0);
4621    sqlite3_ieee_init(p->db, 0, 0);
4622    sqlite3_series_init(p->db, 0, 0);
4623#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4624    sqlite3_dbdata_init(p->db, 0, 0);
4625#endif
4626#ifdef SQLITE_HAVE_ZLIB
4627    sqlite3_zipfile_init(p->db, 0, 0);
4628    sqlite3_sqlar_init(p->db, 0, 0);
4629#endif
4630    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4631                            shellAddSchemaName, 0, 0);
4632    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4633                            shellModuleSchema, 0, 0);
4634    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4635                            shellPutsFunc, 0, 0);
4636    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4637                            shellEscapeCrnl, 0, 0);
4638    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4639                            shellInt32, 0, 0);
4640    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4641                            shellIdQuote, 0, 0);
4642    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
4643                            shellUSleepFunc, 0, 0);
4644#ifndef SQLITE_NOHAVE_SYSTEM
4645    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4646                            editFunc, 0, 0);
4647    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4648                            editFunc, 0, 0);
4649#endif
4650    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4651      char *zSql = sqlite3_mprintf(
4652         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4653      sqlite3_exec(p->db, zSql, 0, 0, 0);
4654      sqlite3_free(zSql);
4655    }
4656#ifndef SQLITE_OMIT_DESERIALIZE
4657    else
4658    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4659      int rc;
4660      int nData = 0;
4661      unsigned char *aData;
4662      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4663        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4664      }else{
4665        aData = readHexDb(p, &nData);
4666        if( aData==0 ){
4667          return;
4668        }
4669      }
4670      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4671                   SQLITE_DESERIALIZE_RESIZEABLE |
4672                   SQLITE_DESERIALIZE_FREEONCLOSE);
4673      if( rc ){
4674        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4675      }
4676      if( p->szMax>0 ){
4677        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4678      }
4679    }
4680#endif
4681  }
4682}
4683
4684/*
4685** Attempt to close the databaes connection.  Report errors.
4686*/
4687void close_db(sqlite3 *db){
4688  int rc = sqlite3_close(db);
4689  if( rc ){
4690    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4691        rc, sqlite3_errmsg(db));
4692  }
4693}
4694
4695#if HAVE_READLINE || HAVE_EDITLINE
4696/*
4697** Readline completion callbacks
4698*/
4699static char *readline_completion_generator(const char *text, int state){
4700  static sqlite3_stmt *pStmt = 0;
4701  char *zRet;
4702  if( state==0 ){
4703    char *zSql;
4704    sqlite3_finalize(pStmt);
4705    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4706                           "  FROM completion(%Q) ORDER BY 1", text);
4707    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4708    sqlite3_free(zSql);
4709  }
4710  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4711    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4712  }else{
4713    sqlite3_finalize(pStmt);
4714    pStmt = 0;
4715    zRet = 0;
4716  }
4717  return zRet;
4718}
4719static char **readline_completion(const char *zText, int iStart, int iEnd){
4720  rl_attempted_completion_over = 1;
4721  return rl_completion_matches(zText, readline_completion_generator);
4722}
4723
4724#elif HAVE_LINENOISE
4725/*
4726** Linenoise completion callback
4727*/
4728static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4729  int nLine = strlen30(zLine);
4730  int i, iStart;
4731  sqlite3_stmt *pStmt = 0;
4732  char *zSql;
4733  char zBuf[1000];
4734
4735  if( nLine>sizeof(zBuf)-30 ) return;
4736  if( zLine[0]=='.' || zLine[0]=='#') return;
4737  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4738  if( i==nLine-1 ) return;
4739  iStart = i+1;
4740  memcpy(zBuf, zLine, iStart);
4741  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4742                         "  FROM completion(%Q,%Q) ORDER BY 1",
4743                         &zLine[iStart], zLine);
4744  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4745  sqlite3_free(zSql);
4746  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4747  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4748    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4749    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4750    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4751      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4752      linenoiseAddCompletion(lc, zBuf);
4753    }
4754  }
4755  sqlite3_finalize(pStmt);
4756}
4757#endif
4758
4759/*
4760** Do C-language style dequoting.
4761**
4762**    \a    -> alarm
4763**    \b    -> backspace
4764**    \t    -> tab
4765**    \n    -> newline
4766**    \v    -> vertical tab
4767**    \f    -> form feed
4768**    \r    -> carriage return
4769**    \s    -> space
4770**    \"    -> "
4771**    \'    -> '
4772**    \\    -> backslash
4773**    \NNN  -> ascii character NNN in octal
4774*/
4775static void resolve_backslashes(char *z){
4776  int i, j;
4777  char c;
4778  while( *z && *z!='\\' ) z++;
4779  for(i=j=0; (c = z[i])!=0; i++, j++){
4780    if( c=='\\' && z[i+1]!=0 ){
4781      c = z[++i];
4782      if( c=='a' ){
4783        c = '\a';
4784      }else if( c=='b' ){
4785        c = '\b';
4786      }else if( c=='t' ){
4787        c = '\t';
4788      }else if( c=='n' ){
4789        c = '\n';
4790      }else if( c=='v' ){
4791        c = '\v';
4792      }else if( c=='f' ){
4793        c = '\f';
4794      }else if( c=='r' ){
4795        c = '\r';
4796      }else if( c=='"' ){
4797        c = '"';
4798      }else if( c=='\'' ){
4799        c = '\'';
4800      }else if( c=='\\' ){
4801        c = '\\';
4802      }else if( c>='0' && c<='7' ){
4803        c -= '0';
4804        if( z[i+1]>='0' && z[i+1]<='7' ){
4805          i++;
4806          c = (c<<3) + z[i] - '0';
4807          if( z[i+1]>='0' && z[i+1]<='7' ){
4808            i++;
4809            c = (c<<3) + z[i] - '0';
4810          }
4811        }
4812      }
4813    }
4814    z[j] = c;
4815  }
4816  if( j<i ) z[j] = 0;
4817}
4818
4819/*
4820** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4821** for TRUE and FALSE.  Return the integer value if appropriate.
4822*/
4823static int booleanValue(const char *zArg){
4824  int i;
4825  if( zArg[0]=='0' && zArg[1]=='x' ){
4826    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4827  }else{
4828    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4829  }
4830  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4831  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4832    return 1;
4833  }
4834  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4835    return 0;
4836  }
4837  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4838          zArg);
4839  return 0;
4840}
4841
4842/*
4843** Set or clear a shell flag according to a boolean value.
4844*/
4845static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4846  if( booleanValue(zArg) ){
4847    ShellSetFlag(p, mFlag);
4848  }else{
4849    ShellClearFlag(p, mFlag);
4850  }
4851}
4852
4853/*
4854** Close an output file, assuming it is not stderr or stdout
4855*/
4856static void output_file_close(FILE *f){
4857  if( f && f!=stdout && f!=stderr ) fclose(f);
4858}
4859
4860/*
4861** Try to open an output file.   The names "stdout" and "stderr" are
4862** recognized and do the right thing.  NULL is returned if the output
4863** filename is "off".
4864*/
4865static FILE *output_file_open(const char *zFile, int bTextMode){
4866  FILE *f;
4867  if( strcmp(zFile,"stdout")==0 ){
4868    f = stdout;
4869  }else if( strcmp(zFile, "stderr")==0 ){
4870    f = stderr;
4871  }else if( strcmp(zFile, "off")==0 ){
4872    f = 0;
4873  }else{
4874    f = fopen(zFile, bTextMode ? "w" : "wb");
4875    if( f==0 ){
4876      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4877    }
4878  }
4879  return f;
4880}
4881
4882#ifndef SQLITE_OMIT_TRACE
4883/*
4884** A routine for handling output from sqlite3_trace().
4885*/
4886static int sql_trace_callback(
4887  unsigned mType,         /* The trace type */
4888  void *pArg,             /* The ShellState pointer */
4889  void *pP,               /* Usually a pointer to sqlite_stmt */
4890  void *pX                /* Auxiliary output */
4891){
4892  ShellState *p = (ShellState*)pArg;
4893  sqlite3_stmt *pStmt;
4894  const char *zSql;
4895  int nSql;
4896  if( p->traceOut==0 ) return 0;
4897  if( mType==SQLITE_TRACE_CLOSE ){
4898    utf8_printf(p->traceOut, "-- closing database connection\n");
4899    return 0;
4900  }
4901  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4902    zSql = (const char*)pX;
4903  }else{
4904    pStmt = (sqlite3_stmt*)pP;
4905    switch( p->eTraceType ){
4906      case SHELL_TRACE_EXPANDED: {
4907        zSql = sqlite3_expanded_sql(pStmt);
4908        break;
4909      }
4910#ifdef SQLITE_ENABLE_NORMALIZE
4911      case SHELL_TRACE_NORMALIZED: {
4912        zSql = sqlite3_normalized_sql(pStmt);
4913        break;
4914      }
4915#endif
4916      default: {
4917        zSql = sqlite3_sql(pStmt);
4918        break;
4919      }
4920    }
4921  }
4922  if( zSql==0 ) return 0;
4923  nSql = strlen30(zSql);
4924  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4925  switch( mType ){
4926    case SQLITE_TRACE_ROW:
4927    case SQLITE_TRACE_STMT: {
4928      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4929      break;
4930    }
4931    case SQLITE_TRACE_PROFILE: {
4932      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4933      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4934      break;
4935    }
4936  }
4937  return 0;
4938}
4939#endif
4940
4941/*
4942** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4943** a useful spot to set a debugger breakpoint.
4944*/
4945static void test_breakpoint(void){
4946  static int nCall = 0;
4947  nCall++;
4948}
4949
4950/*
4951** An object used to read a CSV and other files for import.
4952*/
4953typedef struct ImportCtx ImportCtx;
4954struct ImportCtx {
4955  const char *zFile;  /* Name of the input file */
4956  FILE *in;           /* Read the CSV text from this input stream */
4957  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
4958  char *z;            /* Accumulated text for a field */
4959  int n;              /* Number of bytes in z */
4960  int nAlloc;         /* Space allocated for z[] */
4961  int nLine;          /* Current line number */
4962  int nRow;           /* Number of rows imported */
4963  int nErr;           /* Number of errors encountered */
4964  int bNotFirst;      /* True if one or more bytes already read */
4965  int cTerm;          /* Character that terminated the most recent field */
4966  int cColSep;        /* The column separator character.  (Usually ",") */
4967  int cRowSep;        /* The row separator character.  (Usually "\n") */
4968};
4969
4970/* Clean up resourced used by an ImportCtx */
4971static void import_cleanup(ImportCtx *p){
4972  if( p->in!=0 && p->xCloser!=0 ){
4973    p->xCloser(p->in);
4974    p->in = 0;
4975  }
4976  sqlite3_free(p->z);
4977  p->z = 0;
4978}
4979
4980/* Append a single byte to z[] */
4981static void import_append_char(ImportCtx *p, int c){
4982  if( p->n+1>=p->nAlloc ){
4983    p->nAlloc += p->nAlloc + 100;
4984    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4985    if( p->z==0 ) shell_out_of_memory();
4986  }
4987  p->z[p->n++] = (char)c;
4988}
4989
4990/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4991** with the option of having a separator other than ",".
4992**
4993**   +  Input comes from p->in.
4994**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4995**      from sqlite3_malloc64().
4996**   +  Use p->cSep as the column separator.  The default is ",".
4997**   +  Use p->rSep as the row separator.  The default is "\n".
4998**   +  Keep track of the line number in p->nLine.
4999**   +  Store the character that terminates the field in p->cTerm.  Store
5000**      EOF on end-of-file.
5001**   +  Report syntax errors on stderr
5002*/
5003static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5004  int c;
5005  int cSep = p->cColSep;
5006  int rSep = p->cRowSep;
5007  p->n = 0;
5008  c = fgetc(p->in);
5009  if( c==EOF || seenInterrupt ){
5010    p->cTerm = EOF;
5011    return 0;
5012  }
5013  if( c=='"' ){
5014    int pc, ppc;
5015    int startLine = p->nLine;
5016    int cQuote = c;
5017    pc = ppc = 0;
5018    while( 1 ){
5019      c = fgetc(p->in);
5020      if( c==rSep ) p->nLine++;
5021      if( c==cQuote ){
5022        if( pc==cQuote ){
5023          pc = 0;
5024          continue;
5025        }
5026      }
5027      if( (c==cSep && pc==cQuote)
5028       || (c==rSep && pc==cQuote)
5029       || (c==rSep && pc=='\r' && ppc==cQuote)
5030       || (c==EOF && pc==cQuote)
5031      ){
5032        do{ p->n--; }while( p->z[p->n]!=cQuote );
5033        p->cTerm = c;
5034        break;
5035      }
5036      if( pc==cQuote && c!='\r' ){
5037        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5038                p->zFile, p->nLine, cQuote);
5039      }
5040      if( c==EOF ){
5041        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5042                p->zFile, startLine, cQuote);
5043        p->cTerm = c;
5044        break;
5045      }
5046      import_append_char(p, c);
5047      ppc = pc;
5048      pc = c;
5049    }
5050  }else{
5051    /* If this is the first field being parsed and it begins with the
5052    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5053    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5054      import_append_char(p, c);
5055      c = fgetc(p->in);
5056      if( (c&0xff)==0xbb ){
5057        import_append_char(p, c);
5058        c = fgetc(p->in);
5059        if( (c&0xff)==0xbf ){
5060          p->bNotFirst = 1;
5061          p->n = 0;
5062          return csv_read_one_field(p);
5063        }
5064      }
5065    }
5066    while( c!=EOF && c!=cSep && c!=rSep ){
5067      import_append_char(p, c);
5068      c = fgetc(p->in);
5069    }
5070    if( c==rSep ){
5071      p->nLine++;
5072      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5073    }
5074    p->cTerm = c;
5075  }
5076  if( p->z ) p->z[p->n] = 0;
5077  p->bNotFirst = 1;
5078  return p->z;
5079}
5080
5081/* Read a single field of ASCII delimited text.
5082**
5083**   +  Input comes from p->in.
5084**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5085**      from sqlite3_malloc64().
5086**   +  Use p->cSep as the column separator.  The default is "\x1F".
5087**   +  Use p->rSep as the row separator.  The default is "\x1E".
5088**   +  Keep track of the row number in p->nLine.
5089**   +  Store the character that terminates the field in p->cTerm.  Store
5090**      EOF on end-of-file.
5091**   +  Report syntax errors on stderr
5092*/
5093static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5094  int c;
5095  int cSep = p->cColSep;
5096  int rSep = p->cRowSep;
5097  p->n = 0;
5098  c = fgetc(p->in);
5099  if( c==EOF || seenInterrupt ){
5100    p->cTerm = EOF;
5101    return 0;
5102  }
5103  while( c!=EOF && c!=cSep && c!=rSep ){
5104    import_append_char(p, c);
5105    c = fgetc(p->in);
5106  }
5107  if( c==rSep ){
5108    p->nLine++;
5109  }
5110  p->cTerm = c;
5111  if( p->z ) p->z[p->n] = 0;
5112  return p->z;
5113}
5114
5115/*
5116** Try to transfer data for table zTable.  If an error is seen while
5117** moving forward, try to go backwards.  The backwards movement won't
5118** work for WITHOUT ROWID tables.
5119*/
5120static void tryToCloneData(
5121  ShellState *p,
5122  sqlite3 *newDb,
5123  const char *zTable
5124){
5125  sqlite3_stmt *pQuery = 0;
5126  sqlite3_stmt *pInsert = 0;
5127  char *zQuery = 0;
5128  char *zInsert = 0;
5129  int rc;
5130  int i, j, n;
5131  int nTable = strlen30(zTable);
5132  int k = 0;
5133  int cnt = 0;
5134  const int spinRate = 10000;
5135
5136  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5137  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5138  if( rc ){
5139    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5140            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5141            zQuery);
5142    goto end_data_xfer;
5143  }
5144  n = sqlite3_column_count(pQuery);
5145  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5146  if( zInsert==0 ) shell_out_of_memory();
5147  sqlite3_snprintf(200+nTable,zInsert,
5148                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5149  i = strlen30(zInsert);
5150  for(j=1; j<n; j++){
5151    memcpy(zInsert+i, ",?", 2);
5152    i += 2;
5153  }
5154  memcpy(zInsert+i, ");", 3);
5155  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5156  if( rc ){
5157    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5158            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5159            zQuery);
5160    goto end_data_xfer;
5161  }
5162  for(k=0; k<2; k++){
5163    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5164      for(i=0; i<n; i++){
5165        switch( sqlite3_column_type(pQuery, i) ){
5166          case SQLITE_NULL: {
5167            sqlite3_bind_null(pInsert, i+1);
5168            break;
5169          }
5170          case SQLITE_INTEGER: {
5171            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5172            break;
5173          }
5174          case SQLITE_FLOAT: {
5175            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5176            break;
5177          }
5178          case SQLITE_TEXT: {
5179            sqlite3_bind_text(pInsert, i+1,
5180                             (const char*)sqlite3_column_text(pQuery,i),
5181                             -1, SQLITE_STATIC);
5182            break;
5183          }
5184          case SQLITE_BLOB: {
5185            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5186                                            sqlite3_column_bytes(pQuery,i),
5187                                            SQLITE_STATIC);
5188            break;
5189          }
5190        }
5191      } /* End for */
5192      rc = sqlite3_step(pInsert);
5193      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5194        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5195                        sqlite3_errmsg(newDb));
5196      }
5197      sqlite3_reset(pInsert);
5198      cnt++;
5199      if( (cnt%spinRate)==0 ){
5200        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5201        fflush(stdout);
5202      }
5203    } /* End while */
5204    if( rc==SQLITE_DONE ) break;
5205    sqlite3_finalize(pQuery);
5206    sqlite3_free(zQuery);
5207    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5208                             zTable);
5209    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5210    if( rc ){
5211      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5212      break;
5213    }
5214  } /* End for(k=0...) */
5215
5216end_data_xfer:
5217  sqlite3_finalize(pQuery);
5218  sqlite3_finalize(pInsert);
5219  sqlite3_free(zQuery);
5220  sqlite3_free(zInsert);
5221}
5222
5223
5224/*
5225** Try to transfer all rows of the schema that match zWhere.  For
5226** each row, invoke xForEach() on the object defined by that row.
5227** If an error is encountered while moving forward through the
5228** sqlite_schema table, try again moving backwards.
5229*/
5230static void tryToCloneSchema(
5231  ShellState *p,
5232  sqlite3 *newDb,
5233  const char *zWhere,
5234  void (*xForEach)(ShellState*,sqlite3*,const char*)
5235){
5236  sqlite3_stmt *pQuery = 0;
5237  char *zQuery = 0;
5238  int rc;
5239  const unsigned char *zName;
5240  const unsigned char *zSql;
5241  char *zErrMsg = 0;
5242
5243  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5244                           " WHERE %s", zWhere);
5245  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5246  if( rc ){
5247    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5248                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5249                    zQuery);
5250    goto end_schema_xfer;
5251  }
5252  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5253    zName = sqlite3_column_text(pQuery, 0);
5254    zSql = sqlite3_column_text(pQuery, 1);
5255    printf("%s... ", zName); fflush(stdout);
5256    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5257    if( zErrMsg ){
5258      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5259      sqlite3_free(zErrMsg);
5260      zErrMsg = 0;
5261    }
5262    if( xForEach ){
5263      xForEach(p, newDb, (const char*)zName);
5264    }
5265    printf("done\n");
5266  }
5267  if( rc!=SQLITE_DONE ){
5268    sqlite3_finalize(pQuery);
5269    sqlite3_free(zQuery);
5270    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5271                             " WHERE %s ORDER BY rowid DESC", zWhere);
5272    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5273    if( rc ){
5274      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5275                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5276                      zQuery);
5277      goto end_schema_xfer;
5278    }
5279    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5280      zName = sqlite3_column_text(pQuery, 0);
5281      zSql = sqlite3_column_text(pQuery, 1);
5282      printf("%s... ", zName); fflush(stdout);
5283      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5284      if( zErrMsg ){
5285        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5286        sqlite3_free(zErrMsg);
5287        zErrMsg = 0;
5288      }
5289      if( xForEach ){
5290        xForEach(p, newDb, (const char*)zName);
5291      }
5292      printf("done\n");
5293    }
5294  }
5295end_schema_xfer:
5296  sqlite3_finalize(pQuery);
5297  sqlite3_free(zQuery);
5298}
5299
5300/*
5301** Open a new database file named "zNewDb".  Try to recover as much information
5302** as possible out of the main database (which might be corrupt) and write it
5303** into zNewDb.
5304*/
5305static void tryToClone(ShellState *p, const char *zNewDb){
5306  int rc;
5307  sqlite3 *newDb = 0;
5308  if( access(zNewDb,0)==0 ){
5309    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5310    return;
5311  }
5312  rc = sqlite3_open(zNewDb, &newDb);
5313  if( rc ){
5314    utf8_printf(stderr, "Cannot create output database: %s\n",
5315            sqlite3_errmsg(newDb));
5316  }else{
5317    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5318    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5319    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5320    tryToCloneSchema(p, newDb, "type!='table'", 0);
5321    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5322    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5323  }
5324  close_db(newDb);
5325}
5326
5327/*
5328** Change the output file back to stdout.
5329**
5330** If the p->doXdgOpen flag is set, that means the output was being
5331** redirected to a temporary file named by p->zTempFile.  In that case,
5332** launch start/open/xdg-open on that temporary file.
5333*/
5334static void output_reset(ShellState *p){
5335  if( p->outfile[0]=='|' ){
5336#ifndef SQLITE_OMIT_POPEN
5337    pclose(p->out);
5338#endif
5339  }else{
5340    output_file_close(p->out);
5341#ifndef SQLITE_NOHAVE_SYSTEM
5342    if( p->doXdgOpen ){
5343      const char *zXdgOpenCmd =
5344#if defined(_WIN32)
5345      "start";
5346#elif defined(__APPLE__)
5347      "open";
5348#else
5349      "xdg-open";
5350#endif
5351      char *zCmd;
5352      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5353      if( system(zCmd) ){
5354        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5355      }else{
5356        /* Give the start/open/xdg-open command some time to get
5357        ** going before we continue, and potential delete the
5358        ** p->zTempFile data file out from under it */
5359        sqlite3_sleep(2000);
5360      }
5361      sqlite3_free(zCmd);
5362      outputModePop(p);
5363      p->doXdgOpen = 0;
5364    }
5365#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5366  }
5367  p->outfile[0] = 0;
5368  p->out = stdout;
5369}
5370
5371/*
5372** Run an SQL command and return the single integer result.
5373*/
5374static int db_int(ShellState *p, const char *zSql){
5375  sqlite3_stmt *pStmt;
5376  int res = 0;
5377  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5378  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5379    res = sqlite3_column_int(pStmt,0);
5380  }
5381  sqlite3_finalize(pStmt);
5382  return res;
5383}
5384
5385/*
5386** Convert a 2-byte or 4-byte big-endian integer into a native integer
5387*/
5388static unsigned int get2byteInt(unsigned char *a){
5389  return (a[0]<<8) + a[1];
5390}
5391static unsigned int get4byteInt(unsigned char *a){
5392  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5393}
5394
5395/*
5396** Implementation of the ".dbinfo" command.
5397**
5398** Return 1 on error, 2 to exit, and 0 otherwise.
5399*/
5400static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5401  static const struct { const char *zName; int ofst; } aField[] = {
5402     { "file change counter:",  24  },
5403     { "database page count:",  28  },
5404     { "freelist page count:",  36  },
5405     { "schema cookie:",        40  },
5406     { "schema format:",        44  },
5407     { "default cache size:",   48  },
5408     { "autovacuum top root:",  52  },
5409     { "incremental vacuum:",   64  },
5410     { "text encoding:",        56  },
5411     { "user version:",         60  },
5412     { "application id:",       68  },
5413     { "software version:",     96  },
5414  };
5415  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5416     { "number of tables:",
5417       "SELECT count(*) FROM %s WHERE type='table'" },
5418     { "number of indexes:",
5419       "SELECT count(*) FROM %s WHERE type='index'" },
5420     { "number of triggers:",
5421       "SELECT count(*) FROM %s WHERE type='trigger'" },
5422     { "number of views:",
5423       "SELECT count(*) FROM %s WHERE type='view'" },
5424     { "schema size:",
5425       "SELECT total(length(sql)) FROM %s" },
5426  };
5427  int i, rc;
5428  unsigned iDataVersion;
5429  char *zSchemaTab;
5430  char *zDb = nArg>=2 ? azArg[1] : "main";
5431  sqlite3_stmt *pStmt = 0;
5432  unsigned char aHdr[100];
5433  open_db(p, 0);
5434  if( p->db==0 ) return 1;
5435  rc = sqlite3_prepare_v2(p->db,
5436             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5437             -1, &pStmt, 0);
5438  if( rc ){
5439    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5440    sqlite3_finalize(pStmt);
5441    return 1;
5442  }
5443  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5444  if( sqlite3_step(pStmt)==SQLITE_ROW
5445   && sqlite3_column_bytes(pStmt,0)>100
5446  ){
5447    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5448    sqlite3_finalize(pStmt);
5449  }else{
5450    raw_printf(stderr, "unable to read database header\n");
5451    sqlite3_finalize(pStmt);
5452    return 1;
5453  }
5454  i = get2byteInt(aHdr+16);
5455  if( i==1 ) i = 65536;
5456  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5457  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5458  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5459  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5460  for(i=0; i<ArraySize(aField); i++){
5461    int ofst = aField[i].ofst;
5462    unsigned int val = get4byteInt(aHdr + ofst);
5463    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5464    switch( ofst ){
5465      case 56: {
5466        if( val==1 ) raw_printf(p->out, " (utf8)");
5467        if( val==2 ) raw_printf(p->out, " (utf16le)");
5468        if( val==3 ) raw_printf(p->out, " (utf16be)");
5469      }
5470    }
5471    raw_printf(p->out, "\n");
5472  }
5473  if( zDb==0 ){
5474    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5475  }else if( strcmp(zDb,"temp")==0 ){
5476    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5477  }else{
5478    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5479  }
5480  for(i=0; i<ArraySize(aQuery); i++){
5481    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5482    int val = db_int(p, zSql);
5483    sqlite3_free(zSql);
5484    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5485  }
5486  sqlite3_free(zSchemaTab);
5487  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5488  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5489  return 0;
5490}
5491
5492/*
5493** Print the current sqlite3_errmsg() value to stderr and return 1.
5494*/
5495static int shellDatabaseError(sqlite3 *db){
5496  const char *zErr = sqlite3_errmsg(db);
5497  utf8_printf(stderr, "Error: %s\n", zErr);
5498  return 1;
5499}
5500
5501/*
5502** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5503** if they match and FALSE (0) if they do not match.
5504**
5505** Globbing rules:
5506**
5507**      '*'       Matches any sequence of zero or more characters.
5508**
5509**      '?'       Matches exactly one character.
5510**
5511**     [...]      Matches one character from the enclosed list of
5512**                characters.
5513**
5514**     [^...]     Matches one character not in the enclosed list.
5515**
5516**      '#'       Matches any sequence of one or more digits with an
5517**                optional + or - sign in front
5518**
5519**      ' '       Any span of whitespace matches any other span of
5520**                whitespace.
5521**
5522** Extra whitespace at the end of z[] is ignored.
5523*/
5524static int testcase_glob(const char *zGlob, const char *z){
5525  int c, c2;
5526  int invert;
5527  int seen;
5528
5529  while( (c = (*(zGlob++)))!=0 ){
5530    if( IsSpace(c) ){
5531      if( !IsSpace(*z) ) return 0;
5532      while( IsSpace(*zGlob) ) zGlob++;
5533      while( IsSpace(*z) ) z++;
5534    }else if( c=='*' ){
5535      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5536        if( c=='?' && (*(z++))==0 ) return 0;
5537      }
5538      if( c==0 ){
5539        return 1;
5540      }else if( c=='[' ){
5541        while( *z && testcase_glob(zGlob-1,z)==0 ){
5542          z++;
5543        }
5544        return (*z)!=0;
5545      }
5546      while( (c2 = (*(z++)))!=0 ){
5547        while( c2!=c ){
5548          c2 = *(z++);
5549          if( c2==0 ) return 0;
5550        }
5551        if( testcase_glob(zGlob,z) ) return 1;
5552      }
5553      return 0;
5554    }else if( c=='?' ){
5555      if( (*(z++))==0 ) return 0;
5556    }else if( c=='[' ){
5557      int prior_c = 0;
5558      seen = 0;
5559      invert = 0;
5560      c = *(z++);
5561      if( c==0 ) return 0;
5562      c2 = *(zGlob++);
5563      if( c2=='^' ){
5564        invert = 1;
5565        c2 = *(zGlob++);
5566      }
5567      if( c2==']' ){
5568        if( c==']' ) seen = 1;
5569        c2 = *(zGlob++);
5570      }
5571      while( c2 && c2!=']' ){
5572        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5573          c2 = *(zGlob++);
5574          if( c>=prior_c && c<=c2 ) seen = 1;
5575          prior_c = 0;
5576        }else{
5577          if( c==c2 ){
5578            seen = 1;
5579          }
5580          prior_c = c2;
5581        }
5582        c2 = *(zGlob++);
5583      }
5584      if( c2==0 || (seen ^ invert)==0 ) return 0;
5585    }else if( c=='#' ){
5586      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5587      if( !IsDigit(z[0]) ) return 0;
5588      z++;
5589      while( IsDigit(z[0]) ){ z++; }
5590    }else{
5591      if( c!=(*(z++)) ) return 0;
5592    }
5593  }
5594  while( IsSpace(*z) ){ z++; }
5595  return *z==0;
5596}
5597
5598
5599/*
5600** Compare the string as a command-line option with either one or two
5601** initial "-" characters.
5602*/
5603static int optionMatch(const char *zStr, const char *zOpt){
5604  if( zStr[0]!='-' ) return 0;
5605  zStr++;
5606  if( zStr[0]=='-' ) zStr++;
5607  return strcmp(zStr, zOpt)==0;
5608}
5609
5610/*
5611** Delete a file.
5612*/
5613int shellDeleteFile(const char *zFilename){
5614  int rc;
5615#ifdef _WIN32
5616  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5617  rc = _wunlink(z);
5618  sqlite3_free(z);
5619#else
5620  rc = unlink(zFilename);
5621#endif
5622  return rc;
5623}
5624
5625/*
5626** Try to delete the temporary file (if there is one) and free the
5627** memory used to hold the name of the temp file.
5628*/
5629static void clearTempFile(ShellState *p){
5630  if( p->zTempFile==0 ) return;
5631  if( p->doXdgOpen ) return;
5632  if( shellDeleteFile(p->zTempFile) ) return;
5633  sqlite3_free(p->zTempFile);
5634  p->zTempFile = 0;
5635}
5636
5637/*
5638** Create a new temp file name with the given suffix.
5639*/
5640static void newTempFile(ShellState *p, const char *zSuffix){
5641  clearTempFile(p);
5642  sqlite3_free(p->zTempFile);
5643  p->zTempFile = 0;
5644  if( p->db ){
5645    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5646  }
5647  if( p->zTempFile==0 ){
5648    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5649    ** will not work and we will need to fallback to guessing */
5650    char *zTemp;
5651    sqlite3_uint64 r;
5652    sqlite3_randomness(sizeof(r), &r);
5653    zTemp = getenv("TEMP");
5654    if( zTemp==0 ) zTemp = getenv("TMP");
5655    if( zTemp==0 ){
5656#ifdef _WIN32
5657      zTemp = "\\tmp";
5658#else
5659      zTemp = "/tmp";
5660#endif
5661    }
5662    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5663  }else{
5664    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5665  }
5666  if( p->zTempFile==0 ){
5667    raw_printf(stderr, "out of memory\n");
5668    exit(1);
5669  }
5670}
5671
5672
5673/*
5674** The implementation of SQL scalar function fkey_collate_clause(), used
5675** by the ".lint fkey-indexes" command. This scalar function is always
5676** called with four arguments - the parent table name, the parent column name,
5677** the child table name and the child column name.
5678**
5679**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5680**
5681** If either of the named tables or columns do not exist, this function
5682** returns an empty string. An empty string is also returned if both tables
5683** and columns exist but have the same default collation sequence. Or,
5684** if both exist but the default collation sequences are different, this
5685** function returns the string " COLLATE <parent-collation>", where
5686** <parent-collation> is the default collation sequence of the parent column.
5687*/
5688static void shellFkeyCollateClause(
5689  sqlite3_context *pCtx,
5690  int nVal,
5691  sqlite3_value **apVal
5692){
5693  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5694  const char *zParent;
5695  const char *zParentCol;
5696  const char *zParentSeq;
5697  const char *zChild;
5698  const char *zChildCol;
5699  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5700  int rc;
5701
5702  assert( nVal==4 );
5703  zParent = (const char*)sqlite3_value_text(apVal[0]);
5704  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5705  zChild = (const char*)sqlite3_value_text(apVal[2]);
5706  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5707
5708  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5709  rc = sqlite3_table_column_metadata(
5710      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5711  );
5712  if( rc==SQLITE_OK ){
5713    rc = sqlite3_table_column_metadata(
5714        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5715    );
5716  }
5717
5718  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5719    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5720    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5721    sqlite3_free(z);
5722  }
5723}
5724
5725
5726/*
5727** The implementation of dot-command ".lint fkey-indexes".
5728*/
5729static int lintFkeyIndexes(
5730  ShellState *pState,             /* Current shell tool state */
5731  char **azArg,                   /* Array of arguments passed to dot command */
5732  int nArg                        /* Number of entries in azArg[] */
5733){
5734  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5735  FILE *out = pState->out;        /* Stream to write non-error output to */
5736  int bVerbose = 0;               /* If -verbose is present */
5737  int bGroupByParent = 0;         /* If -groupbyparent is present */
5738  int i;                          /* To iterate through azArg[] */
5739  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5740  int rc;                         /* Return code */
5741  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5742
5743  /*
5744  ** This SELECT statement returns one row for each foreign key constraint
5745  ** in the schema of the main database. The column values are:
5746  **
5747  ** 0. The text of an SQL statement similar to:
5748  **
5749  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5750  **
5751  **    This SELECT is similar to the one that the foreign keys implementation
5752  **    needs to run internally on child tables. If there is an index that can
5753  **    be used to optimize this query, then it can also be used by the FK
5754  **    implementation to optimize DELETE or UPDATE statements on the parent
5755  **    table.
5756  **
5757  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5758  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5759  **    contains an index that can be used to optimize the query.
5760  **
5761  ** 2. Human readable text that describes the child table and columns. e.g.
5762  **
5763  **       "child_table(child_key1, child_key2)"
5764  **
5765  ** 3. Human readable text that describes the parent table and columns. e.g.
5766  **
5767  **       "parent_table(parent_key1, parent_key2)"
5768  **
5769  ** 4. A full CREATE INDEX statement for an index that could be used to
5770  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5771  **
5772  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5773  **
5774  ** 5. The name of the parent table.
5775  **
5776  ** These six values are used by the C logic below to generate the report.
5777  */
5778  const char *zSql =
5779  "SELECT "
5780    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5781    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5782    "  || fkey_collate_clause("
5783    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5784    ", "
5785    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
5786    "  || group_concat('*=?', ' AND ') || ')'"
5787    ", "
5788    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5789    ", "
5790    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5791    ", "
5792    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5793    "  || ' ON ' || quote(s.name) || '('"
5794    "  || group_concat(quote(f.[from]) ||"
5795    "        fkey_collate_clause("
5796    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5797    "  || ');'"
5798    ", "
5799    "     f.[table] "
5800    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5801    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5802    "GROUP BY s.name, f.id "
5803    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5804  ;
5805  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
5806
5807  for(i=2; i<nArg; i++){
5808    int n = strlen30(azArg[i]);
5809    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5810      bVerbose = 1;
5811    }
5812    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5813      bGroupByParent = 1;
5814      zIndent = "    ";
5815    }
5816    else{
5817      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5818          azArg[0], azArg[1]
5819      );
5820      return SQLITE_ERROR;
5821    }
5822  }
5823
5824  /* Register the fkey_collate_clause() SQL function */
5825  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5826      0, shellFkeyCollateClause, 0, 0
5827  );
5828
5829
5830  if( rc==SQLITE_OK ){
5831    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5832  }
5833  if( rc==SQLITE_OK ){
5834    sqlite3_bind_int(pSql, 1, bGroupByParent);
5835  }
5836
5837  if( rc==SQLITE_OK ){
5838    int rc2;
5839    char *zPrev = 0;
5840    while( SQLITE_ROW==sqlite3_step(pSql) ){
5841      int res = -1;
5842      sqlite3_stmt *pExplain = 0;
5843      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5844      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5845      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5846      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5847      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5848      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5849
5850      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5851      if( rc!=SQLITE_OK ) break;
5852      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5853        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5854        res = (
5855              0==sqlite3_strglob(zGlob, zPlan)
5856           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5857        );
5858      }
5859      rc = sqlite3_finalize(pExplain);
5860      if( rc!=SQLITE_OK ) break;
5861
5862      if( res<0 ){
5863        raw_printf(stderr, "Error: internal error");
5864        break;
5865      }else{
5866        if( bGroupByParent
5867        && (bVerbose || res==0)
5868        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5869        ){
5870          raw_printf(out, "-- Parent table %s\n", zParent);
5871          sqlite3_free(zPrev);
5872          zPrev = sqlite3_mprintf("%s", zParent);
5873        }
5874
5875        if( res==0 ){
5876          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5877        }else if( bVerbose ){
5878          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5879              zIndent, zFrom, zTarget
5880          );
5881        }
5882      }
5883    }
5884    sqlite3_free(zPrev);
5885
5886    if( rc!=SQLITE_OK ){
5887      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5888    }
5889
5890    rc2 = sqlite3_finalize(pSql);
5891    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5892      rc = rc2;
5893      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5894    }
5895  }else{
5896    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5897  }
5898
5899  return rc;
5900}
5901
5902/*
5903** Implementation of ".lint" dot command.
5904*/
5905static int lintDotCommand(
5906  ShellState *pState,             /* Current shell tool state */
5907  char **azArg,                   /* Array of arguments passed to dot command */
5908  int nArg                        /* Number of entries in azArg[] */
5909){
5910  int n;
5911  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5912  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5913  return lintFkeyIndexes(pState, azArg, nArg);
5914
5915 usage:
5916  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5917  raw_printf(stderr, "Where sub-commands are:\n");
5918  raw_printf(stderr, "    fkey-indexes\n");
5919  return SQLITE_ERROR;
5920}
5921
5922#if !defined SQLITE_OMIT_VIRTUALTABLE
5923static void shellPrepare(
5924  sqlite3 *db,
5925  int *pRc,
5926  const char *zSql,
5927  sqlite3_stmt **ppStmt
5928){
5929  *ppStmt = 0;
5930  if( *pRc==SQLITE_OK ){
5931    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5932    if( rc!=SQLITE_OK ){
5933      raw_printf(stderr, "sql error: %s (%d)\n",
5934          sqlite3_errmsg(db), sqlite3_errcode(db)
5935      );
5936      *pRc = rc;
5937    }
5938  }
5939}
5940
5941/*
5942** Create a prepared statement using printf-style arguments for the SQL.
5943**
5944** This routine is could be marked "static".  But it is not always used,
5945** depending on compile-time options.  By omitting the "static", we avoid
5946** nuisance compiler warnings about "defined but not used".
5947*/
5948void shellPreparePrintf(
5949  sqlite3 *db,
5950  int *pRc,
5951  sqlite3_stmt **ppStmt,
5952  const char *zFmt,
5953  ...
5954){
5955  *ppStmt = 0;
5956  if( *pRc==SQLITE_OK ){
5957    va_list ap;
5958    char *z;
5959    va_start(ap, zFmt);
5960    z = sqlite3_vmprintf(zFmt, ap);
5961    va_end(ap);
5962    if( z==0 ){
5963      *pRc = SQLITE_NOMEM;
5964    }else{
5965      shellPrepare(db, pRc, z, ppStmt);
5966      sqlite3_free(z);
5967    }
5968  }
5969}
5970
5971/* Finalize the prepared statement created using shellPreparePrintf().
5972**
5973** This routine is could be marked "static".  But it is not always used,
5974** depending on compile-time options.  By omitting the "static", we avoid
5975** nuisance compiler warnings about "defined but not used".
5976*/
5977void shellFinalize(
5978  int *pRc,
5979  sqlite3_stmt *pStmt
5980){
5981  if( pStmt ){
5982    sqlite3 *db = sqlite3_db_handle(pStmt);
5983    int rc = sqlite3_finalize(pStmt);
5984    if( *pRc==SQLITE_OK ){
5985      if( rc!=SQLITE_OK ){
5986        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5987      }
5988      *pRc = rc;
5989    }
5990  }
5991}
5992
5993/* Reset the prepared statement created using shellPreparePrintf().
5994**
5995** This routine is could be marked "static".  But it is not always used,
5996** depending on compile-time options.  By omitting the "static", we avoid
5997** nuisance compiler warnings about "defined but not used".
5998*/
5999void shellReset(
6000  int *pRc,
6001  sqlite3_stmt *pStmt
6002){
6003  int rc = sqlite3_reset(pStmt);
6004  if( *pRc==SQLITE_OK ){
6005    if( rc!=SQLITE_OK ){
6006      sqlite3 *db = sqlite3_db_handle(pStmt);
6007      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6008    }
6009    *pRc = rc;
6010  }
6011}
6012#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6013
6014#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6015/******************************************************************************
6016** The ".archive" or ".ar" command.
6017*/
6018/*
6019** Structure representing a single ".ar" command.
6020*/
6021typedef struct ArCommand ArCommand;
6022struct ArCommand {
6023  u8 eCmd;                        /* An AR_CMD_* value */
6024  u8 bVerbose;                    /* True if --verbose */
6025  u8 bZip;                        /* True if the archive is a ZIP */
6026  u8 bDryRun;                     /* True if --dry-run */
6027  u8 bAppend;                     /* True if --append */
6028  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6029  int nArg;                       /* Number of command arguments */
6030  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6031  const char *zFile;              /* --file argument, or NULL */
6032  const char *zDir;               /* --directory argument, or NULL */
6033  char **azArg;                   /* Array of command arguments */
6034  ShellState *p;                  /* Shell state */
6035  sqlite3 *db;                    /* Database containing the archive */
6036};
6037
6038/*
6039** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6040*/
6041static int arUsage(FILE *f){
6042  showHelp(f,"archive");
6043  return SQLITE_ERROR;
6044}
6045
6046/*
6047** Print an error message for the .ar command to stderr and return
6048** SQLITE_ERROR.
6049*/
6050static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6051  va_list ap;
6052  char *z;
6053  va_start(ap, zFmt);
6054  z = sqlite3_vmprintf(zFmt, ap);
6055  va_end(ap);
6056  utf8_printf(stderr, "Error: %s\n", z);
6057  if( pAr->fromCmdLine ){
6058    utf8_printf(stderr, "Use \"-A\" for more help\n");
6059  }else{
6060    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6061  }
6062  sqlite3_free(z);
6063  return SQLITE_ERROR;
6064}
6065
6066/*
6067** Values for ArCommand.eCmd.
6068*/
6069#define AR_CMD_CREATE       1
6070#define AR_CMD_UPDATE       2
6071#define AR_CMD_INSERT       3
6072#define AR_CMD_EXTRACT      4
6073#define AR_CMD_LIST         5
6074#define AR_CMD_HELP         6
6075
6076/*
6077** Other (non-command) switches.
6078*/
6079#define AR_SWITCH_VERBOSE     7
6080#define AR_SWITCH_FILE        8
6081#define AR_SWITCH_DIRECTORY   9
6082#define AR_SWITCH_APPEND     10
6083#define AR_SWITCH_DRYRUN     11
6084
6085static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6086  switch( eSwitch ){
6087    case AR_CMD_CREATE:
6088    case AR_CMD_EXTRACT:
6089    case AR_CMD_LIST:
6090    case AR_CMD_UPDATE:
6091    case AR_CMD_INSERT:
6092    case AR_CMD_HELP:
6093      if( pAr->eCmd ){
6094        return arErrorMsg(pAr, "multiple command options");
6095      }
6096      pAr->eCmd = eSwitch;
6097      break;
6098
6099    case AR_SWITCH_DRYRUN:
6100      pAr->bDryRun = 1;
6101      break;
6102    case AR_SWITCH_VERBOSE:
6103      pAr->bVerbose = 1;
6104      break;
6105    case AR_SWITCH_APPEND:
6106      pAr->bAppend = 1;
6107      /* Fall thru into --file */
6108    case AR_SWITCH_FILE:
6109      pAr->zFile = zArg;
6110      break;
6111    case AR_SWITCH_DIRECTORY:
6112      pAr->zDir = zArg;
6113      break;
6114  }
6115
6116  return SQLITE_OK;
6117}
6118
6119/*
6120** Parse the command line for an ".ar" command. The results are written into
6121** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6122** successfully, otherwise an error message is written to stderr and
6123** SQLITE_ERROR returned.
6124*/
6125static int arParseCommand(
6126  char **azArg,                   /* Array of arguments passed to dot command */
6127  int nArg,                       /* Number of entries in azArg[] */
6128  ArCommand *pAr                  /* Populate this object */
6129){
6130  struct ArSwitch {
6131    const char *zLong;
6132    char cShort;
6133    u8 eSwitch;
6134    u8 bArg;
6135  } aSwitch[] = {
6136    { "create",    'c', AR_CMD_CREATE,       0 },
6137    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6138    { "insert",    'i', AR_CMD_INSERT,       0 },
6139    { "list",      't', AR_CMD_LIST,         0 },
6140    { "update",    'u', AR_CMD_UPDATE,       0 },
6141    { "help",      'h', AR_CMD_HELP,         0 },
6142    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6143    { "file",      'f', AR_SWITCH_FILE,      1 },
6144    { "append",    'a', AR_SWITCH_APPEND,    1 },
6145    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6146    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6147  };
6148  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6149  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6150
6151  if( nArg<=1 ){
6152    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6153    return arUsage(stderr);
6154  }else{
6155    char *z = azArg[1];
6156    if( z[0]!='-' ){
6157      /* Traditional style [tar] invocation */
6158      int i;
6159      int iArg = 2;
6160      for(i=0; z[i]; i++){
6161        const char *zArg = 0;
6162        struct ArSwitch *pOpt;
6163        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6164          if( z[i]==pOpt->cShort ) break;
6165        }
6166        if( pOpt==pEnd ){
6167          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6168        }
6169        if( pOpt->bArg ){
6170          if( iArg>=nArg ){
6171            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6172          }
6173          zArg = azArg[iArg++];
6174        }
6175        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6176      }
6177      pAr->nArg = nArg-iArg;
6178      if( pAr->nArg>0 ){
6179        pAr->azArg = &azArg[iArg];
6180      }
6181    }else{
6182      /* Non-traditional invocation */
6183      int iArg;
6184      for(iArg=1; iArg<nArg; iArg++){
6185        int n;
6186        z = azArg[iArg];
6187        if( z[0]!='-' ){
6188          /* All remaining command line words are command arguments. */
6189          pAr->azArg = &azArg[iArg];
6190          pAr->nArg = nArg-iArg;
6191          break;
6192        }
6193        n = strlen30(z);
6194
6195        if( z[1]!='-' ){
6196          int i;
6197          /* One or more short options */
6198          for(i=1; i<n; i++){
6199            const char *zArg = 0;
6200            struct ArSwitch *pOpt;
6201            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6202              if( z[i]==pOpt->cShort ) break;
6203            }
6204            if( pOpt==pEnd ){
6205              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6206            }
6207            if( pOpt->bArg ){
6208              if( i<(n-1) ){
6209                zArg = &z[i+1];
6210                i = n;
6211              }else{
6212                if( iArg>=(nArg-1) ){
6213                  return arErrorMsg(pAr, "option requires an argument: %c",
6214                                    z[i]);
6215                }
6216                zArg = azArg[++iArg];
6217              }
6218            }
6219            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6220          }
6221        }else if( z[2]=='\0' ){
6222          /* A -- option, indicating that all remaining command line words
6223          ** are command arguments.  */
6224          pAr->azArg = &azArg[iArg+1];
6225          pAr->nArg = nArg-iArg-1;
6226          break;
6227        }else{
6228          /* A long option */
6229          const char *zArg = 0;             /* Argument for option, if any */
6230          struct ArSwitch *pMatch = 0;      /* Matching option */
6231          struct ArSwitch *pOpt;            /* Iterator */
6232          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6233            const char *zLong = pOpt->zLong;
6234            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6235              if( pMatch ){
6236                return arErrorMsg(pAr, "ambiguous option: %s",z);
6237              }else{
6238                pMatch = pOpt;
6239              }
6240            }
6241          }
6242
6243          if( pMatch==0 ){
6244            return arErrorMsg(pAr, "unrecognized option: %s", z);
6245          }
6246          if( pMatch->bArg ){
6247            if( iArg>=(nArg-1) ){
6248              return arErrorMsg(pAr, "option requires an argument: %s", z);
6249            }
6250            zArg = azArg[++iArg];
6251          }
6252          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6253        }
6254      }
6255    }
6256  }
6257
6258  return SQLITE_OK;
6259}
6260
6261/*
6262** This function assumes that all arguments within the ArCommand.azArg[]
6263** array refer to archive members, as for the --extract or --list commands.
6264** It checks that each of them are present. If any specified file is not
6265** present in the archive, an error is printed to stderr and an error
6266** code returned. Otherwise, if all specified arguments are present in
6267** the archive, SQLITE_OK is returned.
6268**
6269** This function strips any trailing '/' characters from each argument.
6270** This is consistent with the way the [tar] command seems to work on
6271** Linux.
6272*/
6273static int arCheckEntries(ArCommand *pAr){
6274  int rc = SQLITE_OK;
6275  if( pAr->nArg ){
6276    int i, j;
6277    sqlite3_stmt *pTest = 0;
6278
6279    shellPreparePrintf(pAr->db, &rc, &pTest,
6280        "SELECT name FROM %s WHERE name=$name",
6281        pAr->zSrcTable
6282    );
6283    j = sqlite3_bind_parameter_index(pTest, "$name");
6284    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6285      char *z = pAr->azArg[i];
6286      int n = strlen30(z);
6287      int bOk = 0;
6288      while( n>0 && z[n-1]=='/' ) n--;
6289      z[n] = '\0';
6290      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6291      if( SQLITE_ROW==sqlite3_step(pTest) ){
6292        bOk = 1;
6293      }
6294      shellReset(&rc, pTest);
6295      if( rc==SQLITE_OK && bOk==0 ){
6296        utf8_printf(stderr, "not found in archive: %s\n", z);
6297        rc = SQLITE_ERROR;
6298      }
6299    }
6300    shellFinalize(&rc, pTest);
6301  }
6302  return rc;
6303}
6304
6305/*
6306** Format a WHERE clause that can be used against the "sqlar" table to
6307** identify all archive members that match the command arguments held
6308** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6309** The caller is responsible for eventually calling sqlite3_free() on
6310** any non-NULL (*pzWhere) value.
6311*/
6312static void arWhereClause(
6313  int *pRc,
6314  ArCommand *pAr,
6315  char **pzWhere                  /* OUT: New WHERE clause */
6316){
6317  char *zWhere = 0;
6318  if( *pRc==SQLITE_OK ){
6319    if( pAr->nArg==0 ){
6320      zWhere = sqlite3_mprintf("1");
6321    }else{
6322      int i;
6323      const char *zSep = "";
6324      for(i=0; i<pAr->nArg; i++){
6325        const char *z = pAr->azArg[i];
6326        zWhere = sqlite3_mprintf(
6327          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6328          zWhere, zSep, z, strlen30(z)+1, z
6329        );
6330        if( zWhere==0 ){
6331          *pRc = SQLITE_NOMEM;
6332          break;
6333        }
6334        zSep = " OR ";
6335      }
6336    }
6337  }
6338  *pzWhere = zWhere;
6339}
6340
6341/*
6342** Implementation of .ar "lisT" command.
6343*/
6344static int arListCommand(ArCommand *pAr){
6345  const char *zSql = "SELECT %s FROM %s WHERE %s";
6346  const char *azCols[] = {
6347    "name",
6348    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6349  };
6350
6351  char *zWhere = 0;
6352  sqlite3_stmt *pSql = 0;
6353  int rc;
6354
6355  rc = arCheckEntries(pAr);
6356  arWhereClause(&rc, pAr, &zWhere);
6357
6358  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6359                     pAr->zSrcTable, zWhere);
6360  if( pAr->bDryRun ){
6361    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6362  }else{
6363    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6364      if( pAr->bVerbose ){
6365        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6366            sqlite3_column_text(pSql, 0),
6367            sqlite3_column_int(pSql, 1),
6368            sqlite3_column_text(pSql, 2),
6369            sqlite3_column_text(pSql, 3)
6370        );
6371      }else{
6372        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6373      }
6374    }
6375  }
6376  shellFinalize(&rc, pSql);
6377  sqlite3_free(zWhere);
6378  return rc;
6379}
6380
6381
6382/*
6383** Implementation of .ar "eXtract" command.
6384*/
6385static int arExtractCommand(ArCommand *pAr){
6386  const char *zSql1 =
6387    "SELECT "
6388    " ($dir || name),"
6389    " writefile(($dir || name), %s, mode, mtime) "
6390    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6391    " AND name NOT GLOB '*..[/\\]*'";
6392
6393  const char *azExtraArg[] = {
6394    "sqlar_uncompress(data, sz)",
6395    "data"
6396  };
6397
6398  sqlite3_stmt *pSql = 0;
6399  int rc = SQLITE_OK;
6400  char *zDir = 0;
6401  char *zWhere = 0;
6402  int i, j;
6403
6404  /* If arguments are specified, check that they actually exist within
6405  ** the archive before proceeding. And formulate a WHERE clause to
6406  ** match them.  */
6407  rc = arCheckEntries(pAr);
6408  arWhereClause(&rc, pAr, &zWhere);
6409
6410  if( rc==SQLITE_OK ){
6411    if( pAr->zDir ){
6412      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6413    }else{
6414      zDir = sqlite3_mprintf("");
6415    }
6416    if( zDir==0 ) rc = SQLITE_NOMEM;
6417  }
6418
6419  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6420      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6421  );
6422
6423  if( rc==SQLITE_OK ){
6424    j = sqlite3_bind_parameter_index(pSql, "$dir");
6425    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6426
6427    /* Run the SELECT statement twice. The first time, writefile() is called
6428    ** for all archive members that should be extracted. The second time,
6429    ** only for the directories. This is because the timestamps for
6430    ** extracted directories must be reset after they are populated (as
6431    ** populating them changes the timestamp).  */
6432    for(i=0; i<2; i++){
6433      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6434      sqlite3_bind_int(pSql, j, i);
6435      if( pAr->bDryRun ){
6436        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6437      }else{
6438        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6439          if( i==0 && pAr->bVerbose ){
6440            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6441          }
6442        }
6443      }
6444      shellReset(&rc, pSql);
6445    }
6446    shellFinalize(&rc, pSql);
6447  }
6448
6449  sqlite3_free(zDir);
6450  sqlite3_free(zWhere);
6451  return rc;
6452}
6453
6454/*
6455** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6456*/
6457static int arExecSql(ArCommand *pAr, const char *zSql){
6458  int rc;
6459  if( pAr->bDryRun ){
6460    utf8_printf(pAr->p->out, "%s\n", zSql);
6461    rc = SQLITE_OK;
6462  }else{
6463    char *zErr = 0;
6464    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6465    if( zErr ){
6466      utf8_printf(stdout, "ERROR: %s\n", zErr);
6467      sqlite3_free(zErr);
6468    }
6469  }
6470  return rc;
6471}
6472
6473
6474/*
6475** Implementation of .ar "create", "insert", and "update" commands.
6476**
6477**     create    ->     Create a new SQL archive
6478**     insert    ->     Insert or reinsert all files listed
6479**     update    ->     Insert files that have changed or that were not
6480**                      previously in the archive
6481**
6482** Create the "sqlar" table in the database if it does not already exist.
6483** Then add each file in the azFile[] array to the archive. Directories
6484** are added recursively. If argument bVerbose is non-zero, a message is
6485** printed on stdout for each file archived.
6486**
6487** The create command is the same as update, except that it drops
6488** any existing "sqlar" table before beginning.  The "insert" command
6489** always overwrites every file named on the command-line, where as
6490** "update" only overwrites if the size or mtime or mode has changed.
6491*/
6492static int arCreateOrUpdateCommand(
6493  ArCommand *pAr,                 /* Command arguments and options */
6494  int bUpdate,                    /* true for a --create. */
6495  int bOnlyIfChanged              /* Only update if file has changed */
6496){
6497  const char *zCreate =
6498      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6499      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6500      "  mode INT,               -- access permissions\n"
6501      "  mtime INT,              -- last modification time\n"
6502      "  sz INT,                 -- original file size\n"
6503      "  data BLOB               -- compressed content\n"
6504      ")";
6505  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6506  const char *zInsertFmt[2] = {
6507     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6508     "  SELECT\n"
6509     "    %s,\n"
6510     "    mode,\n"
6511     "    mtime,\n"
6512     "    CASE substr(lsmode(mode),1,1)\n"
6513     "      WHEN '-' THEN length(data)\n"
6514     "      WHEN 'd' THEN 0\n"
6515     "      ELSE -1 END,\n"
6516     "    sqlar_compress(data)\n"
6517     "  FROM fsdir(%Q,%Q) AS disk\n"
6518     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6519     ,
6520     "REPLACE INTO %s(name,mode,mtime,data)\n"
6521     "  SELECT\n"
6522     "    %s,\n"
6523     "    mode,\n"
6524     "    mtime,\n"
6525     "    data\n"
6526     "  FROM fsdir(%Q,%Q) AS disk\n"
6527     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6528  };
6529  int i;                          /* For iterating through azFile[] */
6530  int rc;                         /* Return code */
6531  const char *zTab = 0;           /* SQL table into which to insert */
6532  char *zSql;
6533  char zTemp[50];
6534  char *zExists = 0;
6535
6536  arExecSql(pAr, "PRAGMA page_size=512");
6537  rc = arExecSql(pAr, "SAVEPOINT ar;");
6538  if( rc!=SQLITE_OK ) return rc;
6539  zTemp[0] = 0;
6540  if( pAr->bZip ){
6541    /* Initialize the zipfile virtual table, if necessary */
6542    if( pAr->zFile ){
6543      sqlite3_uint64 r;
6544      sqlite3_randomness(sizeof(r),&r);
6545      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6546      zTab = zTemp;
6547      zSql = sqlite3_mprintf(
6548         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6549         zTab, pAr->zFile
6550      );
6551      rc = arExecSql(pAr, zSql);
6552      sqlite3_free(zSql);
6553    }else{
6554      zTab = "zip";
6555    }
6556  }else{
6557    /* Initialize the table for an SQLAR */
6558    zTab = "sqlar";
6559    if( bUpdate==0 ){
6560      rc = arExecSql(pAr, zDrop);
6561      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6562    }
6563    rc = arExecSql(pAr, zCreate);
6564  }
6565  if( bOnlyIfChanged ){
6566    zExists = sqlite3_mprintf(
6567      " AND NOT EXISTS("
6568          "SELECT 1 FROM %s AS mem"
6569          " WHERE mem.name=disk.name"
6570          " AND mem.mtime=disk.mtime"
6571          " AND mem.mode=disk.mode)", zTab);
6572  }else{
6573    zExists = sqlite3_mprintf("");
6574  }
6575  if( zExists==0 ) rc = SQLITE_NOMEM;
6576  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6577    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6578        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6579        pAr->azArg[i], pAr->zDir, zExists);
6580    rc = arExecSql(pAr, zSql2);
6581    sqlite3_free(zSql2);
6582  }
6583end_ar_transaction:
6584  if( rc!=SQLITE_OK ){
6585    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6586  }else{
6587    rc = arExecSql(pAr, "RELEASE ar;");
6588    if( pAr->bZip && pAr->zFile ){
6589      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6590      arExecSql(pAr, zSql);
6591      sqlite3_free(zSql);
6592    }
6593  }
6594  sqlite3_free(zExists);
6595  return rc;
6596}
6597
6598/*
6599** Implementation of ".ar" dot command.
6600*/
6601static int arDotCommand(
6602  ShellState *pState,          /* Current shell tool state */
6603  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6604  char **azArg,                /* Array of arguments passed to dot command */
6605  int nArg                     /* Number of entries in azArg[] */
6606){
6607  ArCommand cmd;
6608  int rc;
6609  memset(&cmd, 0, sizeof(cmd));
6610  cmd.fromCmdLine = fromCmdLine;
6611  rc = arParseCommand(azArg, nArg, &cmd);
6612  if( rc==SQLITE_OK ){
6613    int eDbType = SHELL_OPEN_UNSPEC;
6614    cmd.p = pState;
6615    cmd.db = pState->db;
6616    if( cmd.zFile ){
6617      eDbType = deduceDatabaseType(cmd.zFile, 1);
6618    }else{
6619      eDbType = pState->openMode;
6620    }
6621    if( eDbType==SHELL_OPEN_ZIPFILE ){
6622      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6623        if( cmd.zFile==0 ){
6624          cmd.zSrcTable = sqlite3_mprintf("zip");
6625        }else{
6626          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6627        }
6628      }
6629      cmd.bZip = 1;
6630    }else if( cmd.zFile ){
6631      int flags;
6632      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6633      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6634           || cmd.eCmd==AR_CMD_UPDATE ){
6635        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6636      }else{
6637        flags = SQLITE_OPEN_READONLY;
6638      }
6639      cmd.db = 0;
6640      if( cmd.bDryRun ){
6641        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6642             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6643      }
6644      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6645             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6646      if( rc!=SQLITE_OK ){
6647        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6648            cmd.zFile, sqlite3_errmsg(cmd.db)
6649        );
6650        goto end_ar_command;
6651      }
6652      sqlite3_fileio_init(cmd.db, 0, 0);
6653      sqlite3_sqlar_init(cmd.db, 0, 0);
6654      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6655                              shellPutsFunc, 0, 0);
6656
6657    }
6658    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6659      if( cmd.eCmd!=AR_CMD_CREATE
6660       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6661      ){
6662        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6663        rc = SQLITE_ERROR;
6664        goto end_ar_command;
6665      }
6666      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6667    }
6668
6669    switch( cmd.eCmd ){
6670      case AR_CMD_CREATE:
6671        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6672        break;
6673
6674      case AR_CMD_EXTRACT:
6675        rc = arExtractCommand(&cmd);
6676        break;
6677
6678      case AR_CMD_LIST:
6679        rc = arListCommand(&cmd);
6680        break;
6681
6682      case AR_CMD_HELP:
6683        arUsage(pState->out);
6684        break;
6685
6686      case AR_CMD_INSERT:
6687        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6688        break;
6689
6690      default:
6691        assert( cmd.eCmd==AR_CMD_UPDATE );
6692        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6693        break;
6694    }
6695  }
6696end_ar_command:
6697  if( cmd.db!=pState->db ){
6698    close_db(cmd.db);
6699  }
6700  sqlite3_free(cmd.zSrcTable);
6701
6702  return rc;
6703}
6704/* End of the ".archive" or ".ar" command logic
6705*******************************************************************************/
6706#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6707
6708#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6709/*
6710** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6711** Otherwise, the SQL statement or statements in zSql are executed using
6712** database connection db and the error code written to *pRc before
6713** this function returns.
6714*/
6715static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6716  int rc = *pRc;
6717  if( rc==SQLITE_OK ){
6718    char *zErr = 0;
6719    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6720    if( rc!=SQLITE_OK ){
6721      raw_printf(stderr, "SQL error: %s\n", zErr);
6722    }
6723    sqlite3_free(zErr);
6724    *pRc = rc;
6725  }
6726}
6727
6728/*
6729** Like shellExec(), except that zFmt is a printf() style format string.
6730*/
6731static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6732  char *z = 0;
6733  if( *pRc==SQLITE_OK ){
6734    va_list ap;
6735    va_start(ap, zFmt);
6736    z = sqlite3_vmprintf(zFmt, ap);
6737    va_end(ap);
6738    if( z==0 ){
6739      *pRc = SQLITE_NOMEM;
6740    }else{
6741      shellExec(db, pRc, z);
6742    }
6743    sqlite3_free(z);
6744  }
6745}
6746
6747/*
6748** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6749** Otherwise, an attempt is made to allocate, zero and return a pointer
6750** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6751** to SQLITE_NOMEM and NULL returned.
6752*/
6753static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6754  void *pRet = 0;
6755  if( *pRc==SQLITE_OK ){
6756    pRet = sqlite3_malloc64(nByte);
6757    if( pRet==0 ){
6758      *pRc = SQLITE_NOMEM;
6759    }else{
6760      memset(pRet, 0, nByte);
6761    }
6762  }
6763  return pRet;
6764}
6765
6766/*
6767** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6768** Otherwise, zFmt is treated as a printf() style string. The result of
6769** formatting it along with any trailing arguments is written into a
6770** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6771** It is the responsibility of the caller to eventually free this buffer
6772** using a call to sqlite3_free().
6773**
6774** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6775** pointer returned.
6776*/
6777static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6778  char *z = 0;
6779  if( *pRc==SQLITE_OK ){
6780    va_list ap;
6781    va_start(ap, zFmt);
6782    z = sqlite3_vmprintf(zFmt, ap);
6783    va_end(ap);
6784    if( z==0 ){
6785      *pRc = SQLITE_NOMEM;
6786    }
6787  }
6788  return z;
6789}
6790
6791/*
6792** When running the ".recover" command, each output table, and the special
6793** orphaned row table if it is required, is represented by an instance
6794** of the following struct.
6795*/
6796typedef struct RecoverTable RecoverTable;
6797struct RecoverTable {
6798  char *zQuoted;                  /* Quoted version of table name */
6799  int nCol;                       /* Number of columns in table */
6800  char **azlCol;                  /* Array of column lists */
6801  int iPk;                        /* Index of IPK column */
6802};
6803
6804/*
6805** Free a RecoverTable object allocated by recoverFindTable() or
6806** recoverOrphanTable().
6807*/
6808static void recoverFreeTable(RecoverTable *pTab){
6809  if( pTab ){
6810    sqlite3_free(pTab->zQuoted);
6811    if( pTab->azlCol ){
6812      int i;
6813      for(i=0; i<=pTab->nCol; i++){
6814        sqlite3_free(pTab->azlCol[i]);
6815      }
6816      sqlite3_free(pTab->azlCol);
6817    }
6818    sqlite3_free(pTab);
6819  }
6820}
6821
6822/*
6823** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6824** Otherwise, it allocates and returns a RecoverTable object based on the
6825** final four arguments passed to this function. It is the responsibility
6826** of the caller to eventually free the returned object using
6827** recoverFreeTable().
6828*/
6829static RecoverTable *recoverNewTable(
6830  int *pRc,                       /* IN/OUT: Error code */
6831  const char *zName,              /* Name of table */
6832  const char *zSql,               /* CREATE TABLE statement */
6833  int bIntkey,
6834  int nCol
6835){
6836  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6837  int rc = *pRc;
6838  RecoverTable *pTab = 0;
6839
6840  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6841  if( rc==SQLITE_OK ){
6842    int nSqlCol = 0;
6843    int bSqlIntkey = 0;
6844    sqlite3_stmt *pStmt = 0;
6845
6846    rc = sqlite3_open("", &dbtmp);
6847    if( rc==SQLITE_OK ){
6848      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6849                              shellIdQuote, 0, 0);
6850    }
6851    if( rc==SQLITE_OK ){
6852      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6853    }
6854    if( rc==SQLITE_OK ){
6855      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6856      if( rc==SQLITE_ERROR ){
6857        rc = SQLITE_OK;
6858        goto finished;
6859      }
6860    }
6861    shellPreparePrintf(dbtmp, &rc, &pStmt,
6862        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6863    );
6864    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6865      nSqlCol = sqlite3_column_int(pStmt, 0);
6866    }
6867    shellFinalize(&rc, pStmt);
6868
6869    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6870      goto finished;
6871    }
6872
6873    shellPreparePrintf(dbtmp, &rc, &pStmt,
6874      "SELECT ("
6875      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6876      ") FROM sqlite_schema WHERE name = %Q", zName
6877    );
6878    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6879      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6880    }
6881    shellFinalize(&rc, pStmt);
6882
6883    if( bIntkey==bSqlIntkey ){
6884      int i;
6885      const char *zPk = "_rowid_";
6886      sqlite3_stmt *pPkFinder = 0;
6887
6888      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6889      ** set zPk to the name of the PK column, and pTab->iPk to the index
6890      ** of the column, where columns are 0-numbered from left to right.
6891      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6892      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6893      pTab->iPk = -2;
6894      if( bIntkey ){
6895        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6896          "SELECT cid, name FROM pragma_table_info(%Q) "
6897          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6898          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6899          , zName, zName
6900        );
6901        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6902          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6903          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6904        }
6905      }
6906
6907      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6908      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6909      pTab->nCol = nSqlCol;
6910
6911      if( bIntkey ){
6912        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6913      }else{
6914        pTab->azlCol[0] = shellMPrintf(&rc, "");
6915      }
6916      i = 1;
6917      shellPreparePrintf(dbtmp, &rc, &pStmt,
6918          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6919          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6920          "FROM pragma_table_info(%Q)",
6921          bIntkey ? ", " : "", pTab->iPk,
6922          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6923          zName
6924      );
6925      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6926        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6927        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6928        i++;
6929      }
6930      shellFinalize(&rc, pStmt);
6931
6932      shellFinalize(&rc, pPkFinder);
6933    }
6934  }
6935
6936 finished:
6937  sqlite3_close(dbtmp);
6938  *pRc = rc;
6939  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6940    recoverFreeTable(pTab);
6941    pTab = 0;
6942  }
6943  return pTab;
6944}
6945
6946/*
6947** This function is called to search the schema recovered from the
6948** sqlite_schema table of the (possibly) corrupt database as part
6949** of a ".recover" command. Specifically, for a table with root page
6950** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6951** table must be a WITHOUT ROWID table, or if non-zero, not one of
6952** those.
6953**
6954** If a table is found, a (RecoverTable*) object is returned. Or, if
6955** no such table is found, but bIntkey is false and iRoot is the
6956** root page of an index in the recovered schema, then (*pbNoop) is
6957** set to true and NULL returned. Or, if there is no such table or
6958** index, NULL is returned and (*pbNoop) set to 0, indicating that
6959** the caller should write data to the orphans table.
6960*/
6961static RecoverTable *recoverFindTable(
6962  ShellState *pState,             /* Shell state object */
6963  int *pRc,                       /* IN/OUT: Error code */
6964  int iRoot,                      /* Root page of table */
6965  int bIntkey,                    /* True for an intkey table */
6966  int nCol,                       /* Number of columns in table */
6967  int *pbNoop                     /* OUT: True if iRoot is root of index */
6968){
6969  sqlite3_stmt *pStmt = 0;
6970  RecoverTable *pRet = 0;
6971  int bNoop = 0;
6972  const char *zSql = 0;
6973  const char *zName = 0;
6974
6975  /* Search the recovered schema for an object with root page iRoot. */
6976  shellPreparePrintf(pState->db, pRc, &pStmt,
6977      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6978  );
6979  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6980    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6981    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6982      bNoop = 1;
6983      break;
6984    }
6985    if( sqlite3_stricmp(zType, "table")==0 ){
6986      zName = (const char*)sqlite3_column_text(pStmt, 1);
6987      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6988      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6989      break;
6990    }
6991  }
6992
6993  shellFinalize(pRc, pStmt);
6994  *pbNoop = bNoop;
6995  return pRet;
6996}
6997
6998/*
6999** Return a RecoverTable object representing the orphans table.
7000*/
7001static RecoverTable *recoverOrphanTable(
7002  ShellState *pState,             /* Shell state object */
7003  int *pRc,                       /* IN/OUT: Error code */
7004  const char *zLostAndFound,      /* Base name for orphans table */
7005  int nCol                        /* Number of user data columns */
7006){
7007  RecoverTable *pTab = 0;
7008  if( nCol>=0 && *pRc==SQLITE_OK ){
7009    int i;
7010
7011    /* This block determines the name of the orphan table. The prefered
7012    ** name is zLostAndFound. But if that clashes with another name
7013    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7014    ** and so on until a non-clashing name is found.  */
7015    int iTab = 0;
7016    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7017    sqlite3_stmt *pTest = 0;
7018    shellPrepare(pState->db, pRc,
7019        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7020    );
7021    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7022    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7023      shellReset(pRc, pTest);
7024      sqlite3_free(zTab);
7025      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7026      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7027    }
7028    shellFinalize(pRc, pTest);
7029
7030    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7031    if( pTab ){
7032      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7033      pTab->nCol = nCol;
7034      pTab->iPk = -2;
7035      if( nCol>0 ){
7036        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7037        if( pTab->azlCol ){
7038          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7039          for(i=nCol-1; i>=0; i--){
7040            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7041          }
7042        }
7043      }
7044
7045      if( *pRc!=SQLITE_OK ){
7046        recoverFreeTable(pTab);
7047        pTab = 0;
7048      }else{
7049        raw_printf(pState->out,
7050            "CREATE TABLE %s(rootpgno INTEGER, "
7051            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7052        );
7053        for(i=0; i<nCol; i++){
7054          raw_printf(pState->out, ", c%d", i);
7055        }
7056        raw_printf(pState->out, ");\n");
7057      }
7058    }
7059    sqlite3_free(zTab);
7060  }
7061  return pTab;
7062}
7063
7064/*
7065** This function is called to recover data from the database. A script
7066** to construct a new database containing all recovered data is output
7067** on stream pState->out.
7068*/
7069static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7070  int rc = SQLITE_OK;
7071  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7072  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7073  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7074  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7075  const char *zLostAndFound = "lost_and_found";
7076  int i;
7077  int nOrphan = -1;
7078  RecoverTable *pOrphan = 0;
7079
7080  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7081  int bRowids = 1;                /* 0 if --no-rowids */
7082  for(i=1; i<nArg; i++){
7083    char *z = azArg[i];
7084    int n;
7085    if( z[0]=='-' && z[1]=='-' ) z++;
7086    n = strlen30(z);
7087    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7088      bFreelist = 0;
7089    }else
7090    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7091      i++;
7092      zRecoveryDb = azArg[i];
7093    }else
7094    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7095      i++;
7096      zLostAndFound = azArg[i];
7097    }else
7098    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7099      bRowids = 0;
7100    }
7101    else{
7102      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7103      showHelp(pState->out, azArg[0]);
7104      return 1;
7105    }
7106  }
7107
7108  shellExecPrintf(pState->db, &rc,
7109    /* Attach an in-memory database named 'recovery'. Create an indexed
7110    ** cache of the sqlite_dbptr virtual table. */
7111    "PRAGMA writable_schema = on;"
7112    "ATTACH %Q AS recovery;"
7113    "DROP TABLE IF EXISTS recovery.dbptr;"
7114    "DROP TABLE IF EXISTS recovery.freelist;"
7115    "DROP TABLE IF EXISTS recovery.map;"
7116    "DROP TABLE IF EXISTS recovery.schema;"
7117    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7118  );
7119
7120  if( bFreelist ){
7121    shellExec(pState->db, &rc,
7122      "WITH trunk(pgno) AS ("
7123      "  SELECT shell_int32("
7124      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7125      "      WHERE x>0"
7126      "    UNION"
7127      "  SELECT shell_int32("
7128      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7129      "      FROM trunk WHERE x>0"
7130      "),"
7131      "freelist(data, n, freepgno) AS ("
7132      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7133      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7134      "    UNION ALL"
7135      "  SELECT data, n-1, shell_int32(data, 2+n) "
7136      "      FROM freelist WHERE n>=0"
7137      ")"
7138      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7139    );
7140  }
7141
7142  /* If this is an auto-vacuum database, add all pointer-map pages to
7143  ** the freelist table. Do this regardless of whether or not
7144  ** --freelist-corrupt was specified.  */
7145  shellExec(pState->db, &rc,
7146    "WITH ptrmap(pgno) AS ("
7147    "  SELECT 2 WHERE shell_int32("
7148    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7149    "  )"
7150    "    UNION ALL "
7151    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7152    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7153    ")"
7154    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7155  );
7156
7157  shellExec(pState->db, &rc,
7158    "CREATE TABLE recovery.dbptr("
7159    "      pgno, child, PRIMARY KEY(child, pgno)"
7160    ") WITHOUT ROWID;"
7161    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7162    "    SELECT * FROM sqlite_dbptr"
7163    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7164
7165    /* Delete any pointer to page 1. This ensures that page 1 is considered
7166    ** a root page, regardless of how corrupt the db is. */
7167    "DELETE FROM recovery.dbptr WHERE child = 1;"
7168
7169    /* Delete all pointers to any pages that have more than one pointer
7170    ** to them. Such pages will be treated as root pages when recovering
7171    ** data.  */
7172    "DELETE FROM recovery.dbptr WHERE child IN ("
7173    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7174    ");"
7175
7176    /* Create the "map" table that will (eventually) contain instructions
7177    ** for dealing with each page in the db that contains one or more
7178    ** records. */
7179    "CREATE TABLE recovery.map("
7180      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7181    ");"
7182
7183    /* Populate table [map]. If there are circular loops of pages in the
7184    ** database, the following adds all pages in such a loop to the map
7185    ** as individual root pages. This could be handled better.  */
7186    "WITH pages(i, maxlen) AS ("
7187    "  SELECT page_count, ("
7188    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7189    "  ) FROM pragma_page_count WHERE page_count>0"
7190    "    UNION ALL"
7191    "  SELECT i-1, ("
7192    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7193    "  ) FROM pages WHERE i>=2"
7194    ")"
7195    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7196    "  SELECT i, maxlen, NULL, ("
7197    "    WITH p(orig, pgno, parent) AS ("
7198    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7199    "        UNION "
7200    "      SELECT i, p.parent, "
7201    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7202    "    )"
7203    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7204    ") "
7205    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7206    "UPDATE recovery.map AS o SET intkey = ("
7207    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7208    ");"
7209
7210    /* Extract data from page 1 and any linked pages into table
7211    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7212    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7213    "INSERT INTO recovery.schema SELECT "
7214    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7215    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7216    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7217    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7218    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7219    "FROM sqlite_dbdata WHERE pgno IN ("
7220    "  SELECT pgno FROM recovery.map WHERE root=1"
7221    ")"
7222    "GROUP BY pgno, cell;"
7223    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7224  );
7225
7226  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7227  ** CREATE TABLE statements that extracted from the existing schema.  */
7228  if( rc==SQLITE_OK ){
7229    sqlite3_stmt *pStmt = 0;
7230    /* ".recover" might output content in an order which causes immediate
7231    ** foreign key constraints to be violated. So disable foreign-key
7232    ** constraint enforcement to prevent problems when running the output
7233    ** script. */
7234    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7235    raw_printf(pState->out, "BEGIN;\n");
7236    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7237    shellPrepare(pState->db, &rc,
7238        "SELECT sql FROM recovery.schema "
7239        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7240    );
7241    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7242      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7243      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7244          &zCreateTable[12]
7245      );
7246    }
7247    shellFinalize(&rc, pStmt);
7248  }
7249
7250  /* Figure out if an orphan table will be required. And if so, how many
7251  ** user columns it should contain */
7252  shellPrepare(pState->db, &rc,
7253      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7254      , &pLoop
7255  );
7256  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7257    nOrphan = sqlite3_column_int(pLoop, 0);
7258  }
7259  shellFinalize(&rc, pLoop);
7260  pLoop = 0;
7261
7262  shellPrepare(pState->db, &rc,
7263      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7264  );
7265
7266  shellPrepare(pState->db, &rc,
7267      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7268      "(case when (? AND field<0) then NULL else value end)"
7269      "), ', ')"
7270      ", min(field) "
7271      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7272      "GROUP BY cell", &pCells
7273  );
7274
7275  /* Loop through each root page. */
7276  shellPrepare(pState->db, &rc,
7277      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7278      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7279      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7280      ")", &pLoop
7281  );
7282  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7283    int iRoot = sqlite3_column_int(pLoop, 0);
7284    int bIntkey = sqlite3_column_int(pLoop, 1);
7285    int nCol = sqlite3_column_int(pLoop, 2);
7286    int bNoop = 0;
7287    RecoverTable *pTab;
7288
7289    assert( bIntkey==0 || bIntkey==1 );
7290    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7291    if( bNoop || rc ) continue;
7292    if( pTab==0 ){
7293      if( pOrphan==0 ){
7294        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7295      }
7296      pTab = pOrphan;
7297      if( pTab==0 ) break;
7298    }
7299
7300    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7301      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7302    }
7303    sqlite3_bind_int(pPages, 1, iRoot);
7304    if( bRowids==0 && pTab->iPk<0 ){
7305      sqlite3_bind_int(pCells, 1, 1);
7306    }else{
7307      sqlite3_bind_int(pCells, 1, 0);
7308    }
7309    sqlite3_bind_int(pCells, 3, pTab->iPk);
7310
7311    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7312      int iPgno = sqlite3_column_int(pPages, 0);
7313      sqlite3_bind_int(pCells, 2, iPgno);
7314      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7315        int nField = sqlite3_column_int(pCells, 0);
7316        int iMin = sqlite3_column_int(pCells, 2);
7317        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7318
7319        RecoverTable *pTab2 = pTab;
7320        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7321          if( pOrphan==0 ){
7322            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7323          }
7324          pTab2 = pOrphan;
7325          if( pTab2==0 ) break;
7326        }
7327
7328        nField = nField+1;
7329        if( pTab2==pOrphan ){
7330          raw_printf(pState->out,
7331              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7332              pTab2->zQuoted, iRoot, iPgno, nField,
7333              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7334          );
7335        }else{
7336          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7337              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7338          );
7339        }
7340      }
7341      shellReset(&rc, pCells);
7342    }
7343    shellReset(&rc, pPages);
7344    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7345  }
7346  shellFinalize(&rc, pLoop);
7347  shellFinalize(&rc, pPages);
7348  shellFinalize(&rc, pCells);
7349  recoverFreeTable(pOrphan);
7350
7351  /* The rest of the schema */
7352  if( rc==SQLITE_OK ){
7353    sqlite3_stmt *pStmt = 0;
7354    shellPrepare(pState->db, &rc,
7355        "SELECT sql, name FROM recovery.schema "
7356        "WHERE sql NOT LIKE 'create table%'", &pStmt
7357    );
7358    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7359      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7360      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7361        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7362        char *zPrint = shellMPrintf(&rc,
7363          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7364          zName, zName, zSql
7365        );
7366        raw_printf(pState->out, "%s;\n", zPrint);
7367        sqlite3_free(zPrint);
7368      }else{
7369        raw_printf(pState->out, "%s;\n", zSql);
7370      }
7371    }
7372    shellFinalize(&rc, pStmt);
7373  }
7374
7375  if( rc==SQLITE_OK ){
7376    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7377    raw_printf(pState->out, "COMMIT;\n");
7378  }
7379  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7380  return rc;
7381}
7382#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7383
7384
7385/*
7386** If an input line begins with "." then invoke this routine to
7387** process that line.
7388**
7389** Return 1 on error, 2 to exit, and 0 otherwise.
7390*/
7391static int do_meta_command(char *zLine, ShellState *p){
7392  int h = 1;
7393  int nArg = 0;
7394  int n, c;
7395  int rc = 0;
7396  char *azArg[52];
7397
7398#ifndef SQLITE_OMIT_VIRTUALTABLE
7399  if( p->expert.pExpert ){
7400    expertFinish(p, 1, 0);
7401  }
7402#endif
7403
7404  /* Parse the input line into tokens.
7405  */
7406  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7407    while( IsSpace(zLine[h]) ){ h++; }
7408    if( zLine[h]==0 ) break;
7409    if( zLine[h]=='\'' || zLine[h]=='"' ){
7410      int delim = zLine[h++];
7411      azArg[nArg++] = &zLine[h];
7412      while( zLine[h] && zLine[h]!=delim ){
7413        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7414        h++;
7415      }
7416      if( zLine[h]==delim ){
7417        zLine[h++] = 0;
7418      }
7419      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7420    }else{
7421      azArg[nArg++] = &zLine[h];
7422      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7423      if( zLine[h] ) zLine[h++] = 0;
7424      resolve_backslashes(azArg[nArg-1]);
7425    }
7426  }
7427  azArg[nArg] = 0;
7428
7429  /* Process the input line.
7430  */
7431  if( nArg==0 ) return 0; /* no tokens, no error */
7432  n = strlen30(azArg[0]);
7433  c = azArg[0][0];
7434  clearTempFile(p);
7435
7436#ifndef SQLITE_OMIT_AUTHORIZATION
7437  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7438    if( nArg!=2 ){
7439      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7440      rc = 1;
7441      goto meta_command_exit;
7442    }
7443    open_db(p, 0);
7444    if( booleanValue(azArg[1]) ){
7445      sqlite3_set_authorizer(p->db, shellAuth, p);
7446    }else{
7447      sqlite3_set_authorizer(p->db, 0, 0);
7448    }
7449  }else
7450#endif
7451
7452#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7453  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7454    open_db(p, 0);
7455    rc = arDotCommand(p, 0, azArg, nArg);
7456  }else
7457#endif
7458
7459  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7460   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7461  ){
7462    const char *zDestFile = 0;
7463    const char *zDb = 0;
7464    sqlite3 *pDest;
7465    sqlite3_backup *pBackup;
7466    int j;
7467    int bAsync = 0;
7468    const char *zVfs = 0;
7469    for(j=1; j<nArg; j++){
7470      const char *z = azArg[j];
7471      if( z[0]=='-' ){
7472        if( z[1]=='-' ) z++;
7473        if( strcmp(z, "-append")==0 ){
7474          zVfs = "apndvfs";
7475        }else
7476        if( strcmp(z, "-async")==0 ){
7477          bAsync = 1;
7478        }else
7479        {
7480          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7481          return 1;
7482        }
7483      }else if( zDestFile==0 ){
7484        zDestFile = azArg[j];
7485      }else if( zDb==0 ){
7486        zDb = zDestFile;
7487        zDestFile = azArg[j];
7488      }else{
7489        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7490        return 1;
7491      }
7492    }
7493    if( zDestFile==0 ){
7494      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7495      return 1;
7496    }
7497    if( zDb==0 ) zDb = "main";
7498    rc = sqlite3_open_v2(zDestFile, &pDest,
7499                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7500    if( rc!=SQLITE_OK ){
7501      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7502      close_db(pDest);
7503      return 1;
7504    }
7505    if( bAsync ){
7506      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7507                   0, 0, 0);
7508    }
7509    open_db(p, 0);
7510    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7511    if( pBackup==0 ){
7512      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7513      close_db(pDest);
7514      return 1;
7515    }
7516    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7517    sqlite3_backup_finish(pBackup);
7518    if( rc==SQLITE_DONE ){
7519      rc = 0;
7520    }else{
7521      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7522      rc = 1;
7523    }
7524    close_db(pDest);
7525  }else
7526
7527  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7528    if( nArg==2 ){
7529      bail_on_error = booleanValue(azArg[1]);
7530    }else{
7531      raw_printf(stderr, "Usage: .bail on|off\n");
7532      rc = 1;
7533    }
7534  }else
7535
7536  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7537    if( nArg==2 ){
7538      if( booleanValue(azArg[1]) ){
7539        setBinaryMode(p->out, 1);
7540      }else{
7541        setTextMode(p->out, 1);
7542      }
7543    }else{
7544      raw_printf(stderr, "Usage: .binary on|off\n");
7545      rc = 1;
7546    }
7547  }else
7548
7549  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7550    if( nArg==2 ){
7551#if defined(_WIN32) || defined(WIN32)
7552      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7553      rc = !SetCurrentDirectoryW(z);
7554      sqlite3_free(z);
7555#else
7556      rc = chdir(azArg[1]);
7557#endif
7558      if( rc ){
7559        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7560        rc = 1;
7561      }
7562    }else{
7563      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7564      rc = 1;
7565    }
7566  }else
7567
7568  /* The undocumented ".breakpoint" command causes a call to the no-op
7569  ** routine named test_breakpoint().
7570  */
7571  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7572    test_breakpoint();
7573  }else
7574
7575  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7576    if( nArg==2 ){
7577      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7578    }else{
7579      raw_printf(stderr, "Usage: .changes on|off\n");
7580      rc = 1;
7581    }
7582  }else
7583
7584  /* Cancel output redirection, if it is currently set (by .testcase)
7585  ** Then read the content of the testcase-out.txt file and compare against
7586  ** azArg[1].  If there are differences, report an error and exit.
7587  */
7588  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7589    char *zRes = 0;
7590    output_reset(p);
7591    if( nArg!=2 ){
7592      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7593      rc = 2;
7594    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7595      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7596      rc = 2;
7597    }else if( testcase_glob(azArg[1],zRes)==0 ){
7598      utf8_printf(stderr,
7599                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7600                 p->zTestcase, azArg[1], zRes);
7601      rc = 1;
7602    }else{
7603      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7604      p->nCheck++;
7605    }
7606    sqlite3_free(zRes);
7607  }else
7608
7609  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7610    if( nArg==2 ){
7611      tryToClone(p, azArg[1]);
7612    }else{
7613      raw_printf(stderr, "Usage: .clone FILENAME\n");
7614      rc = 1;
7615    }
7616  }else
7617
7618  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7619    char **azName = 0;
7620    int nName = 0;
7621    sqlite3_stmt *pStmt;
7622    int i;
7623    open_db(p, 0);
7624    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7625    if( rc ){
7626      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7627      rc = 1;
7628    }else{
7629      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7630        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7631        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7632        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7633        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7634        azName[nName*2] = strdup(zSchema);
7635        azName[nName*2+1] = strdup(zFile);
7636        nName++;
7637      }
7638    }
7639    sqlite3_finalize(pStmt);
7640    for(i=0; i<nName; i++){
7641      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7642      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7643      const char *z = azName[i*2+1];
7644      utf8_printf(p->out, "%s: %s %s%s\n",
7645         azName[i*2],
7646         z && z[0] ? z : "\"\"",
7647         bRdonly ? "r/o" : "r/w",
7648         eTxn==SQLITE_TXN_NONE ? "" :
7649            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7650      free(azName[i*2]);
7651      free(azName[i*2+1]);
7652    }
7653    sqlite3_free(azName);
7654  }else
7655
7656  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7657    static const struct DbConfigChoices {
7658      const char *zName;
7659      int op;
7660    } aDbConfig[] = {
7661        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7662        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7663        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7664        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7665        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7666        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7667        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7668        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7669        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7670        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7671        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7672        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7673        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7674        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7675        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7676        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7677    };
7678    int ii, v;
7679    open_db(p, 0);
7680    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7681      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7682      if( nArg>=3 ){
7683        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7684      }
7685      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7686      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7687      if( nArg>1 ) break;
7688    }
7689    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7690      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7691      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7692    }
7693  }else
7694
7695  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7696    rc = shell_dbinfo_command(p, nArg, azArg);
7697  }else
7698
7699#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7700  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7701    open_db(p, 0);
7702    rc = recoverDatabaseCmd(p, nArg, azArg);
7703  }else
7704#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7705
7706  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7707    char *zLike = 0;
7708    char *zSql;
7709    int i;
7710    int savedShowHeader = p->showHeader;
7711    int savedShellFlags = p->shellFlgs;
7712    ShellClearFlag(p,
7713       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7714       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7715    for(i=1; i<nArg; i++){
7716      if( azArg[i][0]=='-' ){
7717        const char *z = azArg[i]+1;
7718        if( z[0]=='-' ) z++;
7719        if( strcmp(z,"preserve-rowids")==0 ){
7720#ifdef SQLITE_OMIT_VIRTUALTABLE
7721          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7722                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7723          rc = 1;
7724          sqlite3_free(zLike);
7725          goto meta_command_exit;
7726#else
7727          ShellSetFlag(p, SHFLG_PreserveRowid);
7728#endif
7729        }else
7730        if( strcmp(z,"newlines")==0 ){
7731          ShellSetFlag(p, SHFLG_Newlines);
7732        }else
7733        if( strcmp(z,"data-only")==0 ){
7734          ShellSetFlag(p, SHFLG_DumpDataOnly);
7735        }else
7736        if( strcmp(z,"nosys")==0 ){
7737          ShellSetFlag(p, SHFLG_DumpNoSys);
7738        }else
7739        {
7740          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7741          rc = 1;
7742          sqlite3_free(zLike);
7743          goto meta_command_exit;
7744        }
7745      }else{
7746        /* azArg[i] contains a LIKE pattern. This ".dump" request should
7747        ** only dump data for tables for which either the table name matches
7748        ** the LIKE pattern, or the table appears to be a shadow table of
7749        ** a virtual table for which the name matches the LIKE pattern.
7750        */
7751        char *zExpr = sqlite3_mprintf(
7752            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
7753            "  SELECT 1 FROM sqlite_schema WHERE "
7754            "    name LIKE %Q ESCAPE '\\' AND"
7755            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
7756            "    substr(o.name, 1, length(name)+1) == (name||'_')"
7757            ")", azArg[i], azArg[i]
7758        );
7759
7760        if( zLike ){
7761          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
7762        }else{
7763          zLike = zExpr;
7764        }
7765      }
7766    }
7767
7768    open_db(p, 0);
7769
7770    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7771      /* When playing back a "dump", the content might appear in an order
7772      ** which causes immediate foreign key constraints to be violated.
7773      ** So disable foreign-key constraint enforcement to prevent problems. */
7774      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7775      raw_printf(p->out, "BEGIN TRANSACTION;\n");
7776    }
7777    p->writableSchema = 0;
7778    p->showHeader = 0;
7779    /* Set writable_schema=ON since doing so forces SQLite to initialize
7780    ** as much of the schema as it can even if the sqlite_schema table is
7781    ** corrupt. */
7782    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7783    p->nErr = 0;
7784    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7785    zSql = sqlite3_mprintf(
7786      "SELECT name, type, sql FROM sqlite_schema AS o "
7787      "WHERE (%s) AND type=='table'"
7788      "  AND sql NOT NULL"
7789      " ORDER BY tbl_name='sqlite_sequence', rowid",
7790      zLike
7791    );
7792    run_schema_dump_query(p,zSql);
7793    sqlite3_free(zSql);
7794    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7795      zSql = sqlite3_mprintf(
7796        "SELECT sql FROM sqlite_schema AS o "
7797        "WHERE (%s) AND sql NOT NULL"
7798        "  AND type IN ('index','trigger','view')",
7799        zLike
7800      );
7801      run_table_dump_query(p, zSql);
7802      sqlite3_free(zSql);
7803    }
7804    sqlite3_free(zLike);
7805    if( p->writableSchema ){
7806      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7807      p->writableSchema = 0;
7808    }
7809    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7810    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7811    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7812      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7813    }
7814    p->showHeader = savedShowHeader;
7815    p->shellFlgs = savedShellFlags;
7816  }else
7817
7818  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7819    if( nArg==2 ){
7820      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7821    }else{
7822      raw_printf(stderr, "Usage: .echo on|off\n");
7823      rc = 1;
7824    }
7825  }else
7826
7827  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7828    if( nArg==2 ){
7829      p->autoEQPtest = 0;
7830      if( p->autoEQPtrace ){
7831        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7832        p->autoEQPtrace = 0;
7833      }
7834      if( strcmp(azArg[1],"full")==0 ){
7835        p->autoEQP = AUTOEQP_full;
7836      }else if( strcmp(azArg[1],"trigger")==0 ){
7837        p->autoEQP = AUTOEQP_trigger;
7838#ifdef SQLITE_DEBUG
7839      }else if( strcmp(azArg[1],"test")==0 ){
7840        p->autoEQP = AUTOEQP_on;
7841        p->autoEQPtest = 1;
7842      }else if( strcmp(azArg[1],"trace")==0 ){
7843        p->autoEQP = AUTOEQP_full;
7844        p->autoEQPtrace = 1;
7845        open_db(p, 0);
7846        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
7847        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7848#endif
7849      }else{
7850        p->autoEQP = (u8)booleanValue(azArg[1]);
7851      }
7852    }else{
7853      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7854      rc = 1;
7855    }
7856  }else
7857
7858  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7859    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7860    rc = 2;
7861  }else
7862
7863  /* The ".explain" command is automatic now.  It is largely pointless.  It
7864  ** retained purely for backwards compatibility */
7865  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7866    int val = 1;
7867    if( nArg>=2 ){
7868      if( strcmp(azArg[1],"auto")==0 ){
7869        val = 99;
7870      }else{
7871        val =  booleanValue(azArg[1]);
7872      }
7873    }
7874    if( val==1 && p->mode!=MODE_Explain ){
7875      p->normalMode = p->mode;
7876      p->mode = MODE_Explain;
7877      p->autoExplain = 0;
7878    }else if( val==0 ){
7879      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7880      p->autoExplain = 0;
7881    }else if( val==99 ){
7882      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7883      p->autoExplain = 1;
7884    }
7885  }else
7886
7887#ifndef SQLITE_OMIT_VIRTUALTABLE
7888  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7889    open_db(p, 0);
7890    expertDotCommand(p, azArg, nArg);
7891  }else
7892#endif
7893
7894  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7895    static const struct {
7896       const char *zCtrlName;   /* Name of a test-control option */
7897       int ctrlCode;            /* Integer code for that option */
7898       const char *zUsage;      /* Usage notes */
7899    } aCtrl[] = {
7900      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7901      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
7902      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7903      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7904      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7905   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7906      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7907      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
7908      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7909      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7910   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7911    };
7912    int filectrl = -1;
7913    int iCtrl = -1;
7914    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7915    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7916    int n2, i;
7917    const char *zCmd = 0;
7918    const char *zSchema = 0;
7919
7920    open_db(p, 0);
7921    zCmd = nArg>=2 ? azArg[1] : "help";
7922
7923    if( zCmd[0]=='-'
7924     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7925     && nArg>=4
7926    ){
7927      zSchema = azArg[2];
7928      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7929      nArg -= 2;
7930      zCmd = azArg[1];
7931    }
7932
7933    /* The argument can optionally begin with "-" or "--" */
7934    if( zCmd[0]=='-' && zCmd[1] ){
7935      zCmd++;
7936      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7937    }
7938
7939    /* --help lists all file-controls */
7940    if( strcmp(zCmd,"help")==0 ){
7941      utf8_printf(p->out, "Available file-controls:\n");
7942      for(i=0; i<ArraySize(aCtrl); i++){
7943        utf8_printf(p->out, "  .filectrl %s %s\n",
7944                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7945      }
7946      rc = 1;
7947      goto meta_command_exit;
7948    }
7949
7950    /* convert filectrl text option to value. allow any unique prefix
7951    ** of the option name, or a numerical value. */
7952    n2 = strlen30(zCmd);
7953    for(i=0; i<ArraySize(aCtrl); i++){
7954      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7955        if( filectrl<0 ){
7956          filectrl = aCtrl[i].ctrlCode;
7957          iCtrl = i;
7958        }else{
7959          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7960                              "Use \".filectrl --help\" for help\n", zCmd);
7961          rc = 1;
7962          goto meta_command_exit;
7963        }
7964      }
7965    }
7966    if( filectrl<0 ){
7967      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7968                         "Use \".filectrl --help\" for help\n", zCmd);
7969    }else{
7970      switch(filectrl){
7971        case SQLITE_FCNTL_SIZE_LIMIT: {
7972          if( nArg!=2 && nArg!=3 ) break;
7973          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7974          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7975          isOk = 1;
7976          break;
7977        }
7978        case SQLITE_FCNTL_LOCK_TIMEOUT:
7979        case SQLITE_FCNTL_CHUNK_SIZE: {
7980          int x;
7981          if( nArg!=3 ) break;
7982          x = (int)integerValue(azArg[2]);
7983          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7984          isOk = 2;
7985          break;
7986        }
7987        case SQLITE_FCNTL_PERSIST_WAL:
7988        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7989          int x;
7990          if( nArg!=2 && nArg!=3 ) break;
7991          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7992          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7993          iRes = x;
7994          isOk = 1;
7995          break;
7996        }
7997        case SQLITE_FCNTL_DATA_VERSION:
7998        case SQLITE_FCNTL_HAS_MOVED: {
7999          int x;
8000          if( nArg!=2 ) break;
8001          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8002          iRes = x;
8003          isOk = 1;
8004          break;
8005        }
8006        case SQLITE_FCNTL_TEMPFILENAME: {
8007          char *z = 0;
8008          if( nArg!=2 ) break;
8009          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8010          if( z ){
8011            utf8_printf(p->out, "%s\n", z);
8012            sqlite3_free(z);
8013          }
8014          isOk = 2;
8015          break;
8016        }
8017        case SQLITE_FCNTL_RESERVE_BYTES: {
8018          int x;
8019          if( nArg>=3 ){
8020            x = atoi(azArg[2]);
8021            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8022          }
8023          x = -1;
8024          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8025          utf8_printf(p->out,"%d\n", x);
8026          isOk = 2;
8027          break;
8028        }
8029      }
8030    }
8031    if( isOk==0 && iCtrl>=0 ){
8032      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8033      rc = 1;
8034    }else if( isOk==1 ){
8035      char zBuf[100];
8036      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8037      raw_printf(p->out, "%s\n", zBuf);
8038    }
8039  }else
8040
8041  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8042    ShellState data;
8043    int doStats = 0;
8044    memcpy(&data, p, sizeof(data));
8045    data.showHeader = 0;
8046    data.cMode = data.mode = MODE_Semi;
8047    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8048      data.cMode = data.mode = MODE_Pretty;
8049      nArg = 1;
8050    }
8051    if( nArg!=1 ){
8052      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8053      rc = 1;
8054      goto meta_command_exit;
8055    }
8056    open_db(p, 0);
8057    rc = sqlite3_exec(p->db,
8058       "SELECT sql FROM"
8059       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8060       "     FROM sqlite_schema UNION ALL"
8061       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8062       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8063       "ORDER BY x",
8064       callback, &data, 0
8065    );
8066    if( rc==SQLITE_OK ){
8067      sqlite3_stmt *pStmt;
8068      rc = sqlite3_prepare_v2(p->db,
8069               "SELECT rowid FROM sqlite_schema"
8070               " WHERE name GLOB 'sqlite_stat[134]'",
8071               -1, &pStmt, 0);
8072      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8073      sqlite3_finalize(pStmt);
8074    }
8075    if( doStats==0 ){
8076      raw_printf(p->out, "/* No STAT tables available */\n");
8077    }else{
8078      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8079      data.cMode = data.mode = MODE_Insert;
8080      data.zDestTable = "sqlite_stat1";
8081      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8082      data.zDestTable = "sqlite_stat4";
8083      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8084      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8085    }
8086  }else
8087
8088  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8089    if( nArg==2 ){
8090      p->showHeader = booleanValue(azArg[1]);
8091      p->shellFlgs |= SHFLG_HeaderSet;
8092    }else{
8093      raw_printf(stderr, "Usage: .headers on|off\n");
8094      rc = 1;
8095    }
8096  }else
8097
8098  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8099    if( nArg>=2 ){
8100      n = showHelp(p->out, azArg[1]);
8101      if( n==0 ){
8102        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8103      }
8104    }else{
8105      showHelp(p->out, 0);
8106    }
8107  }else
8108
8109  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8110    char *zTable = 0;           /* Insert data into this table */
8111    char *zFile = 0;            /* Name of file to extra content from */
8112    sqlite3_stmt *pStmt = NULL; /* A statement */
8113    int nCol;                   /* Number of columns in the table */
8114    int nByte;                  /* Number of bytes in an SQL string */
8115    int i, j;                   /* Loop counters */
8116    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8117    int nSep;                   /* Number of bytes in p->colSeparator[] */
8118    char *zSql;                 /* An SQL statement */
8119    ImportCtx sCtx;             /* Reader context */
8120    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8121    int eVerbose = 0;           /* Larger for more console output */
8122    int nSkip = 0;              /* Initial lines to skip */
8123    int useOutputMode = 1;      /* Use output mode to determine separators */
8124
8125    memset(&sCtx, 0, sizeof(sCtx));
8126    if( p->mode==MODE_Ascii ){
8127      xRead = ascii_read_one_field;
8128    }else{
8129      xRead = csv_read_one_field;
8130    }
8131    for(i=1; i<nArg; i++){
8132      char *z = azArg[i];
8133      if( z[0]=='-' && z[1]=='-' ) z++;
8134      if( z[0]!='-' ){
8135        if( zFile==0 ){
8136          zFile = z;
8137        }else if( zTable==0 ){
8138          zTable = z;
8139        }else{
8140          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8141          showHelp(p->out, "import");
8142          rc = 1;
8143          goto meta_command_exit;
8144        }
8145      }else if( strcmp(z,"-v")==0 ){
8146        eVerbose++;
8147      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8148        nSkip = integerValue(azArg[++i]);
8149      }else if( strcmp(z,"-ascii")==0 ){
8150        sCtx.cColSep = SEP_Unit[0];
8151        sCtx.cRowSep = SEP_Record[0];
8152        xRead = ascii_read_one_field;
8153        useOutputMode = 0;
8154      }else if( strcmp(z,"-csv")==0 ){
8155        sCtx.cColSep = ',';
8156        sCtx.cRowSep = '\n';
8157        xRead = csv_read_one_field;
8158        useOutputMode = 0;
8159      }else{
8160        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8161        showHelp(p->out, "import");
8162        rc = 1;
8163        goto meta_command_exit;
8164      }
8165    }
8166    if( zTable==0 ){
8167      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8168                  zFile==0 ? "FILE" : "TABLE");
8169      showHelp(p->out, "import");
8170      rc = 1;
8171      goto meta_command_exit;
8172    }
8173    seenInterrupt = 0;
8174    open_db(p, 0);
8175    if( useOutputMode ){
8176      /* If neither the --csv or --ascii options are specified, then set
8177      ** the column and row separator characters from the output mode. */
8178      nSep = strlen30(p->colSeparator);
8179      if( nSep==0 ){
8180        raw_printf(stderr,
8181                   "Error: non-null column separator required for import\n");
8182        rc = 1;
8183        goto meta_command_exit;
8184      }
8185      if( nSep>1 ){
8186        raw_printf(stderr,
8187              "Error: multi-character column separators not allowed"
8188              " for import\n");
8189        rc = 1;
8190        goto meta_command_exit;
8191      }
8192      nSep = strlen30(p->rowSeparator);
8193      if( nSep==0 ){
8194        raw_printf(stderr,
8195            "Error: non-null row separator required for import\n");
8196        rc = 1;
8197        goto meta_command_exit;
8198      }
8199      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8200        /* When importing CSV (only), if the row separator is set to the
8201        ** default output row separator, change it to the default input
8202        ** row separator.  This avoids having to maintain different input
8203        ** and output row separators. */
8204        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8205        nSep = strlen30(p->rowSeparator);
8206      }
8207      if( nSep>1 ){
8208        raw_printf(stderr, "Error: multi-character row separators not allowed"
8209                           " for import\n");
8210        rc = 1;
8211        goto meta_command_exit;
8212      }
8213      sCtx.cColSep = p->colSeparator[0];
8214      sCtx.cRowSep = p->rowSeparator[0];
8215    }
8216    sCtx.zFile = zFile;
8217    sCtx.nLine = 1;
8218    if( sCtx.zFile[0]=='|' ){
8219#ifdef SQLITE_OMIT_POPEN
8220      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8221      rc = 1;
8222      goto meta_command_exit;
8223#else
8224      sCtx.in = popen(sCtx.zFile+1, "r");
8225      sCtx.zFile = "<pipe>";
8226      sCtx.xCloser = pclose;
8227#endif
8228    }else{
8229      sCtx.in = fopen(sCtx.zFile, "rb");
8230      sCtx.xCloser = fclose;
8231    }
8232    if( sCtx.in==0 ){
8233      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8234      rc = 1;
8235      goto meta_command_exit;
8236    }
8237    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8238      char zSep[2];
8239      zSep[1] = 0;
8240      zSep[0] = sCtx.cColSep;
8241      utf8_printf(p->out, "Column separator ");
8242      output_c_string(p->out, zSep);
8243      utf8_printf(p->out, ", row separator ");
8244      zSep[0] = sCtx.cRowSep;
8245      output_c_string(p->out, zSep);
8246      utf8_printf(p->out, "\n");
8247    }
8248    while( (nSkip--)>0 ){
8249      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8250    }
8251    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
8252    if( zSql==0 ){
8253      import_cleanup(&sCtx);
8254      shell_out_of_memory();
8255    }
8256    nByte = strlen30(zSql);
8257    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8258    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8259    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8260      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
8261      char cSep = '(';
8262      while( xRead(&sCtx) ){
8263        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8264        cSep = ',';
8265        if( sCtx.cTerm!=sCtx.cColSep ) break;
8266      }
8267      if( cSep=='(' ){
8268        sqlite3_free(zCreate);
8269        import_cleanup(&sCtx);
8270        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8271        rc = 1;
8272        goto meta_command_exit;
8273      }
8274      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8275      if( eVerbose>=1 ){
8276        utf8_printf(p->out, "%s\n", zCreate);
8277      }
8278      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8279      sqlite3_free(zCreate);
8280      if( rc ){
8281        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
8282                sqlite3_errmsg(p->db));
8283        import_cleanup(&sCtx);
8284        rc = 1;
8285        goto meta_command_exit;
8286      }
8287      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8288    }
8289    sqlite3_free(zSql);
8290    if( rc ){
8291      if (pStmt) sqlite3_finalize(pStmt);
8292      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8293      import_cleanup(&sCtx);
8294      rc = 1;
8295      goto meta_command_exit;
8296    }
8297    nCol = sqlite3_column_count(pStmt);
8298    sqlite3_finalize(pStmt);
8299    pStmt = 0;
8300    if( nCol==0 ) return 0; /* no columns, no error */
8301    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8302    if( zSql==0 ){
8303      import_cleanup(&sCtx);
8304      shell_out_of_memory();
8305    }
8306    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8307    j = strlen30(zSql);
8308    for(i=1; i<nCol; i++){
8309      zSql[j++] = ',';
8310      zSql[j++] = '?';
8311    }
8312    zSql[j++] = ')';
8313    zSql[j] = 0;
8314    if( eVerbose>=2 ){
8315      utf8_printf(p->out, "Insert using: %s\n", zSql);
8316    }
8317    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8318    sqlite3_free(zSql);
8319    if( rc ){
8320      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8321      if (pStmt) sqlite3_finalize(pStmt);
8322      import_cleanup(&sCtx);
8323      rc = 1;
8324      goto meta_command_exit;
8325    }
8326    needCommit = sqlite3_get_autocommit(p->db);
8327    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8328    do{
8329      int startLine = sCtx.nLine;
8330      for(i=0; i<nCol; i++){
8331        char *z = xRead(&sCtx);
8332        /*
8333        ** Did we reach end-of-file before finding any columns?
8334        ** If so, stop instead of NULL filling the remaining columns.
8335        */
8336        if( z==0 && i==0 ) break;
8337        /*
8338        ** Did we reach end-of-file OR end-of-line before finding any
8339        ** columns in ASCII mode?  If so, stop instead of NULL filling
8340        ** the remaining columns.
8341        */
8342        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8343        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8344        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8345          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8346                          "filling the rest with NULL\n",
8347                          sCtx.zFile, startLine, nCol, i+1);
8348          i += 2;
8349          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8350        }
8351      }
8352      if( sCtx.cTerm==sCtx.cColSep ){
8353        do{
8354          xRead(&sCtx);
8355          i++;
8356        }while( sCtx.cTerm==sCtx.cColSep );
8357        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8358                        "extras ignored\n",
8359                        sCtx.zFile, startLine, nCol, i);
8360      }
8361      if( i>=nCol ){
8362        sqlite3_step(pStmt);
8363        rc = sqlite3_reset(pStmt);
8364        if( rc!=SQLITE_OK ){
8365          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8366                      startLine, sqlite3_errmsg(p->db));
8367          sCtx.nErr++;
8368        }else{
8369          sCtx.nRow++;
8370        }
8371      }
8372    }while( sCtx.cTerm!=EOF );
8373
8374    import_cleanup(&sCtx);
8375    sqlite3_finalize(pStmt);
8376    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8377    if( eVerbose>0 ){
8378      utf8_printf(p->out,
8379          "Added %d rows with %d errors using %d lines of input\n",
8380          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8381    }
8382  }else
8383
8384#ifndef SQLITE_UNTESTABLE
8385  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8386    char *zSql;
8387    char *zCollist = 0;
8388    sqlite3_stmt *pStmt;
8389    int tnum = 0;
8390    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8391    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8392    int i;
8393    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8394      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8395                          "       .imposter off\n");
8396      /* Also allowed, but not documented:
8397      **
8398      **    .imposter TABLE IMPOSTER
8399      **
8400      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8401      ** imposter is another WITHOUT ROWID table with the columns in
8402      ** storage order. */
8403      rc = 1;
8404      goto meta_command_exit;
8405    }
8406    open_db(p, 0);
8407    if( nArg==2 ){
8408      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8409      goto meta_command_exit;
8410    }
8411    zSql = sqlite3_mprintf(
8412      "SELECT rootpage, 0 FROM sqlite_schema"
8413      " WHERE name='%q' AND type='index'"
8414      "UNION ALL "
8415      "SELECT rootpage, 1 FROM sqlite_schema"
8416      " WHERE name='%q' AND type='table'"
8417      "   AND sql LIKE '%%without%%rowid%%'",
8418      azArg[1], azArg[1]
8419    );
8420    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8421    sqlite3_free(zSql);
8422    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8423      tnum = sqlite3_column_int(pStmt, 0);
8424      isWO = sqlite3_column_int(pStmt, 1);
8425    }
8426    sqlite3_finalize(pStmt);
8427    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8428    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8429    sqlite3_free(zSql);
8430    i = 0;
8431    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8432      char zLabel[20];
8433      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8434      i++;
8435      if( zCol==0 ){
8436        if( sqlite3_column_int(pStmt,1)==-1 ){
8437          zCol = "_ROWID_";
8438        }else{
8439          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8440          zCol = zLabel;
8441        }
8442      }
8443      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8444        lenPK = (int)strlen(zCollist);
8445      }
8446      if( zCollist==0 ){
8447        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8448      }else{
8449        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8450      }
8451    }
8452    sqlite3_finalize(pStmt);
8453    if( i==0 || tnum==0 ){
8454      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8455      rc = 1;
8456      sqlite3_free(zCollist);
8457      goto meta_command_exit;
8458    }
8459    if( lenPK==0 ) lenPK = 100000;
8460    zSql = sqlite3_mprintf(
8461          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8462          azArg[2], zCollist, lenPK, zCollist);
8463    sqlite3_free(zCollist);
8464    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8465    if( rc==SQLITE_OK ){
8466      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8467      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8468      if( rc ){
8469        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8470      }else{
8471        utf8_printf(stdout, "%s;\n", zSql);
8472        raw_printf(stdout,
8473          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8474          azArg[1], isWO ? "table" : "index"
8475        );
8476      }
8477    }else{
8478      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8479      rc = 1;
8480    }
8481    sqlite3_free(zSql);
8482  }else
8483#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8484
8485#ifdef SQLITE_ENABLE_IOTRACE
8486  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8487    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8488    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8489    iotrace = 0;
8490    if( nArg<2 ){
8491      sqlite3IoTrace = 0;
8492    }else if( strcmp(azArg[1], "-")==0 ){
8493      sqlite3IoTrace = iotracePrintf;
8494      iotrace = stdout;
8495    }else{
8496      iotrace = fopen(azArg[1], "w");
8497      if( iotrace==0 ){
8498        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8499        sqlite3IoTrace = 0;
8500        rc = 1;
8501      }else{
8502        sqlite3IoTrace = iotracePrintf;
8503      }
8504    }
8505  }else
8506#endif
8507
8508  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8509    static const struct {
8510       const char *zLimitName;   /* Name of a limit */
8511       int limitCode;            /* Integer code for that limit */
8512    } aLimit[] = {
8513      { "length",                SQLITE_LIMIT_LENGTH                    },
8514      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8515      { "column",                SQLITE_LIMIT_COLUMN                    },
8516      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8517      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8518      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8519      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8520      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8521      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8522      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8523      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8524      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8525    };
8526    int i, n2;
8527    open_db(p, 0);
8528    if( nArg==1 ){
8529      for(i=0; i<ArraySize(aLimit); i++){
8530        printf("%20s %d\n", aLimit[i].zLimitName,
8531               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8532      }
8533    }else if( nArg>3 ){
8534      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8535      rc = 1;
8536      goto meta_command_exit;
8537    }else{
8538      int iLimit = -1;
8539      n2 = strlen30(azArg[1]);
8540      for(i=0; i<ArraySize(aLimit); i++){
8541        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8542          if( iLimit<0 ){
8543            iLimit = i;
8544          }else{
8545            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8546            rc = 1;
8547            goto meta_command_exit;
8548          }
8549        }
8550      }
8551      if( iLimit<0 ){
8552        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8553                        "enter \".limits\" with no arguments for a list.\n",
8554                         azArg[1]);
8555        rc = 1;
8556        goto meta_command_exit;
8557      }
8558      if( nArg==3 ){
8559        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8560                      (int)integerValue(azArg[2]));
8561      }
8562      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8563             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8564    }
8565  }else
8566
8567  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8568    open_db(p, 0);
8569    lintDotCommand(p, azArg, nArg);
8570  }else
8571
8572#ifndef SQLITE_OMIT_LOAD_EXTENSION
8573  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8574    const char *zFile, *zProc;
8575    char *zErrMsg = 0;
8576    if( nArg<2 ){
8577      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8578      rc = 1;
8579      goto meta_command_exit;
8580    }
8581    zFile = azArg[1];
8582    zProc = nArg>=3 ? azArg[2] : 0;
8583    open_db(p, 0);
8584    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8585    if( rc!=SQLITE_OK ){
8586      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8587      sqlite3_free(zErrMsg);
8588      rc = 1;
8589    }
8590  }else
8591#endif
8592
8593  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8594    if( nArg!=2 ){
8595      raw_printf(stderr, "Usage: .log FILENAME\n");
8596      rc = 1;
8597    }else{
8598      const char *zFile = azArg[1];
8599      output_file_close(p->pLog);
8600      p->pLog = output_file_open(zFile, 0);
8601    }
8602  }else
8603
8604  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8605    const char *zMode = nArg>=2 ? azArg[1] : "";
8606    int n2 = strlen30(zMode);
8607    int c2 = zMode[0];
8608    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8609      p->mode = MODE_Line;
8610      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8611    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8612      p->mode = MODE_Column;
8613      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8614        p->showHeader = 1;
8615      }
8616      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8617    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8618      p->mode = MODE_List;
8619      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8620      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8621    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8622      p->mode = MODE_Html;
8623    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8624      p->mode = MODE_Tcl;
8625      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8626      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8627    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8628      p->mode = MODE_Csv;
8629      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8630      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8631    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8632      p->mode = MODE_List;
8633      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8634    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8635      p->mode = MODE_Insert;
8636      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8637    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8638      p->mode = MODE_Quote;
8639      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8640      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8641    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8642      p->mode = MODE_Ascii;
8643      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8644      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8645    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8646      p->mode = MODE_Markdown;
8647    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8648      p->mode = MODE_Table;
8649    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8650      p->mode = MODE_Box;
8651    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8652      p->mode = MODE_Json;
8653    }else if( nArg==1 ){
8654      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8655    }else{
8656      raw_printf(stderr, "Error: mode should be one of: "
8657         "ascii box column csv html insert json line list markdown "
8658         "quote table tabs tcl\n");
8659      rc = 1;
8660    }
8661    p->cMode = p->mode;
8662  }else
8663
8664  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8665    if( nArg==2 ){
8666      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8667                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8668    }else{
8669      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8670      rc = 1;
8671    }
8672  }else
8673
8674#ifdef SQLITE_DEBUG
8675  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8676    int i;
8677    for(i=1; i<nArg; i++){
8678      const char *z = azArg[i];
8679      if( z[0]=='-' && z[1]=='-' ) z++;
8680      if( strcmp(z,"-repeat")==0 ){
8681        if( i==nArg-1 ){
8682          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8683          rc = 1;
8684        }else{
8685          oomRepeat = (int)integerValue(azArg[++i]);
8686        }
8687      }else if( IsDigit(z[0]) ){
8688        oomCounter = (int)integerValue(azArg[i]);
8689      }else{
8690        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8691        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8692        rc = 1;
8693      }
8694    }
8695    if( rc==0 ){
8696      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8697      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8698    }
8699  }else
8700#endif /* SQLITE_DEBUG */
8701
8702  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8703    char *zNewFilename = 0;  /* Name of the database file to open */
8704    int iName = 1;           /* Index in azArg[] of the filename */
8705    int newFlag = 0;         /* True to delete file before opening */
8706    /* Close the existing database */
8707    session_close_all(p);
8708    close_db(p->db);
8709    p->db = 0;
8710    p->zDbFilename = 0;
8711    sqlite3_free(p->zFreeOnClose);
8712    p->zFreeOnClose = 0;
8713    p->openMode = SHELL_OPEN_UNSPEC;
8714    p->openFlags = 0;
8715    p->szMax = 0;
8716    /* Check for command-line arguments */
8717    for(iName=1; iName<nArg; iName++){
8718      const char *z = azArg[iName];
8719      if( optionMatch(z,"new") ){
8720        newFlag = 1;
8721#ifdef SQLITE_HAVE_ZLIB
8722      }else if( optionMatch(z, "zip") ){
8723        p->openMode = SHELL_OPEN_ZIPFILE;
8724#endif
8725      }else if( optionMatch(z, "append") ){
8726        p->openMode = SHELL_OPEN_APPENDVFS;
8727      }else if( optionMatch(z, "readonly") ){
8728        p->openMode = SHELL_OPEN_READONLY;
8729      }else if( optionMatch(z, "nofollow") ){
8730        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8731      }else if( optionMatch(z, "excl") ){
8732        p->openFlags |= SQLITE_OPEN_EXCLUSIVE;
8733#ifndef SQLITE_OMIT_DESERIALIZE
8734      }else if( optionMatch(z, "deserialize") ){
8735        p->openMode = SHELL_OPEN_DESERIALIZE;
8736      }else if( optionMatch(z, "hexdb") ){
8737        p->openMode = SHELL_OPEN_HEXDB;
8738      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8739        p->szMax = integerValue(azArg[++iName]);
8740#endif /* SQLITE_OMIT_DESERIALIZE */
8741      }else if( z[0]=='-' ){
8742        utf8_printf(stderr, "unknown option: %s\n", z);
8743        rc = 1;
8744        goto meta_command_exit;
8745      }else if( zNewFilename ){
8746        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
8747        rc = 1;
8748        goto meta_command_exit;
8749      }else{
8750        zNewFilename = sqlite3_mprintf("%s", z);
8751      }
8752    }
8753    /* If a filename is specified, try to open it first */
8754    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8755      if( newFlag ) shellDeleteFile(zNewFilename);
8756      p->zDbFilename = zNewFilename;
8757      open_db(p, OPEN_DB_KEEPALIVE);
8758      if( p->db==0 ){
8759        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8760        sqlite3_free(zNewFilename);
8761      }else{
8762        p->zFreeOnClose = zNewFilename;
8763      }
8764    }
8765    if( p->db==0 ){
8766      /* As a fall-back open a TEMP database */
8767      p->zDbFilename = 0;
8768      open_db(p, 0);
8769    }
8770  }else
8771
8772  if( (c=='o'
8773        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8774   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8775  ){
8776    char *zFile = 0;
8777    int bTxtMode = 0;
8778    int i;
8779    int eMode = 0;
8780    int bBOM = 0;
8781    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8782
8783    if( c=='e' ){
8784      eMode = 'x';
8785      bOnce = 2;
8786    }else if( strncmp(azArg[0],"once",n)==0 ){
8787      bOnce = 1;
8788    }
8789    for(i=1; i<nArg; i++){
8790      char *z = azArg[i];
8791      if( z[0]=='-' ){
8792        if( z[1]=='-' ) z++;
8793        if( strcmp(z,"-bom")==0 ){
8794          bBOM = 1;
8795        }else if( c!='e' && strcmp(z,"-x")==0 ){
8796          eMode = 'x';  /* spreadsheet */
8797        }else if( c!='e' && strcmp(z,"-e")==0 ){
8798          eMode = 'e';  /* text editor */
8799        }else{
8800          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8801                      azArg[i]);
8802          showHelp(p->out, azArg[0]);
8803          rc = 1;
8804          goto meta_command_exit;
8805        }
8806      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
8807        zFile = sqlite3_mprintf("%s", z);
8808        if( zFile[0]=='|' ){
8809          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
8810          break;
8811        }
8812      }else{
8813        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8814                    azArg[i]);
8815        showHelp(p->out, azArg[0]);
8816        rc = 1;
8817        sqlite3_free(zFile);
8818        goto meta_command_exit;
8819      }
8820    }
8821    if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
8822    if( bOnce ){
8823      p->outCount = 2;
8824    }else{
8825      p->outCount = 0;
8826    }
8827    output_reset(p);
8828#ifndef SQLITE_NOHAVE_SYSTEM
8829    if( eMode=='e' || eMode=='x' ){
8830      p->doXdgOpen = 1;
8831      outputModePush(p);
8832      if( eMode=='x' ){
8833        /* spreadsheet mode.  Output as CSV. */
8834        newTempFile(p, "csv");
8835        ShellClearFlag(p, SHFLG_Echo);
8836        p->mode = MODE_Csv;
8837        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8838        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8839      }else{
8840        /* text editor mode */
8841        newTempFile(p, "txt");
8842        bTxtMode = 1;
8843      }
8844      sqlite3_free(zFile);
8845      zFile = sqlite3_mprintf("%s", p->zTempFile);
8846    }
8847#endif /* SQLITE_NOHAVE_SYSTEM */
8848    if( zFile[0]=='|' ){
8849#ifdef SQLITE_OMIT_POPEN
8850      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8851      rc = 1;
8852      p->out = stdout;
8853#else
8854      p->out = popen(zFile + 1, "w");
8855      if( p->out==0 ){
8856        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8857        p->out = stdout;
8858        rc = 1;
8859      }else{
8860        if( bBOM ) fprintf(p->out,"\357\273\277");
8861        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8862      }
8863#endif
8864    }else{
8865      p->out = output_file_open(zFile, bTxtMode);
8866      if( p->out==0 ){
8867        if( strcmp(zFile,"off")!=0 ){
8868          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8869        }
8870        p->out = stdout;
8871        rc = 1;
8872      } else {
8873        if( bBOM ) fprintf(p->out,"\357\273\277");
8874        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8875      }
8876    }
8877    sqlite3_free(zFile);
8878  }else
8879
8880  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8881    open_db(p,0);
8882    if( nArg<=1 ) goto parameter_syntax_error;
8883
8884    /* .parameter clear
8885    ** Clear all bind parameters by dropping the TEMP table that holds them.
8886    */
8887    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8888      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8889                   0, 0, 0);
8890    }else
8891
8892    /* .parameter list
8893    ** List all bind parameters.
8894    */
8895    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8896      sqlite3_stmt *pStmt = 0;
8897      int rx;
8898      int len = 0;
8899      rx = sqlite3_prepare_v2(p->db,
8900             "SELECT max(length(key)) "
8901             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8902      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8903        len = sqlite3_column_int(pStmt, 0);
8904        if( len>40 ) len = 40;
8905      }
8906      sqlite3_finalize(pStmt);
8907      pStmt = 0;
8908      if( len ){
8909        rx = sqlite3_prepare_v2(p->db,
8910             "SELECT key, quote(value) "
8911             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8912        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8913          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8914                      sqlite3_column_text(pStmt,1));
8915        }
8916        sqlite3_finalize(pStmt);
8917      }
8918    }else
8919
8920    /* .parameter init
8921    ** Make sure the TEMP table used to hold bind parameters exists.
8922    ** Create it if necessary.
8923    */
8924    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8925      bind_table_init(p);
8926    }else
8927
8928    /* .parameter set NAME VALUE
8929    ** Set or reset a bind parameter.  NAME should be the full parameter
8930    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8931    ** VALUE can be in either SQL literal notation, or if not it will be
8932    ** understood to be a text string.
8933    */
8934    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8935      int rx;
8936      char *zSql;
8937      sqlite3_stmt *pStmt;
8938      const char *zKey = azArg[2];
8939      const char *zValue = azArg[3];
8940      bind_table_init(p);
8941      zSql = sqlite3_mprintf(
8942                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8943                  "VALUES(%Q,%s);", zKey, zValue);
8944      if( zSql==0 ) shell_out_of_memory();
8945      pStmt = 0;
8946      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8947      sqlite3_free(zSql);
8948      if( rx!=SQLITE_OK ){
8949        sqlite3_finalize(pStmt);
8950        pStmt = 0;
8951        zSql = sqlite3_mprintf(
8952                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8953                   "VALUES(%Q,%Q);", zKey, zValue);
8954        if( zSql==0 ) shell_out_of_memory();
8955        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8956        sqlite3_free(zSql);
8957        if( rx!=SQLITE_OK ){
8958          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8959          sqlite3_finalize(pStmt);
8960          pStmt = 0;
8961          rc = 1;
8962        }
8963      }
8964      sqlite3_step(pStmt);
8965      sqlite3_finalize(pStmt);
8966    }else
8967
8968    /* .parameter unset NAME
8969    ** Remove the NAME binding from the parameter binding table, if it
8970    ** exists.
8971    */
8972    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8973      char *zSql = sqlite3_mprintf(
8974          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8975      if( zSql==0 ) shell_out_of_memory();
8976      sqlite3_exec(p->db, zSql, 0, 0, 0);
8977      sqlite3_free(zSql);
8978    }else
8979    /* If no command name matches, show a syntax error */
8980    parameter_syntax_error:
8981    showHelp(p->out, "parameter");
8982  }else
8983
8984  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8985    int i;
8986    for(i=1; i<nArg; i++){
8987      if( i>1 ) raw_printf(p->out, " ");
8988      utf8_printf(p->out, "%s", azArg[i]);
8989    }
8990    raw_printf(p->out, "\n");
8991  }else
8992
8993#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8994  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8995    int i;
8996    int nn = 0;
8997    p->flgProgress = 0;
8998    p->mxProgress = 0;
8999    p->nProgress = 0;
9000    for(i=1; i<nArg; i++){
9001      const char *z = azArg[i];
9002      if( z[0]=='-' ){
9003        z++;
9004        if( z[0]=='-' ) z++;
9005        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
9006          p->flgProgress |= SHELL_PROGRESS_QUIET;
9007          continue;
9008        }
9009        if( strcmp(z,"reset")==0 ){
9010          p->flgProgress |= SHELL_PROGRESS_RESET;
9011          continue;
9012        }
9013        if( strcmp(z,"once")==0 ){
9014          p->flgProgress |= SHELL_PROGRESS_ONCE;
9015          continue;
9016        }
9017        if( strcmp(z,"limit")==0 ){
9018          if( i+1>=nArg ){
9019            utf8_printf(stderr, "Error: missing argument on --limit\n");
9020            rc = 1;
9021            goto meta_command_exit;
9022          }else{
9023            p->mxProgress = (int)integerValue(azArg[++i]);
9024          }
9025          continue;
9026        }
9027        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9028        rc = 1;
9029        goto meta_command_exit;
9030      }else{
9031        nn = (int)integerValue(z);
9032      }
9033    }
9034    open_db(p, 0);
9035    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9036  }else
9037#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9038
9039  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9040    if( nArg >= 2) {
9041      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9042    }
9043    if( nArg >= 3) {
9044      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9045    }
9046  }else
9047
9048  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
9049    rc = 2;
9050  }else
9051
9052  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
9053    FILE *inSaved = p->in;
9054    int savedLineno = p->lineno;
9055    if( nArg!=2 ){
9056      raw_printf(stderr, "Usage: .read FILE\n");
9057      rc = 1;
9058      goto meta_command_exit;
9059    }
9060    if( azArg[1][0]=='|' ){
9061#ifdef SQLITE_OMIT_POPEN
9062      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9063      rc = 1;
9064      p->out = stdout;
9065#else
9066      p->in = popen(azArg[1]+1, "r");
9067      if( p->in==0 ){
9068        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9069        rc = 1;
9070      }else{
9071        rc = process_input(p);
9072        pclose(p->in);
9073      }
9074#endif
9075    }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
9076      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9077      rc = 1;
9078    }else{
9079      rc = process_input(p);
9080      fclose(p->in);
9081    }
9082    p->in = inSaved;
9083    p->lineno = savedLineno;
9084  }else
9085
9086  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9087    const char *zSrcFile;
9088    const char *zDb;
9089    sqlite3 *pSrc;
9090    sqlite3_backup *pBackup;
9091    int nTimeout = 0;
9092
9093    if( nArg==2 ){
9094      zSrcFile = azArg[1];
9095      zDb = "main";
9096    }else if( nArg==3 ){
9097      zSrcFile = azArg[2];
9098      zDb = azArg[1];
9099    }else{
9100      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9101      rc = 1;
9102      goto meta_command_exit;
9103    }
9104    rc = sqlite3_open(zSrcFile, &pSrc);
9105    if( rc!=SQLITE_OK ){
9106      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9107      close_db(pSrc);
9108      return 1;
9109    }
9110    open_db(p, 0);
9111    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9112    if( pBackup==0 ){
9113      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9114      close_db(pSrc);
9115      return 1;
9116    }
9117    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9118          || rc==SQLITE_BUSY  ){
9119      if( rc==SQLITE_BUSY ){
9120        if( nTimeout++ >= 3 ) break;
9121        sqlite3_sleep(100);
9122      }
9123    }
9124    sqlite3_backup_finish(pBackup);
9125    if( rc==SQLITE_DONE ){
9126      rc = 0;
9127    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9128      raw_printf(stderr, "Error: source database is busy\n");
9129      rc = 1;
9130    }else{
9131      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9132      rc = 1;
9133    }
9134    close_db(pSrc);
9135  }else
9136
9137  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9138    if( nArg==2 ){
9139      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9140#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9141      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9142#endif
9143    }else{
9144      raw_printf(stderr, "Usage: .scanstats on|off\n");
9145      rc = 1;
9146    }
9147  }else
9148
9149  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9150    ShellText sSelect;
9151    ShellState data;
9152    char *zErrMsg = 0;
9153    const char *zDiv = "(";
9154    const char *zName = 0;
9155    int iSchema = 0;
9156    int bDebug = 0;
9157    int bNoSystemTabs = 0;
9158    int ii;
9159
9160    open_db(p, 0);
9161    memcpy(&data, p, sizeof(data));
9162    data.showHeader = 0;
9163    data.cMode = data.mode = MODE_Semi;
9164    initText(&sSelect);
9165    for(ii=1; ii<nArg; ii++){
9166      if( optionMatch(azArg[ii],"indent") ){
9167        data.cMode = data.mode = MODE_Pretty;
9168      }else if( optionMatch(azArg[ii],"debug") ){
9169        bDebug = 1;
9170      }else if( optionMatch(azArg[ii],"nosys") ){
9171        bNoSystemTabs = 1;
9172      }else if( azArg[ii][0]=='-' ){
9173        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9174        rc = 1;
9175        goto meta_command_exit;
9176      }else if( zName==0 ){
9177        zName = azArg[ii];
9178      }else{
9179        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9180        rc = 1;
9181        goto meta_command_exit;
9182      }
9183    }
9184    if( zName!=0 ){
9185      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9186                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9187                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9188                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9189      if( isSchema ){
9190        char *new_argv[2], *new_colv[2];
9191        new_argv[0] = sqlite3_mprintf(
9192                      "CREATE TABLE %s (\n"
9193                      "  type text,\n"
9194                      "  name text,\n"
9195                      "  tbl_name text,\n"
9196                      "  rootpage integer,\n"
9197                      "  sql text\n"
9198                      ")", zName);
9199        new_argv[1] = 0;
9200        new_colv[0] = "sql";
9201        new_colv[1] = 0;
9202        callback(&data, 1, new_argv, new_colv);
9203        sqlite3_free(new_argv[0]);
9204      }
9205    }
9206    if( zDiv ){
9207      sqlite3_stmt *pStmt = 0;
9208      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9209                              -1, &pStmt, 0);
9210      if( rc ){
9211        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9212        sqlite3_finalize(pStmt);
9213        rc = 1;
9214        goto meta_command_exit;
9215      }
9216      appendText(&sSelect, "SELECT sql FROM", 0);
9217      iSchema = 0;
9218      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9219        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9220        char zScNum[30];
9221        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9222        appendText(&sSelect, zDiv, 0);
9223        zDiv = " UNION ALL ";
9224        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9225        if( sqlite3_stricmp(zDb, "main")!=0 ){
9226          appendText(&sSelect, zDb, '\'');
9227        }else{
9228          appendText(&sSelect, "NULL", 0);
9229        }
9230        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9231        appendText(&sSelect, zScNum, 0);
9232        appendText(&sSelect, " AS snum, ", 0);
9233        appendText(&sSelect, zDb, '\'');
9234        appendText(&sSelect, " AS sname FROM ", 0);
9235        appendText(&sSelect, zDb, quoteChar(zDb));
9236        appendText(&sSelect, ".sqlite_schema", 0);
9237      }
9238      sqlite3_finalize(pStmt);
9239#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9240      if( zName ){
9241        appendText(&sSelect,
9242           " UNION ALL SELECT shell_module_schema(name),"
9243           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9244        0);
9245      }
9246#endif
9247      appendText(&sSelect, ") WHERE ", 0);
9248      if( zName ){
9249        char *zQarg = sqlite3_mprintf("%Q", zName);
9250        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9251                    strchr(zName, '[') != 0;
9252        if( strchr(zName, '.') ){
9253          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9254        }else{
9255          appendText(&sSelect, "lower(tbl_name)", 0);
9256        }
9257        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9258        appendText(&sSelect, zQarg, 0);
9259        if( !bGlob ){
9260          appendText(&sSelect, " ESCAPE '\\' ", 0);
9261        }
9262        appendText(&sSelect, " AND ", 0);
9263        sqlite3_free(zQarg);
9264      }
9265      if( bNoSystemTabs ){
9266        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9267      }
9268      appendText(&sSelect, "sql IS NOT NULL"
9269                           " ORDER BY snum, rowid", 0);
9270      if( bDebug ){
9271        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9272      }else{
9273        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9274      }
9275      freeText(&sSelect);
9276    }
9277    if( zErrMsg ){
9278      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9279      sqlite3_free(zErrMsg);
9280      rc = 1;
9281    }else if( rc != SQLITE_OK ){
9282      raw_printf(stderr,"Error: querying schema information\n");
9283      rc = 1;
9284    }else{
9285      rc = 0;
9286    }
9287  }else
9288
9289  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9290    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
9291    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
9292  }else
9293
9294#if defined(SQLITE_ENABLE_SESSION)
9295  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9296    OpenSession *pSession = &p->aSession[0];
9297    char **azCmd = &azArg[1];
9298    int iSes = 0;
9299    int nCmd = nArg - 1;
9300    int i;
9301    if( nArg<=1 ) goto session_syntax_error;
9302    open_db(p, 0);
9303    if( nArg>=3 ){
9304      for(iSes=0; iSes<p->nSession; iSes++){
9305        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
9306      }
9307      if( iSes<p->nSession ){
9308        pSession = &p->aSession[iSes];
9309        azCmd++;
9310        nCmd--;
9311      }else{
9312        pSession = &p->aSession[0];
9313        iSes = 0;
9314      }
9315    }
9316
9317    /* .session attach TABLE
9318    ** Invoke the sqlite3session_attach() interface to attach a particular
9319    ** table so that it is never filtered.
9320    */
9321    if( strcmp(azCmd[0],"attach")==0 ){
9322      if( nCmd!=2 ) goto session_syntax_error;
9323      if( pSession->p==0 ){
9324        session_not_open:
9325        raw_printf(stderr, "ERROR: No sessions are open\n");
9326      }else{
9327        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9328        if( rc ){
9329          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9330          rc = 0;
9331        }
9332      }
9333    }else
9334
9335    /* .session changeset FILE
9336    ** .session patchset FILE
9337    ** Write a changeset or patchset into a file.  The file is overwritten.
9338    */
9339    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9340      FILE *out = 0;
9341      if( nCmd!=2 ) goto session_syntax_error;
9342      if( pSession->p==0 ) goto session_not_open;
9343      out = fopen(azCmd[1], "wb");
9344      if( out==0 ){
9345        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9346                    azCmd[1]);
9347      }else{
9348        int szChng;
9349        void *pChng;
9350        if( azCmd[0][0]=='c' ){
9351          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9352        }else{
9353          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9354        }
9355        if( rc ){
9356          printf("Error: error code %d\n", rc);
9357          rc = 0;
9358        }
9359        if( pChng
9360          && fwrite(pChng, szChng, 1, out)!=1 ){
9361          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9362                  szChng);
9363        }
9364        sqlite3_free(pChng);
9365        fclose(out);
9366      }
9367    }else
9368
9369    /* .session close
9370    ** Close the identified session
9371    */
9372    if( strcmp(azCmd[0], "close")==0 ){
9373      if( nCmd!=1 ) goto session_syntax_error;
9374      if( p->nSession ){
9375        session_close(pSession);
9376        p->aSession[iSes] = p->aSession[--p->nSession];
9377      }
9378    }else
9379
9380    /* .session enable ?BOOLEAN?
9381    ** Query or set the enable flag
9382    */
9383    if( strcmp(azCmd[0], "enable")==0 ){
9384      int ii;
9385      if( nCmd>2 ) goto session_syntax_error;
9386      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9387      if( p->nSession ){
9388        ii = sqlite3session_enable(pSession->p, ii);
9389        utf8_printf(p->out, "session %s enable flag = %d\n",
9390                    pSession->zName, ii);
9391      }
9392    }else
9393
9394    /* .session filter GLOB ....
9395    ** Set a list of GLOB patterns of table names to be excluded.
9396    */
9397    if( strcmp(azCmd[0], "filter")==0 ){
9398      int ii, nByte;
9399      if( nCmd<2 ) goto session_syntax_error;
9400      if( p->nSession ){
9401        for(ii=0; ii<pSession->nFilter; ii++){
9402          sqlite3_free(pSession->azFilter[ii]);
9403        }
9404        sqlite3_free(pSession->azFilter);
9405        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9406        pSession->azFilter = sqlite3_malloc( nByte );
9407        if( pSession->azFilter==0 ){
9408          raw_printf(stderr, "Error: out or memory\n");
9409          exit(1);
9410        }
9411        for(ii=1; ii<nCmd; ii++){
9412          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9413        }
9414        pSession->nFilter = ii-1;
9415      }
9416    }else
9417
9418    /* .session indirect ?BOOLEAN?
9419    ** Query or set the indirect flag
9420    */
9421    if( strcmp(azCmd[0], "indirect")==0 ){
9422      int ii;
9423      if( nCmd>2 ) goto session_syntax_error;
9424      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9425      if( p->nSession ){
9426        ii = sqlite3session_indirect(pSession->p, ii);
9427        utf8_printf(p->out, "session %s indirect flag = %d\n",
9428                    pSession->zName, ii);
9429      }
9430    }else
9431
9432    /* .session isempty
9433    ** Determine if the session is empty
9434    */
9435    if( strcmp(azCmd[0], "isempty")==0 ){
9436      int ii;
9437      if( nCmd!=1 ) goto session_syntax_error;
9438      if( p->nSession ){
9439        ii = sqlite3session_isempty(pSession->p);
9440        utf8_printf(p->out, "session %s isempty flag = %d\n",
9441                    pSession->zName, ii);
9442      }
9443    }else
9444
9445    /* .session list
9446    ** List all currently open sessions
9447    */
9448    if( strcmp(azCmd[0],"list")==0 ){
9449      for(i=0; i<p->nSession; i++){
9450        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
9451      }
9452    }else
9453
9454    /* .session open DB NAME
9455    ** Open a new session called NAME on the attached database DB.
9456    ** DB is normally "main".
9457    */
9458    if( strcmp(azCmd[0],"open")==0 ){
9459      char *zName;
9460      if( nCmd!=3 ) goto session_syntax_error;
9461      zName = azCmd[2];
9462      if( zName[0]==0 ) goto session_syntax_error;
9463      for(i=0; i<p->nSession; i++){
9464        if( strcmp(p->aSession[i].zName,zName)==0 ){
9465          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9466          goto meta_command_exit;
9467        }
9468      }
9469      if( p->nSession>=ArraySize(p->aSession) ){
9470        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9471        goto meta_command_exit;
9472      }
9473      pSession = &p->aSession[p->nSession];
9474      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9475      if( rc ){
9476        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9477        rc = 0;
9478        goto meta_command_exit;
9479      }
9480      pSession->nFilter = 0;
9481      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9482      p->nSession++;
9483      pSession->zName = sqlite3_mprintf("%s", zName);
9484    }else
9485    /* If no command name matches, show a syntax error */
9486    session_syntax_error:
9487    showHelp(p->out, "session");
9488  }else
9489#endif
9490
9491#ifdef SQLITE_DEBUG
9492  /* Undocumented commands for internal testing.  Subject to change
9493  ** without notice. */
9494  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9495    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9496      int i, v;
9497      for(i=1; i<nArg; i++){
9498        v = booleanValue(azArg[i]);
9499        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9500      }
9501    }
9502    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9503      int i; sqlite3_int64 v;
9504      for(i=1; i<nArg; i++){
9505        char zBuf[200];
9506        v = integerValue(azArg[i]);
9507        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9508        utf8_printf(p->out, "%s", zBuf);
9509      }
9510    }
9511  }else
9512#endif
9513
9514  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9515    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9516    int bVerbose = 0;        /* Verbose output */
9517    int bSelftestExists;     /* True if SELFTEST already exists */
9518    int i, k;                /* Loop counters */
9519    int nTest = 0;           /* Number of tests runs */
9520    int nErr = 0;            /* Number of errors seen */
9521    ShellText str;           /* Answer for a query */
9522    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9523
9524    open_db(p,0);
9525    for(i=1; i<nArg; i++){
9526      const char *z = azArg[i];
9527      if( z[0]=='-' && z[1]=='-' ) z++;
9528      if( strcmp(z,"-init")==0 ){
9529        bIsInit = 1;
9530      }else
9531      if( strcmp(z,"-v")==0 ){
9532        bVerbose++;
9533      }else
9534      {
9535        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9536                    azArg[i], azArg[0]);
9537        raw_printf(stderr, "Should be one of: --init -v\n");
9538        rc = 1;
9539        goto meta_command_exit;
9540      }
9541    }
9542    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9543           != SQLITE_OK ){
9544      bSelftestExists = 0;
9545    }else{
9546      bSelftestExists = 1;
9547    }
9548    if( bIsInit ){
9549      createSelftestTable(p);
9550      bSelftestExists = 1;
9551    }
9552    initText(&str);
9553    appendText(&str, "x", 0);
9554    for(k=bSelftestExists; k>=0; k--){
9555      if( k==1 ){
9556        rc = sqlite3_prepare_v2(p->db,
9557            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9558            -1, &pStmt, 0);
9559      }else{
9560        rc = sqlite3_prepare_v2(p->db,
9561          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9562          "      (1,'run','PRAGMA integrity_check','ok')",
9563          -1, &pStmt, 0);
9564      }
9565      if( rc ){
9566        raw_printf(stderr, "Error querying the selftest table\n");
9567        rc = 1;
9568        sqlite3_finalize(pStmt);
9569        goto meta_command_exit;
9570      }
9571      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9572        int tno = sqlite3_column_int(pStmt, 0);
9573        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9574        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9575        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9576
9577        k = 0;
9578        if( bVerbose>0 ){
9579          char *zQuote = sqlite3_mprintf("%q", zSql);
9580          printf("%d: %s %s\n", tno, zOp, zSql);
9581          sqlite3_free(zQuote);
9582        }
9583        if( strcmp(zOp,"memo")==0 ){
9584          utf8_printf(p->out, "%s\n", zSql);
9585        }else
9586        if( strcmp(zOp,"run")==0 ){
9587          char *zErrMsg = 0;
9588          str.n = 0;
9589          str.z[0] = 0;
9590          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9591          nTest++;
9592          if( bVerbose ){
9593            utf8_printf(p->out, "Result: %s\n", str.z);
9594          }
9595          if( rc || zErrMsg ){
9596            nErr++;
9597            rc = 1;
9598            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9599            sqlite3_free(zErrMsg);
9600          }else if( strcmp(zAns,str.z)!=0 ){
9601            nErr++;
9602            rc = 1;
9603            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9604            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9605          }
9606        }else
9607        {
9608          utf8_printf(stderr,
9609            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9610          rc = 1;
9611          break;
9612        }
9613      } /* End loop over rows of content from SELFTEST */
9614      sqlite3_finalize(pStmt);
9615    } /* End loop over k */
9616    freeText(&str);
9617    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9618  }else
9619
9620  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9621    if( nArg<2 || nArg>3 ){
9622      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9623      rc = 1;
9624    }
9625    if( nArg>=2 ){
9626      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9627                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9628    }
9629    if( nArg>=3 ){
9630      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9631                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9632    }
9633  }else
9634
9635  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9636    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9637    int i;                   /* Loop counter */
9638    int bSchema = 0;         /* Also hash the schema */
9639    int bSeparate = 0;       /* Hash each table separately */
9640    int iSize = 224;         /* Hash algorithm to use */
9641    int bDebug = 0;          /* Only show the query that would have run */
9642    sqlite3_stmt *pStmt;     /* For querying tables names */
9643    char *zSql;              /* SQL to be run */
9644    char *zSep;              /* Separator */
9645    ShellText sSql;          /* Complete SQL for the query to run the hash */
9646    ShellText sQuery;        /* Set of queries used to read all content */
9647    open_db(p, 0);
9648    for(i=1; i<nArg; i++){
9649      const char *z = azArg[i];
9650      if( z[0]=='-' ){
9651        z++;
9652        if( z[0]=='-' ) z++;
9653        if( strcmp(z,"schema")==0 ){
9654          bSchema = 1;
9655        }else
9656        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9657         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9658        ){
9659          iSize = atoi(&z[5]);
9660        }else
9661        if( strcmp(z,"debug")==0 ){
9662          bDebug = 1;
9663        }else
9664        {
9665          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9666                      azArg[i], azArg[0]);
9667          showHelp(p->out, azArg[0]);
9668          rc = 1;
9669          goto meta_command_exit;
9670        }
9671      }else if( zLike ){
9672        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9673        rc = 1;
9674        goto meta_command_exit;
9675      }else{
9676        zLike = z;
9677        bSeparate = 1;
9678        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9679      }
9680    }
9681    if( bSchema ){
9682      zSql = "SELECT lower(name) FROM sqlite_schema"
9683             " WHERE type='table' AND coalesce(rootpage,0)>1"
9684             " UNION ALL SELECT 'sqlite_schema'"
9685             " ORDER BY 1 collate nocase";
9686    }else{
9687      zSql = "SELECT lower(name) FROM sqlite_schema"
9688             " WHERE type='table' AND coalesce(rootpage,0)>1"
9689             " AND name NOT LIKE 'sqlite_%'"
9690             " ORDER BY 1 collate nocase";
9691    }
9692    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9693    initText(&sQuery);
9694    initText(&sSql);
9695    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9696    zSep = "VALUES(";
9697    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9698      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9699      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9700      if( strncmp(zTab, "sqlite_",7)!=0 ){
9701        appendText(&sQuery,"SELECT * FROM ", 0);
9702        appendText(&sQuery,zTab,'"');
9703        appendText(&sQuery," NOT INDEXED;", 0);
9704      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9705        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9706                           " ORDER BY name;", 0);
9707      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9708        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9709                           " ORDER BY name;", 0);
9710      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9711        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9712                           " ORDER BY tbl,idx;", 0);
9713      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9714        appendText(&sQuery, "SELECT * FROM ", 0);
9715        appendText(&sQuery, zTab, 0);
9716        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9717      }
9718      appendText(&sSql, zSep, 0);
9719      appendText(&sSql, sQuery.z, '\'');
9720      sQuery.n = 0;
9721      appendText(&sSql, ",", 0);
9722      appendText(&sSql, zTab, '\'');
9723      zSep = "),(";
9724    }
9725    sqlite3_finalize(pStmt);
9726    if( bSeparate ){
9727      zSql = sqlite3_mprintf(
9728          "%s))"
9729          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9730          "   FROM [sha3sum$query]",
9731          sSql.z, iSize);
9732    }else{
9733      zSql = sqlite3_mprintf(
9734          "%s))"
9735          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9736          "   FROM [sha3sum$query]",
9737          sSql.z, iSize);
9738    }
9739    freeText(&sQuery);
9740    freeText(&sSql);
9741    if( bDebug ){
9742      utf8_printf(p->out, "%s\n", zSql);
9743    }else{
9744      shell_exec(p, zSql, 0);
9745    }
9746    sqlite3_free(zSql);
9747  }else
9748
9749#ifndef SQLITE_NOHAVE_SYSTEM
9750  if( c=='s'
9751   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9752  ){
9753    char *zCmd;
9754    int i, x;
9755    if( nArg<2 ){
9756      raw_printf(stderr, "Usage: .system COMMAND\n");
9757      rc = 1;
9758      goto meta_command_exit;
9759    }
9760    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9761    for(i=2; i<nArg; i++){
9762      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9763                             zCmd, azArg[i]);
9764    }
9765    x = system(zCmd);
9766    sqlite3_free(zCmd);
9767    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9768  }else
9769#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9770
9771  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9772    static const char *azBool[] = { "off", "on", "trigger", "full"};
9773    const char *zOut;
9774    int i;
9775    if( nArg!=1 ){
9776      raw_printf(stderr, "Usage: .show\n");
9777      rc = 1;
9778      goto meta_command_exit;
9779    }
9780    utf8_printf(p->out, "%12.12s: %s\n","echo",
9781                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9782    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9783    utf8_printf(p->out, "%12.12s: %s\n","explain",
9784         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9785    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9786    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9787    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9788      output_c_string(p->out, p->nullValue);
9789      raw_printf(p->out, "\n");
9790    utf8_printf(p->out,"%12.12s: %s\n","output",
9791            strlen30(p->outfile) ? p->outfile : "stdout");
9792    utf8_printf(p->out,"%12.12s: ", "colseparator");
9793      output_c_string(p->out, p->colSeparator);
9794      raw_printf(p->out, "\n");
9795    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9796      output_c_string(p->out, p->rowSeparator);
9797      raw_printf(p->out, "\n");
9798    switch( p->statsOn ){
9799      case 0:  zOut = "off";     break;
9800      default: zOut = "on";      break;
9801      case 2:  zOut = "stmt";    break;
9802      case 3:  zOut = "vmstep";  break;
9803    }
9804    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
9805    utf8_printf(p->out, "%12.12s: ", "width");
9806    for (i=0;i<p->nWidth;i++) {
9807      raw_printf(p->out, "%d ", p->colWidth[i]);
9808    }
9809    raw_printf(p->out, "\n");
9810    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9811                p->zDbFilename ? p->zDbFilename : "");
9812  }else
9813
9814  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9815    if( nArg==2 ){
9816      if( strcmp(azArg[1],"stmt")==0 ){
9817        p->statsOn = 2;
9818      }else if( strcmp(azArg[1],"vmstep")==0 ){
9819        p->statsOn = 3;
9820      }else{
9821        p->statsOn = (u8)booleanValue(azArg[1]);
9822      }
9823    }else if( nArg==1 ){
9824      display_stats(p->db, p, 0);
9825    }else{
9826      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
9827      rc = 1;
9828    }
9829  }else
9830
9831  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9832   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9833                 || strncmp(azArg[0], "indexes", n)==0) )
9834  ){
9835    sqlite3_stmt *pStmt;
9836    char **azResult;
9837    int nRow, nAlloc;
9838    int ii;
9839    ShellText s;
9840    initText(&s);
9841    open_db(p, 0);
9842    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9843    if( rc ){
9844      sqlite3_finalize(pStmt);
9845      return shellDatabaseError(p->db);
9846    }
9847
9848    if( nArg>2 && c=='i' ){
9849      /* It is an historical accident that the .indexes command shows an error
9850      ** when called with the wrong number of arguments whereas the .tables
9851      ** command does not. */
9852      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9853      rc = 1;
9854      sqlite3_finalize(pStmt);
9855      goto meta_command_exit;
9856    }
9857    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9858      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9859      if( zDbName==0 ) continue;
9860      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9861      if( sqlite3_stricmp(zDbName, "main")==0 ){
9862        appendText(&s, "SELECT name FROM ", 0);
9863      }else{
9864        appendText(&s, "SELECT ", 0);
9865        appendText(&s, zDbName, '\'');
9866        appendText(&s, "||'.'||name FROM ", 0);
9867      }
9868      appendText(&s, zDbName, '"');
9869      appendText(&s, ".sqlite_schema ", 0);
9870      if( c=='t' ){
9871        appendText(&s," WHERE type IN ('table','view')"
9872                      "   AND name NOT LIKE 'sqlite_%'"
9873                      "   AND name LIKE ?1", 0);
9874      }else{
9875        appendText(&s," WHERE type='index'"
9876                      "   AND tbl_name LIKE ?1", 0);
9877      }
9878    }
9879    rc = sqlite3_finalize(pStmt);
9880    appendText(&s, " ORDER BY 1", 0);
9881    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9882    freeText(&s);
9883    if( rc ) return shellDatabaseError(p->db);
9884
9885    /* Run the SQL statement prepared by the above block. Store the results
9886    ** as an array of nul-terminated strings in azResult[].  */
9887    nRow = nAlloc = 0;
9888    azResult = 0;
9889    if( nArg>1 ){
9890      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9891    }else{
9892      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9893    }
9894    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9895      if( nRow>=nAlloc ){
9896        char **azNew;
9897        int n2 = nAlloc*2 + 10;
9898        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9899        if( azNew==0 ) shell_out_of_memory();
9900        nAlloc = n2;
9901        azResult = azNew;
9902      }
9903      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9904      if( 0==azResult[nRow] ) shell_out_of_memory();
9905      nRow++;
9906    }
9907    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9908      rc = shellDatabaseError(p->db);
9909    }
9910
9911    /* Pretty-print the contents of array azResult[] to the output */
9912    if( rc==0 && nRow>0 ){
9913      int len, maxlen = 0;
9914      int i, j;
9915      int nPrintCol, nPrintRow;
9916      for(i=0; i<nRow; i++){
9917        len = strlen30(azResult[i]);
9918        if( len>maxlen ) maxlen = len;
9919      }
9920      nPrintCol = 80/(maxlen+2);
9921      if( nPrintCol<1 ) nPrintCol = 1;
9922      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9923      for(i=0; i<nPrintRow; i++){
9924        for(j=i; j<nRow; j+=nPrintRow){
9925          char *zSp = j<nPrintRow ? "" : "  ";
9926          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9927                      azResult[j] ? azResult[j]:"");
9928        }
9929        raw_printf(p->out, "\n");
9930      }
9931    }
9932
9933    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9934    sqlite3_free(azResult);
9935  }else
9936
9937  /* Begin redirecting output to the file "testcase-out.txt" */
9938  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9939    output_reset(p);
9940    p->out = output_file_open("testcase-out.txt", 0);
9941    if( p->out==0 ){
9942      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9943    }
9944    if( nArg>=2 ){
9945      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9946    }else{
9947      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9948    }
9949  }else
9950
9951#ifndef SQLITE_UNTESTABLE
9952  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9953    static const struct {
9954       const char *zCtrlName;   /* Name of a test-control option */
9955       int ctrlCode;            /* Integer code for that option */
9956       const char *zUsage;      /* Usage notes */
9957    } aCtrl[] = {
9958      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9959      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9960    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9961    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9962      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9963      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9964    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9965      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9966      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9967      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9968      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9969      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9970#ifdef YYCOVERAGE
9971      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9972#endif
9973      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9974      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9975      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9976      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9977      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
9978      { "tune",               SQLITE_TESTCTRL_TUNE,          "ID VALUE"       },
9979    };
9980    int testctrl = -1;
9981    int iCtrl = -1;
9982    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9983    int isOk = 0;
9984    int i, n2;
9985    const char *zCmd = 0;
9986
9987    open_db(p, 0);
9988    zCmd = nArg>=2 ? azArg[1] : "help";
9989
9990    /* The argument can optionally begin with "-" or "--" */
9991    if( zCmd[0]=='-' && zCmd[1] ){
9992      zCmd++;
9993      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9994    }
9995
9996    /* --help lists all test-controls */
9997    if( strcmp(zCmd,"help")==0 ){
9998      utf8_printf(p->out, "Available test-controls:\n");
9999      for(i=0; i<ArraySize(aCtrl); i++){
10000        utf8_printf(p->out, "  .testctrl %s %s\n",
10001                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10002      }
10003      rc = 1;
10004      goto meta_command_exit;
10005    }
10006
10007    /* convert testctrl text option to value. allow any unique prefix
10008    ** of the option name, or a numerical value. */
10009    n2 = strlen30(zCmd);
10010    for(i=0; i<ArraySize(aCtrl); i++){
10011      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10012        if( testctrl<0 ){
10013          testctrl = aCtrl[i].ctrlCode;
10014          iCtrl = i;
10015        }else{
10016          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
10017                              "Use \".testctrl --help\" for help\n", zCmd);
10018          rc = 1;
10019          goto meta_command_exit;
10020        }
10021      }
10022    }
10023    if( testctrl<0 ){
10024      utf8_printf(stderr,"Error: unknown test-control: %s\n"
10025                         "Use \".testctrl --help\" for help\n", zCmd);
10026    }else{
10027      switch(testctrl){
10028
10029        /* sqlite3_test_control(int, db, int) */
10030        case SQLITE_TESTCTRL_OPTIMIZATIONS:
10031          if( nArg==3 ){
10032            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
10033            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10034            isOk = 3;
10035          }
10036          break;
10037
10038        /* sqlite3_test_control(int) */
10039        case SQLITE_TESTCTRL_PRNG_SAVE:
10040        case SQLITE_TESTCTRL_PRNG_RESTORE:
10041        case SQLITE_TESTCTRL_BYTEORDER:
10042          if( nArg==2 ){
10043            rc2 = sqlite3_test_control(testctrl);
10044            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
10045          }
10046          break;
10047
10048        /* sqlite3_test_control(int, uint) */
10049        case SQLITE_TESTCTRL_PENDING_BYTE:
10050          if( nArg==3 ){
10051            unsigned int opt = (unsigned int)integerValue(azArg[2]);
10052            rc2 = sqlite3_test_control(testctrl, opt);
10053            isOk = 3;
10054          }
10055          break;
10056
10057        /* sqlite3_test_control(int, int, sqlite3*) */
10058        case SQLITE_TESTCTRL_PRNG_SEED:
10059          if( nArg==3 || nArg==4 ){
10060            int ii = (int)integerValue(azArg[2]);
10061            sqlite3 *db;
10062            if( ii==0 && strcmp(azArg[2],"random")==0 ){
10063              sqlite3_randomness(sizeof(ii),&ii);
10064              printf("-- random seed: %d\n", ii);
10065            }
10066            if( nArg==3 ){
10067              db = 0;
10068            }else{
10069              db = p->db;
10070              /* Make sure the schema has been loaded */
10071              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10072            }
10073            rc2 = sqlite3_test_control(testctrl, ii, db);
10074            isOk = 3;
10075          }
10076          break;
10077
10078        /* sqlite3_test_control(int, int) */
10079        case SQLITE_TESTCTRL_ASSERT:
10080        case SQLITE_TESTCTRL_ALWAYS:
10081          if( nArg==3 ){
10082            int opt = booleanValue(azArg[2]);
10083            rc2 = sqlite3_test_control(testctrl, opt);
10084            isOk = 1;
10085          }
10086          break;
10087
10088        /* sqlite3_test_control(int, int) */
10089        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10090        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10091          if( nArg==3 ){
10092            int opt = booleanValue(azArg[2]);
10093            rc2 = sqlite3_test_control(testctrl, opt);
10094            isOk = 3;
10095          }
10096          break;
10097
10098        /* sqlite3_test_control(sqlite3*) */
10099        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10100          rc2 = sqlite3_test_control(testctrl, p->db);
10101          isOk = 3;
10102          break;
10103
10104        case SQLITE_TESTCTRL_IMPOSTER:
10105          if( nArg==5 ){
10106            rc2 = sqlite3_test_control(testctrl, p->db,
10107                          azArg[2],
10108                          integerValue(azArg[3]),
10109                          integerValue(azArg[4]));
10110            isOk = 3;
10111          }
10112          break;
10113
10114        case SQLITE_TESTCTRL_SEEK_COUNT: {
10115          u64 x = 0;
10116          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10117          utf8_printf(p->out, "%llu\n", x);
10118          isOk = 3;
10119          break;
10120        }
10121
10122#ifdef YYCOVERAGE
10123        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
10124          if( nArg==2 ){
10125            sqlite3_test_control(testctrl, p->out);
10126            isOk = 3;
10127          }
10128          break;
10129        }
10130#endif
10131#ifdef SQLITE_DEBUG
10132        case SQLITE_TESTCTRL_TUNE: {
10133          if( nArg==4 ){
10134            int id = (int)integerValue(azArg[2]);
10135            int val = (int)integerValue(azArg[3]);
10136            sqlite3_test_control(testctrl, id, &val);
10137            isOk = 3;
10138          }else if( nArg==3 ){
10139            int id = (int)integerValue(azArg[2]);
10140            sqlite3_test_control(testctrl, -id, &rc2);
10141            isOk = 1;
10142          }else if( nArg==2 ){
10143            int id = 1;
10144            while(1){
10145              int val = 0;
10146              rc2 = sqlite3_test_control(testctrl, -id, &val);
10147              if( rc2!=SQLITE_OK ) break;
10148              if( id>1 ) utf8_printf(p->out, "  ");
10149              utf8_printf(p->out, "%d: %d", id, val);
10150              id++;
10151            }
10152            if( id>1 ) utf8_printf(p->out, "\n");
10153            isOk = 3;
10154          }
10155          break;
10156        }
10157#endif
10158      }
10159    }
10160    if( isOk==0 && iCtrl>=0 ){
10161      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10162      rc = 1;
10163    }else if( isOk==1 ){
10164      raw_printf(p->out, "%d\n", rc2);
10165    }else if( isOk==2 ){
10166      raw_printf(p->out, "0x%08x\n", rc2);
10167    }
10168  }else
10169#endif /* !defined(SQLITE_UNTESTABLE) */
10170
10171  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10172    open_db(p, 0);
10173    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10174  }else
10175
10176  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10177    if( nArg==2 ){
10178      enableTimer = booleanValue(azArg[1]);
10179      if( enableTimer && !HAS_TIMER ){
10180        raw_printf(stderr, "Error: timer not available on this system.\n");
10181        enableTimer = 0;
10182      }
10183    }else{
10184      raw_printf(stderr, "Usage: .timer on|off\n");
10185      rc = 1;
10186    }
10187  }else
10188
10189#ifndef SQLITE_OMIT_TRACE
10190  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10191    int mType = 0;
10192    int jj;
10193    open_db(p, 0);
10194    for(jj=1; jj<nArg; jj++){
10195      const char *z = azArg[jj];
10196      if( z[0]=='-' ){
10197        if( optionMatch(z, "expanded") ){
10198          p->eTraceType = SHELL_TRACE_EXPANDED;
10199        }
10200#ifdef SQLITE_ENABLE_NORMALIZE
10201        else if( optionMatch(z, "normalized") ){
10202          p->eTraceType = SHELL_TRACE_NORMALIZED;
10203        }
10204#endif
10205        else if( optionMatch(z, "plain") ){
10206          p->eTraceType = SHELL_TRACE_PLAIN;
10207        }
10208        else if( optionMatch(z, "profile") ){
10209          mType |= SQLITE_TRACE_PROFILE;
10210        }
10211        else if( optionMatch(z, "row") ){
10212          mType |= SQLITE_TRACE_ROW;
10213        }
10214        else if( optionMatch(z, "stmt") ){
10215          mType |= SQLITE_TRACE_STMT;
10216        }
10217        else if( optionMatch(z, "close") ){
10218          mType |= SQLITE_TRACE_CLOSE;
10219        }
10220        else {
10221          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10222          rc = 1;
10223          goto meta_command_exit;
10224        }
10225      }else{
10226        output_file_close(p->traceOut);
10227        p->traceOut = output_file_open(azArg[1], 0);
10228      }
10229    }
10230    if( p->traceOut==0 ){
10231      sqlite3_trace_v2(p->db, 0, 0, 0);
10232    }else{
10233      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10234      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10235    }
10236  }else
10237#endif /* !defined(SQLITE_OMIT_TRACE) */
10238
10239#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10240  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10241    int ii;
10242    int lenOpt;
10243    char *zOpt;
10244    if( nArg<2 ){
10245      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10246      rc = 1;
10247      goto meta_command_exit;
10248    }
10249    open_db(p, 0);
10250    zOpt = azArg[1];
10251    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10252    lenOpt = (int)strlen(zOpt);
10253    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10254      assert( azArg[nArg]==0 );
10255      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10256    }else{
10257      for(ii=1; ii<nArg; ii++){
10258        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10259      }
10260    }
10261  }else
10262#endif
10263
10264#if SQLITE_USER_AUTHENTICATION
10265  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10266    if( nArg<2 ){
10267      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10268      rc = 1;
10269      goto meta_command_exit;
10270    }
10271    open_db(p, 0);
10272    if( strcmp(azArg[1],"login")==0 ){
10273      if( nArg!=4 ){
10274        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10275        rc = 1;
10276        goto meta_command_exit;
10277      }
10278      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10279                                     strlen30(azArg[3]));
10280      if( rc ){
10281        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10282        rc = 1;
10283      }
10284    }else if( strcmp(azArg[1],"add")==0 ){
10285      if( nArg!=5 ){
10286        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10287        rc = 1;
10288        goto meta_command_exit;
10289      }
10290      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10291                            booleanValue(azArg[4]));
10292      if( rc ){
10293        raw_printf(stderr, "User-Add failed: %d\n", rc);
10294        rc = 1;
10295      }
10296    }else if( strcmp(azArg[1],"edit")==0 ){
10297      if( nArg!=5 ){
10298        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10299        rc = 1;
10300        goto meta_command_exit;
10301      }
10302      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10303                              booleanValue(azArg[4]));
10304      if( rc ){
10305        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10306        rc = 1;
10307      }
10308    }else if( strcmp(azArg[1],"delete")==0 ){
10309      if( nArg!=3 ){
10310        raw_printf(stderr, "Usage: .user delete USER\n");
10311        rc = 1;
10312        goto meta_command_exit;
10313      }
10314      rc = sqlite3_user_delete(p->db, azArg[2]);
10315      if( rc ){
10316        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10317        rc = 1;
10318      }
10319    }else{
10320      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10321      rc = 1;
10322      goto meta_command_exit;
10323    }
10324  }else
10325#endif /* SQLITE_USER_AUTHENTICATION */
10326
10327  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10328    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10329        sqlite3_libversion(), sqlite3_sourceid());
10330#if SQLITE_HAVE_ZLIB
10331    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10332#endif
10333#define CTIMEOPT_VAL_(opt) #opt
10334#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10335#if defined(__clang__) && defined(__clang_major__)
10336    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10337                    CTIMEOPT_VAL(__clang_minor__) "."
10338                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10339#elif defined(_MSC_VER)
10340    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10341#elif defined(__GNUC__) && defined(__VERSION__)
10342    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10343#endif
10344  }else
10345
10346  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10347    const char *zDbName = nArg==2 ? azArg[1] : "main";
10348    sqlite3_vfs *pVfs = 0;
10349    if( p->db ){
10350      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10351      if( pVfs ){
10352        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10353        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10354        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10355        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10356      }
10357    }
10358  }else
10359
10360  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10361    sqlite3_vfs *pVfs;
10362    sqlite3_vfs *pCurrent = 0;
10363    if( p->db ){
10364      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10365    }
10366    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10367      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10368           pVfs==pCurrent ? "  <--- CURRENT" : "");
10369      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10370      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10371      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10372      if( pVfs->pNext ){
10373        raw_printf(p->out, "-----------------------------------\n");
10374      }
10375    }
10376  }else
10377
10378  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10379    const char *zDbName = nArg==2 ? azArg[1] : "main";
10380    char *zVfsName = 0;
10381    if( p->db ){
10382      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10383      if( zVfsName ){
10384        utf8_printf(p->out, "%s\n", zVfsName);
10385        sqlite3_free(zVfsName);
10386      }
10387    }
10388  }else
10389
10390  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10391    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10392    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
10393  }else
10394
10395  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10396    int j;
10397    assert( nArg<=ArraySize(azArg) );
10398    p->nWidth = nArg-1;
10399    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10400    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10401    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10402    for(j=1; j<nArg; j++){
10403      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10404    }
10405  }else
10406
10407  {
10408    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10409      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10410    rc = 1;
10411  }
10412
10413meta_command_exit:
10414  if( p->outCount ){
10415    p->outCount--;
10416    if( p->outCount==0 ) output_reset(p);
10417  }
10418  return rc;
10419}
10420
10421/*
10422** Return TRUE if a semicolon occurs anywhere in the first N characters
10423** of string z[].
10424*/
10425static int line_contains_semicolon(const char *z, int N){
10426  int i;
10427  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
10428  return 0;
10429}
10430
10431/*
10432** Test to see if a line consists entirely of whitespace.
10433*/
10434static int _all_whitespace(const char *z){
10435  for(; *z; z++){
10436    if( IsSpace(z[0]) ) continue;
10437    if( *z=='/' && z[1]=='*' ){
10438      z += 2;
10439      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
10440      if( *z==0 ) return 0;
10441      z++;
10442      continue;
10443    }
10444    if( *z=='-' && z[1]=='-' ){
10445      z += 2;
10446      while( *z && *z!='\n' ){ z++; }
10447      if( *z==0 ) return 1;
10448      continue;
10449    }
10450    return 0;
10451  }
10452  return 1;
10453}
10454
10455/*
10456** Return TRUE if the line typed in is an SQL command terminator other
10457** than a semi-colon.  The SQL Server style "go" command is understood
10458** as is the Oracle "/".
10459*/
10460static int line_is_command_terminator(const char *zLine){
10461  while( IsSpace(zLine[0]) ){ zLine++; };
10462  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
10463    return 1;  /* Oracle */
10464  }
10465  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
10466         && _all_whitespace(&zLine[2]) ){
10467    return 1;  /* SQL Server */
10468  }
10469  return 0;
10470}
10471
10472/*
10473** We need a default sqlite3_complete() implementation to use in case
10474** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10475** any arbitrary text is a complete SQL statement.  This is not very
10476** user-friendly, but it does seem to work.
10477*/
10478#ifdef SQLITE_OMIT_COMPLETE
10479#define sqlite3_complete(x) 1
10480#endif
10481
10482/*
10483** Return true if zSql is a complete SQL statement.  Return false if it
10484** ends in the middle of a string literal or C-style comment.
10485*/
10486static int line_is_complete(char *zSql, int nSql){
10487  int rc;
10488  if( zSql==0 ) return 1;
10489  zSql[nSql] = ';';
10490  zSql[nSql+1] = 0;
10491  rc = sqlite3_complete(zSql);
10492  zSql[nSql] = 0;
10493  return rc;
10494}
10495
10496/*
10497** Run a single line of SQL.  Return the number of errors.
10498*/
10499static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10500  int rc;
10501  char *zErrMsg = 0;
10502
10503  open_db(p, 0);
10504  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10505  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10506  BEGIN_TIMER;
10507  rc = shell_exec(p, zSql, &zErrMsg);
10508  END_TIMER;
10509  if( rc || zErrMsg ){
10510    char zPrefix[100];
10511    if( in!=0 || !stdin_is_interactive ){
10512      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10513                       "Error: near line %d:", startline);
10514    }else{
10515      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10516    }
10517    if( zErrMsg!=0 ){
10518      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10519      sqlite3_free(zErrMsg);
10520      zErrMsg = 0;
10521    }else{
10522      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10523    }
10524    return 1;
10525  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10526    raw_printf(p->out, "changes: %3lld   total_changes: %lld\n",
10527            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
10528  }
10529  return 0;
10530}
10531
10532
10533/*
10534** Read input from *in and process it.  If *in==0 then input
10535** is interactive - the user is typing it it.  Otherwise, input
10536** is coming from a file or device.  A prompt is issued and history
10537** is saved only if input is interactive.  An interrupt signal will
10538** cause this routine to exit immediately, unless input is interactive.
10539**
10540** Return the number of errors.
10541*/
10542static int process_input(ShellState *p){
10543  char *zLine = 0;          /* A single input line */
10544  char *zSql = 0;           /* Accumulated SQL text */
10545  int nLine;                /* Length of current line */
10546  int nSql = 0;             /* Bytes of zSql[] used */
10547  int nAlloc = 0;           /* Allocated zSql[] space */
10548  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10549  int rc;                   /* Error code */
10550  int errCnt = 0;           /* Number of errors seen */
10551  int startline = 0;        /* Line number for start of current input */
10552
10553  p->lineno = 0;
10554  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10555    fflush(p->out);
10556    zLine = one_input_line(p->in, zLine, nSql>0);
10557    if( zLine==0 ){
10558      /* End of input */
10559      if( p->in==0 && stdin_is_interactive ) printf("\n");
10560      break;
10561    }
10562    if( seenInterrupt ){
10563      if( p->in!=0 ) break;
10564      seenInterrupt = 0;
10565    }
10566    p->lineno++;
10567    if( nSql==0 && _all_whitespace(zLine) ){
10568      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10569      continue;
10570    }
10571    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10572      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10573      if( zLine[0]=='.' ){
10574        rc = do_meta_command(zLine, p);
10575        if( rc==2 ){ /* exit requested */
10576          break;
10577        }else if( rc ){
10578          errCnt++;
10579        }
10580      }
10581      continue;
10582    }
10583    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10584      memcpy(zLine,";",2);
10585    }
10586    nLine = strlen30(zLine);
10587    if( nSql+nLine+2>=nAlloc ){
10588      nAlloc = nSql+nLine+100;
10589      zSql = realloc(zSql, nAlloc);
10590      if( zSql==0 ) shell_out_of_memory();
10591    }
10592    nSqlPrior = nSql;
10593    if( nSql==0 ){
10594      int i;
10595      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10596      assert( nAlloc>0 && zSql!=0 );
10597      memcpy(zSql, zLine+i, nLine+1-i);
10598      startline = p->lineno;
10599      nSql = nLine-i;
10600    }else{
10601      zSql[nSql++] = '\n';
10602      memcpy(zSql+nSql, zLine, nLine+1);
10603      nSql += nLine;
10604    }
10605    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10606                && sqlite3_complete(zSql) ){
10607      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10608      nSql = 0;
10609      if( p->outCount ){
10610        output_reset(p);
10611        p->outCount = 0;
10612      }else{
10613        clearTempFile(p);
10614      }
10615    }else if( nSql && _all_whitespace(zSql) ){
10616      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10617      nSql = 0;
10618    }
10619  }
10620  if( nSql && !_all_whitespace(zSql) ){
10621    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10622  }
10623  free(zSql);
10624  free(zLine);
10625  return errCnt>0;
10626}
10627
10628/*
10629** Return a pathname which is the user's home directory.  A
10630** 0 return indicates an error of some kind.
10631*/
10632static char *find_home_dir(int clearFlag){
10633  static char *home_dir = NULL;
10634  if( clearFlag ){
10635    free(home_dir);
10636    home_dir = 0;
10637    return 0;
10638  }
10639  if( home_dir ) return home_dir;
10640
10641#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10642     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10643  {
10644    struct passwd *pwent;
10645    uid_t uid = getuid();
10646    if( (pwent=getpwuid(uid)) != NULL) {
10647      home_dir = pwent->pw_dir;
10648    }
10649  }
10650#endif
10651
10652#if defined(_WIN32_WCE)
10653  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10654   */
10655  home_dir = "/";
10656#else
10657
10658#if defined(_WIN32) || defined(WIN32)
10659  if (!home_dir) {
10660    home_dir = getenv("USERPROFILE");
10661  }
10662#endif
10663
10664  if (!home_dir) {
10665    home_dir = getenv("HOME");
10666  }
10667
10668#if defined(_WIN32) || defined(WIN32)
10669  if (!home_dir) {
10670    char *zDrive, *zPath;
10671    int n;
10672    zDrive = getenv("HOMEDRIVE");
10673    zPath = getenv("HOMEPATH");
10674    if( zDrive && zPath ){
10675      n = strlen30(zDrive) + strlen30(zPath) + 1;
10676      home_dir = malloc( n );
10677      if( home_dir==0 ) return 0;
10678      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10679      return home_dir;
10680    }
10681    home_dir = "c:\\";
10682  }
10683#endif
10684
10685#endif /* !_WIN32_WCE */
10686
10687  if( home_dir ){
10688    int n = strlen30(home_dir) + 1;
10689    char *z = malloc( n );
10690    if( z ) memcpy(z, home_dir, n);
10691    home_dir = z;
10692  }
10693
10694  return home_dir;
10695}
10696
10697/*
10698** Read input from the file given by sqliterc_override.  Or if that
10699** parameter is NULL, take input from ~/.sqliterc
10700**
10701** Returns the number of errors.
10702*/
10703static void process_sqliterc(
10704  ShellState *p,                  /* Configuration data */
10705  const char *sqliterc_override   /* Name of config file. NULL to use default */
10706){
10707  char *home_dir = NULL;
10708  const char *sqliterc = sqliterc_override;
10709  char *zBuf = 0;
10710  FILE *inSaved = p->in;
10711  int savedLineno = p->lineno;
10712
10713  if (sqliterc == NULL) {
10714    home_dir = find_home_dir(0);
10715    if( home_dir==0 ){
10716      raw_printf(stderr, "-- warning: cannot find home directory;"
10717                      " cannot read ~/.sqliterc\n");
10718      return;
10719    }
10720    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10721    sqliterc = zBuf;
10722  }
10723  p->in = fopen(sqliterc,"rb");
10724  if( p->in ){
10725    if( stdin_is_interactive ){
10726      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10727    }
10728    if( process_input(p) && bail_on_error ) exit(1);
10729    fclose(p->in);
10730  }else if( sqliterc_override!=0 ){
10731    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
10732    if( bail_on_error ) exit(1);
10733  }
10734  p->in = inSaved;
10735  p->lineno = savedLineno;
10736  sqlite3_free(zBuf);
10737}
10738
10739/*
10740** Show available command line options
10741*/
10742static const char zOptions[] =
10743#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10744  "   -A ARGS...           run \".archive ARGS\" and exit\n"
10745#endif
10746  "   -append              append the database to the end of the file\n"
10747  "   -ascii               set output mode to 'ascii'\n"
10748  "   -bail                stop after hitting an error\n"
10749  "   -batch               force batch I/O\n"
10750  "   -box                 set output mode to 'box'\n"
10751  "   -column              set output mode to 'column'\n"
10752  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
10753  "   -csv                 set output mode to 'csv'\n"
10754#if !defined(SQLITE_OMIT_DESERIALIZE)
10755  "   -deserialize         open the database using sqlite3_deserialize()\n"
10756#endif
10757  "   -echo                print commands before execution\n"
10758  "   -init FILENAME       read/process named file\n"
10759  "   -[no]header          turn headers on or off\n"
10760#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10761  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
10762#endif
10763  "   -help                show this message\n"
10764  "   -html                set output mode to HTML\n"
10765  "   -interactive         force interactive I/O\n"
10766  "   -json                set output mode to 'json'\n"
10767  "   -line                set output mode to 'line'\n"
10768  "   -list                set output mode to 'list'\n"
10769  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
10770  "   -markdown            set output mode to 'markdown'\n"
10771#if !defined(SQLITE_OMIT_DESERIALIZE)
10772  "   -maxsize N           maximum size for a --deserialize database\n"
10773#endif
10774  "   -memtrace            trace all memory allocations and deallocations\n"
10775  "   -mmap N              default mmap size set to N\n"
10776#ifdef SQLITE_ENABLE_MULTIPLEX
10777  "   -multiplex           enable the multiplexor VFS\n"
10778#endif
10779  "   -newline SEP         set output row separator. Default: '\\n'\n"
10780  "   -nofollow            refuse to open symbolic links to database files\n"
10781  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
10782  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
10783  "   -quote               set output mode to 'quote'\n"
10784  "   -readonly            open the database read-only\n"
10785  "   -separator SEP       set output column separator. Default: '|'\n"
10786#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10787  "   -sorterref SIZE      sorter references threshold size\n"
10788#endif
10789  "   -stats               print memory stats before each finalize\n"
10790  "   -table               set output mode to 'table'\n"
10791  "   -tabs                set output mode to 'tabs'\n"
10792  "   -version             show SQLite version\n"
10793  "   -vfs NAME            use NAME as the default VFS\n"
10794#ifdef SQLITE_ENABLE_VFSTRACE
10795  "   -vfstrace            enable tracing of all VFS calls\n"
10796#endif
10797#ifdef SQLITE_HAVE_ZLIB
10798  "   -zip                 open the file as a ZIP Archive\n"
10799#endif
10800;
10801static void usage(int showDetail){
10802  utf8_printf(stderr,
10803      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10804      "FILENAME is the name of an SQLite database. A new database is created\n"
10805      "if the file does not previously exist.\n", Argv0);
10806  if( showDetail ){
10807    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10808  }else{
10809    raw_printf(stderr, "Use the -help option for additional information\n");
10810  }
10811  exit(1);
10812}
10813
10814/*
10815** Internal check:  Verify that the SQLite is uninitialized.  Print a
10816** error message if it is initialized.
10817*/
10818static void verify_uninitialized(void){
10819  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10820    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10821                        " initialization.\n");
10822  }
10823}
10824
10825/*
10826** Initialize the state information in data
10827*/
10828static void main_init(ShellState *data) {
10829  memset(data, 0, sizeof(*data));
10830  data->normalMode = data->cMode = data->mode = MODE_List;
10831  data->autoExplain = 1;
10832  memcpy(data->colSeparator,SEP_Column, 2);
10833  memcpy(data->rowSeparator,SEP_Row, 2);
10834  data->showHeader = 0;
10835  data->shellFlgs = SHFLG_Lookaside;
10836  verify_uninitialized();
10837  sqlite3_config(SQLITE_CONFIG_URI, 1);
10838  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10839  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10840  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10841  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10842}
10843
10844/*
10845** Output text to the console in a font that attracts extra attention.
10846*/
10847#ifdef _WIN32
10848static void printBold(const char *zText){
10849#if !SQLITE_OS_WINRT
10850  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10851  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10852  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10853  SetConsoleTextAttribute(out,
10854         FOREGROUND_RED|FOREGROUND_INTENSITY
10855  );
10856#endif
10857  printf("%s", zText);
10858#if !SQLITE_OS_WINRT
10859  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10860#endif
10861}
10862#else
10863static void printBold(const char *zText){
10864  printf("\033[1m%s\033[0m", zText);
10865}
10866#endif
10867
10868/*
10869** Get the argument to an --option.  Throw an error and die if no argument
10870** is available.
10871*/
10872static char *cmdline_option_value(int argc, char **argv, int i){
10873  if( i==argc ){
10874    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10875            argv[0], argv[argc-1]);
10876    exit(1);
10877  }
10878  return argv[i];
10879}
10880
10881#ifndef SQLITE_SHELL_IS_UTF8
10882#  if (defined(_WIN32) || defined(WIN32)) \
10883   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
10884#    define SQLITE_SHELL_IS_UTF8          (0)
10885#  else
10886#    define SQLITE_SHELL_IS_UTF8          (1)
10887#  endif
10888#endif
10889
10890#if SQLITE_SHELL_IS_UTF8
10891int SQLITE_CDECL main(int argc, char **argv){
10892#else
10893int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10894  char **argv;
10895#endif
10896  char *zErrMsg = 0;
10897  ShellState data;
10898  const char *zInitFile = 0;
10899  int i;
10900  int rc = 0;
10901  int warnInmemoryDb = 0;
10902  int readStdin = 1;
10903  int nCmd = 0;
10904  char **azCmd = 0;
10905  const char *zVfs = 0;           /* Value of -vfs command-line option */
10906#if !SQLITE_SHELL_IS_UTF8
10907  char **argvToFree = 0;
10908  int argcToFree = 0;
10909#endif
10910
10911  setBinaryMode(stdin, 0);
10912  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10913  stdin_is_interactive = isatty(0);
10914  stdout_is_console = isatty(1);
10915
10916#ifdef SQLITE_DEBUG
10917  registerOomSimulator();
10918#endif
10919
10920#if !defined(_WIN32_WCE)
10921  if( getenv("SQLITE_DEBUG_BREAK") ){
10922    if( isatty(0) && isatty(2) ){
10923      fprintf(stderr,
10924          "attach debugger to process %d and press any key to continue.\n",
10925          GETPID());
10926      fgetc(stdin);
10927    }else{
10928#if defined(_WIN32) || defined(WIN32)
10929#if SQLITE_OS_WINRT
10930      __debugbreak();
10931#else
10932      DebugBreak();
10933#endif
10934#elif defined(SIGTRAP)
10935      raise(SIGTRAP);
10936#endif
10937    }
10938  }
10939#endif
10940
10941#if USE_SYSTEM_SQLITE+0!=1
10942  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10943    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10944            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10945    exit(1);
10946  }
10947#endif
10948  main_init(&data);
10949
10950  /* On Windows, we must translate command-line arguments into UTF-8.
10951  ** The SQLite memory allocator subsystem has to be enabled in order to
10952  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10953  ** subsequent sqlite3_config() calls will work.  So copy all results into
10954  ** memory that does not come from the SQLite memory allocator.
10955  */
10956#if !SQLITE_SHELL_IS_UTF8
10957  sqlite3_initialize();
10958  argvToFree = malloc(sizeof(argv[0])*argc*2);
10959  argcToFree = argc;
10960  argv = argvToFree + argc;
10961  if( argv==0 ) shell_out_of_memory();
10962  for(i=0; i<argc; i++){
10963    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10964    int n;
10965    if( z==0 ) shell_out_of_memory();
10966    n = (int)strlen(z);
10967    argv[i] = malloc( n+1 );
10968    if( argv[i]==0 ) shell_out_of_memory();
10969    memcpy(argv[i], z, n+1);
10970    argvToFree[i] = argv[i];
10971    sqlite3_free(z);
10972  }
10973  sqlite3_shutdown();
10974#endif
10975
10976  assert( argc>=1 && argv && argv[0] );
10977  Argv0 = argv[0];
10978
10979  /* Make sure we have a valid signal handler early, before anything
10980  ** else is done.
10981  */
10982#ifdef SIGINT
10983  signal(SIGINT, interrupt_handler);
10984#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10985  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10986#endif
10987
10988#ifdef SQLITE_SHELL_DBNAME_PROC
10989  {
10990    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10991    ** of a C-function that will provide the name of the database file.  Use
10992    ** this compile-time option to embed this shell program in larger
10993    ** applications. */
10994    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10995    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10996    warnInmemoryDb = 0;
10997  }
10998#endif
10999
11000  /* Do an initial pass through the command-line argument to locate
11001  ** the name of the database file, the name of the initialization file,
11002  ** the size of the alternative malloc heap,
11003  ** and the first command to execute.
11004  */
11005  verify_uninitialized();
11006  for(i=1; i<argc; i++){
11007    char *z;
11008    z = argv[i];
11009    if( z[0]!='-' ){
11010      if( data.zDbFilename==0 ){
11011        data.zDbFilename = z;
11012      }else{
11013        /* Excesss arguments are interpreted as SQL (or dot-commands) and
11014        ** mean that nothing is read from stdin */
11015        readStdin = 0;
11016        nCmd++;
11017        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
11018        if( azCmd==0 ) shell_out_of_memory();
11019        azCmd[nCmd-1] = z;
11020      }
11021    }
11022    if( z[1]=='-' ) z++;
11023    if( strcmp(z,"-separator")==0
11024     || strcmp(z,"-nullvalue")==0
11025     || strcmp(z,"-newline")==0
11026     || strcmp(z,"-cmd")==0
11027    ){
11028      (void)cmdline_option_value(argc, argv, ++i);
11029    }else if( strcmp(z,"-init")==0 ){
11030      zInitFile = cmdline_option_value(argc, argv, ++i);
11031    }else if( strcmp(z,"-batch")==0 ){
11032      /* Need to check for batch mode here to so we can avoid printing
11033      ** informational messages (like from process_sqliterc) before
11034      ** we do the actual processing of arguments later in a second pass.
11035      */
11036      stdin_is_interactive = 0;
11037    }else if( strcmp(z,"-heap")==0 ){
11038#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11039      const char *zSize;
11040      sqlite3_int64 szHeap;
11041
11042      zSize = cmdline_option_value(argc, argv, ++i);
11043      szHeap = integerValue(zSize);
11044      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
11045      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
11046#else
11047      (void)cmdline_option_value(argc, argv, ++i);
11048#endif
11049    }else if( strcmp(z,"-pagecache")==0 ){
11050      sqlite3_int64 n, sz;
11051      sz = integerValue(cmdline_option_value(argc,argv,++i));
11052      if( sz>70000 ) sz = 70000;
11053      if( sz<0 ) sz = 0;
11054      n = integerValue(cmdline_option_value(argc,argv,++i));
11055      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
11056        n = 0xffffffffffffLL/sz;
11057      }
11058      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
11059                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
11060      data.shellFlgs |= SHFLG_Pagecache;
11061    }else if( strcmp(z,"-lookaside")==0 ){
11062      int n, sz;
11063      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
11064      if( sz<0 ) sz = 0;
11065      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11066      if( n<0 ) n = 0;
11067      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
11068      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
11069#ifdef SQLITE_ENABLE_VFSTRACE
11070    }else if( strcmp(z,"-vfstrace")==0 ){
11071      extern int vfstrace_register(
11072         const char *zTraceName,
11073         const char *zOldVfsName,
11074         int (*xOut)(const char*,void*),
11075         void *pOutArg,
11076         int makeDefault
11077      );
11078      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
11079#endif
11080#ifdef SQLITE_ENABLE_MULTIPLEX
11081    }else if( strcmp(z,"-multiplex")==0 ){
11082      extern int sqlite3_multiple_initialize(const char*,int);
11083      sqlite3_multiplex_initialize(0, 1);
11084#endif
11085    }else if( strcmp(z,"-mmap")==0 ){
11086      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11087      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
11088#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11089    }else if( strcmp(z,"-sorterref")==0 ){
11090      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11091      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11092#endif
11093    }else if( strcmp(z,"-vfs")==0 ){
11094      zVfs = cmdline_option_value(argc, argv, ++i);
11095#ifdef SQLITE_HAVE_ZLIB
11096    }else if( strcmp(z,"-zip")==0 ){
11097      data.openMode = SHELL_OPEN_ZIPFILE;
11098#endif
11099    }else if( strcmp(z,"-append")==0 ){
11100      data.openMode = SHELL_OPEN_APPENDVFS;
11101#ifndef SQLITE_OMIT_DESERIALIZE
11102    }else if( strcmp(z,"-deserialize")==0 ){
11103      data.openMode = SHELL_OPEN_DESERIALIZE;
11104    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11105      data.szMax = integerValue(argv[++i]);
11106#endif
11107    }else if( strcmp(z,"-readonly")==0 ){
11108      data.openMode = SHELL_OPEN_READONLY;
11109    }else if( strcmp(z,"-nofollow")==0 ){
11110      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11111#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11112    }else if( strncmp(z, "-A",2)==0 ){
11113      /* All remaining command-line arguments are passed to the ".archive"
11114      ** command, so ignore them */
11115      break;
11116#endif
11117    }else if( strcmp(z, "-memtrace")==0 ){
11118      sqlite3MemTraceActivate(stderr);
11119    }else if( strcmp(z,"-bail")==0 ){
11120      bail_on_error = 1;
11121    }
11122  }
11123  verify_uninitialized();
11124
11125
11126#ifdef SQLITE_SHELL_INIT_PROC
11127  {
11128    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11129    ** of a C-function that will perform initialization actions on SQLite that
11130    ** occur just before or after sqlite3_initialize(). Use this compile-time
11131    ** option to embed this shell program in larger applications. */
11132    extern void SQLITE_SHELL_INIT_PROC(void);
11133    SQLITE_SHELL_INIT_PROC();
11134  }
11135#else
11136  /* All the sqlite3_config() calls have now been made. So it is safe
11137  ** to call sqlite3_initialize() and process any command line -vfs option. */
11138  sqlite3_initialize();
11139#endif
11140
11141  if( zVfs ){
11142    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11143    if( pVfs ){
11144      sqlite3_vfs_register(pVfs, 1);
11145    }else{
11146      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11147      exit(1);
11148    }
11149  }
11150
11151  if( data.zDbFilename==0 ){
11152#ifndef SQLITE_OMIT_MEMORYDB
11153    data.zDbFilename = ":memory:";
11154    warnInmemoryDb = argc==1;
11155#else
11156    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11157    return 1;
11158#endif
11159  }
11160  data.out = stdout;
11161  sqlite3_appendvfs_init(0,0,0);
11162
11163  /* Go ahead and open the database file if it already exists.  If the
11164  ** file does not exist, delay opening it.  This prevents empty database
11165  ** files from being created if a user mistypes the database name argument
11166  ** to the sqlite command-line tool.
11167  */
11168  if( access(data.zDbFilename, 0)==0 ){
11169    open_db(&data, 0);
11170  }
11171
11172  /* Process the initialization file if there is one.  If no -init option
11173  ** is given on the command line, look for a file named ~/.sqliterc and
11174  ** try to process it.
11175  */
11176  process_sqliterc(&data,zInitFile);
11177
11178  /* Make a second pass through the command-line argument and set
11179  ** options.  This second pass is delayed until after the initialization
11180  ** file is processed so that the command-line arguments will override
11181  ** settings in the initialization file.
11182  */
11183  for(i=1; i<argc; i++){
11184    char *z = argv[i];
11185    if( z[0]!='-' ) continue;
11186    if( z[1]=='-' ){ z++; }
11187    if( strcmp(z,"-init")==0 ){
11188      i++;
11189    }else if( strcmp(z,"-html")==0 ){
11190      data.mode = MODE_Html;
11191    }else if( strcmp(z,"-list")==0 ){
11192      data.mode = MODE_List;
11193    }else if( strcmp(z,"-quote")==0 ){
11194      data.mode = MODE_Quote;
11195      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11196      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11197    }else if( strcmp(z,"-line")==0 ){
11198      data.mode = MODE_Line;
11199    }else if( strcmp(z,"-column")==0 ){
11200      data.mode = MODE_Column;
11201    }else if( strcmp(z,"-json")==0 ){
11202      data.mode = MODE_Json;
11203    }else if( strcmp(z,"-markdown")==0 ){
11204      data.mode = MODE_Markdown;
11205    }else if( strcmp(z,"-table")==0 ){
11206      data.mode = MODE_Table;
11207    }else if( strcmp(z,"-box")==0 ){
11208      data.mode = MODE_Box;
11209    }else if( strcmp(z,"-csv")==0 ){
11210      data.mode = MODE_Csv;
11211      memcpy(data.colSeparator,",",2);
11212#ifdef SQLITE_HAVE_ZLIB
11213    }else if( strcmp(z,"-zip")==0 ){
11214      data.openMode = SHELL_OPEN_ZIPFILE;
11215#endif
11216    }else if( strcmp(z,"-append")==0 ){
11217      data.openMode = SHELL_OPEN_APPENDVFS;
11218#ifndef SQLITE_OMIT_DESERIALIZE
11219    }else if( strcmp(z,"-deserialize")==0 ){
11220      data.openMode = SHELL_OPEN_DESERIALIZE;
11221    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11222      data.szMax = integerValue(argv[++i]);
11223#endif
11224    }else if( strcmp(z,"-readonly")==0 ){
11225      data.openMode = SHELL_OPEN_READONLY;
11226    }else if( strcmp(z,"-nofollow")==0 ){
11227      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11228    }else if( strcmp(z,"-ascii")==0 ){
11229      data.mode = MODE_Ascii;
11230      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
11231      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
11232    }else if( strcmp(z,"-tabs")==0 ){
11233      data.mode = MODE_List;
11234      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
11235      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11236    }else if( strcmp(z,"-separator")==0 ){
11237      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11238                       "%s",cmdline_option_value(argc,argv,++i));
11239    }else if( strcmp(z,"-newline")==0 ){
11240      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11241                       "%s",cmdline_option_value(argc,argv,++i));
11242    }else if( strcmp(z,"-nullvalue")==0 ){
11243      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11244                       "%s",cmdline_option_value(argc,argv,++i));
11245    }else if( strcmp(z,"-header")==0 ){
11246      data.showHeader = 1;
11247    }else if( strcmp(z,"-noheader")==0 ){
11248      data.showHeader = 0;
11249    }else if( strcmp(z,"-echo")==0 ){
11250      ShellSetFlag(&data, SHFLG_Echo);
11251    }else if( strcmp(z,"-eqp")==0 ){
11252      data.autoEQP = AUTOEQP_on;
11253    }else if( strcmp(z,"-eqpfull")==0 ){
11254      data.autoEQP = AUTOEQP_full;
11255    }else if( strcmp(z,"-stats")==0 ){
11256      data.statsOn = 1;
11257    }else if( strcmp(z,"-scanstats")==0 ){
11258      data.scanstatsOn = 1;
11259    }else if( strcmp(z,"-backslash")==0 ){
11260      /* Undocumented command-line option: -backslash
11261      ** Causes C-style backslash escapes to be evaluated in SQL statements
11262      ** prior to sending the SQL into SQLite.  Useful for injecting
11263      ** crazy bytes in the middle of SQL statements for testing and debugging.
11264      */
11265      ShellSetFlag(&data, SHFLG_Backslash);
11266    }else if( strcmp(z,"-bail")==0 ){
11267      /* No-op.  The bail_on_error flag should already be set. */
11268    }else if( strcmp(z,"-version")==0 ){
11269      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11270      return 0;
11271    }else if( strcmp(z,"-interactive")==0 ){
11272      stdin_is_interactive = 1;
11273    }else if( strcmp(z,"-batch")==0 ){
11274      stdin_is_interactive = 0;
11275    }else if( strcmp(z,"-heap")==0 ){
11276      i++;
11277    }else if( strcmp(z,"-pagecache")==0 ){
11278      i+=2;
11279    }else if( strcmp(z,"-lookaside")==0 ){
11280      i+=2;
11281    }else if( strcmp(z,"-mmap")==0 ){
11282      i++;
11283    }else if( strcmp(z,"-memtrace")==0 ){
11284      i++;
11285#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11286    }else if( strcmp(z,"-sorterref")==0 ){
11287      i++;
11288#endif
11289    }else if( strcmp(z,"-vfs")==0 ){
11290      i++;
11291#ifdef SQLITE_ENABLE_VFSTRACE
11292    }else if( strcmp(z,"-vfstrace")==0 ){
11293      i++;
11294#endif
11295#ifdef SQLITE_ENABLE_MULTIPLEX
11296    }else if( strcmp(z,"-multiplex")==0 ){
11297      i++;
11298#endif
11299    }else if( strcmp(z,"-help")==0 ){
11300      usage(1);
11301    }else if( strcmp(z,"-cmd")==0 ){
11302      /* Run commands that follow -cmd first and separately from commands
11303      ** that simply appear on the command-line.  This seems goofy.  It would
11304      ** be better if all commands ran in the order that they appear.  But
11305      ** we retain the goofy behavior for historical compatibility. */
11306      if( i==argc-1 ) break;
11307      z = cmdline_option_value(argc,argv,++i);
11308      if( z[0]=='.' ){
11309        rc = do_meta_command(z, &data);
11310        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11311      }else{
11312        open_db(&data, 0);
11313        rc = shell_exec(&data, z, &zErrMsg);
11314        if( zErrMsg!=0 ){
11315          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11316          if( bail_on_error ) return rc!=0 ? rc : 1;
11317        }else if( rc!=0 ){
11318          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11319          if( bail_on_error ) return rc;
11320        }
11321      }
11322#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11323    }else if( strncmp(z, "-A", 2)==0 ){
11324      if( nCmd>0 ){
11325        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11326                            " with \"%s\"\n", z);
11327        return 1;
11328      }
11329      open_db(&data, OPEN_DB_ZIPFILE);
11330      if( z[2] ){
11331        argv[i] = &z[2];
11332        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11333      }else{
11334        arDotCommand(&data, 1, argv+i, argc-i);
11335      }
11336      readStdin = 0;
11337      break;
11338#endif
11339    }else{
11340      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11341      raw_printf(stderr,"Use -help for a list of options.\n");
11342      return 1;
11343    }
11344    data.cMode = data.mode;
11345  }
11346
11347  if( !readStdin ){
11348    /* Run all arguments that do not begin with '-' as if they were separate
11349    ** command-line inputs, except for the argToSkip argument which contains
11350    ** the database filename.
11351    */
11352    for(i=0; i<nCmd; i++){
11353      if( azCmd[i][0]=='.' ){
11354        rc = do_meta_command(azCmd[i], &data);
11355        if( rc ){
11356          free(azCmd);
11357          return rc==2 ? 0 : rc;
11358        }
11359      }else{
11360        open_db(&data, 0);
11361        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11362        if( zErrMsg || rc ){
11363          if( zErrMsg!=0 ){
11364            utf8_printf(stderr,"Error: %s\n", zErrMsg);
11365          }else{
11366            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11367          }
11368          sqlite3_free(zErrMsg);
11369          free(azCmd);
11370          return rc!=0 ? rc : 1;
11371        }
11372      }
11373    }
11374  }else{
11375    /* Run commands received from standard input
11376    */
11377    if( stdin_is_interactive ){
11378      char *zHome;
11379      char *zHistory;
11380      int nHistory;
11381      printf(
11382        "SQLite version %s %.19s\n" /*extra-version-info*/
11383        "Enter \".help\" for usage hints.\n",
11384        sqlite3_libversion(), sqlite3_sourceid()
11385      );
11386      if( warnInmemoryDb ){
11387        printf("Connected to a ");
11388        printBold("transient in-memory database");
11389        printf(".\nUse \".open FILENAME\" to reopen on a "
11390               "persistent database.\n");
11391      }
11392      zHistory = getenv("SQLITE_HISTORY");
11393      if( zHistory ){
11394        zHistory = strdup(zHistory);
11395      }else if( (zHome = find_home_dir(0))!=0 ){
11396        nHistory = strlen30(zHome) + 20;
11397        if( (zHistory = malloc(nHistory))!=0 ){
11398          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11399        }
11400      }
11401      if( zHistory ){ shell_read_history(zHistory); }
11402#if HAVE_READLINE || HAVE_EDITLINE
11403      rl_attempted_completion_function = readline_completion;
11404#elif HAVE_LINENOISE
11405      linenoiseSetCompletionCallback(linenoise_completion);
11406#endif
11407      data.in = 0;
11408      rc = process_input(&data);
11409      if( zHistory ){
11410        shell_stifle_history(2000);
11411        shell_write_history(zHistory);
11412        free(zHistory);
11413      }
11414    }else{
11415      data.in = stdin;
11416      rc = process_input(&data);
11417    }
11418  }
11419  free(azCmd);
11420  set_table_name(&data, 0);
11421  if( data.db ){
11422    session_close_all(&data);
11423    close_db(data.db);
11424  }
11425  sqlite3_free(data.zFreeOnClose);
11426  find_home_dir(1);
11427  output_reset(&data);
11428  data.doXdgOpen = 0;
11429  clearTempFile(&data);
11430#if !SQLITE_SHELL_IS_UTF8
11431  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11432  free(argvToFree);
11433#endif
11434  free(data.colWidth);
11435  /* Clear the global data structure so that valgrind will detect memory
11436  ** leaks */
11437  memset(&data, 0, sizeof(data));
11438  return rc;
11439}
11440