xref: /sqlite-3.40.0/src/shell.c.in (revision 19f7bd3b)
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->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
230    clockVfs->xCurrentTimeInt64(clockVfs, &t);
231  }else{
232    double r;
233    clockVfs->xCurrentTime(clockVfs, &r);
234    t = (sqlite3_int64)(r*86400000.0);
235  }
236  return t;
237}
238
239#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
240#include <sys/time.h>
241#include <sys/resource.h>
242
243/* VxWorks does not support getrusage() as far as we can determine */
244#if defined(_WRS_KERNEL) || defined(__RTP__)
245struct rusage {
246  struct timeval ru_utime; /* user CPU time used */
247  struct timeval ru_stime; /* system CPU time used */
248};
249#define getrusage(A,B) memset(B,0,sizeof(*B))
250#endif
251
252/* Saved resource information for the beginning of an operation */
253static struct rusage sBegin;  /* CPU time at start */
254static sqlite3_int64 iBegin;  /* Wall-clock time at start */
255
256/*
257** Begin timing an operation
258*/
259static void beginTimer(void){
260  if( enableTimer ){
261    getrusage(RUSAGE_SELF, &sBegin);
262    iBegin = timeOfDay();
263  }
264}
265
266/* Return the difference of two time_structs in seconds */
267static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
268  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
269         (double)(pEnd->tv_sec - pStart->tv_sec);
270}
271
272/*
273** Print the timing results.
274*/
275static void endTimer(void){
276  if( enableTimer ){
277    sqlite3_int64 iEnd = timeOfDay();
278    struct rusage sEnd;
279    getrusage(RUSAGE_SELF, &sEnd);
280    printf("Run Time: real %.3f user %f sys %f\n",
281       (iEnd - iBegin)*0.001,
282       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
283       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
284  }
285}
286
287#define BEGIN_TIMER beginTimer()
288#define END_TIMER endTimer()
289#define HAS_TIMER 1
290
291#elif (defined(_WIN32) || defined(WIN32))
292
293/* Saved resource information for the beginning of an operation */
294static HANDLE hProcess;
295static FILETIME ftKernelBegin;
296static FILETIME ftUserBegin;
297static sqlite3_int64 ftWallBegin;
298typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
299                                    LPFILETIME, LPFILETIME);
300static GETPROCTIMES getProcessTimesAddr = NULL;
301
302/*
303** Check to see if we have timer support.  Return 1 if necessary
304** support found (or found previously).
305*/
306static int hasTimer(void){
307  if( getProcessTimesAddr ){
308    return 1;
309  } else {
310#if !SQLITE_OS_WINRT
311    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
312    ** versions. See if the version we are running on has it, and if it
313    ** does, save off a pointer to it and the current process handle.
314    */
315    hProcess = GetCurrentProcess();
316    if( hProcess ){
317      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
318      if( NULL != hinstLib ){
319        getProcessTimesAddr =
320            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
321        if( NULL != getProcessTimesAddr ){
322          return 1;
323        }
324        FreeLibrary(hinstLib);
325      }
326    }
327#endif
328  }
329  return 0;
330}
331
332/*
333** Begin timing an operation
334*/
335static void beginTimer(void){
336  if( enableTimer && getProcessTimesAddr ){
337    FILETIME ftCreation, ftExit;
338    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
339                        &ftKernelBegin,&ftUserBegin);
340    ftWallBegin = timeOfDay();
341  }
342}
343
344/* Return the difference of two FILETIME structs in seconds */
345static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
346  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
347  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
348  return (double) ((i64End - i64Start) / 10000000.0);
349}
350
351/*
352** Print the timing results.
353*/
354static void endTimer(void){
355  if( enableTimer && getProcessTimesAddr){
356    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
357    sqlite3_int64 ftWallEnd = timeOfDay();
358    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
359    printf("Run Time: real %.3f user %f sys %f\n",
360       (ftWallEnd - ftWallBegin)*0.001,
361       timeDiff(&ftUserBegin, &ftUserEnd),
362       timeDiff(&ftKernelBegin, &ftKernelEnd));
363  }
364}
365
366#define BEGIN_TIMER beginTimer()
367#define END_TIMER endTimer()
368#define HAS_TIMER hasTimer()
369
370#else
371#define BEGIN_TIMER
372#define END_TIMER
373#define HAS_TIMER 0
374#endif
375
376/*
377** Used to prevent warnings about unused parameters
378*/
379#define UNUSED_PARAMETER(x) (void)(x)
380
381/*
382** Number of elements in an array
383*/
384#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
385
386/*
387** If the following flag is set, then command execution stops
388** at an error if we are not interactive.
389*/
390static int bail_on_error = 0;
391
392/*
393** Threat stdin as an interactive input if the following variable
394** is true.  Otherwise, assume stdin is connected to a file or pipe.
395*/
396static int stdin_is_interactive = 1;
397
398/*
399** On Windows systems we have to know if standard output is a console
400** in order to translate UTF-8 into MBCS.  The following variable is
401** true if translation is required.
402*/
403static int stdout_is_console = 1;
404
405/*
406** The following is the open SQLite database.  We make a pointer
407** to this database a static variable so that it can be accessed
408** by the SIGINT handler to interrupt database processing.
409*/
410static sqlite3 *globalDb = 0;
411
412/*
413** True if an interrupt (Control-C) has been received.
414*/
415static volatile int seenInterrupt = 0;
416
417#ifdef SQLITE_DEBUG
418/*
419** Out-of-memory simulator variables
420*/
421static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
422static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
423static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
424#endif /* SQLITE_DEBUG */
425
426/*
427** This is the name of our program. It is set in main(), used
428** in a number of other places, mostly for error messages.
429*/
430static char *Argv0;
431
432/*
433** Prompt strings. Initialized in main. Settable with
434**   .prompt main continue
435*/
436static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
437static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
438
439/*
440** Render output like fprintf().  Except, if the output is going to the
441** console and if this is running on a Windows machine, translate the
442** output from UTF-8 into MBCS.
443*/
444#if defined(_WIN32) || defined(WIN32)
445void utf8_printf(FILE *out, const char *zFormat, ...){
446  va_list ap;
447  va_start(ap, zFormat);
448  if( stdout_is_console && (out==stdout || out==stderr) ){
449    char *z1 = sqlite3_vmprintf(zFormat, ap);
450    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
451    sqlite3_free(z1);
452    fputs(z2, out);
453    sqlite3_free(z2);
454  }else{
455    vfprintf(out, zFormat, ap);
456  }
457  va_end(ap);
458}
459#elif !defined(utf8_printf)
460# define utf8_printf fprintf
461#endif
462
463/*
464** Render output like fprintf().  This should not be used on anything that
465** includes string formatting (e.g. "%s").
466*/
467#if !defined(raw_printf)
468# define raw_printf fprintf
469#endif
470
471/* Indicate out-of-memory and exit. */
472static void shell_out_of_memory(void){
473  raw_printf(stderr,"Error: out of memory\n");
474  exit(1);
475}
476
477#ifdef SQLITE_DEBUG
478/* This routine is called when a simulated OOM occurs.  It is broken
479** out as a separate routine to make it easy to set a breakpoint on
480** the OOM
481*/
482void shellOomFault(void){
483  if( oomRepeat>0 ){
484    oomRepeat--;
485  }else{
486    oomCounter--;
487  }
488}
489#endif /* SQLITE_DEBUG */
490
491#ifdef SQLITE_DEBUG
492/* This routine is a replacement malloc() that is used to simulate
493** Out-Of-Memory (OOM) errors for testing purposes.
494*/
495static void *oomMalloc(int nByte){
496  if( oomCounter ){
497    if( oomCounter==1 ){
498      shellOomFault();
499      return 0;
500    }else{
501      oomCounter--;
502    }
503  }
504  return defaultMalloc(nByte);
505}
506#endif /* SQLITE_DEBUG */
507
508#ifdef SQLITE_DEBUG
509/* Register the OOM simulator.  This must occur before any memory
510** allocations */
511static void registerOomSimulator(void){
512  sqlite3_mem_methods mem;
513  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
514  defaultMalloc = mem.xMalloc;
515  mem.xMalloc = oomMalloc;
516  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
517}
518#endif
519
520/*
521** Write I/O traces to the following stream.
522*/
523#ifdef SQLITE_ENABLE_IOTRACE
524static FILE *iotrace = 0;
525#endif
526
527/*
528** This routine works like printf in that its first argument is a
529** format string and subsequent arguments are values to be substituted
530** in place of % fields.  The result of formatting this string
531** is written to iotrace.
532*/
533#ifdef SQLITE_ENABLE_IOTRACE
534static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
535  va_list ap;
536  char *z;
537  if( iotrace==0 ) return;
538  va_start(ap, zFormat);
539  z = sqlite3_vmprintf(zFormat, ap);
540  va_end(ap);
541  utf8_printf(iotrace, "%s", z);
542  sqlite3_free(z);
543}
544#endif
545
546/*
547** Output string zUtf to stream pOut as w characters.  If w is negative,
548** then right-justify the text.  W is the width in UTF-8 characters, not
549** in bytes.  This is different from the %*.*s specification in printf
550** since with %*.*s the width is measured in bytes, not characters.
551*/
552static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
553  int i;
554  int n;
555  int aw = w<0 ? -w : w;
556  for(i=n=0; zUtf[i]; i++){
557    if( (zUtf[i]&0xc0)!=0x80 ){
558      n++;
559      if( n==aw ){
560        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
561        break;
562      }
563    }
564  }
565  if( n>=aw ){
566    utf8_printf(pOut, "%.*s", i, zUtf);
567  }else if( w<0 ){
568    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
569  }else{
570    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
571  }
572}
573
574
575/*
576** Determines if a string is a number of not.
577*/
578static int isNumber(const char *z, int *realnum){
579  if( *z=='-' || *z=='+' ) z++;
580  if( !IsDigit(*z) ){
581    return 0;
582  }
583  z++;
584  if( realnum ) *realnum = 0;
585  while( IsDigit(*z) ){ z++; }
586  if( *z=='.' ){
587    z++;
588    if( !IsDigit(*z) ) return 0;
589    while( IsDigit(*z) ){ z++; }
590    if( realnum ) *realnum = 1;
591  }
592  if( *z=='e' || *z=='E' ){
593    z++;
594    if( *z=='+' || *z=='-' ) z++;
595    if( !IsDigit(*z) ) return 0;
596    while( IsDigit(*z) ){ z++; }
597    if( realnum ) *realnum = 1;
598  }
599  return *z==0;
600}
601
602/*
603** Compute a string length that is limited to what can be stored in
604** lower 30 bits of a 32-bit signed integer.
605*/
606static int strlen30(const char *z){
607  const char *z2 = z;
608  while( *z2 ){ z2++; }
609  return 0x3fffffff & (int)(z2 - z);
610}
611
612/*
613** Return the length of a string in characters.  Multibyte UTF8 characters
614** count as a single character.
615*/
616static int strlenChar(const char *z){
617  int n = 0;
618  while( *z ){
619    if( (0xc0&*(z++))!=0x80 ) n++;
620  }
621  return n;
622}
623
624/*
625** Return true if zFile does not exist or if it is not an ordinary file.
626*/
627#ifdef _WIN32
628# define notNormalFile(X) 0
629#else
630static int notNormalFile(const char *zFile){
631  struct stat x;
632  int rc;
633  memset(&x, 0, sizeof(x));
634  rc = stat(zFile, &x);
635  return rc || !S_ISREG(x.st_mode);
636}
637#endif
638
639/*
640** This routine reads a line of text from FILE in, stores
641** the text in memory obtained from malloc() and returns a pointer
642** to the text.  NULL is returned at end of file, or if malloc()
643** fails.
644**
645** If zLine is not NULL then it is a malloced buffer returned from
646** a previous call to this routine that may be reused.
647*/
648static char *local_getline(char *zLine, FILE *in){
649  int nLine = zLine==0 ? 0 : 100;
650  int n = 0;
651
652  while( 1 ){
653    if( n+100>nLine ){
654      nLine = nLine*2 + 100;
655      zLine = realloc(zLine, nLine);
656      if( zLine==0 ) shell_out_of_memory();
657    }
658    if( fgets(&zLine[n], nLine - n, in)==0 ){
659      if( n==0 ){
660        free(zLine);
661        return 0;
662      }
663      zLine[n] = 0;
664      break;
665    }
666    while( zLine[n] ) n++;
667    if( n>0 && zLine[n-1]=='\n' ){
668      n--;
669      if( n>0 && zLine[n-1]=='\r' ) n--;
670      zLine[n] = 0;
671      break;
672    }
673  }
674#if defined(_WIN32) || defined(WIN32)
675  /* For interactive input on Windows systems, translate the
676  ** multi-byte characterset characters into UTF-8. */
677  if( stdin_is_interactive && in==stdin ){
678    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
679    if( zTrans ){
680      int nTrans = strlen30(zTrans)+1;
681      if( nTrans>nLine ){
682        zLine = realloc(zLine, nTrans);
683        if( zLine==0 ) shell_out_of_memory();
684      }
685      memcpy(zLine, zTrans, nTrans);
686      sqlite3_free(zTrans);
687    }
688  }
689#endif /* defined(_WIN32) || defined(WIN32) */
690  return zLine;
691}
692
693/*
694** Retrieve a single line of input text.
695**
696** If in==0 then read from standard input and prompt before each line.
697** If isContinuation is true, then a continuation prompt is appropriate.
698** If isContinuation is zero, then the main prompt should be used.
699**
700** If zPrior is not NULL then it is a buffer from a prior call to this
701** routine that can be reused.
702**
703** The result is stored in space obtained from malloc() and must either
704** be freed by the caller or else passed back into this routine via the
705** zPrior argument for reuse.
706*/
707static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
708  char *zPrompt;
709  char *zResult;
710  if( in!=0 ){
711    zResult = local_getline(zPrior, in);
712  }else{
713    zPrompt = isContinuation ? continuePrompt : mainPrompt;
714#if SHELL_USE_LOCAL_GETLINE
715    printf("%s", zPrompt);
716    fflush(stdout);
717    zResult = local_getline(zPrior, stdin);
718#else
719    free(zPrior);
720    zResult = shell_readline(zPrompt);
721    if( zResult && *zResult ) shell_add_history(zResult);
722#endif
723  }
724  return zResult;
725}
726
727
728/*
729** Return the value of a hexadecimal digit.  Return -1 if the input
730** is not a hex digit.
731*/
732static int hexDigitValue(char c){
733  if( c>='0' && c<='9' ) return c - '0';
734  if( c>='a' && c<='f' ) return c - 'a' + 10;
735  if( c>='A' && c<='F' ) return c - 'A' + 10;
736  return -1;
737}
738
739/*
740** Interpret zArg as an integer value, possibly with suffixes.
741*/
742static sqlite3_int64 integerValue(const char *zArg){
743  sqlite3_int64 v = 0;
744  static const struct { char *zSuffix; int iMult; } aMult[] = {
745    { "KiB", 1024 },
746    { "MiB", 1024*1024 },
747    { "GiB", 1024*1024*1024 },
748    { "KB",  1000 },
749    { "MB",  1000000 },
750    { "GB",  1000000000 },
751    { "K",   1000 },
752    { "M",   1000000 },
753    { "G",   1000000000 },
754  };
755  int i;
756  int isNeg = 0;
757  if( zArg[0]=='-' ){
758    isNeg = 1;
759    zArg++;
760  }else if( zArg[0]=='+' ){
761    zArg++;
762  }
763  if( zArg[0]=='0' && zArg[1]=='x' ){
764    int x;
765    zArg += 2;
766    while( (x = hexDigitValue(zArg[0]))>=0 ){
767      v = (v<<4) + x;
768      zArg++;
769    }
770  }else{
771    while( IsDigit(zArg[0]) ){
772      v = v*10 + zArg[0] - '0';
773      zArg++;
774    }
775  }
776  for(i=0; i<ArraySize(aMult); i++){
777    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
778      v *= aMult[i].iMult;
779      break;
780    }
781  }
782  return isNeg? -v : v;
783}
784
785/*
786** A variable length string to which one can append text.
787*/
788typedef struct ShellText ShellText;
789struct ShellText {
790  char *z;
791  int n;
792  int nAlloc;
793};
794
795/*
796** Initialize and destroy a ShellText object
797*/
798static void initText(ShellText *p){
799  memset(p, 0, sizeof(*p));
800}
801static void freeText(ShellText *p){
802  free(p->z);
803  initText(p);
804}
805
806/* zIn is either a pointer to a NULL-terminated string in memory obtained
807** from malloc(), or a NULL pointer. The string pointed to by zAppend is
808** added to zIn, and the result returned in memory obtained from malloc().
809** zIn, if it was not NULL, is freed.
810**
811** If the third argument, quote, is not '\0', then it is used as a
812** quote character for zAppend.
813*/
814static void appendText(ShellText *p, char const *zAppend, char quote){
815  int len;
816  int i;
817  int nAppend = strlen30(zAppend);
818
819  len = nAppend+p->n+1;
820  if( quote ){
821    len += 2;
822    for(i=0; i<nAppend; i++){
823      if( zAppend[i]==quote ) len++;
824    }
825  }
826
827  if( p->n+len>=p->nAlloc ){
828    p->nAlloc = p->nAlloc*2 + len + 20;
829    p->z = realloc(p->z, p->nAlloc);
830    if( p->z==0 ) shell_out_of_memory();
831  }
832
833  if( quote ){
834    char *zCsr = p->z+p->n;
835    *zCsr++ = quote;
836    for(i=0; i<nAppend; i++){
837      *zCsr++ = zAppend[i];
838      if( zAppend[i]==quote ) *zCsr++ = quote;
839    }
840    *zCsr++ = quote;
841    p->n = (int)(zCsr - p->z);
842    *zCsr = '\0';
843  }else{
844    memcpy(p->z+p->n, zAppend, nAppend);
845    p->n += nAppend;
846    p->z[p->n] = '\0';
847  }
848}
849
850/*
851** Attempt to determine if identifier zName needs to be quoted, either
852** because it contains non-alphanumeric characters, or because it is an
853** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
854** that quoting is required.
855**
856** Return '"' if quoting is required.  Return 0 if no quoting is required.
857*/
858static char quoteChar(const char *zName){
859  int i;
860  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
861  for(i=0; zName[i]; i++){
862    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
863  }
864  return sqlite3_keyword_check(zName, i) ? '"' : 0;
865}
866
867/*
868** Construct a fake object name and column list to describe the structure
869** of the view, virtual table, or table valued function zSchema.zName.
870*/
871static char *shellFakeSchema(
872  sqlite3 *db,            /* The database connection containing the vtab */
873  const char *zSchema,    /* Schema of the database holding the vtab */
874  const char *zName       /* The name of the virtual table */
875){
876  sqlite3_stmt *pStmt = 0;
877  char *zSql;
878  ShellText s;
879  char cQuote;
880  char *zDiv = "(";
881  int nRow = 0;
882
883  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
884                         zSchema ? zSchema : "main", zName);
885  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
886  sqlite3_free(zSql);
887  initText(&s);
888  if( zSchema ){
889    cQuote = quoteChar(zSchema);
890    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
891    appendText(&s, zSchema, cQuote);
892    appendText(&s, ".", 0);
893  }
894  cQuote = quoteChar(zName);
895  appendText(&s, zName, cQuote);
896  while( sqlite3_step(pStmt)==SQLITE_ROW ){
897    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
898    nRow++;
899    appendText(&s, zDiv, 0);
900    zDiv = ",";
901    cQuote = quoteChar(zCol);
902    appendText(&s, zCol, cQuote);
903  }
904  appendText(&s, ")", 0);
905  sqlite3_finalize(pStmt);
906  if( nRow==0 ){
907    freeText(&s);
908    s.z = 0;
909  }
910  return s.z;
911}
912
913/*
914** SQL function:  shell_module_schema(X)
915**
916** Return a fake schema for the table-valued function or eponymous virtual
917** table X.
918*/
919static void shellModuleSchema(
920  sqlite3_context *pCtx,
921  int nVal,
922  sqlite3_value **apVal
923){
924  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
925  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
926  UNUSED_PARAMETER(nVal);
927  if( zFake ){
928    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
929                        -1, sqlite3_free);
930    free(zFake);
931  }
932}
933
934/*
935** SQL function:  shell_add_schema(S,X)
936**
937** Add the schema name X to the CREATE statement in S and return the result.
938** Examples:
939**
940**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
941**
942** Also works on
943**
944**    CREATE INDEX
945**    CREATE UNIQUE INDEX
946**    CREATE VIEW
947**    CREATE TRIGGER
948**    CREATE VIRTUAL TABLE
949**
950** This UDF is used by the .schema command to insert the schema name of
951** attached databases into the middle of the sqlite_schema.sql field.
952*/
953static void shellAddSchemaName(
954  sqlite3_context *pCtx,
955  int nVal,
956  sqlite3_value **apVal
957){
958  static const char *aPrefix[] = {
959     "TABLE",
960     "INDEX",
961     "UNIQUE INDEX",
962     "VIEW",
963     "TRIGGER",
964     "VIRTUAL TABLE"
965  };
966  int i = 0;
967  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
968  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
969  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
970  sqlite3 *db = sqlite3_context_db_handle(pCtx);
971  UNUSED_PARAMETER(nVal);
972  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
973    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
974      int n = strlen30(aPrefix[i]);
975      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
976        char *z = 0;
977        char *zFake = 0;
978        if( zSchema ){
979          char cQuote = quoteChar(zSchema);
980          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
981            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
982          }else{
983            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
984          }
985        }
986        if( zName
987         && aPrefix[i][0]=='V'
988         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
989        ){
990          if( z==0 ){
991            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
992          }else{
993            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
994          }
995          free(zFake);
996        }
997        if( z ){
998          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
999          return;
1000        }
1001      }
1002    }
1003  }
1004  sqlite3_result_value(pCtx, apVal[0]);
1005}
1006
1007/*
1008** The source code for several run-time loadable extensions is inserted
1009** below by the ../tool/mkshellc.tcl script.  Before processing that included
1010** code, we need to override some macros to make the included program code
1011** work here in the middle of this regular program.
1012*/
1013#define SQLITE_EXTENSION_INIT1
1014#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1015
1016#if defined(_WIN32) && defined(_MSC_VER)
1017INCLUDE test_windirent.h
1018INCLUDE test_windirent.c
1019#define dirent DIRENT
1020#endif
1021INCLUDE ../ext/misc/shathree.c
1022INCLUDE ../ext/misc/fileio.c
1023INCLUDE ../ext/misc/completion.c
1024INCLUDE ../ext/misc/appendvfs.c
1025INCLUDE ../ext/misc/memtrace.c
1026INCLUDE ../ext/misc/uint.c
1027INCLUDE ../ext/misc/decimal.c
1028INCLUDE ../ext/misc/ieee754.c
1029INCLUDE ../ext/misc/series.c
1030#ifdef SQLITE_HAVE_ZLIB
1031INCLUDE ../ext/misc/zipfile.c
1032INCLUDE ../ext/misc/sqlar.c
1033#endif
1034INCLUDE ../ext/expert/sqlite3expert.h
1035INCLUDE ../ext/expert/sqlite3expert.c
1036
1037#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1038INCLUDE ../ext/misc/dbdata.c
1039#endif
1040
1041#if defined(SQLITE_ENABLE_SESSION)
1042/*
1043** State information for a single open session
1044*/
1045typedef struct OpenSession OpenSession;
1046struct OpenSession {
1047  char *zName;             /* Symbolic name for this session */
1048  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1049  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1050  sqlite3_session *p;      /* The open session */
1051};
1052#endif
1053
1054typedef struct ExpertInfo ExpertInfo;
1055struct ExpertInfo {
1056  sqlite3expert *pExpert;
1057  int bVerbose;
1058};
1059
1060/* A single line in the EQP output */
1061typedef struct EQPGraphRow EQPGraphRow;
1062struct EQPGraphRow {
1063  int iEqpId;           /* ID for this row */
1064  int iParentId;        /* ID of the parent row */
1065  EQPGraphRow *pNext;   /* Next row in sequence */
1066  char zText[1];        /* Text to display for this row */
1067};
1068
1069/* All EQP output is collected into an instance of the following */
1070typedef struct EQPGraph EQPGraph;
1071struct EQPGraph {
1072  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1073  EQPGraphRow *pLast;   /* Last element of the pRow list */
1074  char zPrefix[100];    /* Graph prefix */
1075};
1076
1077/*
1078** State information about the database connection is contained in an
1079** instance of the following structure.
1080*/
1081typedef struct ShellState ShellState;
1082struct ShellState {
1083  sqlite3 *db;           /* The database */
1084  u8 autoExplain;        /* Automatically turn on .explain mode */
1085  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1086  u8 autoEQPtest;        /* autoEQP is in test mode */
1087  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1088  u8 statsOn;            /* True to display memory stats before each finalize */
1089  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1090  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1091  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1092  u8 nEqpLevel;          /* Depth of the EQP output graph */
1093  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1094  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1095  int outCount;          /* Revert to stdout when reaching zero */
1096  int cnt;               /* Number of records displayed so far */
1097  int lineno;            /* Line number of last line read from in */
1098  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1099  FILE *in;              /* Read commands from this stream */
1100  FILE *out;             /* Write results here */
1101  FILE *traceOut;        /* Output for sqlite3_trace() */
1102  int nErr;              /* Number of errors seen */
1103  int mode;              /* An output mode setting */
1104  int modePrior;         /* Saved mode */
1105  int cMode;             /* temporary output mode for the current query */
1106  int normalMode;        /* Output mode before ".explain on" */
1107  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1108  int showHeader;        /* True to show column names in List or Column mode */
1109  int nCheck;            /* Number of ".check" commands run */
1110  unsigned nProgress;    /* Number of progress callbacks encountered */
1111  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1112  unsigned flgProgress;  /* Flags for the progress callback */
1113  unsigned shellFlgs;    /* Various flags */
1114  unsigned priorShFlgs;  /* Saved copy of flags */
1115  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1116  char *zDestTable;      /* Name of destination table when MODE_Insert */
1117  char *zTempFile;       /* Temporary file that might need deleting */
1118  char zTestcase[30];    /* Name of current test case */
1119  char colSeparator[20]; /* Column separator character for several modes */
1120  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1121  char colSepPrior[20];  /* Saved column separator */
1122  char rowSepPrior[20];  /* Saved row separator */
1123  int *colWidth;         /* Requested width of each column in columnar modes */
1124  int *actualWidth;      /* Actual width of each column */
1125  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1126  char nullValue[20];    /* The text to print when a NULL comes back from
1127                         ** the database */
1128  char outfile[FILENAME_MAX]; /* Filename for *out */
1129  const char *zDbFilename;    /* name of the database file */
1130  char *zFreeOnClose;         /* Filename to free when closing */
1131  const char *zVfs;           /* Name of VFS to use */
1132  sqlite3_stmt *pStmt;   /* Current statement if any. */
1133  FILE *pLog;            /* Write log output here */
1134  int *aiIndent;         /* Array of indents used in MODE_Explain */
1135  int nIndent;           /* Size of array aiIndent[] */
1136  int iIndent;           /* Index of current op in aiIndent[] */
1137  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1138#if defined(SQLITE_ENABLE_SESSION)
1139  int nSession;             /* Number of active sessions */
1140  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1141#endif
1142  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1143};
1144
1145
1146/* Allowed values for ShellState.autoEQP
1147*/
1148#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1149#define AUTOEQP_on       1           /* Automatic EQP is on */
1150#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1151#define AUTOEQP_full     3           /* Show full EXPLAIN */
1152
1153/* Allowed values for ShellState.openMode
1154*/
1155#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1156#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1157#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1158#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1159#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1160#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1161#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1162
1163/* Allowed values for ShellState.eTraceType
1164*/
1165#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1166#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1167#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1168
1169/* Bits in the ShellState.flgProgress variable */
1170#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1171#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1172                                   ** callback limit is reached, and for each
1173                                   ** top-level SQL statement */
1174#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1175
1176/*
1177** These are the allowed shellFlgs values
1178*/
1179#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1180#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1181#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1182#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1183#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1184#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1185#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1186#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
1187#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1188#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1189
1190/*
1191** Macros for testing and setting shellFlgs
1192*/
1193#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1194#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1195#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1196
1197/*
1198** These are the allowed modes.
1199*/
1200#define MODE_Line     0  /* One column per line.  Blank line between records */
1201#define MODE_Column   1  /* One record per line in neat columns */
1202#define MODE_List     2  /* One record per line with a separator */
1203#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1204#define MODE_Html     4  /* Generate an XHTML table */
1205#define MODE_Insert   5  /* Generate SQL "insert" statements */
1206#define MODE_Quote    6  /* Quote values as for SQL */
1207#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1208#define MODE_Csv      8  /* Quote strings, numbers are plain */
1209#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1210#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1211#define MODE_Pretty  11  /* Pretty-print schemas */
1212#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1213#define MODE_Json    13  /* Output JSON */
1214#define MODE_Markdown 14 /* Markdown formatting */
1215#define MODE_Table   15  /* MySQL-style table formatting */
1216#define MODE_Box     16  /* Unicode box-drawing characters */
1217
1218static const char *modeDescr[] = {
1219  "line",
1220  "column",
1221  "list",
1222  "semi",
1223  "html",
1224  "insert",
1225  "quote",
1226  "tcl",
1227  "csv",
1228  "explain",
1229  "ascii",
1230  "prettyprint",
1231  "eqp",
1232  "json",
1233  "markdown",
1234  "table",
1235  "box"
1236};
1237
1238/*
1239** These are the column/row/line separators used by the various
1240** import/export modes.
1241*/
1242#define SEP_Column    "|"
1243#define SEP_Row       "\n"
1244#define SEP_Tab       "\t"
1245#define SEP_Space     " "
1246#define SEP_Comma     ","
1247#define SEP_CrLf      "\r\n"
1248#define SEP_Unit      "\x1F"
1249#define SEP_Record    "\x1E"
1250
1251/*
1252** A callback for the sqlite3_log() interface.
1253*/
1254static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1255  ShellState *p = (ShellState*)pArg;
1256  if( p->pLog==0 ) return;
1257  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1258  fflush(p->pLog);
1259}
1260
1261/*
1262** SQL function:  shell_putsnl(X)
1263**
1264** Write the text X to the screen (or whatever output is being directed)
1265** adding a newline at the end, and then return X.
1266*/
1267static void shellPutsFunc(
1268  sqlite3_context *pCtx,
1269  int nVal,
1270  sqlite3_value **apVal
1271){
1272  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1273  (void)nVal;
1274  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1275  sqlite3_result_value(pCtx, apVal[0]);
1276}
1277
1278/*
1279** SQL function:   edit(VALUE)
1280**                 edit(VALUE,EDITOR)
1281**
1282** These steps:
1283**
1284**     (1) Write VALUE into a temporary file.
1285**     (2) Run program EDITOR on that temporary file.
1286**     (3) Read the temporary file back and return its content as the result.
1287**     (4) Delete the temporary file
1288**
1289** If the EDITOR argument is omitted, use the value in the VISUAL
1290** environment variable.  If still there is no EDITOR, through an error.
1291**
1292** Also throw an error if the EDITOR program returns a non-zero exit code.
1293*/
1294#ifndef SQLITE_NOHAVE_SYSTEM
1295static void editFunc(
1296  sqlite3_context *context,
1297  int argc,
1298  sqlite3_value **argv
1299){
1300  const char *zEditor;
1301  char *zTempFile = 0;
1302  sqlite3 *db;
1303  char *zCmd = 0;
1304  int bBin;
1305  int rc;
1306  int hasCRNL = 0;
1307  FILE *f = 0;
1308  sqlite3_int64 sz;
1309  sqlite3_int64 x;
1310  unsigned char *p = 0;
1311
1312  if( argc==2 ){
1313    zEditor = (const char*)sqlite3_value_text(argv[1]);
1314  }else{
1315    zEditor = getenv("VISUAL");
1316  }
1317  if( zEditor==0 ){
1318    sqlite3_result_error(context, "no editor for edit()", -1);
1319    return;
1320  }
1321  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1322    sqlite3_result_error(context, "NULL input to edit()", -1);
1323    return;
1324  }
1325  db = sqlite3_context_db_handle(context);
1326  zTempFile = 0;
1327  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1328  if( zTempFile==0 ){
1329    sqlite3_uint64 r = 0;
1330    sqlite3_randomness(sizeof(r), &r);
1331    zTempFile = sqlite3_mprintf("temp%llx", r);
1332    if( zTempFile==0 ){
1333      sqlite3_result_error_nomem(context);
1334      return;
1335    }
1336  }
1337  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1338  /* When writing the file to be edited, do \n to \r\n conversions on systems
1339  ** that want \r\n line endings */
1340  f = fopen(zTempFile, bBin ? "wb" : "w");
1341  if( f==0 ){
1342    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1343    goto edit_func_end;
1344  }
1345  sz = sqlite3_value_bytes(argv[0]);
1346  if( bBin ){
1347    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1348  }else{
1349    const char *z = (const char*)sqlite3_value_text(argv[0]);
1350    /* Remember whether or not the value originally contained \r\n */
1351    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1352    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1353  }
1354  fclose(f);
1355  f = 0;
1356  if( x!=sz ){
1357    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1358    goto edit_func_end;
1359  }
1360  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1361  if( zCmd==0 ){
1362    sqlite3_result_error_nomem(context);
1363    goto edit_func_end;
1364  }
1365  rc = system(zCmd);
1366  sqlite3_free(zCmd);
1367  if( rc ){
1368    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1369    goto edit_func_end;
1370  }
1371  f = fopen(zTempFile, "rb");
1372  if( f==0 ){
1373    sqlite3_result_error(context,
1374      "edit() cannot reopen temp file after edit", -1);
1375    goto edit_func_end;
1376  }
1377  fseek(f, 0, SEEK_END);
1378  sz = ftell(f);
1379  rewind(f);
1380  p = sqlite3_malloc64( sz+1 );
1381  if( p==0 ){
1382    sqlite3_result_error_nomem(context);
1383    goto edit_func_end;
1384  }
1385  x = fread(p, 1, (size_t)sz, f);
1386  fclose(f);
1387  f = 0;
1388  if( x!=sz ){
1389    sqlite3_result_error(context, "could not read back the whole file", -1);
1390    goto edit_func_end;
1391  }
1392  if( bBin ){
1393    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1394  }else{
1395    sqlite3_int64 i, j;
1396    if( hasCRNL ){
1397      /* If the original contains \r\n then do no conversions back to \n */
1398      j = sz;
1399    }else{
1400      /* If the file did not originally contain \r\n then convert any new
1401      ** \r\n back into \n */
1402      for(i=j=0; i<sz; i++){
1403        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1404        p[j++] = p[i];
1405      }
1406      sz = j;
1407      p[sz] = 0;
1408    }
1409    sqlite3_result_text64(context, (const char*)p, sz,
1410                          sqlite3_free, SQLITE_UTF8);
1411  }
1412  p = 0;
1413
1414edit_func_end:
1415  if( f ) fclose(f);
1416  unlink(zTempFile);
1417  sqlite3_free(zTempFile);
1418  sqlite3_free(p);
1419}
1420#endif /* SQLITE_NOHAVE_SYSTEM */
1421
1422/*
1423** Save or restore the current output mode
1424*/
1425static void outputModePush(ShellState *p){
1426  p->modePrior = p->mode;
1427  p->priorShFlgs = p->shellFlgs;
1428  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1429  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1430}
1431static void outputModePop(ShellState *p){
1432  p->mode = p->modePrior;
1433  p->shellFlgs = p->priorShFlgs;
1434  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1435  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1436}
1437
1438/*
1439** Output the given string as a hex-encoded blob (eg. X'1234' )
1440*/
1441static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1442  int i;
1443  char *zBlob = (char *)pBlob;
1444  raw_printf(out,"X'");
1445  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1446  raw_printf(out,"'");
1447}
1448
1449/*
1450** Find a string that is not found anywhere in z[].  Return a pointer
1451** to that string.
1452**
1453** Try to use zA and zB first.  If both of those are already found in z[]
1454** then make up some string and store it in the buffer zBuf.
1455*/
1456static const char *unused_string(
1457  const char *z,                    /* Result must not appear anywhere in z */
1458  const char *zA, const char *zB,   /* Try these first */
1459  char *zBuf                        /* Space to store a generated string */
1460){
1461  unsigned i = 0;
1462  if( strstr(z, zA)==0 ) return zA;
1463  if( strstr(z, zB)==0 ) return zB;
1464  do{
1465    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1466  }while( strstr(z,zBuf)!=0 );
1467  return zBuf;
1468}
1469
1470/*
1471** Output the given string as a quoted string using SQL quoting conventions.
1472**
1473** See also: output_quoted_escaped_string()
1474*/
1475static void output_quoted_string(FILE *out, const char *z){
1476  int i;
1477  char c;
1478  setBinaryMode(out, 1);
1479  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1480  if( c==0 ){
1481    utf8_printf(out,"'%s'",z);
1482  }else{
1483    raw_printf(out, "'");
1484    while( *z ){
1485      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1486      if( c=='\'' ) i++;
1487      if( i ){
1488        utf8_printf(out, "%.*s", i, z);
1489        z += i;
1490      }
1491      if( c=='\'' ){
1492        raw_printf(out, "'");
1493        continue;
1494      }
1495      if( c==0 ){
1496        break;
1497      }
1498      z++;
1499    }
1500    raw_printf(out, "'");
1501  }
1502  setTextMode(out, 1);
1503}
1504
1505/*
1506** Output the given string as a quoted string using SQL quoting conventions.
1507** Additionallly , escape the "\n" and "\r" characters so that they do not
1508** get corrupted by end-of-line translation facilities in some operating
1509** systems.
1510**
1511** This is like output_quoted_string() but with the addition of the \r\n
1512** escape mechanism.
1513*/
1514static void output_quoted_escaped_string(FILE *out, const char *z){
1515  int i;
1516  char c;
1517  setBinaryMode(out, 1);
1518  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1519  if( c==0 ){
1520    utf8_printf(out,"'%s'",z);
1521  }else{
1522    const char *zNL = 0;
1523    const char *zCR = 0;
1524    int nNL = 0;
1525    int nCR = 0;
1526    char zBuf1[20], zBuf2[20];
1527    for(i=0; z[i]; i++){
1528      if( z[i]=='\n' ) nNL++;
1529      if( z[i]=='\r' ) nCR++;
1530    }
1531    if( nNL ){
1532      raw_printf(out, "replace(");
1533      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1534    }
1535    if( nCR ){
1536      raw_printf(out, "replace(");
1537      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1538    }
1539    raw_printf(out, "'");
1540    while( *z ){
1541      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1542      if( c=='\'' ) i++;
1543      if( i ){
1544        utf8_printf(out, "%.*s", i, z);
1545        z += i;
1546      }
1547      if( c=='\'' ){
1548        raw_printf(out, "'");
1549        continue;
1550      }
1551      if( c==0 ){
1552        break;
1553      }
1554      z++;
1555      if( c=='\n' ){
1556        raw_printf(out, "%s", zNL);
1557        continue;
1558      }
1559      raw_printf(out, "%s", zCR);
1560    }
1561    raw_printf(out, "'");
1562    if( nCR ){
1563      raw_printf(out, ",'%s',char(13))", zCR);
1564    }
1565    if( nNL ){
1566      raw_printf(out, ",'%s',char(10))", zNL);
1567    }
1568  }
1569  setTextMode(out, 1);
1570}
1571
1572/*
1573** Output the given string as a quoted according to C or TCL quoting rules.
1574*/
1575static void output_c_string(FILE *out, const char *z){
1576  unsigned int c;
1577  fputc('"', out);
1578  while( (c = *(z++))!=0 ){
1579    if( c=='\\' ){
1580      fputc(c, out);
1581      fputc(c, out);
1582    }else if( c=='"' ){
1583      fputc('\\', out);
1584      fputc('"', out);
1585    }else if( c=='\t' ){
1586      fputc('\\', out);
1587      fputc('t', out);
1588    }else if( c=='\n' ){
1589      fputc('\\', out);
1590      fputc('n', out);
1591    }else if( c=='\r' ){
1592      fputc('\\', out);
1593      fputc('r', out);
1594    }else if( !isprint(c&0xff) ){
1595      raw_printf(out, "\\%03o", c&0xff);
1596    }else{
1597      fputc(c, out);
1598    }
1599  }
1600  fputc('"', out);
1601}
1602
1603/*
1604** Output the given string as a quoted according to JSON quoting rules.
1605*/
1606static void output_json_string(FILE *out, const char *z, int n){
1607  unsigned int c;
1608  if( n<0 ) n = (int)strlen(z);
1609  fputc('"', out);
1610  while( n-- ){
1611    c = *(z++);
1612    if( c=='\\' || c=='"' ){
1613      fputc('\\', out);
1614      fputc(c, out);
1615    }else if( c<=0x1f ){
1616      fputc('\\', out);
1617      if( c=='\b' ){
1618        fputc('b', out);
1619      }else if( c=='\f' ){
1620        fputc('f', out);
1621      }else if( c=='\n' ){
1622        fputc('n', out);
1623      }else if( c=='\r' ){
1624        fputc('r', out);
1625      }else if( c=='\t' ){
1626        fputc('t', out);
1627      }else{
1628         raw_printf(out, "u%04x",c);
1629      }
1630    }else{
1631      fputc(c, out);
1632    }
1633  }
1634  fputc('"', out);
1635}
1636
1637/*
1638** Output the given string with characters that are special to
1639** HTML escaped.
1640*/
1641static void output_html_string(FILE *out, const char *z){
1642  int i;
1643  if( z==0 ) z = "";
1644  while( *z ){
1645    for(i=0;   z[i]
1646            && z[i]!='<'
1647            && z[i]!='&'
1648            && z[i]!='>'
1649            && z[i]!='\"'
1650            && z[i]!='\'';
1651        i++){}
1652    if( i>0 ){
1653      utf8_printf(out,"%.*s",i,z);
1654    }
1655    if( z[i]=='<' ){
1656      raw_printf(out,"&lt;");
1657    }else if( z[i]=='&' ){
1658      raw_printf(out,"&amp;");
1659    }else if( z[i]=='>' ){
1660      raw_printf(out,"&gt;");
1661    }else if( z[i]=='\"' ){
1662      raw_printf(out,"&quot;");
1663    }else if( z[i]=='\'' ){
1664      raw_printf(out,"&#39;");
1665    }else{
1666      break;
1667    }
1668    z += i + 1;
1669  }
1670}
1671
1672/*
1673** If a field contains any character identified by a 1 in the following
1674** array, then the string must be quoted for CSV.
1675*/
1676static const char needCsvQuote[] = {
1677  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1678  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1679  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1680  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1681  0, 0, 0, 0, 0, 0, 0, 0,   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, 1,
1685  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1686  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 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};
1694
1695/*
1696** Output a single term of CSV.  Actually, p->colSeparator is used for
1697** the separator, which may or may not be a comma.  p->nullValue is
1698** the null value.  Strings are quoted if necessary.  The separator
1699** is only issued if bSep is true.
1700*/
1701static void output_csv(ShellState *p, const char *z, int bSep){
1702  FILE *out = p->out;
1703  if( z==0 ){
1704    utf8_printf(out,"%s",p->nullValue);
1705  }else{
1706    int i;
1707    int nSep = strlen30(p->colSeparator);
1708    for(i=0; z[i]; i++){
1709      if( needCsvQuote[((unsigned char*)z)[i]]
1710         || (z[i]==p->colSeparator[0] &&
1711             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1712        i = 0;
1713        break;
1714      }
1715    }
1716    if( i==0 ){
1717      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1718      utf8_printf(out, "%s", zQuoted);
1719      sqlite3_free(zQuoted);
1720    }else{
1721      utf8_printf(out, "%s", z);
1722    }
1723  }
1724  if( bSep ){
1725    utf8_printf(p->out, "%s", p->colSeparator);
1726  }
1727}
1728
1729/*
1730** This routine runs when the user presses Ctrl-C
1731*/
1732static void interrupt_handler(int NotUsed){
1733  UNUSED_PARAMETER(NotUsed);
1734  seenInterrupt++;
1735  if( seenInterrupt>2 ) exit(1);
1736  if( globalDb ) sqlite3_interrupt(globalDb);
1737}
1738
1739#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1740/*
1741** This routine runs for console events (e.g. Ctrl-C) on Win32
1742*/
1743static BOOL WINAPI ConsoleCtrlHandler(
1744  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1745){
1746  if( dwCtrlType==CTRL_C_EVENT ){
1747    interrupt_handler(0);
1748    return TRUE;
1749  }
1750  return FALSE;
1751}
1752#endif
1753
1754#ifndef SQLITE_OMIT_AUTHORIZATION
1755/*
1756** When the ".auth ON" is set, the following authorizer callback is
1757** invoked.  It always returns SQLITE_OK.
1758*/
1759static int shellAuth(
1760  void *pClientData,
1761  int op,
1762  const char *zA1,
1763  const char *zA2,
1764  const char *zA3,
1765  const char *zA4
1766){
1767  ShellState *p = (ShellState*)pClientData;
1768  static const char *azAction[] = { 0,
1769     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1770     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1771     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1772     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1773     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1774     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1775     "PRAGMA",               "READ",                 "SELECT",
1776     "TRANSACTION",          "UPDATE",               "ATTACH",
1777     "DETACH",               "ALTER_TABLE",          "REINDEX",
1778     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1779     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1780  };
1781  int i;
1782  const char *az[4];
1783  az[0] = zA1;
1784  az[1] = zA2;
1785  az[2] = zA3;
1786  az[3] = zA4;
1787  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1788  for(i=0; i<4; i++){
1789    raw_printf(p->out, " ");
1790    if( az[i] ){
1791      output_c_string(p->out, az[i]);
1792    }else{
1793      raw_printf(p->out, "NULL");
1794    }
1795  }
1796  raw_printf(p->out, "\n");
1797  return SQLITE_OK;
1798}
1799#endif
1800
1801/*
1802** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1803**
1804** This routine converts some CREATE TABLE statements for shadow tables
1805** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1806*/
1807static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1808  if( z==0 ) return;
1809  if( zTail==0 ) return;
1810  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1811    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1812  }else{
1813    utf8_printf(out, "%s%s", z, zTail);
1814  }
1815}
1816static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1817  char c = z[n];
1818  z[n] = 0;
1819  printSchemaLine(out, z, zTail);
1820  z[n] = c;
1821}
1822
1823/*
1824** Return true if string z[] has nothing but whitespace and comments to the
1825** end of the first line.
1826*/
1827static int wsToEol(const char *z){
1828  int i;
1829  for(i=0; z[i]; i++){
1830    if( z[i]=='\n' ) return 1;
1831    if( IsSpace(z[i]) ) continue;
1832    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1833    return 0;
1834  }
1835  return 1;
1836}
1837
1838/*
1839** Add a new entry to the EXPLAIN QUERY PLAN data
1840*/
1841static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1842  EQPGraphRow *pNew;
1843  int nText = strlen30(zText);
1844  if( p->autoEQPtest ){
1845    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1846  }
1847  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1848  if( pNew==0 ) shell_out_of_memory();
1849  pNew->iEqpId = iEqpId;
1850  pNew->iParentId = p2;
1851  memcpy(pNew->zText, zText, nText+1);
1852  pNew->pNext = 0;
1853  if( p->sGraph.pLast ){
1854    p->sGraph.pLast->pNext = pNew;
1855  }else{
1856    p->sGraph.pRow = pNew;
1857  }
1858  p->sGraph.pLast = pNew;
1859}
1860
1861/*
1862** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1863** in p->sGraph.
1864*/
1865static void eqp_reset(ShellState *p){
1866  EQPGraphRow *pRow, *pNext;
1867  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1868    pNext = pRow->pNext;
1869    sqlite3_free(pRow);
1870  }
1871  memset(&p->sGraph, 0, sizeof(p->sGraph));
1872}
1873
1874/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1875** pOld, or return the first such line if pOld is NULL
1876*/
1877static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1878  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1879  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1880  return pRow;
1881}
1882
1883/* Render a single level of the graph that has iEqpId as its parent.  Called
1884** recursively to render sublevels.
1885*/
1886static void eqp_render_level(ShellState *p, int iEqpId){
1887  EQPGraphRow *pRow, *pNext;
1888  int n = strlen30(p->sGraph.zPrefix);
1889  char *z;
1890  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1891    pNext = eqp_next_row(p, iEqpId, pRow);
1892    z = pRow->zText;
1893    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1894                pNext ? "|--" : "`--", z);
1895    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1896      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1897      eqp_render_level(p, pRow->iEqpId);
1898      p->sGraph.zPrefix[n] = 0;
1899    }
1900  }
1901}
1902
1903/*
1904** Display and reset the EXPLAIN QUERY PLAN data
1905*/
1906static void eqp_render(ShellState *p){
1907  EQPGraphRow *pRow = p->sGraph.pRow;
1908  if( pRow ){
1909    if( pRow->zText[0]=='-' ){
1910      if( pRow->pNext==0 ){
1911        eqp_reset(p);
1912        return;
1913      }
1914      utf8_printf(p->out, "%s\n", pRow->zText+3);
1915      p->sGraph.pRow = pRow->pNext;
1916      sqlite3_free(pRow);
1917    }else{
1918      utf8_printf(p->out, "QUERY PLAN\n");
1919    }
1920    p->sGraph.zPrefix[0] = 0;
1921    eqp_render_level(p, 0);
1922    eqp_reset(p);
1923  }
1924}
1925
1926#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1927/*
1928** Progress handler callback.
1929*/
1930static int progress_handler(void *pClientData) {
1931  ShellState *p = (ShellState*)pClientData;
1932  p->nProgress++;
1933  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1934    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1935    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1936    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1937    return 1;
1938  }
1939  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1940    raw_printf(p->out, "Progress %u\n", p->nProgress);
1941  }
1942  return 0;
1943}
1944#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1945
1946/*
1947** Print N dashes
1948*/
1949static void print_dashes(FILE *out, int N){
1950  const char zDash[] = "--------------------------------------------------";
1951  const int nDash = sizeof(zDash) - 1;
1952  while( N>nDash ){
1953    fputs(zDash, out);
1954    N -= nDash;
1955  }
1956  raw_printf(out, "%.*s", N, zDash);
1957}
1958
1959/*
1960** Print a markdown or table-style row separator using ascii-art
1961*/
1962static void print_row_separator(
1963  ShellState *p,
1964  int nArg,
1965  const char *zSep
1966){
1967  int i;
1968  if( nArg>0 ){
1969    fputs(zSep, p->out);
1970    print_dashes(p->out, p->actualWidth[0]+2);
1971    for(i=1; i<nArg; i++){
1972      fputs(zSep, p->out);
1973      print_dashes(p->out, p->actualWidth[i]+2);
1974    }
1975    fputs(zSep, p->out);
1976  }
1977  fputs("\n", p->out);
1978}
1979
1980/*
1981** This is the callback routine that the shell
1982** invokes for each row of a query result.
1983*/
1984static int shell_callback(
1985  void *pArg,
1986  int nArg,        /* Number of result columns */
1987  char **azArg,    /* Text of each result column */
1988  char **azCol,    /* Column names */
1989  int *aiType      /* Column types.  Might be NULL */
1990){
1991  int i;
1992  ShellState *p = (ShellState*)pArg;
1993
1994  if( azArg==0 ) return 0;
1995  switch( p->cMode ){
1996    case MODE_Line: {
1997      int w = 5;
1998      if( azArg==0 ) break;
1999      for(i=0; i<nArg; i++){
2000        int len = strlen30(azCol[i] ? azCol[i] : "");
2001        if( len>w ) w = len;
2002      }
2003      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2004      for(i=0; i<nArg; i++){
2005        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2006                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2007      }
2008      break;
2009    }
2010    case MODE_Explain: {
2011      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2012      if( nArg>ArraySize(aExplainWidth) ){
2013        nArg = ArraySize(aExplainWidth);
2014      }
2015      if( p->cnt++==0 ){
2016        for(i=0; i<nArg; i++){
2017          int w = aExplainWidth[i];
2018          utf8_width_print(p->out, w, azCol[i]);
2019          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2020        }
2021        for(i=0; i<nArg; i++){
2022          int w = aExplainWidth[i];
2023          print_dashes(p->out, w);
2024          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2025        }
2026      }
2027      if( azArg==0 ) break;
2028      for(i=0; i<nArg; i++){
2029        int w = aExplainWidth[i];
2030        if( azArg[i] && strlenChar(azArg[i])>w ){
2031          w = strlenChar(azArg[i]);
2032        }
2033        if( i==1 && p->aiIndent && p->pStmt ){
2034          if( p->iIndent<p->nIndent ){
2035            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2036          }
2037          p->iIndent++;
2038        }
2039        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2040        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2041      }
2042      break;
2043    }
2044    case MODE_Semi: {   /* .schema and .fullschema output */
2045      printSchemaLine(p->out, azArg[0], ";\n");
2046      break;
2047    }
2048    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2049      char *z;
2050      int j;
2051      int nParen = 0;
2052      char cEnd = 0;
2053      char c;
2054      int nLine = 0;
2055      assert( nArg==1 );
2056      if( azArg[0]==0 ) break;
2057      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2058       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2059      ){
2060        utf8_printf(p->out, "%s;\n", azArg[0]);
2061        break;
2062      }
2063      z = sqlite3_mprintf("%s", azArg[0]);
2064      j = 0;
2065      for(i=0; IsSpace(z[i]); i++){}
2066      for(; (c = z[i])!=0; i++){
2067        if( IsSpace(c) ){
2068          if( z[j-1]=='\r' ) z[j-1] = '\n';
2069          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2070        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2071          j--;
2072        }
2073        z[j++] = c;
2074      }
2075      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2076      z[j] = 0;
2077      if( strlen30(z)>=79 ){
2078        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2079          if( c==cEnd ){
2080            cEnd = 0;
2081          }else if( c=='"' || c=='\'' || c=='`' ){
2082            cEnd = c;
2083          }else if( c=='[' ){
2084            cEnd = ']';
2085          }else if( c=='-' && z[i+1]=='-' ){
2086            cEnd = '\n';
2087          }else if( c=='(' ){
2088            nParen++;
2089          }else if( c==')' ){
2090            nParen--;
2091            if( nLine>0 && nParen==0 && j>0 ){
2092              printSchemaLineN(p->out, z, j, "\n");
2093              j = 0;
2094            }
2095          }
2096          z[j++] = c;
2097          if( nParen==1 && cEnd==0
2098           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2099          ){
2100            if( c=='\n' ) j--;
2101            printSchemaLineN(p->out, z, j, "\n  ");
2102            j = 0;
2103            nLine++;
2104            while( IsSpace(z[i+1]) ){ i++; }
2105          }
2106        }
2107        z[j] = 0;
2108      }
2109      printSchemaLine(p->out, z, ";\n");
2110      sqlite3_free(z);
2111      break;
2112    }
2113    case MODE_List: {
2114      if( p->cnt++==0 && p->showHeader ){
2115        for(i=0; i<nArg; i++){
2116          utf8_printf(p->out,"%s%s",azCol[i],
2117                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2118        }
2119      }
2120      if( azArg==0 ) break;
2121      for(i=0; i<nArg; i++){
2122        char *z = azArg[i];
2123        if( z==0 ) z = p->nullValue;
2124        utf8_printf(p->out, "%s", z);
2125        if( i<nArg-1 ){
2126          utf8_printf(p->out, "%s", p->colSeparator);
2127        }else{
2128          utf8_printf(p->out, "%s", p->rowSeparator);
2129        }
2130      }
2131      break;
2132    }
2133    case MODE_Html: {
2134      if( p->cnt++==0 && p->showHeader ){
2135        raw_printf(p->out,"<TR>");
2136        for(i=0; i<nArg; i++){
2137          raw_printf(p->out,"<TH>");
2138          output_html_string(p->out, azCol[i]);
2139          raw_printf(p->out,"</TH>\n");
2140        }
2141        raw_printf(p->out,"</TR>\n");
2142      }
2143      if( azArg==0 ) break;
2144      raw_printf(p->out,"<TR>");
2145      for(i=0; i<nArg; i++){
2146        raw_printf(p->out,"<TD>");
2147        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2148        raw_printf(p->out,"</TD>\n");
2149      }
2150      raw_printf(p->out,"</TR>\n");
2151      break;
2152    }
2153    case MODE_Tcl: {
2154      if( p->cnt++==0 && p->showHeader ){
2155        for(i=0; i<nArg; i++){
2156          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2157          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2158        }
2159        utf8_printf(p->out, "%s", p->rowSeparator);
2160      }
2161      if( azArg==0 ) break;
2162      for(i=0; i<nArg; i++){
2163        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2164        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2165      }
2166      utf8_printf(p->out, "%s", p->rowSeparator);
2167      break;
2168    }
2169    case MODE_Csv: {
2170      setBinaryMode(p->out, 1);
2171      if( p->cnt++==0 && p->showHeader ){
2172        for(i=0; i<nArg; i++){
2173          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2174        }
2175        utf8_printf(p->out, "%s", p->rowSeparator);
2176      }
2177      if( nArg>0 ){
2178        for(i=0; i<nArg; i++){
2179          output_csv(p, azArg[i], i<nArg-1);
2180        }
2181        utf8_printf(p->out, "%s", p->rowSeparator);
2182      }
2183      setTextMode(p->out, 1);
2184      break;
2185    }
2186    case MODE_Insert: {
2187      if( azArg==0 ) break;
2188      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2189      if( p->showHeader ){
2190        raw_printf(p->out,"(");
2191        for(i=0; i<nArg; i++){
2192          if( i>0 ) raw_printf(p->out, ",");
2193          if( quoteChar(azCol[i]) ){
2194            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2195            utf8_printf(p->out, "%s", z);
2196            sqlite3_free(z);
2197          }else{
2198            raw_printf(p->out, "%s", azCol[i]);
2199          }
2200        }
2201        raw_printf(p->out,")");
2202      }
2203      p->cnt++;
2204      for(i=0; i<nArg; i++){
2205        raw_printf(p->out, i>0 ? "," : " VALUES(");
2206        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2207          utf8_printf(p->out,"NULL");
2208        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2209          if( ShellHasFlag(p, SHFLG_Newlines) ){
2210            output_quoted_string(p->out, azArg[i]);
2211          }else{
2212            output_quoted_escaped_string(p->out, azArg[i]);
2213          }
2214        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2215          utf8_printf(p->out,"%s", azArg[i]);
2216        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2217          char z[50];
2218          double r = sqlite3_column_double(p->pStmt, i);
2219          sqlite3_uint64 ur;
2220          memcpy(&ur,&r,sizeof(r));
2221          if( ur==0x7ff0000000000000LL ){
2222            raw_printf(p->out, "1e999");
2223          }else if( ur==0xfff0000000000000LL ){
2224            raw_printf(p->out, "-1e999");
2225          }else{
2226            sqlite3_snprintf(50,z,"%!.20g", r);
2227            raw_printf(p->out, "%s", z);
2228          }
2229        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2230          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2231          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2232          output_hex_blob(p->out, pBlob, nBlob);
2233        }else if( isNumber(azArg[i], 0) ){
2234          utf8_printf(p->out,"%s", azArg[i]);
2235        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2236          output_quoted_string(p->out, azArg[i]);
2237        }else{
2238          output_quoted_escaped_string(p->out, azArg[i]);
2239        }
2240      }
2241      raw_printf(p->out,");\n");
2242      break;
2243    }
2244    case MODE_Json: {
2245      if( azArg==0 ) break;
2246      if( p->cnt==0 ){
2247        fputs("[{", p->out);
2248      }else{
2249        fputs(",\n{", p->out);
2250      }
2251      p->cnt++;
2252      for(i=0; i<nArg; i++){
2253        output_json_string(p->out, azCol[i], -1);
2254        putc(':', p->out);
2255        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2256          fputs("null",p->out);
2257        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2258          char z[50];
2259          double r = sqlite3_column_double(p->pStmt, i);
2260          sqlite3_uint64 ur;
2261          memcpy(&ur,&r,sizeof(r));
2262          if( ur==0x7ff0000000000000LL ){
2263            raw_printf(p->out, "1e999");
2264          }else if( ur==0xfff0000000000000LL ){
2265            raw_printf(p->out, "-1e999");
2266          }else{
2267            sqlite3_snprintf(50,z,"%!.20g", r);
2268            raw_printf(p->out, "%s", z);
2269          }
2270        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2271          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2272          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2273          output_json_string(p->out, pBlob, nBlob);
2274        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2275          output_json_string(p->out, azArg[i], -1);
2276        }else{
2277          utf8_printf(p->out,"%s", azArg[i]);
2278        }
2279        if( i<nArg-1 ){
2280          putc(',', p->out);
2281        }
2282      }
2283      putc('}', p->out);
2284      break;
2285    }
2286    case MODE_Quote: {
2287      if( azArg==0 ) break;
2288      if( p->cnt==0 && p->showHeader ){
2289        for(i=0; i<nArg; i++){
2290          if( i>0 ) fputs(p->colSeparator, p->out);
2291          output_quoted_string(p->out, azCol[i]);
2292        }
2293        fputs(p->rowSeparator, p->out);
2294      }
2295      p->cnt++;
2296      for(i=0; i<nArg; i++){
2297        if( i>0 ) fputs(p->colSeparator, p->out);
2298        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2299          utf8_printf(p->out,"NULL");
2300        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2301          output_quoted_string(p->out, azArg[i]);
2302        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2303          utf8_printf(p->out,"%s", azArg[i]);
2304        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2305          char z[50];
2306          double r = sqlite3_column_double(p->pStmt, i);
2307          sqlite3_snprintf(50,z,"%!.20g", r);
2308          raw_printf(p->out, "%s", z);
2309        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2310          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2311          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2312          output_hex_blob(p->out, pBlob, nBlob);
2313        }else if( isNumber(azArg[i], 0) ){
2314          utf8_printf(p->out,"%s", azArg[i]);
2315        }else{
2316          output_quoted_string(p->out, azArg[i]);
2317        }
2318      }
2319      fputs(p->rowSeparator, p->out);
2320      break;
2321    }
2322    case MODE_Ascii: {
2323      if( p->cnt++==0 && p->showHeader ){
2324        for(i=0; i<nArg; i++){
2325          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2326          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2327        }
2328        utf8_printf(p->out, "%s", p->rowSeparator);
2329      }
2330      if( azArg==0 ) break;
2331      for(i=0; i<nArg; i++){
2332        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2333        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2334      }
2335      utf8_printf(p->out, "%s", p->rowSeparator);
2336      break;
2337    }
2338    case MODE_EQP: {
2339      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2340      break;
2341    }
2342  }
2343  return 0;
2344}
2345
2346/*
2347** This is the callback routine that the SQLite library
2348** invokes for each row of a query result.
2349*/
2350static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2351  /* since we don't have type info, call the shell_callback with a NULL value */
2352  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2353}
2354
2355/*
2356** This is the callback routine from sqlite3_exec() that appends all
2357** output onto the end of a ShellText object.
2358*/
2359static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2360  ShellText *p = (ShellText*)pArg;
2361  int i;
2362  UNUSED_PARAMETER(az);
2363  if( azArg==0 ) return 0;
2364  if( p->n ) appendText(p, "|", 0);
2365  for(i=0; i<nArg; i++){
2366    if( i ) appendText(p, ",", 0);
2367    if( azArg[i] ) appendText(p, azArg[i], 0);
2368  }
2369  return 0;
2370}
2371
2372/*
2373** Generate an appropriate SELFTEST table in the main database.
2374*/
2375static void createSelftestTable(ShellState *p){
2376  char *zErrMsg = 0;
2377  sqlite3_exec(p->db,
2378    "SAVEPOINT selftest_init;\n"
2379    "CREATE TABLE IF NOT EXISTS selftest(\n"
2380    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2381    "  op TEXT,\n"                   /* Operator:  memo run */
2382    "  cmd TEXT,\n"                  /* Command text */
2383    "  ans TEXT\n"                   /* Desired answer */
2384    ");"
2385    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2386    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2387    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2388    "         'memo','Tests generated by --init');\n"
2389    "INSERT INTO [_shell$self]\n"
2390    "  SELECT 'run',\n"
2391    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2392                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2393    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2394                          "FROM sqlite_schema ORDER BY 2',224));\n"
2395    "INSERT INTO [_shell$self]\n"
2396    "  SELECT 'run',"
2397    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2398    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2399    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2400    "  FROM (\n"
2401    "    SELECT name FROM sqlite_schema\n"
2402    "     WHERE type='table'\n"
2403    "       AND name<>'selftest'\n"
2404    "       AND coalesce(rootpage,0)>0\n"
2405    "  )\n"
2406    " ORDER BY name;\n"
2407    "INSERT INTO [_shell$self]\n"
2408    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2409    "INSERT INTO selftest(tno,op,cmd,ans)"
2410    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2411    "DROP TABLE [_shell$self];"
2412    ,0,0,&zErrMsg);
2413  if( zErrMsg ){
2414    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2415    sqlite3_free(zErrMsg);
2416  }
2417  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2418}
2419
2420
2421/*
2422** Set the destination table field of the ShellState structure to
2423** the name of the table given.  Escape any quote characters in the
2424** table name.
2425*/
2426static void set_table_name(ShellState *p, const char *zName){
2427  int i, n;
2428  char cQuote;
2429  char *z;
2430
2431  if( p->zDestTable ){
2432    free(p->zDestTable);
2433    p->zDestTable = 0;
2434  }
2435  if( zName==0 ) return;
2436  cQuote = quoteChar(zName);
2437  n = strlen30(zName);
2438  if( cQuote ) n += n+2;
2439  z = p->zDestTable = malloc( n+1 );
2440  if( z==0 ) shell_out_of_memory();
2441  n = 0;
2442  if( cQuote ) z[n++] = cQuote;
2443  for(i=0; zName[i]; i++){
2444    z[n++] = zName[i];
2445    if( zName[i]==cQuote ) z[n++] = cQuote;
2446  }
2447  if( cQuote ) z[n++] = cQuote;
2448  z[n] = 0;
2449}
2450
2451
2452/*
2453** Execute a query statement that will generate SQL output.  Print
2454** the result columns, comma-separated, on a line and then add a
2455** semicolon terminator to the end of that line.
2456**
2457** If the number of columns is 1 and that column contains text "--"
2458** then write the semicolon on a separate line.  That way, if a
2459** "--" comment occurs at the end of the statement, the comment
2460** won't consume the semicolon terminator.
2461*/
2462static int run_table_dump_query(
2463  ShellState *p,           /* Query context */
2464  const char *zSelect      /* SELECT statement to extract content */
2465){
2466  sqlite3_stmt *pSelect;
2467  int rc;
2468  int nResult;
2469  int i;
2470  const char *z;
2471  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2472  if( rc!=SQLITE_OK || !pSelect ){
2473    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2474                sqlite3_errmsg(p->db));
2475    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2476    return rc;
2477  }
2478  rc = sqlite3_step(pSelect);
2479  nResult = sqlite3_column_count(pSelect);
2480  while( rc==SQLITE_ROW ){
2481    z = (const char*)sqlite3_column_text(pSelect, 0);
2482    utf8_printf(p->out, "%s", z);
2483    for(i=1; i<nResult; i++){
2484      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2485    }
2486    if( z==0 ) z = "";
2487    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2488    if( z[0] ){
2489      raw_printf(p->out, "\n;\n");
2490    }else{
2491      raw_printf(p->out, ";\n");
2492    }
2493    rc = sqlite3_step(pSelect);
2494  }
2495  rc = sqlite3_finalize(pSelect);
2496  if( rc!=SQLITE_OK ){
2497    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2498                sqlite3_errmsg(p->db));
2499    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2500  }
2501  return rc;
2502}
2503
2504/*
2505** Allocate space and save off current error string.
2506*/
2507static char *save_err_msg(
2508  sqlite3 *db            /* Database to query */
2509){
2510  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2511  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2512  if( zErrMsg ){
2513    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2514  }
2515  return zErrMsg;
2516}
2517
2518#ifdef __linux__
2519/*
2520** Attempt to display I/O stats on Linux using /proc/PID/io
2521*/
2522static void displayLinuxIoStats(FILE *out){
2523  FILE *in;
2524  char z[200];
2525  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2526  in = fopen(z, "rb");
2527  if( in==0 ) return;
2528  while( fgets(z, sizeof(z), in)!=0 ){
2529    static const struct {
2530      const char *zPattern;
2531      const char *zDesc;
2532    } aTrans[] = {
2533      { "rchar: ",                  "Bytes received by read():" },
2534      { "wchar: ",                  "Bytes sent to write():"    },
2535      { "syscr: ",                  "Read() system calls:"      },
2536      { "syscw: ",                  "Write() system calls:"     },
2537      { "read_bytes: ",             "Bytes read from storage:"  },
2538      { "write_bytes: ",            "Bytes written to storage:" },
2539      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2540    };
2541    int i;
2542    for(i=0; i<ArraySize(aTrans); i++){
2543      int n = strlen30(aTrans[i].zPattern);
2544      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2545        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2546        break;
2547      }
2548    }
2549  }
2550  fclose(in);
2551}
2552#endif
2553
2554/*
2555** Display a single line of status using 64-bit values.
2556*/
2557static void displayStatLine(
2558  ShellState *p,            /* The shell context */
2559  char *zLabel,             /* Label for this one line */
2560  char *zFormat,            /* Format for the result */
2561  int iStatusCtrl,          /* Which status to display */
2562  int bReset                /* True to reset the stats */
2563){
2564  sqlite3_int64 iCur = -1;
2565  sqlite3_int64 iHiwtr = -1;
2566  int i, nPercent;
2567  char zLine[200];
2568  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2569  for(i=0, nPercent=0; zFormat[i]; i++){
2570    if( zFormat[i]=='%' ) nPercent++;
2571  }
2572  if( nPercent>1 ){
2573    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2574  }else{
2575    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2576  }
2577  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2578}
2579
2580/*
2581** Display memory stats.
2582*/
2583static int display_stats(
2584  sqlite3 *db,                /* Database to query */
2585  ShellState *pArg,           /* Pointer to ShellState */
2586  int bReset                  /* True to reset the stats */
2587){
2588  int iCur;
2589  int iHiwtr;
2590  FILE *out;
2591  if( pArg==0 || pArg->out==0 ) return 0;
2592  out = pArg->out;
2593
2594  if( pArg->pStmt && (pArg->statsOn & 2) ){
2595    int nCol, i, x;
2596    sqlite3_stmt *pStmt = pArg->pStmt;
2597    char z[100];
2598    nCol = sqlite3_column_count(pStmt);
2599    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2600    for(i=0; i<nCol; i++){
2601      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2602      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2603#ifndef SQLITE_OMIT_DECLTYPE
2604      sqlite3_snprintf(30, z+x, "declared type:");
2605      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2606#endif
2607#ifdef SQLITE_ENABLE_COLUMN_METADATA
2608      sqlite3_snprintf(30, z+x, "database name:");
2609      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2610      sqlite3_snprintf(30, z+x, "table name:");
2611      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2612      sqlite3_snprintf(30, z+x, "origin name:");
2613      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2614#endif
2615    }
2616  }
2617
2618  displayStatLine(pArg, "Memory Used:",
2619     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2620  displayStatLine(pArg, "Number of Outstanding Allocations:",
2621     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2622  if( pArg->shellFlgs & SHFLG_Pagecache ){
2623    displayStatLine(pArg, "Number of Pcache Pages Used:",
2624       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2625  }
2626  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2627     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2628  displayStatLine(pArg, "Largest Allocation:",
2629     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2630  displayStatLine(pArg, "Largest Pcache Allocation:",
2631     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2632#ifdef YYTRACKMAXSTACKDEPTH
2633  displayStatLine(pArg, "Deepest Parser Stack:",
2634     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2635#endif
2636
2637  if( db ){
2638    if( pArg->shellFlgs & SHFLG_Lookaside ){
2639      iHiwtr = iCur = -1;
2640      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2641                        &iCur, &iHiwtr, bReset);
2642      raw_printf(pArg->out,
2643              "Lookaside Slots Used:                %d (max %d)\n",
2644              iCur, iHiwtr);
2645      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2646                        &iCur, &iHiwtr, bReset);
2647      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2648              iHiwtr);
2649      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2650                        &iCur, &iHiwtr, bReset);
2651      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2652              iHiwtr);
2653      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2654                        &iCur, &iHiwtr, bReset);
2655      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2656              iHiwtr);
2657    }
2658    iHiwtr = iCur = -1;
2659    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2660    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2661            iCur);
2662    iHiwtr = iCur = -1;
2663    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2664    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2665    iHiwtr = iCur = -1;
2666    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2667    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2668    iHiwtr = iCur = -1;
2669    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2670    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2671    iHiwtr = iCur = -1;
2672    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2673    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2674    iHiwtr = iCur = -1;
2675    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2676    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2677            iCur);
2678    iHiwtr = iCur = -1;
2679    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2680    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2681            iCur);
2682  }
2683
2684  if( pArg->pStmt ){
2685    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2686                               bReset);
2687    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2688    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2689    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2690    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2691    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2692    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2693    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2694    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2695    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2696    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2697    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2698    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2699    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2700  }
2701
2702#ifdef __linux__
2703  displayLinuxIoStats(pArg->out);
2704#endif
2705
2706  /* Do not remove this machine readable comment: extra-stats-output-here */
2707
2708  return 0;
2709}
2710
2711/*
2712** Display scan stats.
2713*/
2714static void display_scanstats(
2715  sqlite3 *db,                    /* Database to query */
2716  ShellState *pArg                /* Pointer to ShellState */
2717){
2718#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2719  UNUSED_PARAMETER(db);
2720  UNUSED_PARAMETER(pArg);
2721#else
2722  int i, k, n, mx;
2723  raw_printf(pArg->out, "-------- scanstats --------\n");
2724  mx = 0;
2725  for(k=0; k<=mx; k++){
2726    double rEstLoop = 1.0;
2727    for(i=n=0; 1; i++){
2728      sqlite3_stmt *p = pArg->pStmt;
2729      sqlite3_int64 nLoop, nVisit;
2730      double rEst;
2731      int iSid;
2732      const char *zExplain;
2733      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2734        break;
2735      }
2736      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2737      if( iSid>mx ) mx = iSid;
2738      if( iSid!=k ) continue;
2739      if( n==0 ){
2740        rEstLoop = (double)nLoop;
2741        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2742      }
2743      n++;
2744      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2745      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2746      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2747      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2748      rEstLoop *= rEst;
2749      raw_printf(pArg->out,
2750          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2751          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2752      );
2753    }
2754  }
2755  raw_printf(pArg->out, "---------------------------\n");
2756#endif
2757}
2758
2759/*
2760** Parameter azArray points to a zero-terminated array of strings. zStr
2761** points to a single nul-terminated string. Return non-zero if zStr
2762** is equal, according to strcmp(), to any of the strings in the array.
2763** Otherwise, return zero.
2764*/
2765static int str_in_array(const char *zStr, const char **azArray){
2766  int i;
2767  for(i=0; azArray[i]; i++){
2768    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2769  }
2770  return 0;
2771}
2772
2773/*
2774** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2775** and populate the ShellState.aiIndent[] array with the number of
2776** spaces each opcode should be indented before it is output.
2777**
2778** The indenting rules are:
2779**
2780**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2781**       all opcodes that occur between the p2 jump destination and the opcode
2782**       itself by 2 spaces.
2783**
2784**     * For each "Goto", if the jump destination is earlier in the program
2785**       and ends on one of:
2786**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2787**       or if the P1 parameter is one instead of zero,
2788**       then indent all opcodes between the earlier instruction
2789**       and "Goto" by 2 spaces.
2790*/
2791static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2792  const char *zSql;               /* The text of the SQL statement */
2793  const char *z;                  /* Used to check if this is an EXPLAIN */
2794  int *abYield = 0;               /* True if op is an OP_Yield */
2795  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2796  int iOp;                        /* Index of operation in p->aiIndent[] */
2797
2798  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2799  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2800                            "Rewind", 0 };
2801  const char *azGoto[] = { "Goto", 0 };
2802
2803  /* Try to figure out if this is really an EXPLAIN statement. If this
2804  ** cannot be verified, return early.  */
2805  if( sqlite3_column_count(pSql)!=8 ){
2806    p->cMode = p->mode;
2807    return;
2808  }
2809  zSql = sqlite3_sql(pSql);
2810  if( zSql==0 ) return;
2811  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2812  if( sqlite3_strnicmp(z, "explain", 7) ){
2813    p->cMode = p->mode;
2814    return;
2815  }
2816
2817  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2818    int i;
2819    int iAddr = sqlite3_column_int(pSql, 0);
2820    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2821
2822    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2823    ** p2 is an instruction address, set variable p2op to the index of that
2824    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2825    ** the current instruction is part of a sub-program generated by an
2826    ** SQL trigger or foreign key.  */
2827    int p2 = sqlite3_column_int(pSql, 3);
2828    int p2op = (p2 + (iOp-iAddr));
2829
2830    /* Grow the p->aiIndent array as required */
2831    if( iOp>=nAlloc ){
2832      if( iOp==0 ){
2833        /* Do further verfication that this is explain output.  Abort if
2834        ** it is not */
2835        static const char *explainCols[] = {
2836           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2837        int jj;
2838        for(jj=0; jj<ArraySize(explainCols); jj++){
2839          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2840            p->cMode = p->mode;
2841            sqlite3_reset(pSql);
2842            return;
2843          }
2844        }
2845      }
2846      nAlloc += 100;
2847      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2848      if( p->aiIndent==0 ) shell_out_of_memory();
2849      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2850      if( abYield==0 ) shell_out_of_memory();
2851    }
2852    abYield[iOp] = str_in_array(zOp, azYield);
2853    p->aiIndent[iOp] = 0;
2854    p->nIndent = iOp+1;
2855
2856    if( str_in_array(zOp, azNext) ){
2857      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2858    }
2859    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2860     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2861    ){
2862      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2863    }
2864  }
2865
2866  p->iIndent = 0;
2867  sqlite3_free(abYield);
2868  sqlite3_reset(pSql);
2869}
2870
2871/*
2872** Free the array allocated by explain_data_prepare().
2873*/
2874static void explain_data_delete(ShellState *p){
2875  sqlite3_free(p->aiIndent);
2876  p->aiIndent = 0;
2877  p->nIndent = 0;
2878  p->iIndent = 0;
2879}
2880
2881/*
2882** Disable and restore .wheretrace and .selecttrace settings.
2883*/
2884#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2885extern unsigned int sqlite3_unsupported_selecttrace;
2886static int savedSelectTrace;
2887#endif
2888#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2889extern int sqlite3WhereTrace;
2890static int savedWhereTrace;
2891#endif
2892static void disable_debug_trace_modes(void){
2893#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2894  savedSelectTrace = sqlite3_unsupported_selecttrace;
2895  sqlite3_unsupported_selecttrace = 0;
2896#endif
2897#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2898  savedWhereTrace = sqlite3WhereTrace;
2899  sqlite3WhereTrace = 0;
2900#endif
2901}
2902static void restore_debug_trace_modes(void){
2903#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2904  sqlite3_unsupported_selecttrace = savedSelectTrace;
2905#endif
2906#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2907  sqlite3WhereTrace = savedWhereTrace;
2908#endif
2909}
2910
2911/* Create the TEMP table used to store parameter bindings */
2912static void bind_table_init(ShellState *p){
2913  int wrSchema = 0;
2914  int defensiveMode = 0;
2915  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2916  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2917  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2918  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2919  sqlite3_exec(p->db,
2920    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2921    "  key TEXT PRIMARY KEY,\n"
2922    "  value ANY\n"
2923    ") WITHOUT ROWID;",
2924    0, 0, 0);
2925  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2926  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2927}
2928
2929/*
2930** Bind parameters on a prepared statement.
2931**
2932** Parameter bindings are taken from a TEMP table of the form:
2933**
2934**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2935**    WITHOUT ROWID;
2936**
2937** No bindings occur if this table does not exist.  The name of the table
2938** begins with "sqlite_" so that it will not collide with ordinary application
2939** tables.  The table must be in the TEMP schema.
2940*/
2941static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2942  int nVar;
2943  int i;
2944  int rc;
2945  sqlite3_stmt *pQ = 0;
2946
2947  nVar = sqlite3_bind_parameter_count(pStmt);
2948  if( nVar==0 ) return;  /* Nothing to do */
2949  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2950                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2951    return; /* Parameter table does not exist */
2952  }
2953  rc = sqlite3_prepare_v2(pArg->db,
2954          "SELECT value FROM temp.sqlite_parameters"
2955          " WHERE key=?1", -1, &pQ, 0);
2956  if( rc || pQ==0 ) return;
2957  for(i=1; i<=nVar; i++){
2958    char zNum[30];
2959    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2960    if( zVar==0 ){
2961      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2962      zVar = zNum;
2963    }
2964    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2965    if( sqlite3_step(pQ)==SQLITE_ROW ){
2966      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2967    }else{
2968      sqlite3_bind_null(pStmt, i);
2969    }
2970    sqlite3_reset(pQ);
2971  }
2972  sqlite3_finalize(pQ);
2973}
2974
2975/*
2976** UTF8 box-drawing characters.  Imagine box lines like this:
2977**
2978**           1
2979**           |
2980**       4 --+-- 2
2981**           |
2982**           3
2983**
2984** Each box characters has between 2 and 4 of the lines leading from
2985** the center.  The characters are here identified by the numbers of
2986** their corresponding lines.
2987*/
2988#define BOX_24   "\342\224\200"  /* U+2500 --- */
2989#define BOX_13   "\342\224\202"  /* U+2502  |  */
2990#define BOX_23   "\342\224\214"  /* U+250c  ,- */
2991#define BOX_34   "\342\224\220"  /* U+2510 -,  */
2992#define BOX_12   "\342\224\224"  /* U+2514  '- */
2993#define BOX_14   "\342\224\230"  /* U+2518 -'  */
2994#define BOX_123  "\342\224\234"  /* U+251c  |- */
2995#define BOX_134  "\342\224\244"  /* U+2524 -|  */
2996#define BOX_234  "\342\224\254"  /* U+252c -,- */
2997#define BOX_124  "\342\224\264"  /* U+2534 -'- */
2998#define BOX_1234 "\342\224\274"  /* U+253c -|- */
2999
3000/* Draw horizontal line N characters long using unicode box
3001** characters
3002*/
3003static void print_box_line(FILE *out, int N){
3004  const char zDash[] =
3005      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3006      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3007  const int nDash = sizeof(zDash) - 1;
3008  N *= 3;
3009  while( N>nDash ){
3010    utf8_printf(out, zDash);
3011    N -= nDash;
3012  }
3013  utf8_printf(out, "%.*s", N, zDash);
3014}
3015
3016/*
3017** Draw a horizontal separator for a MODE_Box table.
3018*/
3019static void print_box_row_separator(
3020  ShellState *p,
3021  int nArg,
3022  const char *zSep1,
3023  const char *zSep2,
3024  const char *zSep3
3025){
3026  int i;
3027  if( nArg>0 ){
3028    utf8_printf(p->out, "%s", zSep1);
3029    print_box_line(p->out, p->actualWidth[0]+2);
3030    for(i=1; i<nArg; i++){
3031      utf8_printf(p->out, "%s", zSep2);
3032      print_box_line(p->out, p->actualWidth[i]+2);
3033    }
3034    utf8_printf(p->out, "%s", zSep3);
3035  }
3036  fputs("\n", p->out);
3037}
3038
3039
3040
3041/*
3042** Run a prepared statement and output the result in one of the
3043** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3044** or MODE_Box.
3045**
3046** This is different from ordinary exec_prepared_stmt() in that
3047** it has to run the entire query and gather the results into memory
3048** first, in order to determine column widths, before providing
3049** any output.
3050*/
3051static void exec_prepared_stmt_columnar(
3052  ShellState *p,                        /* Pointer to ShellState */
3053  sqlite3_stmt *pStmt                   /* Statment to run */
3054){
3055  sqlite3_int64 nRow = 0;
3056  int nColumn = 0;
3057  char **azData = 0;
3058  sqlite3_int64 nAlloc = 0;
3059  const char *z;
3060  int rc;
3061  sqlite3_int64 i, nData;
3062  int j, nTotal, w, n;
3063  const char *colSep = 0;
3064  const char *rowSep = 0;
3065
3066  rc = sqlite3_step(pStmt);
3067  if( rc!=SQLITE_ROW ) return;
3068  nColumn = sqlite3_column_count(pStmt);
3069  nAlloc = nColumn*4;
3070  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3071  if( azData==0 ) shell_out_of_memory();
3072  for(i=0; i<nColumn; i++){
3073    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3074  }
3075  do{
3076    if( (nRow+2)*nColumn >= nAlloc ){
3077      nAlloc *= 2;
3078      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3079      if( azData==0 ) shell_out_of_memory();
3080    }
3081    nRow++;
3082    for(i=0; i<nColumn; i++){
3083      z = (const char*)sqlite3_column_text(pStmt,i);
3084      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3085    }
3086  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
3087  if( nColumn>p->nWidth ){
3088    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3089    if( p->colWidth==0 ) shell_out_of_memory();
3090    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3091    p->nWidth = nColumn;
3092    p->actualWidth = &p->colWidth[nColumn];
3093  }
3094  memset(p->actualWidth, 0, nColumn*sizeof(int));
3095  for(i=0; i<nColumn; i++){
3096    w = p->colWidth[i];
3097    if( w<0 ) w = -w;
3098    p->actualWidth[i] = w;
3099  }
3100  nTotal = nColumn*(nRow+1);
3101  for(i=0; i<nTotal; i++){
3102    z = azData[i];
3103    if( z==0 ) z = p->nullValue;
3104    n = strlenChar(z);
3105    j = i%nColumn;
3106    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3107  }
3108  if( seenInterrupt ) 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#sqlar_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 ?TABLE?            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  "   TABLE is a LIKE pattern for the tables 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#ifdef SQLITE_ENABLE_DESERIALIZE
3984  "        --deserialize   Load into memory useing 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 ?on|off?          Show stats or turn stats on or off",
4064#ifndef SQLITE_NOHAVE_SYSTEM
4065  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4066#endif
4067  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4068  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4069  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4070  "                           Run \".testctrl\" with no arguments for details",
4071  ".timeout MS              Try opening locked tables for MS milliseconds",
4072  ".timer on|off            Turn SQL timer on or off",
4073#ifndef SQLITE_OMIT_TRACE
4074  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4075  "    FILE                    Send output to FILE",
4076  "    stdout                  Send output to stdout",
4077  "    stderr                  Send output to stderr",
4078  "    off                     Disable tracing",
4079  "    --expanded              Expand query parameters",
4080#ifdef SQLITE_ENABLE_NORMALIZE
4081  "    --normalized            Normal the SQL statements",
4082#endif
4083  "    --plain                 Show SQL as it is input",
4084  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4085  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4086  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4087  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4088#endif /* SQLITE_OMIT_TRACE */
4089#ifdef SQLITE_DEBUG
4090  ".unmodule NAME ...       Unregister virtual table modules",
4091  "    --allexcept             Unregister everything except those named",
4092#endif
4093  ".vfsinfo ?AUX?           Information about the top-level VFS",
4094  ".vfslist                 List all available VFSes",
4095  ".vfsname ?AUX?           Print the name of the VFS stack",
4096  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4097  "     Negative values right-justify",
4098};
4099
4100/*
4101** Output help text.
4102**
4103** zPattern describes the set of commands for which help text is provided.
4104** If zPattern is NULL, then show all commands, but only give a one-line
4105** description of each.
4106**
4107** Return the number of matches.
4108*/
4109static int showHelp(FILE *out, const char *zPattern){
4110  int i = 0;
4111  int j = 0;
4112  int n = 0;
4113  char *zPat;
4114  if( zPattern==0
4115   || zPattern[0]=='0'
4116   || strcmp(zPattern,"-a")==0
4117   || strcmp(zPattern,"-all")==0
4118   || strcmp(zPattern,"--all")==0
4119  ){
4120    /* Show all commands, but only one line per command */
4121    if( zPattern==0 ) zPattern = "";
4122    for(i=0; i<ArraySize(azHelp); i++){
4123      if( azHelp[i][0]=='.' || zPattern[0] ){
4124        utf8_printf(out, "%s\n", azHelp[i]);
4125        n++;
4126      }
4127    }
4128  }else{
4129    /* Look for commands that for which zPattern is an exact prefix */
4130    zPat = sqlite3_mprintf(".%s*", zPattern);
4131    for(i=0; i<ArraySize(azHelp); i++){
4132      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4133        utf8_printf(out, "%s\n", azHelp[i]);
4134        j = i+1;
4135        n++;
4136      }
4137    }
4138    sqlite3_free(zPat);
4139    if( n ){
4140      if( n==1 ){
4141        /* when zPattern is a prefix of exactly one command, then include the
4142        ** details of that command, which should begin at offset j */
4143        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4144          utf8_printf(out, "%s\n", azHelp[j]);
4145          j++;
4146        }
4147      }
4148      return n;
4149    }
4150    /* Look for commands that contain zPattern anywhere.  Show the complete
4151    ** text of all commands that match. */
4152    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4153    for(i=0; i<ArraySize(azHelp); i++){
4154      if( azHelp[i][0]=='.' ) j = i;
4155      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4156        utf8_printf(out, "%s\n", azHelp[j]);
4157        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4158          j++;
4159          utf8_printf(out, "%s\n", azHelp[j]);
4160        }
4161        i = j;
4162        n++;
4163      }
4164    }
4165    sqlite3_free(zPat);
4166  }
4167  return n;
4168}
4169
4170/* Forward reference */
4171static int process_input(ShellState *p);
4172
4173/*
4174** Read the content of file zName into memory obtained from sqlite3_malloc64()
4175** and return a pointer to the buffer. The caller is responsible for freeing
4176** the memory.
4177**
4178** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4179** read.
4180**
4181** For convenience, a nul-terminator byte is always appended to the data read
4182** from the file before the buffer is returned. This byte is not included in
4183** the final value of (*pnByte), if applicable.
4184**
4185** NULL is returned if any error is encountered. The final value of *pnByte
4186** is undefined in this case.
4187*/
4188static char *readFile(const char *zName, int *pnByte){
4189  FILE *in = fopen(zName, "rb");
4190  long nIn;
4191  size_t nRead;
4192  char *pBuf;
4193  if( in==0 ) return 0;
4194  fseek(in, 0, SEEK_END);
4195  nIn = ftell(in);
4196  rewind(in);
4197  pBuf = sqlite3_malloc64( nIn+1 );
4198  if( pBuf==0 ){ fclose(in); return 0; }
4199  nRead = fread(pBuf, nIn, 1, in);
4200  fclose(in);
4201  if( nRead!=1 ){
4202    sqlite3_free(pBuf);
4203    return 0;
4204  }
4205  pBuf[nIn] = 0;
4206  if( pnByte ) *pnByte = nIn;
4207  return pBuf;
4208}
4209
4210#if defined(SQLITE_ENABLE_SESSION)
4211/*
4212** Close a single OpenSession object and release all of its associated
4213** resources.
4214*/
4215static void session_close(OpenSession *pSession){
4216  int i;
4217  sqlite3session_delete(pSession->p);
4218  sqlite3_free(pSession->zName);
4219  for(i=0; i<pSession->nFilter; i++){
4220    sqlite3_free(pSession->azFilter[i]);
4221  }
4222  sqlite3_free(pSession->azFilter);
4223  memset(pSession, 0, sizeof(OpenSession));
4224}
4225#endif
4226
4227/*
4228** Close all OpenSession objects and release all associated resources.
4229*/
4230#if defined(SQLITE_ENABLE_SESSION)
4231static void session_close_all(ShellState *p){
4232  int i;
4233  for(i=0; i<p->nSession; i++){
4234    session_close(&p->aSession[i]);
4235  }
4236  p->nSession = 0;
4237}
4238#else
4239# define session_close_all(X)
4240#endif
4241
4242/*
4243** Implementation of the xFilter function for an open session.  Omit
4244** any tables named by ".session filter" but let all other table through.
4245*/
4246#if defined(SQLITE_ENABLE_SESSION)
4247static int session_filter(void *pCtx, const char *zTab){
4248  OpenSession *pSession = (OpenSession*)pCtx;
4249  int i;
4250  for(i=0; i<pSession->nFilter; i++){
4251    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4252  }
4253  return 1;
4254}
4255#endif
4256
4257/*
4258** Try to deduce the type of file for zName based on its content.  Return
4259** one of the SHELL_OPEN_* constants.
4260**
4261** If the file does not exist or is empty but its name looks like a ZIP
4262** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4263** Otherwise, assume an ordinary database regardless of the filename if
4264** the type cannot be determined from content.
4265*/
4266int deduceDatabaseType(const char *zName, int dfltZip){
4267  FILE *f = fopen(zName, "rb");
4268  size_t n;
4269  int rc = SHELL_OPEN_UNSPEC;
4270  char zBuf[100];
4271  if( f==0 ){
4272    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4273       return SHELL_OPEN_ZIPFILE;
4274    }else{
4275       return SHELL_OPEN_NORMAL;
4276    }
4277  }
4278  n = fread(zBuf, 16, 1, f);
4279  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4280    fclose(f);
4281    return SHELL_OPEN_NORMAL;
4282  }
4283  fseek(f, -25, SEEK_END);
4284  n = fread(zBuf, 25, 1, f);
4285  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4286    rc = SHELL_OPEN_APPENDVFS;
4287  }else{
4288    fseek(f, -22, SEEK_END);
4289    n = fread(zBuf, 22, 1, f);
4290    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4291       && zBuf[3]==0x06 ){
4292      rc = SHELL_OPEN_ZIPFILE;
4293    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4294      rc = SHELL_OPEN_ZIPFILE;
4295    }
4296  }
4297  fclose(f);
4298  return rc;
4299}
4300
4301#ifdef SQLITE_ENABLE_DESERIALIZE
4302/*
4303** Reconstruct an in-memory database using the output from the "dbtotxt"
4304** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
4305** is 0, then read from standard input.
4306*/
4307static unsigned char *readHexDb(ShellState *p, int *pnData){
4308  unsigned char *a = 0;
4309  int nLine;
4310  int n = 0;
4311  int pgsz = 0;
4312  int iOffset = 0;
4313  int j, k;
4314  int rc;
4315  FILE *in;
4316  unsigned int x[16];
4317  char zLine[1000];
4318  if( p->zDbFilename ){
4319    in = fopen(p->zDbFilename, "r");
4320    if( in==0 ){
4321      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
4322      return 0;
4323    }
4324    nLine = 0;
4325  }else{
4326    in = p->in;
4327    nLine = p->lineno;
4328    if( in==0 ) in = stdin;
4329  }
4330  *pnData = 0;
4331  nLine++;
4332  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4333  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4334  if( rc!=2 ) goto readHexDb_error;
4335  if( n<0 ) goto readHexDb_error;
4336  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4337  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4338  a = sqlite3_malloc( n ? n : 1 );
4339  if( a==0 ){
4340    utf8_printf(stderr, "Out of memory!\n");
4341    goto readHexDb_error;
4342  }
4343  memset(a, 0, n);
4344  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4345    utf8_printf(stderr, "invalid pagesize\n");
4346    goto readHexDb_error;
4347  }
4348  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4349    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4350    if( rc==2 ){
4351      iOffset = k;
4352      continue;
4353    }
4354    if( strncmp(zLine, "| end ", 6)==0 ){
4355      break;
4356    }
4357    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4358                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4359                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4360    if( rc==17 ){
4361      k = iOffset+j;
4362      if( k+16<=n ){
4363        int ii;
4364        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4365      }
4366    }
4367  }
4368  *pnData = n;
4369  if( in!=p->in ){
4370    fclose(in);
4371  }else{
4372    p->lineno = nLine;
4373  }
4374  return a;
4375
4376readHexDb_error:
4377  if( in!=p->in ){
4378    fclose(in);
4379  }else{
4380    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4381      nLine++;
4382      if(strncmp(zLine, "| end ", 6)==0 ) break;
4383    }
4384    p->lineno = nLine;
4385  }
4386  sqlite3_free(a);
4387  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4388  return 0;
4389}
4390#endif /* SQLITE_ENABLE_DESERIALIZE */
4391
4392/*
4393** Scalar function "shell_int32". The first argument to this function
4394** must be a blob. The second a non-negative integer. This function
4395** reads and returns a 32-bit big-endian integer from byte
4396** offset (4*<arg2>) of the blob.
4397*/
4398static void shellInt32(
4399  sqlite3_context *context,
4400  int argc,
4401  sqlite3_value **argv
4402){
4403  const unsigned char *pBlob;
4404  int nBlob;
4405  int iInt;
4406
4407  UNUSED_PARAMETER(argc);
4408  nBlob = sqlite3_value_bytes(argv[0]);
4409  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4410  iInt = sqlite3_value_int(argv[1]);
4411
4412  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4413    const unsigned char *a = &pBlob[iInt*4];
4414    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4415                       + ((sqlite3_int64)a[1]<<16)
4416                       + ((sqlite3_int64)a[2]<< 8)
4417                       + ((sqlite3_int64)a[3]<< 0);
4418    sqlite3_result_int64(context, iVal);
4419  }
4420}
4421
4422/*
4423** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4424** using "..." with internal double-quote characters doubled.
4425*/
4426static void shellIdQuote(
4427  sqlite3_context *context,
4428  int argc,
4429  sqlite3_value **argv
4430){
4431  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4432  UNUSED_PARAMETER(argc);
4433  if( zName ){
4434    char *z = sqlite3_mprintf("\"%w\"", zName);
4435    sqlite3_result_text(context, z, -1, sqlite3_free);
4436  }
4437}
4438
4439/*
4440** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4441*/
4442static void shellUSleepFunc(
4443  sqlite3_context *context,
4444  int argc,
4445  sqlite3_value **argv
4446){
4447  int sleep = sqlite3_value_int(argv[0]);
4448  sqlite3_sleep(sleep/1000);
4449  sqlite3_result_int(context, sleep);
4450}
4451
4452/*
4453** Scalar function "shell_escape_crnl" used by the .recover command.
4454** The argument passed to this function is the output of built-in
4455** function quote(). If the first character of the input is "'",
4456** indicating that the value passed to quote() was a text value,
4457** then this function searches the input for "\n" and "\r" characters
4458** and adds a wrapper similar to the following:
4459**
4460**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4461**
4462** Or, if the first character of the input is not "'", then a copy
4463** of the input is returned.
4464*/
4465static void shellEscapeCrnl(
4466  sqlite3_context *context,
4467  int argc,
4468  sqlite3_value **argv
4469){
4470  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4471  UNUSED_PARAMETER(argc);
4472  if( zText[0]=='\'' ){
4473    int nText = sqlite3_value_bytes(argv[0]);
4474    int i;
4475    char zBuf1[20];
4476    char zBuf2[20];
4477    const char *zNL = 0;
4478    const char *zCR = 0;
4479    int nCR = 0;
4480    int nNL = 0;
4481
4482    for(i=0; zText[i]; i++){
4483      if( zNL==0 && zText[i]=='\n' ){
4484        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4485        nNL = (int)strlen(zNL);
4486      }
4487      if( zCR==0 && zText[i]=='\r' ){
4488        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4489        nCR = (int)strlen(zCR);
4490      }
4491    }
4492
4493    if( zNL || zCR ){
4494      int iOut = 0;
4495      i64 nMax = (nNL > nCR) ? nNL : nCR;
4496      i64 nAlloc = nMax * nText + (nMax+64)*2;
4497      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4498      if( zOut==0 ){
4499        sqlite3_result_error_nomem(context);
4500        return;
4501      }
4502
4503      if( zNL && zCR ){
4504        memcpy(&zOut[iOut], "replace(replace(", 16);
4505        iOut += 16;
4506      }else{
4507        memcpy(&zOut[iOut], "replace(", 8);
4508        iOut += 8;
4509      }
4510      for(i=0; zText[i]; i++){
4511        if( zText[i]=='\n' ){
4512          memcpy(&zOut[iOut], zNL, nNL);
4513          iOut += nNL;
4514        }else if( zText[i]=='\r' ){
4515          memcpy(&zOut[iOut], zCR, nCR);
4516          iOut += nCR;
4517        }else{
4518          zOut[iOut] = zText[i];
4519          iOut++;
4520        }
4521      }
4522
4523      if( zNL ){
4524        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4525        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4526        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4527      }
4528      if( zCR ){
4529        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4530        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4531        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4532      }
4533
4534      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4535      sqlite3_free(zOut);
4536      return;
4537    }
4538  }
4539
4540  sqlite3_result_value(context, argv[0]);
4541}
4542
4543/* Flags for open_db().
4544**
4545** The default behavior of open_db() is to exit(1) if the database fails to
4546** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4547** but still returns without calling exit.
4548**
4549** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4550** ZIP archive if the file does not exist or is empty and its name matches
4551** the *.zip pattern.
4552*/
4553#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4554#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4555
4556/*
4557** Make sure the database is open.  If it is not, then open it.  If
4558** the database fails to open, print an error message and exit.
4559*/
4560static void open_db(ShellState *p, int openFlags){
4561  if( p->db==0 ){
4562    if( p->openMode==SHELL_OPEN_UNSPEC ){
4563      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4564        p->openMode = SHELL_OPEN_NORMAL;
4565      }else{
4566        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4567                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4568      }
4569    }
4570    switch( p->openMode ){
4571      case SHELL_OPEN_APPENDVFS: {
4572        sqlite3_open_v2(p->zDbFilename, &p->db,
4573           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4574        break;
4575      }
4576      case SHELL_OPEN_HEXDB:
4577      case SHELL_OPEN_DESERIALIZE: {
4578        sqlite3_open(0, &p->db);
4579        break;
4580      }
4581      case SHELL_OPEN_ZIPFILE: {
4582        sqlite3_open(":memory:", &p->db);
4583        break;
4584      }
4585      case SHELL_OPEN_READONLY: {
4586        sqlite3_open_v2(p->zDbFilename, &p->db,
4587            SQLITE_OPEN_READONLY|p->openFlags, 0);
4588        break;
4589      }
4590      case SHELL_OPEN_UNSPEC:
4591      case SHELL_OPEN_NORMAL: {
4592        sqlite3_open_v2(p->zDbFilename, &p->db,
4593           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4594        break;
4595      }
4596    }
4597    globalDb = p->db;
4598    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4599      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4600          p->zDbFilename, sqlite3_errmsg(p->db));
4601      if( openFlags & OPEN_DB_KEEPALIVE ){
4602        sqlite3_open(":memory:", &p->db);
4603        return;
4604      }
4605      exit(1);
4606    }
4607#ifndef SQLITE_OMIT_LOAD_EXTENSION
4608    sqlite3_enable_load_extension(p->db, 1);
4609#endif
4610    sqlite3_fileio_init(p->db, 0, 0);
4611    sqlite3_shathree_init(p->db, 0, 0);
4612    sqlite3_completion_init(p->db, 0, 0);
4613    sqlite3_uint_init(p->db, 0, 0);
4614    sqlite3_decimal_init(p->db, 0, 0);
4615    sqlite3_ieee_init(p->db, 0, 0);
4616    sqlite3_series_init(p->db, 0, 0);
4617#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4618    sqlite3_dbdata_init(p->db, 0, 0);
4619#endif
4620#ifdef SQLITE_HAVE_ZLIB
4621    sqlite3_zipfile_init(p->db, 0, 0);
4622    sqlite3_sqlar_init(p->db, 0, 0);
4623#endif
4624    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4625                            shellAddSchemaName, 0, 0);
4626    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4627                            shellModuleSchema, 0, 0);
4628    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4629                            shellPutsFunc, 0, 0);
4630    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4631                            shellEscapeCrnl, 0, 0);
4632    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4633                            shellInt32, 0, 0);
4634    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4635                            shellIdQuote, 0, 0);
4636    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
4637                            shellUSleepFunc, 0, 0);
4638#ifndef SQLITE_NOHAVE_SYSTEM
4639    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4640                            editFunc, 0, 0);
4641    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4642                            editFunc, 0, 0);
4643#endif
4644    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4645      char *zSql = sqlite3_mprintf(
4646         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4647      sqlite3_exec(p->db, zSql, 0, 0, 0);
4648      sqlite3_free(zSql);
4649    }
4650#ifdef SQLITE_ENABLE_DESERIALIZE
4651    else
4652    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4653      int rc;
4654      int nData = 0;
4655      unsigned char *aData;
4656      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4657        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4658      }else{
4659        aData = readHexDb(p, &nData);
4660        if( aData==0 ){
4661          return;
4662        }
4663      }
4664      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4665                   SQLITE_DESERIALIZE_RESIZEABLE |
4666                   SQLITE_DESERIALIZE_FREEONCLOSE);
4667      if( rc ){
4668        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4669      }
4670      if( p->szMax>0 ){
4671        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4672      }
4673    }
4674#endif
4675  }
4676}
4677
4678/*
4679** Attempt to close the databaes connection.  Report errors.
4680*/
4681void close_db(sqlite3 *db){
4682  int rc = sqlite3_close(db);
4683  if( rc ){
4684    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4685        rc, sqlite3_errmsg(db));
4686  }
4687}
4688
4689#if HAVE_READLINE || HAVE_EDITLINE
4690/*
4691** Readline completion callbacks
4692*/
4693static char *readline_completion_generator(const char *text, int state){
4694  static sqlite3_stmt *pStmt = 0;
4695  char *zRet;
4696  if( state==0 ){
4697    char *zSql;
4698    sqlite3_finalize(pStmt);
4699    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4700                           "  FROM completion(%Q) ORDER BY 1", text);
4701    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4702    sqlite3_free(zSql);
4703  }
4704  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4705    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4706  }else{
4707    sqlite3_finalize(pStmt);
4708    pStmt = 0;
4709    zRet = 0;
4710  }
4711  return zRet;
4712}
4713static char **readline_completion(const char *zText, int iStart, int iEnd){
4714  rl_attempted_completion_over = 1;
4715  return rl_completion_matches(zText, readline_completion_generator);
4716}
4717
4718#elif HAVE_LINENOISE
4719/*
4720** Linenoise completion callback
4721*/
4722static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4723  int nLine = strlen30(zLine);
4724  int i, iStart;
4725  sqlite3_stmt *pStmt = 0;
4726  char *zSql;
4727  char zBuf[1000];
4728
4729  if( nLine>sizeof(zBuf)-30 ) return;
4730  if( zLine[0]=='.' || zLine[0]=='#') return;
4731  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4732  if( i==nLine-1 ) return;
4733  iStart = i+1;
4734  memcpy(zBuf, zLine, iStart);
4735  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4736                         "  FROM completion(%Q,%Q) ORDER BY 1",
4737                         &zLine[iStart], zLine);
4738  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4739  sqlite3_free(zSql);
4740  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4741  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4742    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4743    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4744    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4745      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4746      linenoiseAddCompletion(lc, zBuf);
4747    }
4748  }
4749  sqlite3_finalize(pStmt);
4750}
4751#endif
4752
4753/*
4754** Do C-language style dequoting.
4755**
4756**    \a    -> alarm
4757**    \b    -> backspace
4758**    \t    -> tab
4759**    \n    -> newline
4760**    \v    -> vertical tab
4761**    \f    -> form feed
4762**    \r    -> carriage return
4763**    \s    -> space
4764**    \"    -> "
4765**    \'    -> '
4766**    \\    -> backslash
4767**    \NNN  -> ascii character NNN in octal
4768*/
4769static void resolve_backslashes(char *z){
4770  int i, j;
4771  char c;
4772  while( *z && *z!='\\' ) z++;
4773  for(i=j=0; (c = z[i])!=0; i++, j++){
4774    if( c=='\\' && z[i+1]!=0 ){
4775      c = z[++i];
4776      if( c=='a' ){
4777        c = '\a';
4778      }else if( c=='b' ){
4779        c = '\b';
4780      }else if( c=='t' ){
4781        c = '\t';
4782      }else if( c=='n' ){
4783        c = '\n';
4784      }else if( c=='v' ){
4785        c = '\v';
4786      }else if( c=='f' ){
4787        c = '\f';
4788      }else if( c=='r' ){
4789        c = '\r';
4790      }else if( c=='"' ){
4791        c = '"';
4792      }else if( c=='\'' ){
4793        c = '\'';
4794      }else if( c=='\\' ){
4795        c = '\\';
4796      }else if( c>='0' && c<='7' ){
4797        c -= '0';
4798        if( z[i+1]>='0' && z[i+1]<='7' ){
4799          i++;
4800          c = (c<<3) + z[i] - '0';
4801          if( z[i+1]>='0' && z[i+1]<='7' ){
4802            i++;
4803            c = (c<<3) + z[i] - '0';
4804          }
4805        }
4806      }
4807    }
4808    z[j] = c;
4809  }
4810  if( j<i ) z[j] = 0;
4811}
4812
4813/*
4814** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4815** for TRUE and FALSE.  Return the integer value if appropriate.
4816*/
4817static int booleanValue(const char *zArg){
4818  int i;
4819  if( zArg[0]=='0' && zArg[1]=='x' ){
4820    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4821  }else{
4822    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4823  }
4824  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4825  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4826    return 1;
4827  }
4828  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4829    return 0;
4830  }
4831  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4832          zArg);
4833  return 0;
4834}
4835
4836/*
4837** Set or clear a shell flag according to a boolean value.
4838*/
4839static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4840  if( booleanValue(zArg) ){
4841    ShellSetFlag(p, mFlag);
4842  }else{
4843    ShellClearFlag(p, mFlag);
4844  }
4845}
4846
4847/*
4848** Close an output file, assuming it is not stderr or stdout
4849*/
4850static void output_file_close(FILE *f){
4851  if( f && f!=stdout && f!=stderr ) fclose(f);
4852}
4853
4854/*
4855** Try to open an output file.   The names "stdout" and "stderr" are
4856** recognized and do the right thing.  NULL is returned if the output
4857** filename is "off".
4858*/
4859static FILE *output_file_open(const char *zFile, int bTextMode){
4860  FILE *f;
4861  if( strcmp(zFile,"stdout")==0 ){
4862    f = stdout;
4863  }else if( strcmp(zFile, "stderr")==0 ){
4864    f = stderr;
4865  }else if( strcmp(zFile, "off")==0 ){
4866    f = 0;
4867  }else{
4868    f = fopen(zFile, bTextMode ? "w" : "wb");
4869    if( f==0 ){
4870      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4871    }
4872  }
4873  return f;
4874}
4875
4876#ifndef SQLITE_OMIT_TRACE
4877/*
4878** A routine for handling output from sqlite3_trace().
4879*/
4880static int sql_trace_callback(
4881  unsigned mType,         /* The trace type */
4882  void *pArg,             /* The ShellState pointer */
4883  void *pP,               /* Usually a pointer to sqlite_stmt */
4884  void *pX                /* Auxiliary output */
4885){
4886  ShellState *p = (ShellState*)pArg;
4887  sqlite3_stmt *pStmt;
4888  const char *zSql;
4889  int nSql;
4890  if( p->traceOut==0 ) return 0;
4891  if( mType==SQLITE_TRACE_CLOSE ){
4892    utf8_printf(p->traceOut, "-- closing database connection\n");
4893    return 0;
4894  }
4895  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4896    zSql = (const char*)pX;
4897  }else{
4898    pStmt = (sqlite3_stmt*)pP;
4899    switch( p->eTraceType ){
4900      case SHELL_TRACE_EXPANDED: {
4901        zSql = sqlite3_expanded_sql(pStmt);
4902        break;
4903      }
4904#ifdef SQLITE_ENABLE_NORMALIZE
4905      case SHELL_TRACE_NORMALIZED: {
4906        zSql = sqlite3_normalized_sql(pStmt);
4907        break;
4908      }
4909#endif
4910      default: {
4911        zSql = sqlite3_sql(pStmt);
4912        break;
4913      }
4914    }
4915  }
4916  if( zSql==0 ) return 0;
4917  nSql = strlen30(zSql);
4918  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4919  switch( mType ){
4920    case SQLITE_TRACE_ROW:
4921    case SQLITE_TRACE_STMT: {
4922      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4923      break;
4924    }
4925    case SQLITE_TRACE_PROFILE: {
4926      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4927      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4928      break;
4929    }
4930  }
4931  return 0;
4932}
4933#endif
4934
4935/*
4936** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4937** a useful spot to set a debugger breakpoint.
4938*/
4939static void test_breakpoint(void){
4940  static int nCall = 0;
4941  nCall++;
4942}
4943
4944/*
4945** An object used to read a CSV and other files for import.
4946*/
4947typedef struct ImportCtx ImportCtx;
4948struct ImportCtx {
4949  const char *zFile;  /* Name of the input file */
4950  FILE *in;           /* Read the CSV text from this input stream */
4951  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
4952  char *z;            /* Accumulated text for a field */
4953  int n;              /* Number of bytes in z */
4954  int nAlloc;         /* Space allocated for z[] */
4955  int nLine;          /* Current line number */
4956  int nRow;           /* Number of rows imported */
4957  int nErr;           /* Number of errors encountered */
4958  int bNotFirst;      /* True if one or more bytes already read */
4959  int cTerm;          /* Character that terminated the most recent field */
4960  int cColSep;        /* The column separator character.  (Usually ",") */
4961  int cRowSep;        /* The row separator character.  (Usually "\n") */
4962};
4963
4964/* Clean up resourced used by an ImportCtx */
4965static void import_cleanup(ImportCtx *p){
4966  if( p->in!=0 && p->xCloser!=0 ){
4967    p->xCloser(p->in);
4968    p->in = 0;
4969  }
4970  sqlite3_free(p->z);
4971  p->z = 0;
4972}
4973
4974/* Append a single byte to z[] */
4975static void import_append_char(ImportCtx *p, int c){
4976  if( p->n+1>=p->nAlloc ){
4977    p->nAlloc += p->nAlloc + 100;
4978    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4979    if( p->z==0 ) shell_out_of_memory();
4980  }
4981  p->z[p->n++] = (char)c;
4982}
4983
4984/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4985** with the option of having a separator other than ",".
4986**
4987**   +  Input comes from p->in.
4988**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4989**      from sqlite3_malloc64().
4990**   +  Use p->cSep as the column separator.  The default is ",".
4991**   +  Use p->rSep as the row separator.  The default is "\n".
4992**   +  Keep track of the line number in p->nLine.
4993**   +  Store the character that terminates the field in p->cTerm.  Store
4994**      EOF on end-of-file.
4995**   +  Report syntax errors on stderr
4996*/
4997static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4998  int c;
4999  int cSep = p->cColSep;
5000  int rSep = p->cRowSep;
5001  p->n = 0;
5002  c = fgetc(p->in);
5003  if( c==EOF || seenInterrupt ){
5004    p->cTerm = EOF;
5005    return 0;
5006  }
5007  if( c=='"' ){
5008    int pc, ppc;
5009    int startLine = p->nLine;
5010    int cQuote = c;
5011    pc = ppc = 0;
5012    while( 1 ){
5013      c = fgetc(p->in);
5014      if( c==rSep ) p->nLine++;
5015      if( c==cQuote ){
5016        if( pc==cQuote ){
5017          pc = 0;
5018          continue;
5019        }
5020      }
5021      if( (c==cSep && pc==cQuote)
5022       || (c==rSep && pc==cQuote)
5023       || (c==rSep && pc=='\r' && ppc==cQuote)
5024       || (c==EOF && pc==cQuote)
5025      ){
5026        do{ p->n--; }while( p->z[p->n]!=cQuote );
5027        p->cTerm = c;
5028        break;
5029      }
5030      if( pc==cQuote && c!='\r' ){
5031        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5032                p->zFile, p->nLine, cQuote);
5033      }
5034      if( c==EOF ){
5035        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5036                p->zFile, startLine, cQuote);
5037        p->cTerm = c;
5038        break;
5039      }
5040      import_append_char(p, c);
5041      ppc = pc;
5042      pc = c;
5043    }
5044  }else{
5045    /* If this is the first field being parsed and it begins with the
5046    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5047    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5048      import_append_char(p, c);
5049      c = fgetc(p->in);
5050      if( (c&0xff)==0xbb ){
5051        import_append_char(p, c);
5052        c = fgetc(p->in);
5053        if( (c&0xff)==0xbf ){
5054          p->bNotFirst = 1;
5055          p->n = 0;
5056          return csv_read_one_field(p);
5057        }
5058      }
5059    }
5060    while( c!=EOF && c!=cSep && c!=rSep ){
5061      import_append_char(p, c);
5062      c = fgetc(p->in);
5063    }
5064    if( c==rSep ){
5065      p->nLine++;
5066      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5067    }
5068    p->cTerm = c;
5069  }
5070  if( p->z ) p->z[p->n] = 0;
5071  p->bNotFirst = 1;
5072  return p->z;
5073}
5074
5075/* Read a single field of ASCII delimited text.
5076**
5077**   +  Input comes from p->in.
5078**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5079**      from sqlite3_malloc64().
5080**   +  Use p->cSep as the column separator.  The default is "\x1F".
5081**   +  Use p->rSep as the row separator.  The default is "\x1E".
5082**   +  Keep track of the row number in p->nLine.
5083**   +  Store the character that terminates the field in p->cTerm.  Store
5084**      EOF on end-of-file.
5085**   +  Report syntax errors on stderr
5086*/
5087static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5088  int c;
5089  int cSep = p->cColSep;
5090  int rSep = p->cRowSep;
5091  p->n = 0;
5092  c = fgetc(p->in);
5093  if( c==EOF || seenInterrupt ){
5094    p->cTerm = EOF;
5095    return 0;
5096  }
5097  while( c!=EOF && c!=cSep && c!=rSep ){
5098    import_append_char(p, c);
5099    c = fgetc(p->in);
5100  }
5101  if( c==rSep ){
5102    p->nLine++;
5103  }
5104  p->cTerm = c;
5105  if( p->z ) p->z[p->n] = 0;
5106  return p->z;
5107}
5108
5109/*
5110** Try to transfer data for table zTable.  If an error is seen while
5111** moving forward, try to go backwards.  The backwards movement won't
5112** work for WITHOUT ROWID tables.
5113*/
5114static void tryToCloneData(
5115  ShellState *p,
5116  sqlite3 *newDb,
5117  const char *zTable
5118){
5119  sqlite3_stmt *pQuery = 0;
5120  sqlite3_stmt *pInsert = 0;
5121  char *zQuery = 0;
5122  char *zInsert = 0;
5123  int rc;
5124  int i, j, n;
5125  int nTable = strlen30(zTable);
5126  int k = 0;
5127  int cnt = 0;
5128  const int spinRate = 10000;
5129
5130  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5131  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5132  if( rc ){
5133    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5134            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5135            zQuery);
5136    goto end_data_xfer;
5137  }
5138  n = sqlite3_column_count(pQuery);
5139  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5140  if( zInsert==0 ) shell_out_of_memory();
5141  sqlite3_snprintf(200+nTable,zInsert,
5142                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5143  i = strlen30(zInsert);
5144  for(j=1; j<n; j++){
5145    memcpy(zInsert+i, ",?", 2);
5146    i += 2;
5147  }
5148  memcpy(zInsert+i, ");", 3);
5149  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5150  if( rc ){
5151    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5152            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5153            zQuery);
5154    goto end_data_xfer;
5155  }
5156  for(k=0; k<2; k++){
5157    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5158      for(i=0; i<n; i++){
5159        switch( sqlite3_column_type(pQuery, i) ){
5160          case SQLITE_NULL: {
5161            sqlite3_bind_null(pInsert, i+1);
5162            break;
5163          }
5164          case SQLITE_INTEGER: {
5165            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5166            break;
5167          }
5168          case SQLITE_FLOAT: {
5169            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5170            break;
5171          }
5172          case SQLITE_TEXT: {
5173            sqlite3_bind_text(pInsert, i+1,
5174                             (const char*)sqlite3_column_text(pQuery,i),
5175                             -1, SQLITE_STATIC);
5176            break;
5177          }
5178          case SQLITE_BLOB: {
5179            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5180                                            sqlite3_column_bytes(pQuery,i),
5181                                            SQLITE_STATIC);
5182            break;
5183          }
5184        }
5185      } /* End for */
5186      rc = sqlite3_step(pInsert);
5187      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5188        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5189                        sqlite3_errmsg(newDb));
5190      }
5191      sqlite3_reset(pInsert);
5192      cnt++;
5193      if( (cnt%spinRate)==0 ){
5194        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5195        fflush(stdout);
5196      }
5197    } /* End while */
5198    if( rc==SQLITE_DONE ) break;
5199    sqlite3_finalize(pQuery);
5200    sqlite3_free(zQuery);
5201    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5202                             zTable);
5203    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5204    if( rc ){
5205      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5206      break;
5207    }
5208  } /* End for(k=0...) */
5209
5210end_data_xfer:
5211  sqlite3_finalize(pQuery);
5212  sqlite3_finalize(pInsert);
5213  sqlite3_free(zQuery);
5214  sqlite3_free(zInsert);
5215}
5216
5217
5218/*
5219** Try to transfer all rows of the schema that match zWhere.  For
5220** each row, invoke xForEach() on the object defined by that row.
5221** If an error is encountered while moving forward through the
5222** sqlite_schema table, try again moving backwards.
5223*/
5224static void tryToCloneSchema(
5225  ShellState *p,
5226  sqlite3 *newDb,
5227  const char *zWhere,
5228  void (*xForEach)(ShellState*,sqlite3*,const char*)
5229){
5230  sqlite3_stmt *pQuery = 0;
5231  char *zQuery = 0;
5232  int rc;
5233  const unsigned char *zName;
5234  const unsigned char *zSql;
5235  char *zErrMsg = 0;
5236
5237  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5238                           " WHERE %s", zWhere);
5239  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5240  if( rc ){
5241    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5242                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5243                    zQuery);
5244    goto end_schema_xfer;
5245  }
5246  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5247    zName = sqlite3_column_text(pQuery, 0);
5248    zSql = sqlite3_column_text(pQuery, 1);
5249    printf("%s... ", zName); fflush(stdout);
5250    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5251    if( zErrMsg ){
5252      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5253      sqlite3_free(zErrMsg);
5254      zErrMsg = 0;
5255    }
5256    if( xForEach ){
5257      xForEach(p, newDb, (const char*)zName);
5258    }
5259    printf("done\n");
5260  }
5261  if( rc!=SQLITE_DONE ){
5262    sqlite3_finalize(pQuery);
5263    sqlite3_free(zQuery);
5264    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5265                             " WHERE %s ORDER BY rowid DESC", zWhere);
5266    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5267    if( rc ){
5268      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5269                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5270                      zQuery);
5271      goto end_schema_xfer;
5272    }
5273    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5274      zName = sqlite3_column_text(pQuery, 0);
5275      zSql = sqlite3_column_text(pQuery, 1);
5276      printf("%s... ", zName); fflush(stdout);
5277      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5278      if( zErrMsg ){
5279        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5280        sqlite3_free(zErrMsg);
5281        zErrMsg = 0;
5282      }
5283      if( xForEach ){
5284        xForEach(p, newDb, (const char*)zName);
5285      }
5286      printf("done\n");
5287    }
5288  }
5289end_schema_xfer:
5290  sqlite3_finalize(pQuery);
5291  sqlite3_free(zQuery);
5292}
5293
5294/*
5295** Open a new database file named "zNewDb".  Try to recover as much information
5296** as possible out of the main database (which might be corrupt) and write it
5297** into zNewDb.
5298*/
5299static void tryToClone(ShellState *p, const char *zNewDb){
5300  int rc;
5301  sqlite3 *newDb = 0;
5302  if( access(zNewDb,0)==0 ){
5303    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5304    return;
5305  }
5306  rc = sqlite3_open(zNewDb, &newDb);
5307  if( rc ){
5308    utf8_printf(stderr, "Cannot create output database: %s\n",
5309            sqlite3_errmsg(newDb));
5310  }else{
5311    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5312    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5313    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5314    tryToCloneSchema(p, newDb, "type!='table'", 0);
5315    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5316    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5317  }
5318  close_db(newDb);
5319}
5320
5321/*
5322** Change the output file back to stdout.
5323**
5324** If the p->doXdgOpen flag is set, that means the output was being
5325** redirected to a temporary file named by p->zTempFile.  In that case,
5326** launch start/open/xdg-open on that temporary file.
5327*/
5328static void output_reset(ShellState *p){
5329  if( p->outfile[0]=='|' ){
5330#ifndef SQLITE_OMIT_POPEN
5331    pclose(p->out);
5332#endif
5333  }else{
5334    output_file_close(p->out);
5335#ifndef SQLITE_NOHAVE_SYSTEM
5336    if( p->doXdgOpen ){
5337      const char *zXdgOpenCmd =
5338#if defined(_WIN32)
5339      "start";
5340#elif defined(__APPLE__)
5341      "open";
5342#else
5343      "xdg-open";
5344#endif
5345      char *zCmd;
5346      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5347      if( system(zCmd) ){
5348        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5349      }else{
5350        /* Give the start/open/xdg-open command some time to get
5351        ** going before we continue, and potential delete the
5352        ** p->zTempFile data file out from under it */
5353        sqlite3_sleep(2000);
5354      }
5355      sqlite3_free(zCmd);
5356      outputModePop(p);
5357      p->doXdgOpen = 0;
5358    }
5359#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5360  }
5361  p->outfile[0] = 0;
5362  p->out = stdout;
5363}
5364
5365/*
5366** Run an SQL command and return the single integer result.
5367*/
5368static int db_int(ShellState *p, const char *zSql){
5369  sqlite3_stmt *pStmt;
5370  int res = 0;
5371  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5372  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5373    res = sqlite3_column_int(pStmt,0);
5374  }
5375  sqlite3_finalize(pStmt);
5376  return res;
5377}
5378
5379/*
5380** Convert a 2-byte or 4-byte big-endian integer into a native integer
5381*/
5382static unsigned int get2byteInt(unsigned char *a){
5383  return (a[0]<<8) + a[1];
5384}
5385static unsigned int get4byteInt(unsigned char *a){
5386  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5387}
5388
5389/*
5390** Implementation of the ".dbinfo" command.
5391**
5392** Return 1 on error, 2 to exit, and 0 otherwise.
5393*/
5394static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5395  static const struct { const char *zName; int ofst; } aField[] = {
5396     { "file change counter:",  24  },
5397     { "database page count:",  28  },
5398     { "freelist page count:",  36  },
5399     { "schema cookie:",        40  },
5400     { "schema format:",        44  },
5401     { "default cache size:",   48  },
5402     { "autovacuum top root:",  52  },
5403     { "incremental vacuum:",   64  },
5404     { "text encoding:",        56  },
5405     { "user version:",         60  },
5406     { "application id:",       68  },
5407     { "software version:",     96  },
5408  };
5409  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5410     { "number of tables:",
5411       "SELECT count(*) FROM %s WHERE type='table'" },
5412     { "number of indexes:",
5413       "SELECT count(*) FROM %s WHERE type='index'" },
5414     { "number of triggers:",
5415       "SELECT count(*) FROM %s WHERE type='trigger'" },
5416     { "number of views:",
5417       "SELECT count(*) FROM %s WHERE type='view'" },
5418     { "schema size:",
5419       "SELECT total(length(sql)) FROM %s" },
5420  };
5421  int i, rc;
5422  unsigned iDataVersion;
5423  char *zSchemaTab;
5424  char *zDb = nArg>=2 ? azArg[1] : "main";
5425  sqlite3_stmt *pStmt = 0;
5426  unsigned char aHdr[100];
5427  open_db(p, 0);
5428  if( p->db==0 ) return 1;
5429  rc = sqlite3_prepare_v2(p->db,
5430             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5431             -1, &pStmt, 0);
5432  if( rc ){
5433    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5434    sqlite3_finalize(pStmt);
5435    return 1;
5436  }
5437  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5438  if( sqlite3_step(pStmt)==SQLITE_ROW
5439   && sqlite3_column_bytes(pStmt,0)>100
5440  ){
5441    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5442    sqlite3_finalize(pStmt);
5443  }else{
5444    raw_printf(stderr, "unable to read database header\n");
5445    sqlite3_finalize(pStmt);
5446    return 1;
5447  }
5448  i = get2byteInt(aHdr+16);
5449  if( i==1 ) i = 65536;
5450  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5451  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5452  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5453  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5454  for(i=0; i<ArraySize(aField); i++){
5455    int ofst = aField[i].ofst;
5456    unsigned int val = get4byteInt(aHdr + ofst);
5457    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5458    switch( ofst ){
5459      case 56: {
5460        if( val==1 ) raw_printf(p->out, " (utf8)");
5461        if( val==2 ) raw_printf(p->out, " (utf16le)");
5462        if( val==3 ) raw_printf(p->out, " (utf16be)");
5463      }
5464    }
5465    raw_printf(p->out, "\n");
5466  }
5467  if( zDb==0 ){
5468    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5469  }else if( strcmp(zDb,"temp")==0 ){
5470    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5471  }else{
5472    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5473  }
5474  for(i=0; i<ArraySize(aQuery); i++){
5475    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5476    int val = db_int(p, zSql);
5477    sqlite3_free(zSql);
5478    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5479  }
5480  sqlite3_free(zSchemaTab);
5481  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5482  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5483  return 0;
5484}
5485
5486/*
5487** Print the current sqlite3_errmsg() value to stderr and return 1.
5488*/
5489static int shellDatabaseError(sqlite3 *db){
5490  const char *zErr = sqlite3_errmsg(db);
5491  utf8_printf(stderr, "Error: %s\n", zErr);
5492  return 1;
5493}
5494
5495/*
5496** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5497** if they match and FALSE (0) if they do not match.
5498**
5499** Globbing rules:
5500**
5501**      '*'       Matches any sequence of zero or more characters.
5502**
5503**      '?'       Matches exactly one character.
5504**
5505**     [...]      Matches one character from the enclosed list of
5506**                characters.
5507**
5508**     [^...]     Matches one character not in the enclosed list.
5509**
5510**      '#'       Matches any sequence of one or more digits with an
5511**                optional + or - sign in front
5512**
5513**      ' '       Any span of whitespace matches any other span of
5514**                whitespace.
5515**
5516** Extra whitespace at the end of z[] is ignored.
5517*/
5518static int testcase_glob(const char *zGlob, const char *z){
5519  int c, c2;
5520  int invert;
5521  int seen;
5522
5523  while( (c = (*(zGlob++)))!=0 ){
5524    if( IsSpace(c) ){
5525      if( !IsSpace(*z) ) return 0;
5526      while( IsSpace(*zGlob) ) zGlob++;
5527      while( IsSpace(*z) ) z++;
5528    }else if( c=='*' ){
5529      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5530        if( c=='?' && (*(z++))==0 ) return 0;
5531      }
5532      if( c==0 ){
5533        return 1;
5534      }else if( c=='[' ){
5535        while( *z && testcase_glob(zGlob-1,z)==0 ){
5536          z++;
5537        }
5538        return (*z)!=0;
5539      }
5540      while( (c2 = (*(z++)))!=0 ){
5541        while( c2!=c ){
5542          c2 = *(z++);
5543          if( c2==0 ) return 0;
5544        }
5545        if( testcase_glob(zGlob,z) ) return 1;
5546      }
5547      return 0;
5548    }else if( c=='?' ){
5549      if( (*(z++))==0 ) return 0;
5550    }else if( c=='[' ){
5551      int prior_c = 0;
5552      seen = 0;
5553      invert = 0;
5554      c = *(z++);
5555      if( c==0 ) return 0;
5556      c2 = *(zGlob++);
5557      if( c2=='^' ){
5558        invert = 1;
5559        c2 = *(zGlob++);
5560      }
5561      if( c2==']' ){
5562        if( c==']' ) seen = 1;
5563        c2 = *(zGlob++);
5564      }
5565      while( c2 && c2!=']' ){
5566        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5567          c2 = *(zGlob++);
5568          if( c>=prior_c && c<=c2 ) seen = 1;
5569          prior_c = 0;
5570        }else{
5571          if( c==c2 ){
5572            seen = 1;
5573          }
5574          prior_c = c2;
5575        }
5576        c2 = *(zGlob++);
5577      }
5578      if( c2==0 || (seen ^ invert)==0 ) return 0;
5579    }else if( c=='#' ){
5580      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5581      if( !IsDigit(z[0]) ) return 0;
5582      z++;
5583      while( IsDigit(z[0]) ){ z++; }
5584    }else{
5585      if( c!=(*(z++)) ) return 0;
5586    }
5587  }
5588  while( IsSpace(*z) ){ z++; }
5589  return *z==0;
5590}
5591
5592
5593/*
5594** Compare the string as a command-line option with either one or two
5595** initial "-" characters.
5596*/
5597static int optionMatch(const char *zStr, const char *zOpt){
5598  if( zStr[0]!='-' ) return 0;
5599  zStr++;
5600  if( zStr[0]=='-' ) zStr++;
5601  return strcmp(zStr, zOpt)==0;
5602}
5603
5604/*
5605** Delete a file.
5606*/
5607int shellDeleteFile(const char *zFilename){
5608  int rc;
5609#ifdef _WIN32
5610  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5611  rc = _wunlink(z);
5612  sqlite3_free(z);
5613#else
5614  rc = unlink(zFilename);
5615#endif
5616  return rc;
5617}
5618
5619/*
5620** Try to delete the temporary file (if there is one) and free the
5621** memory used to hold the name of the temp file.
5622*/
5623static void clearTempFile(ShellState *p){
5624  if( p->zTempFile==0 ) return;
5625  if( p->doXdgOpen ) return;
5626  if( shellDeleteFile(p->zTempFile) ) return;
5627  sqlite3_free(p->zTempFile);
5628  p->zTempFile = 0;
5629}
5630
5631/*
5632** Create a new temp file name with the given suffix.
5633*/
5634static void newTempFile(ShellState *p, const char *zSuffix){
5635  clearTempFile(p);
5636  sqlite3_free(p->zTempFile);
5637  p->zTempFile = 0;
5638  if( p->db ){
5639    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5640  }
5641  if( p->zTempFile==0 ){
5642    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5643    ** will not work and we will need to fallback to guessing */
5644    char *zTemp;
5645    sqlite3_uint64 r;
5646    sqlite3_randomness(sizeof(r), &r);
5647    zTemp = getenv("TEMP");
5648    if( zTemp==0 ) zTemp = getenv("TMP");
5649    if( zTemp==0 ){
5650#ifdef _WIN32
5651      zTemp = "\\tmp";
5652#else
5653      zTemp = "/tmp";
5654#endif
5655    }
5656    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5657  }else{
5658    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5659  }
5660  if( p->zTempFile==0 ){
5661    raw_printf(stderr, "out of memory\n");
5662    exit(1);
5663  }
5664}
5665
5666
5667/*
5668** The implementation of SQL scalar function fkey_collate_clause(), used
5669** by the ".lint fkey-indexes" command. This scalar function is always
5670** called with four arguments - the parent table name, the parent column name,
5671** the child table name and the child column name.
5672**
5673**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5674**
5675** If either of the named tables or columns do not exist, this function
5676** returns an empty string. An empty string is also returned if both tables
5677** and columns exist but have the same default collation sequence. Or,
5678** if both exist but the default collation sequences are different, this
5679** function returns the string " COLLATE <parent-collation>", where
5680** <parent-collation> is the default collation sequence of the parent column.
5681*/
5682static void shellFkeyCollateClause(
5683  sqlite3_context *pCtx,
5684  int nVal,
5685  sqlite3_value **apVal
5686){
5687  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5688  const char *zParent;
5689  const char *zParentCol;
5690  const char *zParentSeq;
5691  const char *zChild;
5692  const char *zChildCol;
5693  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5694  int rc;
5695
5696  assert( nVal==4 );
5697  zParent = (const char*)sqlite3_value_text(apVal[0]);
5698  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5699  zChild = (const char*)sqlite3_value_text(apVal[2]);
5700  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5701
5702  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5703  rc = sqlite3_table_column_metadata(
5704      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5705  );
5706  if( rc==SQLITE_OK ){
5707    rc = sqlite3_table_column_metadata(
5708        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5709    );
5710  }
5711
5712  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5713    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5714    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5715    sqlite3_free(z);
5716  }
5717}
5718
5719
5720/*
5721** The implementation of dot-command ".lint fkey-indexes".
5722*/
5723static int lintFkeyIndexes(
5724  ShellState *pState,             /* Current shell tool state */
5725  char **azArg,                   /* Array of arguments passed to dot command */
5726  int nArg                        /* Number of entries in azArg[] */
5727){
5728  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5729  FILE *out = pState->out;        /* Stream to write non-error output to */
5730  int bVerbose = 0;               /* If -verbose is present */
5731  int bGroupByParent = 0;         /* If -groupbyparent is present */
5732  int i;                          /* To iterate through azArg[] */
5733  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5734  int rc;                         /* Return code */
5735  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5736
5737  /*
5738  ** This SELECT statement returns one row for each foreign key constraint
5739  ** in the schema of the main database. The column values are:
5740  **
5741  ** 0. The text of an SQL statement similar to:
5742  **
5743  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5744  **
5745  **    This SELECT is similar to the one that the foreign keys implementation
5746  **    needs to run internally on child tables. If there is an index that can
5747  **    be used to optimize this query, then it can also be used by the FK
5748  **    implementation to optimize DELETE or UPDATE statements on the parent
5749  **    table.
5750  **
5751  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5752  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5753  **    contains an index that can be used to optimize the query.
5754  **
5755  ** 2. Human readable text that describes the child table and columns. e.g.
5756  **
5757  **       "child_table(child_key1, child_key2)"
5758  **
5759  ** 3. Human readable text that describes the parent table and columns. e.g.
5760  **
5761  **       "parent_table(parent_key1, parent_key2)"
5762  **
5763  ** 4. A full CREATE INDEX statement for an index that could be used to
5764  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5765  **
5766  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5767  **
5768  ** 5. The name of the parent table.
5769  **
5770  ** These six values are used by the C logic below to generate the report.
5771  */
5772  const char *zSql =
5773  "SELECT "
5774    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5775    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5776    "  || fkey_collate_clause("
5777    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5778    ", "
5779    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5780    "  || group_concat('*=?', ' AND ') || ')'"
5781    ", "
5782    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5783    ", "
5784    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5785    ", "
5786    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5787    "  || ' ON ' || quote(s.name) || '('"
5788    "  || group_concat(quote(f.[from]) ||"
5789    "        fkey_collate_clause("
5790    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5791    "  || ');'"
5792    ", "
5793    "     f.[table] "
5794    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5795    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5796    "GROUP BY s.name, f.id "
5797    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5798  ;
5799  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5800
5801  for(i=2; i<nArg; i++){
5802    int n = strlen30(azArg[i]);
5803    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5804      bVerbose = 1;
5805    }
5806    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5807      bGroupByParent = 1;
5808      zIndent = "    ";
5809    }
5810    else{
5811      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5812          azArg[0], azArg[1]
5813      );
5814      return SQLITE_ERROR;
5815    }
5816  }
5817
5818  /* Register the fkey_collate_clause() SQL function */
5819  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5820      0, shellFkeyCollateClause, 0, 0
5821  );
5822
5823
5824  if( rc==SQLITE_OK ){
5825    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5826  }
5827  if( rc==SQLITE_OK ){
5828    sqlite3_bind_int(pSql, 1, bGroupByParent);
5829  }
5830
5831  if( rc==SQLITE_OK ){
5832    int rc2;
5833    char *zPrev = 0;
5834    while( SQLITE_ROW==sqlite3_step(pSql) ){
5835      int res = -1;
5836      sqlite3_stmt *pExplain = 0;
5837      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5838      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5839      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5840      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5841      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5842      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5843
5844      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5845      if( rc!=SQLITE_OK ) break;
5846      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5847        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5848        res = (
5849              0==sqlite3_strglob(zGlob, zPlan)
5850           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5851        );
5852      }
5853      rc = sqlite3_finalize(pExplain);
5854      if( rc!=SQLITE_OK ) break;
5855
5856      if( res<0 ){
5857        raw_printf(stderr, "Error: internal error");
5858        break;
5859      }else{
5860        if( bGroupByParent
5861        && (bVerbose || res==0)
5862        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5863        ){
5864          raw_printf(out, "-- Parent table %s\n", zParent);
5865          sqlite3_free(zPrev);
5866          zPrev = sqlite3_mprintf("%s", zParent);
5867        }
5868
5869        if( res==0 ){
5870          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5871        }else if( bVerbose ){
5872          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5873              zIndent, zFrom, zTarget
5874          );
5875        }
5876      }
5877    }
5878    sqlite3_free(zPrev);
5879
5880    if( rc!=SQLITE_OK ){
5881      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5882    }
5883
5884    rc2 = sqlite3_finalize(pSql);
5885    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5886      rc = rc2;
5887      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5888    }
5889  }else{
5890    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5891  }
5892
5893  return rc;
5894}
5895
5896/*
5897** Implementation of ".lint" dot command.
5898*/
5899static int lintDotCommand(
5900  ShellState *pState,             /* Current shell tool state */
5901  char **azArg,                   /* Array of arguments passed to dot command */
5902  int nArg                        /* Number of entries in azArg[] */
5903){
5904  int n;
5905  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5906  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5907  return lintFkeyIndexes(pState, azArg, nArg);
5908
5909 usage:
5910  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5911  raw_printf(stderr, "Where sub-commands are:\n");
5912  raw_printf(stderr, "    fkey-indexes\n");
5913  return SQLITE_ERROR;
5914}
5915
5916#if !defined SQLITE_OMIT_VIRTUALTABLE
5917static void shellPrepare(
5918  sqlite3 *db,
5919  int *pRc,
5920  const char *zSql,
5921  sqlite3_stmt **ppStmt
5922){
5923  *ppStmt = 0;
5924  if( *pRc==SQLITE_OK ){
5925    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5926    if( rc!=SQLITE_OK ){
5927      raw_printf(stderr, "sql error: %s (%d)\n",
5928          sqlite3_errmsg(db), sqlite3_errcode(db)
5929      );
5930      *pRc = rc;
5931    }
5932  }
5933}
5934
5935/*
5936** Create a prepared statement using printf-style arguments for the SQL.
5937**
5938** This routine is could be marked "static".  But it is not always used,
5939** depending on compile-time options.  By omitting the "static", we avoid
5940** nuisance compiler warnings about "defined but not used".
5941*/
5942void shellPreparePrintf(
5943  sqlite3 *db,
5944  int *pRc,
5945  sqlite3_stmt **ppStmt,
5946  const char *zFmt,
5947  ...
5948){
5949  *ppStmt = 0;
5950  if( *pRc==SQLITE_OK ){
5951    va_list ap;
5952    char *z;
5953    va_start(ap, zFmt);
5954    z = sqlite3_vmprintf(zFmt, ap);
5955    va_end(ap);
5956    if( z==0 ){
5957      *pRc = SQLITE_NOMEM;
5958    }else{
5959      shellPrepare(db, pRc, z, ppStmt);
5960      sqlite3_free(z);
5961    }
5962  }
5963}
5964
5965/* Finalize the prepared statement created using shellPreparePrintf().
5966**
5967** This routine is could be marked "static".  But it is not always used,
5968** depending on compile-time options.  By omitting the "static", we avoid
5969** nuisance compiler warnings about "defined but not used".
5970*/
5971void shellFinalize(
5972  int *pRc,
5973  sqlite3_stmt *pStmt
5974){
5975  if( pStmt ){
5976    sqlite3 *db = sqlite3_db_handle(pStmt);
5977    int rc = sqlite3_finalize(pStmt);
5978    if( *pRc==SQLITE_OK ){
5979      if( rc!=SQLITE_OK ){
5980        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5981      }
5982      *pRc = rc;
5983    }
5984  }
5985}
5986
5987/* Reset the prepared statement created using shellPreparePrintf().
5988**
5989** This routine is could be marked "static".  But it is not always used,
5990** depending on compile-time options.  By omitting the "static", we avoid
5991** nuisance compiler warnings about "defined but not used".
5992*/
5993void shellReset(
5994  int *pRc,
5995  sqlite3_stmt *pStmt
5996){
5997  int rc = sqlite3_reset(pStmt);
5998  if( *pRc==SQLITE_OK ){
5999    if( rc!=SQLITE_OK ){
6000      sqlite3 *db = sqlite3_db_handle(pStmt);
6001      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6002    }
6003    *pRc = rc;
6004  }
6005}
6006#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6007
6008#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6009/******************************************************************************
6010** The ".archive" or ".ar" command.
6011*/
6012/*
6013** Structure representing a single ".ar" command.
6014*/
6015typedef struct ArCommand ArCommand;
6016struct ArCommand {
6017  u8 eCmd;                        /* An AR_CMD_* value */
6018  u8 bVerbose;                    /* True if --verbose */
6019  u8 bZip;                        /* True if the archive is a ZIP */
6020  u8 bDryRun;                     /* True if --dry-run */
6021  u8 bAppend;                     /* True if --append */
6022  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6023  int nArg;                       /* Number of command arguments */
6024  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6025  const char *zFile;              /* --file argument, or NULL */
6026  const char *zDir;               /* --directory argument, or NULL */
6027  char **azArg;                   /* Array of command arguments */
6028  ShellState *p;                  /* Shell state */
6029  sqlite3 *db;                    /* Database containing the archive */
6030};
6031
6032/*
6033** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6034*/
6035static int arUsage(FILE *f){
6036  showHelp(f,"archive");
6037  return SQLITE_ERROR;
6038}
6039
6040/*
6041** Print an error message for the .ar command to stderr and return
6042** SQLITE_ERROR.
6043*/
6044static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6045  va_list ap;
6046  char *z;
6047  va_start(ap, zFmt);
6048  z = sqlite3_vmprintf(zFmt, ap);
6049  va_end(ap);
6050  utf8_printf(stderr, "Error: %s\n", z);
6051  if( pAr->fromCmdLine ){
6052    utf8_printf(stderr, "Use \"-A\" for more help\n");
6053  }else{
6054    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6055  }
6056  sqlite3_free(z);
6057  return SQLITE_ERROR;
6058}
6059
6060/*
6061** Values for ArCommand.eCmd.
6062*/
6063#define AR_CMD_CREATE       1
6064#define AR_CMD_UPDATE       2
6065#define AR_CMD_INSERT       3
6066#define AR_CMD_EXTRACT      4
6067#define AR_CMD_LIST         5
6068#define AR_CMD_HELP         6
6069
6070/*
6071** Other (non-command) switches.
6072*/
6073#define AR_SWITCH_VERBOSE     7
6074#define AR_SWITCH_FILE        8
6075#define AR_SWITCH_DIRECTORY   9
6076#define AR_SWITCH_APPEND     10
6077#define AR_SWITCH_DRYRUN     11
6078
6079static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6080  switch( eSwitch ){
6081    case AR_CMD_CREATE:
6082    case AR_CMD_EXTRACT:
6083    case AR_CMD_LIST:
6084    case AR_CMD_UPDATE:
6085    case AR_CMD_INSERT:
6086    case AR_CMD_HELP:
6087      if( pAr->eCmd ){
6088        return arErrorMsg(pAr, "multiple command options");
6089      }
6090      pAr->eCmd = eSwitch;
6091      break;
6092
6093    case AR_SWITCH_DRYRUN:
6094      pAr->bDryRun = 1;
6095      break;
6096    case AR_SWITCH_VERBOSE:
6097      pAr->bVerbose = 1;
6098      break;
6099    case AR_SWITCH_APPEND:
6100      pAr->bAppend = 1;
6101      /* Fall thru into --file */
6102    case AR_SWITCH_FILE:
6103      pAr->zFile = zArg;
6104      break;
6105    case AR_SWITCH_DIRECTORY:
6106      pAr->zDir = zArg;
6107      break;
6108  }
6109
6110  return SQLITE_OK;
6111}
6112
6113/*
6114** Parse the command line for an ".ar" command. The results are written into
6115** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6116** successfully, otherwise an error message is written to stderr and
6117** SQLITE_ERROR returned.
6118*/
6119static int arParseCommand(
6120  char **azArg,                   /* Array of arguments passed to dot command */
6121  int nArg,                       /* Number of entries in azArg[] */
6122  ArCommand *pAr                  /* Populate this object */
6123){
6124  struct ArSwitch {
6125    const char *zLong;
6126    char cShort;
6127    u8 eSwitch;
6128    u8 bArg;
6129  } aSwitch[] = {
6130    { "create",    'c', AR_CMD_CREATE,       0 },
6131    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6132    { "insert",    'i', AR_CMD_INSERT,       0 },
6133    { "list",      't', AR_CMD_LIST,         0 },
6134    { "update",    'u', AR_CMD_UPDATE,       0 },
6135    { "help",      'h', AR_CMD_HELP,         0 },
6136    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6137    { "file",      'f', AR_SWITCH_FILE,      1 },
6138    { "append",    'a', AR_SWITCH_APPEND,    1 },
6139    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6140    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6141  };
6142  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6143  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6144
6145  if( nArg<=1 ){
6146    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6147    return arUsage(stderr);
6148  }else{
6149    char *z = azArg[1];
6150    if( z[0]!='-' ){
6151      /* Traditional style [tar] invocation */
6152      int i;
6153      int iArg = 2;
6154      for(i=0; z[i]; i++){
6155        const char *zArg = 0;
6156        struct ArSwitch *pOpt;
6157        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6158          if( z[i]==pOpt->cShort ) break;
6159        }
6160        if( pOpt==pEnd ){
6161          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6162        }
6163        if( pOpt->bArg ){
6164          if( iArg>=nArg ){
6165            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6166          }
6167          zArg = azArg[iArg++];
6168        }
6169        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6170      }
6171      pAr->nArg = nArg-iArg;
6172      if( pAr->nArg>0 ){
6173        pAr->azArg = &azArg[iArg];
6174      }
6175    }else{
6176      /* Non-traditional invocation */
6177      int iArg;
6178      for(iArg=1; iArg<nArg; iArg++){
6179        int n;
6180        z = azArg[iArg];
6181        if( z[0]!='-' ){
6182          /* All remaining command line words are command arguments. */
6183          pAr->azArg = &azArg[iArg];
6184          pAr->nArg = nArg-iArg;
6185          break;
6186        }
6187        n = strlen30(z);
6188
6189        if( z[1]!='-' ){
6190          int i;
6191          /* One or more short options */
6192          for(i=1; i<n; i++){
6193            const char *zArg = 0;
6194            struct ArSwitch *pOpt;
6195            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6196              if( z[i]==pOpt->cShort ) break;
6197            }
6198            if( pOpt==pEnd ){
6199              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6200            }
6201            if( pOpt->bArg ){
6202              if( i<(n-1) ){
6203                zArg = &z[i+1];
6204                i = n;
6205              }else{
6206                if( iArg>=(nArg-1) ){
6207                  return arErrorMsg(pAr, "option requires an argument: %c",
6208                                    z[i]);
6209                }
6210                zArg = azArg[++iArg];
6211              }
6212            }
6213            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6214          }
6215        }else if( z[2]=='\0' ){
6216          /* A -- option, indicating that all remaining command line words
6217          ** are command arguments.  */
6218          pAr->azArg = &azArg[iArg+1];
6219          pAr->nArg = nArg-iArg-1;
6220          break;
6221        }else{
6222          /* A long option */
6223          const char *zArg = 0;             /* Argument for option, if any */
6224          struct ArSwitch *pMatch = 0;      /* Matching option */
6225          struct ArSwitch *pOpt;            /* Iterator */
6226          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6227            const char *zLong = pOpt->zLong;
6228            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6229              if( pMatch ){
6230                return arErrorMsg(pAr, "ambiguous option: %s",z);
6231              }else{
6232                pMatch = pOpt;
6233              }
6234            }
6235          }
6236
6237          if( pMatch==0 ){
6238            return arErrorMsg(pAr, "unrecognized option: %s", z);
6239          }
6240          if( pMatch->bArg ){
6241            if( iArg>=(nArg-1) ){
6242              return arErrorMsg(pAr, "option requires an argument: %s", z);
6243            }
6244            zArg = azArg[++iArg];
6245          }
6246          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6247        }
6248      }
6249    }
6250  }
6251
6252  return SQLITE_OK;
6253}
6254
6255/*
6256** This function assumes that all arguments within the ArCommand.azArg[]
6257** array refer to archive members, as for the --extract or --list commands.
6258** It checks that each of them are present. If any specified file is not
6259** present in the archive, an error is printed to stderr and an error
6260** code returned. Otherwise, if all specified arguments are present in
6261** the archive, SQLITE_OK is returned.
6262**
6263** This function strips any trailing '/' characters from each argument.
6264** This is consistent with the way the [tar] command seems to work on
6265** Linux.
6266*/
6267static int arCheckEntries(ArCommand *pAr){
6268  int rc = SQLITE_OK;
6269  if( pAr->nArg ){
6270    int i, j;
6271    sqlite3_stmt *pTest = 0;
6272
6273    shellPreparePrintf(pAr->db, &rc, &pTest,
6274        "SELECT name FROM %s WHERE name=$name",
6275        pAr->zSrcTable
6276    );
6277    j = sqlite3_bind_parameter_index(pTest, "$name");
6278    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6279      char *z = pAr->azArg[i];
6280      int n = strlen30(z);
6281      int bOk = 0;
6282      while( n>0 && z[n-1]=='/' ) n--;
6283      z[n] = '\0';
6284      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6285      if( SQLITE_ROW==sqlite3_step(pTest) ){
6286        bOk = 1;
6287      }
6288      shellReset(&rc, pTest);
6289      if( rc==SQLITE_OK && bOk==0 ){
6290        utf8_printf(stderr, "not found in archive: %s\n", z);
6291        rc = SQLITE_ERROR;
6292      }
6293    }
6294    shellFinalize(&rc, pTest);
6295  }
6296  return rc;
6297}
6298
6299/*
6300** Format a WHERE clause that can be used against the "sqlar" table to
6301** identify all archive members that match the command arguments held
6302** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6303** The caller is responsible for eventually calling sqlite3_free() on
6304** any non-NULL (*pzWhere) value.
6305*/
6306static void arWhereClause(
6307  int *pRc,
6308  ArCommand *pAr,
6309  char **pzWhere                  /* OUT: New WHERE clause */
6310){
6311  char *zWhere = 0;
6312  if( *pRc==SQLITE_OK ){
6313    if( pAr->nArg==0 ){
6314      zWhere = sqlite3_mprintf("1");
6315    }else{
6316      int i;
6317      const char *zSep = "";
6318      for(i=0; i<pAr->nArg; i++){
6319        const char *z = pAr->azArg[i];
6320        zWhere = sqlite3_mprintf(
6321          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6322          zWhere, zSep, z, strlen30(z)+1, z
6323        );
6324        if( zWhere==0 ){
6325          *pRc = SQLITE_NOMEM;
6326          break;
6327        }
6328        zSep = " OR ";
6329      }
6330    }
6331  }
6332  *pzWhere = zWhere;
6333}
6334
6335/*
6336** Implementation of .ar "lisT" command.
6337*/
6338static int arListCommand(ArCommand *pAr){
6339  const char *zSql = "SELECT %s FROM %s WHERE %s";
6340  const char *azCols[] = {
6341    "name",
6342    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6343  };
6344
6345  char *zWhere = 0;
6346  sqlite3_stmt *pSql = 0;
6347  int rc;
6348
6349  rc = arCheckEntries(pAr);
6350  arWhereClause(&rc, pAr, &zWhere);
6351
6352  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6353                     pAr->zSrcTable, zWhere);
6354  if( pAr->bDryRun ){
6355    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6356  }else{
6357    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6358      if( pAr->bVerbose ){
6359        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6360            sqlite3_column_text(pSql, 0),
6361            sqlite3_column_int(pSql, 1),
6362            sqlite3_column_text(pSql, 2),
6363            sqlite3_column_text(pSql, 3)
6364        );
6365      }else{
6366        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6367      }
6368    }
6369  }
6370  shellFinalize(&rc, pSql);
6371  sqlite3_free(zWhere);
6372  return rc;
6373}
6374
6375
6376/*
6377** Implementation of .ar "eXtract" command.
6378*/
6379static int arExtractCommand(ArCommand *pAr){
6380  const char *zSql1 =
6381    "SELECT "
6382    " ($dir || name),"
6383    " writefile(($dir || name), %s, mode, mtime) "
6384    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6385    " AND name NOT GLOB '*..[/\\]*'";
6386
6387  const char *azExtraArg[] = {
6388    "sqlar_uncompress(data, sz)",
6389    "data"
6390  };
6391
6392  sqlite3_stmt *pSql = 0;
6393  int rc = SQLITE_OK;
6394  char *zDir = 0;
6395  char *zWhere = 0;
6396  int i, j;
6397
6398  /* If arguments are specified, check that they actually exist within
6399  ** the archive before proceeding. And formulate a WHERE clause to
6400  ** match them.  */
6401  rc = arCheckEntries(pAr);
6402  arWhereClause(&rc, pAr, &zWhere);
6403
6404  if( rc==SQLITE_OK ){
6405    if( pAr->zDir ){
6406      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6407    }else{
6408      zDir = sqlite3_mprintf("");
6409    }
6410    if( zDir==0 ) rc = SQLITE_NOMEM;
6411  }
6412
6413  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6414      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6415  );
6416
6417  if( rc==SQLITE_OK ){
6418    j = sqlite3_bind_parameter_index(pSql, "$dir");
6419    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6420
6421    /* Run the SELECT statement twice. The first time, writefile() is called
6422    ** for all archive members that should be extracted. The second time,
6423    ** only for the directories. This is because the timestamps for
6424    ** extracted directories must be reset after they are populated (as
6425    ** populating them changes the timestamp).  */
6426    for(i=0; i<2; i++){
6427      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6428      sqlite3_bind_int(pSql, j, i);
6429      if( pAr->bDryRun ){
6430        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6431      }else{
6432        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6433          if( i==0 && pAr->bVerbose ){
6434            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6435          }
6436        }
6437      }
6438      shellReset(&rc, pSql);
6439    }
6440    shellFinalize(&rc, pSql);
6441  }
6442
6443  sqlite3_free(zDir);
6444  sqlite3_free(zWhere);
6445  return rc;
6446}
6447
6448/*
6449** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6450*/
6451static int arExecSql(ArCommand *pAr, const char *zSql){
6452  int rc;
6453  if( pAr->bDryRun ){
6454    utf8_printf(pAr->p->out, "%s\n", zSql);
6455    rc = SQLITE_OK;
6456  }else{
6457    char *zErr = 0;
6458    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6459    if( zErr ){
6460      utf8_printf(stdout, "ERROR: %s\n", zErr);
6461      sqlite3_free(zErr);
6462    }
6463  }
6464  return rc;
6465}
6466
6467
6468/*
6469** Implementation of .ar "create", "insert", and "update" commands.
6470**
6471**     create    ->     Create a new SQL archive
6472**     insert    ->     Insert or reinsert all files listed
6473**     update    ->     Insert files that have changed or that were not
6474**                      previously in the archive
6475**
6476** Create the "sqlar" table in the database if it does not already exist.
6477** Then add each file in the azFile[] array to the archive. Directories
6478** are added recursively. If argument bVerbose is non-zero, a message is
6479** printed on stdout for each file archived.
6480**
6481** The create command is the same as update, except that it drops
6482** any existing "sqlar" table before beginning.  The "insert" command
6483** always overwrites every file named on the command-line, where as
6484** "update" only overwrites if the size or mtime or mode has changed.
6485*/
6486static int arCreateOrUpdateCommand(
6487  ArCommand *pAr,                 /* Command arguments and options */
6488  int bUpdate,                    /* true for a --create. */
6489  int bOnlyIfChanged              /* Only update if file has changed */
6490){
6491  const char *zCreate =
6492      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6493      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6494      "  mode INT,               -- access permissions\n"
6495      "  mtime INT,              -- last modification time\n"
6496      "  sz INT,                 -- original file size\n"
6497      "  data BLOB               -- compressed content\n"
6498      ")";
6499  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6500  const char *zInsertFmt[2] = {
6501     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6502     "  SELECT\n"
6503     "    %s,\n"
6504     "    mode,\n"
6505     "    mtime,\n"
6506     "    CASE substr(lsmode(mode),1,1)\n"
6507     "      WHEN '-' THEN length(data)\n"
6508     "      WHEN 'd' THEN 0\n"
6509     "      ELSE -1 END,\n"
6510     "    sqlar_compress(data)\n"
6511     "  FROM fsdir(%Q,%Q) AS disk\n"
6512     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6513     ,
6514     "REPLACE INTO %s(name,mode,mtime,data)\n"
6515     "  SELECT\n"
6516     "    %s,\n"
6517     "    mode,\n"
6518     "    mtime,\n"
6519     "    data\n"
6520     "  FROM fsdir(%Q,%Q) AS disk\n"
6521     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6522  };
6523  int i;                          /* For iterating through azFile[] */
6524  int rc;                         /* Return code */
6525  const char *zTab = 0;           /* SQL table into which to insert */
6526  char *zSql;
6527  char zTemp[50];
6528  char *zExists = 0;
6529
6530  arExecSql(pAr, "PRAGMA page_size=512");
6531  rc = arExecSql(pAr, "SAVEPOINT ar;");
6532  if( rc!=SQLITE_OK ) return rc;
6533  zTemp[0] = 0;
6534  if( pAr->bZip ){
6535    /* Initialize the zipfile virtual table, if necessary */
6536    if( pAr->zFile ){
6537      sqlite3_uint64 r;
6538      sqlite3_randomness(sizeof(r),&r);
6539      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6540      zTab = zTemp;
6541      zSql = sqlite3_mprintf(
6542         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6543         zTab, pAr->zFile
6544      );
6545      rc = arExecSql(pAr, zSql);
6546      sqlite3_free(zSql);
6547    }else{
6548      zTab = "zip";
6549    }
6550  }else{
6551    /* Initialize the table for an SQLAR */
6552    zTab = "sqlar";
6553    if( bUpdate==0 ){
6554      rc = arExecSql(pAr, zDrop);
6555      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6556    }
6557    rc = arExecSql(pAr, zCreate);
6558  }
6559  if( bOnlyIfChanged ){
6560    zExists = sqlite3_mprintf(
6561      " AND NOT EXISTS("
6562          "SELECT 1 FROM %s AS mem"
6563          " WHERE mem.name=disk.name"
6564          " AND mem.mtime=disk.mtime"
6565          " AND mem.mode=disk.mode)", zTab);
6566  }else{
6567    zExists = sqlite3_mprintf("");
6568  }
6569  if( zExists==0 ) rc = SQLITE_NOMEM;
6570  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6571    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6572        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6573        pAr->azArg[i], pAr->zDir, zExists);
6574    rc = arExecSql(pAr, zSql2);
6575    sqlite3_free(zSql2);
6576  }
6577end_ar_transaction:
6578  if( rc!=SQLITE_OK ){
6579    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6580  }else{
6581    rc = arExecSql(pAr, "RELEASE ar;");
6582    if( pAr->bZip && pAr->zFile ){
6583      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6584      arExecSql(pAr, zSql);
6585      sqlite3_free(zSql);
6586    }
6587  }
6588  sqlite3_free(zExists);
6589  return rc;
6590}
6591
6592/*
6593** Implementation of ".ar" dot command.
6594*/
6595static int arDotCommand(
6596  ShellState *pState,          /* Current shell tool state */
6597  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6598  char **azArg,                /* Array of arguments passed to dot command */
6599  int nArg                     /* Number of entries in azArg[] */
6600){
6601  ArCommand cmd;
6602  int rc;
6603  memset(&cmd, 0, sizeof(cmd));
6604  cmd.fromCmdLine = fromCmdLine;
6605  rc = arParseCommand(azArg, nArg, &cmd);
6606  if( rc==SQLITE_OK ){
6607    int eDbType = SHELL_OPEN_UNSPEC;
6608    cmd.p = pState;
6609    cmd.db = pState->db;
6610    if( cmd.zFile ){
6611      eDbType = deduceDatabaseType(cmd.zFile, 1);
6612    }else{
6613      eDbType = pState->openMode;
6614    }
6615    if( eDbType==SHELL_OPEN_ZIPFILE ){
6616      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6617        if( cmd.zFile==0 ){
6618          cmd.zSrcTable = sqlite3_mprintf("zip");
6619        }else{
6620          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6621        }
6622      }
6623      cmd.bZip = 1;
6624    }else if( cmd.zFile ){
6625      int flags;
6626      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6627      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6628           || cmd.eCmd==AR_CMD_UPDATE ){
6629        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6630      }else{
6631        flags = SQLITE_OPEN_READONLY;
6632      }
6633      cmd.db = 0;
6634      if( cmd.bDryRun ){
6635        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6636             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6637      }
6638      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6639             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6640      if( rc!=SQLITE_OK ){
6641        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6642            cmd.zFile, sqlite3_errmsg(cmd.db)
6643        );
6644        goto end_ar_command;
6645      }
6646      sqlite3_fileio_init(cmd.db, 0, 0);
6647      sqlite3_sqlar_init(cmd.db, 0, 0);
6648      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6649                              shellPutsFunc, 0, 0);
6650
6651    }
6652    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6653      if( cmd.eCmd!=AR_CMD_CREATE
6654       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6655      ){
6656        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6657        rc = SQLITE_ERROR;
6658        goto end_ar_command;
6659      }
6660      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6661    }
6662
6663    switch( cmd.eCmd ){
6664      case AR_CMD_CREATE:
6665        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6666        break;
6667
6668      case AR_CMD_EXTRACT:
6669        rc = arExtractCommand(&cmd);
6670        break;
6671
6672      case AR_CMD_LIST:
6673        rc = arListCommand(&cmd);
6674        break;
6675
6676      case AR_CMD_HELP:
6677        arUsage(pState->out);
6678        break;
6679
6680      case AR_CMD_INSERT:
6681        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6682        break;
6683
6684      default:
6685        assert( cmd.eCmd==AR_CMD_UPDATE );
6686        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6687        break;
6688    }
6689  }
6690end_ar_command:
6691  if( cmd.db!=pState->db ){
6692    close_db(cmd.db);
6693  }
6694  sqlite3_free(cmd.zSrcTable);
6695
6696  return rc;
6697}
6698/* End of the ".archive" or ".ar" command logic
6699*******************************************************************************/
6700#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6701
6702#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6703/*
6704** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6705** Otherwise, the SQL statement or statements in zSql are executed using
6706** database connection db and the error code written to *pRc before
6707** this function returns.
6708*/
6709static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6710  int rc = *pRc;
6711  if( rc==SQLITE_OK ){
6712    char *zErr = 0;
6713    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6714    if( rc!=SQLITE_OK ){
6715      raw_printf(stderr, "SQL error: %s\n", zErr);
6716    }
6717    *pRc = rc;
6718  }
6719}
6720
6721/*
6722** Like shellExec(), except that zFmt is a printf() style format string.
6723*/
6724static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6725  char *z = 0;
6726  if( *pRc==SQLITE_OK ){
6727    va_list ap;
6728    va_start(ap, zFmt);
6729    z = sqlite3_vmprintf(zFmt, ap);
6730    va_end(ap);
6731    if( z==0 ){
6732      *pRc = SQLITE_NOMEM;
6733    }else{
6734      shellExec(db, pRc, z);
6735    }
6736    sqlite3_free(z);
6737  }
6738}
6739
6740/*
6741** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6742** Otherwise, an attempt is made to allocate, zero and return a pointer
6743** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6744** to SQLITE_NOMEM and NULL returned.
6745*/
6746static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6747  void *pRet = 0;
6748  if( *pRc==SQLITE_OK ){
6749    pRet = sqlite3_malloc64(nByte);
6750    if( pRet==0 ){
6751      *pRc = SQLITE_NOMEM;
6752    }else{
6753      memset(pRet, 0, nByte);
6754    }
6755  }
6756  return pRet;
6757}
6758
6759/*
6760** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6761** Otherwise, zFmt is treated as a printf() style string. The result of
6762** formatting it along with any trailing arguments is written into a
6763** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6764** It is the responsibility of the caller to eventually free this buffer
6765** using a call to sqlite3_free().
6766**
6767** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6768** pointer returned.
6769*/
6770static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6771  char *z = 0;
6772  if( *pRc==SQLITE_OK ){
6773    va_list ap;
6774    va_start(ap, zFmt);
6775    z = sqlite3_vmprintf(zFmt, ap);
6776    va_end(ap);
6777    if( z==0 ){
6778      *pRc = SQLITE_NOMEM;
6779    }
6780  }
6781  return z;
6782}
6783
6784/*
6785** When running the ".recover" command, each output table, and the special
6786** orphaned row table if it is required, is represented by an instance
6787** of the following struct.
6788*/
6789typedef struct RecoverTable RecoverTable;
6790struct RecoverTable {
6791  char *zQuoted;                  /* Quoted version of table name */
6792  int nCol;                       /* Number of columns in table */
6793  char **azlCol;                  /* Array of column lists */
6794  int iPk;                        /* Index of IPK column */
6795};
6796
6797/*
6798** Free a RecoverTable object allocated by recoverFindTable() or
6799** recoverOrphanTable().
6800*/
6801static void recoverFreeTable(RecoverTable *pTab){
6802  if( pTab ){
6803    sqlite3_free(pTab->zQuoted);
6804    if( pTab->azlCol ){
6805      int i;
6806      for(i=0; i<=pTab->nCol; i++){
6807        sqlite3_free(pTab->azlCol[i]);
6808      }
6809      sqlite3_free(pTab->azlCol);
6810    }
6811    sqlite3_free(pTab);
6812  }
6813}
6814
6815/*
6816** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6817** Otherwise, it allocates and returns a RecoverTable object based on the
6818** final four arguments passed to this function. It is the responsibility
6819** of the caller to eventually free the returned object using
6820** recoverFreeTable().
6821*/
6822static RecoverTable *recoverNewTable(
6823  int *pRc,                       /* IN/OUT: Error code */
6824  const char *zName,              /* Name of table */
6825  const char *zSql,               /* CREATE TABLE statement */
6826  int bIntkey,
6827  int nCol
6828){
6829  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6830  int rc = *pRc;
6831  RecoverTable *pTab = 0;
6832
6833  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6834  if( rc==SQLITE_OK ){
6835    int nSqlCol = 0;
6836    int bSqlIntkey = 0;
6837    sqlite3_stmt *pStmt = 0;
6838
6839    rc = sqlite3_open("", &dbtmp);
6840    if( rc==SQLITE_OK ){
6841      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6842                              shellIdQuote, 0, 0);
6843    }
6844    if( rc==SQLITE_OK ){
6845      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6846    }
6847    if( rc==SQLITE_OK ){
6848      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6849      if( rc==SQLITE_ERROR ){
6850        rc = SQLITE_OK;
6851        goto finished;
6852      }
6853    }
6854    shellPreparePrintf(dbtmp, &rc, &pStmt,
6855        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6856    );
6857    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6858      nSqlCol = sqlite3_column_int(pStmt, 0);
6859    }
6860    shellFinalize(&rc, pStmt);
6861
6862    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6863      goto finished;
6864    }
6865
6866    shellPreparePrintf(dbtmp, &rc, &pStmt,
6867      "SELECT ("
6868      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6869      ") FROM sqlite_schema WHERE name = %Q", zName
6870    );
6871    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6872      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6873    }
6874    shellFinalize(&rc, pStmt);
6875
6876    if( bIntkey==bSqlIntkey ){
6877      int i;
6878      const char *zPk = "_rowid_";
6879      sqlite3_stmt *pPkFinder = 0;
6880
6881      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6882      ** set zPk to the name of the PK column, and pTab->iPk to the index
6883      ** of the column, where columns are 0-numbered from left to right.
6884      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6885      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6886      pTab->iPk = -2;
6887      if( bIntkey ){
6888        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6889          "SELECT cid, name FROM pragma_table_info(%Q) "
6890          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6891          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6892          , zName, zName
6893        );
6894        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6895          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6896          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6897        }
6898      }
6899
6900      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6901      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6902      pTab->nCol = nSqlCol;
6903
6904      if( bIntkey ){
6905        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6906      }else{
6907        pTab->azlCol[0] = shellMPrintf(&rc, "");
6908      }
6909      i = 1;
6910      shellPreparePrintf(dbtmp, &rc, &pStmt,
6911          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6912          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6913          "FROM pragma_table_info(%Q)",
6914          bIntkey ? ", " : "", pTab->iPk,
6915          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6916          zName
6917      );
6918      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6919        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6920        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6921        i++;
6922      }
6923      shellFinalize(&rc, pStmt);
6924
6925      shellFinalize(&rc, pPkFinder);
6926    }
6927  }
6928
6929 finished:
6930  sqlite3_close(dbtmp);
6931  *pRc = rc;
6932  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6933    recoverFreeTable(pTab);
6934    pTab = 0;
6935  }
6936  return pTab;
6937}
6938
6939/*
6940** This function is called to search the schema recovered from the
6941** sqlite_schema table of the (possibly) corrupt database as part
6942** of a ".recover" command. Specifically, for a table with root page
6943** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6944** table must be a WITHOUT ROWID table, or if non-zero, not one of
6945** those.
6946**
6947** If a table is found, a (RecoverTable*) object is returned. Or, if
6948** no such table is found, but bIntkey is false and iRoot is the
6949** root page of an index in the recovered schema, then (*pbNoop) is
6950** set to true and NULL returned. Or, if there is no such table or
6951** index, NULL is returned and (*pbNoop) set to 0, indicating that
6952** the caller should write data to the orphans table.
6953*/
6954static RecoverTable *recoverFindTable(
6955  ShellState *pState,             /* Shell state object */
6956  int *pRc,                       /* IN/OUT: Error code */
6957  int iRoot,                      /* Root page of table */
6958  int bIntkey,                    /* True for an intkey table */
6959  int nCol,                       /* Number of columns in table */
6960  int *pbNoop                     /* OUT: True if iRoot is root of index */
6961){
6962  sqlite3_stmt *pStmt = 0;
6963  RecoverTable *pRet = 0;
6964  int bNoop = 0;
6965  const char *zSql = 0;
6966  const char *zName = 0;
6967
6968  /* Search the recovered schema for an object with root page iRoot. */
6969  shellPreparePrintf(pState->db, pRc, &pStmt,
6970      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6971  );
6972  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6973    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6974    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6975      bNoop = 1;
6976      break;
6977    }
6978    if( sqlite3_stricmp(zType, "table")==0 ){
6979      zName = (const char*)sqlite3_column_text(pStmt, 1);
6980      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6981      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6982      break;
6983    }
6984  }
6985
6986  shellFinalize(pRc, pStmt);
6987  *pbNoop = bNoop;
6988  return pRet;
6989}
6990
6991/*
6992** Return a RecoverTable object representing the orphans table.
6993*/
6994static RecoverTable *recoverOrphanTable(
6995  ShellState *pState,             /* Shell state object */
6996  int *pRc,                       /* IN/OUT: Error code */
6997  const char *zLostAndFound,      /* Base name for orphans table */
6998  int nCol                        /* Number of user data columns */
6999){
7000  RecoverTable *pTab = 0;
7001  if( nCol>=0 && *pRc==SQLITE_OK ){
7002    int i;
7003
7004    /* This block determines the name of the orphan table. The prefered
7005    ** name is zLostAndFound. But if that clashes with another name
7006    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7007    ** and so on until a non-clashing name is found.  */
7008    int iTab = 0;
7009    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7010    sqlite3_stmt *pTest = 0;
7011    shellPrepare(pState->db, pRc,
7012        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7013    );
7014    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7015    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7016      shellReset(pRc, pTest);
7017      sqlite3_free(zTab);
7018      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7019      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7020    }
7021    shellFinalize(pRc, pTest);
7022
7023    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7024    if( pTab ){
7025      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7026      pTab->nCol = nCol;
7027      pTab->iPk = -2;
7028      if( nCol>0 ){
7029        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7030        if( pTab->azlCol ){
7031          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7032          for(i=nCol-1; i>=0; i--){
7033            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7034          }
7035        }
7036      }
7037
7038      if( *pRc!=SQLITE_OK ){
7039        recoverFreeTable(pTab);
7040        pTab = 0;
7041      }else{
7042        raw_printf(pState->out,
7043            "CREATE TABLE %s(rootpgno INTEGER, "
7044            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7045        );
7046        for(i=0; i<nCol; i++){
7047          raw_printf(pState->out, ", c%d", i);
7048        }
7049        raw_printf(pState->out, ");\n");
7050      }
7051    }
7052    sqlite3_free(zTab);
7053  }
7054  return pTab;
7055}
7056
7057/*
7058** This function is called to recover data from the database. A script
7059** to construct a new database containing all recovered data is output
7060** on stream pState->out.
7061*/
7062static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7063  int rc = SQLITE_OK;
7064  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7065  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7066  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7067  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7068  const char *zLostAndFound = "lost_and_found";
7069  int i;
7070  int nOrphan = -1;
7071  RecoverTable *pOrphan = 0;
7072
7073  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7074  int bRowids = 1;                /* 0 if --no-rowids */
7075  for(i=1; i<nArg; i++){
7076    char *z = azArg[i];
7077    int n;
7078    if( z[0]=='-' && z[1]=='-' ) z++;
7079    n = strlen30(z);
7080    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7081      bFreelist = 0;
7082    }else
7083    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7084      i++;
7085      zRecoveryDb = azArg[i];
7086    }else
7087    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7088      i++;
7089      zLostAndFound = azArg[i];
7090    }else
7091    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7092      bRowids = 0;
7093    }
7094    else{
7095      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7096      showHelp(pState->out, azArg[0]);
7097      return 1;
7098    }
7099  }
7100
7101  shellExecPrintf(pState->db, &rc,
7102    /* Attach an in-memory database named 'recovery'. Create an indexed
7103    ** cache of the sqlite_dbptr virtual table. */
7104    "PRAGMA writable_schema = on;"
7105    "ATTACH %Q AS recovery;"
7106    "DROP TABLE IF EXISTS recovery.dbptr;"
7107    "DROP TABLE IF EXISTS recovery.freelist;"
7108    "DROP TABLE IF EXISTS recovery.map;"
7109    "DROP TABLE IF EXISTS recovery.schema;"
7110    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7111  );
7112
7113  if( bFreelist ){
7114    shellExec(pState->db, &rc,
7115      "WITH trunk(pgno) AS ("
7116      "  SELECT shell_int32("
7117      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7118      "      WHERE x>0"
7119      "    UNION"
7120      "  SELECT shell_int32("
7121      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7122      "      FROM trunk WHERE x>0"
7123      "),"
7124      "freelist(data, n, freepgno) AS ("
7125      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7126      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7127      "    UNION ALL"
7128      "  SELECT data, n-1, shell_int32(data, 2+n) "
7129      "      FROM freelist WHERE n>=0"
7130      ")"
7131      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7132    );
7133  }
7134
7135  /* If this is an auto-vacuum database, add all pointer-map pages to
7136  ** the freelist table. Do this regardless of whether or not
7137  ** --freelist-corrupt was specified.  */
7138  shellExec(pState->db, &rc,
7139    "WITH ptrmap(pgno) AS ("
7140    "  SELECT 2 WHERE shell_int32("
7141    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7142    "  )"
7143    "    UNION ALL "
7144    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7145    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7146    ")"
7147    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7148  );
7149
7150  shellExec(pState->db, &rc,
7151    "CREATE TABLE recovery.dbptr("
7152    "      pgno, child, PRIMARY KEY(child, pgno)"
7153    ") WITHOUT ROWID;"
7154    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7155    "    SELECT * FROM sqlite_dbptr"
7156    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7157
7158    /* Delete any pointer to page 1. This ensures that page 1 is considered
7159    ** a root page, regardless of how corrupt the db is. */
7160    "DELETE FROM recovery.dbptr WHERE child = 1;"
7161
7162    /* Delete all pointers to any pages that have more than one pointer
7163    ** to them. Such pages will be treated as root pages when recovering
7164    ** data.  */
7165    "DELETE FROM recovery.dbptr WHERE child IN ("
7166    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7167    ");"
7168
7169    /* Create the "map" table that will (eventually) contain instructions
7170    ** for dealing with each page in the db that contains one or more
7171    ** records. */
7172    "CREATE TABLE recovery.map("
7173      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7174    ");"
7175
7176    /* Populate table [map]. If there are circular loops of pages in the
7177    ** database, the following adds all pages in such a loop to the map
7178    ** as individual root pages. This could be handled better.  */
7179    "WITH pages(i, maxlen) AS ("
7180    "  SELECT page_count, ("
7181    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7182    "  ) FROM pragma_page_count WHERE page_count>0"
7183    "    UNION ALL"
7184    "  SELECT i-1, ("
7185    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7186    "  ) FROM pages WHERE i>=2"
7187    ")"
7188    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7189    "  SELECT i, maxlen, NULL, ("
7190    "    WITH p(orig, pgno, parent) AS ("
7191    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7192    "        UNION "
7193    "      SELECT i, p.parent, "
7194    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7195    "    )"
7196    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7197    ") "
7198    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7199    "UPDATE recovery.map AS o SET intkey = ("
7200    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7201    ");"
7202
7203    /* Extract data from page 1 and any linked pages into table
7204    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7205    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7206    "INSERT INTO recovery.schema SELECT "
7207    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7208    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7209    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7210    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7211    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7212    "FROM sqlite_dbdata WHERE pgno IN ("
7213    "  SELECT pgno FROM recovery.map WHERE root=1"
7214    ")"
7215    "GROUP BY pgno, cell;"
7216    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7217  );
7218
7219  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7220  ** CREATE TABLE statements that extracted from the existing schema.  */
7221  if( rc==SQLITE_OK ){
7222    sqlite3_stmt *pStmt = 0;
7223    /* ".recover" might output content in an order which causes immediate
7224    ** foreign key constraints to be violated. So disable foreign-key
7225    ** constraint enforcement to prevent problems when running the output
7226    ** script. */
7227    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7228    raw_printf(pState->out, "BEGIN;\n");
7229    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7230    shellPrepare(pState->db, &rc,
7231        "SELECT sql FROM recovery.schema "
7232        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7233    );
7234    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7235      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7236      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7237          &zCreateTable[12]
7238      );
7239    }
7240    shellFinalize(&rc, pStmt);
7241  }
7242
7243  /* Figure out if an orphan table will be required. And if so, how many
7244  ** user columns it should contain */
7245  shellPrepare(pState->db, &rc,
7246      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7247      , &pLoop
7248  );
7249  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7250    nOrphan = sqlite3_column_int(pLoop, 0);
7251  }
7252  shellFinalize(&rc, pLoop);
7253  pLoop = 0;
7254
7255  shellPrepare(pState->db, &rc,
7256      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7257  );
7258
7259  shellPrepare(pState->db, &rc,
7260      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7261      "(case when (? AND field<0) then NULL else value end)"
7262      "), ', ')"
7263      ", min(field) "
7264      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7265      "GROUP BY cell", &pCells
7266  );
7267
7268  /* Loop through each root page. */
7269  shellPrepare(pState->db, &rc,
7270      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7271      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7272      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7273      ")", &pLoop
7274  );
7275  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7276    int iRoot = sqlite3_column_int(pLoop, 0);
7277    int bIntkey = sqlite3_column_int(pLoop, 1);
7278    int nCol = sqlite3_column_int(pLoop, 2);
7279    int bNoop = 0;
7280    RecoverTable *pTab;
7281
7282    assert( bIntkey==0 || bIntkey==1 );
7283    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7284    if( bNoop || rc ) continue;
7285    if( pTab==0 ){
7286      if( pOrphan==0 ){
7287        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7288      }
7289      pTab = pOrphan;
7290      if( pTab==0 ) break;
7291    }
7292
7293    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7294      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7295    }
7296    sqlite3_bind_int(pPages, 1, iRoot);
7297    if( bRowids==0 && pTab->iPk<0 ){
7298      sqlite3_bind_int(pCells, 1, 1);
7299    }else{
7300      sqlite3_bind_int(pCells, 1, 0);
7301    }
7302    sqlite3_bind_int(pCells, 3, pTab->iPk);
7303
7304    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7305      int iPgno = sqlite3_column_int(pPages, 0);
7306      sqlite3_bind_int(pCells, 2, iPgno);
7307      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7308        int nField = sqlite3_column_int(pCells, 0);
7309        int iMin = sqlite3_column_int(pCells, 2);
7310        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7311
7312        RecoverTable *pTab2 = pTab;
7313        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7314          if( pOrphan==0 ){
7315            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7316          }
7317          pTab2 = pOrphan;
7318          if( pTab2==0 ) break;
7319        }
7320
7321        nField = nField+1;
7322        if( pTab2==pOrphan ){
7323          raw_printf(pState->out,
7324              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7325              pTab2->zQuoted, iRoot, iPgno, nField,
7326              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7327          );
7328        }else{
7329          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7330              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7331          );
7332        }
7333      }
7334      shellReset(&rc, pCells);
7335    }
7336    shellReset(&rc, pPages);
7337    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7338  }
7339  shellFinalize(&rc, pLoop);
7340  shellFinalize(&rc, pPages);
7341  shellFinalize(&rc, pCells);
7342  recoverFreeTable(pOrphan);
7343
7344  /* The rest of the schema */
7345  if( rc==SQLITE_OK ){
7346    sqlite3_stmt *pStmt = 0;
7347    shellPrepare(pState->db, &rc,
7348        "SELECT sql, name FROM recovery.schema "
7349        "WHERE sql NOT LIKE 'create table%'", &pStmt
7350    );
7351    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7352      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7353      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7354        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7355        char *zPrint = shellMPrintf(&rc,
7356          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7357          zName, zName, zSql
7358        );
7359        raw_printf(pState->out, "%s;\n", zPrint);
7360        sqlite3_free(zPrint);
7361      }else{
7362        raw_printf(pState->out, "%s;\n", zSql);
7363      }
7364    }
7365    shellFinalize(&rc, pStmt);
7366  }
7367
7368  if( rc==SQLITE_OK ){
7369    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7370    raw_printf(pState->out, "COMMIT;\n");
7371  }
7372  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7373  return rc;
7374}
7375#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7376
7377
7378/*
7379** If an input line begins with "." then invoke this routine to
7380** process that line.
7381**
7382** Return 1 on error, 2 to exit, and 0 otherwise.
7383*/
7384static int do_meta_command(char *zLine, ShellState *p){
7385  int h = 1;
7386  int nArg = 0;
7387  int n, c;
7388  int rc = 0;
7389  char *azArg[52];
7390
7391#ifndef SQLITE_OMIT_VIRTUALTABLE
7392  if( p->expert.pExpert ){
7393    expertFinish(p, 1, 0);
7394  }
7395#endif
7396
7397  /* Parse the input line into tokens.
7398  */
7399  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7400    while( IsSpace(zLine[h]) ){ h++; }
7401    if( zLine[h]==0 ) break;
7402    if( zLine[h]=='\'' || zLine[h]=='"' ){
7403      int delim = zLine[h++];
7404      azArg[nArg++] = &zLine[h];
7405      while( zLine[h] && zLine[h]!=delim ){
7406        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7407        h++;
7408      }
7409      if( zLine[h]==delim ){
7410        zLine[h++] = 0;
7411      }
7412      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7413    }else{
7414      azArg[nArg++] = &zLine[h];
7415      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7416      if( zLine[h] ) zLine[h++] = 0;
7417      resolve_backslashes(azArg[nArg-1]);
7418    }
7419  }
7420  azArg[nArg] = 0;
7421
7422  /* Process the input line.
7423  */
7424  if( nArg==0 ) return 0; /* no tokens, no error */
7425  n = strlen30(azArg[0]);
7426  c = azArg[0][0];
7427  clearTempFile(p);
7428
7429#ifndef SQLITE_OMIT_AUTHORIZATION
7430  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7431    if( nArg!=2 ){
7432      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7433      rc = 1;
7434      goto meta_command_exit;
7435    }
7436    open_db(p, 0);
7437    if( booleanValue(azArg[1]) ){
7438      sqlite3_set_authorizer(p->db, shellAuth, p);
7439    }else{
7440      sqlite3_set_authorizer(p->db, 0, 0);
7441    }
7442  }else
7443#endif
7444
7445#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7446  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7447    open_db(p, 0);
7448    rc = arDotCommand(p, 0, azArg, nArg);
7449  }else
7450#endif
7451
7452  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7453   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7454  ){
7455    const char *zDestFile = 0;
7456    const char *zDb = 0;
7457    sqlite3 *pDest;
7458    sqlite3_backup *pBackup;
7459    int j;
7460    int bAsync = 0;
7461    const char *zVfs = 0;
7462    for(j=1; j<nArg; j++){
7463      const char *z = azArg[j];
7464      if( z[0]=='-' ){
7465        if( z[1]=='-' ) z++;
7466        if( strcmp(z, "-append")==0 ){
7467          zVfs = "apndvfs";
7468        }else
7469        if( strcmp(z, "-async")==0 ){
7470          bAsync = 1;
7471        }else
7472        {
7473          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7474          return 1;
7475        }
7476      }else if( zDestFile==0 ){
7477        zDestFile = azArg[j];
7478      }else if( zDb==0 ){
7479        zDb = zDestFile;
7480        zDestFile = azArg[j];
7481      }else{
7482        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7483        return 1;
7484      }
7485    }
7486    if( zDestFile==0 ){
7487      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7488      return 1;
7489    }
7490    if( zDb==0 ) zDb = "main";
7491    rc = sqlite3_open_v2(zDestFile, &pDest,
7492                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7493    if( rc!=SQLITE_OK ){
7494      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7495      close_db(pDest);
7496      return 1;
7497    }
7498    if( bAsync ){
7499      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7500                   0, 0, 0);
7501    }
7502    open_db(p, 0);
7503    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7504    if( pBackup==0 ){
7505      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7506      close_db(pDest);
7507      return 1;
7508    }
7509    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7510    sqlite3_backup_finish(pBackup);
7511    if( rc==SQLITE_DONE ){
7512      rc = 0;
7513    }else{
7514      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7515      rc = 1;
7516    }
7517    close_db(pDest);
7518  }else
7519
7520  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7521    if( nArg==2 ){
7522      bail_on_error = booleanValue(azArg[1]);
7523    }else{
7524      raw_printf(stderr, "Usage: .bail on|off\n");
7525      rc = 1;
7526    }
7527  }else
7528
7529  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7530    if( nArg==2 ){
7531      if( booleanValue(azArg[1]) ){
7532        setBinaryMode(p->out, 1);
7533      }else{
7534        setTextMode(p->out, 1);
7535      }
7536    }else{
7537      raw_printf(stderr, "Usage: .binary on|off\n");
7538      rc = 1;
7539    }
7540  }else
7541
7542  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7543    if( nArg==2 ){
7544#if defined(_WIN32) || defined(WIN32)
7545      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7546      rc = !SetCurrentDirectoryW(z);
7547      sqlite3_free(z);
7548#else
7549      rc = chdir(azArg[1]);
7550#endif
7551      if( rc ){
7552        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7553        rc = 1;
7554      }
7555    }else{
7556      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7557      rc = 1;
7558    }
7559  }else
7560
7561  /* The undocumented ".breakpoint" command causes a call to the no-op
7562  ** routine named test_breakpoint().
7563  */
7564  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7565    test_breakpoint();
7566  }else
7567
7568  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7569    if( nArg==2 ){
7570      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7571    }else{
7572      raw_printf(stderr, "Usage: .changes on|off\n");
7573      rc = 1;
7574    }
7575  }else
7576
7577  /* Cancel output redirection, if it is currently set (by .testcase)
7578  ** Then read the content of the testcase-out.txt file and compare against
7579  ** azArg[1].  If there are differences, report an error and exit.
7580  */
7581  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7582    char *zRes = 0;
7583    output_reset(p);
7584    if( nArg!=2 ){
7585      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7586      rc = 2;
7587    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7588      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7589      rc = 2;
7590    }else if( testcase_glob(azArg[1],zRes)==0 ){
7591      utf8_printf(stderr,
7592                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7593                 p->zTestcase, azArg[1], zRes);
7594      rc = 1;
7595    }else{
7596      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7597      p->nCheck++;
7598    }
7599    sqlite3_free(zRes);
7600  }else
7601
7602  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7603    if( nArg==2 ){
7604      tryToClone(p, azArg[1]);
7605    }else{
7606      raw_printf(stderr, "Usage: .clone FILENAME\n");
7607      rc = 1;
7608    }
7609  }else
7610
7611  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7612    char **azName = 0;
7613    int nName = 0;
7614    sqlite3_stmt *pStmt;
7615    int i;
7616    open_db(p, 0);
7617    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7618    if( rc ){
7619      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7620      rc = 1;
7621    }else{
7622      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7623        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7624        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7625        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7626        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7627        azName[nName*2] = strdup(zSchema);
7628        azName[nName*2+1] = strdup(zFile);
7629        nName++;
7630      }
7631    }
7632    sqlite3_finalize(pStmt);
7633    for(i=0; i<nName; i++){
7634      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7635      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7636      const char *z = azName[i*2+1];
7637      utf8_printf(p->out, "%s: %s %s%s\n",
7638         azName[i*2],
7639         z && z[0] ? z : "\"\"",
7640         bRdonly ? "r/o" : "r/w",
7641         eTxn==SQLITE_TXN_NONE ? "" :
7642            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7643      free(azName[i*2]);
7644      free(azName[i*2+1]);
7645    }
7646    sqlite3_free(azName);
7647  }else
7648
7649  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7650    static const struct DbConfigChoices {
7651      const char *zName;
7652      int op;
7653    } aDbConfig[] = {
7654        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7655        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7656        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7657        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7658        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7659        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7660        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7661        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7662        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7663        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7664        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7665        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7666        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7667        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7668        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7669        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7670    };
7671    int ii, v;
7672    open_db(p, 0);
7673    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7674      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7675      if( nArg>=3 ){
7676        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7677      }
7678      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7679      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7680      if( nArg>1 ) break;
7681    }
7682    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7683      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7684      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7685    }
7686  }else
7687
7688  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7689    rc = shell_dbinfo_command(p, nArg, azArg);
7690  }else
7691
7692#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7693  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7694    open_db(p, 0);
7695    rc = recoverDatabaseCmd(p, nArg, azArg);
7696  }else
7697#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7698
7699  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7700    char *zLike = 0;
7701    char *zSql;
7702    int i;
7703    int savedShowHeader = p->showHeader;
7704    int savedShellFlags = p->shellFlgs;
7705    ShellClearFlag(p,
7706       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7707       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7708    for(i=1; i<nArg; i++){
7709      if( azArg[i][0]=='-' ){
7710        const char *z = azArg[i]+1;
7711        if( z[0]=='-' ) z++;
7712        if( strcmp(z,"preserve-rowids")==0 ){
7713#ifdef SQLITE_OMIT_VIRTUALTABLE
7714          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7715                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7716          rc = 1;
7717          sqlite3_free(zLike);
7718          goto meta_command_exit;
7719#else
7720          ShellSetFlag(p, SHFLG_PreserveRowid);
7721#endif
7722        }else
7723        if( strcmp(z,"newlines")==0 ){
7724          ShellSetFlag(p, SHFLG_Newlines);
7725        }else
7726        if( strcmp(z,"data-only")==0 ){
7727          ShellSetFlag(p, SHFLG_DumpDataOnly);
7728        }else
7729        if( strcmp(z,"nosys")==0 ){
7730          ShellSetFlag(p, SHFLG_DumpNoSys);
7731        }else
7732        {
7733          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7734          rc = 1;
7735          sqlite3_free(zLike);
7736          goto meta_command_exit;
7737        }
7738      }else if( zLike ){
7739        zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
7740                zLike, azArg[i]);
7741      }else{
7742        zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
7743      }
7744    }
7745
7746    open_db(p, 0);
7747
7748    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7749      /* When playing back a "dump", the content might appear in an order
7750      ** which causes immediate foreign key constraints to be violated.
7751      ** So disable foreign-key constraint enforcement to prevent problems. */
7752      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7753      raw_printf(p->out, "BEGIN TRANSACTION;\n");
7754    }
7755    p->writableSchema = 0;
7756    p->showHeader = 0;
7757    /* Set writable_schema=ON since doing so forces SQLite to initialize
7758    ** as much of the schema as it can even if the sqlite_schema table is
7759    ** corrupt. */
7760    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7761    p->nErr = 0;
7762    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7763    zSql = sqlite3_mprintf(
7764      "SELECT name, type, sql FROM sqlite_schema "
7765      "WHERE (%s) AND type=='table'"
7766      "  AND sql NOT NULL"
7767      " ORDER BY tbl_name='sqlite_sequence', rowid",
7768      zLike
7769    );
7770    run_schema_dump_query(p,zSql);
7771    sqlite3_free(zSql);
7772    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7773      zSql = sqlite3_mprintf(
7774        "SELECT sql FROM sqlite_schema "
7775        "WHERE (%s) AND sql NOT NULL"
7776        "  AND type IN ('index','trigger','view')",
7777        zLike
7778      );
7779      run_table_dump_query(p, zSql);
7780      sqlite3_free(zSql);
7781    }
7782    sqlite3_free(zLike);
7783    if( p->writableSchema ){
7784      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7785      p->writableSchema = 0;
7786    }
7787    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7788    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7789    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7790      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7791    }
7792    p->showHeader = savedShowHeader;
7793    p->shellFlgs = savedShellFlags;
7794  }else
7795
7796  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7797    if( nArg==2 ){
7798      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7799    }else{
7800      raw_printf(stderr, "Usage: .echo on|off\n");
7801      rc = 1;
7802    }
7803  }else
7804
7805  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7806    if( nArg==2 ){
7807      p->autoEQPtest = 0;
7808      if( p->autoEQPtrace ){
7809        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7810        p->autoEQPtrace = 0;
7811      }
7812      if( strcmp(azArg[1],"full")==0 ){
7813        p->autoEQP = AUTOEQP_full;
7814      }else if( strcmp(azArg[1],"trigger")==0 ){
7815        p->autoEQP = AUTOEQP_trigger;
7816#ifdef SQLITE_DEBUG
7817      }else if( strcmp(azArg[1],"test")==0 ){
7818        p->autoEQP = AUTOEQP_on;
7819        p->autoEQPtest = 1;
7820      }else if( strcmp(azArg[1],"trace")==0 ){
7821        p->autoEQP = AUTOEQP_full;
7822        p->autoEQPtrace = 1;
7823        open_db(p, 0);
7824        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
7825        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7826#endif
7827      }else{
7828        p->autoEQP = (u8)booleanValue(azArg[1]);
7829      }
7830    }else{
7831      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7832      rc = 1;
7833    }
7834  }else
7835
7836  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7837    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7838    rc = 2;
7839  }else
7840
7841  /* The ".explain" command is automatic now.  It is largely pointless.  It
7842  ** retained purely for backwards compatibility */
7843  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7844    int val = 1;
7845    if( nArg>=2 ){
7846      if( strcmp(azArg[1],"auto")==0 ){
7847        val = 99;
7848      }else{
7849        val =  booleanValue(azArg[1]);
7850      }
7851    }
7852    if( val==1 && p->mode!=MODE_Explain ){
7853      p->normalMode = p->mode;
7854      p->mode = MODE_Explain;
7855      p->autoExplain = 0;
7856    }else if( val==0 ){
7857      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7858      p->autoExplain = 0;
7859    }else if( val==99 ){
7860      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7861      p->autoExplain = 1;
7862    }
7863  }else
7864
7865#ifndef SQLITE_OMIT_VIRTUALTABLE
7866  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7867    open_db(p, 0);
7868    expertDotCommand(p, azArg, nArg);
7869  }else
7870#endif
7871
7872  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7873    static const struct {
7874       const char *zCtrlName;   /* Name of a test-control option */
7875       int ctrlCode;            /* Integer code for that option */
7876       const char *zUsage;      /* Usage notes */
7877    } aCtrl[] = {
7878      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7879      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7880   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7881      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7882      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7883   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7884      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7885      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7886      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7887      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
7888    };
7889    int filectrl = -1;
7890    int iCtrl = -1;
7891    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7892    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7893    int n2, i;
7894    const char *zCmd = 0;
7895    const char *zSchema = 0;
7896
7897    open_db(p, 0);
7898    zCmd = nArg>=2 ? azArg[1] : "help";
7899
7900    if( zCmd[0]=='-'
7901     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7902     && nArg>=4
7903    ){
7904      zSchema = azArg[2];
7905      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7906      nArg -= 2;
7907      zCmd = azArg[1];
7908    }
7909
7910    /* The argument can optionally begin with "-" or "--" */
7911    if( zCmd[0]=='-' && zCmd[1] ){
7912      zCmd++;
7913      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7914    }
7915
7916    /* --help lists all file-controls */
7917    if( strcmp(zCmd,"help")==0 ){
7918      utf8_printf(p->out, "Available file-controls:\n");
7919      for(i=0; i<ArraySize(aCtrl); i++){
7920        utf8_printf(p->out, "  .filectrl %s %s\n",
7921                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7922      }
7923      rc = 1;
7924      goto meta_command_exit;
7925    }
7926
7927    /* convert filectrl text option to value. allow any unique prefix
7928    ** of the option name, or a numerical value. */
7929    n2 = strlen30(zCmd);
7930    for(i=0; i<ArraySize(aCtrl); i++){
7931      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7932        if( filectrl<0 ){
7933          filectrl = aCtrl[i].ctrlCode;
7934          iCtrl = i;
7935        }else{
7936          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7937                              "Use \".filectrl --help\" for help\n", zCmd);
7938          rc = 1;
7939          goto meta_command_exit;
7940        }
7941      }
7942    }
7943    if( filectrl<0 ){
7944      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7945                         "Use \".filectrl --help\" for help\n", zCmd);
7946    }else{
7947      switch(filectrl){
7948        case SQLITE_FCNTL_SIZE_LIMIT: {
7949          if( nArg!=2 && nArg!=3 ) break;
7950          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7951          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7952          isOk = 1;
7953          break;
7954        }
7955        case SQLITE_FCNTL_LOCK_TIMEOUT:
7956        case SQLITE_FCNTL_CHUNK_SIZE: {
7957          int x;
7958          if( nArg!=3 ) break;
7959          x = (int)integerValue(azArg[2]);
7960          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7961          isOk = 2;
7962          break;
7963        }
7964        case SQLITE_FCNTL_PERSIST_WAL:
7965        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7966          int x;
7967          if( nArg!=2 && nArg!=3 ) break;
7968          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7969          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7970          iRes = x;
7971          isOk = 1;
7972          break;
7973        }
7974        case SQLITE_FCNTL_HAS_MOVED: {
7975          int x;
7976          if( nArg!=2 ) break;
7977          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7978          iRes = x;
7979          isOk = 1;
7980          break;
7981        }
7982        case SQLITE_FCNTL_TEMPFILENAME: {
7983          char *z = 0;
7984          if( nArg!=2 ) break;
7985          sqlite3_file_control(p->db, zSchema, filectrl, &z);
7986          if( z ){
7987            utf8_printf(p->out, "%s\n", z);
7988            sqlite3_free(z);
7989          }
7990          isOk = 2;
7991          break;
7992        }
7993        case SQLITE_FCNTL_RESERVE_BYTES: {
7994          int x;
7995          if( nArg>=3 ){
7996            x = atoi(azArg[2]);
7997            sqlite3_file_control(p->db, zSchema, filectrl, &x);
7998          }
7999          x = -1;
8000          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8001          utf8_printf(p->out,"%d\n", x);
8002          isOk = 2;
8003          break;
8004        }
8005      }
8006    }
8007    if( isOk==0 && iCtrl>=0 ){
8008      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8009      rc = 1;
8010    }else if( isOk==1 ){
8011      char zBuf[100];
8012      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8013      raw_printf(p->out, "%s\n", zBuf);
8014    }
8015  }else
8016
8017  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8018    ShellState data;
8019    char *zErrMsg = 0;
8020    int doStats = 0;
8021    memcpy(&data, p, sizeof(data));
8022    data.showHeader = 0;
8023    data.cMode = data.mode = MODE_Semi;
8024    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8025      data.cMode = data.mode = MODE_Pretty;
8026      nArg = 1;
8027    }
8028    if( nArg!=1 ){
8029      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8030      rc = 1;
8031      goto meta_command_exit;
8032    }
8033    open_db(p, 0);
8034    rc = sqlite3_exec(p->db,
8035       "SELECT sql FROM"
8036       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8037       "     FROM sqlite_schema UNION ALL"
8038       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8039       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8040       "ORDER BY rowid",
8041       callback, &data, &zErrMsg
8042    );
8043    if( rc==SQLITE_OK ){
8044      sqlite3_stmt *pStmt;
8045      rc = sqlite3_prepare_v2(p->db,
8046               "SELECT rowid FROM sqlite_schema"
8047               " WHERE name GLOB 'sqlite_stat[134]'",
8048               -1, &pStmt, 0);
8049      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8050      sqlite3_finalize(pStmt);
8051    }
8052    if( doStats==0 ){
8053      raw_printf(p->out, "/* No STAT tables available */\n");
8054    }else{
8055      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8056      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
8057                   callback, &data, &zErrMsg);
8058      data.cMode = data.mode = MODE_Insert;
8059      data.zDestTable = "sqlite_stat1";
8060      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
8061      data.zDestTable = "sqlite_stat4";
8062      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
8063      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8064    }
8065  }else
8066
8067  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8068    if( nArg==2 ){
8069      p->showHeader = booleanValue(azArg[1]);
8070      p->shellFlgs |= SHFLG_HeaderSet;
8071    }else{
8072      raw_printf(stderr, "Usage: .headers on|off\n");
8073      rc = 1;
8074    }
8075  }else
8076
8077  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8078    if( nArg>=2 ){
8079      n = showHelp(p->out, azArg[1]);
8080      if( n==0 ){
8081        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8082      }
8083    }else{
8084      showHelp(p->out, 0);
8085    }
8086  }else
8087
8088  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8089    char *zTable = 0;           /* Insert data into this table */
8090    char *zFile = 0;            /* Name of file to extra content from */
8091    sqlite3_stmt *pStmt = NULL; /* A statement */
8092    int nCol;                   /* Number of columns in the table */
8093    int nByte;                  /* Number of bytes in an SQL string */
8094    int i, j;                   /* Loop counters */
8095    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8096    int nSep;                   /* Number of bytes in p->colSeparator[] */
8097    char *zSql;                 /* An SQL statement */
8098    ImportCtx sCtx;             /* Reader context */
8099    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8100    int eVerbose = 0;           /* Larger for more console output */
8101    int nSkip = 0;              /* Initial lines to skip */
8102    int useOutputMode = 1;      /* Use output mode to determine separators */
8103
8104    memset(&sCtx, 0, sizeof(sCtx));
8105    if( p->mode==MODE_Ascii ){
8106      xRead = ascii_read_one_field;
8107    }else{
8108      xRead = csv_read_one_field;
8109    }
8110    for(i=1; i<nArg; i++){
8111      char *z = azArg[i];
8112      if( z[0]=='-' && z[1]=='-' ) z++;
8113      if( z[0]!='-' ){
8114        if( zFile==0 ){
8115          zFile = z;
8116        }else if( zTable==0 ){
8117          zTable = z;
8118        }else{
8119          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8120          showHelp(p->out, "import");
8121          rc = 1;
8122          goto meta_command_exit;
8123        }
8124      }else if( strcmp(z,"-v")==0 ){
8125        eVerbose++;
8126      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8127        nSkip = integerValue(azArg[++i]);
8128      }else if( strcmp(z,"-ascii")==0 ){
8129        sCtx.cColSep = SEP_Unit[0];
8130        sCtx.cRowSep = SEP_Record[0];
8131        xRead = ascii_read_one_field;
8132        useOutputMode = 0;
8133      }else if( strcmp(z,"-csv")==0 ){
8134        sCtx.cColSep = ',';
8135        sCtx.cRowSep = '\n';
8136        xRead = csv_read_one_field;
8137        useOutputMode = 0;
8138      }else{
8139        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8140        showHelp(p->out, "import");
8141        rc = 1;
8142        goto meta_command_exit;
8143      }
8144    }
8145    if( zTable==0 ){
8146      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8147                  zFile==0 ? "FILE" : "TABLE");
8148      showHelp(p->out, "import");
8149      rc = 1;
8150      goto meta_command_exit;
8151    }
8152    seenInterrupt = 0;
8153    open_db(p, 0);
8154    if( useOutputMode ){
8155      /* If neither the --csv or --ascii options are specified, then set
8156      ** the column and row separator characters from the output mode. */
8157      nSep = strlen30(p->colSeparator);
8158      if( nSep==0 ){
8159        raw_printf(stderr,
8160                   "Error: non-null column separator required for import\n");
8161        rc = 1;
8162        goto meta_command_exit;
8163      }
8164      if( nSep>1 ){
8165        raw_printf(stderr,
8166              "Error: multi-character column separators not allowed"
8167              " for import\n");
8168        rc = 1;
8169        goto meta_command_exit;
8170      }
8171      nSep = strlen30(p->rowSeparator);
8172      if( nSep==0 ){
8173        raw_printf(stderr,
8174            "Error: non-null row separator required for import\n");
8175        rc = 1;
8176        goto meta_command_exit;
8177      }
8178      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8179        /* When importing CSV (only), if the row separator is set to the
8180        ** default output row separator, change it to the default input
8181        ** row separator.  This avoids having to maintain different input
8182        ** and output row separators. */
8183        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8184        nSep = strlen30(p->rowSeparator);
8185      }
8186      if( nSep>1 ){
8187        raw_printf(stderr, "Error: multi-character row separators not allowed"
8188                           " for import\n");
8189        rc = 1;
8190        goto meta_command_exit;
8191      }
8192      sCtx.cColSep = p->colSeparator[0];
8193      sCtx.cRowSep = p->rowSeparator[0];
8194    }
8195    sCtx.zFile = zFile;
8196    sCtx.nLine = 1;
8197    if( sCtx.zFile[0]=='|' ){
8198#ifdef SQLITE_OMIT_POPEN
8199      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8200      rc = 1;
8201      goto meta_command_exit;
8202#else
8203      sCtx.in = popen(sCtx.zFile+1, "r");
8204      sCtx.zFile = "<pipe>";
8205      sCtx.xCloser = pclose;
8206#endif
8207    }else{
8208      sCtx.in = fopen(sCtx.zFile, "rb");
8209      sCtx.xCloser = fclose;
8210    }
8211    if( sCtx.in==0 ){
8212      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8213      rc = 1;
8214      goto meta_command_exit;
8215    }
8216    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8217      char zSep[2];
8218      zSep[1] = 0;
8219      zSep[0] = sCtx.cColSep;
8220      utf8_printf(p->out, "Column separator ");
8221      output_c_string(p->out, zSep);
8222      utf8_printf(p->out, ", row separator ");
8223      zSep[0] = sCtx.cRowSep;
8224      output_c_string(p->out, zSep);
8225      utf8_printf(p->out, "\n");
8226    }
8227    while( (nSkip--)>0 ){
8228      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8229    }
8230    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
8231    if( zSql==0 ){
8232      import_cleanup(&sCtx);
8233      shell_out_of_memory();
8234    }
8235    nByte = strlen30(zSql);
8236    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8237    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8238    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8239      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
8240      char cSep = '(';
8241      while( xRead(&sCtx) ){
8242        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8243        cSep = ',';
8244        if( sCtx.cTerm!=sCtx.cColSep ) break;
8245      }
8246      if( cSep=='(' ){
8247        sqlite3_free(zCreate);
8248        import_cleanup(&sCtx);
8249        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8250        rc = 1;
8251        goto meta_command_exit;
8252      }
8253      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8254      if( eVerbose>=1 ){
8255        utf8_printf(p->out, "%s\n", zCreate);
8256      }
8257      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8258      sqlite3_free(zCreate);
8259      if( rc ){
8260        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
8261                sqlite3_errmsg(p->db));
8262        import_cleanup(&sCtx);
8263        rc = 1;
8264        goto meta_command_exit;
8265      }
8266      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8267    }
8268    sqlite3_free(zSql);
8269    if( rc ){
8270      if (pStmt) sqlite3_finalize(pStmt);
8271      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8272      import_cleanup(&sCtx);
8273      rc = 1;
8274      goto meta_command_exit;
8275    }
8276    nCol = sqlite3_column_count(pStmt);
8277    sqlite3_finalize(pStmt);
8278    pStmt = 0;
8279    if( nCol==0 ) return 0; /* no columns, no error */
8280    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8281    if( zSql==0 ){
8282      import_cleanup(&sCtx);
8283      shell_out_of_memory();
8284    }
8285    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8286    j = strlen30(zSql);
8287    for(i=1; i<nCol; i++){
8288      zSql[j++] = ',';
8289      zSql[j++] = '?';
8290    }
8291    zSql[j++] = ')';
8292    zSql[j] = 0;
8293    if( eVerbose>=2 ){
8294      utf8_printf(p->out, "Insert using: %s\n", zSql);
8295    }
8296    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8297    sqlite3_free(zSql);
8298    if( rc ){
8299      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8300      if (pStmt) sqlite3_finalize(pStmt);
8301      import_cleanup(&sCtx);
8302      rc = 1;
8303      goto meta_command_exit;
8304    }
8305    needCommit = sqlite3_get_autocommit(p->db);
8306    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8307    do{
8308      int startLine = sCtx.nLine;
8309      for(i=0; i<nCol; i++){
8310        char *z = xRead(&sCtx);
8311        /*
8312        ** Did we reach end-of-file before finding any columns?
8313        ** If so, stop instead of NULL filling the remaining columns.
8314        */
8315        if( z==0 && i==0 ) break;
8316        /*
8317        ** Did we reach end-of-file OR end-of-line before finding any
8318        ** columns in ASCII mode?  If so, stop instead of NULL filling
8319        ** the remaining columns.
8320        */
8321        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8322        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8323        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8324          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8325                          "filling the rest with NULL\n",
8326                          sCtx.zFile, startLine, nCol, i+1);
8327          i += 2;
8328          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8329        }
8330      }
8331      if( sCtx.cTerm==sCtx.cColSep ){
8332        do{
8333          xRead(&sCtx);
8334          i++;
8335        }while( sCtx.cTerm==sCtx.cColSep );
8336        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8337                        "extras ignored\n",
8338                        sCtx.zFile, startLine, nCol, i);
8339      }
8340      if( i>=nCol ){
8341        sqlite3_step(pStmt);
8342        rc = sqlite3_reset(pStmt);
8343        if( rc!=SQLITE_OK ){
8344          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8345                      startLine, sqlite3_errmsg(p->db));
8346          sCtx.nErr++;
8347        }else{
8348          sCtx.nRow++;
8349        }
8350      }
8351    }while( sCtx.cTerm!=EOF );
8352
8353    import_cleanup(&sCtx);
8354    sqlite3_finalize(pStmt);
8355    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8356    if( eVerbose>0 ){
8357      utf8_printf(p->out,
8358          "Added %d rows with %d errors using %d lines of input\n",
8359          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8360    }
8361  }else
8362
8363#ifndef SQLITE_UNTESTABLE
8364  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8365    char *zSql;
8366    char *zCollist = 0;
8367    sqlite3_stmt *pStmt;
8368    int tnum = 0;
8369    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8370    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8371    int i;
8372    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8373      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8374                          "       .imposter off\n");
8375      /* Also allowed, but not documented:
8376      **
8377      **    .imposter TABLE IMPOSTER
8378      **
8379      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8380      ** imposter is another WITHOUT ROWID table with the columns in
8381      ** storage order. */
8382      rc = 1;
8383      goto meta_command_exit;
8384    }
8385    open_db(p, 0);
8386    if( nArg==2 ){
8387      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8388      goto meta_command_exit;
8389    }
8390    zSql = sqlite3_mprintf(
8391      "SELECT rootpage, 0 FROM sqlite_schema"
8392      " WHERE name='%q' AND type='index'"
8393      "UNION ALL "
8394      "SELECT rootpage, 1 FROM sqlite_schema"
8395      " WHERE name='%q' AND type='table'"
8396      "   AND sql LIKE '%%without%%rowid%%'",
8397      azArg[1], azArg[1]
8398    );
8399    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8400    sqlite3_free(zSql);
8401    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8402      tnum = sqlite3_column_int(pStmt, 0);
8403      isWO = sqlite3_column_int(pStmt, 1);
8404    }
8405    sqlite3_finalize(pStmt);
8406    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8407    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8408    sqlite3_free(zSql);
8409    i = 0;
8410    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8411      char zLabel[20];
8412      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8413      i++;
8414      if( zCol==0 ){
8415        if( sqlite3_column_int(pStmt,1)==-1 ){
8416          zCol = "_ROWID_";
8417        }else{
8418          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8419          zCol = zLabel;
8420        }
8421      }
8422      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8423        lenPK = (int)strlen(zCollist);
8424      }
8425      if( zCollist==0 ){
8426        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8427      }else{
8428        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8429      }
8430    }
8431    sqlite3_finalize(pStmt);
8432    if( i==0 || tnum==0 ){
8433      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8434      rc = 1;
8435      sqlite3_free(zCollist);
8436      goto meta_command_exit;
8437    }
8438    if( lenPK==0 ) lenPK = 100000;
8439    zSql = sqlite3_mprintf(
8440          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8441          azArg[2], zCollist, lenPK, zCollist);
8442    sqlite3_free(zCollist);
8443    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8444    if( rc==SQLITE_OK ){
8445      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8446      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8447      if( rc ){
8448        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8449      }else{
8450        utf8_printf(stdout, "%s;\n", zSql);
8451        raw_printf(stdout,
8452          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8453          azArg[1], isWO ? "table" : "index"
8454        );
8455      }
8456    }else{
8457      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8458      rc = 1;
8459    }
8460    sqlite3_free(zSql);
8461  }else
8462#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8463
8464#ifdef SQLITE_ENABLE_IOTRACE
8465  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8466    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8467    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8468    iotrace = 0;
8469    if( nArg<2 ){
8470      sqlite3IoTrace = 0;
8471    }else if( strcmp(azArg[1], "-")==0 ){
8472      sqlite3IoTrace = iotracePrintf;
8473      iotrace = stdout;
8474    }else{
8475      iotrace = fopen(azArg[1], "w");
8476      if( iotrace==0 ){
8477        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8478        sqlite3IoTrace = 0;
8479        rc = 1;
8480      }else{
8481        sqlite3IoTrace = iotracePrintf;
8482      }
8483    }
8484  }else
8485#endif
8486
8487  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8488    static const struct {
8489       const char *zLimitName;   /* Name of a limit */
8490       int limitCode;            /* Integer code for that limit */
8491    } aLimit[] = {
8492      { "length",                SQLITE_LIMIT_LENGTH                    },
8493      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8494      { "column",                SQLITE_LIMIT_COLUMN                    },
8495      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8496      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8497      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8498      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8499      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8500      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8501      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8502      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8503      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8504    };
8505    int i, n2;
8506    open_db(p, 0);
8507    if( nArg==1 ){
8508      for(i=0; i<ArraySize(aLimit); i++){
8509        printf("%20s %d\n", aLimit[i].zLimitName,
8510               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8511      }
8512    }else if( nArg>3 ){
8513      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8514      rc = 1;
8515      goto meta_command_exit;
8516    }else{
8517      int iLimit = -1;
8518      n2 = strlen30(azArg[1]);
8519      for(i=0; i<ArraySize(aLimit); i++){
8520        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8521          if( iLimit<0 ){
8522            iLimit = i;
8523          }else{
8524            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8525            rc = 1;
8526            goto meta_command_exit;
8527          }
8528        }
8529      }
8530      if( iLimit<0 ){
8531        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8532                        "enter \".limits\" with no arguments for a list.\n",
8533                         azArg[1]);
8534        rc = 1;
8535        goto meta_command_exit;
8536      }
8537      if( nArg==3 ){
8538        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8539                      (int)integerValue(azArg[2]));
8540      }
8541      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8542             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8543    }
8544  }else
8545
8546  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8547    open_db(p, 0);
8548    lintDotCommand(p, azArg, nArg);
8549  }else
8550
8551#ifndef SQLITE_OMIT_LOAD_EXTENSION
8552  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8553    const char *zFile, *zProc;
8554    char *zErrMsg = 0;
8555    if( nArg<2 ){
8556      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8557      rc = 1;
8558      goto meta_command_exit;
8559    }
8560    zFile = azArg[1];
8561    zProc = nArg>=3 ? azArg[2] : 0;
8562    open_db(p, 0);
8563    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8564    if( rc!=SQLITE_OK ){
8565      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8566      sqlite3_free(zErrMsg);
8567      rc = 1;
8568    }
8569  }else
8570#endif
8571
8572  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8573    if( nArg!=2 ){
8574      raw_printf(stderr, "Usage: .log FILENAME\n");
8575      rc = 1;
8576    }else{
8577      const char *zFile = azArg[1];
8578      output_file_close(p->pLog);
8579      p->pLog = output_file_open(zFile, 0);
8580    }
8581  }else
8582
8583  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8584    const char *zMode = nArg>=2 ? azArg[1] : "";
8585    int n2 = strlen30(zMode);
8586    int c2 = zMode[0];
8587    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8588      p->mode = MODE_Line;
8589      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8590    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8591      p->mode = MODE_Column;
8592      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8593        p->showHeader = 1;
8594      }
8595      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8596    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8597      p->mode = MODE_List;
8598      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8599      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8600    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8601      p->mode = MODE_Html;
8602    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8603      p->mode = MODE_Tcl;
8604      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8605      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8606    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8607      p->mode = MODE_Csv;
8608      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8609      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8610    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8611      p->mode = MODE_List;
8612      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8613    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8614      p->mode = MODE_Insert;
8615      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8616    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8617      p->mode = MODE_Quote;
8618      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8619      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8620    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8621      p->mode = MODE_Ascii;
8622      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8623      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8624    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8625      p->mode = MODE_Markdown;
8626    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8627      p->mode = MODE_Table;
8628    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8629      p->mode = MODE_Box;
8630    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8631      p->mode = MODE_Json;
8632    }else if( nArg==1 ){
8633      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8634    }else{
8635      raw_printf(stderr, "Error: mode should be one of: "
8636         "ascii box column csv html insert json line list markdown "
8637         "quote table tabs tcl\n");
8638      rc = 1;
8639    }
8640    p->cMode = p->mode;
8641  }else
8642
8643  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8644    if( nArg==2 ){
8645      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8646                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8647    }else{
8648      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8649      rc = 1;
8650    }
8651  }else
8652
8653#ifdef SQLITE_DEBUG
8654  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8655    int i;
8656    for(i=1; i<nArg; i++){
8657      const char *z = azArg[i];
8658      if( z[0]=='-' && z[1]=='-' ) z++;
8659      if( strcmp(z,"-repeat")==0 ){
8660        if( i==nArg-1 ){
8661          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8662          rc = 1;
8663        }else{
8664          oomRepeat = (int)integerValue(azArg[++i]);
8665        }
8666      }else if( IsDigit(z[0]) ){
8667        oomCounter = (int)integerValue(azArg[i]);
8668      }else{
8669        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8670        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8671        rc = 1;
8672      }
8673    }
8674    if( rc==0 ){
8675      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8676      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8677    }
8678  }else
8679#endif /* SQLITE_DEBUG */
8680
8681  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8682    char *zNewFilename;  /* Name of the database file to open */
8683    int iName = 1;       /* Index in azArg[] of the filename */
8684    int newFlag = 0;     /* True to delete file before opening */
8685    /* Close the existing database */
8686    session_close_all(p);
8687    close_db(p->db);
8688    p->db = 0;
8689    p->zDbFilename = 0;
8690    sqlite3_free(p->zFreeOnClose);
8691    p->zFreeOnClose = 0;
8692    p->openMode = SHELL_OPEN_UNSPEC;
8693    p->openFlags = 0;
8694    p->szMax = 0;
8695    /* Check for command-line arguments */
8696    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
8697      const char *z = azArg[iName];
8698      if( optionMatch(z,"new") ){
8699        newFlag = 1;
8700#ifdef SQLITE_HAVE_ZLIB
8701      }else if( optionMatch(z, "zip") ){
8702        p->openMode = SHELL_OPEN_ZIPFILE;
8703#endif
8704      }else if( optionMatch(z, "append") ){
8705        p->openMode = SHELL_OPEN_APPENDVFS;
8706      }else if( optionMatch(z, "readonly") ){
8707        p->openMode = SHELL_OPEN_READONLY;
8708      }else if( optionMatch(z, "nofollow") ){
8709        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8710#ifdef SQLITE_ENABLE_DESERIALIZE
8711      }else if( optionMatch(z, "deserialize") ){
8712        p->openMode = SHELL_OPEN_DESERIALIZE;
8713      }else if( optionMatch(z, "hexdb") ){
8714        p->openMode = SHELL_OPEN_HEXDB;
8715      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8716        p->szMax = integerValue(azArg[++iName]);
8717#endif /* SQLITE_ENABLE_DESERIALIZE */
8718      }else if( z[0]=='-' ){
8719        utf8_printf(stderr, "unknown option: %s\n", z);
8720        rc = 1;
8721        goto meta_command_exit;
8722      }
8723    }
8724    /* If a filename is specified, try to open it first */
8725    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
8726    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8727      if( newFlag ) shellDeleteFile(zNewFilename);
8728      p->zDbFilename = zNewFilename;
8729      open_db(p, OPEN_DB_KEEPALIVE);
8730      if( p->db==0 ){
8731        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8732        sqlite3_free(zNewFilename);
8733      }else{
8734        p->zFreeOnClose = zNewFilename;
8735      }
8736    }
8737    if( p->db==0 ){
8738      /* As a fall-back open a TEMP database */
8739      p->zDbFilename = 0;
8740      open_db(p, 0);
8741    }
8742  }else
8743
8744  if( (c=='o'
8745        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8746   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8747  ){
8748    const char *zFile = 0;
8749    int bTxtMode = 0;
8750    int i;
8751    int eMode = 0;
8752    int bBOM = 0;
8753    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8754
8755    if( c=='e' ){
8756      eMode = 'x';
8757      bOnce = 2;
8758    }else if( strncmp(azArg[0],"once",n)==0 ){
8759      bOnce = 1;
8760    }
8761    for(i=1; i<nArg; i++){
8762      char *z = azArg[i];
8763      if( z[0]=='-' ){
8764        if( z[1]=='-' ) z++;
8765        if( strcmp(z,"-bom")==0 ){
8766          bBOM = 1;
8767        }else if( c!='e' && strcmp(z,"-x")==0 ){
8768          eMode = 'x';  /* spreadsheet */
8769        }else if( c!='e' && strcmp(z,"-e")==0 ){
8770          eMode = 'e';  /* text editor */
8771        }else{
8772          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8773                      azArg[i]);
8774          showHelp(p->out, azArg[0]);
8775          rc = 1;
8776          goto meta_command_exit;
8777        }
8778      }else if( zFile==0 ){
8779        zFile = z;
8780      }else{
8781        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8782                    azArg[i]);
8783        showHelp(p->out, azArg[0]);
8784        rc = 1;
8785        goto meta_command_exit;
8786      }
8787    }
8788    if( zFile==0 ) zFile = "stdout";
8789    if( bOnce ){
8790      p->outCount = 2;
8791    }else{
8792      p->outCount = 0;
8793    }
8794    output_reset(p);
8795#ifndef SQLITE_NOHAVE_SYSTEM
8796    if( eMode=='e' || eMode=='x' ){
8797      p->doXdgOpen = 1;
8798      outputModePush(p);
8799      if( eMode=='x' ){
8800        /* spreadsheet mode.  Output as CSV. */
8801        newTempFile(p, "csv");
8802        ShellClearFlag(p, SHFLG_Echo);
8803        p->mode = MODE_Csv;
8804        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8805        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8806      }else{
8807        /* text editor mode */
8808        newTempFile(p, "txt");
8809        bTxtMode = 1;
8810      }
8811      zFile = p->zTempFile;
8812    }
8813#endif /* SQLITE_NOHAVE_SYSTEM */
8814    if( zFile[0]=='|' ){
8815#ifdef SQLITE_OMIT_POPEN
8816      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8817      rc = 1;
8818      p->out = stdout;
8819#else
8820      p->out = popen(zFile + 1, "w");
8821      if( p->out==0 ){
8822        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8823        p->out = stdout;
8824        rc = 1;
8825      }else{
8826        if( bBOM ) fprintf(p->out,"\357\273\277");
8827        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8828      }
8829#endif
8830    }else{
8831      p->out = output_file_open(zFile, bTxtMode);
8832      if( p->out==0 ){
8833        if( strcmp(zFile,"off")!=0 ){
8834          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8835        }
8836        p->out = stdout;
8837        rc = 1;
8838      } else {
8839        if( bBOM ) fprintf(p->out,"\357\273\277");
8840        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8841      }
8842    }
8843  }else
8844
8845  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8846    open_db(p,0);
8847    if( nArg<=1 ) goto parameter_syntax_error;
8848
8849    /* .parameter clear
8850    ** Clear all bind parameters by dropping the TEMP table that holds them.
8851    */
8852    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8853      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8854                   0, 0, 0);
8855    }else
8856
8857    /* .parameter list
8858    ** List all bind parameters.
8859    */
8860    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8861      sqlite3_stmt *pStmt = 0;
8862      int rx;
8863      int len = 0;
8864      rx = sqlite3_prepare_v2(p->db,
8865             "SELECT max(length(key)) "
8866             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8867      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8868        len = sqlite3_column_int(pStmt, 0);
8869        if( len>40 ) len = 40;
8870      }
8871      sqlite3_finalize(pStmt);
8872      pStmt = 0;
8873      if( len ){
8874        rx = sqlite3_prepare_v2(p->db,
8875             "SELECT key, quote(value) "
8876             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8877        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8878          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8879                      sqlite3_column_text(pStmt,1));
8880        }
8881        sqlite3_finalize(pStmt);
8882      }
8883    }else
8884
8885    /* .parameter init
8886    ** Make sure the TEMP table used to hold bind parameters exists.
8887    ** Create it if necessary.
8888    */
8889    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8890      bind_table_init(p);
8891    }else
8892
8893    /* .parameter set NAME VALUE
8894    ** Set or reset a bind parameter.  NAME should be the full parameter
8895    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8896    ** VALUE can be in either SQL literal notation, or if not it will be
8897    ** understood to be a text string.
8898    */
8899    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8900      int rx;
8901      char *zSql;
8902      sqlite3_stmt *pStmt;
8903      const char *zKey = azArg[2];
8904      const char *zValue = azArg[3];
8905      bind_table_init(p);
8906      zSql = sqlite3_mprintf(
8907                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8908                  "VALUES(%Q,%s);", zKey, zValue);
8909      if( zSql==0 ) shell_out_of_memory();
8910      pStmt = 0;
8911      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8912      sqlite3_free(zSql);
8913      if( rx!=SQLITE_OK ){
8914        sqlite3_finalize(pStmt);
8915        pStmt = 0;
8916        zSql = sqlite3_mprintf(
8917                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8918                   "VALUES(%Q,%Q);", zKey, zValue);
8919        if( zSql==0 ) shell_out_of_memory();
8920        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8921        sqlite3_free(zSql);
8922        if( rx!=SQLITE_OK ){
8923          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8924          sqlite3_finalize(pStmt);
8925          pStmt = 0;
8926          rc = 1;
8927        }
8928      }
8929      sqlite3_step(pStmt);
8930      sqlite3_finalize(pStmt);
8931    }else
8932
8933    /* .parameter unset NAME
8934    ** Remove the NAME binding from the parameter binding table, if it
8935    ** exists.
8936    */
8937    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8938      char *zSql = sqlite3_mprintf(
8939          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8940      if( zSql==0 ) shell_out_of_memory();
8941      sqlite3_exec(p->db, zSql, 0, 0, 0);
8942      sqlite3_free(zSql);
8943    }else
8944    /* If no command name matches, show a syntax error */
8945    parameter_syntax_error:
8946    showHelp(p->out, "parameter");
8947  }else
8948
8949  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8950    int i;
8951    for(i=1; i<nArg; i++){
8952      if( i>1 ) raw_printf(p->out, " ");
8953      utf8_printf(p->out, "%s", azArg[i]);
8954    }
8955    raw_printf(p->out, "\n");
8956  }else
8957
8958#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8959  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8960    int i;
8961    int nn = 0;
8962    p->flgProgress = 0;
8963    p->mxProgress = 0;
8964    p->nProgress = 0;
8965    for(i=1; i<nArg; i++){
8966      const char *z = azArg[i];
8967      if( z[0]=='-' ){
8968        z++;
8969        if( z[0]=='-' ) z++;
8970        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
8971          p->flgProgress |= SHELL_PROGRESS_QUIET;
8972          continue;
8973        }
8974        if( strcmp(z,"reset")==0 ){
8975          p->flgProgress |= SHELL_PROGRESS_RESET;
8976          continue;
8977        }
8978        if( strcmp(z,"once")==0 ){
8979          p->flgProgress |= SHELL_PROGRESS_ONCE;
8980          continue;
8981        }
8982        if( strcmp(z,"limit")==0 ){
8983          if( i+1>=nArg ){
8984            utf8_printf(stderr, "Error: missing argument on --limit\n");
8985            rc = 1;
8986            goto meta_command_exit;
8987          }else{
8988            p->mxProgress = (int)integerValue(azArg[++i]);
8989          }
8990          continue;
8991        }
8992        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
8993        rc = 1;
8994        goto meta_command_exit;
8995      }else{
8996        nn = (int)integerValue(z);
8997      }
8998    }
8999    open_db(p, 0);
9000    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9001  }else
9002#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9003
9004  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9005    if( nArg >= 2) {
9006      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9007    }
9008    if( nArg >= 3) {
9009      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9010    }
9011  }else
9012
9013  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
9014    rc = 2;
9015  }else
9016
9017  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
9018    FILE *inSaved = p->in;
9019    int savedLineno = p->lineno;
9020    if( nArg!=2 ){
9021      raw_printf(stderr, "Usage: .read FILE\n");
9022      rc = 1;
9023      goto meta_command_exit;
9024    }
9025    if( azArg[1][0]=='|' ){
9026      p->in = popen(azArg[1]+1, "r");
9027      if( p->in==0 ){
9028        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9029        rc = 1;
9030      }else{
9031        rc = process_input(p);
9032        pclose(p->in);
9033      }
9034    }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
9035      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9036      rc = 1;
9037    }else{
9038      rc = process_input(p);
9039      fclose(p->in);
9040    }
9041    p->in = inSaved;
9042    p->lineno = savedLineno;
9043  }else
9044
9045  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9046    const char *zSrcFile;
9047    const char *zDb;
9048    sqlite3 *pSrc;
9049    sqlite3_backup *pBackup;
9050    int nTimeout = 0;
9051
9052    if( nArg==2 ){
9053      zSrcFile = azArg[1];
9054      zDb = "main";
9055    }else if( nArg==3 ){
9056      zSrcFile = azArg[2];
9057      zDb = azArg[1];
9058    }else{
9059      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9060      rc = 1;
9061      goto meta_command_exit;
9062    }
9063    rc = sqlite3_open(zSrcFile, &pSrc);
9064    if( rc!=SQLITE_OK ){
9065      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9066      close_db(pSrc);
9067      return 1;
9068    }
9069    open_db(p, 0);
9070    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9071    if( pBackup==0 ){
9072      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9073      close_db(pSrc);
9074      return 1;
9075    }
9076    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9077          || rc==SQLITE_BUSY  ){
9078      if( rc==SQLITE_BUSY ){
9079        if( nTimeout++ >= 3 ) break;
9080        sqlite3_sleep(100);
9081      }
9082    }
9083    sqlite3_backup_finish(pBackup);
9084    if( rc==SQLITE_DONE ){
9085      rc = 0;
9086    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9087      raw_printf(stderr, "Error: source database is busy\n");
9088      rc = 1;
9089    }else{
9090      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9091      rc = 1;
9092    }
9093    close_db(pSrc);
9094  }else
9095
9096  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9097    if( nArg==2 ){
9098      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9099#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9100      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9101#endif
9102    }else{
9103      raw_printf(stderr, "Usage: .scanstats on|off\n");
9104      rc = 1;
9105    }
9106  }else
9107
9108  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9109    ShellText sSelect;
9110    ShellState data;
9111    char *zErrMsg = 0;
9112    const char *zDiv = "(";
9113    const char *zName = 0;
9114    int iSchema = 0;
9115    int bDebug = 0;
9116    int bNoSystemTabs = 0;
9117    int ii;
9118
9119    open_db(p, 0);
9120    memcpy(&data, p, sizeof(data));
9121    data.showHeader = 0;
9122    data.cMode = data.mode = MODE_Semi;
9123    initText(&sSelect);
9124    for(ii=1; ii<nArg; ii++){
9125      if( optionMatch(azArg[ii],"indent") ){
9126        data.cMode = data.mode = MODE_Pretty;
9127      }else if( optionMatch(azArg[ii],"debug") ){
9128        bDebug = 1;
9129      }else if( optionMatch(azArg[ii],"nosys") ){
9130        bNoSystemTabs = 1;
9131      }else if( azArg[ii][0]=='-' ){
9132        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9133        rc = 1;
9134        goto meta_command_exit;
9135      }else if( zName==0 ){
9136        zName = azArg[ii];
9137      }else{
9138        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9139        rc = 1;
9140        goto meta_command_exit;
9141      }
9142    }
9143    if( zName!=0 ){
9144      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9145                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9146                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9147                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9148      if( isSchema ){
9149        char *new_argv[2], *new_colv[2];
9150        new_argv[0] = sqlite3_mprintf(
9151                      "CREATE TABLE %s (\n"
9152                      "  type text,\n"
9153                      "  name text,\n"
9154                      "  tbl_name text,\n"
9155                      "  rootpage integer,\n"
9156                      "  sql text\n"
9157                      ")", zName);
9158        new_argv[1] = 0;
9159        new_colv[0] = "sql";
9160        new_colv[1] = 0;
9161        callback(&data, 1, new_argv, new_colv);
9162        sqlite3_free(new_argv[0]);
9163      }
9164    }
9165    if( zDiv ){
9166      sqlite3_stmt *pStmt = 0;
9167      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9168                              -1, &pStmt, 0);
9169      if( rc ){
9170        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9171        sqlite3_finalize(pStmt);
9172        rc = 1;
9173        goto meta_command_exit;
9174      }
9175      appendText(&sSelect, "SELECT sql FROM", 0);
9176      iSchema = 0;
9177      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9178        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9179        char zScNum[30];
9180        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9181        appendText(&sSelect, zDiv, 0);
9182        zDiv = " UNION ALL ";
9183        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9184        if( sqlite3_stricmp(zDb, "main")!=0 ){
9185          appendText(&sSelect, zDb, '\'');
9186        }else{
9187          appendText(&sSelect, "NULL", 0);
9188        }
9189        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9190        appendText(&sSelect, zScNum, 0);
9191        appendText(&sSelect, " AS snum, ", 0);
9192        appendText(&sSelect, zDb, '\'');
9193        appendText(&sSelect, " AS sname FROM ", 0);
9194        appendText(&sSelect, zDb, quoteChar(zDb));
9195        appendText(&sSelect, ".sqlite_schema", 0);
9196      }
9197      sqlite3_finalize(pStmt);
9198#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9199      if( zName ){
9200        appendText(&sSelect,
9201           " UNION ALL SELECT shell_module_schema(name),"
9202           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9203        0);
9204      }
9205#endif
9206      appendText(&sSelect, ") WHERE ", 0);
9207      if( zName ){
9208        char *zQarg = sqlite3_mprintf("%Q", zName);
9209        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9210                    strchr(zName, '[') != 0;
9211        if( strchr(zName, '.') ){
9212          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9213        }else{
9214          appendText(&sSelect, "lower(tbl_name)", 0);
9215        }
9216        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9217        appendText(&sSelect, zQarg, 0);
9218        if( !bGlob ){
9219          appendText(&sSelect, " ESCAPE '\\' ", 0);
9220        }
9221        appendText(&sSelect, " AND ", 0);
9222        sqlite3_free(zQarg);
9223      }
9224      if( bNoSystemTabs ){
9225        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9226      }
9227      appendText(&sSelect, "sql IS NOT NULL"
9228                           " ORDER BY snum, rowid", 0);
9229      if( bDebug ){
9230        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9231      }else{
9232        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9233      }
9234      freeText(&sSelect);
9235    }
9236    if( zErrMsg ){
9237      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9238      sqlite3_free(zErrMsg);
9239      rc = 1;
9240    }else if( rc != SQLITE_OK ){
9241      raw_printf(stderr,"Error: querying schema information\n");
9242      rc = 1;
9243    }else{
9244      rc = 0;
9245    }
9246  }else
9247
9248#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9249  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9250    sqlite3_unsupported_selecttrace = nArg>=2 ? (int)integerValue(azArg[1]) : 0xffff;
9251  }else
9252#endif
9253
9254#if defined(SQLITE_ENABLE_SESSION)
9255  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9256    OpenSession *pSession = &p->aSession[0];
9257    char **azCmd = &azArg[1];
9258    int iSes = 0;
9259    int nCmd = nArg - 1;
9260    int i;
9261    if( nArg<=1 ) goto session_syntax_error;
9262    open_db(p, 0);
9263    if( nArg>=3 ){
9264      for(iSes=0; iSes<p->nSession; iSes++){
9265        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
9266      }
9267      if( iSes<p->nSession ){
9268        pSession = &p->aSession[iSes];
9269        azCmd++;
9270        nCmd--;
9271      }else{
9272        pSession = &p->aSession[0];
9273        iSes = 0;
9274      }
9275    }
9276
9277    /* .session attach TABLE
9278    ** Invoke the sqlite3session_attach() interface to attach a particular
9279    ** table so that it is never filtered.
9280    */
9281    if( strcmp(azCmd[0],"attach")==0 ){
9282      if( nCmd!=2 ) goto session_syntax_error;
9283      if( pSession->p==0 ){
9284        session_not_open:
9285        raw_printf(stderr, "ERROR: No sessions are open\n");
9286      }else{
9287        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9288        if( rc ){
9289          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9290          rc = 0;
9291        }
9292      }
9293    }else
9294
9295    /* .session changeset FILE
9296    ** .session patchset FILE
9297    ** Write a changeset or patchset into a file.  The file is overwritten.
9298    */
9299    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9300      FILE *out = 0;
9301      if( nCmd!=2 ) goto session_syntax_error;
9302      if( pSession->p==0 ) goto session_not_open;
9303      out = fopen(azCmd[1], "wb");
9304      if( out==0 ){
9305        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9306                    azCmd[1]);
9307      }else{
9308        int szChng;
9309        void *pChng;
9310        if( azCmd[0][0]=='c' ){
9311          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9312        }else{
9313          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9314        }
9315        if( rc ){
9316          printf("Error: error code %d\n", rc);
9317          rc = 0;
9318        }
9319        if( pChng
9320          && fwrite(pChng, szChng, 1, out)!=1 ){
9321          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9322                  szChng);
9323        }
9324        sqlite3_free(pChng);
9325        fclose(out);
9326      }
9327    }else
9328
9329    /* .session close
9330    ** Close the identified session
9331    */
9332    if( strcmp(azCmd[0], "close")==0 ){
9333      if( nCmd!=1 ) goto session_syntax_error;
9334      if( p->nSession ){
9335        session_close(pSession);
9336        p->aSession[iSes] = p->aSession[--p->nSession];
9337      }
9338    }else
9339
9340    /* .session enable ?BOOLEAN?
9341    ** Query or set the enable flag
9342    */
9343    if( strcmp(azCmd[0], "enable")==0 ){
9344      int ii;
9345      if( nCmd>2 ) goto session_syntax_error;
9346      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9347      if( p->nSession ){
9348        ii = sqlite3session_enable(pSession->p, ii);
9349        utf8_printf(p->out, "session %s enable flag = %d\n",
9350                    pSession->zName, ii);
9351      }
9352    }else
9353
9354    /* .session filter GLOB ....
9355    ** Set a list of GLOB patterns of table names to be excluded.
9356    */
9357    if( strcmp(azCmd[0], "filter")==0 ){
9358      int ii, nByte;
9359      if( nCmd<2 ) goto session_syntax_error;
9360      if( p->nSession ){
9361        for(ii=0; ii<pSession->nFilter; ii++){
9362          sqlite3_free(pSession->azFilter[ii]);
9363        }
9364        sqlite3_free(pSession->azFilter);
9365        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9366        pSession->azFilter = sqlite3_malloc( nByte );
9367        if( pSession->azFilter==0 ){
9368          raw_printf(stderr, "Error: out or memory\n");
9369          exit(1);
9370        }
9371        for(ii=1; ii<nCmd; ii++){
9372          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9373        }
9374        pSession->nFilter = ii-1;
9375      }
9376    }else
9377
9378    /* .session indirect ?BOOLEAN?
9379    ** Query or set the indirect flag
9380    */
9381    if( strcmp(azCmd[0], "indirect")==0 ){
9382      int ii;
9383      if( nCmd>2 ) goto session_syntax_error;
9384      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9385      if( p->nSession ){
9386        ii = sqlite3session_indirect(pSession->p, ii);
9387        utf8_printf(p->out, "session %s indirect flag = %d\n",
9388                    pSession->zName, ii);
9389      }
9390    }else
9391
9392    /* .session isempty
9393    ** Determine if the session is empty
9394    */
9395    if( strcmp(azCmd[0], "isempty")==0 ){
9396      int ii;
9397      if( nCmd!=1 ) goto session_syntax_error;
9398      if( p->nSession ){
9399        ii = sqlite3session_isempty(pSession->p);
9400        utf8_printf(p->out, "session %s isempty flag = %d\n",
9401                    pSession->zName, ii);
9402      }
9403    }else
9404
9405    /* .session list
9406    ** List all currently open sessions
9407    */
9408    if( strcmp(azCmd[0],"list")==0 ){
9409      for(i=0; i<p->nSession; i++){
9410        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
9411      }
9412    }else
9413
9414    /* .session open DB NAME
9415    ** Open a new session called NAME on the attached database DB.
9416    ** DB is normally "main".
9417    */
9418    if( strcmp(azCmd[0],"open")==0 ){
9419      char *zName;
9420      if( nCmd!=3 ) goto session_syntax_error;
9421      zName = azCmd[2];
9422      if( zName[0]==0 ) goto session_syntax_error;
9423      for(i=0; i<p->nSession; i++){
9424        if( strcmp(p->aSession[i].zName,zName)==0 ){
9425          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9426          goto meta_command_exit;
9427        }
9428      }
9429      if( p->nSession>=ArraySize(p->aSession) ){
9430        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9431        goto meta_command_exit;
9432      }
9433      pSession = &p->aSession[p->nSession];
9434      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9435      if( rc ){
9436        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9437        rc = 0;
9438        goto meta_command_exit;
9439      }
9440      pSession->nFilter = 0;
9441      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9442      p->nSession++;
9443      pSession->zName = sqlite3_mprintf("%s", zName);
9444    }else
9445    /* If no command name matches, show a syntax error */
9446    session_syntax_error:
9447    showHelp(p->out, "session");
9448  }else
9449#endif
9450
9451#ifdef SQLITE_DEBUG
9452  /* Undocumented commands for internal testing.  Subject to change
9453  ** without notice. */
9454  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9455    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9456      int i, v;
9457      for(i=1; i<nArg; i++){
9458        v = booleanValue(azArg[i]);
9459        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9460      }
9461    }
9462    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9463      int i; sqlite3_int64 v;
9464      for(i=1; i<nArg; i++){
9465        char zBuf[200];
9466        v = integerValue(azArg[i]);
9467        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9468        utf8_printf(p->out, "%s", zBuf);
9469      }
9470    }
9471  }else
9472#endif
9473
9474  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9475    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9476    int bVerbose = 0;        /* Verbose output */
9477    int bSelftestExists;     /* True if SELFTEST already exists */
9478    int i, k;                /* Loop counters */
9479    int nTest = 0;           /* Number of tests runs */
9480    int nErr = 0;            /* Number of errors seen */
9481    ShellText str;           /* Answer for a query */
9482    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9483
9484    open_db(p,0);
9485    for(i=1; i<nArg; i++){
9486      const char *z = azArg[i];
9487      if( z[0]=='-' && z[1]=='-' ) z++;
9488      if( strcmp(z,"-init")==0 ){
9489        bIsInit = 1;
9490      }else
9491      if( strcmp(z,"-v")==0 ){
9492        bVerbose++;
9493      }else
9494      {
9495        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9496                    azArg[i], azArg[0]);
9497        raw_printf(stderr, "Should be one of: --init -v\n");
9498        rc = 1;
9499        goto meta_command_exit;
9500      }
9501    }
9502    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9503           != SQLITE_OK ){
9504      bSelftestExists = 0;
9505    }else{
9506      bSelftestExists = 1;
9507    }
9508    if( bIsInit ){
9509      createSelftestTable(p);
9510      bSelftestExists = 1;
9511    }
9512    initText(&str);
9513    appendText(&str, "x", 0);
9514    for(k=bSelftestExists; k>=0; k--){
9515      if( k==1 ){
9516        rc = sqlite3_prepare_v2(p->db,
9517            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9518            -1, &pStmt, 0);
9519      }else{
9520        rc = sqlite3_prepare_v2(p->db,
9521          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9522          "      (1,'run','PRAGMA integrity_check','ok')",
9523          -1, &pStmt, 0);
9524      }
9525      if( rc ){
9526        raw_printf(stderr, "Error querying the selftest table\n");
9527        rc = 1;
9528        sqlite3_finalize(pStmt);
9529        goto meta_command_exit;
9530      }
9531      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9532        int tno = sqlite3_column_int(pStmt, 0);
9533        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9534        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9535        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9536
9537        k = 0;
9538        if( bVerbose>0 ){
9539          char *zQuote = sqlite3_mprintf("%q", zSql);
9540          printf("%d: %s %s\n", tno, zOp, zSql);
9541          sqlite3_free(zQuote);
9542        }
9543        if( strcmp(zOp,"memo")==0 ){
9544          utf8_printf(p->out, "%s\n", zSql);
9545        }else
9546        if( strcmp(zOp,"run")==0 ){
9547          char *zErrMsg = 0;
9548          str.n = 0;
9549          str.z[0] = 0;
9550          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9551          nTest++;
9552          if( bVerbose ){
9553            utf8_printf(p->out, "Result: %s\n", str.z);
9554          }
9555          if( rc || zErrMsg ){
9556            nErr++;
9557            rc = 1;
9558            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9559            sqlite3_free(zErrMsg);
9560          }else if( strcmp(zAns,str.z)!=0 ){
9561            nErr++;
9562            rc = 1;
9563            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9564            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9565          }
9566        }else
9567        {
9568          utf8_printf(stderr,
9569            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9570          rc = 1;
9571          break;
9572        }
9573      } /* End loop over rows of content from SELFTEST */
9574      sqlite3_finalize(pStmt);
9575    } /* End loop over k */
9576    freeText(&str);
9577    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9578  }else
9579
9580  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9581    if( nArg<2 || nArg>3 ){
9582      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9583      rc = 1;
9584    }
9585    if( nArg>=2 ){
9586      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9587                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9588    }
9589    if( nArg>=3 ){
9590      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9591                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9592    }
9593  }else
9594
9595  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9596    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9597    int i;                   /* Loop counter */
9598    int bSchema = 0;         /* Also hash the schema */
9599    int bSeparate = 0;       /* Hash each table separately */
9600    int iSize = 224;         /* Hash algorithm to use */
9601    int bDebug = 0;          /* Only show the query that would have run */
9602    sqlite3_stmt *pStmt;     /* For querying tables names */
9603    char *zSql;              /* SQL to be run */
9604    char *zSep;              /* Separator */
9605    ShellText sSql;          /* Complete SQL for the query to run the hash */
9606    ShellText sQuery;        /* Set of queries used to read all content */
9607    open_db(p, 0);
9608    for(i=1; i<nArg; i++){
9609      const char *z = azArg[i];
9610      if( z[0]=='-' ){
9611        z++;
9612        if( z[0]=='-' ) z++;
9613        if( strcmp(z,"schema")==0 ){
9614          bSchema = 1;
9615        }else
9616        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9617         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9618        ){
9619          iSize = atoi(&z[5]);
9620        }else
9621        if( strcmp(z,"debug")==0 ){
9622          bDebug = 1;
9623        }else
9624        {
9625          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9626                      azArg[i], azArg[0]);
9627          showHelp(p->out, azArg[0]);
9628          rc = 1;
9629          goto meta_command_exit;
9630        }
9631      }else if( zLike ){
9632        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9633        rc = 1;
9634        goto meta_command_exit;
9635      }else{
9636        zLike = z;
9637        bSeparate = 1;
9638        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9639      }
9640    }
9641    if( bSchema ){
9642      zSql = "SELECT lower(name) FROM sqlite_schema"
9643             " WHERE type='table' AND coalesce(rootpage,0)>1"
9644             " UNION ALL SELECT 'sqlite_schema'"
9645             " ORDER BY 1 collate nocase";
9646    }else{
9647      zSql = "SELECT lower(name) FROM sqlite_schema"
9648             " WHERE type='table' AND coalesce(rootpage,0)>1"
9649             " AND name NOT LIKE 'sqlite_%'"
9650             " ORDER BY 1 collate nocase";
9651    }
9652    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9653    initText(&sQuery);
9654    initText(&sSql);
9655    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9656    zSep = "VALUES(";
9657    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9658      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9659      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9660      if( strncmp(zTab, "sqlite_",7)!=0 ){
9661        appendText(&sQuery,"SELECT * FROM ", 0);
9662        appendText(&sQuery,zTab,'"');
9663        appendText(&sQuery," NOT INDEXED;", 0);
9664      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9665        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9666                           " ORDER BY name;", 0);
9667      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9668        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9669                           " ORDER BY name;", 0);
9670      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9671        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9672                           " ORDER BY tbl,idx;", 0);
9673      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9674        appendText(&sQuery, "SELECT * FROM ", 0);
9675        appendText(&sQuery, zTab, 0);
9676        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9677      }
9678      appendText(&sSql, zSep, 0);
9679      appendText(&sSql, sQuery.z, '\'');
9680      sQuery.n = 0;
9681      appendText(&sSql, ",", 0);
9682      appendText(&sSql, zTab, '\'');
9683      zSep = "),(";
9684    }
9685    sqlite3_finalize(pStmt);
9686    if( bSeparate ){
9687      zSql = sqlite3_mprintf(
9688          "%s))"
9689          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9690          "   FROM [sha3sum$query]",
9691          sSql.z, iSize);
9692    }else{
9693      zSql = sqlite3_mprintf(
9694          "%s))"
9695          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9696          "   FROM [sha3sum$query]",
9697          sSql.z, iSize);
9698    }
9699    freeText(&sQuery);
9700    freeText(&sSql);
9701    if( bDebug ){
9702      utf8_printf(p->out, "%s\n", zSql);
9703    }else{
9704      shell_exec(p, zSql, 0);
9705    }
9706    sqlite3_free(zSql);
9707  }else
9708
9709#ifndef SQLITE_NOHAVE_SYSTEM
9710  if( c=='s'
9711   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9712  ){
9713    char *zCmd;
9714    int i, x;
9715    if( nArg<2 ){
9716      raw_printf(stderr, "Usage: .system COMMAND\n");
9717      rc = 1;
9718      goto meta_command_exit;
9719    }
9720    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9721    for(i=2; i<nArg; i++){
9722      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9723                             zCmd, azArg[i]);
9724    }
9725    x = system(zCmd);
9726    sqlite3_free(zCmd);
9727    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9728  }else
9729#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9730
9731  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9732    static const char *azBool[] = { "off", "on", "trigger", "full"};
9733    int i;
9734    if( nArg!=1 ){
9735      raw_printf(stderr, "Usage: .show\n");
9736      rc = 1;
9737      goto meta_command_exit;
9738    }
9739    utf8_printf(p->out, "%12.12s: %s\n","echo",
9740                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9741    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9742    utf8_printf(p->out, "%12.12s: %s\n","explain",
9743         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9744    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9745    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9746    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9747      output_c_string(p->out, p->nullValue);
9748      raw_printf(p->out, "\n");
9749    utf8_printf(p->out,"%12.12s: %s\n","output",
9750            strlen30(p->outfile) ? p->outfile : "stdout");
9751    utf8_printf(p->out,"%12.12s: ", "colseparator");
9752      output_c_string(p->out, p->colSeparator);
9753      raw_printf(p->out, "\n");
9754    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9755      output_c_string(p->out, p->rowSeparator);
9756      raw_printf(p->out, "\n");
9757    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
9758    utf8_printf(p->out, "%12.12s: ", "width");
9759    for (i=0;i<p->nWidth;i++) {
9760      raw_printf(p->out, "%d ", p->colWidth[i]);
9761    }
9762    raw_printf(p->out, "\n");
9763    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9764                p->zDbFilename ? p->zDbFilename : "");
9765  }else
9766
9767  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9768    if( nArg==2 ){
9769      p->statsOn = (u8)booleanValue(azArg[1]);
9770    }else if( nArg==1 ){
9771      display_stats(p->db, p, 0);
9772    }else{
9773      raw_printf(stderr, "Usage: .stats ?on|off?\n");
9774      rc = 1;
9775    }
9776  }else
9777
9778  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9779   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9780                 || strncmp(azArg[0], "indexes", n)==0) )
9781  ){
9782    sqlite3_stmt *pStmt;
9783    char **azResult;
9784    int nRow, nAlloc;
9785    int ii;
9786    ShellText s;
9787    initText(&s);
9788    open_db(p, 0);
9789    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9790    if( rc ){
9791      sqlite3_finalize(pStmt);
9792      return shellDatabaseError(p->db);
9793    }
9794
9795    if( nArg>2 && c=='i' ){
9796      /* It is an historical accident that the .indexes command shows an error
9797      ** when called with the wrong number of arguments whereas the .tables
9798      ** command does not. */
9799      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9800      rc = 1;
9801      sqlite3_finalize(pStmt);
9802      goto meta_command_exit;
9803    }
9804    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9805      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9806      if( zDbName==0 ) continue;
9807      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9808      if( sqlite3_stricmp(zDbName, "main")==0 ){
9809        appendText(&s, "SELECT name FROM ", 0);
9810      }else{
9811        appendText(&s, "SELECT ", 0);
9812        appendText(&s, zDbName, '\'');
9813        appendText(&s, "||'.'||name FROM ", 0);
9814      }
9815      appendText(&s, zDbName, '"');
9816      appendText(&s, ".sqlite_schema ", 0);
9817      if( c=='t' ){
9818        appendText(&s," WHERE type IN ('table','view')"
9819                      "   AND name NOT LIKE 'sqlite_%'"
9820                      "   AND name LIKE ?1", 0);
9821      }else{
9822        appendText(&s," WHERE type='index'"
9823                      "   AND tbl_name LIKE ?1", 0);
9824      }
9825    }
9826    rc = sqlite3_finalize(pStmt);
9827    appendText(&s, " ORDER BY 1", 0);
9828    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9829    freeText(&s);
9830    if( rc ) return shellDatabaseError(p->db);
9831
9832    /* Run the SQL statement prepared by the above block. Store the results
9833    ** as an array of nul-terminated strings in azResult[].  */
9834    nRow = nAlloc = 0;
9835    azResult = 0;
9836    if( nArg>1 ){
9837      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9838    }else{
9839      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9840    }
9841    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9842      if( nRow>=nAlloc ){
9843        char **azNew;
9844        int n2 = nAlloc*2 + 10;
9845        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9846        if( azNew==0 ) shell_out_of_memory();
9847        nAlloc = n2;
9848        azResult = azNew;
9849      }
9850      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9851      if( 0==azResult[nRow] ) shell_out_of_memory();
9852      nRow++;
9853    }
9854    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9855      rc = shellDatabaseError(p->db);
9856    }
9857
9858    /* Pretty-print the contents of array azResult[] to the output */
9859    if( rc==0 && nRow>0 ){
9860      int len, maxlen = 0;
9861      int i, j;
9862      int nPrintCol, nPrintRow;
9863      for(i=0; i<nRow; i++){
9864        len = strlen30(azResult[i]);
9865        if( len>maxlen ) maxlen = len;
9866      }
9867      nPrintCol = 80/(maxlen+2);
9868      if( nPrintCol<1 ) nPrintCol = 1;
9869      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9870      for(i=0; i<nPrintRow; i++){
9871        for(j=i; j<nRow; j+=nPrintRow){
9872          char *zSp = j<nPrintRow ? "" : "  ";
9873          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9874                      azResult[j] ? azResult[j]:"");
9875        }
9876        raw_printf(p->out, "\n");
9877      }
9878    }
9879
9880    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9881    sqlite3_free(azResult);
9882  }else
9883
9884  /* Begin redirecting output to the file "testcase-out.txt" */
9885  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9886    output_reset(p);
9887    p->out = output_file_open("testcase-out.txt", 0);
9888    if( p->out==0 ){
9889      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9890    }
9891    if( nArg>=2 ){
9892      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9893    }else{
9894      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9895    }
9896  }else
9897
9898#ifndef SQLITE_UNTESTABLE
9899  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9900    static const struct {
9901       const char *zCtrlName;   /* Name of a test-control option */
9902       int ctrlCode;            /* Integer code for that option */
9903       const char *zUsage;      /* Usage notes */
9904    } aCtrl[] = {
9905      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9906      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9907    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9908    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9909      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9910      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9911    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9912      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9913      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9914      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9915      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9916      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9917#ifdef YYCOVERAGE
9918      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9919#endif
9920      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9921      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9922      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9923      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9924      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
9925    };
9926    int testctrl = -1;
9927    int iCtrl = -1;
9928    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9929    int isOk = 0;
9930    int i, n2;
9931    const char *zCmd = 0;
9932
9933    open_db(p, 0);
9934    zCmd = nArg>=2 ? azArg[1] : "help";
9935
9936    /* The argument can optionally begin with "-" or "--" */
9937    if( zCmd[0]=='-' && zCmd[1] ){
9938      zCmd++;
9939      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9940    }
9941
9942    /* --help lists all test-controls */
9943    if( strcmp(zCmd,"help")==0 ){
9944      utf8_printf(p->out, "Available test-controls:\n");
9945      for(i=0; i<ArraySize(aCtrl); i++){
9946        utf8_printf(p->out, "  .testctrl %s %s\n",
9947                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
9948      }
9949      rc = 1;
9950      goto meta_command_exit;
9951    }
9952
9953    /* convert testctrl text option to value. allow any unique prefix
9954    ** of the option name, or a numerical value. */
9955    n2 = strlen30(zCmd);
9956    for(i=0; i<ArraySize(aCtrl); i++){
9957      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
9958        if( testctrl<0 ){
9959          testctrl = aCtrl[i].ctrlCode;
9960          iCtrl = i;
9961        }else{
9962          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
9963                              "Use \".testctrl --help\" for help\n", zCmd);
9964          rc = 1;
9965          goto meta_command_exit;
9966        }
9967      }
9968    }
9969    if( testctrl<0 ){
9970      utf8_printf(stderr,"Error: unknown test-control: %s\n"
9971                         "Use \".testctrl --help\" for help\n", zCmd);
9972    }else{
9973      switch(testctrl){
9974
9975        /* sqlite3_test_control(int, db, int) */
9976        case SQLITE_TESTCTRL_OPTIMIZATIONS:
9977          if( nArg==3 ){
9978            int opt = (int)strtol(azArg[2], 0, 0);
9979            rc2 = sqlite3_test_control(testctrl, p->db, opt);
9980            isOk = 3;
9981          }
9982          break;
9983
9984        /* sqlite3_test_control(int) */
9985        case SQLITE_TESTCTRL_PRNG_SAVE:
9986        case SQLITE_TESTCTRL_PRNG_RESTORE:
9987        case SQLITE_TESTCTRL_BYTEORDER:
9988          if( nArg==2 ){
9989            rc2 = sqlite3_test_control(testctrl);
9990            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
9991          }
9992          break;
9993
9994        /* sqlite3_test_control(int, uint) */
9995        case SQLITE_TESTCTRL_PENDING_BYTE:
9996          if( nArg==3 ){
9997            unsigned int opt = (unsigned int)integerValue(azArg[2]);
9998            rc2 = sqlite3_test_control(testctrl, opt);
9999            isOk = 3;
10000          }
10001          break;
10002
10003        /* sqlite3_test_control(int, int, sqlite3*) */
10004        case SQLITE_TESTCTRL_PRNG_SEED:
10005          if( nArg==3 || nArg==4 ){
10006            int ii = (int)integerValue(azArg[2]);
10007            sqlite3 *db;
10008            if( ii==0 && strcmp(azArg[2],"random")==0 ){
10009              sqlite3_randomness(sizeof(ii),&ii);
10010              printf("-- random seed: %d\n", ii);
10011            }
10012            if( nArg==3 ){
10013              db = 0;
10014            }else{
10015              db = p->db;
10016              /* Make sure the schema has been loaded */
10017              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10018            }
10019            rc2 = sqlite3_test_control(testctrl, ii, db);
10020            isOk = 3;
10021          }
10022          break;
10023
10024        /* sqlite3_test_control(int, int) */
10025        case SQLITE_TESTCTRL_ASSERT:
10026        case SQLITE_TESTCTRL_ALWAYS:
10027          if( nArg==3 ){
10028            int opt = booleanValue(azArg[2]);
10029            rc2 = sqlite3_test_control(testctrl, opt);
10030            isOk = 1;
10031          }
10032          break;
10033
10034        /* sqlite3_test_control(int, int) */
10035        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10036        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10037          if( nArg==3 ){
10038            int opt = booleanValue(azArg[2]);
10039            rc2 = sqlite3_test_control(testctrl, opt);
10040            isOk = 3;
10041          }
10042          break;
10043
10044        /* sqlite3_test_control(sqlite3*) */
10045        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10046          rc2 = sqlite3_test_control(testctrl, p->db);
10047          isOk = 3;
10048          break;
10049
10050        case SQLITE_TESTCTRL_IMPOSTER:
10051          if( nArg==5 ){
10052            rc2 = sqlite3_test_control(testctrl, p->db,
10053                          azArg[2],
10054                          integerValue(azArg[3]),
10055                          integerValue(azArg[4]));
10056            isOk = 3;
10057          }
10058          break;
10059
10060        case SQLITE_TESTCTRL_SEEK_COUNT: {
10061          u64 x = 0;
10062          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10063          utf8_printf(p->out, "%llu\n", x);
10064          isOk = 3;
10065          break;
10066        }
10067
10068#ifdef YYCOVERAGE
10069        case SQLITE_TESTCTRL_PARSER_COVERAGE:
10070          if( nArg==2 ){
10071            sqlite3_test_control(testctrl, p->out);
10072            isOk = 3;
10073          }
10074#endif
10075      }
10076    }
10077    if( isOk==0 && iCtrl>=0 ){
10078      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10079      rc = 1;
10080    }else if( isOk==1 ){
10081      raw_printf(p->out, "%d\n", rc2);
10082    }else if( isOk==2 ){
10083      raw_printf(p->out, "0x%08x\n", rc2);
10084    }
10085  }else
10086#endif /* !defined(SQLITE_UNTESTABLE) */
10087
10088  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10089    open_db(p, 0);
10090    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10091  }else
10092
10093  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10094    if( nArg==2 ){
10095      enableTimer = booleanValue(azArg[1]);
10096      if( enableTimer && !HAS_TIMER ){
10097        raw_printf(stderr, "Error: timer not available on this system.\n");
10098        enableTimer = 0;
10099      }
10100    }else{
10101      raw_printf(stderr, "Usage: .timer on|off\n");
10102      rc = 1;
10103    }
10104  }else
10105
10106#ifndef SQLITE_OMIT_TRACE
10107  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10108    int mType = 0;
10109    int jj;
10110    open_db(p, 0);
10111    for(jj=1; jj<nArg; jj++){
10112      const char *z = azArg[jj];
10113      if( z[0]=='-' ){
10114        if( optionMatch(z, "expanded") ){
10115          p->eTraceType = SHELL_TRACE_EXPANDED;
10116        }
10117#ifdef SQLITE_ENABLE_NORMALIZE
10118        else if( optionMatch(z, "normalized") ){
10119          p->eTraceType = SHELL_TRACE_NORMALIZED;
10120        }
10121#endif
10122        else if( optionMatch(z, "plain") ){
10123          p->eTraceType = SHELL_TRACE_PLAIN;
10124        }
10125        else if( optionMatch(z, "profile") ){
10126          mType |= SQLITE_TRACE_PROFILE;
10127        }
10128        else if( optionMatch(z, "row") ){
10129          mType |= SQLITE_TRACE_ROW;
10130        }
10131        else if( optionMatch(z, "stmt") ){
10132          mType |= SQLITE_TRACE_STMT;
10133        }
10134        else if( optionMatch(z, "close") ){
10135          mType |= SQLITE_TRACE_CLOSE;
10136        }
10137        else {
10138          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10139          rc = 1;
10140          goto meta_command_exit;
10141        }
10142      }else{
10143        output_file_close(p->traceOut);
10144        p->traceOut = output_file_open(azArg[1], 0);
10145      }
10146    }
10147    if( p->traceOut==0 ){
10148      sqlite3_trace_v2(p->db, 0, 0, 0);
10149    }else{
10150      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10151      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10152    }
10153  }else
10154#endif /* !defined(SQLITE_OMIT_TRACE) */
10155
10156#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10157  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10158    int ii;
10159    int lenOpt;
10160    char *zOpt;
10161    if( nArg<2 ){
10162      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10163      rc = 1;
10164      goto meta_command_exit;
10165    }
10166    open_db(p, 0);
10167    zOpt = azArg[1];
10168    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10169    lenOpt = (int)strlen(zOpt);
10170    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10171      assert( azArg[nArg]==0 );
10172      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10173    }else{
10174      for(ii=1; ii<nArg; ii++){
10175        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10176      }
10177    }
10178  }else
10179#endif
10180
10181#if SQLITE_USER_AUTHENTICATION
10182  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10183    if( nArg<2 ){
10184      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10185      rc = 1;
10186      goto meta_command_exit;
10187    }
10188    open_db(p, 0);
10189    if( strcmp(azArg[1],"login")==0 ){
10190      if( nArg!=4 ){
10191        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10192        rc = 1;
10193        goto meta_command_exit;
10194      }
10195      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10196                                     strlen30(azArg[3]));
10197      if( rc ){
10198        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10199        rc = 1;
10200      }
10201    }else if( strcmp(azArg[1],"add")==0 ){
10202      if( nArg!=5 ){
10203        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10204        rc = 1;
10205        goto meta_command_exit;
10206      }
10207      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10208                            booleanValue(azArg[4]));
10209      if( rc ){
10210        raw_printf(stderr, "User-Add failed: %d\n", rc);
10211        rc = 1;
10212      }
10213    }else if( strcmp(azArg[1],"edit")==0 ){
10214      if( nArg!=5 ){
10215        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10216        rc = 1;
10217        goto meta_command_exit;
10218      }
10219      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10220                              booleanValue(azArg[4]));
10221      if( rc ){
10222        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10223        rc = 1;
10224      }
10225    }else if( strcmp(azArg[1],"delete")==0 ){
10226      if( nArg!=3 ){
10227        raw_printf(stderr, "Usage: .user delete USER\n");
10228        rc = 1;
10229        goto meta_command_exit;
10230      }
10231      rc = sqlite3_user_delete(p->db, azArg[2]);
10232      if( rc ){
10233        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10234        rc = 1;
10235      }
10236    }else{
10237      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10238      rc = 1;
10239      goto meta_command_exit;
10240    }
10241  }else
10242#endif /* SQLITE_USER_AUTHENTICATION */
10243
10244  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10245    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10246        sqlite3_libversion(), sqlite3_sourceid());
10247#if SQLITE_HAVE_ZLIB
10248    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10249#endif
10250#define CTIMEOPT_VAL_(opt) #opt
10251#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10252#if defined(__clang__) && defined(__clang_major__)
10253    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10254                    CTIMEOPT_VAL(__clang_minor__) "."
10255                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10256#elif defined(_MSC_VER)
10257    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10258#elif defined(__GNUC__) && defined(__VERSION__)
10259    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10260#endif
10261  }else
10262
10263  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10264    const char *zDbName = nArg==2 ? azArg[1] : "main";
10265    sqlite3_vfs *pVfs = 0;
10266    if( p->db ){
10267      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10268      if( pVfs ){
10269        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10270        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10271        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10272        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10273      }
10274    }
10275  }else
10276
10277  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10278    sqlite3_vfs *pVfs;
10279    sqlite3_vfs *pCurrent = 0;
10280    if( p->db ){
10281      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10282    }
10283    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10284      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10285           pVfs==pCurrent ? "  <--- CURRENT" : "");
10286      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10287      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10288      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10289      if( pVfs->pNext ){
10290        raw_printf(p->out, "-----------------------------------\n");
10291      }
10292    }
10293  }else
10294
10295  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10296    const char *zDbName = nArg==2 ? azArg[1] : "main";
10297    char *zVfsName = 0;
10298    if( p->db ){
10299      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10300      if( zVfsName ){
10301        utf8_printf(p->out, "%s\n", zVfsName);
10302        sqlite3_free(zVfsName);
10303      }
10304    }
10305  }else
10306
10307#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10308  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10309    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
10310  }else
10311#endif
10312
10313  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10314    int j;
10315    assert( nArg<=ArraySize(azArg) );
10316    p->nWidth = nArg-1;
10317    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10318    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10319    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10320    for(j=1; j<nArg; j++){
10321      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10322    }
10323  }else
10324
10325  {
10326    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10327      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10328    rc = 1;
10329  }
10330
10331meta_command_exit:
10332  if( p->outCount ){
10333    p->outCount--;
10334    if( p->outCount==0 ) output_reset(p);
10335  }
10336  return rc;
10337}
10338
10339/*
10340** Return TRUE if a semicolon occurs anywhere in the first N characters
10341** of string z[].
10342*/
10343static int line_contains_semicolon(const char *z, int N){
10344  int i;
10345  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
10346  return 0;
10347}
10348
10349/*
10350** Test to see if a line consists entirely of whitespace.
10351*/
10352static int _all_whitespace(const char *z){
10353  for(; *z; z++){
10354    if( IsSpace(z[0]) ) continue;
10355    if( *z=='/' && z[1]=='*' ){
10356      z += 2;
10357      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
10358      if( *z==0 ) return 0;
10359      z++;
10360      continue;
10361    }
10362    if( *z=='-' && z[1]=='-' ){
10363      z += 2;
10364      while( *z && *z!='\n' ){ z++; }
10365      if( *z==0 ) return 1;
10366      continue;
10367    }
10368    return 0;
10369  }
10370  return 1;
10371}
10372
10373/*
10374** Return TRUE if the line typed in is an SQL command terminator other
10375** than a semi-colon.  The SQL Server style "go" command is understood
10376** as is the Oracle "/".
10377*/
10378static int line_is_command_terminator(const char *zLine){
10379  while( IsSpace(zLine[0]) ){ zLine++; };
10380  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
10381    return 1;  /* Oracle */
10382  }
10383  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
10384         && _all_whitespace(&zLine[2]) ){
10385    return 1;  /* SQL Server */
10386  }
10387  return 0;
10388}
10389
10390/*
10391** We need a default sqlite3_complete() implementation to use in case
10392** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10393** any arbitrary text is a complete SQL statement.  This is not very
10394** user-friendly, but it does seem to work.
10395*/
10396#ifdef SQLITE_OMIT_COMPLETE
10397#define sqlite3_complete(x) 1
10398#endif
10399
10400/*
10401** Return true if zSql is a complete SQL statement.  Return false if it
10402** ends in the middle of a string literal or C-style comment.
10403*/
10404static int line_is_complete(char *zSql, int nSql){
10405  int rc;
10406  if( zSql==0 ) return 1;
10407  zSql[nSql] = ';';
10408  zSql[nSql+1] = 0;
10409  rc = sqlite3_complete(zSql);
10410  zSql[nSql] = 0;
10411  return rc;
10412}
10413
10414/*
10415** Run a single line of SQL.  Return the number of errors.
10416*/
10417static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10418  int rc;
10419  char *zErrMsg = 0;
10420
10421  open_db(p, 0);
10422  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10423  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10424  BEGIN_TIMER;
10425  rc = shell_exec(p, zSql, &zErrMsg);
10426  END_TIMER;
10427  if( rc || zErrMsg ){
10428    char zPrefix[100];
10429    if( in!=0 || !stdin_is_interactive ){
10430      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10431                       "Error: near line %d:", startline);
10432    }else{
10433      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10434    }
10435    if( zErrMsg!=0 ){
10436      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10437      sqlite3_free(zErrMsg);
10438      zErrMsg = 0;
10439    }else{
10440      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10441    }
10442    return 1;
10443  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10444    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
10445            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
10446  }
10447  return 0;
10448}
10449
10450
10451/*
10452** Read input from *in and process it.  If *in==0 then input
10453** is interactive - the user is typing it it.  Otherwise, input
10454** is coming from a file or device.  A prompt is issued and history
10455** is saved only if input is interactive.  An interrupt signal will
10456** cause this routine to exit immediately, unless input is interactive.
10457**
10458** Return the number of errors.
10459*/
10460static int process_input(ShellState *p){
10461  char *zLine = 0;          /* A single input line */
10462  char *zSql = 0;           /* Accumulated SQL text */
10463  int nLine;                /* Length of current line */
10464  int nSql = 0;             /* Bytes of zSql[] used */
10465  int nAlloc = 0;           /* Allocated zSql[] space */
10466  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10467  int rc;                   /* Error code */
10468  int errCnt = 0;           /* Number of errors seen */
10469  int startline = 0;        /* Line number for start of current input */
10470
10471  p->lineno = 0;
10472  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10473    fflush(p->out);
10474    zLine = one_input_line(p->in, zLine, nSql>0);
10475    if( zLine==0 ){
10476      /* End of input */
10477      if( p->in==0 && stdin_is_interactive ) printf("\n");
10478      break;
10479    }
10480    if( seenInterrupt ){
10481      if( p->in!=0 ) break;
10482      seenInterrupt = 0;
10483    }
10484    p->lineno++;
10485    if( nSql==0 && _all_whitespace(zLine) ){
10486      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10487      continue;
10488    }
10489    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10490      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10491      if( zLine[0]=='.' ){
10492        rc = do_meta_command(zLine, p);
10493        if( rc==2 ){ /* exit requested */
10494          break;
10495        }else if( rc ){
10496          errCnt++;
10497        }
10498      }
10499      continue;
10500    }
10501    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10502      memcpy(zLine,";",2);
10503    }
10504    nLine = strlen30(zLine);
10505    if( nSql+nLine+2>=nAlloc ){
10506      nAlloc = nSql+nLine+100;
10507      zSql = realloc(zSql, nAlloc);
10508      if( zSql==0 ) shell_out_of_memory();
10509    }
10510    nSqlPrior = nSql;
10511    if( nSql==0 ){
10512      int i;
10513      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10514      assert( nAlloc>0 && zSql!=0 );
10515      memcpy(zSql, zLine+i, nLine+1-i);
10516      startline = p->lineno;
10517      nSql = nLine-i;
10518    }else{
10519      zSql[nSql++] = '\n';
10520      memcpy(zSql+nSql, zLine, nLine+1);
10521      nSql += nLine;
10522    }
10523    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10524                && sqlite3_complete(zSql) ){
10525      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10526      nSql = 0;
10527      if( p->outCount ){
10528        output_reset(p);
10529        p->outCount = 0;
10530      }else{
10531        clearTempFile(p);
10532      }
10533    }else if( nSql && _all_whitespace(zSql) ){
10534      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10535      nSql = 0;
10536    }
10537  }
10538  if( nSql && !_all_whitespace(zSql) ){
10539    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10540  }
10541  free(zSql);
10542  free(zLine);
10543  return errCnt>0;
10544}
10545
10546/*
10547** Return a pathname which is the user's home directory.  A
10548** 0 return indicates an error of some kind.
10549*/
10550static char *find_home_dir(int clearFlag){
10551  static char *home_dir = NULL;
10552  if( clearFlag ){
10553    free(home_dir);
10554    home_dir = 0;
10555    return 0;
10556  }
10557  if( home_dir ) return home_dir;
10558
10559#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10560     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10561  {
10562    struct passwd *pwent;
10563    uid_t uid = getuid();
10564    if( (pwent=getpwuid(uid)) != NULL) {
10565      home_dir = pwent->pw_dir;
10566    }
10567  }
10568#endif
10569
10570#if defined(_WIN32_WCE)
10571  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10572   */
10573  home_dir = "/";
10574#else
10575
10576#if defined(_WIN32) || defined(WIN32)
10577  if (!home_dir) {
10578    home_dir = getenv("USERPROFILE");
10579  }
10580#endif
10581
10582  if (!home_dir) {
10583    home_dir = getenv("HOME");
10584  }
10585
10586#if defined(_WIN32) || defined(WIN32)
10587  if (!home_dir) {
10588    char *zDrive, *zPath;
10589    int n;
10590    zDrive = getenv("HOMEDRIVE");
10591    zPath = getenv("HOMEPATH");
10592    if( zDrive && zPath ){
10593      n = strlen30(zDrive) + strlen30(zPath) + 1;
10594      home_dir = malloc( n );
10595      if( home_dir==0 ) return 0;
10596      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10597      return home_dir;
10598    }
10599    home_dir = "c:\\";
10600  }
10601#endif
10602
10603#endif /* !_WIN32_WCE */
10604
10605  if( home_dir ){
10606    int n = strlen30(home_dir) + 1;
10607    char *z = malloc( n );
10608    if( z ) memcpy(z, home_dir, n);
10609    home_dir = z;
10610  }
10611
10612  return home_dir;
10613}
10614
10615/*
10616** Read input from the file given by sqliterc_override.  Or if that
10617** parameter is NULL, take input from ~/.sqliterc
10618**
10619** Returns the number of errors.
10620*/
10621static void process_sqliterc(
10622  ShellState *p,                  /* Configuration data */
10623  const char *sqliterc_override   /* Name of config file. NULL to use default */
10624){
10625  char *home_dir = NULL;
10626  const char *sqliterc = sqliterc_override;
10627  char *zBuf = 0;
10628  FILE *inSaved = p->in;
10629  int savedLineno = p->lineno;
10630
10631  if (sqliterc == NULL) {
10632    home_dir = find_home_dir(0);
10633    if( home_dir==0 ){
10634      raw_printf(stderr, "-- warning: cannot find home directory;"
10635                      " cannot read ~/.sqliterc\n");
10636      return;
10637    }
10638    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10639    sqliterc = zBuf;
10640  }
10641  p->in = fopen(sqliterc,"rb");
10642  if( p->in ){
10643    if( stdin_is_interactive ){
10644      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10645    }
10646    process_input(p);
10647    fclose(p->in);
10648  }
10649  p->in = inSaved;
10650  p->lineno = savedLineno;
10651  sqlite3_free(zBuf);
10652}
10653
10654/*
10655** Show available command line options
10656*/
10657static const char zOptions[] =
10658#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10659  "   -A ARGS...           run \".archive ARGS\" and exit\n"
10660#endif
10661  "   -append              append the database to the end of the file\n"
10662  "   -ascii               set output mode to 'ascii'\n"
10663  "   -bail                stop after hitting an error\n"
10664  "   -batch               force batch I/O\n"
10665  "   -box                 set output mode to 'box'\n"
10666  "   -column              set output mode to 'column'\n"
10667  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
10668  "   -csv                 set output mode to 'csv'\n"
10669#if defined(SQLITE_ENABLE_DESERIALIZE)
10670  "   -deserialize         open the database using sqlite3_deserialize()\n"
10671#endif
10672  "   -echo                print commands before execution\n"
10673  "   -init FILENAME       read/process named file\n"
10674  "   -[no]header          turn headers on or off\n"
10675#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10676  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
10677#endif
10678  "   -help                show this message\n"
10679  "   -html                set output mode to HTML\n"
10680  "   -interactive         force interactive I/O\n"
10681  "   -json                set output mode to 'json'\n"
10682  "   -line                set output mode to 'line'\n"
10683  "   -list                set output mode to 'list'\n"
10684  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
10685  "   -markdown            set output mode to 'markdown'\n"
10686#if defined(SQLITE_ENABLE_DESERIALIZE)
10687  "   -maxsize N           maximum size for a --deserialize database\n"
10688#endif
10689  "   -memtrace            trace all memory allocations and deallocations\n"
10690  "   -mmap N              default mmap size set to N\n"
10691#ifdef SQLITE_ENABLE_MULTIPLEX
10692  "   -multiplex           enable the multiplexor VFS\n"
10693#endif
10694  "   -newline SEP         set output row separator. Default: '\\n'\n"
10695  "   -nofollow            refuse to open symbolic links to database files\n"
10696  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
10697  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
10698  "   -quote               set output mode to 'quote'\n"
10699  "   -readonly            open the database read-only\n"
10700  "   -separator SEP       set output column separator. Default: '|'\n"
10701#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10702  "   -sorterref SIZE      sorter references threshold size\n"
10703#endif
10704  "   -stats               print memory stats before each finalize\n"
10705  "   -table               set output mode to 'table'\n"
10706  "   -version             show SQLite version\n"
10707  "   -vfs NAME            use NAME as the default VFS\n"
10708#ifdef SQLITE_ENABLE_VFSTRACE
10709  "   -vfstrace            enable tracing of all VFS calls\n"
10710#endif
10711#ifdef SQLITE_HAVE_ZLIB
10712  "   -zip                 open the file as a ZIP Archive\n"
10713#endif
10714;
10715static void usage(int showDetail){
10716  utf8_printf(stderr,
10717      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10718      "FILENAME is the name of an SQLite database. A new database is created\n"
10719      "if the file does not previously exist.\n", Argv0);
10720  if( showDetail ){
10721    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10722  }else{
10723    raw_printf(stderr, "Use the -help option for additional information\n");
10724  }
10725  exit(1);
10726}
10727
10728/*
10729** Internal check:  Verify that the SQLite is uninitialized.  Print a
10730** error message if it is initialized.
10731*/
10732static void verify_uninitialized(void){
10733  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10734    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10735                        " initialization.\n");
10736  }
10737}
10738
10739/*
10740** Initialize the state information in data
10741*/
10742static void main_init(ShellState *data) {
10743  memset(data, 0, sizeof(*data));
10744  data->normalMode = data->cMode = data->mode = MODE_List;
10745  data->autoExplain = 1;
10746  memcpy(data->colSeparator,SEP_Column, 2);
10747  memcpy(data->rowSeparator,SEP_Row, 2);
10748  data->showHeader = 0;
10749  data->shellFlgs = SHFLG_Lookaside;
10750  verify_uninitialized();
10751  sqlite3_config(SQLITE_CONFIG_URI, 1);
10752  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10753  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10754  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10755  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10756}
10757
10758/*
10759** Output text to the console in a font that attracts extra attention.
10760*/
10761#ifdef _WIN32
10762static void printBold(const char *zText){
10763#if !SQLITE_OS_WINRT
10764  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10765  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10766  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10767  SetConsoleTextAttribute(out,
10768         FOREGROUND_RED|FOREGROUND_INTENSITY
10769  );
10770#endif
10771  printf("%s", zText);
10772#if !SQLITE_OS_WINRT
10773  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10774#endif
10775}
10776#else
10777static void printBold(const char *zText){
10778  printf("\033[1m%s\033[0m", zText);
10779}
10780#endif
10781
10782/*
10783** Get the argument to an --option.  Throw an error and die if no argument
10784** is available.
10785*/
10786static char *cmdline_option_value(int argc, char **argv, int i){
10787  if( i==argc ){
10788    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10789            argv[0], argv[argc-1]);
10790    exit(1);
10791  }
10792  return argv[i];
10793}
10794
10795#ifndef SQLITE_SHELL_IS_UTF8
10796#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
10797#    define SQLITE_SHELL_IS_UTF8          (0)
10798#  else
10799#    define SQLITE_SHELL_IS_UTF8          (1)
10800#  endif
10801#endif
10802
10803#if SQLITE_SHELL_IS_UTF8
10804int SQLITE_CDECL main(int argc, char **argv){
10805#else
10806int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10807  char **argv;
10808#endif
10809  char *zErrMsg = 0;
10810  ShellState data;
10811  const char *zInitFile = 0;
10812  int i;
10813  int rc = 0;
10814  int warnInmemoryDb = 0;
10815  int readStdin = 1;
10816  int nCmd = 0;
10817  char **azCmd = 0;
10818  const char *zVfs = 0;           /* Value of -vfs command-line option */
10819#if !SQLITE_SHELL_IS_UTF8
10820  char **argvToFree = 0;
10821  int argcToFree = 0;
10822#endif
10823
10824  setBinaryMode(stdin, 0);
10825  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10826  stdin_is_interactive = isatty(0);
10827  stdout_is_console = isatty(1);
10828
10829#ifdef SQLITE_DEBUG
10830  registerOomSimulator();
10831#endif
10832
10833#if !defined(_WIN32_WCE)
10834  if( getenv("SQLITE_DEBUG_BREAK") ){
10835    if( isatty(0) && isatty(2) ){
10836      fprintf(stderr,
10837          "attach debugger to process %d and press any key to continue.\n",
10838          GETPID());
10839      fgetc(stdin);
10840    }else{
10841#if defined(_WIN32) || defined(WIN32)
10842#if SQLITE_OS_WINRT
10843      __debugbreak();
10844#else
10845      DebugBreak();
10846#endif
10847#elif defined(SIGTRAP)
10848      raise(SIGTRAP);
10849#endif
10850    }
10851  }
10852#endif
10853
10854#if USE_SYSTEM_SQLITE+0!=1
10855  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10856    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10857            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10858    exit(1);
10859  }
10860#endif
10861  main_init(&data);
10862
10863  /* On Windows, we must translate command-line arguments into UTF-8.
10864  ** The SQLite memory allocator subsystem has to be enabled in order to
10865  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10866  ** subsequent sqlite3_config() calls will work.  So copy all results into
10867  ** memory that does not come from the SQLite memory allocator.
10868  */
10869#if !SQLITE_SHELL_IS_UTF8
10870  sqlite3_initialize();
10871  argvToFree = malloc(sizeof(argv[0])*argc*2);
10872  argcToFree = argc;
10873  argv = argvToFree + argc;
10874  if( argv==0 ) shell_out_of_memory();
10875  for(i=0; i<argc; i++){
10876    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10877    int n;
10878    if( z==0 ) shell_out_of_memory();
10879    n = (int)strlen(z);
10880    argv[i] = malloc( n+1 );
10881    if( argv[i]==0 ) shell_out_of_memory();
10882    memcpy(argv[i], z, n+1);
10883    argvToFree[i] = argv[i];
10884    sqlite3_free(z);
10885  }
10886  sqlite3_shutdown();
10887#endif
10888
10889  assert( argc>=1 && argv && argv[0] );
10890  Argv0 = argv[0];
10891
10892  /* Make sure we have a valid signal handler early, before anything
10893  ** else is done.
10894  */
10895#ifdef SIGINT
10896  signal(SIGINT, interrupt_handler);
10897#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10898  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10899#endif
10900
10901#ifdef SQLITE_SHELL_DBNAME_PROC
10902  {
10903    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10904    ** of a C-function that will provide the name of the database file.  Use
10905    ** this compile-time option to embed this shell program in larger
10906    ** applications. */
10907    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10908    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10909    warnInmemoryDb = 0;
10910  }
10911#endif
10912
10913  /* Do an initial pass through the command-line argument to locate
10914  ** the name of the database file, the name of the initialization file,
10915  ** the size of the alternative malloc heap,
10916  ** and the first command to execute.
10917  */
10918  verify_uninitialized();
10919  for(i=1; i<argc; i++){
10920    char *z;
10921    z = argv[i];
10922    if( z[0]!='-' ){
10923      if( data.zDbFilename==0 ){
10924        data.zDbFilename = z;
10925      }else{
10926        /* Excesss arguments are interpreted as SQL (or dot-commands) and
10927        ** mean that nothing is read from stdin */
10928        readStdin = 0;
10929        nCmd++;
10930        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
10931        if( azCmd==0 ) shell_out_of_memory();
10932        azCmd[nCmd-1] = z;
10933      }
10934    }
10935    if( z[1]=='-' ) z++;
10936    if( strcmp(z,"-separator")==0
10937     || strcmp(z,"-nullvalue")==0
10938     || strcmp(z,"-newline")==0
10939     || strcmp(z,"-cmd")==0
10940    ){
10941      (void)cmdline_option_value(argc, argv, ++i);
10942    }else if( strcmp(z,"-init")==0 ){
10943      zInitFile = cmdline_option_value(argc, argv, ++i);
10944    }else if( strcmp(z,"-batch")==0 ){
10945      /* Need to check for batch mode here to so we can avoid printing
10946      ** informational messages (like from process_sqliterc) before
10947      ** we do the actual processing of arguments later in a second pass.
10948      */
10949      stdin_is_interactive = 0;
10950    }else if( strcmp(z,"-heap")==0 ){
10951#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10952      const char *zSize;
10953      sqlite3_int64 szHeap;
10954
10955      zSize = cmdline_option_value(argc, argv, ++i);
10956      szHeap = integerValue(zSize);
10957      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
10958      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
10959#else
10960      (void)cmdline_option_value(argc, argv, ++i);
10961#endif
10962    }else if( strcmp(z,"-pagecache")==0 ){
10963      sqlite3_int64 n, sz;
10964      sz = integerValue(cmdline_option_value(argc,argv,++i));
10965      if( sz>70000 ) sz = 70000;
10966      if( sz<0 ) sz = 0;
10967      n = integerValue(cmdline_option_value(argc,argv,++i));
10968      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
10969        n = 0xffffffffffffLL/sz;
10970      }
10971      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
10972                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
10973      data.shellFlgs |= SHFLG_Pagecache;
10974    }else if( strcmp(z,"-lookaside")==0 ){
10975      int n, sz;
10976      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10977      if( sz<0 ) sz = 0;
10978      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10979      if( n<0 ) n = 0;
10980      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
10981      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
10982#ifdef SQLITE_ENABLE_VFSTRACE
10983    }else if( strcmp(z,"-vfstrace")==0 ){
10984      extern int vfstrace_register(
10985         const char *zTraceName,
10986         const char *zOldVfsName,
10987         int (*xOut)(const char*,void*),
10988         void *pOutArg,
10989         int makeDefault
10990      );
10991      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
10992#endif
10993#ifdef SQLITE_ENABLE_MULTIPLEX
10994    }else if( strcmp(z,"-multiplex")==0 ){
10995      extern int sqlite3_multiple_initialize(const char*,int);
10996      sqlite3_multiplex_initialize(0, 1);
10997#endif
10998    }else if( strcmp(z,"-mmap")==0 ){
10999      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11000      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
11001#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11002    }else if( strcmp(z,"-sorterref")==0 ){
11003      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11004      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11005#endif
11006    }else if( strcmp(z,"-vfs")==0 ){
11007      zVfs = cmdline_option_value(argc, argv, ++i);
11008#ifdef SQLITE_HAVE_ZLIB
11009    }else if( strcmp(z,"-zip")==0 ){
11010      data.openMode = SHELL_OPEN_ZIPFILE;
11011#endif
11012    }else if( strcmp(z,"-append")==0 ){
11013      data.openMode = SHELL_OPEN_APPENDVFS;
11014#ifdef SQLITE_ENABLE_DESERIALIZE
11015    }else if( strcmp(z,"-deserialize")==0 ){
11016      data.openMode = SHELL_OPEN_DESERIALIZE;
11017    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11018      data.szMax = integerValue(argv[++i]);
11019#endif
11020    }else if( strcmp(z,"-readonly")==0 ){
11021      data.openMode = SHELL_OPEN_READONLY;
11022    }else if( strcmp(z,"-nofollow")==0 ){
11023      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11024#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11025    }else if( strncmp(z, "-A",2)==0 ){
11026      /* All remaining command-line arguments are passed to the ".archive"
11027      ** command, so ignore them */
11028      break;
11029#endif
11030    }else if( strcmp(z, "-memtrace")==0 ){
11031      sqlite3MemTraceActivate(stderr);
11032    }
11033  }
11034  verify_uninitialized();
11035
11036
11037#ifdef SQLITE_SHELL_INIT_PROC
11038  {
11039    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11040    ** of a C-function that will perform initialization actions on SQLite that
11041    ** occur just before or after sqlite3_initialize(). Use this compile-time
11042    ** option to embed this shell program in larger applications. */
11043    extern void SQLITE_SHELL_INIT_PROC(void);
11044    SQLITE_SHELL_INIT_PROC();
11045  }
11046#else
11047  /* All the sqlite3_config() calls have now been made. So it is safe
11048  ** to call sqlite3_initialize() and process any command line -vfs option. */
11049  sqlite3_initialize();
11050#endif
11051
11052  if( zVfs ){
11053    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11054    if( pVfs ){
11055      sqlite3_vfs_register(pVfs, 1);
11056    }else{
11057      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11058      exit(1);
11059    }
11060  }
11061
11062  if( data.zDbFilename==0 ){
11063#ifndef SQLITE_OMIT_MEMORYDB
11064    data.zDbFilename = ":memory:";
11065    warnInmemoryDb = argc==1;
11066#else
11067    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11068    return 1;
11069#endif
11070  }
11071  data.out = stdout;
11072  sqlite3_appendvfs_init(0,0,0);
11073
11074  /* Go ahead and open the database file if it already exists.  If the
11075  ** file does not exist, delay opening it.  This prevents empty database
11076  ** files from being created if a user mistypes the database name argument
11077  ** to the sqlite command-line tool.
11078  */
11079  if( access(data.zDbFilename, 0)==0 ){
11080    open_db(&data, 0);
11081  }
11082
11083  /* Process the initialization file if there is one.  If no -init option
11084  ** is given on the command line, look for a file named ~/.sqliterc and
11085  ** try to process it.
11086  */
11087  process_sqliterc(&data,zInitFile);
11088
11089  /* Make a second pass through the command-line argument and set
11090  ** options.  This second pass is delayed until after the initialization
11091  ** file is processed so that the command-line arguments will override
11092  ** settings in the initialization file.
11093  */
11094  for(i=1; i<argc; i++){
11095    char *z = argv[i];
11096    if( z[0]!='-' ) continue;
11097    if( z[1]=='-' ){ z++; }
11098    if( strcmp(z,"-init")==0 ){
11099      i++;
11100    }else if( strcmp(z,"-html")==0 ){
11101      data.mode = MODE_Html;
11102    }else if( strcmp(z,"-list")==0 ){
11103      data.mode = MODE_List;
11104    }else if( strcmp(z,"-quote")==0 ){
11105      data.mode = MODE_Quote;
11106      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11107      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11108    }else if( strcmp(z,"-line")==0 ){
11109      data.mode = MODE_Line;
11110    }else if( strcmp(z,"-column")==0 ){
11111      data.mode = MODE_Column;
11112    }else if( strcmp(z,"-json")==0 ){
11113      data.mode = MODE_Json;
11114    }else if( strcmp(z,"-markdown")==0 ){
11115      data.mode = MODE_Markdown;
11116    }else if( strcmp(z,"-table")==0 ){
11117      data.mode = MODE_Table;
11118    }else if( strcmp(z,"-box")==0 ){
11119      data.mode = MODE_Box;
11120    }else if( strcmp(z,"-csv")==0 ){
11121      data.mode = MODE_Csv;
11122      memcpy(data.colSeparator,",",2);
11123#ifdef SQLITE_HAVE_ZLIB
11124    }else if( strcmp(z,"-zip")==0 ){
11125      data.openMode = SHELL_OPEN_ZIPFILE;
11126#endif
11127    }else if( strcmp(z,"-append")==0 ){
11128      data.openMode = SHELL_OPEN_APPENDVFS;
11129#ifdef SQLITE_ENABLE_DESERIALIZE
11130    }else if( strcmp(z,"-deserialize")==0 ){
11131      data.openMode = SHELL_OPEN_DESERIALIZE;
11132    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11133      data.szMax = integerValue(argv[++i]);
11134#endif
11135    }else if( strcmp(z,"-readonly")==0 ){
11136      data.openMode = SHELL_OPEN_READONLY;
11137    }else if( strcmp(z,"-nofollow")==0 ){
11138      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11139    }else if( strcmp(z,"-ascii")==0 ){
11140      data.mode = MODE_Ascii;
11141      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11142                       SEP_Unit);
11143      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11144                       SEP_Record);
11145    }else if( strcmp(z,"-separator")==0 ){
11146      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11147                       "%s",cmdline_option_value(argc,argv,++i));
11148    }else if( strcmp(z,"-newline")==0 ){
11149      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11150                       "%s",cmdline_option_value(argc,argv,++i));
11151    }else if( strcmp(z,"-nullvalue")==0 ){
11152      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11153                       "%s",cmdline_option_value(argc,argv,++i));
11154    }else if( strcmp(z,"-header")==0 ){
11155      data.showHeader = 1;
11156    }else if( strcmp(z,"-noheader")==0 ){
11157      data.showHeader = 0;
11158    }else if( strcmp(z,"-echo")==0 ){
11159      ShellSetFlag(&data, SHFLG_Echo);
11160    }else if( strcmp(z,"-eqp")==0 ){
11161      data.autoEQP = AUTOEQP_on;
11162    }else if( strcmp(z,"-eqpfull")==0 ){
11163      data.autoEQP = AUTOEQP_full;
11164    }else if( strcmp(z,"-stats")==0 ){
11165      data.statsOn = 1;
11166    }else if( strcmp(z,"-scanstats")==0 ){
11167      data.scanstatsOn = 1;
11168    }else if( strcmp(z,"-backslash")==0 ){
11169      /* Undocumented command-line option: -backslash
11170      ** Causes C-style backslash escapes to be evaluated in SQL statements
11171      ** prior to sending the SQL into SQLite.  Useful for injecting
11172      ** crazy bytes in the middle of SQL statements for testing and debugging.
11173      */
11174      ShellSetFlag(&data, SHFLG_Backslash);
11175    }else if( strcmp(z,"-bail")==0 ){
11176      bail_on_error = 1;
11177    }else if( strcmp(z,"-version")==0 ){
11178      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11179      return 0;
11180    }else if( strcmp(z,"-interactive")==0 ){
11181      stdin_is_interactive = 1;
11182    }else if( strcmp(z,"-batch")==0 ){
11183      stdin_is_interactive = 0;
11184    }else if( strcmp(z,"-heap")==0 ){
11185      i++;
11186    }else if( strcmp(z,"-pagecache")==0 ){
11187      i+=2;
11188    }else if( strcmp(z,"-lookaside")==0 ){
11189      i+=2;
11190    }else if( strcmp(z,"-mmap")==0 ){
11191      i++;
11192    }else if( strcmp(z,"-memtrace")==0 ){
11193      i++;
11194#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11195    }else if( strcmp(z,"-sorterref")==0 ){
11196      i++;
11197#endif
11198    }else if( strcmp(z,"-vfs")==0 ){
11199      i++;
11200#ifdef SQLITE_ENABLE_VFSTRACE
11201    }else if( strcmp(z,"-vfstrace")==0 ){
11202      i++;
11203#endif
11204#ifdef SQLITE_ENABLE_MULTIPLEX
11205    }else if( strcmp(z,"-multiplex")==0 ){
11206      i++;
11207#endif
11208    }else if( strcmp(z,"-help")==0 ){
11209      usage(1);
11210    }else if( strcmp(z,"-cmd")==0 ){
11211      /* Run commands that follow -cmd first and separately from commands
11212      ** that simply appear on the command-line.  This seems goofy.  It would
11213      ** be better if all commands ran in the order that they appear.  But
11214      ** we retain the goofy behavior for historical compatibility. */
11215      if( i==argc-1 ) break;
11216      z = cmdline_option_value(argc,argv,++i);
11217      if( z[0]=='.' ){
11218        rc = do_meta_command(z, &data);
11219        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11220      }else{
11221        open_db(&data, 0);
11222        rc = shell_exec(&data, z, &zErrMsg);
11223        if( zErrMsg!=0 ){
11224          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11225          if( bail_on_error ) return rc!=0 ? rc : 1;
11226        }else if( rc!=0 ){
11227          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11228          if( bail_on_error ) return rc;
11229        }
11230      }
11231#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11232    }else if( strncmp(z, "-A", 2)==0 ){
11233      if( nCmd>0 ){
11234        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11235                            " with \"%s\"\n", z);
11236        return 1;
11237      }
11238      open_db(&data, OPEN_DB_ZIPFILE);
11239      if( z[2] ){
11240        argv[i] = &z[2];
11241        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11242      }else{
11243        arDotCommand(&data, 1, argv+i, argc-i);
11244      }
11245      readStdin = 0;
11246      break;
11247#endif
11248    }else{
11249      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11250      raw_printf(stderr,"Use -help for a list of options.\n");
11251      return 1;
11252    }
11253    data.cMode = data.mode;
11254  }
11255
11256  if( !readStdin ){
11257    /* Run all arguments that do not begin with '-' as if they were separate
11258    ** command-line inputs, except for the argToSkip argument which contains
11259    ** the database filename.
11260    */
11261    for(i=0; i<nCmd; i++){
11262      if( azCmd[i][0]=='.' ){
11263        rc = do_meta_command(azCmd[i], &data);
11264        if( rc ) return rc==2 ? 0 : rc;
11265      }else{
11266        open_db(&data, 0);
11267        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11268        if( zErrMsg!=0 ){
11269          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11270          return rc!=0 ? rc : 1;
11271        }else if( rc!=0 ){
11272          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11273          return rc;
11274        }
11275      }
11276    }
11277    free(azCmd);
11278  }else{
11279    /* Run commands received from standard input
11280    */
11281    if( stdin_is_interactive ){
11282      char *zHome;
11283      char *zHistory;
11284      int nHistory;
11285      printf(
11286        "SQLite version %s %.19s\n" /*extra-version-info*/
11287        "Enter \".help\" for usage hints.\n",
11288        sqlite3_libversion(), sqlite3_sourceid()
11289      );
11290      if( warnInmemoryDb ){
11291        printf("Connected to a ");
11292        printBold("transient in-memory database");
11293        printf(".\nUse \".open FILENAME\" to reopen on a "
11294               "persistent database.\n");
11295      }
11296      zHistory = getenv("SQLITE_HISTORY");
11297      if( zHistory ){
11298        zHistory = strdup(zHistory);
11299      }else if( (zHome = find_home_dir(0))!=0 ){
11300        nHistory = strlen30(zHome) + 20;
11301        if( (zHistory = malloc(nHistory))!=0 ){
11302          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11303        }
11304      }
11305      if( zHistory ){ shell_read_history(zHistory); }
11306#if HAVE_READLINE || HAVE_EDITLINE
11307      rl_attempted_completion_function = readline_completion;
11308#elif HAVE_LINENOISE
11309      linenoiseSetCompletionCallback(linenoise_completion);
11310#endif
11311      data.in = 0;
11312      rc = process_input(&data);
11313      if( zHistory ){
11314        shell_stifle_history(2000);
11315        shell_write_history(zHistory);
11316        free(zHistory);
11317      }
11318    }else{
11319      data.in = stdin;
11320      rc = process_input(&data);
11321    }
11322  }
11323  set_table_name(&data, 0);
11324  if( data.db ){
11325    session_close_all(&data);
11326    close_db(data.db);
11327  }
11328  sqlite3_free(data.zFreeOnClose);
11329  find_home_dir(1);
11330  output_reset(&data);
11331  data.doXdgOpen = 0;
11332  clearTempFile(&data);
11333#if !SQLITE_SHELL_IS_UTF8
11334  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11335  free(argvToFree);
11336#endif
11337  free(data.colWidth);
11338  /* Clear the global data structure so that valgrind will detect memory
11339  ** leaks */
11340  memset(&data, 0, sizeof(data));
11341  return rc;
11342}
11343