xref: /sqlite-3.40.0/src/shell.c.in (revision b0906b60)
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** Optionally #include a user-defined header, whereby compilation options
22** may be set prior to where they take effect, but after platform setup.
23** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
24** file. Note that this macro has a like effect on sqlite3.c compilation.
25*/
26#ifdef SQLITE_CUSTOM_INCLUDE
27# define INC_STRINGIFY_(f) #f
28# define INC_STRINGIFY(f) INC_STRINGIFY_(f)
29# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
30#endif
31
32/*
33** Determine if we are dealing with WinRT, which provides only a subset of
34** the full Win32 API.
35*/
36#if !defined(SQLITE_OS_WINRT)
37# define SQLITE_OS_WINRT 0
38#endif
39
40/*
41** Warning pragmas copied from msvc.h in the core.
42*/
43#if defined(_MSC_VER)
44#pragma warning(disable : 4054)
45#pragma warning(disable : 4055)
46#pragma warning(disable : 4100)
47#pragma warning(disable : 4127)
48#pragma warning(disable : 4130)
49#pragma warning(disable : 4152)
50#pragma warning(disable : 4189)
51#pragma warning(disable : 4206)
52#pragma warning(disable : 4210)
53#pragma warning(disable : 4232)
54#pragma warning(disable : 4244)
55#pragma warning(disable : 4305)
56#pragma warning(disable : 4306)
57#pragma warning(disable : 4702)
58#pragma warning(disable : 4706)
59#endif /* defined(_MSC_VER) */
60
61/*
62** No support for loadable extensions in VxWorks.
63*/
64#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
65# define SQLITE_OMIT_LOAD_EXTENSION 1
66#endif
67
68/*
69** Enable large-file support for fopen() and friends on unix.
70*/
71#ifndef SQLITE_DISABLE_LFS
72# define _LARGE_FILE       1
73# ifndef _FILE_OFFSET_BITS
74#   define _FILE_OFFSET_BITS 64
75# endif
76# define _LARGEFILE_SOURCE 1
77#endif
78
79#include <stdlib.h>
80#include <string.h>
81#include <stdio.h>
82#include <assert.h>
83#include "sqlite3.h"
84typedef sqlite3_int64 i64;
85typedef sqlite3_uint64 u64;
86typedef unsigned char u8;
87#if SQLITE_USER_AUTHENTICATION
88# include "sqlite3userauth.h"
89#endif
90#include <ctype.h>
91#include <stdarg.h>
92
93#if !defined(_WIN32) && !defined(WIN32)
94# include <signal.h>
95# if !defined(__RTP__) && !defined(_WRS_KERNEL)
96#  include <pwd.h>
97# endif
98#endif
99#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
100# include <unistd.h>
101# include <dirent.h>
102# define GETPID getpid
103# if defined(__MINGW32__)
104#  define DIRENT dirent
105#  ifndef S_ISLNK
106#   define S_ISLNK(mode) (0)
107#  endif
108# endif
109#else
110# define GETPID (int)GetCurrentProcessId
111#endif
112#include <sys/types.h>
113#include <sys/stat.h>
114
115#if HAVE_READLINE
116# include <readline/readline.h>
117# include <readline/history.h>
118#endif
119
120#if HAVE_EDITLINE
121# include <editline/readline.h>
122#endif
123
124#if HAVE_EDITLINE || HAVE_READLINE
125
126# define shell_add_history(X) add_history(X)
127# define shell_read_history(X) read_history(X)
128# define shell_write_history(X) write_history(X)
129# define shell_stifle_history(X) stifle_history(X)
130# define shell_readline(X) readline(X)
131
132#elif HAVE_LINENOISE
133
134# include "linenoise.h"
135# define shell_add_history(X) linenoiseHistoryAdd(X)
136# define shell_read_history(X) linenoiseHistoryLoad(X)
137# define shell_write_history(X) linenoiseHistorySave(X)
138# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
139# define shell_readline(X) linenoise(X)
140
141#else
142
143# define shell_read_history(X)
144# define shell_write_history(X)
145# define shell_stifle_history(X)
146
147# define SHELL_USE_LOCAL_GETLINE 1
148#endif
149
150
151#if defined(_WIN32) || defined(WIN32)
152# if SQLITE_OS_WINRT
153#  define SQLITE_OMIT_POPEN 1
154# else
155#  include <io.h>
156#  include <fcntl.h>
157#  define isatty(h) _isatty(h)
158#  ifndef access
159#   define access(f,m) _access((f),(m))
160#  endif
161#  ifndef unlink
162#   define unlink _unlink
163#  endif
164#  ifndef strdup
165#   define strdup _strdup
166#  endif
167#  undef popen
168#  define popen _popen
169#  undef pclose
170#  define pclose _pclose
171# endif
172#else
173 /* Make sure isatty() has a prototype. */
174 extern int isatty(int);
175
176# if !defined(__RTP__) && !defined(_WRS_KERNEL)
177  /* popen and pclose are not C89 functions and so are
178  ** sometimes omitted from the <stdio.h> header */
179   extern FILE *popen(const char*,const char*);
180   extern int pclose(FILE*);
181# else
182#  define SQLITE_OMIT_POPEN 1
183# endif
184#endif
185
186#if defined(_WIN32_WCE)
187/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
188 * thus we always assume that we have a console. That can be
189 * overridden with the -batch command line option.
190 */
191#define isatty(x) 1
192#endif
193
194/* ctype macros that work with signed characters */
195#define IsSpace(X)  isspace((unsigned char)X)
196#define IsDigit(X)  isdigit((unsigned char)X)
197#define ToLower(X)  (char)tolower((unsigned char)X)
198
199#if defined(_WIN32) || defined(WIN32)
200#if SQLITE_OS_WINRT
201#include <intrin.h>
202#endif
203#include <windows.h>
204
205/* string conversion routines only needed on Win32 */
206extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
207extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
208extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
209extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
210#endif
211
212/* On Windows, we normally run with output mode of TEXT so that \n characters
213** are automatically translated into \r\n.  However, this behavior needs
214** to be disabled in some cases (ex: when generating CSV output and when
215** rendering quoted strings that contain \n characters).  The following
216** routines take care of that.
217*/
218#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
219static void setBinaryMode(FILE *file, int isOutput){
220  if( isOutput ) fflush(file);
221  _setmode(_fileno(file), _O_BINARY);
222}
223static void setTextMode(FILE *file, int isOutput){
224  if( isOutput ) fflush(file);
225  _setmode(_fileno(file), _O_TEXT);
226}
227#else
228# define setBinaryMode(X,Y)
229# define setTextMode(X,Y)
230#endif
231
232
233/* True if the timer is enabled */
234static int enableTimer = 0;
235
236/* Return the current wall-clock time */
237static sqlite3_int64 timeOfDay(void){
238  static sqlite3_vfs *clockVfs = 0;
239  sqlite3_int64 t;
240  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
241  if( clockVfs==0 ) return 0;  /* Never actually happens */
242  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
243    clockVfs->xCurrentTimeInt64(clockVfs, &t);
244  }else{
245    double r;
246    clockVfs->xCurrentTime(clockVfs, &r);
247    t = (sqlite3_int64)(r*86400000.0);
248  }
249  return t;
250}
251
252#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
253#include <sys/time.h>
254#include <sys/resource.h>
255
256/* VxWorks does not support getrusage() as far as we can determine */
257#if defined(_WRS_KERNEL) || defined(__RTP__)
258struct rusage {
259  struct timeval ru_utime; /* user CPU time used */
260  struct timeval ru_stime; /* system CPU time used */
261};
262#define getrusage(A,B) memset(B,0,sizeof(*B))
263#endif
264
265/* Saved resource information for the beginning of an operation */
266static struct rusage sBegin;  /* CPU time at start */
267static sqlite3_int64 iBegin;  /* Wall-clock time at start */
268
269/*
270** Begin timing an operation
271*/
272static void beginTimer(void){
273  if( enableTimer ){
274    getrusage(RUSAGE_SELF, &sBegin);
275    iBegin = timeOfDay();
276  }
277}
278
279/* Return the difference of two time_structs in seconds */
280static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
281  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
282         (double)(pEnd->tv_sec - pStart->tv_sec);
283}
284
285/*
286** Print the timing results.
287*/
288static void endTimer(void){
289  if( enableTimer ){
290    sqlite3_int64 iEnd = timeOfDay();
291    struct rusage sEnd;
292    getrusage(RUSAGE_SELF, &sEnd);
293    printf("Run Time: real %.3f user %f sys %f\n",
294       (iEnd - iBegin)*0.001,
295       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
296       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
297  }
298}
299
300#define BEGIN_TIMER beginTimer()
301#define END_TIMER endTimer()
302#define HAS_TIMER 1
303
304#elif (defined(_WIN32) || defined(WIN32))
305
306/* Saved resource information for the beginning of an operation */
307static HANDLE hProcess;
308static FILETIME ftKernelBegin;
309static FILETIME ftUserBegin;
310static sqlite3_int64 ftWallBegin;
311typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
312                                    LPFILETIME, LPFILETIME);
313static GETPROCTIMES getProcessTimesAddr = NULL;
314
315/*
316** Check to see if we have timer support.  Return 1 if necessary
317** support found (or found previously).
318*/
319static int hasTimer(void){
320  if( getProcessTimesAddr ){
321    return 1;
322  } else {
323#if !SQLITE_OS_WINRT
324    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
325    ** versions. See if the version we are running on has it, and if it
326    ** does, save off a pointer to it and the current process handle.
327    */
328    hProcess = GetCurrentProcess();
329    if( hProcess ){
330      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
331      if( NULL != hinstLib ){
332        getProcessTimesAddr =
333            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
334        if( NULL != getProcessTimesAddr ){
335          return 1;
336        }
337        FreeLibrary(hinstLib);
338      }
339    }
340#endif
341  }
342  return 0;
343}
344
345/*
346** Begin timing an operation
347*/
348static void beginTimer(void){
349  if( enableTimer && getProcessTimesAddr ){
350    FILETIME ftCreation, ftExit;
351    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
352                        &ftKernelBegin,&ftUserBegin);
353    ftWallBegin = timeOfDay();
354  }
355}
356
357/* Return the difference of two FILETIME structs in seconds */
358static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
359  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
360  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
361  return (double) ((i64End - i64Start) / 10000000.0);
362}
363
364/*
365** Print the timing results.
366*/
367static void endTimer(void){
368  if( enableTimer && getProcessTimesAddr){
369    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
370    sqlite3_int64 ftWallEnd = timeOfDay();
371    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
372    printf("Run Time: real %.3f user %f sys %f\n",
373       (ftWallEnd - ftWallBegin)*0.001,
374       timeDiff(&ftUserBegin, &ftUserEnd),
375       timeDiff(&ftKernelBegin, &ftKernelEnd));
376  }
377}
378
379#define BEGIN_TIMER beginTimer()
380#define END_TIMER endTimer()
381#define HAS_TIMER hasTimer()
382
383#else
384#define BEGIN_TIMER
385#define END_TIMER
386#define HAS_TIMER 0
387#endif
388
389/*
390** Used to prevent warnings about unused parameters
391*/
392#define UNUSED_PARAMETER(x) (void)(x)
393
394/*
395** Number of elements in an array
396*/
397#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
398
399/*
400** If the following flag is set, then command execution stops
401** at an error if we are not interactive.
402*/
403static int bail_on_error = 0;
404
405/*
406** Threat stdin as an interactive input if the following variable
407** is true.  Otherwise, assume stdin is connected to a file or pipe.
408*/
409static int stdin_is_interactive = 1;
410
411/*
412** On Windows systems we have to know if standard output is a console
413** in order to translate UTF-8 into MBCS.  The following variable is
414** true if translation is required.
415*/
416static int stdout_is_console = 1;
417
418/*
419** The following is the open SQLite database.  We make a pointer
420** to this database a static variable so that it can be accessed
421** by the SIGINT handler to interrupt database processing.
422*/
423static sqlite3 *globalDb = 0;
424
425/*
426** True if an interrupt (Control-C) has been received.
427*/
428static volatile int seenInterrupt = 0;
429
430#ifdef SQLITE_DEBUG
431/*
432** Out-of-memory simulator variables
433*/
434static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
435static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
436static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
437#endif /* SQLITE_DEBUG */
438
439/*
440** This is the name of our program. It is set in main(), used
441** in a number of other places, mostly for error messages.
442*/
443static char *Argv0;
444
445/*
446** Prompt strings. Initialized in main. Settable with
447**   .prompt main continue
448*/
449static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
450static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
451
452/*
453** Render output like fprintf().  Except, if the output is going to the
454** console and if this is running on a Windows machine, translate the
455** output from UTF-8 into MBCS.
456*/
457#if defined(_WIN32) || defined(WIN32)
458void utf8_printf(FILE *out, const char *zFormat, ...){
459  va_list ap;
460  va_start(ap, zFormat);
461  if( stdout_is_console && (out==stdout || out==stderr) ){
462    char *z1 = sqlite3_vmprintf(zFormat, ap);
463    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
464    sqlite3_free(z1);
465    fputs(z2, out);
466    sqlite3_free(z2);
467  }else{
468    vfprintf(out, zFormat, ap);
469  }
470  va_end(ap);
471}
472#elif !defined(utf8_printf)
473# define utf8_printf fprintf
474#endif
475
476/*
477** Render output like fprintf().  This should not be used on anything that
478** includes string formatting (e.g. "%s").
479*/
480#if !defined(raw_printf)
481# define raw_printf fprintf
482#endif
483
484/* Indicate out-of-memory and exit. */
485static void shell_out_of_memory(void){
486  raw_printf(stderr,"Error: out of memory\n");
487  exit(1);
488}
489
490#ifdef SQLITE_DEBUG
491/* This routine is called when a simulated OOM occurs.  It is broken
492** out as a separate routine to make it easy to set a breakpoint on
493** the OOM
494*/
495void shellOomFault(void){
496  if( oomRepeat>0 ){
497    oomRepeat--;
498  }else{
499    oomCounter--;
500  }
501}
502#endif /* SQLITE_DEBUG */
503
504#ifdef SQLITE_DEBUG
505/* This routine is a replacement malloc() that is used to simulate
506** Out-Of-Memory (OOM) errors for testing purposes.
507*/
508static void *oomMalloc(int nByte){
509  if( oomCounter ){
510    if( oomCounter==1 ){
511      shellOomFault();
512      return 0;
513    }else{
514      oomCounter--;
515    }
516  }
517  return defaultMalloc(nByte);
518}
519#endif /* SQLITE_DEBUG */
520
521#ifdef SQLITE_DEBUG
522/* Register the OOM simulator.  This must occur before any memory
523** allocations */
524static void registerOomSimulator(void){
525  sqlite3_mem_methods mem;
526  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
527  defaultMalloc = mem.xMalloc;
528  mem.xMalloc = oomMalloc;
529  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
530}
531#endif
532
533/*
534** Write I/O traces to the following stream.
535*/
536#ifdef SQLITE_ENABLE_IOTRACE
537static FILE *iotrace = 0;
538#endif
539
540/*
541** This routine works like printf in that its first argument is a
542** format string and subsequent arguments are values to be substituted
543** in place of % fields.  The result of formatting this string
544** is written to iotrace.
545*/
546#ifdef SQLITE_ENABLE_IOTRACE
547static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
548  va_list ap;
549  char *z;
550  if( iotrace==0 ) return;
551  va_start(ap, zFormat);
552  z = sqlite3_vmprintf(zFormat, ap);
553  va_end(ap);
554  utf8_printf(iotrace, "%s", z);
555  sqlite3_free(z);
556}
557#endif
558
559/*
560** Output string zUtf to stream pOut as w characters.  If w is negative,
561** then right-justify the text.  W is the width in UTF-8 characters, not
562** in bytes.  This is different from the %*.*s specification in printf
563** since with %*.*s the width is measured in bytes, not characters.
564*/
565static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
566  int i;
567  int n;
568  int aw = w<0 ? -w : w;
569  for(i=n=0; zUtf[i]; i++){
570    if( (zUtf[i]&0xc0)!=0x80 ){
571      n++;
572      if( n==aw ){
573        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
574        break;
575      }
576    }
577  }
578  if( n>=aw ){
579    utf8_printf(pOut, "%.*s", i, zUtf);
580  }else if( w<0 ){
581    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
582  }else{
583    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
584  }
585}
586
587
588/*
589** Determines if a string is a number of not.
590*/
591static int isNumber(const char *z, int *realnum){
592  if( *z=='-' || *z=='+' ) z++;
593  if( !IsDigit(*z) ){
594    return 0;
595  }
596  z++;
597  if( realnum ) *realnum = 0;
598  while( IsDigit(*z) ){ z++; }
599  if( *z=='.' ){
600    z++;
601    if( !IsDigit(*z) ) return 0;
602    while( IsDigit(*z) ){ z++; }
603    if( realnum ) *realnum = 1;
604  }
605  if( *z=='e' || *z=='E' ){
606    z++;
607    if( *z=='+' || *z=='-' ) z++;
608    if( !IsDigit(*z) ) return 0;
609    while( IsDigit(*z) ){ z++; }
610    if( realnum ) *realnum = 1;
611  }
612  return *z==0;
613}
614
615/*
616** Compute a string length that is limited to what can be stored in
617** lower 30 bits of a 32-bit signed integer.
618*/
619static int strlen30(const char *z){
620  const char *z2 = z;
621  while( *z2 ){ z2++; }
622  return 0x3fffffff & (int)(z2 - z);
623}
624
625/*
626** Return the length of a string in characters.  Multibyte UTF8 characters
627** count as a single character.
628*/
629static int strlenChar(const char *z){
630  int n = 0;
631  while( *z ){
632    if( (0xc0&*(z++))!=0x80 ) n++;
633  }
634  return n;
635}
636
637/*
638** Return open FILE * if zFile exists, can be opened for read
639** and is an ordinary file or a character stream source.
640** Otherwise return 0.
641*/
642static FILE * openChrSource(const char *zFile){
643#ifdef _WIN32
644  struct _stat x = {0};
645# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
646  /* On Windows, open first, then check the stream nature. This order
647  ** is necessary because _stat() and sibs, when checking a named pipe,
648  ** effectively break the pipe as its supplier sees it. */
649  FILE *rv = fopen(zFile, "rb");
650  if( rv==0 ) return 0;
651  if( _fstat(_fileno(rv), &x) != 0
652      || !STAT_CHR_SRC(x.st_mode)){
653    fclose(rv);
654    rv = 0;
655  }
656  return rv;
657#else
658  struct stat x = {0};
659  int rc = stat(zFile, &x);
660# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
661  if( rc!=0 ) return 0;
662  if( STAT_CHR_SRC(x.st_mode) ){
663    return fopen(zFile, "rb");
664  }else{
665    return 0;
666  }
667#endif
668#undef STAT_CHR_SRC
669}
670
671/*
672** This routine reads a line of text from FILE in, stores
673** the text in memory obtained from malloc() and returns a pointer
674** to the text.  NULL is returned at end of file, or if malloc()
675** fails.
676**
677** If zLine is not NULL then it is a malloced buffer returned from
678** a previous call to this routine that may be reused.
679*/
680static char *local_getline(char *zLine, FILE *in){
681  int nLine = zLine==0 ? 0 : 100;
682  int n = 0;
683
684  while( 1 ){
685    if( n+100>nLine ){
686      nLine = nLine*2 + 100;
687      zLine = realloc(zLine, nLine);
688      if( zLine==0 ) shell_out_of_memory();
689    }
690    if( fgets(&zLine[n], nLine - n, in)==0 ){
691      if( n==0 ){
692        free(zLine);
693        return 0;
694      }
695      zLine[n] = 0;
696      break;
697    }
698    while( zLine[n] ) n++;
699    if( n>0 && zLine[n-1]=='\n' ){
700      n--;
701      if( n>0 && zLine[n-1]=='\r' ) n--;
702      zLine[n] = 0;
703      break;
704    }
705  }
706#if defined(_WIN32) || defined(WIN32)
707  /* For interactive input on Windows systems, translate the
708  ** multi-byte characterset characters into UTF-8. */
709  if( stdin_is_interactive && in==stdin ){
710    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
711    if( zTrans ){
712      int nTrans = strlen30(zTrans)+1;
713      if( nTrans>nLine ){
714        zLine = realloc(zLine, nTrans);
715        if( zLine==0 ) shell_out_of_memory();
716      }
717      memcpy(zLine, zTrans, nTrans);
718      sqlite3_free(zTrans);
719    }
720  }
721#endif /* defined(_WIN32) || defined(WIN32) */
722  return zLine;
723}
724
725/*
726** Retrieve a single line of input text.
727**
728** If in==0 then read from standard input and prompt before each line.
729** If isContinuation is true, then a continuation prompt is appropriate.
730** If isContinuation is zero, then the main prompt should be used.
731**
732** If zPrior is not NULL then it is a buffer from a prior call to this
733** routine that can be reused.
734**
735** The result is stored in space obtained from malloc() and must either
736** be freed by the caller or else passed back into this routine via the
737** zPrior argument for reuse.
738*/
739static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
740  char *zPrompt;
741  char *zResult;
742  if( in!=0 ){
743    zResult = local_getline(zPrior, in);
744  }else{
745    zPrompt = isContinuation ? continuePrompt : mainPrompt;
746#if SHELL_USE_LOCAL_GETLINE
747    printf("%s", zPrompt);
748    fflush(stdout);
749    zResult = local_getline(zPrior, stdin);
750#else
751    free(zPrior);
752    zResult = shell_readline(zPrompt);
753    if( zResult && *zResult ) shell_add_history(zResult);
754#endif
755  }
756  return zResult;
757}
758
759
760/*
761** Return the value of a hexadecimal digit.  Return -1 if the input
762** is not a hex digit.
763*/
764static int hexDigitValue(char c){
765  if( c>='0' && c<='9' ) return c - '0';
766  if( c>='a' && c<='f' ) return c - 'a' + 10;
767  if( c>='A' && c<='F' ) return c - 'A' + 10;
768  return -1;
769}
770
771/*
772** Interpret zArg as an integer value, possibly with suffixes.
773*/
774static sqlite3_int64 integerValue(const char *zArg){
775  sqlite3_int64 v = 0;
776  static const struct { char *zSuffix; int iMult; } aMult[] = {
777    { "KiB", 1024 },
778    { "MiB", 1024*1024 },
779    { "GiB", 1024*1024*1024 },
780    { "KB",  1000 },
781    { "MB",  1000000 },
782    { "GB",  1000000000 },
783    { "K",   1000 },
784    { "M",   1000000 },
785    { "G",   1000000000 },
786  };
787  int i;
788  int isNeg = 0;
789  if( zArg[0]=='-' ){
790    isNeg = 1;
791    zArg++;
792  }else if( zArg[0]=='+' ){
793    zArg++;
794  }
795  if( zArg[0]=='0' && zArg[1]=='x' ){
796    int x;
797    zArg += 2;
798    while( (x = hexDigitValue(zArg[0]))>=0 ){
799      v = (v<<4) + x;
800      zArg++;
801    }
802  }else{
803    while( IsDigit(zArg[0]) ){
804      v = v*10 + zArg[0] - '0';
805      zArg++;
806    }
807  }
808  for(i=0; i<ArraySize(aMult); i++){
809    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
810      v *= aMult[i].iMult;
811      break;
812    }
813  }
814  return isNeg? -v : v;
815}
816
817/*
818** A variable length string to which one can append text.
819*/
820typedef struct ShellText ShellText;
821struct ShellText {
822  char *z;
823  int n;
824  int nAlloc;
825};
826
827/*
828** Initialize and destroy a ShellText object
829*/
830static void initText(ShellText *p){
831  memset(p, 0, sizeof(*p));
832}
833static void freeText(ShellText *p){
834  free(p->z);
835  initText(p);
836}
837
838/* zIn is either a pointer to a NULL-terminated string in memory obtained
839** from malloc(), or a NULL pointer. The string pointed to by zAppend is
840** added to zIn, and the result returned in memory obtained from malloc().
841** zIn, if it was not NULL, is freed.
842**
843** If the third argument, quote, is not '\0', then it is used as a
844** quote character for zAppend.
845*/
846static void appendText(ShellText *p, char const *zAppend, char quote){
847  int len;
848  int i;
849  int nAppend = strlen30(zAppend);
850
851  len = nAppend+p->n+1;
852  if( quote ){
853    len += 2;
854    for(i=0; i<nAppend; i++){
855      if( zAppend[i]==quote ) len++;
856    }
857  }
858
859  if( p->z==0 || p->n+len>=p->nAlloc ){
860    p->nAlloc = p->nAlloc*2 + len + 20;
861    p->z = realloc(p->z, p->nAlloc);
862    if( p->z==0 ) shell_out_of_memory();
863  }
864
865  if( quote ){
866    char *zCsr = p->z+p->n;
867    *zCsr++ = quote;
868    for(i=0; i<nAppend; i++){
869      *zCsr++ = zAppend[i];
870      if( zAppend[i]==quote ) *zCsr++ = quote;
871    }
872    *zCsr++ = quote;
873    p->n = (int)(zCsr - p->z);
874    *zCsr = '\0';
875  }else{
876    memcpy(p->z+p->n, zAppend, nAppend);
877    p->n += nAppend;
878    p->z[p->n] = '\0';
879  }
880}
881
882/*
883** Attempt to determine if identifier zName needs to be quoted, either
884** because it contains non-alphanumeric characters, or because it is an
885** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
886** that quoting is required.
887**
888** Return '"' if quoting is required.  Return 0 if no quoting is required.
889*/
890static char quoteChar(const char *zName){
891  int i;
892  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
893  for(i=0; zName[i]; i++){
894    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
895  }
896  return sqlite3_keyword_check(zName, i) ? '"' : 0;
897}
898
899/*
900** Construct a fake object name and column list to describe the structure
901** of the view, virtual table, or table valued function zSchema.zName.
902*/
903static char *shellFakeSchema(
904  sqlite3 *db,            /* The database connection containing the vtab */
905  const char *zSchema,    /* Schema of the database holding the vtab */
906  const char *zName       /* The name of the virtual table */
907){
908  sqlite3_stmt *pStmt = 0;
909  char *zSql;
910  ShellText s;
911  char cQuote;
912  char *zDiv = "(";
913  int nRow = 0;
914
915  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
916                         zSchema ? zSchema : "main", zName);
917  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
918  sqlite3_free(zSql);
919  initText(&s);
920  if( zSchema ){
921    cQuote = quoteChar(zSchema);
922    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
923    appendText(&s, zSchema, cQuote);
924    appendText(&s, ".", 0);
925  }
926  cQuote = quoteChar(zName);
927  appendText(&s, zName, cQuote);
928  while( sqlite3_step(pStmt)==SQLITE_ROW ){
929    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
930    nRow++;
931    appendText(&s, zDiv, 0);
932    zDiv = ",";
933    cQuote = quoteChar(zCol);
934    appendText(&s, zCol, cQuote);
935  }
936  appendText(&s, ")", 0);
937  sqlite3_finalize(pStmt);
938  if( nRow==0 ){
939    freeText(&s);
940    s.z = 0;
941  }
942  return s.z;
943}
944
945/*
946** SQL function:  shell_module_schema(X)
947**
948** Return a fake schema for the table-valued function or eponymous virtual
949** table X.
950*/
951static void shellModuleSchema(
952  sqlite3_context *pCtx,
953  int nVal,
954  sqlite3_value **apVal
955){
956  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
957  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
958  UNUSED_PARAMETER(nVal);
959  if( zFake ){
960    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
961                        -1, sqlite3_free);
962    free(zFake);
963  }
964}
965
966/*
967** SQL function:  shell_add_schema(S,X)
968**
969** Add the schema name X to the CREATE statement in S and return the result.
970** Examples:
971**
972**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
973**
974** Also works on
975**
976**    CREATE INDEX
977**    CREATE UNIQUE INDEX
978**    CREATE VIEW
979**    CREATE TRIGGER
980**    CREATE VIRTUAL TABLE
981**
982** This UDF is used by the .schema command to insert the schema name of
983** attached databases into the middle of the sqlite_schema.sql field.
984*/
985static void shellAddSchemaName(
986  sqlite3_context *pCtx,
987  int nVal,
988  sqlite3_value **apVal
989){
990  static const char *aPrefix[] = {
991     "TABLE",
992     "INDEX",
993     "UNIQUE INDEX",
994     "VIEW",
995     "TRIGGER",
996     "VIRTUAL TABLE"
997  };
998  int i = 0;
999  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
1000  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
1001  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
1002  sqlite3 *db = sqlite3_context_db_handle(pCtx);
1003  UNUSED_PARAMETER(nVal);
1004  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
1005    for(i=0; i<ArraySize(aPrefix); i++){
1006      int n = strlen30(aPrefix[i]);
1007      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1008        char *z = 0;
1009        char *zFake = 0;
1010        if( zSchema ){
1011          char cQuote = quoteChar(zSchema);
1012          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1013            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1014          }else{
1015            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1016          }
1017        }
1018        if( zName
1019         && aPrefix[i][0]=='V'
1020         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1021        ){
1022          if( z==0 ){
1023            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1024          }else{
1025            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1026          }
1027          free(zFake);
1028        }
1029        if( z ){
1030          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1031          return;
1032        }
1033      }
1034    }
1035  }
1036  sqlite3_result_value(pCtx, apVal[0]);
1037}
1038
1039/*
1040** The source code for several run-time loadable extensions is inserted
1041** below by the ../tool/mkshellc.tcl script.  Before processing that included
1042** code, we need to override some macros to make the included program code
1043** work here in the middle of this regular program.
1044*/
1045#define SQLITE_EXTENSION_INIT1
1046#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1047
1048#if defined(_WIN32) && defined(_MSC_VER)
1049INCLUDE test_windirent.h
1050INCLUDE test_windirent.c
1051#define dirent DIRENT
1052#endif
1053INCLUDE ../ext/misc/shathree.c
1054INCLUDE ../ext/misc/fileio.c
1055INCLUDE ../ext/misc/completion.c
1056INCLUDE ../ext/misc/appendvfs.c
1057INCLUDE ../ext/misc/memtrace.c
1058INCLUDE ../ext/misc/uint.c
1059INCLUDE ../ext/misc/decimal.c
1060INCLUDE ../ext/misc/ieee754.c
1061INCLUDE ../ext/misc/series.c
1062INCLUDE ../ext/misc/regexp.c
1063#ifdef SQLITE_HAVE_ZLIB
1064INCLUDE ../ext/misc/zipfile.c
1065INCLUDE ../ext/misc/sqlar.c
1066#endif
1067INCLUDE ../ext/expert/sqlite3expert.h
1068INCLUDE ../ext/expert/sqlite3expert.c
1069
1070#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1071INCLUDE ../ext/misc/dbdata.c
1072#endif
1073
1074#if defined(SQLITE_ENABLE_SESSION)
1075/*
1076** State information for a single open session
1077*/
1078typedef struct OpenSession OpenSession;
1079struct OpenSession {
1080  char *zName;             /* Symbolic name for this session */
1081  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1082  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1083  sqlite3_session *p;      /* The open session */
1084};
1085#endif
1086
1087typedef struct ExpertInfo ExpertInfo;
1088struct ExpertInfo {
1089  sqlite3expert *pExpert;
1090  int bVerbose;
1091};
1092
1093/* A single line in the EQP output */
1094typedef struct EQPGraphRow EQPGraphRow;
1095struct EQPGraphRow {
1096  int iEqpId;           /* ID for this row */
1097  int iParentId;        /* ID of the parent row */
1098  EQPGraphRow *pNext;   /* Next row in sequence */
1099  char zText[1];        /* Text to display for this row */
1100};
1101
1102/* All EQP output is collected into an instance of the following */
1103typedef struct EQPGraph EQPGraph;
1104struct EQPGraph {
1105  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1106  EQPGraphRow *pLast;   /* Last element of the pRow list */
1107  char zPrefix[100];    /* Graph prefix */
1108};
1109
1110/*
1111** State information about the database connection is contained in an
1112** instance of the following structure.
1113*/
1114typedef struct ShellState ShellState;
1115struct ShellState {
1116  sqlite3 *db;           /* The database */
1117  u8 autoExplain;        /* Automatically turn on .explain mode */
1118  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1119  u8 autoEQPtest;        /* autoEQP is in test mode */
1120  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1121  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1122  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1123  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1124  u8 nEqpLevel;          /* Depth of the EQP output graph */
1125  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1126  u8 bSafeMode;          /* True to prohibit unsafe operations */
1127  u8 bSafeModePersist;   /* The long-term value of bSafeMode */
1128  unsigned statsOn;      /* True to display memory stats before each finalize */
1129  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1130  int outCount;          /* Revert to stdout when reaching zero */
1131  int cnt;               /* Number of records displayed so far */
1132  int lineno;            /* Line number of last line read from in */
1133  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1134  FILE *in;              /* Read commands from this stream */
1135  FILE *out;             /* Write results here */
1136  FILE *traceOut;        /* Output for sqlite3_trace() */
1137  int nErr;              /* Number of errors seen */
1138  int mode;              /* An output mode setting */
1139  int modePrior;         /* Saved mode */
1140  int cMode;             /* temporary output mode for the current query */
1141  int normalMode;        /* Output mode before ".explain on" */
1142  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1143  int showHeader;        /* True to show column names in List or Column mode */
1144  int nCheck;            /* Number of ".check" commands run */
1145  unsigned nProgress;    /* Number of progress callbacks encountered */
1146  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1147  unsigned flgProgress;  /* Flags for the progress callback */
1148  unsigned shellFlgs;    /* Various flags */
1149  unsigned priorShFlgs;  /* Saved copy of flags */
1150  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1151  char *zDestTable;      /* Name of destination table when MODE_Insert */
1152  char *zTempFile;       /* Temporary file that might need deleting */
1153  char zTestcase[30];    /* Name of current test case */
1154  char colSeparator[20]; /* Column separator character for several modes */
1155  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1156  char colSepPrior[20];  /* Saved column separator */
1157  char rowSepPrior[20];  /* Saved row separator */
1158  int *colWidth;         /* Requested width of each column in columnar modes */
1159  int *actualWidth;      /* Actual width of each column */
1160  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1161  char nullValue[20];    /* The text to print when a NULL comes back from
1162                         ** the database */
1163  char outfile[FILENAME_MAX]; /* Filename for *out */
1164  sqlite3_stmt *pStmt;   /* Current statement if any. */
1165  FILE *pLog;            /* Write log output here */
1166  struct AuxDb {         /* Storage space for auxiliary database connections */
1167    sqlite3 *db;               /* Connection pointer */
1168    const char *zDbFilename;   /* Filename used to open the connection */
1169    char *zFreeOnClose;        /* Free this memory allocation on close */
1170#if defined(SQLITE_ENABLE_SESSION)
1171    int nSession;              /* Number of active sessions */
1172    OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
1173#endif
1174  } aAuxDb[5],           /* Array of all database connections */
1175    *pAuxDb;             /* Currently active database connection */
1176  int *aiIndent;         /* Array of indents used in MODE_Explain */
1177  int nIndent;           /* Size of array aiIndent[] */
1178  int iIndent;           /* Index of current op in aiIndent[] */
1179  char *zNonce;          /* Nonce for temporary safe-mode excapes */
1180  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1181  ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
1182};
1183
1184
1185/* Allowed values for ShellState.autoEQP
1186*/
1187#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1188#define AUTOEQP_on       1           /* Automatic EQP is on */
1189#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1190#define AUTOEQP_full     3           /* Show full EXPLAIN */
1191
1192/* Allowed values for ShellState.openMode
1193*/
1194#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1195#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1196#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1197#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1198#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1199#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1200#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1201
1202/* Allowed values for ShellState.eTraceType
1203*/
1204#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1205#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1206#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1207
1208/* Bits in the ShellState.flgProgress variable */
1209#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1210#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1211                                   ** callback limit is reached, and for each
1212                                   ** top-level SQL statement */
1213#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1214
1215/*
1216** These are the allowed shellFlgs values
1217*/
1218#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1219#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1220#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1221#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1222#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1223#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1224#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1225#define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
1226#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1227#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1228
1229/*
1230** Macros for testing and setting shellFlgs
1231*/
1232#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1233#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1234#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1235
1236/*
1237** These are the allowed modes.
1238*/
1239#define MODE_Line     0  /* One column per line.  Blank line between records */
1240#define MODE_Column   1  /* One record per line in neat columns */
1241#define MODE_List     2  /* One record per line with a separator */
1242#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1243#define MODE_Html     4  /* Generate an XHTML table */
1244#define MODE_Insert   5  /* Generate SQL "insert" statements */
1245#define MODE_Quote    6  /* Quote values as for SQL */
1246#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1247#define MODE_Csv      8  /* Quote strings, numbers are plain */
1248#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1249#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1250#define MODE_Pretty  11  /* Pretty-print schemas */
1251#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1252#define MODE_Json    13  /* Output JSON */
1253#define MODE_Markdown 14 /* Markdown formatting */
1254#define MODE_Table   15  /* MySQL-style table formatting */
1255#define MODE_Box     16  /* Unicode box-drawing characters */
1256
1257static const char *modeDescr[] = {
1258  "line",
1259  "column",
1260  "list",
1261  "semi",
1262  "html",
1263  "insert",
1264  "quote",
1265  "tcl",
1266  "csv",
1267  "explain",
1268  "ascii",
1269  "prettyprint",
1270  "eqp",
1271  "json",
1272  "markdown",
1273  "table",
1274  "box"
1275};
1276
1277/*
1278** These are the column/row/line separators used by the various
1279** import/export modes.
1280*/
1281#define SEP_Column    "|"
1282#define SEP_Row       "\n"
1283#define SEP_Tab       "\t"
1284#define SEP_Space     " "
1285#define SEP_Comma     ","
1286#define SEP_CrLf      "\r\n"
1287#define SEP_Unit      "\x1F"
1288#define SEP_Record    "\x1E"
1289
1290/*
1291** A callback for the sqlite3_log() interface.
1292*/
1293static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1294  ShellState *p = (ShellState*)pArg;
1295  if( p->pLog==0 ) return;
1296  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1297  fflush(p->pLog);
1298}
1299
1300/*
1301** SQL function:  shell_putsnl(X)
1302**
1303** Write the text X to the screen (or whatever output is being directed)
1304** adding a newline at the end, and then return X.
1305*/
1306static void shellPutsFunc(
1307  sqlite3_context *pCtx,
1308  int nVal,
1309  sqlite3_value **apVal
1310){
1311  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1312  (void)nVal;
1313  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1314  sqlite3_result_value(pCtx, apVal[0]);
1315}
1316
1317/*
1318** If in safe mode, print an error message described by the arguments
1319** and exit immediately.
1320*/
1321static void failIfSafeMode(
1322  ShellState *p,
1323  const char *zErrMsg,
1324  ...
1325){
1326  if( p->bSafeMode ){
1327    va_list ap;
1328    char *zMsg;
1329    va_start(ap, zErrMsg);
1330    zMsg = sqlite3_vmprintf(zErrMsg, ap);
1331    va_end(ap);
1332    raw_printf(stderr, "line %d: ", p->lineno);
1333    utf8_printf(stderr, "%s\n", zMsg);
1334    exit(1);
1335  }
1336}
1337
1338/*
1339** SQL function:   edit(VALUE)
1340**                 edit(VALUE,EDITOR)
1341**
1342** These steps:
1343**
1344**     (1) Write VALUE into a temporary file.
1345**     (2) Run program EDITOR on that temporary file.
1346**     (3) Read the temporary file back and return its content as the result.
1347**     (4) Delete the temporary file
1348**
1349** If the EDITOR argument is omitted, use the value in the VISUAL
1350** environment variable.  If still there is no EDITOR, through an error.
1351**
1352** Also throw an error if the EDITOR program returns a non-zero exit code.
1353*/
1354#ifndef SQLITE_NOHAVE_SYSTEM
1355static void editFunc(
1356  sqlite3_context *context,
1357  int argc,
1358  sqlite3_value **argv
1359){
1360  const char *zEditor;
1361  char *zTempFile = 0;
1362  sqlite3 *db;
1363  char *zCmd = 0;
1364  int bBin;
1365  int rc;
1366  int hasCRNL = 0;
1367  FILE *f = 0;
1368  sqlite3_int64 sz;
1369  sqlite3_int64 x;
1370  unsigned char *p = 0;
1371
1372  if( argc==2 ){
1373    zEditor = (const char*)sqlite3_value_text(argv[1]);
1374  }else{
1375    zEditor = getenv("VISUAL");
1376  }
1377  if( zEditor==0 ){
1378    sqlite3_result_error(context, "no editor for edit()", -1);
1379    return;
1380  }
1381  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1382    sqlite3_result_error(context, "NULL input to edit()", -1);
1383    return;
1384  }
1385  db = sqlite3_context_db_handle(context);
1386  zTempFile = 0;
1387  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1388  if( zTempFile==0 ){
1389    sqlite3_uint64 r = 0;
1390    sqlite3_randomness(sizeof(r), &r);
1391    zTempFile = sqlite3_mprintf("temp%llx", r);
1392    if( zTempFile==0 ){
1393      sqlite3_result_error_nomem(context);
1394      return;
1395    }
1396  }
1397  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1398  /* When writing the file to be edited, do \n to \r\n conversions on systems
1399  ** that want \r\n line endings */
1400  f = fopen(zTempFile, bBin ? "wb" : "w");
1401  if( f==0 ){
1402    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1403    goto edit_func_end;
1404  }
1405  sz = sqlite3_value_bytes(argv[0]);
1406  if( bBin ){
1407    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1408  }else{
1409    const char *z = (const char*)sqlite3_value_text(argv[0]);
1410    /* Remember whether or not the value originally contained \r\n */
1411    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1412    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1413  }
1414  fclose(f);
1415  f = 0;
1416  if( x!=sz ){
1417    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1418    goto edit_func_end;
1419  }
1420  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1421  if( zCmd==0 ){
1422    sqlite3_result_error_nomem(context);
1423    goto edit_func_end;
1424  }
1425  rc = system(zCmd);
1426  sqlite3_free(zCmd);
1427  if( rc ){
1428    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1429    goto edit_func_end;
1430  }
1431  f = fopen(zTempFile, "rb");
1432  if( f==0 ){
1433    sqlite3_result_error(context,
1434      "edit() cannot reopen temp file after edit", -1);
1435    goto edit_func_end;
1436  }
1437  fseek(f, 0, SEEK_END);
1438  sz = ftell(f);
1439  rewind(f);
1440  p = sqlite3_malloc64( sz+1 );
1441  if( p==0 ){
1442    sqlite3_result_error_nomem(context);
1443    goto edit_func_end;
1444  }
1445  x = fread(p, 1, (size_t)sz, f);
1446  fclose(f);
1447  f = 0;
1448  if( x!=sz ){
1449    sqlite3_result_error(context, "could not read back the whole file", -1);
1450    goto edit_func_end;
1451  }
1452  if( bBin ){
1453    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1454  }else{
1455    sqlite3_int64 i, j;
1456    if( hasCRNL ){
1457      /* If the original contains \r\n then do no conversions back to \n */
1458    }else{
1459      /* If the file did not originally contain \r\n then convert any new
1460      ** \r\n back into \n */
1461      for(i=j=0; i<sz; i++){
1462        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1463        p[j++] = p[i];
1464      }
1465      sz = j;
1466      p[sz] = 0;
1467    }
1468    sqlite3_result_text64(context, (const char*)p, sz,
1469                          sqlite3_free, SQLITE_UTF8);
1470  }
1471  p = 0;
1472
1473edit_func_end:
1474  if( f ) fclose(f);
1475  unlink(zTempFile);
1476  sqlite3_free(zTempFile);
1477  sqlite3_free(p);
1478}
1479#endif /* SQLITE_NOHAVE_SYSTEM */
1480
1481/*
1482** Save or restore the current output mode
1483*/
1484static void outputModePush(ShellState *p){
1485  p->modePrior = p->mode;
1486  p->priorShFlgs = p->shellFlgs;
1487  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1488  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1489}
1490static void outputModePop(ShellState *p){
1491  p->mode = p->modePrior;
1492  p->shellFlgs = p->priorShFlgs;
1493  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1494  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1495}
1496
1497/*
1498** Output the given string as a hex-encoded blob (eg. X'1234' )
1499*/
1500static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1501  int i;
1502  char *zBlob = (char *)pBlob;
1503  raw_printf(out,"X'");
1504  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1505  raw_printf(out,"'");
1506}
1507
1508/*
1509** Find a string that is not found anywhere in z[].  Return a pointer
1510** to that string.
1511**
1512** Try to use zA and zB first.  If both of those are already found in z[]
1513** then make up some string and store it in the buffer zBuf.
1514*/
1515static const char *unused_string(
1516  const char *z,                    /* Result must not appear anywhere in z */
1517  const char *zA, const char *zB,   /* Try these first */
1518  char *zBuf                        /* Space to store a generated string */
1519){
1520  unsigned i = 0;
1521  if( strstr(z, zA)==0 ) return zA;
1522  if( strstr(z, zB)==0 ) return zB;
1523  do{
1524    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1525  }while( strstr(z,zBuf)!=0 );
1526  return zBuf;
1527}
1528
1529/*
1530** Output the given string as a quoted string using SQL quoting conventions.
1531**
1532** See also: output_quoted_escaped_string()
1533*/
1534static void output_quoted_string(FILE *out, const char *z){
1535  int i;
1536  char c;
1537  setBinaryMode(out, 1);
1538  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1539  if( c==0 ){
1540    utf8_printf(out,"'%s'",z);
1541  }else{
1542    raw_printf(out, "'");
1543    while( *z ){
1544      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1545      if( c=='\'' ) i++;
1546      if( i ){
1547        utf8_printf(out, "%.*s", i, z);
1548        z += i;
1549      }
1550      if( c=='\'' ){
1551        raw_printf(out, "'");
1552        continue;
1553      }
1554      if( c==0 ){
1555        break;
1556      }
1557      z++;
1558    }
1559    raw_printf(out, "'");
1560  }
1561  setTextMode(out, 1);
1562}
1563
1564/*
1565** Output the given string as a quoted string using SQL quoting conventions.
1566** Additionallly , escape the "\n" and "\r" characters so that they do not
1567** get corrupted by end-of-line translation facilities in some operating
1568** systems.
1569**
1570** This is like output_quoted_string() but with the addition of the \r\n
1571** escape mechanism.
1572*/
1573static void output_quoted_escaped_string(FILE *out, const char *z){
1574  int i;
1575  char c;
1576  setBinaryMode(out, 1);
1577  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1578  if( c==0 ){
1579    utf8_printf(out,"'%s'",z);
1580  }else{
1581    const char *zNL = 0;
1582    const char *zCR = 0;
1583    int nNL = 0;
1584    int nCR = 0;
1585    char zBuf1[20], zBuf2[20];
1586    for(i=0; z[i]; i++){
1587      if( z[i]=='\n' ) nNL++;
1588      if( z[i]=='\r' ) nCR++;
1589    }
1590    if( nNL ){
1591      raw_printf(out, "replace(");
1592      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1593    }
1594    if( nCR ){
1595      raw_printf(out, "replace(");
1596      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1597    }
1598    raw_printf(out, "'");
1599    while( *z ){
1600      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1601      if( c=='\'' ) i++;
1602      if( i ){
1603        utf8_printf(out, "%.*s", i, z);
1604        z += i;
1605      }
1606      if( c=='\'' ){
1607        raw_printf(out, "'");
1608        continue;
1609      }
1610      if( c==0 ){
1611        break;
1612      }
1613      z++;
1614      if( c=='\n' ){
1615        raw_printf(out, "%s", zNL);
1616        continue;
1617      }
1618      raw_printf(out, "%s", zCR);
1619    }
1620    raw_printf(out, "'");
1621    if( nCR ){
1622      raw_printf(out, ",'%s',char(13))", zCR);
1623    }
1624    if( nNL ){
1625      raw_printf(out, ",'%s',char(10))", zNL);
1626    }
1627  }
1628  setTextMode(out, 1);
1629}
1630
1631/*
1632** Output the given string as a quoted according to C or TCL quoting rules.
1633*/
1634static void output_c_string(FILE *out, const char *z){
1635  unsigned int c;
1636  fputc('"', out);
1637  while( (c = *(z++))!=0 ){
1638    if( c=='\\' ){
1639      fputc(c, out);
1640      fputc(c, out);
1641    }else if( c=='"' ){
1642      fputc('\\', out);
1643      fputc('"', out);
1644    }else if( c=='\t' ){
1645      fputc('\\', out);
1646      fputc('t', out);
1647    }else if( c=='\n' ){
1648      fputc('\\', out);
1649      fputc('n', out);
1650    }else if( c=='\r' ){
1651      fputc('\\', out);
1652      fputc('r', out);
1653    }else if( !isprint(c&0xff) ){
1654      raw_printf(out, "\\%03o", c&0xff);
1655    }else{
1656      fputc(c, out);
1657    }
1658  }
1659  fputc('"', out);
1660}
1661
1662/*
1663** Output the given string as a quoted according to JSON quoting rules.
1664*/
1665static void output_json_string(FILE *out, const char *z, int n){
1666  unsigned int c;
1667  if( n<0 ) n = (int)strlen(z);
1668  fputc('"', out);
1669  while( n-- ){
1670    c = *(z++);
1671    if( c=='\\' || c=='"' ){
1672      fputc('\\', out);
1673      fputc(c, out);
1674    }else if( c<=0x1f ){
1675      fputc('\\', out);
1676      if( c=='\b' ){
1677        fputc('b', out);
1678      }else if( c=='\f' ){
1679        fputc('f', out);
1680      }else if( c=='\n' ){
1681        fputc('n', out);
1682      }else if( c=='\r' ){
1683        fputc('r', out);
1684      }else if( c=='\t' ){
1685        fputc('t', out);
1686      }else{
1687         raw_printf(out, "u%04x",c);
1688      }
1689    }else{
1690      fputc(c, out);
1691    }
1692  }
1693  fputc('"', out);
1694}
1695
1696/*
1697** Output the given string with characters that are special to
1698** HTML escaped.
1699*/
1700static void output_html_string(FILE *out, const char *z){
1701  int i;
1702  if( z==0 ) z = "";
1703  while( *z ){
1704    for(i=0;   z[i]
1705            && z[i]!='<'
1706            && z[i]!='&'
1707            && z[i]!='>'
1708            && z[i]!='\"'
1709            && z[i]!='\'';
1710        i++){}
1711    if( i>0 ){
1712      utf8_printf(out,"%.*s",i,z);
1713    }
1714    if( z[i]=='<' ){
1715      raw_printf(out,"&lt;");
1716    }else if( z[i]=='&' ){
1717      raw_printf(out,"&amp;");
1718    }else if( z[i]=='>' ){
1719      raw_printf(out,"&gt;");
1720    }else if( z[i]=='\"' ){
1721      raw_printf(out,"&quot;");
1722    }else if( z[i]=='\'' ){
1723      raw_printf(out,"&#39;");
1724    }else{
1725      break;
1726    }
1727    z += i + 1;
1728  }
1729}
1730
1731/*
1732** If a field contains any character identified by a 1 in the following
1733** array, then the string must be quoted for CSV.
1734*/
1735static const char needCsvQuote[] = {
1736  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1737  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1738  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1739  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1740  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1741  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1742  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1743  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1744  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1745  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1746  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1747  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1748  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1749  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1750  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1751  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1752};
1753
1754/*
1755** Output a single term of CSV.  Actually, p->colSeparator is used for
1756** the separator, which may or may not be a comma.  p->nullValue is
1757** the null value.  Strings are quoted if necessary.  The separator
1758** is only issued if bSep is true.
1759*/
1760static void output_csv(ShellState *p, const char *z, int bSep){
1761  FILE *out = p->out;
1762  if( z==0 ){
1763    utf8_printf(out,"%s",p->nullValue);
1764  }else{
1765    unsigned i;
1766    for(i=0; z[i]; i++){
1767      if( needCsvQuote[((unsigned char*)z)[i]] ){
1768        i = 0;
1769        break;
1770      }
1771    }
1772    if( i==0 || strstr(z, p->colSeparator)!=0 ){
1773      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1774      utf8_printf(out, "%s", zQuoted);
1775      sqlite3_free(zQuoted);
1776    }else{
1777      utf8_printf(out, "%s", z);
1778    }
1779  }
1780  if( bSep ){
1781    utf8_printf(p->out, "%s", p->colSeparator);
1782  }
1783}
1784
1785/*
1786** This routine runs when the user presses Ctrl-C
1787*/
1788static void interrupt_handler(int NotUsed){
1789  UNUSED_PARAMETER(NotUsed);
1790  seenInterrupt++;
1791  if( seenInterrupt>2 ) exit(1);
1792  if( globalDb ) sqlite3_interrupt(globalDb);
1793}
1794
1795#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1796/*
1797** This routine runs for console events (e.g. Ctrl-C) on Win32
1798*/
1799static BOOL WINAPI ConsoleCtrlHandler(
1800  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1801){
1802  if( dwCtrlType==CTRL_C_EVENT ){
1803    interrupt_handler(0);
1804    return TRUE;
1805  }
1806  return FALSE;
1807}
1808#endif
1809
1810#ifndef SQLITE_OMIT_AUTHORIZATION
1811/*
1812** This authorizer runs in safe mode.
1813*/
1814static int safeModeAuth(
1815  void *pClientData,
1816  int op,
1817  const char *zA1,
1818  const char *zA2,
1819  const char *zA3,
1820  const char *zA4
1821){
1822  ShellState *p = (ShellState*)pClientData;
1823  static const char *azProhibitedFunctions[] = {
1824    "edit",
1825    "fts3_tokenizer",
1826    "load_extension",
1827    "readfile",
1828    "writefile",
1829    "zipfile",
1830    "zipfile_cds",
1831  };
1832  UNUSED_PARAMETER(zA2);
1833  UNUSED_PARAMETER(zA3);
1834  UNUSED_PARAMETER(zA4);
1835  switch( op ){
1836    case SQLITE_ATTACH: {
1837      failIfSafeMode(p, "cannot run ATTACH in safe mode");
1838      break;
1839    }
1840    case SQLITE_FUNCTION: {
1841      int i;
1842      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
1843        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
1844          failIfSafeMode(p, "cannot use the %s() function in safe mode",
1845                         azProhibitedFunctions[i]);
1846        }
1847      }
1848      break;
1849    }
1850  }
1851  return SQLITE_OK;
1852}
1853
1854/*
1855** When the ".auth ON" is set, the following authorizer callback is
1856** invoked.  It always returns SQLITE_OK.
1857*/
1858static int shellAuth(
1859  void *pClientData,
1860  int op,
1861  const char *zA1,
1862  const char *zA2,
1863  const char *zA3,
1864  const char *zA4
1865){
1866  ShellState *p = (ShellState*)pClientData;
1867  static const char *azAction[] = { 0,
1868     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1869     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1870     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1871     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1872     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1873     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1874     "PRAGMA",               "READ",                 "SELECT",
1875     "TRANSACTION",          "UPDATE",               "ATTACH",
1876     "DETACH",               "ALTER_TABLE",          "REINDEX",
1877     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1878     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1879  };
1880  int i;
1881  const char *az[4];
1882  az[0] = zA1;
1883  az[1] = zA2;
1884  az[2] = zA3;
1885  az[3] = zA4;
1886  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1887  for(i=0; i<4; i++){
1888    raw_printf(p->out, " ");
1889    if( az[i] ){
1890      output_c_string(p->out, az[i]);
1891    }else{
1892      raw_printf(p->out, "NULL");
1893    }
1894  }
1895  raw_printf(p->out, "\n");
1896  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1897  return SQLITE_OK;
1898}
1899#endif
1900
1901/*
1902** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1903**
1904** This routine converts some CREATE TABLE statements for shadow tables
1905** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1906*/
1907static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1908  if( z==0 ) return;
1909  if( zTail==0 ) return;
1910  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1911    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1912  }else{
1913    utf8_printf(out, "%s%s", z, zTail);
1914  }
1915}
1916static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1917  char c = z[n];
1918  z[n] = 0;
1919  printSchemaLine(out, z, zTail);
1920  z[n] = c;
1921}
1922
1923/*
1924** Return true if string z[] has nothing but whitespace and comments to the
1925** end of the first line.
1926*/
1927static int wsToEol(const char *z){
1928  int i;
1929  for(i=0; z[i]; i++){
1930    if( z[i]=='\n' ) return 1;
1931    if( IsSpace(z[i]) ) continue;
1932    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1933    return 0;
1934  }
1935  return 1;
1936}
1937
1938/*
1939** Add a new entry to the EXPLAIN QUERY PLAN data
1940*/
1941static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1942  EQPGraphRow *pNew;
1943  int nText = strlen30(zText);
1944  if( p->autoEQPtest ){
1945    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1946  }
1947  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1948  if( pNew==0 ) shell_out_of_memory();
1949  pNew->iEqpId = iEqpId;
1950  pNew->iParentId = p2;
1951  memcpy(pNew->zText, zText, nText+1);
1952  pNew->pNext = 0;
1953  if( p->sGraph.pLast ){
1954    p->sGraph.pLast->pNext = pNew;
1955  }else{
1956    p->sGraph.pRow = pNew;
1957  }
1958  p->sGraph.pLast = pNew;
1959}
1960
1961/*
1962** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1963** in p->sGraph.
1964*/
1965static void eqp_reset(ShellState *p){
1966  EQPGraphRow *pRow, *pNext;
1967  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1968    pNext = pRow->pNext;
1969    sqlite3_free(pRow);
1970  }
1971  memset(&p->sGraph, 0, sizeof(p->sGraph));
1972}
1973
1974/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1975** pOld, or return the first such line if pOld is NULL
1976*/
1977static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1978  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1979  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1980  return pRow;
1981}
1982
1983/* Render a single level of the graph that has iEqpId as its parent.  Called
1984** recursively to render sublevels.
1985*/
1986static void eqp_render_level(ShellState *p, int iEqpId){
1987  EQPGraphRow *pRow, *pNext;
1988  int n = strlen30(p->sGraph.zPrefix);
1989  char *z;
1990  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1991    pNext = eqp_next_row(p, iEqpId, pRow);
1992    z = pRow->zText;
1993    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1994                pNext ? "|--" : "`--", z);
1995    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1996      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1997      eqp_render_level(p, pRow->iEqpId);
1998      p->sGraph.zPrefix[n] = 0;
1999    }
2000  }
2001}
2002
2003/*
2004** Display and reset the EXPLAIN QUERY PLAN data
2005*/
2006static void eqp_render(ShellState *p){
2007  EQPGraphRow *pRow = p->sGraph.pRow;
2008  if( pRow ){
2009    if( pRow->zText[0]=='-' ){
2010      if( pRow->pNext==0 ){
2011        eqp_reset(p);
2012        return;
2013      }
2014      utf8_printf(p->out, "%s\n", pRow->zText+3);
2015      p->sGraph.pRow = pRow->pNext;
2016      sqlite3_free(pRow);
2017    }else{
2018      utf8_printf(p->out, "QUERY PLAN\n");
2019    }
2020    p->sGraph.zPrefix[0] = 0;
2021    eqp_render_level(p, 0);
2022    eqp_reset(p);
2023  }
2024}
2025
2026#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2027/*
2028** Progress handler callback.
2029*/
2030static int progress_handler(void *pClientData) {
2031  ShellState *p = (ShellState*)pClientData;
2032  p->nProgress++;
2033  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2034    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2035    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2036    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2037    return 1;
2038  }
2039  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2040    raw_printf(p->out, "Progress %u\n", p->nProgress);
2041  }
2042  return 0;
2043}
2044#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2045
2046/*
2047** Print N dashes
2048*/
2049static void print_dashes(FILE *out, int N){
2050  const char zDash[] = "--------------------------------------------------";
2051  const int nDash = sizeof(zDash) - 1;
2052  while( N>nDash ){
2053    fputs(zDash, out);
2054    N -= nDash;
2055  }
2056  raw_printf(out, "%.*s", N, zDash);
2057}
2058
2059/*
2060** Print a markdown or table-style row separator using ascii-art
2061*/
2062static void print_row_separator(
2063  ShellState *p,
2064  int nArg,
2065  const char *zSep
2066){
2067  int i;
2068  if( nArg>0 ){
2069    fputs(zSep, p->out);
2070    print_dashes(p->out, p->actualWidth[0]+2);
2071    for(i=1; i<nArg; i++){
2072      fputs(zSep, p->out);
2073      print_dashes(p->out, p->actualWidth[i]+2);
2074    }
2075    fputs(zSep, p->out);
2076  }
2077  fputs("\n", p->out);
2078}
2079
2080/*
2081** This is the callback routine that the shell
2082** invokes for each row of a query result.
2083*/
2084static int shell_callback(
2085  void *pArg,
2086  int nArg,        /* Number of result columns */
2087  char **azArg,    /* Text of each result column */
2088  char **azCol,    /* Column names */
2089  int *aiType      /* Column types.  Might be NULL */
2090){
2091  int i;
2092  ShellState *p = (ShellState*)pArg;
2093
2094  if( azArg==0 ) return 0;
2095  switch( p->cMode ){
2096    case MODE_Line: {
2097      int w = 5;
2098      if( azArg==0 ) break;
2099      for(i=0; i<nArg; i++){
2100        int len = strlen30(azCol[i] ? azCol[i] : "");
2101        if( len>w ) w = len;
2102      }
2103      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2104      for(i=0; i<nArg; i++){
2105        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2106                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2107      }
2108      break;
2109    }
2110    case MODE_Explain: {
2111      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2112      if( nArg>ArraySize(aExplainWidth) ){
2113        nArg = ArraySize(aExplainWidth);
2114      }
2115      if( p->cnt++==0 ){
2116        for(i=0; i<nArg; i++){
2117          int w = aExplainWidth[i];
2118          utf8_width_print(p->out, w, azCol[i]);
2119          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2120        }
2121        for(i=0; i<nArg; i++){
2122          int w = aExplainWidth[i];
2123          print_dashes(p->out, w);
2124          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2125        }
2126      }
2127      if( azArg==0 ) break;
2128      for(i=0; i<nArg; i++){
2129        int w = aExplainWidth[i];
2130        if( i==nArg-1 ) w = 0;
2131        if( azArg[i] && strlenChar(azArg[i])>w ){
2132          w = strlenChar(azArg[i]);
2133        }
2134        if( i==1 && p->aiIndent && p->pStmt ){
2135          if( p->iIndent<p->nIndent ){
2136            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2137          }
2138          p->iIndent++;
2139        }
2140        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2141        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2142      }
2143      break;
2144    }
2145    case MODE_Semi: {   /* .schema and .fullschema output */
2146      printSchemaLine(p->out, azArg[0], ";\n");
2147      break;
2148    }
2149    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2150      char *z;
2151      int j;
2152      int nParen = 0;
2153      char cEnd = 0;
2154      char c;
2155      int nLine = 0;
2156      assert( nArg==1 );
2157      if( azArg[0]==0 ) break;
2158      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2159       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2160      ){
2161        utf8_printf(p->out, "%s;\n", azArg[0]);
2162        break;
2163      }
2164      z = sqlite3_mprintf("%s", azArg[0]);
2165      j = 0;
2166      for(i=0; IsSpace(z[i]); i++){}
2167      for(; (c = z[i])!=0; i++){
2168        if( IsSpace(c) ){
2169          if( z[j-1]=='\r' ) z[j-1] = '\n';
2170          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2171        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2172          j--;
2173        }
2174        z[j++] = c;
2175      }
2176      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2177      z[j] = 0;
2178      if( strlen30(z)>=79 ){
2179        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2180          if( c==cEnd ){
2181            cEnd = 0;
2182          }else if( c=='"' || c=='\'' || c=='`' ){
2183            cEnd = c;
2184          }else if( c=='[' ){
2185            cEnd = ']';
2186          }else if( c=='-' && z[i+1]=='-' ){
2187            cEnd = '\n';
2188          }else if( c=='(' ){
2189            nParen++;
2190          }else if( c==')' ){
2191            nParen--;
2192            if( nLine>0 && nParen==0 && j>0 ){
2193              printSchemaLineN(p->out, z, j, "\n");
2194              j = 0;
2195            }
2196          }
2197          z[j++] = c;
2198          if( nParen==1 && cEnd==0
2199           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2200          ){
2201            if( c=='\n' ) j--;
2202            printSchemaLineN(p->out, z, j, "\n  ");
2203            j = 0;
2204            nLine++;
2205            while( IsSpace(z[i+1]) ){ i++; }
2206          }
2207        }
2208        z[j] = 0;
2209      }
2210      printSchemaLine(p->out, z, ";\n");
2211      sqlite3_free(z);
2212      break;
2213    }
2214    case MODE_List: {
2215      if( p->cnt++==0 && p->showHeader ){
2216        for(i=0; i<nArg; i++){
2217          utf8_printf(p->out,"%s%s",azCol[i],
2218                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2219        }
2220      }
2221      if( azArg==0 ) break;
2222      for(i=0; i<nArg; i++){
2223        char *z = azArg[i];
2224        if( z==0 ) z = p->nullValue;
2225        utf8_printf(p->out, "%s", z);
2226        if( i<nArg-1 ){
2227          utf8_printf(p->out, "%s", p->colSeparator);
2228        }else{
2229          utf8_printf(p->out, "%s", p->rowSeparator);
2230        }
2231      }
2232      break;
2233    }
2234    case MODE_Html: {
2235      if( p->cnt++==0 && p->showHeader ){
2236        raw_printf(p->out,"<TR>");
2237        for(i=0; i<nArg; i++){
2238          raw_printf(p->out,"<TH>");
2239          output_html_string(p->out, azCol[i]);
2240          raw_printf(p->out,"</TH>\n");
2241        }
2242        raw_printf(p->out,"</TR>\n");
2243      }
2244      if( azArg==0 ) break;
2245      raw_printf(p->out,"<TR>");
2246      for(i=0; i<nArg; i++){
2247        raw_printf(p->out,"<TD>");
2248        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2249        raw_printf(p->out,"</TD>\n");
2250      }
2251      raw_printf(p->out,"</TR>\n");
2252      break;
2253    }
2254    case MODE_Tcl: {
2255      if( p->cnt++==0 && p->showHeader ){
2256        for(i=0; i<nArg; i++){
2257          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2258          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2259        }
2260        utf8_printf(p->out, "%s", p->rowSeparator);
2261      }
2262      if( azArg==0 ) break;
2263      for(i=0; i<nArg; i++){
2264        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2265        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2266      }
2267      utf8_printf(p->out, "%s", p->rowSeparator);
2268      break;
2269    }
2270    case MODE_Csv: {
2271      setBinaryMode(p->out, 1);
2272      if( p->cnt++==0 && p->showHeader ){
2273        for(i=0; i<nArg; i++){
2274          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2275        }
2276        utf8_printf(p->out, "%s", p->rowSeparator);
2277      }
2278      if( nArg>0 ){
2279        for(i=0; i<nArg; i++){
2280          output_csv(p, azArg[i], i<nArg-1);
2281        }
2282        utf8_printf(p->out, "%s", p->rowSeparator);
2283      }
2284      setTextMode(p->out, 1);
2285      break;
2286    }
2287    case MODE_Insert: {
2288      if( azArg==0 ) break;
2289      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2290      if( p->showHeader ){
2291        raw_printf(p->out,"(");
2292        for(i=0; i<nArg; i++){
2293          if( i>0 ) raw_printf(p->out, ",");
2294          if( quoteChar(azCol[i]) ){
2295            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2296            utf8_printf(p->out, "%s", z);
2297            sqlite3_free(z);
2298          }else{
2299            raw_printf(p->out, "%s", azCol[i]);
2300          }
2301        }
2302        raw_printf(p->out,")");
2303      }
2304      p->cnt++;
2305      for(i=0; i<nArg; i++){
2306        raw_printf(p->out, i>0 ? "," : " VALUES(");
2307        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2308          utf8_printf(p->out,"NULL");
2309        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2310          if( ShellHasFlag(p, SHFLG_Newlines) ){
2311            output_quoted_string(p->out, azArg[i]);
2312          }else{
2313            output_quoted_escaped_string(p->out, azArg[i]);
2314          }
2315        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2316          utf8_printf(p->out,"%s", azArg[i]);
2317        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2318          char z[50];
2319          double r = sqlite3_column_double(p->pStmt, i);
2320          sqlite3_uint64 ur;
2321          memcpy(&ur,&r,sizeof(r));
2322          if( ur==0x7ff0000000000000LL ){
2323            raw_printf(p->out, "1e999");
2324          }else if( ur==0xfff0000000000000LL ){
2325            raw_printf(p->out, "-1e999");
2326          }else{
2327            sqlite3_snprintf(50,z,"%!.20g", r);
2328            raw_printf(p->out, "%s", z);
2329          }
2330        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2331          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2332          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2333          output_hex_blob(p->out, pBlob, nBlob);
2334        }else if( isNumber(azArg[i], 0) ){
2335          utf8_printf(p->out,"%s", azArg[i]);
2336        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2337          output_quoted_string(p->out, azArg[i]);
2338        }else{
2339          output_quoted_escaped_string(p->out, azArg[i]);
2340        }
2341      }
2342      raw_printf(p->out,");\n");
2343      break;
2344    }
2345    case MODE_Json: {
2346      if( azArg==0 ) break;
2347      if( p->cnt==0 ){
2348        fputs("[{", p->out);
2349      }else{
2350        fputs(",\n{", p->out);
2351      }
2352      p->cnt++;
2353      for(i=0; i<nArg; i++){
2354        output_json_string(p->out, azCol[i], -1);
2355        putc(':', p->out);
2356        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2357          fputs("null",p->out);
2358        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2359          char z[50];
2360          double r = sqlite3_column_double(p->pStmt, i);
2361          sqlite3_uint64 ur;
2362          memcpy(&ur,&r,sizeof(r));
2363          if( ur==0x7ff0000000000000LL ){
2364            raw_printf(p->out, "1e999");
2365          }else if( ur==0xfff0000000000000LL ){
2366            raw_printf(p->out, "-1e999");
2367          }else{
2368            sqlite3_snprintf(50,z,"%!.20g", r);
2369            raw_printf(p->out, "%s", z);
2370          }
2371        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2372          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2373          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2374          output_json_string(p->out, pBlob, nBlob);
2375        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2376          output_json_string(p->out, azArg[i], -1);
2377        }else{
2378          utf8_printf(p->out,"%s", azArg[i]);
2379        }
2380        if( i<nArg-1 ){
2381          putc(',', p->out);
2382        }
2383      }
2384      putc('}', p->out);
2385      break;
2386    }
2387    case MODE_Quote: {
2388      if( azArg==0 ) break;
2389      if( p->cnt==0 && p->showHeader ){
2390        for(i=0; i<nArg; i++){
2391          if( i>0 ) fputs(p->colSeparator, p->out);
2392          output_quoted_string(p->out, azCol[i]);
2393        }
2394        fputs(p->rowSeparator, p->out);
2395      }
2396      p->cnt++;
2397      for(i=0; i<nArg; i++){
2398        if( i>0 ) fputs(p->colSeparator, p->out);
2399        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2400          utf8_printf(p->out,"NULL");
2401        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2402          output_quoted_string(p->out, azArg[i]);
2403        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2404          utf8_printf(p->out,"%s", azArg[i]);
2405        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2406          char z[50];
2407          double r = sqlite3_column_double(p->pStmt, i);
2408          sqlite3_snprintf(50,z,"%!.20g", r);
2409          raw_printf(p->out, "%s", z);
2410        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2411          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2412          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2413          output_hex_blob(p->out, pBlob, nBlob);
2414        }else if( isNumber(azArg[i], 0) ){
2415          utf8_printf(p->out,"%s", azArg[i]);
2416        }else{
2417          output_quoted_string(p->out, azArg[i]);
2418        }
2419      }
2420      fputs(p->rowSeparator, p->out);
2421      break;
2422    }
2423    case MODE_Ascii: {
2424      if( p->cnt++==0 && p->showHeader ){
2425        for(i=0; i<nArg; i++){
2426          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2427          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2428        }
2429        utf8_printf(p->out, "%s", p->rowSeparator);
2430      }
2431      if( azArg==0 ) break;
2432      for(i=0; i<nArg; i++){
2433        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2434        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2435      }
2436      utf8_printf(p->out, "%s", p->rowSeparator);
2437      break;
2438    }
2439    case MODE_EQP: {
2440      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2441      break;
2442    }
2443  }
2444  return 0;
2445}
2446
2447/*
2448** This is the callback routine that the SQLite library
2449** invokes for each row of a query result.
2450*/
2451static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2452  /* since we don't have type info, call the shell_callback with a NULL value */
2453  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2454}
2455
2456/*
2457** This is the callback routine from sqlite3_exec() that appends all
2458** output onto the end of a ShellText object.
2459*/
2460static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2461  ShellText *p = (ShellText*)pArg;
2462  int i;
2463  UNUSED_PARAMETER(az);
2464  if( azArg==0 ) return 0;
2465  if( p->n ) appendText(p, "|", 0);
2466  for(i=0; i<nArg; i++){
2467    if( i ) appendText(p, ",", 0);
2468    if( azArg[i] ) appendText(p, azArg[i], 0);
2469  }
2470  return 0;
2471}
2472
2473/*
2474** Generate an appropriate SELFTEST table in the main database.
2475*/
2476static void createSelftestTable(ShellState *p){
2477  char *zErrMsg = 0;
2478  sqlite3_exec(p->db,
2479    "SAVEPOINT selftest_init;\n"
2480    "CREATE TABLE IF NOT EXISTS selftest(\n"
2481    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2482    "  op TEXT,\n"                   /* Operator:  memo run */
2483    "  cmd TEXT,\n"                  /* Command text */
2484    "  ans TEXT\n"                   /* Desired answer */
2485    ");"
2486    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2487    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2488    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2489    "         'memo','Tests generated by --init');\n"
2490    "INSERT INTO [_shell$self]\n"
2491    "  SELECT 'run',\n"
2492    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2493                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2494    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2495                          "FROM sqlite_schema ORDER BY 2',224));\n"
2496    "INSERT INTO [_shell$self]\n"
2497    "  SELECT 'run',"
2498    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2499    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2500    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2501    "  FROM (\n"
2502    "    SELECT name FROM sqlite_schema\n"
2503    "     WHERE type='table'\n"
2504    "       AND name<>'selftest'\n"
2505    "       AND coalesce(rootpage,0)>0\n"
2506    "  )\n"
2507    " ORDER BY name;\n"
2508    "INSERT INTO [_shell$self]\n"
2509    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2510    "INSERT INTO selftest(tno,op,cmd,ans)"
2511    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2512    "DROP TABLE [_shell$self];"
2513    ,0,0,&zErrMsg);
2514  if( zErrMsg ){
2515    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2516    sqlite3_free(zErrMsg);
2517  }
2518  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2519}
2520
2521
2522/*
2523** Set the destination table field of the ShellState structure to
2524** the name of the table given.  Escape any quote characters in the
2525** table name.
2526*/
2527static void set_table_name(ShellState *p, const char *zName){
2528  int i, n;
2529  char cQuote;
2530  char *z;
2531
2532  if( p->zDestTable ){
2533    free(p->zDestTable);
2534    p->zDestTable = 0;
2535  }
2536  if( zName==0 ) return;
2537  cQuote = quoteChar(zName);
2538  n = strlen30(zName);
2539  if( cQuote ) n += n+2;
2540  z = p->zDestTable = malloc( n+1 );
2541  if( z==0 ) shell_out_of_memory();
2542  n = 0;
2543  if( cQuote ) z[n++] = cQuote;
2544  for(i=0; zName[i]; i++){
2545    z[n++] = zName[i];
2546    if( zName[i]==cQuote ) z[n++] = cQuote;
2547  }
2548  if( cQuote ) z[n++] = cQuote;
2549  z[n] = 0;
2550}
2551
2552
2553/*
2554** Execute a query statement that will generate SQL output.  Print
2555** the result columns, comma-separated, on a line and then add a
2556** semicolon terminator to the end of that line.
2557**
2558** If the number of columns is 1 and that column contains text "--"
2559** then write the semicolon on a separate line.  That way, if a
2560** "--" comment occurs at the end of the statement, the comment
2561** won't consume the semicolon terminator.
2562*/
2563static int run_table_dump_query(
2564  ShellState *p,           /* Query context */
2565  const char *zSelect      /* SELECT statement to extract content */
2566){
2567  sqlite3_stmt *pSelect;
2568  int rc;
2569  int nResult;
2570  int i;
2571  const char *z;
2572  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2573  if( rc!=SQLITE_OK || !pSelect ){
2574    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2575                sqlite3_errmsg(p->db));
2576    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2577    return rc;
2578  }
2579  rc = sqlite3_step(pSelect);
2580  nResult = sqlite3_column_count(pSelect);
2581  while( rc==SQLITE_ROW ){
2582    z = (const char*)sqlite3_column_text(pSelect, 0);
2583    utf8_printf(p->out, "%s", z);
2584    for(i=1; i<nResult; i++){
2585      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2586    }
2587    if( z==0 ) z = "";
2588    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2589    if( z[0] ){
2590      raw_printf(p->out, "\n;\n");
2591    }else{
2592      raw_printf(p->out, ";\n");
2593    }
2594    rc = sqlite3_step(pSelect);
2595  }
2596  rc = sqlite3_finalize(pSelect);
2597  if( rc!=SQLITE_OK ){
2598    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2599                sqlite3_errmsg(p->db));
2600    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2601  }
2602  return rc;
2603}
2604
2605/*
2606** Allocate space and save off string indicating current error.
2607*/
2608static char *save_err_msg(
2609  sqlite3 *db,           /* Database to query */
2610  const char *zWhen,     /* Qualifier (format) wrapper */
2611  int rc                 /* Error code returned from API */
2612){
2613  if( zWhen==0 )
2614    zWhen = "%s (%d)";
2615  return sqlite3_mprintf(zWhen, sqlite3_errmsg(db), rc);
2616}
2617
2618#ifdef __linux__
2619/*
2620** Attempt to display I/O stats on Linux using /proc/PID/io
2621*/
2622static void displayLinuxIoStats(FILE *out){
2623  FILE *in;
2624  char z[200];
2625  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2626  in = fopen(z, "rb");
2627  if( in==0 ) return;
2628  while( fgets(z, sizeof(z), in)!=0 ){
2629    static const struct {
2630      const char *zPattern;
2631      const char *zDesc;
2632    } aTrans[] = {
2633      { "rchar: ",                  "Bytes received by read():" },
2634      { "wchar: ",                  "Bytes sent to write():"    },
2635      { "syscr: ",                  "Read() system calls:"      },
2636      { "syscw: ",                  "Write() system calls:"     },
2637      { "read_bytes: ",             "Bytes read from storage:"  },
2638      { "write_bytes: ",            "Bytes written to storage:" },
2639      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2640    };
2641    int i;
2642    for(i=0; i<ArraySize(aTrans); i++){
2643      int n = strlen30(aTrans[i].zPattern);
2644      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2645        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2646        break;
2647      }
2648    }
2649  }
2650  fclose(in);
2651}
2652#endif
2653
2654/*
2655** Display a single line of status using 64-bit values.
2656*/
2657static void displayStatLine(
2658  ShellState *p,            /* The shell context */
2659  char *zLabel,             /* Label for this one line */
2660  char *zFormat,            /* Format for the result */
2661  int iStatusCtrl,          /* Which status to display */
2662  int bReset                /* True to reset the stats */
2663){
2664  sqlite3_int64 iCur = -1;
2665  sqlite3_int64 iHiwtr = -1;
2666  int i, nPercent;
2667  char zLine[200];
2668  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2669  for(i=0, nPercent=0; zFormat[i]; i++){
2670    if( zFormat[i]=='%' ) nPercent++;
2671  }
2672  if( nPercent>1 ){
2673    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2674  }else{
2675    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2676  }
2677  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2678}
2679
2680/*
2681** Display memory stats.
2682*/
2683static int display_stats(
2684  sqlite3 *db,                /* Database to query */
2685  ShellState *pArg,           /* Pointer to ShellState */
2686  int bReset                  /* True to reset the stats */
2687){
2688  int iCur;
2689  int iHiwtr;
2690  FILE *out;
2691  if( pArg==0 || pArg->out==0 ) return 0;
2692  out = pArg->out;
2693
2694  if( pArg->pStmt && pArg->statsOn==2 ){
2695    int nCol, i, x;
2696    sqlite3_stmt *pStmt = pArg->pStmt;
2697    char z[100];
2698    nCol = sqlite3_column_count(pStmt);
2699    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2700    for(i=0; i<nCol; i++){
2701      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2702      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2703#ifndef SQLITE_OMIT_DECLTYPE
2704      sqlite3_snprintf(30, z+x, "declared type:");
2705      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2706#endif
2707#ifdef SQLITE_ENABLE_COLUMN_METADATA
2708      sqlite3_snprintf(30, z+x, "database name:");
2709      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2710      sqlite3_snprintf(30, z+x, "table name:");
2711      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2712      sqlite3_snprintf(30, z+x, "origin name:");
2713      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2714#endif
2715    }
2716  }
2717
2718  if( pArg->statsOn==3 ){
2719    if( pArg->pStmt ){
2720      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2721      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2722    }
2723    return 0;
2724  }
2725
2726  displayStatLine(pArg, "Memory Used:",
2727     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2728  displayStatLine(pArg, "Number of Outstanding Allocations:",
2729     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2730  if( pArg->shellFlgs & SHFLG_Pagecache ){
2731    displayStatLine(pArg, "Number of Pcache Pages Used:",
2732       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2733  }
2734  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2735     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2736  displayStatLine(pArg, "Largest Allocation:",
2737     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2738  displayStatLine(pArg, "Largest Pcache Allocation:",
2739     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2740#ifdef YYTRACKMAXSTACKDEPTH
2741  displayStatLine(pArg, "Deepest Parser Stack:",
2742     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2743#endif
2744
2745  if( db ){
2746    if( pArg->shellFlgs & SHFLG_Lookaside ){
2747      iHiwtr = iCur = -1;
2748      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2749                        &iCur, &iHiwtr, bReset);
2750      raw_printf(pArg->out,
2751              "Lookaside Slots Used:                %d (max %d)\n",
2752              iCur, iHiwtr);
2753      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2754                        &iCur, &iHiwtr, bReset);
2755      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2756              iHiwtr);
2757      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2758                        &iCur, &iHiwtr, bReset);
2759      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2760              iHiwtr);
2761      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2762                        &iCur, &iHiwtr, bReset);
2763      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2764              iHiwtr);
2765    }
2766    iHiwtr = iCur = -1;
2767    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2768    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2769            iCur);
2770    iHiwtr = iCur = -1;
2771    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2772    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2773    iHiwtr = iCur = -1;
2774    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2775    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2776    iHiwtr = iCur = -1;
2777    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2778    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2779    iHiwtr = iCur = -1;
2780    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2781    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2782    iHiwtr = iCur = -1;
2783    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2784    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2785            iCur);
2786    iHiwtr = iCur = -1;
2787    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2788    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2789            iCur);
2790  }
2791
2792  if( pArg->pStmt ){
2793    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2794                               bReset);
2795    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2796    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2797    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2798    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2799    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2800    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2801    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2802    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2803    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2804    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2805    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2806    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2807    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2808  }
2809
2810#ifdef __linux__
2811  displayLinuxIoStats(pArg->out);
2812#endif
2813
2814  /* Do not remove this machine readable comment: extra-stats-output-here */
2815
2816  return 0;
2817}
2818
2819/*
2820** Display scan stats.
2821*/
2822static void display_scanstats(
2823  sqlite3 *db,                    /* Database to query */
2824  ShellState *pArg                /* Pointer to ShellState */
2825){
2826#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2827  UNUSED_PARAMETER(db);
2828  UNUSED_PARAMETER(pArg);
2829#else
2830  int i, k, n, mx;
2831  raw_printf(pArg->out, "-------- scanstats --------\n");
2832  mx = 0;
2833  for(k=0; k<=mx; k++){
2834    double rEstLoop = 1.0;
2835    for(i=n=0; 1; i++){
2836      sqlite3_stmt *p = pArg->pStmt;
2837      sqlite3_int64 nLoop, nVisit;
2838      double rEst;
2839      int iSid;
2840      const char *zExplain;
2841      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2842        break;
2843      }
2844      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2845      if( iSid>mx ) mx = iSid;
2846      if( iSid!=k ) continue;
2847      if( n==0 ){
2848        rEstLoop = (double)nLoop;
2849        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2850      }
2851      n++;
2852      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2853      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2854      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2855      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2856      rEstLoop *= rEst;
2857      raw_printf(pArg->out,
2858          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2859          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2860      );
2861    }
2862  }
2863  raw_printf(pArg->out, "---------------------------\n");
2864#endif
2865}
2866
2867/*
2868** Parameter azArray points to a zero-terminated array of strings. zStr
2869** points to a single nul-terminated string. Return non-zero if zStr
2870** is equal, according to strcmp(), to any of the strings in the array.
2871** Otherwise, return zero.
2872*/
2873static int str_in_array(const char *zStr, const char **azArray){
2874  int i;
2875  for(i=0; azArray[i]; i++){
2876    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2877  }
2878  return 0;
2879}
2880
2881/*
2882** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2883** and populate the ShellState.aiIndent[] array with the number of
2884** spaces each opcode should be indented before it is output.
2885**
2886** The indenting rules are:
2887**
2888**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2889**       all opcodes that occur between the p2 jump destination and the opcode
2890**       itself by 2 spaces.
2891**
2892**     * For each "Goto", if the jump destination is earlier in the program
2893**       and ends on one of:
2894**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2895**       or if the P1 parameter is one instead of zero,
2896**       then indent all opcodes between the earlier instruction
2897**       and "Goto" by 2 spaces.
2898*/
2899static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2900  const char *zSql;               /* The text of the SQL statement */
2901  const char *z;                  /* Used to check if this is an EXPLAIN */
2902  int *abYield = 0;               /* True if op is an OP_Yield */
2903  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2904  int iOp;                        /* Index of operation in p->aiIndent[] */
2905
2906  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2907  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2908                            "Rewind", 0 };
2909  const char *azGoto[] = { "Goto", 0 };
2910
2911  /* Try to figure out if this is really an EXPLAIN statement. If this
2912  ** cannot be verified, return early.  */
2913  if( sqlite3_column_count(pSql)!=8 ){
2914    p->cMode = p->mode;
2915    return;
2916  }
2917  zSql = sqlite3_sql(pSql);
2918  if( zSql==0 ) return;
2919  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2920  if( sqlite3_strnicmp(z, "explain", 7) ){
2921    p->cMode = p->mode;
2922    return;
2923  }
2924
2925  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2926    int i;
2927    int iAddr = sqlite3_column_int(pSql, 0);
2928    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2929
2930    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2931    ** p2 is an instruction address, set variable p2op to the index of that
2932    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2933    ** the current instruction is part of a sub-program generated by an
2934    ** SQL trigger or foreign key.  */
2935    int p2 = sqlite3_column_int(pSql, 3);
2936    int p2op = (p2 + (iOp-iAddr));
2937
2938    /* Grow the p->aiIndent array as required */
2939    if( iOp>=nAlloc ){
2940      if( iOp==0 ){
2941        /* Do further verfication that this is explain output.  Abort if
2942        ** it is not */
2943        static const char *explainCols[] = {
2944           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2945        int jj;
2946        for(jj=0; jj<ArraySize(explainCols); jj++){
2947          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2948            p->cMode = p->mode;
2949            sqlite3_reset(pSql);
2950            return;
2951          }
2952        }
2953      }
2954      nAlloc += 100;
2955      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2956      if( p->aiIndent==0 ) shell_out_of_memory();
2957      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2958      if( abYield==0 ) shell_out_of_memory();
2959    }
2960    abYield[iOp] = str_in_array(zOp, azYield);
2961    p->aiIndent[iOp] = 0;
2962    p->nIndent = iOp+1;
2963
2964    if( str_in_array(zOp, azNext) ){
2965      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2966    }
2967    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2968     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2969    ){
2970      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2971    }
2972  }
2973
2974  p->iIndent = 0;
2975  sqlite3_free(abYield);
2976  sqlite3_reset(pSql);
2977}
2978
2979/*
2980** Free the array allocated by explain_data_prepare().
2981*/
2982static void explain_data_delete(ShellState *p){
2983  sqlite3_free(p->aiIndent);
2984  p->aiIndent = 0;
2985  p->nIndent = 0;
2986  p->iIndent = 0;
2987}
2988
2989/*
2990** Disable and restore .wheretrace and .selecttrace settings.
2991*/
2992static unsigned int savedSelectTrace;
2993static unsigned int savedWhereTrace;
2994static void disable_debug_trace_modes(void){
2995  unsigned int zero = 0;
2996  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
2997  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
2998  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
2999  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3000}
3001static void restore_debug_trace_modes(void){
3002  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3003  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3004}
3005
3006/* Create the TEMP table used to store parameter bindings */
3007static void bind_table_init(ShellState *p){
3008  int wrSchema = 0;
3009  int defensiveMode = 0;
3010  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3011  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3012  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3013  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3014  sqlite3_exec(p->db,
3015    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3016    "  key TEXT PRIMARY KEY,\n"
3017    "  value\n"
3018    ") WITHOUT ROWID;",
3019    0, 0, 0);
3020  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3021  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3022}
3023
3024/*
3025** Bind parameters on a prepared statement.
3026**
3027** Parameter bindings are taken from a TEMP table of the form:
3028**
3029**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3030**    WITHOUT ROWID;
3031**
3032** No bindings occur if this table does not exist.  The name of the table
3033** begins with "sqlite_" so that it will not collide with ordinary application
3034** tables.  The table must be in the TEMP schema.
3035*/
3036static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3037  int nVar;
3038  int i;
3039  int rc;
3040  sqlite3_stmt *pQ = 0;
3041
3042  nVar = sqlite3_bind_parameter_count(pStmt);
3043  if( nVar==0 ) return;  /* Nothing to do */
3044  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3045                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3046    return; /* Parameter table does not exist */
3047  }
3048  rc = sqlite3_prepare_v2(pArg->db,
3049          "SELECT value FROM temp.sqlite_parameters"
3050          " WHERE key=?1", -1, &pQ, 0);
3051  if( rc || pQ==0 ) return;
3052  for(i=1; i<=nVar; i++){
3053    char zNum[30];
3054    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3055    if( zVar==0 ){
3056      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3057      zVar = zNum;
3058    }
3059    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3060    if( sqlite3_step(pQ)==SQLITE_ROW ){
3061      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3062    }else{
3063      sqlite3_bind_null(pStmt, i);
3064    }
3065    sqlite3_reset(pQ);
3066  }
3067  sqlite3_finalize(pQ);
3068}
3069
3070/*
3071** UTF8 box-drawing characters.  Imagine box lines like this:
3072**
3073**           1
3074**           |
3075**       4 --+-- 2
3076**           |
3077**           3
3078**
3079** Each box characters has between 2 and 4 of the lines leading from
3080** the center.  The characters are here identified by the numbers of
3081** their corresponding lines.
3082*/
3083#define BOX_24   "\342\224\200"  /* U+2500 --- */
3084#define BOX_13   "\342\224\202"  /* U+2502  |  */
3085#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3086#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3087#define BOX_12   "\342\224\224"  /* U+2514  '- */
3088#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3089#define BOX_123  "\342\224\234"  /* U+251c  |- */
3090#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3091#define BOX_234  "\342\224\254"  /* U+252c -,- */
3092#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3093#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3094
3095/* Draw horizontal line N characters long using unicode box
3096** characters
3097*/
3098static void print_box_line(FILE *out, int N){
3099  const char zDash[] =
3100      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3101      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3102  const int nDash = sizeof(zDash) - 1;
3103  N *= 3;
3104  while( N>nDash ){
3105    utf8_printf(out, zDash);
3106    N -= nDash;
3107  }
3108  utf8_printf(out, "%.*s", N, zDash);
3109}
3110
3111/*
3112** Draw a horizontal separator for a MODE_Box table.
3113*/
3114static void print_box_row_separator(
3115  ShellState *p,
3116  int nArg,
3117  const char *zSep1,
3118  const char *zSep2,
3119  const char *zSep3
3120){
3121  int i;
3122  if( nArg>0 ){
3123    utf8_printf(p->out, "%s", zSep1);
3124    print_box_line(p->out, p->actualWidth[0]+2);
3125    for(i=1; i<nArg; i++){
3126      utf8_printf(p->out, "%s", zSep2);
3127      print_box_line(p->out, p->actualWidth[i]+2);
3128    }
3129    utf8_printf(p->out, "%s", zSep3);
3130  }
3131  fputs("\n", p->out);
3132}
3133
3134
3135
3136/*
3137** Run a prepared statement and output the result in one of the
3138** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3139** or MODE_Box.
3140**
3141** This is different from ordinary exec_prepared_stmt() in that
3142** it has to run the entire query and gather the results into memory
3143** first, in order to determine column widths, before providing
3144** any output.
3145*/
3146static void exec_prepared_stmt_columnar(
3147  ShellState *p,                        /* Pointer to ShellState */
3148  sqlite3_stmt *pStmt                   /* Statment to run */
3149){
3150  sqlite3_int64 nRow = 0;
3151  int nColumn = 0;
3152  char **azData = 0;
3153  sqlite3_int64 nAlloc = 0;
3154  const char *z;
3155  int rc;
3156  sqlite3_int64 i, nData;
3157  int j, nTotal, w, n;
3158  const char *colSep = 0;
3159  const char *rowSep = 0;
3160
3161  rc = sqlite3_step(pStmt);
3162  if( rc!=SQLITE_ROW ) return;
3163  nColumn = sqlite3_column_count(pStmt);
3164  nAlloc = nColumn*4;
3165  if( nAlloc<=0 ) nAlloc = 1;
3166  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3167  if( azData==0 ) shell_out_of_memory();
3168  for(i=0; i<nColumn; i++){
3169    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3170  }
3171  do{
3172    if( (nRow+2)*nColumn >= nAlloc ){
3173      nAlloc *= 2;
3174      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3175      if( azData==0 ) shell_out_of_memory();
3176    }
3177    nRow++;
3178    for(i=0; i<nColumn; i++){
3179      z = (const char*)sqlite3_column_text(pStmt,i);
3180      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3181    }
3182  }while( sqlite3_step(pStmt)==SQLITE_ROW );
3183  if( nColumn>p->nWidth ){
3184    p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
3185    if( p->colWidth==0 ) shell_out_of_memory();
3186    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3187    p->nWidth = nColumn;
3188    p->actualWidth = &p->colWidth[nColumn];
3189  }
3190  memset(p->actualWidth, 0, nColumn*sizeof(int));
3191  for(i=0; i<nColumn; i++){
3192    w = p->colWidth[i];
3193    if( w<0 ) w = -w;
3194    p->actualWidth[i] = w;
3195  }
3196  nTotal = nColumn*(nRow+1);
3197  for(i=0; i<nTotal; i++){
3198    z = azData[i];
3199    if( z==0 ) z = p->nullValue;
3200    n = strlenChar(z);
3201    j = i%nColumn;
3202    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3203  }
3204  if( seenInterrupt ) goto columnar_end;
3205  if( nColumn==0 ) goto columnar_end;
3206  switch( p->cMode ){
3207    case MODE_Column: {
3208      colSep = "  ";
3209      rowSep = "\n";
3210      if( p->showHeader ){
3211        for(i=0; i<nColumn; i++){
3212          w = p->actualWidth[i];
3213          if( p->colWidth[i]<0 ) w = -w;
3214          utf8_width_print(p->out, w, azData[i]);
3215          fputs(i==nColumn-1?"\n":"  ", p->out);
3216        }
3217        for(i=0; i<nColumn; i++){
3218          print_dashes(p->out, p->actualWidth[i]);
3219          fputs(i==nColumn-1?"\n":"  ", p->out);
3220        }
3221      }
3222      break;
3223    }
3224    case MODE_Table: {
3225      colSep = " | ";
3226      rowSep = " |\n";
3227      print_row_separator(p, nColumn, "+");
3228      fputs("| ", p->out);
3229      for(i=0; i<nColumn; i++){
3230        w = p->actualWidth[i];
3231        n = strlenChar(azData[i]);
3232        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3233        fputs(i==nColumn-1?" |\n":" | ", p->out);
3234      }
3235      print_row_separator(p, nColumn, "+");
3236      break;
3237    }
3238    case MODE_Markdown: {
3239      colSep = " | ";
3240      rowSep = " |\n";
3241      fputs("| ", p->out);
3242      for(i=0; i<nColumn; i++){
3243        w = p->actualWidth[i];
3244        n = strlenChar(azData[i]);
3245        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3246        fputs(i==nColumn-1?" |\n":" | ", p->out);
3247      }
3248      print_row_separator(p, nColumn, "|");
3249      break;
3250    }
3251    case MODE_Box: {
3252      colSep = " " BOX_13 " ";
3253      rowSep = " " BOX_13 "\n";
3254      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3255      utf8_printf(p->out, BOX_13 " ");
3256      for(i=0; i<nColumn; i++){
3257        w = p->actualWidth[i];
3258        n = strlenChar(azData[i]);
3259        utf8_printf(p->out, "%*s%s%*s%s",
3260            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3261            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3262      }
3263      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3264      break;
3265    }
3266  }
3267  for(i=nColumn, j=0; i<nTotal; i++, j++){
3268    if( j==0 && p->cMode!=MODE_Column ){
3269      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3270    }
3271    z = azData[i];
3272    if( z==0 ) z = p->nullValue;
3273    w = p->actualWidth[j];
3274    if( p->colWidth[j]<0 ) w = -w;
3275    utf8_width_print(p->out, w, z);
3276    if( j==nColumn-1 ){
3277      utf8_printf(p->out, "%s", rowSep);
3278      j = -1;
3279      if( seenInterrupt ) goto columnar_end;
3280    }else{
3281      utf8_printf(p->out, "%s", colSep);
3282    }
3283  }
3284  if( p->cMode==MODE_Table ){
3285    print_row_separator(p, nColumn, "+");
3286  }else if( p->cMode==MODE_Box ){
3287    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3288  }
3289columnar_end:
3290  if( seenInterrupt ){
3291    utf8_printf(p->out, "Interrupt\n");
3292  }
3293  nData = (nRow+1)*nColumn;
3294  for(i=0; i<nData; i++) free(azData[i]);
3295  sqlite3_free(azData);
3296}
3297
3298/*
3299** Run a prepared statement
3300*/
3301static void exec_prepared_stmt(
3302  ShellState *pArg,                                /* Pointer to ShellState */
3303  sqlite3_stmt *pStmt                              /* Statment to run */
3304){
3305  int rc;
3306
3307  if( pArg->cMode==MODE_Column
3308   || pArg->cMode==MODE_Table
3309   || pArg->cMode==MODE_Box
3310   || pArg->cMode==MODE_Markdown
3311  ){
3312    exec_prepared_stmt_columnar(pArg, pStmt);
3313    return;
3314  }
3315
3316  /* perform the first step.  this will tell us if we
3317  ** have a result set or not and how wide it is.
3318  */
3319  rc = sqlite3_step(pStmt);
3320  /* if we have a result set... */
3321  if( SQLITE_ROW == rc ){
3322    /* allocate space for col name ptr, value ptr, and type */
3323    int nCol = sqlite3_column_count(pStmt);
3324    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3325    if( !pData ){
3326      shell_out_of_memory();
3327    }else{
3328      char **azCols = (char **)pData;      /* Names of result columns */
3329      char **azVals = &azCols[nCol];       /* Results */
3330      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3331      int i, x;
3332      assert(sizeof(int) <= sizeof(char *));
3333      /* save off ptrs to column names */
3334      for(i=0; i<nCol; i++){
3335        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3336      }
3337      do{
3338        /* extract the data and data types */
3339        for(i=0; i<nCol; i++){
3340          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3341          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3342            azVals[i] = "";
3343          }else{
3344            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3345          }
3346          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3347            rc = SQLITE_NOMEM;
3348            break; /* from for */
3349          }
3350        } /* end for */
3351
3352        /* if data and types extracted successfully... */
3353        if( SQLITE_ROW == rc ){
3354          /* call the supplied callback with the result row data */
3355          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3356            rc = SQLITE_ABORT;
3357          }else{
3358            rc = sqlite3_step(pStmt);
3359          }
3360        }
3361      } while( SQLITE_ROW == rc );
3362      sqlite3_free(pData);
3363      if( pArg->cMode==MODE_Json ){
3364        fputs("]\n", pArg->out);
3365      }
3366    }
3367  }
3368}
3369
3370#ifndef SQLITE_OMIT_VIRTUALTABLE
3371/*
3372** This function is called to process SQL if the previous shell command
3373** was ".expert". It passes the SQL in the second argument directly to
3374** the sqlite3expert object.
3375**
3376** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3377** code. In this case, (*pzErr) may be set to point to a buffer containing
3378** an English language error message. It is the responsibility of the
3379** caller to eventually free this buffer using sqlite3_free().
3380*/
3381static int expertHandleSQL(
3382  ShellState *pState,
3383  const char *zSql,
3384  char **pzErr
3385){
3386  assert( pState->expert.pExpert );
3387  assert( pzErr==0 || *pzErr==0 );
3388  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3389}
3390
3391/*
3392** This function is called either to silently clean up the object
3393** created by the ".expert" command (if bCancel==1), or to generate a
3394** report from it and then clean it up (if bCancel==0).
3395**
3396** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3397** code. In this case, (*pzErr) may be set to point to a buffer containing
3398** an English language error message. It is the responsibility of the
3399** caller to eventually free this buffer using sqlite3_free().
3400*/
3401static int expertFinish(
3402  ShellState *pState,
3403  int bCancel,
3404  char **pzErr
3405){
3406  int rc = SQLITE_OK;
3407  sqlite3expert *p = pState->expert.pExpert;
3408  assert( p );
3409  assert( bCancel || pzErr==0 || *pzErr==0 );
3410  if( bCancel==0 ){
3411    FILE *out = pState->out;
3412    int bVerbose = pState->expert.bVerbose;
3413
3414    rc = sqlite3_expert_analyze(p, pzErr);
3415    if( rc==SQLITE_OK ){
3416      int nQuery = sqlite3_expert_count(p);
3417      int i;
3418
3419      if( bVerbose ){
3420        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3421        raw_printf(out, "-- Candidates -----------------------------\n");
3422        raw_printf(out, "%s\n", zCand);
3423      }
3424      for(i=0; i<nQuery; i++){
3425        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3426        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3427        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3428        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3429        if( bVerbose ){
3430          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3431          raw_printf(out, "%s\n\n", zSql);
3432        }
3433        raw_printf(out, "%s\n", zIdx);
3434        raw_printf(out, "%s\n", zEQP);
3435      }
3436    }
3437  }
3438  sqlite3_expert_destroy(p);
3439  pState->expert.pExpert = 0;
3440  return rc;
3441}
3442
3443/*
3444** Implementation of ".expert" dot command.
3445*/
3446static int expertDotCommand(
3447  ShellState *pState,             /* Current shell tool state */
3448  char **azArg,                   /* Array of arguments passed to dot command */
3449  int nArg                        /* Number of entries in azArg[] */
3450){
3451  int rc = SQLITE_OK;
3452  char *zErr = 0;
3453  int i;
3454  int iSample = 0;
3455
3456  assert( pState->expert.pExpert==0 );
3457  memset(&pState->expert, 0, sizeof(ExpertInfo));
3458
3459  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3460    char *z = azArg[i];
3461    int n;
3462    if( z[0]=='-' && z[1]=='-' ) z++;
3463    n = strlen30(z);
3464    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3465      pState->expert.bVerbose = 1;
3466    }
3467    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3468      if( i==(nArg-1) ){
3469        raw_printf(stderr, "option requires an argument: %s\n", z);
3470        rc = SQLITE_ERROR;
3471      }else{
3472        iSample = (int)integerValue(azArg[++i]);
3473        if( iSample<0 || iSample>100 ){
3474          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3475          rc = SQLITE_ERROR;
3476        }
3477      }
3478    }
3479    else{
3480      raw_printf(stderr, "unknown option: %s\n", z);
3481      rc = SQLITE_ERROR;
3482    }
3483  }
3484
3485  if( rc==SQLITE_OK ){
3486    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3487    if( pState->expert.pExpert==0 ){
3488      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3489      rc = SQLITE_ERROR;
3490    }else{
3491      sqlite3_expert_config(
3492          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3493      );
3494    }
3495  }
3496
3497  return rc;
3498}
3499#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3500
3501/*
3502** Execute a statement or set of statements.  Print
3503** any result rows/columns depending on the current mode
3504** set via the supplied callback.
3505**
3506** This is very similar to SQLite's built-in sqlite3_exec()
3507** function except it takes a slightly different callback
3508** and callback data argument.
3509*/
3510static int shell_exec(
3511  ShellState *pArg,                         /* Pointer to ShellState */
3512  const char *zSql,                         /* SQL to be evaluated */
3513  char **pzErrMsg                           /* Error msg written here */
3514){
3515  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3516  int rc = SQLITE_OK;             /* Return Code */
3517  int rc2;
3518  const char *zLeftover;          /* Tail of unprocessed SQL */
3519  sqlite3 *db = pArg->db;
3520
3521  if( pzErrMsg ){
3522    *pzErrMsg = NULL;
3523  }
3524
3525#ifndef SQLITE_OMIT_VIRTUALTABLE
3526  if( pArg->expert.pExpert ){
3527    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3528    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3529  }
3530#endif
3531
3532  while( zSql[0] && (SQLITE_OK == rc) ){
3533    static const char *zStmtSql;
3534    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3535    if( SQLITE_OK != rc ){
3536      if( pzErrMsg ){
3537        *pzErrMsg = save_err_msg(db, "in prepare, %s (%d)", rc);
3538      }
3539    }else{
3540      if( !pStmt ){
3541        /* this happens for a comment or white-space */
3542        zSql = zLeftover;
3543        while( IsSpace(zSql[0]) ) zSql++;
3544        continue;
3545      }
3546      zStmtSql = sqlite3_sql(pStmt);
3547      if( zStmtSql==0 ) zStmtSql = "";
3548      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3549
3550      /* save off the prepared statment handle and reset row count */
3551      if( pArg ){
3552        pArg->pStmt = pStmt;
3553        pArg->cnt = 0;
3554      }
3555
3556      /* echo the sql statement if echo on */
3557      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3558        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3559      }
3560
3561      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3562      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3563        sqlite3_stmt *pExplain;
3564        char *zEQP;
3565        int triggerEQP = 0;
3566        disable_debug_trace_modes();
3567        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3568        if( pArg->autoEQP>=AUTOEQP_trigger ){
3569          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3570        }
3571        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3572        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3573        if( rc==SQLITE_OK ){
3574          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3575            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3576            int iEqpId = sqlite3_column_int(pExplain, 0);
3577            int iParentId = sqlite3_column_int(pExplain, 1);
3578            if( zEQPLine==0 ) zEQPLine = "";
3579            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3580            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3581          }
3582          eqp_render(pArg);
3583        }
3584        sqlite3_finalize(pExplain);
3585        sqlite3_free(zEQP);
3586        if( pArg->autoEQP>=AUTOEQP_full ){
3587          /* Also do an EXPLAIN for ".eqp full" mode */
3588          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3589          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3590          if( rc==SQLITE_OK ){
3591            pArg->cMode = MODE_Explain;
3592            explain_data_prepare(pArg, pExplain);
3593            exec_prepared_stmt(pArg, pExplain);
3594            explain_data_delete(pArg);
3595          }
3596          sqlite3_finalize(pExplain);
3597          sqlite3_free(zEQP);
3598        }
3599        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3600          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3601          /* Reprepare pStmt before reactiving trace modes */
3602          sqlite3_finalize(pStmt);
3603          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3604          if( pArg ) pArg->pStmt = pStmt;
3605        }
3606        restore_debug_trace_modes();
3607      }
3608
3609      if( pArg ){
3610        pArg->cMode = pArg->mode;
3611        if( pArg->autoExplain ){
3612          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3613            pArg->cMode = MODE_Explain;
3614          }
3615          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3616            pArg->cMode = MODE_EQP;
3617          }
3618        }
3619
3620        /* If the shell is currently in ".explain" mode, gather the extra
3621        ** data required to add indents to the output.*/
3622        if( pArg->cMode==MODE_Explain ){
3623          explain_data_prepare(pArg, pStmt);
3624        }
3625      }
3626
3627      bind_prepared_stmt(pArg, pStmt);
3628      exec_prepared_stmt(pArg, pStmt);
3629      explain_data_delete(pArg);
3630      eqp_render(pArg);
3631
3632      /* print usage stats if stats on */
3633      if( pArg && pArg->statsOn ){
3634        display_stats(db, pArg, 0);
3635      }
3636
3637      /* print loop-counters if required */
3638      if( pArg && pArg->scanstatsOn ){
3639        display_scanstats(db, pArg);
3640      }
3641
3642      /* Finalize the statement just executed. If this fails, save a
3643      ** copy of the error message. Otherwise, set zSql to point to the
3644      ** next statement to execute. */
3645      rc2 = sqlite3_finalize(pStmt);
3646      if( rc!=SQLITE_NOMEM ) rc = rc2;
3647      if( rc==SQLITE_OK ){
3648        zSql = zLeftover;
3649        while( IsSpace(zSql[0]) ) zSql++;
3650      }else if( pzErrMsg ){
3651        *pzErrMsg = save_err_msg(db, "stepping, %s (%d)", rc);
3652      }
3653
3654      /* clear saved stmt handle */
3655      if( pArg ){
3656        pArg->pStmt = NULL;
3657      }
3658    }
3659  } /* end while */
3660
3661  return rc;
3662}
3663
3664/*
3665** Release memory previously allocated by tableColumnList().
3666*/
3667static void freeColumnList(char **azCol){
3668  int i;
3669  for(i=1; azCol[i]; i++){
3670    sqlite3_free(azCol[i]);
3671  }
3672  /* azCol[0] is a static string */
3673  sqlite3_free(azCol);
3674}
3675
3676/*
3677** Return a list of pointers to strings which are the names of all
3678** columns in table zTab.   The memory to hold the names is dynamically
3679** allocated and must be released by the caller using a subsequent call
3680** to freeColumnList().
3681**
3682** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3683** value that needs to be preserved, then azCol[0] is filled in with the
3684** name of the rowid column.
3685**
3686** The first regular column in the table is azCol[1].  The list is terminated
3687** by an entry with azCol[i]==0.
3688*/
3689static char **tableColumnList(ShellState *p, const char *zTab){
3690  char **azCol = 0;
3691  sqlite3_stmt *pStmt;
3692  char *zSql;
3693  int nCol = 0;
3694  int nAlloc = 0;
3695  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3696  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3697  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3698  int rc;
3699
3700  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3701  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3702  sqlite3_free(zSql);
3703  if( rc ) return 0;
3704  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3705    if( nCol>=nAlloc-2 ){
3706      nAlloc = nAlloc*2 + nCol + 10;
3707      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3708      if( azCol==0 ) shell_out_of_memory();
3709    }
3710    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3711    if( sqlite3_column_int(pStmt, 5) ){
3712      nPK++;
3713      if( nPK==1
3714       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3715                          "INTEGER")==0
3716      ){
3717        isIPK = 1;
3718      }else{
3719        isIPK = 0;
3720      }
3721    }
3722  }
3723  sqlite3_finalize(pStmt);
3724  if( azCol==0 ) return 0;
3725  azCol[0] = 0;
3726  azCol[nCol+1] = 0;
3727
3728  /* The decision of whether or not a rowid really needs to be preserved
3729  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3730  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3731  ** rowids on tables where the rowid is inaccessible because there are other
3732  ** columns in the table named "rowid", "_rowid_", and "oid".
3733  */
3734  if( preserveRowid && isIPK ){
3735    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3736    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3737    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3738    ** ROWID aliases.  To distinguish these cases, check to see if
3739    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3740    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3741    */
3742    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3743                           " WHERE origin='pk'", zTab);
3744    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3745    sqlite3_free(zSql);
3746    if( rc ){
3747      freeColumnList(azCol);
3748      return 0;
3749    }
3750    rc = sqlite3_step(pStmt);
3751    sqlite3_finalize(pStmt);
3752    preserveRowid = rc==SQLITE_ROW;
3753  }
3754  if( preserveRowid ){
3755    /* Only preserve the rowid if we can find a name to use for the
3756    ** rowid */
3757    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3758    int i, j;
3759    for(j=0; j<3; j++){
3760      for(i=1; i<=nCol; i++){
3761        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3762      }
3763      if( i>nCol ){
3764        /* At this point, we know that azRowid[j] is not the name of any
3765        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3766        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3767        ** tables will fail this last check */
3768        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3769        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3770        break;
3771      }
3772    }
3773  }
3774  return azCol;
3775}
3776
3777/*
3778** Toggle the reverse_unordered_selects setting.
3779*/
3780static void toggleSelectOrder(sqlite3 *db){
3781  sqlite3_stmt *pStmt = 0;
3782  int iSetting = 0;
3783  char zStmt[100];
3784  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3785  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3786    iSetting = sqlite3_column_int(pStmt, 0);
3787  }
3788  sqlite3_finalize(pStmt);
3789  sqlite3_snprintf(sizeof(zStmt), zStmt,
3790       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3791  sqlite3_exec(db, zStmt, 0, 0, 0);
3792}
3793
3794/*
3795** This is a different callback routine used for dumping the database.
3796** Each row received by this callback consists of a table name,
3797** the table type ("index" or "table") and SQL to create the table.
3798** This routine should print text sufficient to recreate the table.
3799*/
3800static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3801  int rc;
3802  const char *zTable;
3803  const char *zType;
3804  const char *zSql;
3805  ShellState *p = (ShellState *)pArg;
3806  int dataOnly;
3807  int noSys;
3808
3809  UNUSED_PARAMETER(azNotUsed);
3810  if( nArg!=3 || azArg==0 ) return 0;
3811  zTable = azArg[0];
3812  zType = azArg[1];
3813  zSql = azArg[2];
3814  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
3815  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
3816
3817  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
3818    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3819  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
3820    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3821  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3822    return 0;
3823  }else if( dataOnly ){
3824    /* no-op */
3825  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3826    char *zIns;
3827    if( !p->writableSchema ){
3828      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3829      p->writableSchema = 1;
3830    }
3831    zIns = sqlite3_mprintf(
3832       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3833       "VALUES('table','%q','%q',0,'%q');",
3834       zTable, zTable, zSql);
3835    utf8_printf(p->out, "%s\n", zIns);
3836    sqlite3_free(zIns);
3837    return 0;
3838  }else{
3839    printSchemaLine(p->out, zSql, ";\n");
3840  }
3841
3842  if( strcmp(zType, "table")==0 ){
3843    ShellText sSelect;
3844    ShellText sTable;
3845    char **azCol;
3846    int i;
3847    char *savedDestTable;
3848    int savedMode;
3849
3850    azCol = tableColumnList(p, zTable);
3851    if( azCol==0 ){
3852      p->nErr++;
3853      return 0;
3854    }
3855
3856    /* Always quote the table name, even if it appears to be pure ascii,
3857    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3858    initText(&sTable);
3859    appendText(&sTable, zTable, quoteChar(zTable));
3860    /* If preserving the rowid, add a column list after the table name.
3861    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3862    ** instead of the usual "INSERT INTO tab VALUES(...)".
3863    */
3864    if( azCol[0] ){
3865      appendText(&sTable, "(", 0);
3866      appendText(&sTable, azCol[0], 0);
3867      for(i=1; azCol[i]; i++){
3868        appendText(&sTable, ",", 0);
3869        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3870      }
3871      appendText(&sTable, ")", 0);
3872    }
3873
3874    /* Build an appropriate SELECT statement */
3875    initText(&sSelect);
3876    appendText(&sSelect, "SELECT ", 0);
3877    if( azCol[0] ){
3878      appendText(&sSelect, azCol[0], 0);
3879      appendText(&sSelect, ",", 0);
3880    }
3881    for(i=1; azCol[i]; i++){
3882      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3883      if( azCol[i+1] ){
3884        appendText(&sSelect, ",", 0);
3885      }
3886    }
3887    freeColumnList(azCol);
3888    appendText(&sSelect, " FROM ", 0);
3889    appendText(&sSelect, zTable, quoteChar(zTable));
3890
3891    savedDestTable = p->zDestTable;
3892    savedMode = p->mode;
3893    p->zDestTable = sTable.z;
3894    p->mode = p->cMode = MODE_Insert;
3895    rc = shell_exec(p, sSelect.z, 0);
3896    if( (rc&0xff)==SQLITE_CORRUPT ){
3897      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3898      toggleSelectOrder(p->db);
3899      shell_exec(p, sSelect.z, 0);
3900      toggleSelectOrder(p->db);
3901    }
3902    p->zDestTable = savedDestTable;
3903    p->mode = savedMode;
3904    freeText(&sTable);
3905    freeText(&sSelect);
3906    if( rc ) p->nErr++;
3907  }
3908  return 0;
3909}
3910
3911/*
3912** Run zQuery.  Use dump_callback() as the callback routine so that
3913** the contents of the query are output as SQL statements.
3914**
3915** If we get a SQLITE_CORRUPT error, rerun the query after appending
3916** "ORDER BY rowid DESC" to the end.
3917*/
3918static int run_schema_dump_query(
3919  ShellState *p,
3920  const char *zQuery
3921){
3922  int rc;
3923  char *zErr = 0;
3924  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3925  if( rc==SQLITE_CORRUPT ){
3926    char *zQ2;
3927    int len = strlen30(zQuery);
3928    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3929    if( zErr ){
3930      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3931      sqlite3_free(zErr);
3932      zErr = 0;
3933    }
3934    zQ2 = malloc( len+100 );
3935    if( zQ2==0 ) return rc;
3936    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3937    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3938    if( rc ){
3939      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3940    }else{
3941      rc = SQLITE_CORRUPT;
3942    }
3943    sqlite3_free(zErr);
3944    free(zQ2);
3945  }
3946  return rc;
3947}
3948
3949/*
3950** Text of help messages.
3951**
3952** The help text for each individual command begins with a line that starts
3953** with ".".  Subsequent lines are supplimental information.
3954**
3955** There must be two or more spaces between the end of the command and the
3956** start of the description of what that command does.
3957*/
3958static const char *(azHelp[]) = {
3959#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3960  ".archive ...             Manage SQL archives",
3961  "   Each command must have exactly one of the following options:",
3962  "     -c, --create               Create a new archive",
3963  "     -u, --update               Add or update files with changed mtime",
3964  "     -i, --insert               Like -u but always add even if unchanged",
3965  "     -r, --remove               Remove files from archive",
3966  "     -t, --list                 List contents of archive",
3967  "     -x, --extract              Extract files from archive",
3968  "   Optional arguments:",
3969  "     -v, --verbose              Print each filename as it is processed",
3970  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3971  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3972  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3973  "     -g, --glob                 Use glob matching for names in archive",
3974  "     -n, --dryrun               Show the SQL that would have occurred",
3975  "   Examples:",
3976  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3977  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3978  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3979  "   See also:",
3980  "      http://sqlite.org/cli.html#sqlite_archive_support",
3981#endif
3982#ifndef SQLITE_OMIT_AUTHORIZATION
3983  ".auth ON|OFF             Show authorizer callbacks",
3984#endif
3985  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3986  "       --append            Use the appendvfs",
3987  "       --async             Write to FILE without journal and fsync()",
3988  ".bail on|off             Stop after hitting an error.  Default OFF",
3989  ".binary on|off           Turn binary output on or off.  Default OFF",
3990  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3991  ".changes on|off          Show number of rows changed by SQL",
3992  ".check GLOB              Fail if output since .testcase does not match",
3993  ".clone NEWDB             Clone data into NEWDB from the existing database",
3994  ".connection [close] [#]  Open or close an auxiliary database connection",
3995  ".databases               List names and files of attached databases",
3996  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3997  ".dbinfo ?DB?             Show status information about the database",
3998  ".dump ?OBJECTS?          Render database content as SQL",
3999  "   Options:",
4000  "     --data-only            Output only INSERT statements",
4001  "     --newlines             Allow unescaped newline characters in output",
4002  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4003  "     --preserve-rowids      Include ROWID values in the output",
4004  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4005  "   Additional LIKE patterns can be given in subsequent arguments",
4006  ".echo on|off             Turn command echo on or off",
4007  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4008  "   Other Modes:",
4009#ifdef SQLITE_DEBUG
4010  "      test                  Show raw EXPLAIN QUERY PLAN output",
4011  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4012#endif
4013  "      trigger               Like \"full\" but also show trigger bytecode",
4014  ".excel                   Display the output of next command in spreadsheet",
4015  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4016  ".exit ?CODE?             Exit this program with return-code CODE",
4017  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4018  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4019  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4020  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4021  "   --help                  Show CMD details",
4022  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4023  ".headers on|off          Turn display of headers on or off",
4024  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4025  ".import FILE TABLE       Import data from FILE into TABLE",
4026  "   Options:",
4027  "     --ascii               Use \\037 and \\036 as column and row separators",
4028  "     --csv                 Use , and \\n as column and row separators",
4029  "     --skip N              Skip the first N rows of input",
4030  "     -v                    \"Verbose\" - increase auxiliary output",
4031  "   Notes:",
4032  "     *  If TABLE does not exist, it is created.  The first row of input",
4033  "        determines the column names.",
4034  "     *  If neither --csv or --ascii are used, the input mode is derived",
4035  "        from the \".mode\" output mode",
4036  "     *  If FILE begins with \"|\" then it is a command that generates the",
4037  "        input text.",
4038#ifndef SQLITE_OMIT_TEST_CONTROL
4039  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4040#endif
4041  ".indexes ?TABLE?         Show names of indexes",
4042  "                           If TABLE is specified, only show indexes for",
4043  "                           tables matching TABLE using the LIKE operator.",
4044#ifdef SQLITE_ENABLE_IOTRACE
4045  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4046#endif
4047  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4048  ".lint OPTIONS            Report potential schema issues.",
4049  "     Options:",
4050  "        fkey-indexes     Find missing foreign key indexes",
4051#ifndef SQLITE_OMIT_LOAD_EXTENSION
4052  ".load FILE ?ENTRY?       Load an extension library",
4053#endif
4054  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4055  ".mode MODE ?TABLE?       Set output mode",
4056  "   MODE is one of:",
4057  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
4058  "     box       Tables using unicode box-drawing characters",
4059  "     csv       Comma-separated values",
4060  "     column    Output in columns.  (See .width)",
4061  "     html      HTML <table> code",
4062  "     insert    SQL insert statements for TABLE",
4063  "     json      Results in a JSON array",
4064  "     line      One value per line",
4065  "     list      Values delimited by \"|\"",
4066  "     markdown  Markdown table format",
4067  "     quote     Escape answers as for SQL",
4068  "     table     ASCII-art table",
4069  "     tabs      Tab-separated values",
4070  "     tcl       TCL list elements",
4071  ".nonce STRING            Disable safe mode for one command if the nonce matches",
4072  ".nullvalue STRING        Use STRING in place of NULL values",
4073  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4074  "     If FILE begins with '|' then open as a pipe",
4075  "       --bom  Put a UTF8 byte-order mark at the beginning",
4076  "       -e     Send output to the system text editor",
4077  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4078#ifdef SQLITE_DEBUG
4079  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
4080#endif
4081  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4082  "     Options:",
4083  "        --append        Use appendvfs to append database to the end of FILE",
4084#ifndef SQLITE_OMIT_DESERIALIZE
4085  "        --deserialize   Load into memory using sqlite3_deserialize()",
4086  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4087  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4088#endif
4089  "        --new           Initialize FILE to an empty database",
4090  "        --nofollow      Do not follow symbolic links",
4091  "        --readonly      Open FILE readonly",
4092  "        --zip           FILE is a ZIP archive",
4093  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4094  "   If FILE begins with '|' then open it as a pipe.",
4095  "   Options:",
4096  "     --bom                 Prefix output with a UTF8 byte-order mark",
4097  "     -e                    Send output to the system text editor",
4098  "     -x                    Send output as CSV to a spreadsheet",
4099  ".parameter CMD ...       Manage SQL parameter bindings",
4100  "   clear                   Erase all bindings",
4101  "   init                    Initialize the TEMP table that holds bindings",
4102  "   list                    List the current parameter bindings",
4103  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4104  "                           PARAMETER should start with one of: $ : @ ?",
4105  "   unset PARAMETER         Remove PARAMETER from the binding table",
4106  ".print STRING...         Print literal STRING",
4107#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4108  ".progress N              Invoke progress handler after every N opcodes",
4109  "   --limit N                 Interrupt after N progress callbacks",
4110  "   --once                    Do no more than one progress interrupt",
4111  "   --quiet|-q                No output except at interrupts",
4112  "   --reset                   Reset the count for each input and interrupt",
4113#endif
4114  ".prompt MAIN CONTINUE    Replace the standard prompts",
4115  ".quit                    Exit this program",
4116  ".read FILE               Read input from FILE",
4117#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4118  ".recover                 Recover as much data as possible from corrupt db.",
4119  "   --freelist-corrupt       Assume the freelist is corrupt",
4120  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4121  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4122  "   --no-rowids              Do not attempt to recover rowid values",
4123  "                            that are not also INTEGER PRIMARY KEYs",
4124#endif
4125  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4126  ".save FILE               Write in-memory database into FILE",
4127  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4128  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4129  "   Options:",
4130  "      --indent             Try to pretty-print the schema",
4131  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4132  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4133  "    Options:",
4134  "       --init               Create a new SELFTEST table",
4135  "       -v                   Verbose output",
4136  ".separator COL ?ROW?     Change the column and row separators",
4137#if defined(SQLITE_ENABLE_SESSION)
4138  ".session ?NAME? CMD ...  Create or control sessions",
4139  "   Subcommands:",
4140  "     attach TABLE             Attach TABLE",
4141  "     changeset FILE           Write a changeset into FILE",
4142  "     close                    Close one session",
4143  "     enable ?BOOLEAN?         Set or query the enable bit",
4144  "     filter GLOB...           Reject tables matching GLOBs",
4145  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4146  "     isempty                  Query whether the session is empty",
4147  "     list                     List currently open session names",
4148  "     open DB NAME             Open a new session on DB",
4149  "     patchset FILE            Write a patchset into FILE",
4150  "   If ?NAME? is omitted, the first defined session is used.",
4151#endif
4152  ".sha3sum ...             Compute a SHA3 hash of database content",
4153  "    Options:",
4154  "      --schema              Also hash the sqlite_schema table",
4155  "      --sha3-224            Use the sha3-224 algorithm",
4156  "      --sha3-256            Use the sha3-256 algorithm (default)",
4157  "      --sha3-384            Use the sha3-384 algorithm",
4158  "      --sha3-512            Use the sha3-512 algorithm",
4159  "    Any other argument is a LIKE pattern for tables to hash",
4160#ifndef SQLITE_NOHAVE_SYSTEM
4161  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4162#endif
4163  ".show                    Show the current values for various settings",
4164  ".stats ?ARG?             Show stats or turn stats on or off",
4165  "   off                      Turn off automatic stat display",
4166  "   on                       Turn on automatic stat display",
4167  "   stmt                     Show statement stats",
4168  "   vmstep                   Show the virtual machine step count only",
4169#ifndef SQLITE_NOHAVE_SYSTEM
4170  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4171#endif
4172  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4173  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4174  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4175  "                           Run \".testctrl\" with no arguments for details",
4176  ".timeout MS              Try opening locked tables for MS milliseconds",
4177  ".timer on|off            Turn SQL timer on or off",
4178#ifndef SQLITE_OMIT_TRACE
4179  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4180  "    FILE                    Send output to FILE",
4181  "    stdout                  Send output to stdout",
4182  "    stderr                  Send output to stderr",
4183  "    off                     Disable tracing",
4184  "    --expanded              Expand query parameters",
4185#ifdef SQLITE_ENABLE_NORMALIZE
4186  "    --normalized            Normal the SQL statements",
4187#endif
4188  "    --plain                 Show SQL as it is input",
4189  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4190  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4191  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4192  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4193#endif /* SQLITE_OMIT_TRACE */
4194#ifdef SQLITE_DEBUG
4195  ".unmodule NAME ...       Unregister virtual table modules",
4196  "    --allexcept             Unregister everything except those named",
4197#endif
4198  ".vfsinfo ?AUX?           Information about the top-level VFS",
4199  ".vfslist                 List all available VFSes",
4200  ".vfsname ?AUX?           Print the name of the VFS stack",
4201  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4202  "     Negative values right-justify",
4203};
4204
4205/*
4206** Output help text.
4207**
4208** zPattern describes the set of commands for which help text is provided.
4209** If zPattern is NULL, then show all commands, but only give a one-line
4210** description of each.
4211**
4212** Return the number of matches.
4213*/
4214static int showHelp(FILE *out, const char *zPattern){
4215  int i = 0;
4216  int j = 0;
4217  int n = 0;
4218  char *zPat;
4219  if( zPattern==0
4220   || zPattern[0]=='0'
4221   || strcmp(zPattern,"-a")==0
4222   || strcmp(zPattern,"-all")==0
4223   || strcmp(zPattern,"--all")==0
4224  ){
4225    /* Show all commands, but only one line per command */
4226    if( zPattern==0 ) zPattern = "";
4227    for(i=0; i<ArraySize(azHelp); i++){
4228      if( azHelp[i][0]=='.' || zPattern[0] ){
4229        utf8_printf(out, "%s\n", azHelp[i]);
4230        n++;
4231      }
4232    }
4233  }else{
4234    /* Look for commands that for which zPattern is an exact prefix */
4235    zPat = sqlite3_mprintf(".%s*", zPattern);
4236    for(i=0; i<ArraySize(azHelp); i++){
4237      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4238        utf8_printf(out, "%s\n", azHelp[i]);
4239        j = i+1;
4240        n++;
4241      }
4242    }
4243    sqlite3_free(zPat);
4244    if( n ){
4245      if( n==1 ){
4246        /* when zPattern is a prefix of exactly one command, then include the
4247        ** details of that command, which should begin at offset j */
4248        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4249          utf8_printf(out, "%s\n", azHelp[j]);
4250          j++;
4251        }
4252      }
4253      return n;
4254    }
4255    /* Look for commands that contain zPattern anywhere.  Show the complete
4256    ** text of all commands that match. */
4257    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4258    for(i=0; i<ArraySize(azHelp); i++){
4259      if( azHelp[i][0]=='.' ) j = i;
4260      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4261        utf8_printf(out, "%s\n", azHelp[j]);
4262        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4263          j++;
4264          utf8_printf(out, "%s\n", azHelp[j]);
4265        }
4266        i = j;
4267        n++;
4268      }
4269    }
4270    sqlite3_free(zPat);
4271  }
4272  return n;
4273}
4274
4275/* Forward reference */
4276static int process_input(ShellState *p);
4277
4278/*
4279** Read the content of file zName into memory obtained from sqlite3_malloc64()
4280** and return a pointer to the buffer. The caller is responsible for freeing
4281** the memory.
4282**
4283** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4284** read.
4285**
4286** For convenience, a nul-terminator byte is always appended to the data read
4287** from the file before the buffer is returned. This byte is not included in
4288** the final value of (*pnByte), if applicable.
4289**
4290** NULL is returned if any error is encountered. The final value of *pnByte
4291** is undefined in this case.
4292*/
4293static char *readFile(const char *zName, int *pnByte){
4294  FILE *in = fopen(zName, "rb");
4295  long nIn;
4296  size_t nRead;
4297  char *pBuf;
4298  if( in==0 ) return 0;
4299  fseek(in, 0, SEEK_END);
4300  nIn = ftell(in);
4301  rewind(in);
4302  pBuf = sqlite3_malloc64( nIn+1 );
4303  if( pBuf==0 ){ fclose(in); return 0; }
4304  nRead = fread(pBuf, nIn, 1, in);
4305  fclose(in);
4306  if( nRead!=1 ){
4307    sqlite3_free(pBuf);
4308    return 0;
4309  }
4310  pBuf[nIn] = 0;
4311  if( pnByte ) *pnByte = nIn;
4312  return pBuf;
4313}
4314
4315#if defined(SQLITE_ENABLE_SESSION)
4316/*
4317** Close a single OpenSession object and release all of its associated
4318** resources.
4319*/
4320static void session_close(OpenSession *pSession){
4321  int i;
4322  sqlite3session_delete(pSession->p);
4323  sqlite3_free(pSession->zName);
4324  for(i=0; i<pSession->nFilter; i++){
4325    sqlite3_free(pSession->azFilter[i]);
4326  }
4327  sqlite3_free(pSession->azFilter);
4328  memset(pSession, 0, sizeof(OpenSession));
4329}
4330#endif
4331
4332/*
4333** Close all OpenSession objects and release all associated resources.
4334*/
4335#if defined(SQLITE_ENABLE_SESSION)
4336static void session_close_all(ShellState *p, int i){
4337  int j;
4338  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4339  for(j=0; j<pAuxDb->nSession; j++){
4340    session_close(&pAuxDb->aSession[j]);
4341  }
4342  pAuxDb->nSession = 0;
4343}
4344#else
4345# define session_close_all(X,Y)
4346#endif
4347
4348/*
4349** Implementation of the xFilter function for an open session.  Omit
4350** any tables named by ".session filter" but let all other table through.
4351*/
4352#if defined(SQLITE_ENABLE_SESSION)
4353static int session_filter(void *pCtx, const char *zTab){
4354  OpenSession *pSession = (OpenSession*)pCtx;
4355  int i;
4356  for(i=0; i<pSession->nFilter; i++){
4357    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4358  }
4359  return 1;
4360}
4361#endif
4362
4363/*
4364** Try to deduce the type of file for zName based on its content.  Return
4365** one of the SHELL_OPEN_* constants.
4366**
4367** If the file does not exist or is empty but its name looks like a ZIP
4368** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4369** Otherwise, assume an ordinary database regardless of the filename if
4370** the type cannot be determined from content.
4371*/
4372int deduceDatabaseType(const char *zName, int dfltZip){
4373  FILE *f = fopen(zName, "rb");
4374  size_t n;
4375  int rc = SHELL_OPEN_UNSPEC;
4376  char zBuf[100];
4377  if( f==0 ){
4378    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4379       return SHELL_OPEN_ZIPFILE;
4380    }else{
4381       return SHELL_OPEN_NORMAL;
4382    }
4383  }
4384  n = fread(zBuf, 16, 1, f);
4385  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4386    fclose(f);
4387    return SHELL_OPEN_NORMAL;
4388  }
4389  fseek(f, -25, SEEK_END);
4390  n = fread(zBuf, 25, 1, f);
4391  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4392    rc = SHELL_OPEN_APPENDVFS;
4393  }else{
4394    fseek(f, -22, SEEK_END);
4395    n = fread(zBuf, 22, 1, f);
4396    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4397       && zBuf[3]==0x06 ){
4398      rc = SHELL_OPEN_ZIPFILE;
4399    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4400      rc = SHELL_OPEN_ZIPFILE;
4401    }
4402  }
4403  fclose(f);
4404  return rc;
4405}
4406
4407#ifndef SQLITE_OMIT_DESERIALIZE
4408/*
4409** Reconstruct an in-memory database using the output from the "dbtotxt"
4410** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4411** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4412*/
4413static unsigned char *readHexDb(ShellState *p, int *pnData){
4414  unsigned char *a = 0;
4415  int nLine;
4416  int n = 0;
4417  int pgsz = 0;
4418  int iOffset = 0;
4419  int j, k;
4420  int rc;
4421  FILE *in;
4422  const char *zDbFilename = p->pAuxDb->zDbFilename;
4423  unsigned int x[16];
4424  char zLine[1000];
4425  if( zDbFilename ){
4426    in = fopen(zDbFilename, "r");
4427    if( in==0 ){
4428      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4429      return 0;
4430    }
4431    nLine = 0;
4432  }else{
4433    in = p->in;
4434    nLine = p->lineno;
4435    if( in==0 ) in = stdin;
4436  }
4437  *pnData = 0;
4438  nLine++;
4439  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4440  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4441  if( rc!=2 ) goto readHexDb_error;
4442  if( n<0 ) goto readHexDb_error;
4443  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4444  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4445  a = sqlite3_malloc( n ? n : 1 );
4446  if( a==0 ){
4447    utf8_printf(stderr, "Out of memory!\n");
4448    goto readHexDb_error;
4449  }
4450  memset(a, 0, n);
4451  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4452    utf8_printf(stderr, "invalid pagesize\n");
4453    goto readHexDb_error;
4454  }
4455  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4456    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4457    if( rc==2 ){
4458      iOffset = k;
4459      continue;
4460    }
4461    if( strncmp(zLine, "| end ", 6)==0 ){
4462      break;
4463    }
4464    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4465                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4466                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4467    if( rc==17 ){
4468      k = iOffset+j;
4469      if( k+16<=n && k>=0 ){
4470        int ii;
4471        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4472      }
4473    }
4474  }
4475  *pnData = n;
4476  if( in!=p->in ){
4477    fclose(in);
4478  }else{
4479    p->lineno = nLine;
4480  }
4481  return a;
4482
4483readHexDb_error:
4484  if( in!=p->in ){
4485    fclose(in);
4486  }else{
4487    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4488      nLine++;
4489      if(strncmp(zLine, "| end ", 6)==0 ) break;
4490    }
4491    p->lineno = nLine;
4492  }
4493  sqlite3_free(a);
4494  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4495  return 0;
4496}
4497#endif /* SQLITE_OMIT_DESERIALIZE */
4498
4499/*
4500** Scalar function "shell_int32". The first argument to this function
4501** must be a blob. The second a non-negative integer. This function
4502** reads and returns a 32-bit big-endian integer from byte
4503** offset (4*<arg2>) of the blob.
4504*/
4505static void shellInt32(
4506  sqlite3_context *context,
4507  int argc,
4508  sqlite3_value **argv
4509){
4510  const unsigned char *pBlob;
4511  int nBlob;
4512  int iInt;
4513
4514  UNUSED_PARAMETER(argc);
4515  nBlob = sqlite3_value_bytes(argv[0]);
4516  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4517  iInt = sqlite3_value_int(argv[1]);
4518
4519  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4520    const unsigned char *a = &pBlob[iInt*4];
4521    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4522                       + ((sqlite3_int64)a[1]<<16)
4523                       + ((sqlite3_int64)a[2]<< 8)
4524                       + ((sqlite3_int64)a[3]<< 0);
4525    sqlite3_result_int64(context, iVal);
4526  }
4527}
4528
4529/*
4530** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4531** using "..." with internal double-quote characters doubled.
4532*/
4533static void shellIdQuote(
4534  sqlite3_context *context,
4535  int argc,
4536  sqlite3_value **argv
4537){
4538  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4539  UNUSED_PARAMETER(argc);
4540  if( zName ){
4541    char *z = sqlite3_mprintf("\"%w\"", zName);
4542    sqlite3_result_text(context, z, -1, sqlite3_free);
4543  }
4544}
4545
4546/*
4547** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4548*/
4549static void shellUSleepFunc(
4550  sqlite3_context *context,
4551  int argcUnused,
4552  sqlite3_value **argv
4553){
4554  int sleep = sqlite3_value_int(argv[0]);
4555  (void)argcUnused;
4556  sqlite3_sleep(sleep/1000);
4557  sqlite3_result_int(context, sleep);
4558}
4559
4560/*
4561** Scalar function "shell_escape_crnl" used by the .recover command.
4562** The argument passed to this function is the output of built-in
4563** function quote(). If the first character of the input is "'",
4564** indicating that the value passed to quote() was a text value,
4565** then this function searches the input for "\n" and "\r" characters
4566** and adds a wrapper similar to the following:
4567**
4568**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4569**
4570** Or, if the first character of the input is not "'", then a copy
4571** of the input is returned.
4572*/
4573static void shellEscapeCrnl(
4574  sqlite3_context *context,
4575  int argc,
4576  sqlite3_value **argv
4577){
4578  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4579  UNUSED_PARAMETER(argc);
4580  if( zText[0]=='\'' ){
4581    int nText = sqlite3_value_bytes(argv[0]);
4582    int i;
4583    char zBuf1[20];
4584    char zBuf2[20];
4585    const char *zNL = 0;
4586    const char *zCR = 0;
4587    int nCR = 0;
4588    int nNL = 0;
4589
4590    for(i=0; zText[i]; i++){
4591      if( zNL==0 && zText[i]=='\n' ){
4592        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4593        nNL = (int)strlen(zNL);
4594      }
4595      if( zCR==0 && zText[i]=='\r' ){
4596        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4597        nCR = (int)strlen(zCR);
4598      }
4599    }
4600
4601    if( zNL || zCR ){
4602      int iOut = 0;
4603      i64 nMax = (nNL > nCR) ? nNL : nCR;
4604      i64 nAlloc = nMax * nText + (nMax+64)*2;
4605      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4606      if( zOut==0 ){
4607        sqlite3_result_error_nomem(context);
4608        return;
4609      }
4610
4611      if( zNL && zCR ){
4612        memcpy(&zOut[iOut], "replace(replace(", 16);
4613        iOut += 16;
4614      }else{
4615        memcpy(&zOut[iOut], "replace(", 8);
4616        iOut += 8;
4617      }
4618      for(i=0; zText[i]; i++){
4619        if( zText[i]=='\n' ){
4620          memcpy(&zOut[iOut], zNL, nNL);
4621          iOut += nNL;
4622        }else if( zText[i]=='\r' ){
4623          memcpy(&zOut[iOut], zCR, nCR);
4624          iOut += nCR;
4625        }else{
4626          zOut[iOut] = zText[i];
4627          iOut++;
4628        }
4629      }
4630
4631      if( zNL ){
4632        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4633        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4634        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4635      }
4636      if( zCR ){
4637        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4638        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4639        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4640      }
4641
4642      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4643      sqlite3_free(zOut);
4644      return;
4645    }
4646  }
4647
4648  sqlite3_result_value(context, argv[0]);
4649}
4650
4651/* Flags for open_db().
4652**
4653** The default behavior of open_db() is to exit(1) if the database fails to
4654** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4655** but still returns without calling exit.
4656**
4657** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4658** ZIP archive if the file does not exist or is empty and its name matches
4659** the *.zip pattern.
4660*/
4661#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4662#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4663
4664/*
4665** Make sure the database is open.  If it is not, then open it.  If
4666** the database fails to open, print an error message and exit.
4667*/
4668static void open_db(ShellState *p, int openFlags){
4669  if( p->db==0 ){
4670    const char *zDbFilename = p->pAuxDb->zDbFilename;
4671    if( p->openMode==SHELL_OPEN_UNSPEC ){
4672      if( zDbFilename==0 || zDbFilename[0]==0 ){
4673        p->openMode = SHELL_OPEN_NORMAL;
4674      }else{
4675        p->openMode = (u8)deduceDatabaseType(zDbFilename,
4676                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4677      }
4678    }
4679    switch( p->openMode ){
4680      case SHELL_OPEN_APPENDVFS: {
4681        sqlite3_open_v2(zDbFilename, &p->db,
4682           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4683        break;
4684      }
4685      case SHELL_OPEN_HEXDB:
4686      case SHELL_OPEN_DESERIALIZE: {
4687        sqlite3_open(0, &p->db);
4688        break;
4689      }
4690      case SHELL_OPEN_ZIPFILE: {
4691        sqlite3_open(":memory:", &p->db);
4692        break;
4693      }
4694      case SHELL_OPEN_READONLY: {
4695        sqlite3_open_v2(zDbFilename, &p->db,
4696            SQLITE_OPEN_READONLY|p->openFlags, 0);
4697        break;
4698      }
4699      case SHELL_OPEN_UNSPEC:
4700      case SHELL_OPEN_NORMAL: {
4701        sqlite3_open_v2(zDbFilename, &p->db,
4702           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4703        break;
4704      }
4705    }
4706    globalDb = p->db;
4707    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4708      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4709          zDbFilename, sqlite3_errmsg(p->db));
4710      if( openFlags & OPEN_DB_KEEPALIVE ){
4711        sqlite3_open(":memory:", &p->db);
4712        return;
4713      }
4714      exit(1);
4715    }
4716#ifndef SQLITE_OMIT_LOAD_EXTENSION
4717    sqlite3_enable_load_extension(p->db, 1);
4718#endif
4719    sqlite3_fileio_init(p->db, 0, 0);
4720    sqlite3_shathree_init(p->db, 0, 0);
4721    sqlite3_completion_init(p->db, 0, 0);
4722    sqlite3_uint_init(p->db, 0, 0);
4723    sqlite3_decimal_init(p->db, 0, 0);
4724    sqlite3_regexp_init(p->db, 0, 0);
4725    sqlite3_ieee_init(p->db, 0, 0);
4726    sqlite3_series_init(p->db, 0, 0);
4727#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4728    sqlite3_dbdata_init(p->db, 0, 0);
4729#endif
4730#ifdef SQLITE_HAVE_ZLIB
4731    sqlite3_zipfile_init(p->db, 0, 0);
4732    sqlite3_sqlar_init(p->db, 0, 0);
4733#endif
4734    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4735                            shellAddSchemaName, 0, 0);
4736    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4737                            shellModuleSchema, 0, 0);
4738    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4739                            shellPutsFunc, 0, 0);
4740    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4741                            shellEscapeCrnl, 0, 0);
4742    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4743                            shellInt32, 0, 0);
4744    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4745                            shellIdQuote, 0, 0);
4746    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
4747                            shellUSleepFunc, 0, 0);
4748#ifndef SQLITE_NOHAVE_SYSTEM
4749    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4750                            editFunc, 0, 0);
4751    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4752                            editFunc, 0, 0);
4753#endif
4754    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4755      char *zSql = sqlite3_mprintf(
4756         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
4757      sqlite3_exec(p->db, zSql, 0, 0, 0);
4758      sqlite3_free(zSql);
4759    }
4760#ifndef SQLITE_OMIT_DESERIALIZE
4761    else
4762    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4763      int rc;
4764      int nData = 0;
4765      unsigned char *aData;
4766      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4767        aData = (unsigned char*)readFile(zDbFilename, &nData);
4768      }else{
4769        aData = readHexDb(p, &nData);
4770        if( aData==0 ){
4771          return;
4772        }
4773      }
4774      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4775                   SQLITE_DESERIALIZE_RESIZEABLE |
4776                   SQLITE_DESERIALIZE_FREEONCLOSE);
4777      if( rc ){
4778        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4779      }
4780      if( p->szMax>0 ){
4781        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4782      }
4783    }
4784#endif
4785  }
4786  if( p->bSafeModePersist && p->db!=0 ){
4787    sqlite3_set_authorizer(p->db, safeModeAuth, p);
4788  }
4789}
4790
4791/*
4792** Attempt to close the databaes connection.  Report errors.
4793*/
4794void close_db(sqlite3 *db){
4795  int rc = sqlite3_close(db);
4796  if( rc ){
4797    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4798        rc, sqlite3_errmsg(db));
4799  }
4800}
4801
4802#if HAVE_READLINE || HAVE_EDITLINE
4803/*
4804** Readline completion callbacks
4805*/
4806static char *readline_completion_generator(const char *text, int state){
4807  static sqlite3_stmt *pStmt = 0;
4808  char *zRet;
4809  if( state==0 ){
4810    char *zSql;
4811    sqlite3_finalize(pStmt);
4812    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4813                           "  FROM completion(%Q) ORDER BY 1", text);
4814    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4815    sqlite3_free(zSql);
4816  }
4817  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4818    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4819  }else{
4820    sqlite3_finalize(pStmt);
4821    pStmt = 0;
4822    zRet = 0;
4823  }
4824  return zRet;
4825}
4826static char **readline_completion(const char *zText, int iStart, int iEnd){
4827  rl_attempted_completion_over = 1;
4828  return rl_completion_matches(zText, readline_completion_generator);
4829}
4830
4831#elif HAVE_LINENOISE
4832/*
4833** Linenoise completion callback
4834*/
4835static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4836  int nLine = strlen30(zLine);
4837  int i, iStart;
4838  sqlite3_stmt *pStmt = 0;
4839  char *zSql;
4840  char zBuf[1000];
4841
4842  if( nLine>sizeof(zBuf)-30 ) return;
4843  if( zLine[0]=='.' || zLine[0]=='#') return;
4844  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4845  if( i==nLine-1 ) return;
4846  iStart = i+1;
4847  memcpy(zBuf, zLine, iStart);
4848  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4849                         "  FROM completion(%Q,%Q) ORDER BY 1",
4850                         &zLine[iStart], zLine);
4851  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4852  sqlite3_free(zSql);
4853  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4854  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4855    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4856    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4857    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4858      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4859      linenoiseAddCompletion(lc, zBuf);
4860    }
4861  }
4862  sqlite3_finalize(pStmt);
4863}
4864#endif
4865
4866/*
4867** Do C-language style dequoting.
4868**
4869**    \a    -> alarm
4870**    \b    -> backspace
4871**    \t    -> tab
4872**    \n    -> newline
4873**    \v    -> vertical tab
4874**    \f    -> form feed
4875**    \r    -> carriage return
4876**    \s    -> space
4877**    \"    -> "
4878**    \'    -> '
4879**    \\    -> backslash
4880**    \NNN  -> ascii character NNN in octal
4881*/
4882static void resolve_backslashes(char *z){
4883  int i, j;
4884  char c;
4885  while( *z && *z!='\\' ) z++;
4886  for(i=j=0; (c = z[i])!=0; i++, j++){
4887    if( c=='\\' && z[i+1]!=0 ){
4888      c = z[++i];
4889      if( c=='a' ){
4890        c = '\a';
4891      }else if( c=='b' ){
4892        c = '\b';
4893      }else if( c=='t' ){
4894        c = '\t';
4895      }else if( c=='n' ){
4896        c = '\n';
4897      }else if( c=='v' ){
4898        c = '\v';
4899      }else if( c=='f' ){
4900        c = '\f';
4901      }else if( c=='r' ){
4902        c = '\r';
4903      }else if( c=='"' ){
4904        c = '"';
4905      }else if( c=='\'' ){
4906        c = '\'';
4907      }else if( c=='\\' ){
4908        c = '\\';
4909      }else if( c>='0' && c<='7' ){
4910        c -= '0';
4911        if( z[i+1]>='0' && z[i+1]<='7' ){
4912          i++;
4913          c = (c<<3) + z[i] - '0';
4914          if( z[i+1]>='0' && z[i+1]<='7' ){
4915            i++;
4916            c = (c<<3) + z[i] - '0';
4917          }
4918        }
4919      }
4920    }
4921    z[j] = c;
4922  }
4923  if( j<i ) z[j] = 0;
4924}
4925
4926/*
4927** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4928** for TRUE and FALSE.  Return the integer value if appropriate.
4929*/
4930static int booleanValue(const char *zArg){
4931  int i;
4932  if( zArg[0]=='0' && zArg[1]=='x' ){
4933    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4934  }else{
4935    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4936  }
4937  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4938  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4939    return 1;
4940  }
4941  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4942    return 0;
4943  }
4944  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4945          zArg);
4946  return 0;
4947}
4948
4949/*
4950** Set or clear a shell flag according to a boolean value.
4951*/
4952static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4953  if( booleanValue(zArg) ){
4954    ShellSetFlag(p, mFlag);
4955  }else{
4956    ShellClearFlag(p, mFlag);
4957  }
4958}
4959
4960/*
4961** Close an output file, assuming it is not stderr or stdout
4962*/
4963static void output_file_close(FILE *f){
4964  if( f && f!=stdout && f!=stderr ) fclose(f);
4965}
4966
4967/*
4968** Try to open an output file.   The names "stdout" and "stderr" are
4969** recognized and do the right thing.  NULL is returned if the output
4970** filename is "off".
4971*/
4972static FILE *output_file_open(const char *zFile, int bTextMode){
4973  FILE *f;
4974  if( strcmp(zFile,"stdout")==0 ){
4975    f = stdout;
4976  }else if( strcmp(zFile, "stderr")==0 ){
4977    f = stderr;
4978  }else if( strcmp(zFile, "off")==0 ){
4979    f = 0;
4980  }else{
4981    f = fopen(zFile, bTextMode ? "w" : "wb");
4982    if( f==0 ){
4983      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4984    }
4985  }
4986  return f;
4987}
4988
4989#ifndef SQLITE_OMIT_TRACE
4990/*
4991** A routine for handling output from sqlite3_trace().
4992*/
4993static int sql_trace_callback(
4994  unsigned mType,         /* The trace type */
4995  void *pArg,             /* The ShellState pointer */
4996  void *pP,               /* Usually a pointer to sqlite_stmt */
4997  void *pX                /* Auxiliary output */
4998){
4999  ShellState *p = (ShellState*)pArg;
5000  sqlite3_stmt *pStmt;
5001  const char *zSql;
5002  int nSql;
5003  if( p->traceOut==0 ) return 0;
5004  if( mType==SQLITE_TRACE_CLOSE ){
5005    utf8_printf(p->traceOut, "-- closing database connection\n");
5006    return 0;
5007  }
5008  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5009    zSql = (const char*)pX;
5010  }else{
5011    pStmt = (sqlite3_stmt*)pP;
5012    switch( p->eTraceType ){
5013      case SHELL_TRACE_EXPANDED: {
5014        zSql = sqlite3_expanded_sql(pStmt);
5015        break;
5016      }
5017#ifdef SQLITE_ENABLE_NORMALIZE
5018      case SHELL_TRACE_NORMALIZED: {
5019        zSql = sqlite3_normalized_sql(pStmt);
5020        break;
5021      }
5022#endif
5023      default: {
5024        zSql = sqlite3_sql(pStmt);
5025        break;
5026      }
5027    }
5028  }
5029  if( zSql==0 ) return 0;
5030  nSql = strlen30(zSql);
5031  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5032  switch( mType ){
5033    case SQLITE_TRACE_ROW:
5034    case SQLITE_TRACE_STMT: {
5035      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
5036      break;
5037    }
5038    case SQLITE_TRACE_PROFILE: {
5039      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5040      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
5041      break;
5042    }
5043  }
5044  return 0;
5045}
5046#endif
5047
5048/*
5049** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5050** a useful spot to set a debugger breakpoint.
5051*/
5052static void test_breakpoint(void){
5053  static int nCall = 0;
5054  nCall++;
5055}
5056
5057/*
5058** An object used to read a CSV and other files for import.
5059*/
5060typedef struct ImportCtx ImportCtx;
5061struct ImportCtx {
5062  const char *zFile;  /* Name of the input file */
5063  FILE *in;           /* Read the CSV text from this input stream */
5064  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5065  char *z;            /* Accumulated text for a field */
5066  int n;              /* Number of bytes in z */
5067  int nAlloc;         /* Space allocated for z[] */
5068  int nLine;          /* Current line number */
5069  int nRow;           /* Number of rows imported */
5070  int nErr;           /* Number of errors encountered */
5071  int bNotFirst;      /* True if one or more bytes already read */
5072  int cTerm;          /* Character that terminated the most recent field */
5073  int cColSep;        /* The column separator character.  (Usually ",") */
5074  int cRowSep;        /* The row separator character.  (Usually "\n") */
5075};
5076
5077/* Clean up resourced used by an ImportCtx */
5078static void import_cleanup(ImportCtx *p){
5079  if( p->in!=0 && p->xCloser!=0 ){
5080    p->xCloser(p->in);
5081    p->in = 0;
5082  }
5083  sqlite3_free(p->z);
5084  p->z = 0;
5085}
5086
5087/* Append a single byte to z[] */
5088static void import_append_char(ImportCtx *p, int c){
5089  if( p->n+1>=p->nAlloc ){
5090    p->nAlloc += p->nAlloc + 100;
5091    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5092    if( p->z==0 ) shell_out_of_memory();
5093  }
5094  p->z[p->n++] = (char)c;
5095}
5096
5097/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5098** with the option of having a separator other than ",".
5099**
5100**   +  Input comes from p->in.
5101**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5102**      from sqlite3_malloc64().
5103**   +  Use p->cSep as the column separator.  The default is ",".
5104**   +  Use p->rSep as the row separator.  The default is "\n".
5105**   +  Keep track of the line number in p->nLine.
5106**   +  Store the character that terminates the field in p->cTerm.  Store
5107**      EOF on end-of-file.
5108**   +  Report syntax errors on stderr
5109*/
5110static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5111  int c;
5112  int cSep = p->cColSep;
5113  int rSep = p->cRowSep;
5114  p->n = 0;
5115  c = fgetc(p->in);
5116  if( c==EOF || seenInterrupt ){
5117    p->cTerm = EOF;
5118    return 0;
5119  }
5120  if( c=='"' ){
5121    int pc, ppc;
5122    int startLine = p->nLine;
5123    int cQuote = c;
5124    pc = ppc = 0;
5125    while( 1 ){
5126      c = fgetc(p->in);
5127      if( c==rSep ) p->nLine++;
5128      if( c==cQuote ){
5129        if( pc==cQuote ){
5130          pc = 0;
5131          continue;
5132        }
5133      }
5134      if( (c==cSep && pc==cQuote)
5135       || (c==rSep && pc==cQuote)
5136       || (c==rSep && pc=='\r' && ppc==cQuote)
5137       || (c==EOF && pc==cQuote)
5138      ){
5139        do{ p->n--; }while( p->z[p->n]!=cQuote );
5140        p->cTerm = c;
5141        break;
5142      }
5143      if( pc==cQuote && c!='\r' ){
5144        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5145                p->zFile, p->nLine, cQuote);
5146      }
5147      if( c==EOF ){
5148        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5149                p->zFile, startLine, cQuote);
5150        p->cTerm = c;
5151        break;
5152      }
5153      import_append_char(p, c);
5154      ppc = pc;
5155      pc = c;
5156    }
5157  }else{
5158    /* If this is the first field being parsed and it begins with the
5159    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5160    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5161      import_append_char(p, c);
5162      c = fgetc(p->in);
5163      if( (c&0xff)==0xbb ){
5164        import_append_char(p, c);
5165        c = fgetc(p->in);
5166        if( (c&0xff)==0xbf ){
5167          p->bNotFirst = 1;
5168          p->n = 0;
5169          return csv_read_one_field(p);
5170        }
5171      }
5172    }
5173    while( c!=EOF && c!=cSep && c!=rSep ){
5174      import_append_char(p, c);
5175      c = fgetc(p->in);
5176    }
5177    if( c==rSep ){
5178      p->nLine++;
5179      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5180    }
5181    p->cTerm = c;
5182  }
5183  if( p->z ) p->z[p->n] = 0;
5184  p->bNotFirst = 1;
5185  return p->z;
5186}
5187
5188/* Read a single field of ASCII delimited text.
5189**
5190**   +  Input comes from p->in.
5191**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5192**      from sqlite3_malloc64().
5193**   +  Use p->cSep as the column separator.  The default is "\x1F".
5194**   +  Use p->rSep as the row separator.  The default is "\x1E".
5195**   +  Keep track of the row number in p->nLine.
5196**   +  Store the character that terminates the field in p->cTerm.  Store
5197**      EOF on end-of-file.
5198**   +  Report syntax errors on stderr
5199*/
5200static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5201  int c;
5202  int cSep = p->cColSep;
5203  int rSep = p->cRowSep;
5204  p->n = 0;
5205  c = fgetc(p->in);
5206  if( c==EOF || seenInterrupt ){
5207    p->cTerm = EOF;
5208    return 0;
5209  }
5210  while( c!=EOF && c!=cSep && c!=rSep ){
5211    import_append_char(p, c);
5212    c = fgetc(p->in);
5213  }
5214  if( c==rSep ){
5215    p->nLine++;
5216  }
5217  p->cTerm = c;
5218  if( p->z ) p->z[p->n] = 0;
5219  return p->z;
5220}
5221
5222/*
5223** Try to transfer data for table zTable.  If an error is seen while
5224** moving forward, try to go backwards.  The backwards movement won't
5225** work for WITHOUT ROWID tables.
5226*/
5227static void tryToCloneData(
5228  ShellState *p,
5229  sqlite3 *newDb,
5230  const char *zTable
5231){
5232  sqlite3_stmt *pQuery = 0;
5233  sqlite3_stmt *pInsert = 0;
5234  char *zQuery = 0;
5235  char *zInsert = 0;
5236  int rc;
5237  int i, j, n;
5238  int nTable = strlen30(zTable);
5239  int k = 0;
5240  int cnt = 0;
5241  const int spinRate = 10000;
5242
5243  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5244  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5245  if( rc ){
5246    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5247            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5248            zQuery);
5249    goto end_data_xfer;
5250  }
5251  n = sqlite3_column_count(pQuery);
5252  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5253  if( zInsert==0 ) shell_out_of_memory();
5254  sqlite3_snprintf(200+nTable,zInsert,
5255                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5256  i = strlen30(zInsert);
5257  for(j=1; j<n; j++){
5258    memcpy(zInsert+i, ",?", 2);
5259    i += 2;
5260  }
5261  memcpy(zInsert+i, ");", 3);
5262  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5263  if( rc ){
5264    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5265            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5266            zQuery);
5267    goto end_data_xfer;
5268  }
5269  for(k=0; k<2; k++){
5270    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5271      for(i=0; i<n; i++){
5272        switch( sqlite3_column_type(pQuery, i) ){
5273          case SQLITE_NULL: {
5274            sqlite3_bind_null(pInsert, i+1);
5275            break;
5276          }
5277          case SQLITE_INTEGER: {
5278            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5279            break;
5280          }
5281          case SQLITE_FLOAT: {
5282            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5283            break;
5284          }
5285          case SQLITE_TEXT: {
5286            sqlite3_bind_text(pInsert, i+1,
5287                             (const char*)sqlite3_column_text(pQuery,i),
5288                             -1, SQLITE_STATIC);
5289            break;
5290          }
5291          case SQLITE_BLOB: {
5292            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5293                                            sqlite3_column_bytes(pQuery,i),
5294                                            SQLITE_STATIC);
5295            break;
5296          }
5297        }
5298      } /* End for */
5299      rc = sqlite3_step(pInsert);
5300      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5301        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5302                        sqlite3_errmsg(newDb));
5303      }
5304      sqlite3_reset(pInsert);
5305      cnt++;
5306      if( (cnt%spinRate)==0 ){
5307        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5308        fflush(stdout);
5309      }
5310    } /* End while */
5311    if( rc==SQLITE_DONE ) break;
5312    sqlite3_finalize(pQuery);
5313    sqlite3_free(zQuery);
5314    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5315                             zTable);
5316    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5317    if( rc ){
5318      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5319      break;
5320    }
5321  } /* End for(k=0...) */
5322
5323end_data_xfer:
5324  sqlite3_finalize(pQuery);
5325  sqlite3_finalize(pInsert);
5326  sqlite3_free(zQuery);
5327  sqlite3_free(zInsert);
5328}
5329
5330
5331/*
5332** Try to transfer all rows of the schema that match zWhere.  For
5333** each row, invoke xForEach() on the object defined by that row.
5334** If an error is encountered while moving forward through the
5335** sqlite_schema table, try again moving backwards.
5336*/
5337static void tryToCloneSchema(
5338  ShellState *p,
5339  sqlite3 *newDb,
5340  const char *zWhere,
5341  void (*xForEach)(ShellState*,sqlite3*,const char*)
5342){
5343  sqlite3_stmt *pQuery = 0;
5344  char *zQuery = 0;
5345  int rc;
5346  const unsigned char *zName;
5347  const unsigned char *zSql;
5348  char *zErrMsg = 0;
5349
5350  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5351                           " WHERE %s", zWhere);
5352  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5353  if( rc ){
5354    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5355                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5356                    zQuery);
5357    goto end_schema_xfer;
5358  }
5359  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5360    zName = sqlite3_column_text(pQuery, 0);
5361    zSql = sqlite3_column_text(pQuery, 1);
5362    printf("%s... ", zName); fflush(stdout);
5363    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5364    if( zErrMsg ){
5365      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5366      sqlite3_free(zErrMsg);
5367      zErrMsg = 0;
5368    }
5369    if( xForEach ){
5370      xForEach(p, newDb, (const char*)zName);
5371    }
5372    printf("done\n");
5373  }
5374  if( rc!=SQLITE_DONE ){
5375    sqlite3_finalize(pQuery);
5376    sqlite3_free(zQuery);
5377    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5378                             " WHERE %s ORDER BY rowid DESC", zWhere);
5379    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5380    if( rc ){
5381      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5382                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5383                      zQuery);
5384      goto end_schema_xfer;
5385    }
5386    while( sqlite3_step(pQuery)==SQLITE_ROW ){
5387      zName = sqlite3_column_text(pQuery, 0);
5388      zSql = sqlite3_column_text(pQuery, 1);
5389      printf("%s... ", zName); fflush(stdout);
5390      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5391      if( zErrMsg ){
5392        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5393        sqlite3_free(zErrMsg);
5394        zErrMsg = 0;
5395      }
5396      if( xForEach ){
5397        xForEach(p, newDb, (const char*)zName);
5398      }
5399      printf("done\n");
5400    }
5401  }
5402end_schema_xfer:
5403  sqlite3_finalize(pQuery);
5404  sqlite3_free(zQuery);
5405}
5406
5407/*
5408** Open a new database file named "zNewDb".  Try to recover as much information
5409** as possible out of the main database (which might be corrupt) and write it
5410** into zNewDb.
5411*/
5412static void tryToClone(ShellState *p, const char *zNewDb){
5413  int rc;
5414  sqlite3 *newDb = 0;
5415  if( access(zNewDb,0)==0 ){
5416    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5417    return;
5418  }
5419  rc = sqlite3_open(zNewDb, &newDb);
5420  if( rc ){
5421    utf8_printf(stderr, "Cannot create output database: %s\n",
5422            sqlite3_errmsg(newDb));
5423  }else{
5424    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5425    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5426    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5427    tryToCloneSchema(p, newDb, "type!='table'", 0);
5428    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5429    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5430  }
5431  close_db(newDb);
5432}
5433
5434/*
5435** Change the output file back to stdout.
5436**
5437** If the p->doXdgOpen flag is set, that means the output was being
5438** redirected to a temporary file named by p->zTempFile.  In that case,
5439** launch start/open/xdg-open on that temporary file.
5440*/
5441static void output_reset(ShellState *p){
5442  if( p->outfile[0]=='|' ){
5443#ifndef SQLITE_OMIT_POPEN
5444    pclose(p->out);
5445#endif
5446  }else{
5447    output_file_close(p->out);
5448#ifndef SQLITE_NOHAVE_SYSTEM
5449    if( p->doXdgOpen ){
5450      const char *zXdgOpenCmd =
5451#if defined(_WIN32)
5452      "start";
5453#elif defined(__APPLE__)
5454      "open";
5455#else
5456      "xdg-open";
5457#endif
5458      char *zCmd;
5459      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5460      if( system(zCmd) ){
5461        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5462      }else{
5463        /* Give the start/open/xdg-open command some time to get
5464        ** going before we continue, and potential delete the
5465        ** p->zTempFile data file out from under it */
5466        sqlite3_sleep(2000);
5467      }
5468      sqlite3_free(zCmd);
5469      outputModePop(p);
5470      p->doXdgOpen = 0;
5471    }
5472#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5473  }
5474  p->outfile[0] = 0;
5475  p->out = stdout;
5476}
5477
5478/*
5479** Run an SQL command and return the single integer result.
5480*/
5481static int db_int(ShellState *p, const char *zSql){
5482  sqlite3_stmt *pStmt;
5483  int res = 0;
5484  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5485  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5486    res = sqlite3_column_int(pStmt,0);
5487  }
5488  sqlite3_finalize(pStmt);
5489  return res;
5490}
5491
5492/*
5493** Convert a 2-byte or 4-byte big-endian integer into a native integer
5494*/
5495static unsigned int get2byteInt(unsigned char *a){
5496  return (a[0]<<8) + a[1];
5497}
5498static unsigned int get4byteInt(unsigned char *a){
5499  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5500}
5501
5502/*
5503** Implementation of the ".dbinfo" command.
5504**
5505** Return 1 on error, 2 to exit, and 0 otherwise.
5506*/
5507static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5508  static const struct { const char *zName; int ofst; } aField[] = {
5509     { "file change counter:",  24  },
5510     { "database page count:",  28  },
5511     { "freelist page count:",  36  },
5512     { "schema cookie:",        40  },
5513     { "schema format:",        44  },
5514     { "default cache size:",   48  },
5515     { "autovacuum top root:",  52  },
5516     { "incremental vacuum:",   64  },
5517     { "text encoding:",        56  },
5518     { "user version:",         60  },
5519     { "application id:",       68  },
5520     { "software version:",     96  },
5521  };
5522  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5523     { "number of tables:",
5524       "SELECT count(*) FROM %s WHERE type='table'" },
5525     { "number of indexes:",
5526       "SELECT count(*) FROM %s WHERE type='index'" },
5527     { "number of triggers:",
5528       "SELECT count(*) FROM %s WHERE type='trigger'" },
5529     { "number of views:",
5530       "SELECT count(*) FROM %s WHERE type='view'" },
5531     { "schema size:",
5532       "SELECT total(length(sql)) FROM %s" },
5533  };
5534  int i, rc;
5535  unsigned iDataVersion;
5536  char *zSchemaTab;
5537  char *zDb = nArg>=2 ? azArg[1] : "main";
5538  sqlite3_stmt *pStmt = 0;
5539  unsigned char aHdr[100];
5540  open_db(p, 0);
5541  if( p->db==0 ) return 1;
5542  rc = sqlite3_prepare_v2(p->db,
5543             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5544             -1, &pStmt, 0);
5545  if( rc ){
5546    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5547    sqlite3_finalize(pStmt);
5548    return 1;
5549  }
5550  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5551  if( sqlite3_step(pStmt)==SQLITE_ROW
5552   && sqlite3_column_bytes(pStmt,0)>100
5553  ){
5554    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5555    sqlite3_finalize(pStmt);
5556  }else{
5557    raw_printf(stderr, "unable to read database header\n");
5558    sqlite3_finalize(pStmt);
5559    return 1;
5560  }
5561  i = get2byteInt(aHdr+16);
5562  if( i==1 ) i = 65536;
5563  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5564  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5565  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5566  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5567  for(i=0; i<ArraySize(aField); i++){
5568    int ofst = aField[i].ofst;
5569    unsigned int val = get4byteInt(aHdr + ofst);
5570    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5571    switch( ofst ){
5572      case 56: {
5573        if( val==1 ) raw_printf(p->out, " (utf8)");
5574        if( val==2 ) raw_printf(p->out, " (utf16le)");
5575        if( val==3 ) raw_printf(p->out, " (utf16be)");
5576      }
5577    }
5578    raw_printf(p->out, "\n");
5579  }
5580  if( zDb==0 ){
5581    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5582  }else if( strcmp(zDb,"temp")==0 ){
5583    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5584  }else{
5585    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5586  }
5587  for(i=0; i<ArraySize(aQuery); i++){
5588    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5589    int val = db_int(p, zSql);
5590    sqlite3_free(zSql);
5591    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5592  }
5593  sqlite3_free(zSchemaTab);
5594  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5595  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5596  return 0;
5597}
5598
5599/*
5600** Print the current sqlite3_errmsg() value to stderr and return 1.
5601*/
5602static int shellDatabaseError(sqlite3 *db){
5603  const char *zErr = sqlite3_errmsg(db);
5604  utf8_printf(stderr, "Error: %s\n", zErr);
5605  return 1;
5606}
5607
5608/*
5609** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5610** if they match and FALSE (0) if they do not match.
5611**
5612** Globbing rules:
5613**
5614**      '*'       Matches any sequence of zero or more characters.
5615**
5616**      '?'       Matches exactly one character.
5617**
5618**     [...]      Matches one character from the enclosed list of
5619**                characters.
5620**
5621**     [^...]     Matches one character not in the enclosed list.
5622**
5623**      '#'       Matches any sequence of one or more digits with an
5624**                optional + or - sign in front
5625**
5626**      ' '       Any span of whitespace matches any other span of
5627**                whitespace.
5628**
5629** Extra whitespace at the end of z[] is ignored.
5630*/
5631static int testcase_glob(const char *zGlob, const char *z){
5632  int c, c2;
5633  int invert;
5634  int seen;
5635
5636  while( (c = (*(zGlob++)))!=0 ){
5637    if( IsSpace(c) ){
5638      if( !IsSpace(*z) ) return 0;
5639      while( IsSpace(*zGlob) ) zGlob++;
5640      while( IsSpace(*z) ) z++;
5641    }else if( c=='*' ){
5642      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5643        if( c=='?' && (*(z++))==0 ) return 0;
5644      }
5645      if( c==0 ){
5646        return 1;
5647      }else if( c=='[' ){
5648        while( *z && testcase_glob(zGlob-1,z)==0 ){
5649          z++;
5650        }
5651        return (*z)!=0;
5652      }
5653      while( (c2 = (*(z++)))!=0 ){
5654        while( c2!=c ){
5655          c2 = *(z++);
5656          if( c2==0 ) return 0;
5657        }
5658        if( testcase_glob(zGlob,z) ) return 1;
5659      }
5660      return 0;
5661    }else if( c=='?' ){
5662      if( (*(z++))==0 ) return 0;
5663    }else if( c=='[' ){
5664      int prior_c = 0;
5665      seen = 0;
5666      invert = 0;
5667      c = *(z++);
5668      if( c==0 ) return 0;
5669      c2 = *(zGlob++);
5670      if( c2=='^' ){
5671        invert = 1;
5672        c2 = *(zGlob++);
5673      }
5674      if( c2==']' ){
5675        if( c==']' ) seen = 1;
5676        c2 = *(zGlob++);
5677      }
5678      while( c2 && c2!=']' ){
5679        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5680          c2 = *(zGlob++);
5681          if( c>=prior_c && c<=c2 ) seen = 1;
5682          prior_c = 0;
5683        }else{
5684          if( c==c2 ){
5685            seen = 1;
5686          }
5687          prior_c = c2;
5688        }
5689        c2 = *(zGlob++);
5690      }
5691      if( c2==0 || (seen ^ invert)==0 ) return 0;
5692    }else if( c=='#' ){
5693      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5694      if( !IsDigit(z[0]) ) return 0;
5695      z++;
5696      while( IsDigit(z[0]) ){ z++; }
5697    }else{
5698      if( c!=(*(z++)) ) return 0;
5699    }
5700  }
5701  while( IsSpace(*z) ){ z++; }
5702  return *z==0;
5703}
5704
5705
5706/*
5707** Compare the string as a command-line option with either one or two
5708** initial "-" characters.
5709*/
5710static int optionMatch(const char *zStr, const char *zOpt){
5711  if( zStr[0]!='-' ) return 0;
5712  zStr++;
5713  if( zStr[0]=='-' ) zStr++;
5714  return strcmp(zStr, zOpt)==0;
5715}
5716
5717/*
5718** Delete a file.
5719*/
5720int shellDeleteFile(const char *zFilename){
5721  int rc;
5722#ifdef _WIN32
5723  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5724  rc = _wunlink(z);
5725  sqlite3_free(z);
5726#else
5727  rc = unlink(zFilename);
5728#endif
5729  return rc;
5730}
5731
5732/*
5733** Try to delete the temporary file (if there is one) and free the
5734** memory used to hold the name of the temp file.
5735*/
5736static void clearTempFile(ShellState *p){
5737  if( p->zTempFile==0 ) return;
5738  if( p->doXdgOpen ) return;
5739  if( shellDeleteFile(p->zTempFile) ) return;
5740  sqlite3_free(p->zTempFile);
5741  p->zTempFile = 0;
5742}
5743
5744/*
5745** Create a new temp file name with the given suffix.
5746*/
5747static void newTempFile(ShellState *p, const char *zSuffix){
5748  clearTempFile(p);
5749  sqlite3_free(p->zTempFile);
5750  p->zTempFile = 0;
5751  if( p->db ){
5752    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5753  }
5754  if( p->zTempFile==0 ){
5755    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5756    ** will not work and we will need to fallback to guessing */
5757    char *zTemp;
5758    sqlite3_uint64 r;
5759    sqlite3_randomness(sizeof(r), &r);
5760    zTemp = getenv("TEMP");
5761    if( zTemp==0 ) zTemp = getenv("TMP");
5762    if( zTemp==0 ){
5763#ifdef _WIN32
5764      zTemp = "\\tmp";
5765#else
5766      zTemp = "/tmp";
5767#endif
5768    }
5769    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5770  }else{
5771    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5772  }
5773  if( p->zTempFile==0 ){
5774    shell_out_of_memory();
5775  }
5776}
5777
5778
5779/*
5780** The implementation of SQL scalar function fkey_collate_clause(), used
5781** by the ".lint fkey-indexes" command. This scalar function is always
5782** called with four arguments - the parent table name, the parent column name,
5783** the child table name and the child column name.
5784**
5785**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5786**
5787** If either of the named tables or columns do not exist, this function
5788** returns an empty string. An empty string is also returned if both tables
5789** and columns exist but have the same default collation sequence. Or,
5790** if both exist but the default collation sequences are different, this
5791** function returns the string " COLLATE <parent-collation>", where
5792** <parent-collation> is the default collation sequence of the parent column.
5793*/
5794static void shellFkeyCollateClause(
5795  sqlite3_context *pCtx,
5796  int nVal,
5797  sqlite3_value **apVal
5798){
5799  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5800  const char *zParent;
5801  const char *zParentCol;
5802  const char *zParentSeq;
5803  const char *zChild;
5804  const char *zChildCol;
5805  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5806  int rc;
5807
5808  assert( nVal==4 );
5809  zParent = (const char*)sqlite3_value_text(apVal[0]);
5810  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5811  zChild = (const char*)sqlite3_value_text(apVal[2]);
5812  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5813
5814  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5815  rc = sqlite3_table_column_metadata(
5816      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5817  );
5818  if( rc==SQLITE_OK ){
5819    rc = sqlite3_table_column_metadata(
5820        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5821    );
5822  }
5823
5824  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5825    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5826    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5827    sqlite3_free(z);
5828  }
5829}
5830
5831
5832/*
5833** The implementation of dot-command ".lint fkey-indexes".
5834*/
5835static int lintFkeyIndexes(
5836  ShellState *pState,             /* Current shell tool state */
5837  char **azArg,                   /* Array of arguments passed to dot command */
5838  int nArg                        /* Number of entries in azArg[] */
5839){
5840  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5841  FILE *out = pState->out;        /* Stream to write non-error output to */
5842  int bVerbose = 0;               /* If -verbose is present */
5843  int bGroupByParent = 0;         /* If -groupbyparent is present */
5844  int i;                          /* To iterate through azArg[] */
5845  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5846  int rc;                         /* Return code */
5847  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5848
5849  /*
5850  ** This SELECT statement returns one row for each foreign key constraint
5851  ** in the schema of the main database. The column values are:
5852  **
5853  ** 0. The text of an SQL statement similar to:
5854  **
5855  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5856  **
5857  **    This SELECT is similar to the one that the foreign keys implementation
5858  **    needs to run internally on child tables. If there is an index that can
5859  **    be used to optimize this query, then it can also be used by the FK
5860  **    implementation to optimize DELETE or UPDATE statements on the parent
5861  **    table.
5862  **
5863  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5864  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5865  **    contains an index that can be used to optimize the query.
5866  **
5867  ** 2. Human readable text that describes the child table and columns. e.g.
5868  **
5869  **       "child_table(child_key1, child_key2)"
5870  **
5871  ** 3. Human readable text that describes the parent table and columns. e.g.
5872  **
5873  **       "parent_table(parent_key1, parent_key2)"
5874  **
5875  ** 4. A full CREATE INDEX statement for an index that could be used to
5876  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5877  **
5878  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5879  **
5880  ** 5. The name of the parent table.
5881  **
5882  ** These six values are used by the C logic below to generate the report.
5883  */
5884  const char *zSql =
5885  "SELECT "
5886    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5887    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5888    "  || fkey_collate_clause("
5889    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5890    ", "
5891    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
5892    "  || group_concat('*=?', ' AND ') || ')'"
5893    ", "
5894    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5895    ", "
5896    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5897    ", "
5898    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5899    "  || ' ON ' || quote(s.name) || '('"
5900    "  || group_concat(quote(f.[from]) ||"
5901    "        fkey_collate_clause("
5902    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5903    "  || ');'"
5904    ", "
5905    "     f.[table] "
5906    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5907    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5908    "GROUP BY s.name, f.id "
5909    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5910  ;
5911  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
5912
5913  for(i=2; i<nArg; i++){
5914    int n = strlen30(azArg[i]);
5915    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5916      bVerbose = 1;
5917    }
5918    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5919      bGroupByParent = 1;
5920      zIndent = "    ";
5921    }
5922    else{
5923      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5924          azArg[0], azArg[1]
5925      );
5926      return SQLITE_ERROR;
5927    }
5928  }
5929
5930  /* Register the fkey_collate_clause() SQL function */
5931  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5932      0, shellFkeyCollateClause, 0, 0
5933  );
5934
5935
5936  if( rc==SQLITE_OK ){
5937    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5938  }
5939  if( rc==SQLITE_OK ){
5940    sqlite3_bind_int(pSql, 1, bGroupByParent);
5941  }
5942
5943  if( rc==SQLITE_OK ){
5944    int rc2;
5945    char *zPrev = 0;
5946    while( SQLITE_ROW==sqlite3_step(pSql) ){
5947      int res = -1;
5948      sqlite3_stmt *pExplain = 0;
5949      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5950      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5951      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5952      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5953      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5954      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5955
5956      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5957      if( rc!=SQLITE_OK ) break;
5958      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5959        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5960        res = (
5961              0==sqlite3_strglob(zGlob, zPlan)
5962           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5963        );
5964      }
5965      rc = sqlite3_finalize(pExplain);
5966      if( rc!=SQLITE_OK ) break;
5967
5968      if( res<0 ){
5969        raw_printf(stderr, "Error: internal error");
5970        break;
5971      }else{
5972        if( bGroupByParent
5973        && (bVerbose || res==0)
5974        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5975        ){
5976          raw_printf(out, "-- Parent table %s\n", zParent);
5977          sqlite3_free(zPrev);
5978          zPrev = sqlite3_mprintf("%s", zParent);
5979        }
5980
5981        if( res==0 ){
5982          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5983        }else if( bVerbose ){
5984          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5985              zIndent, zFrom, zTarget
5986          );
5987        }
5988      }
5989    }
5990    sqlite3_free(zPrev);
5991
5992    if( rc!=SQLITE_OK ){
5993      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5994    }
5995
5996    rc2 = sqlite3_finalize(pSql);
5997    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5998      rc = rc2;
5999      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6000    }
6001  }else{
6002    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6003  }
6004
6005  return rc;
6006}
6007
6008/*
6009** Implementation of ".lint" dot command.
6010*/
6011static int lintDotCommand(
6012  ShellState *pState,             /* Current shell tool state */
6013  char **azArg,                   /* Array of arguments passed to dot command */
6014  int nArg                        /* Number of entries in azArg[] */
6015){
6016  int n;
6017  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6018  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6019  return lintFkeyIndexes(pState, azArg, nArg);
6020
6021 usage:
6022  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6023  raw_printf(stderr, "Where sub-commands are:\n");
6024  raw_printf(stderr, "    fkey-indexes\n");
6025  return SQLITE_ERROR;
6026}
6027
6028#if !defined SQLITE_OMIT_VIRTUALTABLE
6029static void shellPrepare(
6030  sqlite3 *db,
6031  int *pRc,
6032  const char *zSql,
6033  sqlite3_stmt **ppStmt
6034){
6035  *ppStmt = 0;
6036  if( *pRc==SQLITE_OK ){
6037    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6038    if( rc!=SQLITE_OK ){
6039      raw_printf(stderr, "sql error: %s (%d)\n",
6040          sqlite3_errmsg(db), sqlite3_errcode(db)
6041      );
6042      *pRc = rc;
6043    }
6044  }
6045}
6046
6047/*
6048** Create a prepared statement using printf-style arguments for the SQL.
6049**
6050** This routine is could be marked "static".  But it is not always used,
6051** depending on compile-time options.  By omitting the "static", we avoid
6052** nuisance compiler warnings about "defined but not used".
6053*/
6054void shellPreparePrintf(
6055  sqlite3 *db,
6056  int *pRc,
6057  sqlite3_stmt **ppStmt,
6058  const char *zFmt,
6059  ...
6060){
6061  *ppStmt = 0;
6062  if( *pRc==SQLITE_OK ){
6063    va_list ap;
6064    char *z;
6065    va_start(ap, zFmt);
6066    z = sqlite3_vmprintf(zFmt, ap);
6067    va_end(ap);
6068    if( z==0 ){
6069      *pRc = SQLITE_NOMEM;
6070    }else{
6071      shellPrepare(db, pRc, z, ppStmt);
6072      sqlite3_free(z);
6073    }
6074  }
6075}
6076
6077/* Finalize the prepared statement created using shellPreparePrintf().
6078**
6079** This routine is could be marked "static".  But it is not always used,
6080** depending on compile-time options.  By omitting the "static", we avoid
6081** nuisance compiler warnings about "defined but not used".
6082*/
6083void shellFinalize(
6084  int *pRc,
6085  sqlite3_stmt *pStmt
6086){
6087  if( pStmt ){
6088    sqlite3 *db = sqlite3_db_handle(pStmt);
6089    int rc = sqlite3_finalize(pStmt);
6090    if( *pRc==SQLITE_OK ){
6091      if( rc!=SQLITE_OK ){
6092        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6093      }
6094      *pRc = rc;
6095    }
6096  }
6097}
6098
6099/* Reset the prepared statement created using shellPreparePrintf().
6100**
6101** This routine is could be marked "static".  But it is not always used,
6102** depending on compile-time options.  By omitting the "static", we avoid
6103** nuisance compiler warnings about "defined but not used".
6104*/
6105void shellReset(
6106  int *pRc,
6107  sqlite3_stmt *pStmt
6108){
6109  int rc = sqlite3_reset(pStmt);
6110  if( *pRc==SQLITE_OK ){
6111    if( rc!=SQLITE_OK ){
6112      sqlite3 *db = sqlite3_db_handle(pStmt);
6113      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6114    }
6115    *pRc = rc;
6116  }
6117}
6118#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6119
6120#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6121/******************************************************************************
6122** The ".archive" or ".ar" command.
6123*/
6124/*
6125** Structure representing a single ".ar" command.
6126*/
6127typedef struct ArCommand ArCommand;
6128struct ArCommand {
6129  u8 eCmd;                        /* An AR_CMD_* value */
6130  u8 bVerbose;                    /* True if --verbose */
6131  u8 bZip;                        /* True if the archive is a ZIP */
6132  u8 bDryRun;                     /* True if --dry-run */
6133  u8 bAppend;                     /* True if --append */
6134  u8 bGlob;                       /* True if --glob */
6135  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6136  int nArg;                       /* Number of command arguments */
6137  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6138  const char *zFile;              /* --file argument, or NULL */
6139  const char *zDir;               /* --directory argument, or NULL */
6140  char **azArg;                   /* Array of command arguments */
6141  ShellState *p;                  /* Shell state */
6142  sqlite3 *db;                    /* Database containing the archive */
6143};
6144
6145/*
6146** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6147*/
6148static int arUsage(FILE *f){
6149  showHelp(f,"archive");
6150  return SQLITE_ERROR;
6151}
6152
6153/*
6154** Print an error message for the .ar command to stderr and return
6155** SQLITE_ERROR.
6156*/
6157static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6158  va_list ap;
6159  char *z;
6160  va_start(ap, zFmt);
6161  z = sqlite3_vmprintf(zFmt, ap);
6162  va_end(ap);
6163  utf8_printf(stderr, "Error: %s\n", z);
6164  if( pAr->fromCmdLine ){
6165    utf8_printf(stderr, "Use \"-A\" for more help\n");
6166  }else{
6167    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6168  }
6169  sqlite3_free(z);
6170  return SQLITE_ERROR;
6171}
6172
6173/*
6174** Values for ArCommand.eCmd.
6175*/
6176#define AR_CMD_CREATE       1
6177#define AR_CMD_UPDATE       2
6178#define AR_CMD_INSERT       3
6179#define AR_CMD_EXTRACT      4
6180#define AR_CMD_LIST         5
6181#define AR_CMD_HELP         6
6182#define AR_CMD_REMOVE       7
6183
6184/*
6185** Other (non-command) switches.
6186*/
6187#define AR_SWITCH_VERBOSE     8
6188#define AR_SWITCH_FILE        9
6189#define AR_SWITCH_DIRECTORY  10
6190#define AR_SWITCH_APPEND     11
6191#define AR_SWITCH_DRYRUN     12
6192#define AR_SWITCH_GLOB       13
6193
6194static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6195  switch( eSwitch ){
6196    case AR_CMD_CREATE:
6197    case AR_CMD_EXTRACT:
6198    case AR_CMD_LIST:
6199    case AR_CMD_REMOVE:
6200    case AR_CMD_UPDATE:
6201    case AR_CMD_INSERT:
6202    case AR_CMD_HELP:
6203      if( pAr->eCmd ){
6204        return arErrorMsg(pAr, "multiple command options");
6205      }
6206      pAr->eCmd = eSwitch;
6207      break;
6208
6209    case AR_SWITCH_DRYRUN:
6210      pAr->bDryRun = 1;
6211      break;
6212    case AR_SWITCH_GLOB:
6213      pAr->bGlob = 1;
6214      break;
6215    case AR_SWITCH_VERBOSE:
6216      pAr->bVerbose = 1;
6217      break;
6218    case AR_SWITCH_APPEND:
6219      pAr->bAppend = 1;
6220      /* Fall thru into --file */
6221    case AR_SWITCH_FILE:
6222      pAr->zFile = zArg;
6223      break;
6224    case AR_SWITCH_DIRECTORY:
6225      pAr->zDir = zArg;
6226      break;
6227  }
6228
6229  return SQLITE_OK;
6230}
6231
6232/*
6233** Parse the command line for an ".ar" command. The results are written into
6234** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6235** successfully, otherwise an error message is written to stderr and
6236** SQLITE_ERROR returned.
6237*/
6238static int arParseCommand(
6239  char **azArg,                   /* Array of arguments passed to dot command */
6240  int nArg,                       /* Number of entries in azArg[] */
6241  ArCommand *pAr                  /* Populate this object */
6242){
6243  struct ArSwitch {
6244    const char *zLong;
6245    char cShort;
6246    u8 eSwitch;
6247    u8 bArg;
6248  } aSwitch[] = {
6249    { "create",    'c', AR_CMD_CREATE,       0 },
6250    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6251    { "insert",    'i', AR_CMD_INSERT,       0 },
6252    { "list",      't', AR_CMD_LIST,         0 },
6253    { "remove",    'r', AR_CMD_REMOVE,       0 },
6254    { "update",    'u', AR_CMD_UPDATE,       0 },
6255    { "help",      'h', AR_CMD_HELP,         0 },
6256    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6257    { "file",      'f', AR_SWITCH_FILE,      1 },
6258    { "append",    'a', AR_SWITCH_APPEND,    1 },
6259    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6260    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6261    { "glob",      'g', AR_SWITCH_GLOB,      0 },
6262  };
6263  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6264  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6265
6266  if( nArg<=1 ){
6267    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6268    return arUsage(stderr);
6269  }else{
6270    char *z = azArg[1];
6271    if( z[0]!='-' ){
6272      /* Traditional style [tar] invocation */
6273      int i;
6274      int iArg = 2;
6275      for(i=0; z[i]; i++){
6276        const char *zArg = 0;
6277        struct ArSwitch *pOpt;
6278        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6279          if( z[i]==pOpt->cShort ) break;
6280        }
6281        if( pOpt==pEnd ){
6282          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6283        }
6284        if( pOpt->bArg ){
6285          if( iArg>=nArg ){
6286            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6287          }
6288          zArg = azArg[iArg++];
6289        }
6290        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6291      }
6292      pAr->nArg = nArg-iArg;
6293      if( pAr->nArg>0 ){
6294        pAr->azArg = &azArg[iArg];
6295      }
6296    }else{
6297      /* Non-traditional invocation */
6298      int iArg;
6299      for(iArg=1; iArg<nArg; iArg++){
6300        int n;
6301        z = azArg[iArg];
6302        if( z[0]!='-' ){
6303          /* All remaining command line words are command arguments. */
6304          pAr->azArg = &azArg[iArg];
6305          pAr->nArg = nArg-iArg;
6306          break;
6307        }
6308        n = strlen30(z);
6309
6310        if( z[1]!='-' ){
6311          int i;
6312          /* One or more short options */
6313          for(i=1; i<n; i++){
6314            const char *zArg = 0;
6315            struct ArSwitch *pOpt;
6316            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6317              if( z[i]==pOpt->cShort ) break;
6318            }
6319            if( pOpt==pEnd ){
6320              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6321            }
6322            if( pOpt->bArg ){
6323              if( i<(n-1) ){
6324                zArg = &z[i+1];
6325                i = n;
6326              }else{
6327                if( iArg>=(nArg-1) ){
6328                  return arErrorMsg(pAr, "option requires an argument: %c",
6329                                    z[i]);
6330                }
6331                zArg = azArg[++iArg];
6332              }
6333            }
6334            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6335          }
6336        }else if( z[2]=='\0' ){
6337          /* A -- option, indicating that all remaining command line words
6338          ** are command arguments.  */
6339          pAr->azArg = &azArg[iArg+1];
6340          pAr->nArg = nArg-iArg-1;
6341          break;
6342        }else{
6343          /* A long option */
6344          const char *zArg = 0;             /* Argument for option, if any */
6345          struct ArSwitch *pMatch = 0;      /* Matching option */
6346          struct ArSwitch *pOpt;            /* Iterator */
6347          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6348            const char *zLong = pOpt->zLong;
6349            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6350              if( pMatch ){
6351                return arErrorMsg(pAr, "ambiguous option: %s",z);
6352              }else{
6353                pMatch = pOpt;
6354              }
6355            }
6356          }
6357
6358          if( pMatch==0 ){
6359            return arErrorMsg(pAr, "unrecognized option: %s", z);
6360          }
6361          if( pMatch->bArg ){
6362            if( iArg>=(nArg-1) ){
6363              return arErrorMsg(pAr, "option requires an argument: %s", z);
6364            }
6365            zArg = azArg[++iArg];
6366          }
6367          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6368        }
6369      }
6370    }
6371  }
6372
6373  return SQLITE_OK;
6374}
6375
6376/*
6377** This function assumes that all arguments within the ArCommand.azArg[]
6378** array refer to archive members, as for the --extract, --list or --remove
6379** commands. It checks that each of them are "present". If any specified
6380** file is not present in the archive, an error is printed to stderr and an
6381** error code returned. Otherwise, if all specified arguments are present
6382** in the archive, SQLITE_OK is returned. Here, "present" means either an
6383** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
6384** when pAr->bGlob is true.
6385**
6386** This function strips any trailing '/' characters from each argument.
6387** This is consistent with the way the [tar] command seems to work on
6388** Linux.
6389*/
6390static int arCheckEntries(ArCommand *pAr){
6391  int rc = SQLITE_OK;
6392  if( pAr->nArg ){
6393    int i, j;
6394    sqlite3_stmt *pTest = 0;
6395    const char *zSel = (pAr->bGlob)
6396      ? "SELECT name FROM %s WHERE glob($name,name)"
6397      : "SELECT name FROM %s WHERE name=$name";
6398
6399    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
6400    j = sqlite3_bind_parameter_index(pTest, "$name");
6401    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6402      char *z = pAr->azArg[i];
6403      int n = strlen30(z);
6404      int bOk = 0;
6405      while( n>0 && z[n-1]=='/' ) n--;
6406      z[n] = '\0';
6407      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6408      if( SQLITE_ROW==sqlite3_step(pTest) ){
6409        bOk = 1;
6410      }
6411      shellReset(&rc, pTest);
6412      if( rc==SQLITE_OK && bOk==0 ){
6413        utf8_printf(stderr, "not found in archive: %s\n", z);
6414        rc = SQLITE_ERROR;
6415      }
6416    }
6417    shellFinalize(&rc, pTest);
6418  }
6419  return rc;
6420}
6421
6422/*
6423** Format a WHERE clause that can be used against the "sqlar" table to
6424** identify all archive members that match the command arguments held
6425** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6426** The caller is responsible for eventually calling sqlite3_free() on
6427** any non-NULL (*pzWhere) value. Here, "match" means strict equality
6428** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
6429*/
6430static void arWhereClause(
6431  int *pRc,
6432  ArCommand *pAr,
6433  char **pzWhere                  /* OUT: New WHERE clause */
6434){
6435  char *zWhere = 0;
6436  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
6437  if( *pRc==SQLITE_OK ){
6438    if( pAr->nArg==0 ){
6439      zWhere = sqlite3_mprintf("1");
6440    }else{
6441      int i;
6442      const char *zSep = "";
6443      for(i=0; i<pAr->nArg; i++){
6444        const char *z = pAr->azArg[i];
6445        zWhere = sqlite3_mprintf(
6446          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
6447          zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
6448        );
6449        if( zWhere==0 ){
6450          *pRc = SQLITE_NOMEM;
6451          break;
6452        }
6453        zSep = " OR ";
6454      }
6455    }
6456  }
6457  *pzWhere = zWhere;
6458}
6459
6460/*
6461** Implementation of .ar "lisT" command.
6462*/
6463static int arListCommand(ArCommand *pAr){
6464  const char *zSql = "SELECT %s FROM %s WHERE %s";
6465  const char *azCols[] = {
6466    "name",
6467    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6468  };
6469
6470  char *zWhere = 0;
6471  sqlite3_stmt *pSql = 0;
6472  int rc;
6473
6474  rc = arCheckEntries(pAr);
6475  arWhereClause(&rc, pAr, &zWhere);
6476
6477  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6478                     pAr->zSrcTable, zWhere);
6479  if( pAr->bDryRun ){
6480    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6481  }else{
6482    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6483      if( pAr->bVerbose ){
6484        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6485            sqlite3_column_text(pSql, 0),
6486            sqlite3_column_int(pSql, 1),
6487            sqlite3_column_text(pSql, 2),
6488            sqlite3_column_text(pSql, 3)
6489        );
6490      }else{
6491        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6492      }
6493    }
6494  }
6495  shellFinalize(&rc, pSql);
6496  sqlite3_free(zWhere);
6497  return rc;
6498}
6499
6500
6501/*
6502** Implementation of .ar "Remove" command.
6503*/
6504static int arRemoveCommand(ArCommand *pAr){
6505  int rc = 0;
6506  char *zSql = 0;
6507  char *zWhere = 0;
6508
6509  if( pAr->nArg ){
6510    /* Verify that args actually exist within the archive before proceeding.
6511    ** And formulate a WHERE clause to match them.  */
6512    rc = arCheckEntries(pAr);
6513    arWhereClause(&rc, pAr, &zWhere);
6514  }
6515  if( rc==SQLITE_OK ){
6516    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
6517                           pAr->zSrcTable, zWhere);
6518    if( pAr->bDryRun ){
6519      utf8_printf(pAr->p->out, "%s\n", zSql);
6520    }else{
6521      char *zErr = 0;
6522      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
6523      if( rc==SQLITE_OK ){
6524        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6525        if( rc!=SQLITE_OK ){
6526          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6527        }else{
6528          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
6529        }
6530      }
6531      if( zErr ){
6532        utf8_printf(stdout, "ERROR: %s\n", zErr);
6533        sqlite3_free(zErr);
6534      }
6535    }
6536  }
6537  sqlite3_free(zWhere);
6538  sqlite3_free(zSql);
6539  return rc;
6540}
6541
6542/*
6543** Implementation of .ar "eXtract" command.
6544*/
6545static int arExtractCommand(ArCommand *pAr){
6546  const char *zSql1 =
6547    "SELECT "
6548    " ($dir || name),"
6549    " writefile(($dir || name), %s, mode, mtime) "
6550    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6551    " AND name NOT GLOB '*..[/\\]*'";
6552
6553  const char *azExtraArg[] = {
6554    "sqlar_uncompress(data, sz)",
6555    "data"
6556  };
6557
6558  sqlite3_stmt *pSql = 0;
6559  int rc = SQLITE_OK;
6560  char *zDir = 0;
6561  char *zWhere = 0;
6562  int i, j;
6563
6564  /* If arguments are specified, check that they actually exist within
6565  ** the archive before proceeding. And formulate a WHERE clause to
6566  ** match them.  */
6567  rc = arCheckEntries(pAr);
6568  arWhereClause(&rc, pAr, &zWhere);
6569
6570  if( rc==SQLITE_OK ){
6571    if( pAr->zDir ){
6572      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6573    }else{
6574      zDir = sqlite3_mprintf("");
6575    }
6576    if( zDir==0 ) rc = SQLITE_NOMEM;
6577  }
6578
6579  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6580      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6581  );
6582
6583  if( rc==SQLITE_OK ){
6584    j = sqlite3_bind_parameter_index(pSql, "$dir");
6585    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6586
6587    /* Run the SELECT statement twice. The first time, writefile() is called
6588    ** for all archive members that should be extracted. The second time,
6589    ** only for the directories. This is because the timestamps for
6590    ** extracted directories must be reset after they are populated (as
6591    ** populating them changes the timestamp).  */
6592    for(i=0; i<2; i++){
6593      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6594      sqlite3_bind_int(pSql, j, i);
6595      if( pAr->bDryRun ){
6596        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6597      }else{
6598        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6599          if( i==0 && pAr->bVerbose ){
6600            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6601          }
6602        }
6603      }
6604      shellReset(&rc, pSql);
6605    }
6606    shellFinalize(&rc, pSql);
6607  }
6608
6609  sqlite3_free(zDir);
6610  sqlite3_free(zWhere);
6611  return rc;
6612}
6613
6614/*
6615** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6616*/
6617static int arExecSql(ArCommand *pAr, const char *zSql){
6618  int rc;
6619  if( pAr->bDryRun ){
6620    utf8_printf(pAr->p->out, "%s\n", zSql);
6621    rc = SQLITE_OK;
6622  }else{
6623    char *zErr = 0;
6624    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6625    if( zErr ){
6626      utf8_printf(stdout, "ERROR: %s\n", zErr);
6627      sqlite3_free(zErr);
6628    }
6629  }
6630  return rc;
6631}
6632
6633
6634/*
6635** Implementation of .ar "create", "insert", and "update" commands.
6636**
6637**     create    ->     Create a new SQL archive
6638**     insert    ->     Insert or reinsert all files listed
6639**     update    ->     Insert files that have changed or that were not
6640**                      previously in the archive
6641**
6642** Create the "sqlar" table in the database if it does not already exist.
6643** Then add each file in the azFile[] array to the archive. Directories
6644** are added recursively. If argument bVerbose is non-zero, a message is
6645** printed on stdout for each file archived.
6646**
6647** The create command is the same as update, except that it drops
6648** any existing "sqlar" table before beginning.  The "insert" command
6649** always overwrites every file named on the command-line, where as
6650** "update" only overwrites if the size or mtime or mode has changed.
6651*/
6652static int arCreateOrUpdateCommand(
6653  ArCommand *pAr,                 /* Command arguments and options */
6654  int bUpdate,                    /* true for a --create. */
6655  int bOnlyIfChanged              /* Only update if file has changed */
6656){
6657  const char *zCreate =
6658      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6659      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6660      "  mode INT,               -- access permissions\n"
6661      "  mtime INT,              -- last modification time\n"
6662      "  sz INT,                 -- original file size\n"
6663      "  data BLOB               -- compressed content\n"
6664      ")";
6665  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6666  const char *zInsertFmt[2] = {
6667     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6668     "  SELECT\n"
6669     "    %s,\n"
6670     "    mode,\n"
6671     "    mtime,\n"
6672     "    CASE substr(lsmode(mode),1,1)\n"
6673     "      WHEN '-' THEN length(data)\n"
6674     "      WHEN 'd' THEN 0\n"
6675     "      ELSE -1 END,\n"
6676     "    sqlar_compress(data)\n"
6677     "  FROM fsdir(%Q,%Q) AS disk\n"
6678     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6679     ,
6680     "REPLACE INTO %s(name,mode,mtime,data)\n"
6681     "  SELECT\n"
6682     "    %s,\n"
6683     "    mode,\n"
6684     "    mtime,\n"
6685     "    data\n"
6686     "  FROM fsdir(%Q,%Q) AS disk\n"
6687     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6688  };
6689  int i;                          /* For iterating through azFile[] */
6690  int rc;                         /* Return code */
6691  const char *zTab = 0;           /* SQL table into which to insert */
6692  char *zSql;
6693  char zTemp[50];
6694  char *zExists = 0;
6695
6696  arExecSql(pAr, "PRAGMA page_size=512");
6697  rc = arExecSql(pAr, "SAVEPOINT ar;");
6698  if( rc!=SQLITE_OK ) return rc;
6699  zTemp[0] = 0;
6700  if( pAr->bZip ){
6701    /* Initialize the zipfile virtual table, if necessary */
6702    if( pAr->zFile ){
6703      sqlite3_uint64 r;
6704      sqlite3_randomness(sizeof(r),&r);
6705      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6706      zTab = zTemp;
6707      zSql = sqlite3_mprintf(
6708         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6709         zTab, pAr->zFile
6710      );
6711      rc = arExecSql(pAr, zSql);
6712      sqlite3_free(zSql);
6713    }else{
6714      zTab = "zip";
6715    }
6716  }else{
6717    /* Initialize the table for an SQLAR */
6718    zTab = "sqlar";
6719    if( bUpdate==0 ){
6720      rc = arExecSql(pAr, zDrop);
6721      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6722    }
6723    rc = arExecSql(pAr, zCreate);
6724  }
6725  if( bOnlyIfChanged ){
6726    zExists = sqlite3_mprintf(
6727      " AND NOT EXISTS("
6728          "SELECT 1 FROM %s AS mem"
6729          " WHERE mem.name=disk.name"
6730          " AND mem.mtime=disk.mtime"
6731          " AND mem.mode=disk.mode)", zTab);
6732  }else{
6733    zExists = sqlite3_mprintf("");
6734  }
6735  if( zExists==0 ) rc = SQLITE_NOMEM;
6736  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6737    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6738        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6739        pAr->azArg[i], pAr->zDir, zExists);
6740    rc = arExecSql(pAr, zSql2);
6741    sqlite3_free(zSql2);
6742  }
6743end_ar_transaction:
6744  if( rc!=SQLITE_OK ){
6745    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6746  }else{
6747    rc = arExecSql(pAr, "RELEASE ar;");
6748    if( pAr->bZip && pAr->zFile ){
6749      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6750      arExecSql(pAr, zSql);
6751      sqlite3_free(zSql);
6752    }
6753  }
6754  sqlite3_free(zExists);
6755  return rc;
6756}
6757
6758/*
6759** Implementation of ".ar" dot command.
6760*/
6761static int arDotCommand(
6762  ShellState *pState,          /* Current shell tool state */
6763  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6764  char **azArg,                /* Array of arguments passed to dot command */
6765  int nArg                     /* Number of entries in azArg[] */
6766){
6767  ArCommand cmd;
6768  int rc;
6769  memset(&cmd, 0, sizeof(cmd));
6770  cmd.fromCmdLine = fromCmdLine;
6771  rc = arParseCommand(azArg, nArg, &cmd);
6772  if( rc==SQLITE_OK ){
6773    int eDbType = SHELL_OPEN_UNSPEC;
6774    cmd.p = pState;
6775    cmd.db = pState->db;
6776    if( cmd.zFile ){
6777      eDbType = deduceDatabaseType(cmd.zFile, 1);
6778    }else{
6779      eDbType = pState->openMode;
6780    }
6781    if( eDbType==SHELL_OPEN_ZIPFILE ){
6782      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6783        if( cmd.zFile==0 ){
6784          cmd.zSrcTable = sqlite3_mprintf("zip");
6785        }else{
6786          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6787        }
6788      }
6789      cmd.bZip = 1;
6790    }else if( cmd.zFile ){
6791      int flags;
6792      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6793      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6794           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
6795        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6796      }else{
6797        flags = SQLITE_OPEN_READONLY;
6798      }
6799      cmd.db = 0;
6800      if( cmd.bDryRun ){
6801        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6802             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6803      }
6804      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6805             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6806      if( rc!=SQLITE_OK ){
6807        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6808            cmd.zFile, sqlite3_errmsg(cmd.db)
6809        );
6810        goto end_ar_command;
6811      }
6812      sqlite3_fileio_init(cmd.db, 0, 0);
6813      sqlite3_sqlar_init(cmd.db, 0, 0);
6814      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6815                              shellPutsFunc, 0, 0);
6816
6817    }
6818    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6819      if( cmd.eCmd!=AR_CMD_CREATE
6820       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6821      ){
6822        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6823        rc = SQLITE_ERROR;
6824        goto end_ar_command;
6825      }
6826      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6827    }
6828
6829    switch( cmd.eCmd ){
6830      case AR_CMD_CREATE:
6831        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6832        break;
6833
6834      case AR_CMD_EXTRACT:
6835        rc = arExtractCommand(&cmd);
6836        break;
6837
6838      case AR_CMD_LIST:
6839        rc = arListCommand(&cmd);
6840        break;
6841
6842      case AR_CMD_HELP:
6843        arUsage(pState->out);
6844        break;
6845
6846      case AR_CMD_INSERT:
6847        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6848        break;
6849
6850      case AR_CMD_REMOVE:
6851        rc = arRemoveCommand(&cmd);
6852        break;
6853
6854      default:
6855        assert( cmd.eCmd==AR_CMD_UPDATE );
6856        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6857        break;
6858    }
6859  }
6860end_ar_command:
6861  if( cmd.db!=pState->db ){
6862    close_db(cmd.db);
6863  }
6864  sqlite3_free(cmd.zSrcTable);
6865
6866  return rc;
6867}
6868/* End of the ".archive" or ".ar" command logic
6869*******************************************************************************/
6870#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6871
6872#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6873/*
6874** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6875** Otherwise, the SQL statement or statements in zSql are executed using
6876** database connection db and the error code written to *pRc before
6877** this function returns.
6878*/
6879static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6880  int rc = *pRc;
6881  if( rc==SQLITE_OK ){
6882    char *zErr = 0;
6883    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6884    if( rc!=SQLITE_OK ){
6885      raw_printf(stderr, "SQL error: %s\n", zErr);
6886    }
6887    sqlite3_free(zErr);
6888    *pRc = rc;
6889  }
6890}
6891
6892/*
6893** Like shellExec(), except that zFmt is a printf() style format string.
6894*/
6895static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6896  char *z = 0;
6897  if( *pRc==SQLITE_OK ){
6898    va_list ap;
6899    va_start(ap, zFmt);
6900    z = sqlite3_vmprintf(zFmt, ap);
6901    va_end(ap);
6902    if( z==0 ){
6903      *pRc = SQLITE_NOMEM;
6904    }else{
6905      shellExec(db, pRc, z);
6906    }
6907    sqlite3_free(z);
6908  }
6909}
6910
6911/*
6912** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6913** Otherwise, an attempt is made to allocate, zero and return a pointer
6914** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6915** to SQLITE_NOMEM and NULL returned.
6916*/
6917static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6918  void *pRet = 0;
6919  if( *pRc==SQLITE_OK ){
6920    pRet = sqlite3_malloc64(nByte);
6921    if( pRet==0 ){
6922      *pRc = SQLITE_NOMEM;
6923    }else{
6924      memset(pRet, 0, nByte);
6925    }
6926  }
6927  return pRet;
6928}
6929
6930/*
6931** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6932** Otherwise, zFmt is treated as a printf() style string. The result of
6933** formatting it along with any trailing arguments is written into a
6934** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6935** It is the responsibility of the caller to eventually free this buffer
6936** using a call to sqlite3_free().
6937**
6938** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6939** pointer returned.
6940*/
6941static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6942  char *z = 0;
6943  if( *pRc==SQLITE_OK ){
6944    va_list ap;
6945    va_start(ap, zFmt);
6946    z = sqlite3_vmprintf(zFmt, ap);
6947    va_end(ap);
6948    if( z==0 ){
6949      *pRc = SQLITE_NOMEM;
6950    }
6951  }
6952  return z;
6953}
6954
6955/*
6956** When running the ".recover" command, each output table, and the special
6957** orphaned row table if it is required, is represented by an instance
6958** of the following struct.
6959*/
6960typedef struct RecoverTable RecoverTable;
6961struct RecoverTable {
6962  char *zQuoted;                  /* Quoted version of table name */
6963  int nCol;                       /* Number of columns in table */
6964  char **azlCol;                  /* Array of column lists */
6965  int iPk;                        /* Index of IPK column */
6966};
6967
6968/*
6969** Free a RecoverTable object allocated by recoverFindTable() or
6970** recoverOrphanTable().
6971*/
6972static void recoverFreeTable(RecoverTable *pTab){
6973  if( pTab ){
6974    sqlite3_free(pTab->zQuoted);
6975    if( pTab->azlCol ){
6976      int i;
6977      for(i=0; i<=pTab->nCol; i++){
6978        sqlite3_free(pTab->azlCol[i]);
6979      }
6980      sqlite3_free(pTab->azlCol);
6981    }
6982    sqlite3_free(pTab);
6983  }
6984}
6985
6986/*
6987** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6988** Otherwise, it allocates and returns a RecoverTable object based on the
6989** final four arguments passed to this function. It is the responsibility
6990** of the caller to eventually free the returned object using
6991** recoverFreeTable().
6992*/
6993static RecoverTable *recoverNewTable(
6994  int *pRc,                       /* IN/OUT: Error code */
6995  const char *zName,              /* Name of table */
6996  const char *zSql,               /* CREATE TABLE statement */
6997  int bIntkey,
6998  int nCol
6999){
7000  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
7001  int rc = *pRc;
7002  RecoverTable *pTab = 0;
7003
7004  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
7005  if( rc==SQLITE_OK ){
7006    int nSqlCol = 0;
7007    int bSqlIntkey = 0;
7008    sqlite3_stmt *pStmt = 0;
7009
7010    rc = sqlite3_open("", &dbtmp);
7011    if( rc==SQLITE_OK ){
7012      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
7013                              shellIdQuote, 0, 0);
7014    }
7015    if( rc==SQLITE_OK ){
7016      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
7017    }
7018    if( rc==SQLITE_OK ){
7019      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
7020      if( rc==SQLITE_ERROR ){
7021        rc = SQLITE_OK;
7022        goto finished;
7023      }
7024    }
7025    shellPreparePrintf(dbtmp, &rc, &pStmt,
7026        "SELECT count(*) FROM pragma_table_info(%Q)", zName
7027    );
7028    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7029      nSqlCol = sqlite3_column_int(pStmt, 0);
7030    }
7031    shellFinalize(&rc, pStmt);
7032
7033    if( rc!=SQLITE_OK || nSqlCol<nCol ){
7034      goto finished;
7035    }
7036
7037    shellPreparePrintf(dbtmp, &rc, &pStmt,
7038      "SELECT ("
7039      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
7040      ") FROM sqlite_schema WHERE name = %Q", zName
7041    );
7042    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7043      bSqlIntkey = sqlite3_column_int(pStmt, 0);
7044    }
7045    shellFinalize(&rc, pStmt);
7046
7047    if( bIntkey==bSqlIntkey ){
7048      int i;
7049      const char *zPk = "_rowid_";
7050      sqlite3_stmt *pPkFinder = 0;
7051
7052      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
7053      ** set zPk to the name of the PK column, and pTab->iPk to the index
7054      ** of the column, where columns are 0-numbered from left to right.
7055      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
7056      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
7057      pTab->iPk = -2;
7058      if( bIntkey ){
7059        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
7060          "SELECT cid, name FROM pragma_table_info(%Q) "
7061          "  WHERE pk=1 AND type='integer' COLLATE nocase"
7062          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
7063          , zName, zName
7064        );
7065        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
7066          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
7067          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
7068        }
7069      }
7070
7071      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
7072      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
7073      pTab->nCol = nSqlCol;
7074
7075      if( bIntkey ){
7076        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
7077      }else{
7078        pTab->azlCol[0] = shellMPrintf(&rc, "");
7079      }
7080      i = 1;
7081      shellPreparePrintf(dbtmp, &rc, &pStmt,
7082          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
7083          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
7084          "FROM pragma_table_info(%Q)",
7085          bIntkey ? ", " : "", pTab->iPk,
7086          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
7087          zName
7088      );
7089      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7090        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
7091        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
7092        i++;
7093      }
7094      shellFinalize(&rc, pStmt);
7095
7096      shellFinalize(&rc, pPkFinder);
7097    }
7098  }
7099
7100 finished:
7101  sqlite3_close(dbtmp);
7102  *pRc = rc;
7103  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
7104    recoverFreeTable(pTab);
7105    pTab = 0;
7106  }
7107  return pTab;
7108}
7109
7110/*
7111** This function is called to search the schema recovered from the
7112** sqlite_schema table of the (possibly) corrupt database as part
7113** of a ".recover" command. Specifically, for a table with root page
7114** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
7115** table must be a WITHOUT ROWID table, or if non-zero, not one of
7116** those.
7117**
7118** If a table is found, a (RecoverTable*) object is returned. Or, if
7119** no such table is found, but bIntkey is false and iRoot is the
7120** root page of an index in the recovered schema, then (*pbNoop) is
7121** set to true and NULL returned. Or, if there is no such table or
7122** index, NULL is returned and (*pbNoop) set to 0, indicating that
7123** the caller should write data to the orphans table.
7124*/
7125static RecoverTable *recoverFindTable(
7126  ShellState *pState,             /* Shell state object */
7127  int *pRc,                       /* IN/OUT: Error code */
7128  int iRoot,                      /* Root page of table */
7129  int bIntkey,                    /* True for an intkey table */
7130  int nCol,                       /* Number of columns in table */
7131  int *pbNoop                     /* OUT: True if iRoot is root of index */
7132){
7133  sqlite3_stmt *pStmt = 0;
7134  RecoverTable *pRet = 0;
7135  int bNoop = 0;
7136  const char *zSql = 0;
7137  const char *zName = 0;
7138
7139  /* Search the recovered schema for an object with root page iRoot. */
7140  shellPreparePrintf(pState->db, pRc, &pStmt,
7141      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
7142  );
7143  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7144    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
7145    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
7146      bNoop = 1;
7147      break;
7148    }
7149    if( sqlite3_stricmp(zType, "table")==0 ){
7150      zName = (const char*)sqlite3_column_text(pStmt, 1);
7151      zSql = (const char*)sqlite3_column_text(pStmt, 2);
7152      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
7153      break;
7154    }
7155  }
7156
7157  shellFinalize(pRc, pStmt);
7158  *pbNoop = bNoop;
7159  return pRet;
7160}
7161
7162/*
7163** Return a RecoverTable object representing the orphans table.
7164*/
7165static RecoverTable *recoverOrphanTable(
7166  ShellState *pState,             /* Shell state object */
7167  int *pRc,                       /* IN/OUT: Error code */
7168  const char *zLostAndFound,      /* Base name for orphans table */
7169  int nCol                        /* Number of user data columns */
7170){
7171  RecoverTable *pTab = 0;
7172  if( nCol>=0 && *pRc==SQLITE_OK ){
7173    int i;
7174
7175    /* This block determines the name of the orphan table. The prefered
7176    ** name is zLostAndFound. But if that clashes with another name
7177    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7178    ** and so on until a non-clashing name is found.  */
7179    int iTab = 0;
7180    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7181    sqlite3_stmt *pTest = 0;
7182    shellPrepare(pState->db, pRc,
7183        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7184    );
7185    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7186    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7187      shellReset(pRc, pTest);
7188      sqlite3_free(zTab);
7189      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7190      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7191    }
7192    shellFinalize(pRc, pTest);
7193
7194    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7195    if( pTab ){
7196      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7197      pTab->nCol = nCol;
7198      pTab->iPk = -2;
7199      if( nCol>0 ){
7200        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7201        if( pTab->azlCol ){
7202          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7203          for(i=nCol-1; i>=0; i--){
7204            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7205          }
7206        }
7207      }
7208
7209      if( *pRc!=SQLITE_OK ){
7210        recoverFreeTable(pTab);
7211        pTab = 0;
7212      }else{
7213        raw_printf(pState->out,
7214            "CREATE TABLE %s(rootpgno INTEGER, "
7215            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7216        );
7217        for(i=0; i<nCol; i++){
7218          raw_printf(pState->out, ", c%d", i);
7219        }
7220        raw_printf(pState->out, ");\n");
7221      }
7222    }
7223    sqlite3_free(zTab);
7224  }
7225  return pTab;
7226}
7227
7228/*
7229** This function is called to recover data from the database. A script
7230** to construct a new database containing all recovered data is output
7231** on stream pState->out.
7232*/
7233static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7234  int rc = SQLITE_OK;
7235  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7236  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7237  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7238  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7239  const char *zLostAndFound = "lost_and_found";
7240  int i;
7241  int nOrphan = -1;
7242  RecoverTable *pOrphan = 0;
7243
7244  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7245  int bRowids = 1;                /* 0 if --no-rowids */
7246  for(i=1; i<nArg; i++){
7247    char *z = azArg[i];
7248    int n;
7249    if( z[0]=='-' && z[1]=='-' ) z++;
7250    n = strlen30(z);
7251    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7252      bFreelist = 0;
7253    }else
7254    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7255      i++;
7256      zRecoveryDb = azArg[i];
7257    }else
7258    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7259      i++;
7260      zLostAndFound = azArg[i];
7261    }else
7262    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7263      bRowids = 0;
7264    }
7265    else{
7266      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7267      showHelp(pState->out, azArg[0]);
7268      return 1;
7269    }
7270  }
7271
7272  shellExecPrintf(pState->db, &rc,
7273    /* Attach an in-memory database named 'recovery'. Create an indexed
7274    ** cache of the sqlite_dbptr virtual table. */
7275    "PRAGMA writable_schema = on;"
7276    "ATTACH %Q AS recovery;"
7277    "DROP TABLE IF EXISTS recovery.dbptr;"
7278    "DROP TABLE IF EXISTS recovery.freelist;"
7279    "DROP TABLE IF EXISTS recovery.map;"
7280    "DROP TABLE IF EXISTS recovery.schema;"
7281    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7282  );
7283
7284  if( bFreelist ){
7285    shellExec(pState->db, &rc,
7286      "WITH trunk(pgno) AS ("
7287      "  SELECT shell_int32("
7288      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7289      "      WHERE x>0"
7290      "    UNION"
7291      "  SELECT shell_int32("
7292      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7293      "      FROM trunk WHERE x>0"
7294      "),"
7295      "freelist(data, n, freepgno) AS ("
7296      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7297      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7298      "    UNION ALL"
7299      "  SELECT data, n-1, shell_int32(data, 2+n) "
7300      "      FROM freelist WHERE n>=0"
7301      ")"
7302      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7303    );
7304  }
7305
7306  /* If this is an auto-vacuum database, add all pointer-map pages to
7307  ** the freelist table. Do this regardless of whether or not
7308  ** --freelist-corrupt was specified.  */
7309  shellExec(pState->db, &rc,
7310    "WITH ptrmap(pgno) AS ("
7311    "  SELECT 2 WHERE shell_int32("
7312    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7313    "  )"
7314    "    UNION ALL "
7315    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7316    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7317    ")"
7318    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7319  );
7320
7321  shellExec(pState->db, &rc,
7322    "CREATE TABLE recovery.dbptr("
7323    "      pgno, child, PRIMARY KEY(child, pgno)"
7324    ") WITHOUT ROWID;"
7325    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7326    "    SELECT * FROM sqlite_dbptr"
7327    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7328
7329    /* Delete any pointer to page 1. This ensures that page 1 is considered
7330    ** a root page, regardless of how corrupt the db is. */
7331    "DELETE FROM recovery.dbptr WHERE child = 1;"
7332
7333    /* Delete all pointers to any pages that have more than one pointer
7334    ** to them. Such pages will be treated as root pages when recovering
7335    ** data.  */
7336    "DELETE FROM recovery.dbptr WHERE child IN ("
7337    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7338    ");"
7339
7340    /* Create the "map" table that will (eventually) contain instructions
7341    ** for dealing with each page in the db that contains one or more
7342    ** records. */
7343    "CREATE TABLE recovery.map("
7344      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7345    ");"
7346
7347    /* Populate table [map]. If there are circular loops of pages in the
7348    ** database, the following adds all pages in such a loop to the map
7349    ** as individual root pages. This could be handled better.  */
7350    "WITH pages(i, maxlen) AS ("
7351    "  SELECT page_count, ("
7352    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7353    "  ) FROM pragma_page_count WHERE page_count>0"
7354    "    UNION ALL"
7355    "  SELECT i-1, ("
7356    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7357    "  ) FROM pages WHERE i>=2"
7358    ")"
7359    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7360    "  SELECT i, maxlen, NULL, ("
7361    "    WITH p(orig, pgno, parent) AS ("
7362    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7363    "        UNION "
7364    "      SELECT i, p.parent, "
7365    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7366    "    )"
7367    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7368    ") "
7369    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7370    "UPDATE recovery.map AS o SET intkey = ("
7371    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7372    ");"
7373
7374    /* Extract data from page 1 and any linked pages into table
7375    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7376    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7377    "INSERT INTO recovery.schema SELECT "
7378    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7379    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7380    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7381    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7382    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7383    "FROM sqlite_dbdata WHERE pgno IN ("
7384    "  SELECT pgno FROM recovery.map WHERE root=1"
7385    ")"
7386    "GROUP BY pgno, cell;"
7387    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7388  );
7389
7390  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7391  ** CREATE TABLE statements that extracted from the existing schema.  */
7392  if( rc==SQLITE_OK ){
7393    sqlite3_stmt *pStmt = 0;
7394    /* ".recover" might output content in an order which causes immediate
7395    ** foreign key constraints to be violated. So disable foreign-key
7396    ** constraint enforcement to prevent problems when running the output
7397    ** script. */
7398    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7399    raw_printf(pState->out, "BEGIN;\n");
7400    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7401    shellPrepare(pState->db, &rc,
7402        "SELECT sql FROM recovery.schema "
7403        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7404    );
7405    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7406      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7407      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7408          &zCreateTable[12]
7409      );
7410    }
7411    shellFinalize(&rc, pStmt);
7412  }
7413
7414  /* Figure out if an orphan table will be required. And if so, how many
7415  ** user columns it should contain */
7416  shellPrepare(pState->db, &rc,
7417      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7418      , &pLoop
7419  );
7420  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7421    nOrphan = sqlite3_column_int(pLoop, 0);
7422  }
7423  shellFinalize(&rc, pLoop);
7424  pLoop = 0;
7425
7426  shellPrepare(pState->db, &rc,
7427      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7428  );
7429
7430  shellPrepare(pState->db, &rc,
7431      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7432      "(case when (? AND field<0) then NULL else value end)"
7433      "), ', ')"
7434      ", min(field) "
7435      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7436      "GROUP BY cell", &pCells
7437  );
7438
7439  /* Loop through each root page. */
7440  shellPrepare(pState->db, &rc,
7441      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7442      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7443      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7444      ")", &pLoop
7445  );
7446  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7447    int iRoot = sqlite3_column_int(pLoop, 0);
7448    int bIntkey = sqlite3_column_int(pLoop, 1);
7449    int nCol = sqlite3_column_int(pLoop, 2);
7450    int bNoop = 0;
7451    RecoverTable *pTab;
7452
7453    assert( bIntkey==0 || bIntkey==1 );
7454    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7455    if( bNoop || rc ) continue;
7456    if( pTab==0 ){
7457      if( pOrphan==0 ){
7458        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7459      }
7460      pTab = pOrphan;
7461      if( pTab==0 ) break;
7462    }
7463
7464    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7465      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7466    }
7467    sqlite3_bind_int(pPages, 1, iRoot);
7468    if( bRowids==0 && pTab->iPk<0 ){
7469      sqlite3_bind_int(pCells, 1, 1);
7470    }else{
7471      sqlite3_bind_int(pCells, 1, 0);
7472    }
7473    sqlite3_bind_int(pCells, 3, pTab->iPk);
7474
7475    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7476      int iPgno = sqlite3_column_int(pPages, 0);
7477      sqlite3_bind_int(pCells, 2, iPgno);
7478      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7479        int nField = sqlite3_column_int(pCells, 0);
7480        int iMin = sqlite3_column_int(pCells, 2);
7481        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7482
7483        RecoverTable *pTab2 = pTab;
7484        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7485          if( pOrphan==0 ){
7486            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7487          }
7488          pTab2 = pOrphan;
7489          if( pTab2==0 ) break;
7490        }
7491
7492        nField = nField+1;
7493        if( pTab2==pOrphan ){
7494          raw_printf(pState->out,
7495              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7496              pTab2->zQuoted, iRoot, iPgno, nField,
7497              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7498          );
7499        }else{
7500          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7501              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7502          );
7503        }
7504      }
7505      shellReset(&rc, pCells);
7506    }
7507    shellReset(&rc, pPages);
7508    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7509  }
7510  shellFinalize(&rc, pLoop);
7511  shellFinalize(&rc, pPages);
7512  shellFinalize(&rc, pCells);
7513  recoverFreeTable(pOrphan);
7514
7515  /* The rest of the schema */
7516  if( rc==SQLITE_OK ){
7517    sqlite3_stmt *pStmt = 0;
7518    shellPrepare(pState->db, &rc,
7519        "SELECT sql, name FROM recovery.schema "
7520        "WHERE sql NOT LIKE 'create table%'", &pStmt
7521    );
7522    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7523      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7524      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7525        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7526        char *zPrint = shellMPrintf(&rc,
7527          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7528          zName, zName, zSql
7529        );
7530        raw_printf(pState->out, "%s;\n", zPrint);
7531        sqlite3_free(zPrint);
7532      }else{
7533        raw_printf(pState->out, "%s;\n", zSql);
7534      }
7535    }
7536    shellFinalize(&rc, pStmt);
7537  }
7538
7539  if( rc==SQLITE_OK ){
7540    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7541    raw_printf(pState->out, "COMMIT;\n");
7542  }
7543  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7544  return rc;
7545}
7546#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7547
7548/*
7549** If an input line begins with "." then invoke this routine to
7550** process that line.
7551**
7552** Return 1 on error, 2 to exit, and 0 otherwise.
7553*/
7554static int do_meta_command(char *zLine, ShellState *p){
7555  int h = 1;
7556  int nArg = 0;
7557  int n, c;
7558  int rc = 0;
7559  char *azArg[52];
7560
7561#ifndef SQLITE_OMIT_VIRTUALTABLE
7562  if( p->expert.pExpert ){
7563    expertFinish(p, 1, 0);
7564  }
7565#endif
7566
7567  /* Parse the input line into tokens.
7568  */
7569  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7570    while( IsSpace(zLine[h]) ){ h++; }
7571    if( zLine[h]==0 ) break;
7572    if( zLine[h]=='\'' || zLine[h]=='"' ){
7573      int delim = zLine[h++];
7574      azArg[nArg++] = &zLine[h];
7575      while( zLine[h] && zLine[h]!=delim ){
7576        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7577        h++;
7578      }
7579      if( zLine[h]==delim ){
7580        zLine[h++] = 0;
7581      }
7582      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7583    }else{
7584      azArg[nArg++] = &zLine[h];
7585      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7586      if( zLine[h] ) zLine[h++] = 0;
7587      resolve_backslashes(azArg[nArg-1]);
7588    }
7589  }
7590  azArg[nArg] = 0;
7591
7592  /* Process the input line.
7593  */
7594  if( nArg==0 ) return 0; /* no tokens, no error */
7595  n = strlen30(azArg[0]);
7596  c = azArg[0][0];
7597  clearTempFile(p);
7598
7599#ifndef SQLITE_OMIT_AUTHORIZATION
7600  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7601    if( nArg!=2 ){
7602      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7603      rc = 1;
7604      goto meta_command_exit;
7605    }
7606    open_db(p, 0);
7607    if( booleanValue(azArg[1]) ){
7608      sqlite3_set_authorizer(p->db, shellAuth, p);
7609    }else if( p->bSafeModePersist ){
7610      sqlite3_set_authorizer(p->db, safeModeAuth, p);
7611    }else{
7612      sqlite3_set_authorizer(p->db, 0, 0);
7613    }
7614  }else
7615#endif
7616
7617#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7618  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7619    open_db(p, 0);
7620    failIfSafeMode(p, "cannot run .archive in safe mode");
7621    rc = arDotCommand(p, 0, azArg, nArg);
7622  }else
7623#endif
7624
7625  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7626   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7627  ){
7628    const char *zDestFile = 0;
7629    const char *zDb = 0;
7630    sqlite3 *pDest;
7631    sqlite3_backup *pBackup;
7632    int j;
7633    int bAsync = 0;
7634    const char *zVfs = 0;
7635    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
7636    for(j=1; j<nArg; j++){
7637      const char *z = azArg[j];
7638      if( z[0]=='-' ){
7639        if( z[1]=='-' ) z++;
7640        if( strcmp(z, "-append")==0 ){
7641          zVfs = "apndvfs";
7642        }else
7643        if( strcmp(z, "-async")==0 ){
7644          bAsync = 1;
7645        }else
7646        {
7647          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7648          return 1;
7649        }
7650      }else if( zDestFile==0 ){
7651        zDestFile = azArg[j];
7652      }else if( zDb==0 ){
7653        zDb = zDestFile;
7654        zDestFile = azArg[j];
7655      }else{
7656        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7657        return 1;
7658      }
7659    }
7660    if( zDestFile==0 ){
7661      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7662      return 1;
7663    }
7664    if( zDb==0 ) zDb = "main";
7665    rc = sqlite3_open_v2(zDestFile, &pDest,
7666                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7667    if( rc!=SQLITE_OK ){
7668      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7669      close_db(pDest);
7670      return 1;
7671    }
7672    if( bAsync ){
7673      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7674                   0, 0, 0);
7675    }
7676    open_db(p, 0);
7677    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7678    if( pBackup==0 ){
7679      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7680      close_db(pDest);
7681      return 1;
7682    }
7683    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7684    sqlite3_backup_finish(pBackup);
7685    if( rc==SQLITE_DONE ){
7686      rc = 0;
7687    }else{
7688      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7689      rc = 1;
7690    }
7691    close_db(pDest);
7692  }else
7693
7694  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7695    if( nArg==2 ){
7696      bail_on_error = booleanValue(azArg[1]);
7697    }else{
7698      raw_printf(stderr, "Usage: .bail on|off\n");
7699      rc = 1;
7700    }
7701  }else
7702
7703  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7704    if( nArg==2 ){
7705      if( booleanValue(azArg[1]) ){
7706        setBinaryMode(p->out, 1);
7707      }else{
7708        setTextMode(p->out, 1);
7709      }
7710    }else{
7711      raw_printf(stderr, "Usage: .binary on|off\n");
7712      rc = 1;
7713    }
7714  }else
7715
7716  /* The undocumented ".breakpoint" command causes a call to the no-op
7717  ** routine named test_breakpoint().
7718  */
7719  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7720    test_breakpoint();
7721  }else
7722
7723  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7724    failIfSafeMode(p, "cannot run .cd in safe mode");
7725    if( nArg==2 ){
7726#if defined(_WIN32) || defined(WIN32)
7727      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7728      rc = !SetCurrentDirectoryW(z);
7729      sqlite3_free(z);
7730#else
7731      rc = chdir(azArg[1]);
7732#endif
7733      if( rc ){
7734        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7735        rc = 1;
7736      }
7737    }else{
7738      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7739      rc = 1;
7740    }
7741  }else
7742
7743  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7744    if( nArg==2 ){
7745      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7746    }else{
7747      raw_printf(stderr, "Usage: .changes on|off\n");
7748      rc = 1;
7749    }
7750  }else
7751
7752  /* Cancel output redirection, if it is currently set (by .testcase)
7753  ** Then read the content of the testcase-out.txt file and compare against
7754  ** azArg[1].  If there are differences, report an error and exit.
7755  */
7756  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7757    char *zRes = 0;
7758    output_reset(p);
7759    if( nArg!=2 ){
7760      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7761      rc = 2;
7762    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7763      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7764      rc = 2;
7765    }else if( testcase_glob(azArg[1],zRes)==0 ){
7766      utf8_printf(stderr,
7767                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7768                 p->zTestcase, azArg[1], zRes);
7769      rc = 1;
7770    }else{
7771      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7772      p->nCheck++;
7773    }
7774    sqlite3_free(zRes);
7775  }else
7776
7777  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7778    failIfSafeMode(p, "cannot run .clone in safe mode");
7779    if( nArg==2 ){
7780      tryToClone(p, azArg[1]);
7781    }else{
7782      raw_printf(stderr, "Usage: .clone FILENAME\n");
7783      rc = 1;
7784    }
7785  }else
7786
7787  if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
7788    if( nArg==1 ){
7789      /* List available connections */
7790      int i;
7791      for(i=0; i<ArraySize(p->aAuxDb); i++){
7792        const char *zFile = p->aAuxDb[i].zDbFilename;
7793        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
7794          zFile = "(not open)";
7795        }else if( zFile==0 ){
7796          zFile = "(memory)";
7797        }else if( zFile[0]==0 ){
7798          zFile = "(temporary-file)";
7799        }
7800        if( p->pAuxDb == &p->aAuxDb[i] ){
7801          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
7802        }else if( p->aAuxDb[i].db!=0 ){
7803          utf8_printf(stdout, "       %d: %s\n", i, zFile);
7804        }
7805      }
7806    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
7807      int i = azArg[1][0] - '0';
7808      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
7809        p->pAuxDb->db = p->db;
7810        p->pAuxDb = &p->aAuxDb[i];
7811        globalDb = p->db = p->pAuxDb->db;
7812        p->pAuxDb->db = 0;
7813      }
7814    }else if( nArg==3 && strcmp(azArg[1], "close")==0
7815           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
7816      int i = azArg[2][0] - '0';
7817      if( i<0 || i>=ArraySize(p->aAuxDb) ){
7818        /* No-op */
7819      }else if( p->pAuxDb == &p->aAuxDb[i] ){
7820        raw_printf(stderr, "cannot close the active database connection\n");
7821        rc = 1;
7822      }else if( p->aAuxDb[i].db ){
7823        session_close_all(p, i);
7824        close_db(p->aAuxDb[i].db);
7825        p->aAuxDb[i].db = 0;
7826      }
7827    }else{
7828      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
7829      rc = 1;
7830    }
7831  }else
7832
7833  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7834    char **azName = 0;
7835    int nName = 0;
7836    sqlite3_stmt *pStmt;
7837    int i;
7838    open_db(p, 0);
7839    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7840    if( rc ){
7841      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7842      rc = 1;
7843    }else{
7844      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7845        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7846        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7847        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7848        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7849        azName[nName*2] = strdup(zSchema);
7850        azName[nName*2+1] = strdup(zFile);
7851        nName++;
7852      }
7853    }
7854    sqlite3_finalize(pStmt);
7855    for(i=0; i<nName; i++){
7856      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7857      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7858      const char *z = azName[i*2+1];
7859      utf8_printf(p->out, "%s: %s %s%s\n",
7860         azName[i*2],
7861         z && z[0] ? z : "\"\"",
7862         bRdonly ? "r/o" : "r/w",
7863         eTxn==SQLITE_TXN_NONE ? "" :
7864            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7865      free(azName[i*2]);
7866      free(azName[i*2+1]);
7867    }
7868    sqlite3_free(azName);
7869  }else
7870
7871  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7872    static const struct DbConfigChoices {
7873      const char *zName;
7874      int op;
7875    } aDbConfig[] = {
7876        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7877        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7878        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7879        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7880        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7881        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7882        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7883        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7884        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7885        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7886        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7887        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7888        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7889        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7890        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7891        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7892    };
7893    int ii, v;
7894    open_db(p, 0);
7895    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7896      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7897      if( nArg>=3 ){
7898        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7899      }
7900      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7901      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7902      if( nArg>1 ) break;
7903    }
7904    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7905      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7906      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7907    }
7908  }else
7909
7910  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7911    rc = shell_dbinfo_command(p, nArg, azArg);
7912  }else
7913
7914#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7915  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7916    open_db(p, 0);
7917    rc = recoverDatabaseCmd(p, nArg, azArg);
7918  }else
7919#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7920
7921  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7922    char *zLike = 0;
7923    char *zSql;
7924    int i;
7925    int savedShowHeader = p->showHeader;
7926    int savedShellFlags = p->shellFlgs;
7927    ShellClearFlag(p,
7928       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7929       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7930    for(i=1; i<nArg; i++){
7931      if( azArg[i][0]=='-' ){
7932        const char *z = azArg[i]+1;
7933        if( z[0]=='-' ) z++;
7934        if( strcmp(z,"preserve-rowids")==0 ){
7935#ifdef SQLITE_OMIT_VIRTUALTABLE
7936          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7937                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7938          rc = 1;
7939          sqlite3_free(zLike);
7940          goto meta_command_exit;
7941#else
7942          ShellSetFlag(p, SHFLG_PreserveRowid);
7943#endif
7944        }else
7945        if( strcmp(z,"newlines")==0 ){
7946          ShellSetFlag(p, SHFLG_Newlines);
7947        }else
7948        if( strcmp(z,"data-only")==0 ){
7949          ShellSetFlag(p, SHFLG_DumpDataOnly);
7950        }else
7951        if( strcmp(z,"nosys")==0 ){
7952          ShellSetFlag(p, SHFLG_DumpNoSys);
7953        }else
7954        {
7955          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7956          rc = 1;
7957          sqlite3_free(zLike);
7958          goto meta_command_exit;
7959        }
7960      }else{
7961        /* azArg[i] contains a LIKE pattern. This ".dump" request should
7962        ** only dump data for tables for which either the table name matches
7963        ** the LIKE pattern, or the table appears to be a shadow table of
7964        ** a virtual table for which the name matches the LIKE pattern.
7965        */
7966        char *zExpr = sqlite3_mprintf(
7967            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
7968            "  SELECT 1 FROM sqlite_schema WHERE "
7969            "    name LIKE %Q ESCAPE '\\' AND"
7970            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
7971            "    substr(o.name, 1, length(name)+1) == (name||'_')"
7972            ")", azArg[i], azArg[i]
7973        );
7974
7975        if( zLike ){
7976          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
7977        }else{
7978          zLike = zExpr;
7979        }
7980      }
7981    }
7982
7983    open_db(p, 0);
7984
7985    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7986      /* When playing back a "dump", the content might appear in an order
7987      ** which causes immediate foreign key constraints to be violated.
7988      ** So disable foreign-key constraint enforcement to prevent problems. */
7989      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7990      raw_printf(p->out, "BEGIN TRANSACTION;\n");
7991    }
7992    p->writableSchema = 0;
7993    p->showHeader = 0;
7994    /* Set writable_schema=ON since doing so forces SQLite to initialize
7995    ** as much of the schema as it can even if the sqlite_schema table is
7996    ** corrupt. */
7997    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7998    p->nErr = 0;
7999    if( zLike==0 ) zLike = sqlite3_mprintf("true");
8000    zSql = sqlite3_mprintf(
8001      "SELECT name, type, sql FROM sqlite_schema AS o "
8002      "WHERE (%s) AND type=='table'"
8003      "  AND sql NOT NULL"
8004      " ORDER BY tbl_name='sqlite_sequence', rowid",
8005      zLike
8006    );
8007    run_schema_dump_query(p,zSql);
8008    sqlite3_free(zSql);
8009    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8010      zSql = sqlite3_mprintf(
8011        "SELECT sql FROM sqlite_schema AS o "
8012        "WHERE (%s) AND sql NOT NULL"
8013        "  AND type IN ('index','trigger','view')",
8014        zLike
8015      );
8016      run_table_dump_query(p, zSql);
8017      sqlite3_free(zSql);
8018    }
8019    sqlite3_free(zLike);
8020    if( p->writableSchema ){
8021      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8022      p->writableSchema = 0;
8023    }
8024    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8025    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8026    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8027      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8028    }
8029    p->showHeader = savedShowHeader;
8030    p->shellFlgs = savedShellFlags;
8031  }else
8032
8033  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
8034    if( nArg==2 ){
8035      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8036    }else{
8037      raw_printf(stderr, "Usage: .echo on|off\n");
8038      rc = 1;
8039    }
8040  }else
8041
8042  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
8043    if( nArg==2 ){
8044      p->autoEQPtest = 0;
8045      if( p->autoEQPtrace ){
8046        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8047        p->autoEQPtrace = 0;
8048      }
8049      if( strcmp(azArg[1],"full")==0 ){
8050        p->autoEQP = AUTOEQP_full;
8051      }else if( strcmp(azArg[1],"trigger")==0 ){
8052        p->autoEQP = AUTOEQP_trigger;
8053#ifdef SQLITE_DEBUG
8054      }else if( strcmp(azArg[1],"test")==0 ){
8055        p->autoEQP = AUTOEQP_on;
8056        p->autoEQPtest = 1;
8057      }else if( strcmp(azArg[1],"trace")==0 ){
8058        p->autoEQP = AUTOEQP_full;
8059        p->autoEQPtrace = 1;
8060        open_db(p, 0);
8061        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8062        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8063#endif
8064      }else{
8065        p->autoEQP = (u8)booleanValue(azArg[1]);
8066      }
8067    }else{
8068      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8069      rc = 1;
8070    }
8071  }else
8072
8073  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
8074    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8075    rc = 2;
8076  }else
8077
8078  /* The ".explain" command is automatic now.  It is largely pointless.  It
8079  ** retained purely for backwards compatibility */
8080  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
8081    int val = 1;
8082    if( nArg>=2 ){
8083      if( strcmp(azArg[1],"auto")==0 ){
8084        val = 99;
8085      }else{
8086        val =  booleanValue(azArg[1]);
8087      }
8088    }
8089    if( val==1 && p->mode!=MODE_Explain ){
8090      p->normalMode = p->mode;
8091      p->mode = MODE_Explain;
8092      p->autoExplain = 0;
8093    }else if( val==0 ){
8094      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8095      p->autoExplain = 0;
8096    }else if( val==99 ){
8097      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8098      p->autoExplain = 1;
8099    }
8100  }else
8101
8102#ifndef SQLITE_OMIT_VIRTUALTABLE
8103  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
8104    open_db(p, 0);
8105    expertDotCommand(p, azArg, nArg);
8106  }else
8107#endif
8108
8109  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
8110    static const struct {
8111       const char *zCtrlName;   /* Name of a test-control option */
8112       int ctrlCode;            /* Integer code for that option */
8113       const char *zUsage;      /* Usage notes */
8114    } aCtrl[] = {
8115      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8116      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8117      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8118      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8119      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8120   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8121      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8122      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8123      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8124      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8125   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8126    };
8127    int filectrl = -1;
8128    int iCtrl = -1;
8129    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8130    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8131    int n2, i;
8132    const char *zCmd = 0;
8133    const char *zSchema = 0;
8134
8135    open_db(p, 0);
8136    zCmd = nArg>=2 ? azArg[1] : "help";
8137
8138    if( zCmd[0]=='-'
8139     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
8140     && nArg>=4
8141    ){
8142      zSchema = azArg[2];
8143      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8144      nArg -= 2;
8145      zCmd = azArg[1];
8146    }
8147
8148    /* The argument can optionally begin with "-" or "--" */
8149    if( zCmd[0]=='-' && zCmd[1] ){
8150      zCmd++;
8151      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8152    }
8153
8154    /* --help lists all file-controls */
8155    if( strcmp(zCmd,"help")==0 ){
8156      utf8_printf(p->out, "Available file-controls:\n");
8157      for(i=0; i<ArraySize(aCtrl); i++){
8158        utf8_printf(p->out, "  .filectrl %s %s\n",
8159                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8160      }
8161      rc = 1;
8162      goto meta_command_exit;
8163    }
8164
8165    /* convert filectrl text option to value. allow any unique prefix
8166    ** of the option name, or a numerical value. */
8167    n2 = strlen30(zCmd);
8168    for(i=0; i<ArraySize(aCtrl); i++){
8169      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8170        if( filectrl<0 ){
8171          filectrl = aCtrl[i].ctrlCode;
8172          iCtrl = i;
8173        }else{
8174          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8175                              "Use \".filectrl --help\" for help\n", zCmd);
8176          rc = 1;
8177          goto meta_command_exit;
8178        }
8179      }
8180    }
8181    if( filectrl<0 ){
8182      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8183                         "Use \".filectrl --help\" for help\n", zCmd);
8184    }else{
8185      switch(filectrl){
8186        case SQLITE_FCNTL_SIZE_LIMIT: {
8187          if( nArg!=2 && nArg!=3 ) break;
8188          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8189          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8190          isOk = 1;
8191          break;
8192        }
8193        case SQLITE_FCNTL_LOCK_TIMEOUT:
8194        case SQLITE_FCNTL_CHUNK_SIZE: {
8195          int x;
8196          if( nArg!=3 ) break;
8197          x = (int)integerValue(azArg[2]);
8198          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8199          isOk = 2;
8200          break;
8201        }
8202        case SQLITE_FCNTL_PERSIST_WAL:
8203        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8204          int x;
8205          if( nArg!=2 && nArg!=3 ) break;
8206          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8207          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8208          iRes = x;
8209          isOk = 1;
8210          break;
8211        }
8212        case SQLITE_FCNTL_DATA_VERSION:
8213        case SQLITE_FCNTL_HAS_MOVED: {
8214          int x;
8215          if( nArg!=2 ) break;
8216          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8217          iRes = x;
8218          isOk = 1;
8219          break;
8220        }
8221        case SQLITE_FCNTL_TEMPFILENAME: {
8222          char *z = 0;
8223          if( nArg!=2 ) break;
8224          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8225          if( z ){
8226            utf8_printf(p->out, "%s\n", z);
8227            sqlite3_free(z);
8228          }
8229          isOk = 2;
8230          break;
8231        }
8232        case SQLITE_FCNTL_RESERVE_BYTES: {
8233          int x;
8234          if( nArg>=3 ){
8235            x = atoi(azArg[2]);
8236            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8237          }
8238          x = -1;
8239          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8240          utf8_printf(p->out,"%d\n", x);
8241          isOk = 2;
8242          break;
8243        }
8244      }
8245    }
8246    if( isOk==0 && iCtrl>=0 ){
8247      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8248      rc = 1;
8249    }else if( isOk==1 ){
8250      char zBuf[100];
8251      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8252      raw_printf(p->out, "%s\n", zBuf);
8253    }
8254  }else
8255
8256  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8257    ShellState data;
8258    int doStats = 0;
8259    memcpy(&data, p, sizeof(data));
8260    data.showHeader = 0;
8261    data.cMode = data.mode = MODE_Semi;
8262    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8263      data.cMode = data.mode = MODE_Pretty;
8264      nArg = 1;
8265    }
8266    if( nArg!=1 ){
8267      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8268      rc = 1;
8269      goto meta_command_exit;
8270    }
8271    open_db(p, 0);
8272    rc = sqlite3_exec(p->db,
8273       "SELECT sql FROM"
8274       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8275       "     FROM sqlite_schema UNION ALL"
8276       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8277       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8278       "ORDER BY x",
8279       callback, &data, 0
8280    );
8281    if( rc==SQLITE_OK ){
8282      sqlite3_stmt *pStmt;
8283      rc = sqlite3_prepare_v2(p->db,
8284               "SELECT rowid FROM sqlite_schema"
8285               " WHERE name GLOB 'sqlite_stat[134]'",
8286               -1, &pStmt, 0);
8287      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8288      sqlite3_finalize(pStmt);
8289    }
8290    if( doStats==0 ){
8291      raw_printf(p->out, "/* No STAT tables available */\n");
8292    }else{
8293      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8294      data.cMode = data.mode = MODE_Insert;
8295      data.zDestTable = "sqlite_stat1";
8296      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8297      data.zDestTable = "sqlite_stat4";
8298      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8299      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8300    }
8301  }else
8302
8303  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8304    if( nArg==2 ){
8305      p->showHeader = booleanValue(azArg[1]);
8306      p->shellFlgs |= SHFLG_HeaderSet;
8307    }else{
8308      raw_printf(stderr, "Usage: .headers on|off\n");
8309      rc = 1;
8310    }
8311  }else
8312
8313  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8314    if( nArg>=2 ){
8315      n = showHelp(p->out, azArg[1]);
8316      if( n==0 ){
8317        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8318      }
8319    }else{
8320      showHelp(p->out, 0);
8321    }
8322  }else
8323
8324  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8325    char *zTable = 0;           /* Insert data into this table */
8326    char *zFile = 0;            /* Name of file to extra content from */
8327    sqlite3_stmt *pStmt = NULL; /* A statement */
8328    int nCol;                   /* Number of columns in the table */
8329    int nByte;                  /* Number of bytes in an SQL string */
8330    int i, j;                   /* Loop counters */
8331    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8332    int nSep;                   /* Number of bytes in p->colSeparator[] */
8333    char *zSql;                 /* An SQL statement */
8334    ImportCtx sCtx;             /* Reader context */
8335    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8336    int eVerbose = 0;           /* Larger for more console output */
8337    int nSkip = 0;              /* Initial lines to skip */
8338    int useOutputMode = 1;      /* Use output mode to determine separators */
8339
8340    failIfSafeMode(p, "cannot run .import in safe mode");
8341    memset(&sCtx, 0, sizeof(sCtx));
8342    sCtx.z = sqlite3_malloc64(120);
8343    if( sCtx.z==0 ){
8344      import_cleanup(&sCtx);
8345      shell_out_of_memory();
8346    }
8347    if( p->mode==MODE_Ascii ){
8348      xRead = ascii_read_one_field;
8349    }else{
8350      xRead = csv_read_one_field;
8351    }
8352    for(i=1; i<nArg; i++){
8353      char *z = azArg[i];
8354      if( z[0]=='-' && z[1]=='-' ) z++;
8355      if( z[0]!='-' ){
8356        if( zFile==0 ){
8357          zFile = z;
8358        }else if( zTable==0 ){
8359          zTable = z;
8360        }else{
8361          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8362          showHelp(p->out, "import");
8363          rc = 1;
8364          goto meta_command_exit;
8365        }
8366      }else if( strcmp(z,"-v")==0 ){
8367        eVerbose++;
8368      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8369        nSkip = integerValue(azArg[++i]);
8370      }else if( strcmp(z,"-ascii")==0 ){
8371        sCtx.cColSep = SEP_Unit[0];
8372        sCtx.cRowSep = SEP_Record[0];
8373        xRead = ascii_read_one_field;
8374        useOutputMode = 0;
8375      }else if( strcmp(z,"-csv")==0 ){
8376        sCtx.cColSep = ',';
8377        sCtx.cRowSep = '\n';
8378        xRead = csv_read_one_field;
8379        useOutputMode = 0;
8380      }else{
8381        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8382        showHelp(p->out, "import");
8383        rc = 1;
8384        goto meta_command_exit;
8385      }
8386    }
8387    if( zTable==0 ){
8388      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8389                  zFile==0 ? "FILE" : "TABLE");
8390      showHelp(p->out, "import");
8391      rc = 1;
8392      goto meta_command_exit;
8393    }
8394    seenInterrupt = 0;
8395    open_db(p, 0);
8396    if( useOutputMode ){
8397      /* If neither the --csv or --ascii options are specified, then set
8398      ** the column and row separator characters from the output mode. */
8399      nSep = strlen30(p->colSeparator);
8400      if( nSep==0 ){
8401        raw_printf(stderr,
8402                   "Error: non-null column separator required for import\n");
8403        rc = 1;
8404        goto meta_command_exit;
8405      }
8406      if( nSep>1 ){
8407        raw_printf(stderr,
8408              "Error: multi-character column separators not allowed"
8409              " for import\n");
8410        rc = 1;
8411        goto meta_command_exit;
8412      }
8413      nSep = strlen30(p->rowSeparator);
8414      if( nSep==0 ){
8415        raw_printf(stderr,
8416            "Error: non-null row separator required for import\n");
8417        rc = 1;
8418        goto meta_command_exit;
8419      }
8420      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8421        /* When importing CSV (only), if the row separator is set to the
8422        ** default output row separator, change it to the default input
8423        ** row separator.  This avoids having to maintain different input
8424        ** and output row separators. */
8425        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8426        nSep = strlen30(p->rowSeparator);
8427      }
8428      if( nSep>1 ){
8429        raw_printf(stderr, "Error: multi-character row separators not allowed"
8430                           " for import\n");
8431        rc = 1;
8432        goto meta_command_exit;
8433      }
8434      sCtx.cColSep = p->colSeparator[0];
8435      sCtx.cRowSep = p->rowSeparator[0];
8436    }
8437    sCtx.zFile = zFile;
8438    sCtx.nLine = 1;
8439    if( sCtx.zFile[0]=='|' ){
8440#ifdef SQLITE_OMIT_POPEN
8441      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8442      rc = 1;
8443      goto meta_command_exit;
8444#else
8445      sCtx.in = popen(sCtx.zFile+1, "r");
8446      sCtx.zFile = "<pipe>";
8447      sCtx.xCloser = pclose;
8448#endif
8449    }else{
8450      sCtx.in = fopen(sCtx.zFile, "rb");
8451      sCtx.xCloser = fclose;
8452    }
8453    if( sCtx.in==0 ){
8454      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8455      rc = 1;
8456      import_cleanup(&sCtx);
8457      goto meta_command_exit;
8458    }
8459    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8460      char zSep[2];
8461      zSep[1] = 0;
8462      zSep[0] = sCtx.cColSep;
8463      utf8_printf(p->out, "Column separator ");
8464      output_c_string(p->out, zSep);
8465      utf8_printf(p->out, ", row separator ");
8466      zSep[0] = sCtx.cRowSep;
8467      output_c_string(p->out, zSep);
8468      utf8_printf(p->out, "\n");
8469    }
8470    while( (nSkip--)>0 ){
8471      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8472    }
8473    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
8474    if( zSql==0 ){
8475      import_cleanup(&sCtx);
8476      shell_out_of_memory();
8477    }
8478    nByte = strlen30(zSql);
8479    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8480    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8481    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8482      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
8483      char cSep = '(';
8484      while( xRead(&sCtx) ){
8485        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8486        cSep = ',';
8487        if( sCtx.cTerm!=sCtx.cColSep ) break;
8488      }
8489      if( cSep=='(' ){
8490        sqlite3_free(zCreate);
8491        import_cleanup(&sCtx);
8492        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8493        rc = 1;
8494        goto meta_command_exit;
8495      }
8496      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8497      if( eVerbose>=1 ){
8498        utf8_printf(p->out, "%s\n", zCreate);
8499      }
8500      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8501      sqlite3_free(zCreate);
8502      if( rc ){
8503        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
8504                sqlite3_errmsg(p->db));
8505        import_cleanup(&sCtx);
8506        rc = 1;
8507        goto meta_command_exit;
8508      }
8509      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8510    }
8511    sqlite3_free(zSql);
8512    if( rc ){
8513      if (pStmt) sqlite3_finalize(pStmt);
8514      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8515      import_cleanup(&sCtx);
8516      rc = 1;
8517      goto meta_command_exit;
8518    }
8519    nCol = sqlite3_column_count(pStmt);
8520    sqlite3_finalize(pStmt);
8521    pStmt = 0;
8522    if( nCol==0 ) return 0; /* no columns, no error */
8523    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8524    if( zSql==0 ){
8525      import_cleanup(&sCtx);
8526      shell_out_of_memory();
8527    }
8528    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8529    j = strlen30(zSql);
8530    for(i=1; i<nCol; i++){
8531      zSql[j++] = ',';
8532      zSql[j++] = '?';
8533    }
8534    zSql[j++] = ')';
8535    zSql[j] = 0;
8536    if( eVerbose>=2 ){
8537      utf8_printf(p->out, "Insert using: %s\n", zSql);
8538    }
8539    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8540    sqlite3_free(zSql);
8541    if( rc ){
8542      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8543      if (pStmt) sqlite3_finalize(pStmt);
8544      import_cleanup(&sCtx);
8545      rc = 1;
8546      goto meta_command_exit;
8547    }
8548    needCommit = sqlite3_get_autocommit(p->db);
8549    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8550    do{
8551      int startLine = sCtx.nLine;
8552      for(i=0; i<nCol; i++){
8553        char *z = xRead(&sCtx);
8554        /*
8555        ** Did we reach end-of-file before finding any columns?
8556        ** If so, stop instead of NULL filling the remaining columns.
8557        */
8558        if( z==0 && i==0 ) break;
8559        /*
8560        ** Did we reach end-of-file OR end-of-line before finding any
8561        ** columns in ASCII mode?  If so, stop instead of NULL filling
8562        ** the remaining columns.
8563        */
8564        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8565        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8566        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8567          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8568                          "filling the rest with NULL\n",
8569                          sCtx.zFile, startLine, nCol, i+1);
8570          i += 2;
8571          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8572        }
8573      }
8574      if( sCtx.cTerm==sCtx.cColSep ){
8575        do{
8576          xRead(&sCtx);
8577          i++;
8578        }while( sCtx.cTerm==sCtx.cColSep );
8579        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8580                        "extras ignored\n",
8581                        sCtx.zFile, startLine, nCol, i);
8582      }
8583      if( i>=nCol ){
8584        sqlite3_step(pStmt);
8585        rc = sqlite3_reset(pStmt);
8586        if( rc!=SQLITE_OK ){
8587          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8588                      startLine, sqlite3_errmsg(p->db));
8589          sCtx.nErr++;
8590        }else{
8591          sCtx.nRow++;
8592        }
8593      }
8594    }while( sCtx.cTerm!=EOF );
8595
8596    import_cleanup(&sCtx);
8597    sqlite3_finalize(pStmt);
8598    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8599    if( eVerbose>0 ){
8600      utf8_printf(p->out,
8601          "Added %d rows with %d errors using %d lines of input\n",
8602          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8603    }
8604  }else
8605
8606#ifndef SQLITE_UNTESTABLE
8607  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8608    char *zSql;
8609    char *zCollist = 0;
8610    sqlite3_stmt *pStmt;
8611    int tnum = 0;
8612    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8613    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8614    int i;
8615    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8616      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8617                          "       .imposter off\n");
8618      /* Also allowed, but not documented:
8619      **
8620      **    .imposter TABLE IMPOSTER
8621      **
8622      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8623      ** imposter is another WITHOUT ROWID table with the columns in
8624      ** storage order. */
8625      rc = 1;
8626      goto meta_command_exit;
8627    }
8628    open_db(p, 0);
8629    if( nArg==2 ){
8630      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8631      goto meta_command_exit;
8632    }
8633    zSql = sqlite3_mprintf(
8634      "SELECT rootpage, 0 FROM sqlite_schema"
8635      " WHERE name='%q' AND type='index'"
8636      "UNION ALL "
8637      "SELECT rootpage, 1 FROM sqlite_schema"
8638      " WHERE name='%q' AND type='table'"
8639      "   AND sql LIKE '%%without%%rowid%%'",
8640      azArg[1], azArg[1]
8641    );
8642    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8643    sqlite3_free(zSql);
8644    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8645      tnum = sqlite3_column_int(pStmt, 0);
8646      isWO = sqlite3_column_int(pStmt, 1);
8647    }
8648    sqlite3_finalize(pStmt);
8649    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8650    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8651    sqlite3_free(zSql);
8652    i = 0;
8653    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8654      char zLabel[20];
8655      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8656      i++;
8657      if( zCol==0 ){
8658        if( sqlite3_column_int(pStmt,1)==-1 ){
8659          zCol = "_ROWID_";
8660        }else{
8661          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8662          zCol = zLabel;
8663        }
8664      }
8665      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8666        lenPK = (int)strlen(zCollist);
8667      }
8668      if( zCollist==0 ){
8669        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8670      }else{
8671        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8672      }
8673    }
8674    sqlite3_finalize(pStmt);
8675    if( i==0 || tnum==0 ){
8676      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8677      rc = 1;
8678      sqlite3_free(zCollist);
8679      goto meta_command_exit;
8680    }
8681    if( lenPK==0 ) lenPK = 100000;
8682    zSql = sqlite3_mprintf(
8683          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8684          azArg[2], zCollist, lenPK, zCollist);
8685    sqlite3_free(zCollist);
8686    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8687    if( rc==SQLITE_OK ){
8688      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8689      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8690      if( rc ){
8691        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8692      }else{
8693        utf8_printf(stdout, "%s;\n", zSql);
8694        raw_printf(stdout,
8695          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8696          azArg[1], isWO ? "table" : "index"
8697        );
8698      }
8699    }else{
8700      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8701      rc = 1;
8702    }
8703    sqlite3_free(zSql);
8704  }else
8705#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8706
8707#ifdef SQLITE_ENABLE_IOTRACE
8708  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8709    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8710    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8711    iotrace = 0;
8712    if( nArg<2 ){
8713      sqlite3IoTrace = 0;
8714    }else if( strcmp(azArg[1], "-")==0 ){
8715      sqlite3IoTrace = iotracePrintf;
8716      iotrace = stdout;
8717    }else{
8718      iotrace = fopen(azArg[1], "w");
8719      if( iotrace==0 ){
8720        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8721        sqlite3IoTrace = 0;
8722        rc = 1;
8723      }else{
8724        sqlite3IoTrace = iotracePrintf;
8725      }
8726    }
8727  }else
8728#endif
8729
8730  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8731    static const struct {
8732       const char *zLimitName;   /* Name of a limit */
8733       int limitCode;            /* Integer code for that limit */
8734    } aLimit[] = {
8735      { "length",                SQLITE_LIMIT_LENGTH                    },
8736      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8737      { "column",                SQLITE_LIMIT_COLUMN                    },
8738      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8739      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8740      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8741      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8742      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8743      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8744      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8745      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8746      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8747    };
8748    int i, n2;
8749    open_db(p, 0);
8750    if( nArg==1 ){
8751      for(i=0; i<ArraySize(aLimit); i++){
8752        printf("%20s %d\n", aLimit[i].zLimitName,
8753               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8754      }
8755    }else if( nArg>3 ){
8756      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8757      rc = 1;
8758      goto meta_command_exit;
8759    }else{
8760      int iLimit = -1;
8761      n2 = strlen30(azArg[1]);
8762      for(i=0; i<ArraySize(aLimit); i++){
8763        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8764          if( iLimit<0 ){
8765            iLimit = i;
8766          }else{
8767            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8768            rc = 1;
8769            goto meta_command_exit;
8770          }
8771        }
8772      }
8773      if( iLimit<0 ){
8774        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8775                        "enter \".limits\" with no arguments for a list.\n",
8776                         azArg[1]);
8777        rc = 1;
8778        goto meta_command_exit;
8779      }
8780      if( nArg==3 ){
8781        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8782                      (int)integerValue(azArg[2]));
8783      }
8784      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8785             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8786    }
8787  }else
8788
8789  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8790    open_db(p, 0);
8791    lintDotCommand(p, azArg, nArg);
8792  }else
8793
8794#ifndef SQLITE_OMIT_LOAD_EXTENSION
8795  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8796    const char *zFile, *zProc;
8797    char *zErrMsg = 0;
8798    failIfSafeMode(p, "cannot run .load in safe mode");
8799    if( nArg<2 ){
8800      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8801      rc = 1;
8802      goto meta_command_exit;
8803    }
8804    zFile = azArg[1];
8805    zProc = nArg>=3 ? azArg[2] : 0;
8806    open_db(p, 0);
8807    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8808    if( rc!=SQLITE_OK ){
8809      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8810      sqlite3_free(zErrMsg);
8811      rc = 1;
8812    }
8813  }else
8814#endif
8815
8816  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8817    failIfSafeMode(p, "cannot run .log in safe mode");
8818    if( nArg!=2 ){
8819      raw_printf(stderr, "Usage: .log FILENAME\n");
8820      rc = 1;
8821    }else{
8822      const char *zFile = azArg[1];
8823      output_file_close(p->pLog);
8824      p->pLog = output_file_open(zFile, 0);
8825    }
8826  }else
8827
8828  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8829    const char *zMode = nArg>=2 ? azArg[1] : "";
8830    int n2 = strlen30(zMode);
8831    int c2 = zMode[0];
8832    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8833      p->mode = MODE_Line;
8834      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8835    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8836      p->mode = MODE_Column;
8837      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8838        p->showHeader = 1;
8839      }
8840      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8841    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8842      p->mode = MODE_List;
8843      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8844      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8845    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8846      p->mode = MODE_Html;
8847    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8848      p->mode = MODE_Tcl;
8849      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8850      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8851    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8852      p->mode = MODE_Csv;
8853      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8854      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8855    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8856      p->mode = MODE_List;
8857      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8858    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8859      p->mode = MODE_Insert;
8860      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8861    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8862      p->mode = MODE_Quote;
8863      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8864      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8865    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8866      p->mode = MODE_Ascii;
8867      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8868      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8869    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8870      p->mode = MODE_Markdown;
8871    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8872      p->mode = MODE_Table;
8873    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8874      p->mode = MODE_Box;
8875    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8876      p->mode = MODE_Json;
8877    }else if( nArg==1 ){
8878      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8879    }else{
8880      raw_printf(stderr, "Error: mode should be one of: "
8881         "ascii box column csv html insert json line list markdown "
8882         "quote table tabs tcl\n");
8883      rc = 1;
8884    }
8885    p->cMode = p->mode;
8886  }else
8887
8888  if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
8889    if( nArg!=2 ){
8890      raw_printf(stderr, "Usage: .nonce NONCE\n");
8891      rc = 1;
8892    }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
8893      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", p->lineno, azArg[1]);
8894      exit(1);
8895    }else{
8896      p->bSafeMode = 0;
8897      return 0;  /* Return immediately to bypass the safe mode reset
8898                 ** at the end of this procedure */
8899    }
8900  }else
8901
8902  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8903    if( nArg==2 ){
8904      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8905                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8906    }else{
8907      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8908      rc = 1;
8909    }
8910  }else
8911
8912#ifdef SQLITE_DEBUG
8913  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8914    int i;
8915    for(i=1; i<nArg; i++){
8916      const char *z = azArg[i];
8917      if( z[0]=='-' && z[1]=='-' ) z++;
8918      if( strcmp(z,"-repeat")==0 ){
8919        if( i==nArg-1 ){
8920          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8921          rc = 1;
8922        }else{
8923          oomRepeat = (int)integerValue(azArg[++i]);
8924        }
8925      }else if( IsDigit(z[0]) ){
8926        oomCounter = (int)integerValue(azArg[i]);
8927      }else{
8928        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8929        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8930        rc = 1;
8931      }
8932    }
8933    if( rc==0 ){
8934      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8935      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8936    }
8937  }else
8938#endif /* SQLITE_DEBUG */
8939
8940  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8941    char *zNewFilename = 0;  /* Name of the database file to open */
8942    int iName = 1;           /* Index in azArg[] of the filename */
8943    int newFlag = 0;         /* True to delete file before opening */
8944    /* Close the existing database */
8945    session_close_all(p, -1);
8946    close_db(p->db);
8947    p->db = 0;
8948    p->pAuxDb->zDbFilename = 0;
8949    sqlite3_free(p->pAuxDb->zFreeOnClose);
8950    p->pAuxDb->zFreeOnClose = 0;
8951    p->openMode = SHELL_OPEN_UNSPEC;
8952    p->openFlags = 0;
8953    p->szMax = 0;
8954    /* Check for command-line arguments */
8955    for(iName=1; iName<nArg; iName++){
8956      const char *z = azArg[iName];
8957      if( optionMatch(z,"new") ){
8958        newFlag = 1;
8959#ifdef SQLITE_HAVE_ZLIB
8960      }else if( optionMatch(z, "zip") ){
8961        p->openMode = SHELL_OPEN_ZIPFILE;
8962#endif
8963      }else if( optionMatch(z, "append") ){
8964        p->openMode = SHELL_OPEN_APPENDVFS;
8965      }else if( optionMatch(z, "readonly") ){
8966        p->openMode = SHELL_OPEN_READONLY;
8967      }else if( optionMatch(z, "nofollow") ){
8968        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8969#ifndef SQLITE_OMIT_DESERIALIZE
8970      }else if( optionMatch(z, "deserialize") ){
8971        p->openMode = SHELL_OPEN_DESERIALIZE;
8972      }else if( optionMatch(z, "hexdb") ){
8973        p->openMode = SHELL_OPEN_HEXDB;
8974      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8975        p->szMax = integerValue(azArg[++iName]);
8976#endif /* SQLITE_OMIT_DESERIALIZE */
8977      }else if( z[0]=='-' ){
8978        utf8_printf(stderr, "unknown option: %s\n", z);
8979        rc = 1;
8980        goto meta_command_exit;
8981      }else if( zNewFilename ){
8982        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
8983        rc = 1;
8984        goto meta_command_exit;
8985      }else{
8986        zNewFilename = sqlite3_mprintf("%s", z);
8987      }
8988    }
8989    /* If a filename is specified, try to open it first */
8990    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8991      if( newFlag && !p->bSafeMode ) shellDeleteFile(zNewFilename);
8992      if( p->bSafeMode
8993       && p->openMode!=SHELL_OPEN_HEXDB
8994       && zNewFilename
8995       && strcmp(zNewFilename,":memory:")!=0
8996      ){
8997        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
8998      }
8999      p->pAuxDb->zDbFilename = zNewFilename;
9000      open_db(p, OPEN_DB_KEEPALIVE);
9001      if( p->db==0 ){
9002        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9003        sqlite3_free(zNewFilename);
9004      }else{
9005        p->pAuxDb->zFreeOnClose = zNewFilename;
9006      }
9007    }
9008    if( p->db==0 ){
9009      /* As a fall-back open a TEMP database */
9010      p->pAuxDb->zDbFilename = 0;
9011      open_db(p, 0);
9012    }
9013  }else
9014
9015  if( (c=='o'
9016        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
9017   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
9018  ){
9019    char *zFile = 0;
9020    int bTxtMode = 0;
9021    int i;
9022    int eMode = 0;
9023    int bBOM = 0;
9024    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
9025
9026    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9027    if( c=='e' ){
9028      eMode = 'x';
9029      bOnce = 2;
9030    }else if( strncmp(azArg[0],"once",n)==0 ){
9031      bOnce = 1;
9032    }
9033    for(i=1; i<nArg; i++){
9034      char *z = azArg[i];
9035      if( z[0]=='-' ){
9036        if( z[1]=='-' ) z++;
9037        if( strcmp(z,"-bom")==0 ){
9038          bBOM = 1;
9039        }else if( c!='e' && strcmp(z,"-x")==0 ){
9040          eMode = 'x';  /* spreadsheet */
9041        }else if( c!='e' && strcmp(z,"-e")==0 ){
9042          eMode = 'e';  /* text editor */
9043        }else{
9044          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
9045                      azArg[i]);
9046          showHelp(p->out, azArg[0]);
9047          rc = 1;
9048          goto meta_command_exit;
9049        }
9050      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9051        zFile = sqlite3_mprintf("%s", z);
9052        if( zFile[0]=='|' ){
9053          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9054          break;
9055        }
9056      }else{
9057        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
9058                    azArg[i]);
9059        showHelp(p->out, azArg[0]);
9060        rc = 1;
9061        sqlite3_free(zFile);
9062        goto meta_command_exit;
9063      }
9064    }
9065    if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
9066    if( bOnce ){
9067      p->outCount = 2;
9068    }else{
9069      p->outCount = 0;
9070    }
9071    output_reset(p);
9072#ifndef SQLITE_NOHAVE_SYSTEM
9073    if( eMode=='e' || eMode=='x' ){
9074      p->doXdgOpen = 1;
9075      outputModePush(p);
9076      if( eMode=='x' ){
9077        /* spreadsheet mode.  Output as CSV. */
9078        newTempFile(p, "csv");
9079        ShellClearFlag(p, SHFLG_Echo);
9080        p->mode = MODE_Csv;
9081        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9082        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9083      }else{
9084        /* text editor mode */
9085        newTempFile(p, "txt");
9086        bTxtMode = 1;
9087      }
9088      sqlite3_free(zFile);
9089      zFile = sqlite3_mprintf("%s", p->zTempFile);
9090    }
9091#endif /* SQLITE_NOHAVE_SYSTEM */
9092    if( zFile[0]=='|' ){
9093#ifdef SQLITE_OMIT_POPEN
9094      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9095      rc = 1;
9096      p->out = stdout;
9097#else
9098      p->out = popen(zFile + 1, "w");
9099      if( p->out==0 ){
9100        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9101        p->out = stdout;
9102        rc = 1;
9103      }else{
9104        if( bBOM ) fprintf(p->out,"\357\273\277");
9105        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9106      }
9107#endif
9108    }else{
9109      p->out = output_file_open(zFile, bTxtMode);
9110      if( p->out==0 ){
9111        if( strcmp(zFile,"off")!=0 ){
9112          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9113        }
9114        p->out = stdout;
9115        rc = 1;
9116      } else {
9117        if( bBOM ) fprintf(p->out,"\357\273\277");
9118        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9119      }
9120    }
9121    sqlite3_free(zFile);
9122  }else
9123
9124  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
9125    open_db(p,0);
9126    if( nArg<=1 ) goto parameter_syntax_error;
9127
9128    /* .parameter clear
9129    ** Clear all bind parameters by dropping the TEMP table that holds them.
9130    */
9131    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
9132      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9133                   0, 0, 0);
9134    }else
9135
9136    /* .parameter list
9137    ** List all bind parameters.
9138    */
9139    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
9140      sqlite3_stmt *pStmt = 0;
9141      int rx;
9142      int len = 0;
9143      rx = sqlite3_prepare_v2(p->db,
9144             "SELECT max(length(key)) "
9145             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9146      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9147        len = sqlite3_column_int(pStmt, 0);
9148        if( len>40 ) len = 40;
9149      }
9150      sqlite3_finalize(pStmt);
9151      pStmt = 0;
9152      if( len ){
9153        rx = sqlite3_prepare_v2(p->db,
9154             "SELECT key, quote(value) "
9155             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9156        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9157          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9158                      sqlite3_column_text(pStmt,1));
9159        }
9160        sqlite3_finalize(pStmt);
9161      }
9162    }else
9163
9164    /* .parameter init
9165    ** Make sure the TEMP table used to hold bind parameters exists.
9166    ** Create it if necessary.
9167    */
9168    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
9169      bind_table_init(p);
9170    }else
9171
9172    /* .parameter set NAME VALUE
9173    ** Set or reset a bind parameter.  NAME should be the full parameter
9174    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9175    ** VALUE can be in either SQL literal notation, or if not it will be
9176    ** understood to be a text string.
9177    */
9178    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
9179      int rx;
9180      char *zSql;
9181      sqlite3_stmt *pStmt;
9182      const char *zKey = azArg[2];
9183      const char *zValue = azArg[3];
9184      bind_table_init(p);
9185      zSql = sqlite3_mprintf(
9186                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9187                  "VALUES(%Q,%s);", zKey, zValue);
9188      if( zSql==0 ) shell_out_of_memory();
9189      pStmt = 0;
9190      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9191      sqlite3_free(zSql);
9192      if( rx!=SQLITE_OK ){
9193        sqlite3_finalize(pStmt);
9194        pStmt = 0;
9195        zSql = sqlite3_mprintf(
9196                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9197                   "VALUES(%Q,%Q);", zKey, zValue);
9198        if( zSql==0 ) shell_out_of_memory();
9199        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9200        sqlite3_free(zSql);
9201        if( rx!=SQLITE_OK ){
9202          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9203          sqlite3_finalize(pStmt);
9204          pStmt = 0;
9205          rc = 1;
9206        }
9207      }
9208      sqlite3_step(pStmt);
9209      sqlite3_finalize(pStmt);
9210    }else
9211
9212    /* .parameter unset NAME
9213    ** Remove the NAME binding from the parameter binding table, if it
9214    ** exists.
9215    */
9216    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
9217      char *zSql = sqlite3_mprintf(
9218          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9219      if( zSql==0 ) shell_out_of_memory();
9220      sqlite3_exec(p->db, zSql, 0, 0, 0);
9221      sqlite3_free(zSql);
9222    }else
9223    /* If no command name matches, show a syntax error */
9224    parameter_syntax_error:
9225    showHelp(p->out, "parameter");
9226  }else
9227
9228  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
9229    int i;
9230    for(i=1; i<nArg; i++){
9231      if( i>1 ) raw_printf(p->out, " ");
9232      utf8_printf(p->out, "%s", azArg[i]);
9233    }
9234    raw_printf(p->out, "\n");
9235  }else
9236
9237#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9238  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
9239    int i;
9240    int nn = 0;
9241    p->flgProgress = 0;
9242    p->mxProgress = 0;
9243    p->nProgress = 0;
9244    for(i=1; i<nArg; i++){
9245      const char *z = azArg[i];
9246      if( z[0]=='-' ){
9247        z++;
9248        if( z[0]=='-' ) z++;
9249        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
9250          p->flgProgress |= SHELL_PROGRESS_QUIET;
9251          continue;
9252        }
9253        if( strcmp(z,"reset")==0 ){
9254          p->flgProgress |= SHELL_PROGRESS_RESET;
9255          continue;
9256        }
9257        if( strcmp(z,"once")==0 ){
9258          p->flgProgress |= SHELL_PROGRESS_ONCE;
9259          continue;
9260        }
9261        if( strcmp(z,"limit")==0 ){
9262          if( i+1>=nArg ){
9263            utf8_printf(stderr, "Error: missing argument on --limit\n");
9264            rc = 1;
9265            goto meta_command_exit;
9266          }else{
9267            p->mxProgress = (int)integerValue(azArg[++i]);
9268          }
9269          continue;
9270        }
9271        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9272        rc = 1;
9273        goto meta_command_exit;
9274      }else{
9275        nn = (int)integerValue(z);
9276      }
9277    }
9278    open_db(p, 0);
9279    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9280  }else
9281#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9282
9283  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9284    if( nArg >= 2) {
9285      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9286    }
9287    if( nArg >= 3) {
9288      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9289    }
9290  }else
9291
9292  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
9293    rc = 2;
9294  }else
9295
9296  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
9297    FILE *inSaved = p->in;
9298    int savedLineno = p->lineno;
9299    failIfSafeMode(p, "cannot run .read in safe mode");
9300    if( nArg!=2 ){
9301      raw_printf(stderr, "Usage: .read FILE\n");
9302      rc = 1;
9303      goto meta_command_exit;
9304    }
9305    if( azArg[1][0]=='|' ){
9306#ifdef SQLITE_OMIT_POPEN
9307      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9308      rc = 1;
9309      p->out = stdout;
9310#else
9311      p->in = popen(azArg[1]+1, "r");
9312      if( p->in==0 ){
9313        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9314        rc = 1;
9315      }else{
9316        rc = process_input(p);
9317        pclose(p->in);
9318      }
9319#endif
9320    }else if( (p->in = openChrSource(azArg[1]))==0 ){
9321      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9322      rc = 1;
9323    }else{
9324      rc = process_input(p);
9325      fclose(p->in);
9326    }
9327    p->in = inSaved;
9328    p->lineno = savedLineno;
9329  }else
9330
9331  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9332    const char *zSrcFile;
9333    const char *zDb;
9334    sqlite3 *pSrc;
9335    sqlite3_backup *pBackup;
9336    int nTimeout = 0;
9337
9338    failIfSafeMode(p, "cannot run .restore in safe mode");
9339    if( nArg==2 ){
9340      zSrcFile = azArg[1];
9341      zDb = "main";
9342    }else if( nArg==3 ){
9343      zSrcFile = azArg[2];
9344      zDb = azArg[1];
9345    }else{
9346      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9347      rc = 1;
9348      goto meta_command_exit;
9349    }
9350    rc = sqlite3_open(zSrcFile, &pSrc);
9351    if( rc!=SQLITE_OK ){
9352      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9353      close_db(pSrc);
9354      return 1;
9355    }
9356    open_db(p, 0);
9357    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9358    if( pBackup==0 ){
9359      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9360      close_db(pSrc);
9361      return 1;
9362    }
9363    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9364          || rc==SQLITE_BUSY  ){
9365      if( rc==SQLITE_BUSY ){
9366        if( nTimeout++ >= 3 ) break;
9367        sqlite3_sleep(100);
9368      }
9369    }
9370    sqlite3_backup_finish(pBackup);
9371    if( rc==SQLITE_DONE ){
9372      rc = 0;
9373    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9374      raw_printf(stderr, "Error: source database is busy\n");
9375      rc = 1;
9376    }else{
9377      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9378      rc = 1;
9379    }
9380    close_db(pSrc);
9381  }else
9382
9383  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9384    if( nArg==2 ){
9385      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9386#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9387      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9388#endif
9389    }else{
9390      raw_printf(stderr, "Usage: .scanstats on|off\n");
9391      rc = 1;
9392    }
9393  }else
9394
9395  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9396    ShellText sSelect;
9397    ShellState data;
9398    char *zErrMsg = 0;
9399    const char *zDiv = "(";
9400    const char *zName = 0;
9401    int iSchema = 0;
9402    int bDebug = 0;
9403    int bNoSystemTabs = 0;
9404    int ii;
9405
9406    open_db(p, 0);
9407    memcpy(&data, p, sizeof(data));
9408    data.showHeader = 0;
9409    data.cMode = data.mode = MODE_Semi;
9410    initText(&sSelect);
9411    for(ii=1; ii<nArg; ii++){
9412      if( optionMatch(azArg[ii],"indent") ){
9413        data.cMode = data.mode = MODE_Pretty;
9414      }else if( optionMatch(azArg[ii],"debug") ){
9415        bDebug = 1;
9416      }else if( optionMatch(azArg[ii],"nosys") ){
9417        bNoSystemTabs = 1;
9418      }else if( azArg[ii][0]=='-' ){
9419        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9420        rc = 1;
9421        goto meta_command_exit;
9422      }else if( zName==0 ){
9423        zName = azArg[ii];
9424      }else{
9425        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9426        rc = 1;
9427        goto meta_command_exit;
9428      }
9429    }
9430    if( zName!=0 ){
9431      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9432                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9433                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9434                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9435      if( isSchema ){
9436        char *new_argv[2], *new_colv[2];
9437        new_argv[0] = sqlite3_mprintf(
9438                      "CREATE TABLE %s (\n"
9439                      "  type text,\n"
9440                      "  name text,\n"
9441                      "  tbl_name text,\n"
9442                      "  rootpage integer,\n"
9443                      "  sql text\n"
9444                      ")", zName);
9445        new_argv[1] = 0;
9446        new_colv[0] = "sql";
9447        new_colv[1] = 0;
9448        callback(&data, 1, new_argv, new_colv);
9449        sqlite3_free(new_argv[0]);
9450      }
9451    }
9452    if( zDiv ){
9453      sqlite3_stmt *pStmt = 0;
9454      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9455                              -1, &pStmt, 0);
9456      if( rc ){
9457        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9458        sqlite3_finalize(pStmt);
9459        rc = 1;
9460        goto meta_command_exit;
9461      }
9462      appendText(&sSelect, "SELECT sql FROM", 0);
9463      iSchema = 0;
9464      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9465        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9466        char zScNum[30];
9467        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9468        appendText(&sSelect, zDiv, 0);
9469        zDiv = " UNION ALL ";
9470        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9471        if( sqlite3_stricmp(zDb, "main")!=0 ){
9472          appendText(&sSelect, zDb, '\'');
9473        }else{
9474          appendText(&sSelect, "NULL", 0);
9475        }
9476        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9477        appendText(&sSelect, zScNum, 0);
9478        appendText(&sSelect, " AS snum, ", 0);
9479        appendText(&sSelect, zDb, '\'');
9480        appendText(&sSelect, " AS sname FROM ", 0);
9481        appendText(&sSelect, zDb, quoteChar(zDb));
9482        appendText(&sSelect, ".sqlite_schema", 0);
9483      }
9484      sqlite3_finalize(pStmt);
9485#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9486      if( zName ){
9487        appendText(&sSelect,
9488           " UNION ALL SELECT shell_module_schema(name),"
9489           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9490        0);
9491      }
9492#endif
9493      appendText(&sSelect, ") WHERE ", 0);
9494      if( zName ){
9495        char *zQarg = sqlite3_mprintf("%Q", zName);
9496        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9497                    strchr(zName, '[') != 0;
9498        if( strchr(zName, '.') ){
9499          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9500        }else{
9501          appendText(&sSelect, "lower(tbl_name)", 0);
9502        }
9503        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9504        appendText(&sSelect, zQarg, 0);
9505        if( !bGlob ){
9506          appendText(&sSelect, " ESCAPE '\\' ", 0);
9507        }
9508        appendText(&sSelect, " AND ", 0);
9509        sqlite3_free(zQarg);
9510      }
9511      if( bNoSystemTabs ){
9512        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9513      }
9514      appendText(&sSelect, "sql IS NOT NULL"
9515                           " ORDER BY snum, rowid", 0);
9516      if( bDebug ){
9517        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9518      }else{
9519        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9520      }
9521      freeText(&sSelect);
9522    }
9523    if( zErrMsg ){
9524      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9525      sqlite3_free(zErrMsg);
9526      rc = 1;
9527    }else if( rc != SQLITE_OK ){
9528      raw_printf(stderr,"Error: querying schema information\n");
9529      rc = 1;
9530    }else{
9531      rc = 0;
9532    }
9533  }else
9534
9535  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9536    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
9537    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
9538  }else
9539
9540#if defined(SQLITE_ENABLE_SESSION)
9541  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9542    struct AuxDb *pAuxDb = p->pAuxDb;
9543    OpenSession *pSession = &pAuxDb->aSession[0];
9544    char **azCmd = &azArg[1];
9545    int iSes = 0;
9546    int nCmd = nArg - 1;
9547    int i;
9548    if( nArg<=1 ) goto session_syntax_error;
9549    open_db(p, 0);
9550    if( nArg>=3 ){
9551      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
9552        if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
9553      }
9554      if( iSes<pAuxDb->nSession ){
9555        pSession = &pAuxDb->aSession[iSes];
9556        azCmd++;
9557        nCmd--;
9558      }else{
9559        pSession = &pAuxDb->aSession[0];
9560        iSes = 0;
9561      }
9562    }
9563
9564    /* .session attach TABLE
9565    ** Invoke the sqlite3session_attach() interface to attach a particular
9566    ** table so that it is never filtered.
9567    */
9568    if( strcmp(azCmd[0],"attach")==0 ){
9569      if( nCmd!=2 ) goto session_syntax_error;
9570      if( pSession->p==0 ){
9571        session_not_open:
9572        raw_printf(stderr, "ERROR: No sessions are open\n");
9573      }else{
9574        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9575        if( rc ){
9576          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9577          rc = 0;
9578        }
9579      }
9580    }else
9581
9582    /* .session changeset FILE
9583    ** .session patchset FILE
9584    ** Write a changeset or patchset into a file.  The file is overwritten.
9585    */
9586    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9587      FILE *out = 0;
9588      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
9589      if( nCmd!=2 ) goto session_syntax_error;
9590      if( pSession->p==0 ) goto session_not_open;
9591      out = fopen(azCmd[1], "wb");
9592      if( out==0 ){
9593        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9594                    azCmd[1]);
9595      }else{
9596        int szChng;
9597        void *pChng;
9598        if( azCmd[0][0]=='c' ){
9599          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9600        }else{
9601          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9602        }
9603        if( rc ){
9604          printf("Error: error code %d\n", rc);
9605          rc = 0;
9606        }
9607        if( pChng
9608          && fwrite(pChng, szChng, 1, out)!=1 ){
9609          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9610                  szChng);
9611        }
9612        sqlite3_free(pChng);
9613        fclose(out);
9614      }
9615    }else
9616
9617    /* .session close
9618    ** Close the identified session
9619    */
9620    if( strcmp(azCmd[0], "close")==0 ){
9621      if( nCmd!=1 ) goto session_syntax_error;
9622      if( pAuxDb->nSession ){
9623        session_close(pSession);
9624        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
9625      }
9626    }else
9627
9628    /* .session enable ?BOOLEAN?
9629    ** Query or set the enable flag
9630    */
9631    if( strcmp(azCmd[0], "enable")==0 ){
9632      int ii;
9633      if( nCmd>2 ) goto session_syntax_error;
9634      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9635      if( pAuxDb->nSession ){
9636        ii = sqlite3session_enable(pSession->p, ii);
9637        utf8_printf(p->out, "session %s enable flag = %d\n",
9638                    pSession->zName, ii);
9639      }
9640    }else
9641
9642    /* .session filter GLOB ....
9643    ** Set a list of GLOB patterns of table names to be excluded.
9644    */
9645    if( strcmp(azCmd[0], "filter")==0 ){
9646      int ii, nByte;
9647      if( nCmd<2 ) goto session_syntax_error;
9648      if( pAuxDb->nSession ){
9649        for(ii=0; ii<pSession->nFilter; ii++){
9650          sqlite3_free(pSession->azFilter[ii]);
9651        }
9652        sqlite3_free(pSession->azFilter);
9653        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9654        pSession->azFilter = sqlite3_malloc( nByte );
9655        if( pSession->azFilter==0 ){
9656          raw_printf(stderr, "Error: out or memory\n");
9657          exit(1);
9658        }
9659        for(ii=1; ii<nCmd; ii++){
9660          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9661        }
9662        pSession->nFilter = ii-1;
9663      }
9664    }else
9665
9666    /* .session indirect ?BOOLEAN?
9667    ** Query or set the indirect flag
9668    */
9669    if( strcmp(azCmd[0], "indirect")==0 ){
9670      int ii;
9671      if( nCmd>2 ) goto session_syntax_error;
9672      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9673      if( pAuxDb->nSession ){
9674        ii = sqlite3session_indirect(pSession->p, ii);
9675        utf8_printf(p->out, "session %s indirect flag = %d\n",
9676                    pSession->zName, ii);
9677      }
9678    }else
9679
9680    /* .session isempty
9681    ** Determine if the session is empty
9682    */
9683    if( strcmp(azCmd[0], "isempty")==0 ){
9684      int ii;
9685      if( nCmd!=1 ) goto session_syntax_error;
9686      if( pAuxDb->nSession ){
9687        ii = sqlite3session_isempty(pSession->p);
9688        utf8_printf(p->out, "session %s isempty flag = %d\n",
9689                    pSession->zName, ii);
9690      }
9691    }else
9692
9693    /* .session list
9694    ** List all currently open sessions
9695    */
9696    if( strcmp(azCmd[0],"list")==0 ){
9697      for(i=0; i<pAuxDb->nSession; i++){
9698        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
9699      }
9700    }else
9701
9702    /* .session open DB NAME
9703    ** Open a new session called NAME on the attached database DB.
9704    ** DB is normally "main".
9705    */
9706    if( strcmp(azCmd[0],"open")==0 ){
9707      char *zName;
9708      if( nCmd!=3 ) goto session_syntax_error;
9709      zName = azCmd[2];
9710      if( zName[0]==0 ) goto session_syntax_error;
9711      for(i=0; i<pAuxDb->nSession; i++){
9712        if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
9713          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9714          goto meta_command_exit;
9715        }
9716      }
9717      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
9718        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
9719        goto meta_command_exit;
9720      }
9721      pSession = &pAuxDb->aSession[pAuxDb->nSession];
9722      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9723      if( rc ){
9724        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9725        rc = 0;
9726        goto meta_command_exit;
9727      }
9728      pSession->nFilter = 0;
9729      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9730      pAuxDb->nSession++;
9731      pSession->zName = sqlite3_mprintf("%s", zName);
9732    }else
9733    /* If no command name matches, show a syntax error */
9734    session_syntax_error:
9735    showHelp(p->out, "session");
9736  }else
9737#endif
9738
9739#ifdef SQLITE_DEBUG
9740  /* Undocumented commands for internal testing.  Subject to change
9741  ** without notice. */
9742  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9743    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9744      int i, v;
9745      for(i=1; i<nArg; i++){
9746        v = booleanValue(azArg[i]);
9747        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9748      }
9749    }
9750    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9751      int i; sqlite3_int64 v;
9752      for(i=1; i<nArg; i++){
9753        char zBuf[200];
9754        v = integerValue(azArg[i]);
9755        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9756        utf8_printf(p->out, "%s", zBuf);
9757      }
9758    }
9759  }else
9760#endif
9761
9762  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9763    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9764    int bVerbose = 0;        /* Verbose output */
9765    int bSelftestExists;     /* True if SELFTEST already exists */
9766    int i, k;                /* Loop counters */
9767    int nTest = 0;           /* Number of tests runs */
9768    int nErr = 0;            /* Number of errors seen */
9769    ShellText str;           /* Answer for a query */
9770    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9771
9772    open_db(p,0);
9773    for(i=1; i<nArg; i++){
9774      const char *z = azArg[i];
9775      if( z[0]=='-' && z[1]=='-' ) z++;
9776      if( strcmp(z,"-init")==0 ){
9777        bIsInit = 1;
9778      }else
9779      if( strcmp(z,"-v")==0 ){
9780        bVerbose++;
9781      }else
9782      {
9783        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9784                    azArg[i], azArg[0]);
9785        raw_printf(stderr, "Should be one of: --init -v\n");
9786        rc = 1;
9787        goto meta_command_exit;
9788      }
9789    }
9790    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9791           != SQLITE_OK ){
9792      bSelftestExists = 0;
9793    }else{
9794      bSelftestExists = 1;
9795    }
9796    if( bIsInit ){
9797      createSelftestTable(p);
9798      bSelftestExists = 1;
9799    }
9800    initText(&str);
9801    appendText(&str, "x", 0);
9802    for(k=bSelftestExists; k>=0; k--){
9803      if( k==1 ){
9804        rc = sqlite3_prepare_v2(p->db,
9805            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9806            -1, &pStmt, 0);
9807      }else{
9808        rc = sqlite3_prepare_v2(p->db,
9809          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9810          "      (1,'run','PRAGMA integrity_check','ok')",
9811          -1, &pStmt, 0);
9812      }
9813      if( rc ){
9814        raw_printf(stderr, "Error querying the selftest table\n");
9815        rc = 1;
9816        sqlite3_finalize(pStmt);
9817        goto meta_command_exit;
9818      }
9819      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9820        int tno = sqlite3_column_int(pStmt, 0);
9821        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9822        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9823        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9824
9825        k = 0;
9826        if( bVerbose>0 ){
9827          char *zQuote = sqlite3_mprintf("%q", zSql);
9828          printf("%d: %s %s\n", tno, zOp, zSql);
9829          sqlite3_free(zQuote);
9830        }
9831        if( strcmp(zOp,"memo")==0 ){
9832          utf8_printf(p->out, "%s\n", zSql);
9833        }else
9834        if( strcmp(zOp,"run")==0 ){
9835          char *zErrMsg = 0;
9836          str.n = 0;
9837          str.z[0] = 0;
9838          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9839          nTest++;
9840          if( bVerbose ){
9841            utf8_printf(p->out, "Result: %s\n", str.z);
9842          }
9843          if( rc || zErrMsg ){
9844            nErr++;
9845            rc = 1;
9846            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9847            sqlite3_free(zErrMsg);
9848          }else if( strcmp(zAns,str.z)!=0 ){
9849            nErr++;
9850            rc = 1;
9851            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9852            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9853          }
9854        }else
9855        {
9856          utf8_printf(stderr,
9857            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9858          rc = 1;
9859          break;
9860        }
9861      } /* End loop over rows of content from SELFTEST */
9862      sqlite3_finalize(pStmt);
9863    } /* End loop over k */
9864    freeText(&str);
9865    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9866  }else
9867
9868  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9869    if( nArg<2 || nArg>3 ){
9870      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9871      rc = 1;
9872    }
9873    if( nArg>=2 ){
9874      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9875                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9876    }
9877    if( nArg>=3 ){
9878      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9879                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9880    }
9881  }else
9882
9883  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9884    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9885    int i;                   /* Loop counter */
9886    int bSchema = 0;         /* Also hash the schema */
9887    int bSeparate = 0;       /* Hash each table separately */
9888    int iSize = 224;         /* Hash algorithm to use */
9889    int bDebug = 0;          /* Only show the query that would have run */
9890    sqlite3_stmt *pStmt;     /* For querying tables names */
9891    char *zSql;              /* SQL to be run */
9892    char *zSep;              /* Separator */
9893    ShellText sSql;          /* Complete SQL for the query to run the hash */
9894    ShellText sQuery;        /* Set of queries used to read all content */
9895    open_db(p, 0);
9896    for(i=1; i<nArg; i++){
9897      const char *z = azArg[i];
9898      if( z[0]=='-' ){
9899        z++;
9900        if( z[0]=='-' ) z++;
9901        if( strcmp(z,"schema")==0 ){
9902          bSchema = 1;
9903        }else
9904        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9905         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9906        ){
9907          iSize = atoi(&z[5]);
9908        }else
9909        if( strcmp(z,"debug")==0 ){
9910          bDebug = 1;
9911        }else
9912        {
9913          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9914                      azArg[i], azArg[0]);
9915          showHelp(p->out, azArg[0]);
9916          rc = 1;
9917          goto meta_command_exit;
9918        }
9919      }else if( zLike ){
9920        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9921        rc = 1;
9922        goto meta_command_exit;
9923      }else{
9924        zLike = z;
9925        bSeparate = 1;
9926        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9927      }
9928    }
9929    if( bSchema ){
9930      zSql = "SELECT lower(name) FROM sqlite_schema"
9931             " WHERE type='table' AND coalesce(rootpage,0)>1"
9932             " UNION ALL SELECT 'sqlite_schema'"
9933             " ORDER BY 1 collate nocase";
9934    }else{
9935      zSql = "SELECT lower(name) FROM sqlite_schema"
9936             " WHERE type='table' AND coalesce(rootpage,0)>1"
9937             " AND name NOT LIKE 'sqlite_%'"
9938             " ORDER BY 1 collate nocase";
9939    }
9940    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9941    initText(&sQuery);
9942    initText(&sSql);
9943    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9944    zSep = "VALUES(";
9945    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9946      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9947      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9948      if( strncmp(zTab, "sqlite_",7)!=0 ){
9949        appendText(&sQuery,"SELECT * FROM ", 0);
9950        appendText(&sQuery,zTab,'"');
9951        appendText(&sQuery," NOT INDEXED;", 0);
9952      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9953        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9954                           " ORDER BY name;", 0);
9955      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9956        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9957                           " ORDER BY name;", 0);
9958      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9959        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9960                           " ORDER BY tbl,idx;", 0);
9961      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9962        appendText(&sQuery, "SELECT * FROM ", 0);
9963        appendText(&sQuery, zTab, 0);
9964        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9965      }
9966      appendText(&sSql, zSep, 0);
9967      appendText(&sSql, sQuery.z, '\'');
9968      sQuery.n = 0;
9969      appendText(&sSql, ",", 0);
9970      appendText(&sSql, zTab, '\'');
9971      zSep = "),(";
9972    }
9973    sqlite3_finalize(pStmt);
9974    if( bSeparate ){
9975      zSql = sqlite3_mprintf(
9976          "%s))"
9977          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9978          "   FROM [sha3sum$query]",
9979          sSql.z, iSize);
9980    }else{
9981      zSql = sqlite3_mprintf(
9982          "%s))"
9983          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9984          "   FROM [sha3sum$query]",
9985          sSql.z, iSize);
9986    }
9987    freeText(&sQuery);
9988    freeText(&sSql);
9989    if( bDebug ){
9990      utf8_printf(p->out, "%s\n", zSql);
9991    }else{
9992      shell_exec(p, zSql, 0);
9993    }
9994    sqlite3_free(zSql);
9995  }else
9996
9997#ifndef SQLITE_NOHAVE_SYSTEM
9998  if( c=='s'
9999   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
10000  ){
10001    char *zCmd;
10002    int i, x;
10003    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10004    if( nArg<2 ){
10005      raw_printf(stderr, "Usage: .system COMMAND\n");
10006      rc = 1;
10007      goto meta_command_exit;
10008    }
10009    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10010    for(i=2; i<nArg; i++){
10011      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10012                             zCmd, azArg[i]);
10013    }
10014    x = system(zCmd);
10015    sqlite3_free(zCmd);
10016    if( x ) raw_printf(stderr, "System command returns %d\n", x);
10017  }else
10018#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
10019
10020  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
10021    static const char *azBool[] = { "off", "on", "trigger", "full"};
10022    const char *zOut;
10023    int i;
10024    if( nArg!=1 ){
10025      raw_printf(stderr, "Usage: .show\n");
10026      rc = 1;
10027      goto meta_command_exit;
10028    }
10029    utf8_printf(p->out, "%12.12s: %s\n","echo",
10030                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
10031    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10032    utf8_printf(p->out, "%12.12s: %s\n","explain",
10033         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10034    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10035    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10036    utf8_printf(p->out, "%12.12s: ", "nullvalue");
10037      output_c_string(p->out, p->nullValue);
10038      raw_printf(p->out, "\n");
10039    utf8_printf(p->out,"%12.12s: %s\n","output",
10040            strlen30(p->outfile) ? p->outfile : "stdout");
10041    utf8_printf(p->out,"%12.12s: ", "colseparator");
10042      output_c_string(p->out, p->colSeparator);
10043      raw_printf(p->out, "\n");
10044    utf8_printf(p->out,"%12.12s: ", "rowseparator");
10045      output_c_string(p->out, p->rowSeparator);
10046      raw_printf(p->out, "\n");
10047    switch( p->statsOn ){
10048      case 0:  zOut = "off";     break;
10049      default: zOut = "on";      break;
10050      case 2:  zOut = "stmt";    break;
10051      case 3:  zOut = "vmstep";  break;
10052    }
10053    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10054    utf8_printf(p->out, "%12.12s: ", "width");
10055    for (i=0;i<p->nWidth;i++) {
10056      raw_printf(p->out, "%d ", p->colWidth[i]);
10057    }
10058    raw_printf(p->out, "\n");
10059    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10060                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10061  }else
10062
10063  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
10064    if( nArg==2 ){
10065      if( strcmp(azArg[1],"stmt")==0 ){
10066        p->statsOn = 2;
10067      }else if( strcmp(azArg[1],"vmstep")==0 ){
10068        p->statsOn = 3;
10069      }else{
10070        p->statsOn = (u8)booleanValue(azArg[1]);
10071      }
10072    }else if( nArg==1 ){
10073      display_stats(p->db, p, 0);
10074    }else{
10075      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10076      rc = 1;
10077    }
10078  }else
10079
10080  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
10081   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
10082                 || strncmp(azArg[0], "indexes", n)==0) )
10083  ){
10084    sqlite3_stmt *pStmt;
10085    char **azResult;
10086    int nRow, nAlloc;
10087    int ii;
10088    ShellText s;
10089    initText(&s);
10090    open_db(p, 0);
10091    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10092    if( rc ){
10093      sqlite3_finalize(pStmt);
10094      return shellDatabaseError(p->db);
10095    }
10096
10097    if( nArg>2 && c=='i' ){
10098      /* It is an historical accident that the .indexes command shows an error
10099      ** when called with the wrong number of arguments whereas the .tables
10100      ** command does not. */
10101      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10102      rc = 1;
10103      sqlite3_finalize(pStmt);
10104      goto meta_command_exit;
10105    }
10106    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10107      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10108      if( zDbName==0 ) continue;
10109      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10110      if( sqlite3_stricmp(zDbName, "main")==0 ){
10111        appendText(&s, "SELECT name FROM ", 0);
10112      }else{
10113        appendText(&s, "SELECT ", 0);
10114        appendText(&s, zDbName, '\'');
10115        appendText(&s, "||'.'||name FROM ", 0);
10116      }
10117      appendText(&s, zDbName, '"');
10118      appendText(&s, ".sqlite_schema ", 0);
10119      if( c=='t' ){
10120        appendText(&s," WHERE type IN ('table','view')"
10121                      "   AND name NOT LIKE 'sqlite_%'"
10122                      "   AND name LIKE ?1", 0);
10123      }else{
10124        appendText(&s," WHERE type='index'"
10125                      "   AND tbl_name LIKE ?1", 0);
10126      }
10127    }
10128    rc = sqlite3_finalize(pStmt);
10129    if( rc==SQLITE_OK ){
10130      appendText(&s, " ORDER BY 1", 0);
10131      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10132    }
10133    freeText(&s);
10134    if( rc ) return shellDatabaseError(p->db);
10135
10136    /* Run the SQL statement prepared by the above block. Store the results
10137    ** as an array of nul-terminated strings in azResult[].  */
10138    nRow = nAlloc = 0;
10139    azResult = 0;
10140    if( nArg>1 ){
10141      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10142    }else{
10143      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10144    }
10145    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10146      if( nRow>=nAlloc ){
10147        char **azNew;
10148        int n2 = nAlloc*2 + 10;
10149        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10150        if( azNew==0 ) shell_out_of_memory();
10151        nAlloc = n2;
10152        azResult = azNew;
10153      }
10154      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10155      if( 0==azResult[nRow] ) shell_out_of_memory();
10156      nRow++;
10157    }
10158    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10159      rc = shellDatabaseError(p->db);
10160    }
10161
10162    /* Pretty-print the contents of array azResult[] to the output */
10163    if( rc==0 && nRow>0 ){
10164      int len, maxlen = 0;
10165      int i, j;
10166      int nPrintCol, nPrintRow;
10167      for(i=0; i<nRow; i++){
10168        len = strlen30(azResult[i]);
10169        if( len>maxlen ) maxlen = len;
10170      }
10171      nPrintCol = 80/(maxlen+2);
10172      if( nPrintCol<1 ) nPrintCol = 1;
10173      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10174      for(i=0; i<nPrintRow; i++){
10175        for(j=i; j<nRow; j+=nPrintRow){
10176          char *zSp = j<nPrintRow ? "" : "  ";
10177          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10178                      azResult[j] ? azResult[j]:"");
10179        }
10180        raw_printf(p->out, "\n");
10181      }
10182    }
10183
10184    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10185    sqlite3_free(azResult);
10186  }else
10187
10188  /* Begin redirecting output to the file "testcase-out.txt" */
10189  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
10190    output_reset(p);
10191    p->out = output_file_open("testcase-out.txt", 0);
10192    if( p->out==0 ){
10193      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10194    }
10195    if( nArg>=2 ){
10196      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10197    }else{
10198      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10199    }
10200  }else
10201
10202#ifndef SQLITE_UNTESTABLE
10203  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
10204    static const struct {
10205       const char *zCtrlName;   /* Name of a test-control option */
10206       int ctrlCode;            /* Integer code for that option */
10207       const char *zUsage;      /* Usage notes */
10208    } aCtrl[] = {
10209      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
10210      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
10211    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
10212    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
10213      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
10214      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
10215    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
10216      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
10217      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
10218      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
10219      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
10220      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
10221#ifdef YYCOVERAGE
10222      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
10223#endif
10224      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
10225      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
10226      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
10227      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
10228      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
10229      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP,   "NMAX"           },
10230      { "tune",               SQLITE_TESTCTRL_TUNE,          "ID VALUE"       },
10231    };
10232    int testctrl = -1;
10233    int iCtrl = -1;
10234    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10235    int isOk = 0;
10236    int i, n2;
10237    const char *zCmd = 0;
10238
10239    open_db(p, 0);
10240    zCmd = nArg>=2 ? azArg[1] : "help";
10241
10242    /* The argument can optionally begin with "-" or "--" */
10243    if( zCmd[0]=='-' && zCmd[1] ){
10244      zCmd++;
10245      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10246    }
10247
10248    /* --help lists all test-controls */
10249    if( strcmp(zCmd,"help")==0 ){
10250      utf8_printf(p->out, "Available test-controls:\n");
10251      for(i=0; i<ArraySize(aCtrl); i++){
10252        utf8_printf(p->out, "  .testctrl %s %s\n",
10253                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10254      }
10255      rc = 1;
10256      goto meta_command_exit;
10257    }
10258
10259    /* convert testctrl text option to value. allow any unique prefix
10260    ** of the option name, or a numerical value. */
10261    n2 = strlen30(zCmd);
10262    for(i=0; i<ArraySize(aCtrl); i++){
10263      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10264        if( testctrl<0 ){
10265          testctrl = aCtrl[i].ctrlCode;
10266          iCtrl = i;
10267        }else{
10268          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
10269                              "Use \".testctrl --help\" for help\n", zCmd);
10270          rc = 1;
10271          goto meta_command_exit;
10272        }
10273      }
10274    }
10275    if( testctrl<0 ){
10276      utf8_printf(stderr,"Error: unknown test-control: %s\n"
10277                         "Use \".testctrl --help\" for help\n", zCmd);
10278    }else{
10279      switch(testctrl){
10280
10281        /* sqlite3_test_control(int, db, int) */
10282        case SQLITE_TESTCTRL_OPTIMIZATIONS:
10283          if( nArg==3 ){
10284            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
10285            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10286            isOk = 3;
10287          }
10288          break;
10289
10290        /* sqlite3_test_control(int) */
10291        case SQLITE_TESTCTRL_PRNG_SAVE:
10292        case SQLITE_TESTCTRL_PRNG_RESTORE:
10293        case SQLITE_TESTCTRL_BYTEORDER:
10294          if( nArg==2 ){
10295            rc2 = sqlite3_test_control(testctrl);
10296            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
10297          }
10298          break;
10299
10300        /* sqlite3_test_control(int, uint) */
10301        case SQLITE_TESTCTRL_PENDING_BYTE:
10302          if( nArg==3 ){
10303            unsigned int opt = (unsigned int)integerValue(azArg[2]);
10304            rc2 = sqlite3_test_control(testctrl, opt);
10305            isOk = 3;
10306          }
10307          break;
10308
10309        /* sqlite3_test_control(int, int, sqlite3*) */
10310        case SQLITE_TESTCTRL_PRNG_SEED:
10311          if( nArg==3 || nArg==4 ){
10312            int ii = (int)integerValue(azArg[2]);
10313            sqlite3 *db;
10314            if( ii==0 && strcmp(azArg[2],"random")==0 ){
10315              sqlite3_randomness(sizeof(ii),&ii);
10316              printf("-- random seed: %d\n", ii);
10317            }
10318            if( nArg==3 ){
10319              db = 0;
10320            }else{
10321              db = p->db;
10322              /* Make sure the schema has been loaded */
10323              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10324            }
10325            rc2 = sqlite3_test_control(testctrl, ii, db);
10326            isOk = 3;
10327          }
10328          break;
10329
10330        /* sqlite3_test_control(int, int) */
10331        case SQLITE_TESTCTRL_ASSERT:
10332        case SQLITE_TESTCTRL_ALWAYS:
10333          if( nArg==3 ){
10334            int opt = booleanValue(azArg[2]);
10335            rc2 = sqlite3_test_control(testctrl, opt);
10336            isOk = 1;
10337          }
10338          break;
10339
10340        /* sqlite3_test_control(int, int) */
10341        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10342        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10343          if( nArg==3 ){
10344            int opt = booleanValue(azArg[2]);
10345            rc2 = sqlite3_test_control(testctrl, opt);
10346            isOk = 3;
10347          }
10348          break;
10349
10350        /* sqlite3_test_control(sqlite3*) */
10351        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10352          rc2 = sqlite3_test_control(testctrl, p->db);
10353          isOk = 3;
10354          break;
10355
10356        case SQLITE_TESTCTRL_IMPOSTER:
10357          if( nArg==5 ){
10358            rc2 = sqlite3_test_control(testctrl, p->db,
10359                          azArg[2],
10360                          integerValue(azArg[3]),
10361                          integerValue(azArg[4]));
10362            isOk = 3;
10363          }
10364          break;
10365
10366        case SQLITE_TESTCTRL_SEEK_COUNT: {
10367          u64 x = 0;
10368          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10369          utf8_printf(p->out, "%llu\n", x);
10370          isOk = 3;
10371          break;
10372        }
10373
10374#ifdef YYCOVERAGE
10375        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
10376          if( nArg==2 ){
10377            sqlite3_test_control(testctrl, p->out);
10378            isOk = 3;
10379          }
10380          break;
10381        }
10382#endif
10383#ifdef SQLITE_DEBUG
10384        case SQLITE_TESTCTRL_TUNE: {
10385          if( nArg==4 ){
10386            int id = (int)integerValue(azArg[2]);
10387            int val = (int)integerValue(azArg[3]);
10388            sqlite3_test_control(testctrl, id, &val);
10389            isOk = 3;
10390          }else if( nArg==3 ){
10391            int id = (int)integerValue(azArg[2]);
10392            sqlite3_test_control(testctrl, -id, &rc2);
10393            isOk = 1;
10394          }else if( nArg==2 ){
10395            int id = 1;
10396            while(1){
10397              int val = 0;
10398              rc2 = sqlite3_test_control(testctrl, -id, &val);
10399              if( rc2!=SQLITE_OK ) break;
10400              if( id>1 ) utf8_printf(p->out, "  ");
10401              utf8_printf(p->out, "%d: %d", id, val);
10402              id++;
10403            }
10404            if( id>1 ) utf8_printf(p->out, "\n");
10405            isOk = 3;
10406          }
10407          break;
10408        }
10409#endif
10410        case SQLITE_TESTCTRL_SORTER_MMAP:
10411          if( nArg==3 ){
10412            int opt = (unsigned int)integerValue(azArg[2]);
10413            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10414            isOk = 3;
10415          }
10416          break;
10417      }
10418    }
10419    if( isOk==0 && iCtrl>=0 ){
10420      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10421      rc = 1;
10422    }else if( isOk==1 ){
10423      raw_printf(p->out, "%d\n", rc2);
10424    }else if( isOk==2 ){
10425      raw_printf(p->out, "0x%08x\n", rc2);
10426    }
10427  }else
10428#endif /* !defined(SQLITE_UNTESTABLE) */
10429
10430  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10431    open_db(p, 0);
10432    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10433  }else
10434
10435  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10436    if( nArg==2 ){
10437      enableTimer = booleanValue(azArg[1]);
10438      if( enableTimer && !HAS_TIMER ){
10439        raw_printf(stderr, "Error: timer not available on this system.\n");
10440        enableTimer = 0;
10441      }
10442    }else{
10443      raw_printf(stderr, "Usage: .timer on|off\n");
10444      rc = 1;
10445    }
10446  }else
10447
10448#ifndef SQLITE_OMIT_TRACE
10449  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10450    int mType = 0;
10451    int jj;
10452    open_db(p, 0);
10453    for(jj=1; jj<nArg; jj++){
10454      const char *z = azArg[jj];
10455      if( z[0]=='-' ){
10456        if( optionMatch(z, "expanded") ){
10457          p->eTraceType = SHELL_TRACE_EXPANDED;
10458        }
10459#ifdef SQLITE_ENABLE_NORMALIZE
10460        else if( optionMatch(z, "normalized") ){
10461          p->eTraceType = SHELL_TRACE_NORMALIZED;
10462        }
10463#endif
10464        else if( optionMatch(z, "plain") ){
10465          p->eTraceType = SHELL_TRACE_PLAIN;
10466        }
10467        else if( optionMatch(z, "profile") ){
10468          mType |= SQLITE_TRACE_PROFILE;
10469        }
10470        else if( optionMatch(z, "row") ){
10471          mType |= SQLITE_TRACE_ROW;
10472        }
10473        else if( optionMatch(z, "stmt") ){
10474          mType |= SQLITE_TRACE_STMT;
10475        }
10476        else if( optionMatch(z, "close") ){
10477          mType |= SQLITE_TRACE_CLOSE;
10478        }
10479        else {
10480          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10481          rc = 1;
10482          goto meta_command_exit;
10483        }
10484      }else{
10485        output_file_close(p->traceOut);
10486        p->traceOut = output_file_open(azArg[1], 0);
10487      }
10488    }
10489    if( p->traceOut==0 ){
10490      sqlite3_trace_v2(p->db, 0, 0, 0);
10491    }else{
10492      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10493      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10494    }
10495  }else
10496#endif /* !defined(SQLITE_OMIT_TRACE) */
10497
10498#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10499  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10500    int ii;
10501    int lenOpt;
10502    char *zOpt;
10503    if( nArg<2 ){
10504      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10505      rc = 1;
10506      goto meta_command_exit;
10507    }
10508    open_db(p, 0);
10509    zOpt = azArg[1];
10510    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10511    lenOpt = (int)strlen(zOpt);
10512    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10513      assert( azArg[nArg]==0 );
10514      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10515    }else{
10516      for(ii=1; ii<nArg; ii++){
10517        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10518      }
10519    }
10520  }else
10521#endif
10522
10523#if SQLITE_USER_AUTHENTICATION
10524  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10525    if( nArg<2 ){
10526      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10527      rc = 1;
10528      goto meta_command_exit;
10529    }
10530    open_db(p, 0);
10531    if( strcmp(azArg[1],"login")==0 ){
10532      if( nArg!=4 ){
10533        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10534        rc = 1;
10535        goto meta_command_exit;
10536      }
10537      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10538                                     strlen30(azArg[3]));
10539      if( rc ){
10540        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10541        rc = 1;
10542      }
10543    }else if( strcmp(azArg[1],"add")==0 ){
10544      if( nArg!=5 ){
10545        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10546        rc = 1;
10547        goto meta_command_exit;
10548      }
10549      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10550                            booleanValue(azArg[4]));
10551      if( rc ){
10552        raw_printf(stderr, "User-Add failed: %d\n", rc);
10553        rc = 1;
10554      }
10555    }else if( strcmp(azArg[1],"edit")==0 ){
10556      if( nArg!=5 ){
10557        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10558        rc = 1;
10559        goto meta_command_exit;
10560      }
10561      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10562                              booleanValue(azArg[4]));
10563      if( rc ){
10564        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10565        rc = 1;
10566      }
10567    }else if( strcmp(azArg[1],"delete")==0 ){
10568      if( nArg!=3 ){
10569        raw_printf(stderr, "Usage: .user delete USER\n");
10570        rc = 1;
10571        goto meta_command_exit;
10572      }
10573      rc = sqlite3_user_delete(p->db, azArg[2]);
10574      if( rc ){
10575        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10576        rc = 1;
10577      }
10578    }else{
10579      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10580      rc = 1;
10581      goto meta_command_exit;
10582    }
10583  }else
10584#endif /* SQLITE_USER_AUTHENTICATION */
10585
10586  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10587    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10588        sqlite3_libversion(), sqlite3_sourceid());
10589#if SQLITE_HAVE_ZLIB
10590    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10591#endif
10592#define CTIMEOPT_VAL_(opt) #opt
10593#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10594#if defined(__clang__) && defined(__clang_major__)
10595    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10596                    CTIMEOPT_VAL(__clang_minor__) "."
10597                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10598#elif defined(_MSC_VER)
10599    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10600#elif defined(__GNUC__) && defined(__VERSION__)
10601    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10602#endif
10603  }else
10604
10605  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10606    const char *zDbName = nArg==2 ? azArg[1] : "main";
10607    sqlite3_vfs *pVfs = 0;
10608    if( p->db ){
10609      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10610      if( pVfs ){
10611        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10612        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10613        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10614        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10615      }
10616    }
10617  }else
10618
10619  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10620    sqlite3_vfs *pVfs;
10621    sqlite3_vfs *pCurrent = 0;
10622    if( p->db ){
10623      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10624    }
10625    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10626      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10627           pVfs==pCurrent ? "  <--- CURRENT" : "");
10628      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10629      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10630      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10631      if( pVfs->pNext ){
10632        raw_printf(p->out, "-----------------------------------\n");
10633      }
10634    }
10635  }else
10636
10637  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10638    const char *zDbName = nArg==2 ? azArg[1] : "main";
10639    char *zVfsName = 0;
10640    if( p->db ){
10641      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10642      if( zVfsName ){
10643        utf8_printf(p->out, "%s\n", zVfsName);
10644        sqlite3_free(zVfsName);
10645      }
10646    }
10647  }else
10648
10649  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10650    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10651    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
10652  }else
10653
10654  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10655    int j;
10656    assert( nArg<=ArraySize(azArg) );
10657    p->nWidth = nArg-1;
10658    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
10659    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10660    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10661    for(j=1; j<nArg; j++){
10662      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10663    }
10664  }else
10665
10666  {
10667    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10668      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10669    rc = 1;
10670  }
10671
10672meta_command_exit:
10673  if( p->outCount ){
10674    p->outCount--;
10675    if( p->outCount==0 ) output_reset(p);
10676  }
10677  p->bSafeMode = p->bSafeModePersist;
10678  return rc;
10679}
10680
10681/* Line scan result and intermediate states (supporting scan resumption)
10682*/
10683#ifndef CHAR_BIT
10684# define CHAR_BIT 8
10685#endif
10686typedef enum {
10687  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
10688  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
10689  QSS_Start = 0
10690} QuickScanState;
10691#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
10692#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
10693#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
10694#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
10695#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
10696
10697/*
10698** Scan line for classification to guide shell's handling.
10699** The scan is resumable for subsequent lines when prior
10700** return values are passed as the 2nd argument.
10701*/
10702static QuickScanState quickscan(char *zLine, QuickScanState qss){
10703  char cin;
10704  char cWait = (char)qss; /* intentional narrowing loss */
10705  if( cWait==0 ){
10706  PlainScan:
10707    assert( cWait==0 );
10708    while( (cin = *zLine++)!=0 ){
10709      if( IsSpace(cin) )
10710        continue;
10711      switch (cin){
10712      case '-':
10713        if( *zLine!='-' )
10714          break;
10715        while((cin = *++zLine)!=0 )
10716          if( cin=='\n')
10717            goto PlainScan;
10718        return qss;
10719      case ';':
10720        qss |= QSS_EndingSemi;
10721        continue;
10722      case '/':
10723        if( *zLine=='*' ){
10724          ++zLine;
10725          cWait = '*';
10726          qss = QSS_SETV(qss, cWait);
10727          goto TermScan;
10728        }
10729        break;
10730      case '[':
10731        cin = ']';
10732        /* fall thru */
10733      case '`': case '\'': case '"':
10734        cWait = cin;
10735        qss = QSS_HasDark | cWait;
10736        goto TermScan;
10737      default:
10738        break;
10739      }
10740      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
10741    }
10742  }else{
10743  TermScan:
10744    while( (cin = *zLine++)!=0 ){
10745      if( cin==cWait ){
10746        switch( cWait ){
10747        case '*':
10748          if( *zLine != '/' )
10749            continue;
10750          ++zLine;
10751          cWait = 0;
10752          qss = QSS_SETV(qss, 0);
10753          goto PlainScan;
10754        case '`': case '\'': case '"':
10755          if(*zLine==cWait){
10756            ++zLine;
10757            continue;
10758          }
10759          /* fall thru */
10760        case ']':
10761          cWait = 0;
10762          qss = QSS_SETV(qss, 0);
10763          goto PlainScan;
10764        default: assert(0);
10765        }
10766      }
10767    }
10768  }
10769  return qss;
10770}
10771
10772/*
10773** Return TRUE if the line typed in is an SQL command terminator other
10774** than a semi-colon.  The SQL Server style "go" command is understood
10775** as is the Oracle "/".
10776*/
10777static int line_is_command_terminator(char *zLine){
10778  while( IsSpace(zLine[0]) ){ zLine++; };
10779  if( zLine[0]=='/' )
10780    zLine += 1; /* Oracle */
10781  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
10782    zLine += 2; /* SQL Server */
10783  else
10784    return 0;
10785  return quickscan(zLine,QSS_Start)==QSS_Start;
10786}
10787
10788/*
10789** We need a default sqlite3_complete() implementation to use in case
10790** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10791** any arbitrary text is a complete SQL statement.  This is not very
10792** user-friendly, but it does seem to work.
10793*/
10794#ifdef SQLITE_OMIT_COMPLETE
10795#define sqlite3_complete(x) 1
10796#endif
10797
10798/*
10799** Return true if zSql is a complete SQL statement.  Return false if it
10800** ends in the middle of a string literal or C-style comment.
10801*/
10802static int line_is_complete(char *zSql, int nSql){
10803  int rc;
10804  if( zSql==0 ) return 1;
10805  zSql[nSql] = ';';
10806  zSql[nSql+1] = 0;
10807  rc = sqlite3_complete(zSql);
10808  zSql[nSql] = 0;
10809  return rc;
10810}
10811
10812/*
10813** Run a single line of SQL.  Return the number of errors.
10814*/
10815static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10816  int rc;
10817  char *zErrMsg = 0;
10818
10819  open_db(p, 0);
10820  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10821  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10822  BEGIN_TIMER;
10823  rc = shell_exec(p, zSql, &zErrMsg);
10824  END_TIMER;
10825  if( rc || zErrMsg ){
10826    char zPrefix[100];
10827    if( in!=0 || !stdin_is_interactive ){
10828      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10829                       "Error: near line %d:", startline);
10830    }else{
10831      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10832    }
10833    if( zErrMsg!=0 ){
10834      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10835      sqlite3_free(zErrMsg);
10836      zErrMsg = 0;
10837    }else{
10838      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10839    }
10840    return 1;
10841  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10842    char zLineBuf[2000];
10843    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
10844            "changes: %lld   total_changes: %lld",
10845            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
10846    raw_printf(p->out, "%s\n", zLineBuf);
10847  }
10848  return 0;
10849}
10850
10851
10852/*
10853** Read input from *in and process it.  If *in==0 then input
10854** is interactive - the user is typing it it.  Otherwise, input
10855** is coming from a file or device.  A prompt is issued and history
10856** is saved only if input is interactive.  An interrupt signal will
10857** cause this routine to exit immediately, unless input is interactive.
10858**
10859** Return the number of errors.
10860*/
10861static int process_input(ShellState *p){
10862  char *zLine = 0;          /* A single input line */
10863  char *zSql = 0;           /* Accumulated SQL text */
10864  int nLine;                /* Length of current line */
10865  int nSql = 0;             /* Bytes of zSql[] used */
10866  int nAlloc = 0;           /* Allocated zSql[] space */
10867  int rc;                   /* Error code */
10868  int errCnt = 0;           /* Number of errors seen */
10869  int startline = 0;        /* Line number for start of current input */
10870  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
10871
10872  p->lineno = 0;
10873  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10874    fflush(p->out);
10875    zLine = one_input_line(p->in, zLine, nSql>0);
10876    if( zLine==0 ){
10877      /* End of input */
10878      if( p->in==0 && stdin_is_interactive ) printf("\n");
10879      break;
10880    }
10881    if( seenInterrupt ){
10882      if( p->in!=0 ) break;
10883      seenInterrupt = 0;
10884    }
10885    p->lineno++;
10886    if( QSS_INPLAIN(qss)
10887        && line_is_command_terminator(zLine)
10888        && line_is_complete(zSql, nSql) ){
10889      memcpy(zLine,";",2);
10890    }
10891    qss = quickscan(zLine, qss);
10892    if( QSS_PLAINWHITE(qss) && nSql==0 ){
10893      if( ShellHasFlag(p, SHFLG_Echo) )
10894        printf("%s\n", zLine);
10895      /* Just swallow single-line whitespace */
10896      qss = QSS_Start;
10897      continue;
10898    }
10899    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10900      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10901      if( zLine[0]=='.' ){
10902        rc = do_meta_command(zLine, p);
10903        if( rc==2 ){ /* exit requested */
10904          break;
10905        }else if( rc ){
10906          errCnt++;
10907        }
10908      }
10909      qss = QSS_Start;
10910      continue;
10911    }
10912    /* No single-line dispositions remain; accumulate line(s). */
10913    nLine = strlen30(zLine);
10914    if( nSql+nLine+2>=nAlloc ){
10915      /* Grow buffer by half-again increments when big. */
10916      nAlloc = nSql+(nSql>>1)+nLine+100;
10917      zSql = realloc(zSql, nAlloc);
10918      if( zSql==0 ) shell_out_of_memory();
10919    }
10920    if( nSql==0 ){
10921      int i;
10922      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10923      assert( nAlloc>0 && zSql!=0 );
10924      memcpy(zSql, zLine+i, nLine+1-i);
10925      startline = p->lineno;
10926      nSql = nLine-i;
10927    }else{
10928      zSql[nSql++] = '\n';
10929      memcpy(zSql+nSql, zLine, nLine+1);
10930      nSql += nLine;
10931    }
10932    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
10933      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10934      nSql = 0;
10935      if( p->outCount ){
10936        output_reset(p);
10937        p->outCount = 0;
10938      }else{
10939        clearTempFile(p);
10940      }
10941      p->bSafeMode = p->bSafeModePersist;
10942      qss = QSS_Start;
10943    }else if( nSql && QSS_PLAINWHITE(qss) ){
10944      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10945      nSql = 0;
10946      qss = QSS_Start;
10947    }
10948  }
10949  if( nSql && QSS_PLAINDARK(qss) ){
10950    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10951  }
10952  free(zSql);
10953  free(zLine);
10954  return errCnt>0;
10955}
10956
10957/*
10958** Return a pathname which is the user's home directory.  A
10959** 0 return indicates an error of some kind.
10960*/
10961static char *find_home_dir(int clearFlag){
10962  static char *home_dir = NULL;
10963  if( clearFlag ){
10964    free(home_dir);
10965    home_dir = 0;
10966    return 0;
10967  }
10968  if( home_dir ) return home_dir;
10969
10970#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10971     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10972  {
10973    struct passwd *pwent;
10974    uid_t uid = getuid();
10975    if( (pwent=getpwuid(uid)) != NULL) {
10976      home_dir = pwent->pw_dir;
10977    }
10978  }
10979#endif
10980
10981#if defined(_WIN32_WCE)
10982  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10983   */
10984  home_dir = "/";
10985#else
10986
10987#if defined(_WIN32) || defined(WIN32)
10988  if (!home_dir) {
10989    home_dir = getenv("USERPROFILE");
10990  }
10991#endif
10992
10993  if (!home_dir) {
10994    home_dir = getenv("HOME");
10995  }
10996
10997#if defined(_WIN32) || defined(WIN32)
10998  if (!home_dir) {
10999    char *zDrive, *zPath;
11000    int n;
11001    zDrive = getenv("HOMEDRIVE");
11002    zPath = getenv("HOMEPATH");
11003    if( zDrive && zPath ){
11004      n = strlen30(zDrive) + strlen30(zPath) + 1;
11005      home_dir = malloc( n );
11006      if( home_dir==0 ) return 0;
11007      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11008      return home_dir;
11009    }
11010    home_dir = "c:\\";
11011  }
11012#endif
11013
11014#endif /* !_WIN32_WCE */
11015
11016  if( home_dir ){
11017    int n = strlen30(home_dir) + 1;
11018    char *z = malloc( n );
11019    if( z ) memcpy(z, home_dir, n);
11020    home_dir = z;
11021  }
11022
11023  return home_dir;
11024}
11025
11026/*
11027** Read input from the file given by sqliterc_override.  Or if that
11028** parameter is NULL, take input from ~/.sqliterc
11029**
11030** Returns the number of errors.
11031*/
11032static void process_sqliterc(
11033  ShellState *p,                  /* Configuration data */
11034  const char *sqliterc_override   /* Name of config file. NULL to use default */
11035){
11036  char *home_dir = NULL;
11037  const char *sqliterc = sqliterc_override;
11038  char *zBuf = 0;
11039  FILE *inSaved = p->in;
11040  int savedLineno = p->lineno;
11041
11042  if (sqliterc == NULL) {
11043    home_dir = find_home_dir(0);
11044    if( home_dir==0 ){
11045      raw_printf(stderr, "-- warning: cannot find home directory;"
11046                      " cannot read ~/.sqliterc\n");
11047      return;
11048    }
11049    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11050    sqliterc = zBuf;
11051  }
11052  p->in = fopen(sqliterc,"rb");
11053  if( p->in ){
11054    if( stdin_is_interactive ){
11055      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11056    }
11057    if( process_input(p) && bail_on_error ) exit(1);
11058    fclose(p->in);
11059  }else if( sqliterc_override!=0 ){
11060    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11061    if( bail_on_error ) exit(1);
11062  }
11063  p->in = inSaved;
11064  p->lineno = savedLineno;
11065  sqlite3_free(zBuf);
11066}
11067
11068/*
11069** Show available command line options
11070*/
11071static const char zOptions[] =
11072#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11073  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11074#endif
11075  "   -append              append the database to the end of the file\n"
11076  "   -ascii               set output mode to 'ascii'\n"
11077  "   -bail                stop after hitting an error\n"
11078  "   -batch               force batch I/O\n"
11079  "   -box                 set output mode to 'box'\n"
11080  "   -column              set output mode to 'column'\n"
11081  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11082  "   -csv                 set output mode to 'csv'\n"
11083#if !defined(SQLITE_OMIT_DESERIALIZE)
11084  "   -deserialize         open the database using sqlite3_deserialize()\n"
11085#endif
11086  "   -echo                print commands before execution\n"
11087  "   -init FILENAME       read/process named file\n"
11088  "   -[no]header          turn headers on or off\n"
11089#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11090  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11091#endif
11092  "   -help                show this message\n"
11093  "   -html                set output mode to HTML\n"
11094  "   -interactive         force interactive I/O\n"
11095  "   -json                set output mode to 'json'\n"
11096  "   -line                set output mode to 'line'\n"
11097  "   -list                set output mode to 'list'\n"
11098  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11099  "   -markdown            set output mode to 'markdown'\n"
11100#if !defined(SQLITE_OMIT_DESERIALIZE)
11101  "   -maxsize N           maximum size for a --deserialize database\n"
11102#endif
11103  "   -memtrace            trace all memory allocations and deallocations\n"
11104  "   -mmap N              default mmap size set to N\n"
11105#ifdef SQLITE_ENABLE_MULTIPLEX
11106  "   -multiplex           enable the multiplexor VFS\n"
11107#endif
11108  "   -newline SEP         set output row separator. Default: '\\n'\n"
11109  "   -nofollow            refuse to open symbolic links to database files\n"
11110  "   -nonce STRING        set the safe-mode escape nonce\n"
11111  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11112  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11113  "   -quote               set output mode to 'quote'\n"
11114  "   -readonly            open the database read-only\n"
11115  "   -safe                enable safe-mode\n"
11116  "   -separator SEP       set output column separator. Default: '|'\n"
11117#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11118  "   -sorterref SIZE      sorter references threshold size\n"
11119#endif
11120  "   -stats               print memory stats before each finalize\n"
11121  "   -table               set output mode to 'table'\n"
11122  "   -tabs                set output mode to 'tabs'\n"
11123  "   -version             show SQLite version\n"
11124  "   -vfs NAME            use NAME as the default VFS\n"
11125#ifdef SQLITE_ENABLE_VFSTRACE
11126  "   -vfstrace            enable tracing of all VFS calls\n"
11127#endif
11128#ifdef SQLITE_HAVE_ZLIB
11129  "   -zip                 open the file as a ZIP Archive\n"
11130#endif
11131;
11132static void usage(int showDetail){
11133  utf8_printf(stderr,
11134      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11135      "FILENAME is the name of an SQLite database. A new database is created\n"
11136      "if the file does not previously exist.\n", Argv0);
11137  if( showDetail ){
11138    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11139  }else{
11140    raw_printf(stderr, "Use the -help option for additional information\n");
11141  }
11142  exit(1);
11143}
11144
11145/*
11146** Internal check:  Verify that the SQLite is uninitialized.  Print a
11147** error message if it is initialized.
11148*/
11149static void verify_uninitialized(void){
11150  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11151    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11152                        " initialization.\n");
11153  }
11154}
11155
11156/*
11157** Initialize the state information in data
11158*/
11159static void main_init(ShellState *data) {
11160  memset(data, 0, sizeof(*data));
11161  data->normalMode = data->cMode = data->mode = MODE_List;
11162  data->autoExplain = 1;
11163  data->pAuxDb = &data->aAuxDb[0];
11164  memcpy(data->colSeparator,SEP_Column, 2);
11165  memcpy(data->rowSeparator,SEP_Row, 2);
11166  data->showHeader = 0;
11167  data->shellFlgs = SHFLG_Lookaside;
11168  verify_uninitialized();
11169  sqlite3_config(SQLITE_CONFIG_URI, 1);
11170  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11171  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11172  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11173  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11174}
11175
11176/*
11177** Output text to the console in a font that attracts extra attention.
11178*/
11179#ifdef _WIN32
11180static void printBold(const char *zText){
11181#if !SQLITE_OS_WINRT
11182  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
11183  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
11184  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
11185  SetConsoleTextAttribute(out,
11186         FOREGROUND_RED|FOREGROUND_INTENSITY
11187  );
11188#endif
11189  printf("%s", zText);
11190#if !SQLITE_OS_WINRT
11191  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
11192#endif
11193}
11194#else
11195static void printBold(const char *zText){
11196  printf("\033[1m%s\033[0m", zText);
11197}
11198#endif
11199
11200/*
11201** Get the argument to an --option.  Throw an error and die if no argument
11202** is available.
11203*/
11204static char *cmdline_option_value(int argc, char **argv, int i){
11205  if( i==argc ){
11206    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
11207            argv[0], argv[argc-1]);
11208    exit(1);
11209  }
11210  return argv[i];
11211}
11212
11213#ifndef SQLITE_SHELL_IS_UTF8
11214#  if (defined(_WIN32) || defined(WIN32)) \
11215   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
11216#    define SQLITE_SHELL_IS_UTF8          (0)
11217#  else
11218#    define SQLITE_SHELL_IS_UTF8          (1)
11219#  endif
11220#endif
11221
11222#if SQLITE_SHELL_IS_UTF8
11223int SQLITE_CDECL main(int argc, char **argv){
11224#else
11225int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
11226  char **argv;
11227#endif
11228  char *zErrMsg = 0;
11229  ShellState data;
11230  const char *zInitFile = 0;
11231  int i;
11232  int rc = 0;
11233  int warnInmemoryDb = 0;
11234  int readStdin = 1;
11235  int nCmd = 0;
11236  char **azCmd = 0;
11237  const char *zVfs = 0;           /* Value of -vfs command-line option */
11238#if !SQLITE_SHELL_IS_UTF8
11239  char **argvToFree = 0;
11240  int argcToFree = 0;
11241#endif
11242
11243  setBinaryMode(stdin, 0);
11244  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
11245  stdin_is_interactive = isatty(0);
11246  stdout_is_console = isatty(1);
11247
11248#ifdef SQLITE_DEBUG
11249  registerOomSimulator();
11250#endif
11251
11252#if !defined(_WIN32_WCE)
11253  if( getenv("SQLITE_DEBUG_BREAK") ){
11254    if( isatty(0) && isatty(2) ){
11255      fprintf(stderr,
11256          "attach debugger to process %d and press any key to continue.\n",
11257          GETPID());
11258      fgetc(stdin);
11259    }else{
11260#if defined(_WIN32) || defined(WIN32)
11261#if SQLITE_OS_WINRT
11262      __debugbreak();
11263#else
11264      DebugBreak();
11265#endif
11266#elif defined(SIGTRAP)
11267      raise(SIGTRAP);
11268#endif
11269    }
11270  }
11271#endif
11272
11273#if USE_SYSTEM_SQLITE+0!=1
11274  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
11275    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
11276            sqlite3_sourceid(), SQLITE_SOURCE_ID);
11277    exit(1);
11278  }
11279#endif
11280  main_init(&data);
11281
11282  /* On Windows, we must translate command-line arguments into UTF-8.
11283  ** The SQLite memory allocator subsystem has to be enabled in order to
11284  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
11285  ** subsequent sqlite3_config() calls will work.  So copy all results into
11286  ** memory that does not come from the SQLite memory allocator.
11287  */
11288#if !SQLITE_SHELL_IS_UTF8
11289  sqlite3_initialize();
11290  argvToFree = malloc(sizeof(argv[0])*argc*2);
11291  argcToFree = argc;
11292  argv = argvToFree + argc;
11293  if( argv==0 ) shell_out_of_memory();
11294  for(i=0; i<argc; i++){
11295    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
11296    int n;
11297    if( z==0 ) shell_out_of_memory();
11298    n = (int)strlen(z);
11299    argv[i] = malloc( n+1 );
11300    if( argv[i]==0 ) shell_out_of_memory();
11301    memcpy(argv[i], z, n+1);
11302    argvToFree[i] = argv[i];
11303    sqlite3_free(z);
11304  }
11305  sqlite3_shutdown();
11306#endif
11307
11308  assert( argc>=1 && argv && argv[0] );
11309  Argv0 = argv[0];
11310
11311  /* Make sure we have a valid signal handler early, before anything
11312  ** else is done.
11313  */
11314#ifdef SIGINT
11315  signal(SIGINT, interrupt_handler);
11316#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11317  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
11318#endif
11319
11320#ifdef SQLITE_SHELL_DBNAME_PROC
11321  {
11322    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
11323    ** of a C-function that will provide the name of the database file.  Use
11324    ** this compile-time option to embed this shell program in larger
11325    ** applications. */
11326    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
11327    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
11328    warnInmemoryDb = 0;
11329  }
11330#endif
11331
11332  /* Do an initial pass through the command-line argument to locate
11333  ** the name of the database file, the name of the initialization file,
11334  ** the size of the alternative malloc heap,
11335  ** and the first command to execute.
11336  */
11337  verify_uninitialized();
11338  for(i=1; i<argc; i++){
11339    char *z;
11340    z = argv[i];
11341    if( z[0]!='-' ){
11342      if( data.aAuxDb->zDbFilename==0 ){
11343        data.aAuxDb->zDbFilename = z;
11344      }else{
11345        /* Excesss arguments are interpreted as SQL (or dot-commands) and
11346        ** mean that nothing is read from stdin */
11347        readStdin = 0;
11348        nCmd++;
11349        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
11350        if( azCmd==0 ) shell_out_of_memory();
11351        azCmd[nCmd-1] = z;
11352      }
11353    }
11354    if( z[1]=='-' ) z++;
11355    if( strcmp(z,"-separator")==0
11356     || strcmp(z,"-nullvalue")==0
11357     || strcmp(z,"-newline")==0
11358     || strcmp(z,"-cmd")==0
11359    ){
11360      (void)cmdline_option_value(argc, argv, ++i);
11361    }else if( strcmp(z,"-init")==0 ){
11362      zInitFile = cmdline_option_value(argc, argv, ++i);
11363    }else if( strcmp(z,"-batch")==0 ){
11364      /* Need to check for batch mode here to so we can avoid printing
11365      ** informational messages (like from process_sqliterc) before
11366      ** we do the actual processing of arguments later in a second pass.
11367      */
11368      stdin_is_interactive = 0;
11369    }else if( strcmp(z,"-heap")==0 ){
11370#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11371      const char *zSize;
11372      sqlite3_int64 szHeap;
11373
11374      zSize = cmdline_option_value(argc, argv, ++i);
11375      szHeap = integerValue(zSize);
11376      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
11377      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
11378#else
11379      (void)cmdline_option_value(argc, argv, ++i);
11380#endif
11381    }else if( strcmp(z,"-pagecache")==0 ){
11382      sqlite3_int64 n, sz;
11383      sz = integerValue(cmdline_option_value(argc,argv,++i));
11384      if( sz>70000 ) sz = 70000;
11385      if( sz<0 ) sz = 0;
11386      n = integerValue(cmdline_option_value(argc,argv,++i));
11387      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
11388        n = 0xffffffffffffLL/sz;
11389      }
11390      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
11391                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
11392      data.shellFlgs |= SHFLG_Pagecache;
11393    }else if( strcmp(z,"-lookaside")==0 ){
11394      int n, sz;
11395      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
11396      if( sz<0 ) sz = 0;
11397      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11398      if( n<0 ) n = 0;
11399      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
11400      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
11401    }else if( strcmp(z,"-threadsafe")==0 ){
11402      int n;
11403      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11404      switch( n ){
11405         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
11406         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
11407         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
11408      }
11409#ifdef SQLITE_ENABLE_VFSTRACE
11410    }else if( strcmp(z,"-vfstrace")==0 ){
11411      extern int vfstrace_register(
11412         const char *zTraceName,
11413         const char *zOldVfsName,
11414         int (*xOut)(const char*,void*),
11415         void *pOutArg,
11416         int makeDefault
11417      );
11418      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
11419#endif
11420#ifdef SQLITE_ENABLE_MULTIPLEX
11421    }else if( strcmp(z,"-multiplex")==0 ){
11422      extern int sqlite3_multiple_initialize(const char*,int);
11423      sqlite3_multiplex_initialize(0, 1);
11424#endif
11425    }else if( strcmp(z,"-mmap")==0 ){
11426      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11427      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
11428#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11429    }else if( strcmp(z,"-sorterref")==0 ){
11430      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11431      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11432#endif
11433    }else if( strcmp(z,"-vfs")==0 ){
11434      zVfs = cmdline_option_value(argc, argv, ++i);
11435#ifdef SQLITE_HAVE_ZLIB
11436    }else if( strcmp(z,"-zip")==0 ){
11437      data.openMode = SHELL_OPEN_ZIPFILE;
11438#endif
11439    }else if( strcmp(z,"-append")==0 ){
11440      data.openMode = SHELL_OPEN_APPENDVFS;
11441#ifndef SQLITE_OMIT_DESERIALIZE
11442    }else if( strcmp(z,"-deserialize")==0 ){
11443      data.openMode = SHELL_OPEN_DESERIALIZE;
11444    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11445      data.szMax = integerValue(argv[++i]);
11446#endif
11447    }else if( strcmp(z,"-readonly")==0 ){
11448      data.openMode = SHELL_OPEN_READONLY;
11449    }else if( strcmp(z,"-nofollow")==0 ){
11450      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11451#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11452    }else if( strncmp(z, "-A",2)==0 ){
11453      /* All remaining command-line arguments are passed to the ".archive"
11454      ** command, so ignore them */
11455      break;
11456#endif
11457    }else if( strcmp(z, "-memtrace")==0 ){
11458      sqlite3MemTraceActivate(stderr);
11459    }else if( strcmp(z,"-bail")==0 ){
11460      bail_on_error = 1;
11461    }else if( strcmp(z,"-nonce")==0 ){
11462      free(data.zNonce);
11463      data.zNonce = strdup(argv[++i]);
11464    }else if( strcmp(z,"-safe")==0 ){
11465      /* no-op - catch this on the second pass */
11466    }
11467  }
11468  verify_uninitialized();
11469
11470
11471#ifdef SQLITE_SHELL_INIT_PROC
11472  {
11473    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11474    ** of a C-function that will perform initialization actions on SQLite that
11475    ** occur just before or after sqlite3_initialize(). Use this compile-time
11476    ** option to embed this shell program in larger applications. */
11477    extern void SQLITE_SHELL_INIT_PROC(void);
11478    SQLITE_SHELL_INIT_PROC();
11479  }
11480#else
11481  /* All the sqlite3_config() calls have now been made. So it is safe
11482  ** to call sqlite3_initialize() and process any command line -vfs option. */
11483  sqlite3_initialize();
11484#endif
11485
11486  if( zVfs ){
11487    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11488    if( pVfs ){
11489      sqlite3_vfs_register(pVfs, 1);
11490    }else{
11491      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11492      exit(1);
11493    }
11494  }
11495
11496  if( data.pAuxDb->zDbFilename==0 ){
11497#ifndef SQLITE_OMIT_MEMORYDB
11498    data.pAuxDb->zDbFilename = ":memory:";
11499    warnInmemoryDb = argc==1;
11500#else
11501    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11502    return 1;
11503#endif
11504  }
11505  data.out = stdout;
11506  sqlite3_appendvfs_init(0,0,0);
11507
11508  /* Go ahead and open the database file if it already exists.  If the
11509  ** file does not exist, delay opening it.  This prevents empty database
11510  ** files from being created if a user mistypes the database name argument
11511  ** to the sqlite command-line tool.
11512  */
11513  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
11514    open_db(&data, 0);
11515  }
11516
11517  /* Process the initialization file if there is one.  If no -init option
11518  ** is given on the command line, look for a file named ~/.sqliterc and
11519  ** try to process it.
11520  */
11521  process_sqliterc(&data,zInitFile);
11522
11523  /* Make a second pass through the command-line argument and set
11524  ** options.  This second pass is delayed until after the initialization
11525  ** file is processed so that the command-line arguments will override
11526  ** settings in the initialization file.
11527  */
11528  for(i=1; i<argc; i++){
11529    char *z = argv[i];
11530    if( z[0]!='-' ) continue;
11531    if( z[1]=='-' ){ z++; }
11532    if( strcmp(z,"-init")==0 ){
11533      i++;
11534    }else if( strcmp(z,"-html")==0 ){
11535      data.mode = MODE_Html;
11536    }else if( strcmp(z,"-list")==0 ){
11537      data.mode = MODE_List;
11538    }else if( strcmp(z,"-quote")==0 ){
11539      data.mode = MODE_Quote;
11540      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11541      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11542    }else if( strcmp(z,"-line")==0 ){
11543      data.mode = MODE_Line;
11544    }else if( strcmp(z,"-column")==0 ){
11545      data.mode = MODE_Column;
11546    }else if( strcmp(z,"-json")==0 ){
11547      data.mode = MODE_Json;
11548    }else if( strcmp(z,"-markdown")==0 ){
11549      data.mode = MODE_Markdown;
11550    }else if( strcmp(z,"-table")==0 ){
11551      data.mode = MODE_Table;
11552    }else if( strcmp(z,"-box")==0 ){
11553      data.mode = MODE_Box;
11554    }else if( strcmp(z,"-csv")==0 ){
11555      data.mode = MODE_Csv;
11556      memcpy(data.colSeparator,",",2);
11557#ifdef SQLITE_HAVE_ZLIB
11558    }else if( strcmp(z,"-zip")==0 ){
11559      data.openMode = SHELL_OPEN_ZIPFILE;
11560#endif
11561    }else if( strcmp(z,"-append")==0 ){
11562      data.openMode = SHELL_OPEN_APPENDVFS;
11563#ifndef SQLITE_OMIT_DESERIALIZE
11564    }else if( strcmp(z,"-deserialize")==0 ){
11565      data.openMode = SHELL_OPEN_DESERIALIZE;
11566    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11567      data.szMax = integerValue(argv[++i]);
11568#endif
11569    }else if( strcmp(z,"-readonly")==0 ){
11570      data.openMode = SHELL_OPEN_READONLY;
11571    }else if( strcmp(z,"-nofollow")==0 ){
11572      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11573    }else if( strcmp(z,"-ascii")==0 ){
11574      data.mode = MODE_Ascii;
11575      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
11576      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
11577    }else if( strcmp(z,"-tabs")==0 ){
11578      data.mode = MODE_List;
11579      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
11580      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11581    }else if( strcmp(z,"-separator")==0 ){
11582      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11583                       "%s",cmdline_option_value(argc,argv,++i));
11584    }else if( strcmp(z,"-newline")==0 ){
11585      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11586                       "%s",cmdline_option_value(argc,argv,++i));
11587    }else if( strcmp(z,"-nullvalue")==0 ){
11588      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11589                       "%s",cmdline_option_value(argc,argv,++i));
11590    }else if( strcmp(z,"-header")==0 ){
11591      data.showHeader = 1;
11592      ShellSetFlag(&data, SHFLG_HeaderSet);
11593     }else if( strcmp(z,"-noheader")==0 ){
11594      data.showHeader = 0;
11595      ShellSetFlag(&data, SHFLG_HeaderSet);
11596    }else if( strcmp(z,"-echo")==0 ){
11597      ShellSetFlag(&data, SHFLG_Echo);
11598    }else if( strcmp(z,"-eqp")==0 ){
11599      data.autoEQP = AUTOEQP_on;
11600    }else if( strcmp(z,"-eqpfull")==0 ){
11601      data.autoEQP = AUTOEQP_full;
11602    }else if( strcmp(z,"-stats")==0 ){
11603      data.statsOn = 1;
11604    }else if( strcmp(z,"-scanstats")==0 ){
11605      data.scanstatsOn = 1;
11606    }else if( strcmp(z,"-backslash")==0 ){
11607      /* Undocumented command-line option: -backslash
11608      ** Causes C-style backslash escapes to be evaluated in SQL statements
11609      ** prior to sending the SQL into SQLite.  Useful for injecting
11610      ** crazy bytes in the middle of SQL statements for testing and debugging.
11611      */
11612      ShellSetFlag(&data, SHFLG_Backslash);
11613    }else if( strcmp(z,"-bail")==0 ){
11614      /* No-op.  The bail_on_error flag should already be set. */
11615    }else if( strcmp(z,"-version")==0 ){
11616      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11617      return 0;
11618    }else if( strcmp(z,"-interactive")==0 ){
11619      stdin_is_interactive = 1;
11620    }else if( strcmp(z,"-batch")==0 ){
11621      stdin_is_interactive = 0;
11622    }else if( strcmp(z,"-heap")==0 ){
11623      i++;
11624    }else if( strcmp(z,"-pagecache")==0 ){
11625      i+=2;
11626    }else if( strcmp(z,"-lookaside")==0 ){
11627      i+=2;
11628    }else if( strcmp(z,"-threadsafe")==0 ){
11629      i+=2;
11630    }else if( strcmp(z,"-nonce")==0 ){
11631      i += 2;
11632    }else if( strcmp(z,"-mmap")==0 ){
11633      i++;
11634    }else if( strcmp(z,"-memtrace")==0 ){
11635      i++;
11636#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11637    }else if( strcmp(z,"-sorterref")==0 ){
11638      i++;
11639#endif
11640    }else if( strcmp(z,"-vfs")==0 ){
11641      i++;
11642#ifdef SQLITE_ENABLE_VFSTRACE
11643    }else if( strcmp(z,"-vfstrace")==0 ){
11644      i++;
11645#endif
11646#ifdef SQLITE_ENABLE_MULTIPLEX
11647    }else if( strcmp(z,"-multiplex")==0 ){
11648      i++;
11649#endif
11650    }else if( strcmp(z,"-help")==0 ){
11651      usage(1);
11652    }else if( strcmp(z,"-cmd")==0 ){
11653      /* Run commands that follow -cmd first and separately from commands
11654      ** that simply appear on the command-line.  This seems goofy.  It would
11655      ** be better if all commands ran in the order that they appear.  But
11656      ** we retain the goofy behavior for historical compatibility. */
11657      if( i==argc-1 ) break;
11658      z = cmdline_option_value(argc,argv,++i);
11659      if( z[0]=='.' ){
11660        rc = do_meta_command(z, &data);
11661        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11662      }else{
11663        open_db(&data, 0);
11664        rc = shell_exec(&data, z, &zErrMsg);
11665        if( zErrMsg!=0 ){
11666          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11667          if( bail_on_error ) return rc!=0 ? rc : 1;
11668        }else if( rc!=0 ){
11669          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11670          if( bail_on_error ) return rc;
11671        }
11672      }
11673#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11674    }else if( strncmp(z, "-A", 2)==0 ){
11675      if( nCmd>0 ){
11676        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11677                            " with \"%s\"\n", z);
11678        return 1;
11679      }
11680      open_db(&data, OPEN_DB_ZIPFILE);
11681      if( z[2] ){
11682        argv[i] = &z[2];
11683        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11684      }else{
11685        arDotCommand(&data, 1, argv+i, argc-i);
11686      }
11687      readStdin = 0;
11688      break;
11689#endif
11690    }else if( strcmp(z,"-safe")==0 ){
11691      data.bSafeMode = data.bSafeModePersist = 1;
11692    }else{
11693      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11694      raw_printf(stderr,"Use -help for a list of options.\n");
11695      return 1;
11696    }
11697    data.cMode = data.mode;
11698  }
11699
11700  if( !readStdin ){
11701    /* Run all arguments that do not begin with '-' as if they were separate
11702    ** command-line inputs, except for the argToSkip argument which contains
11703    ** the database filename.
11704    */
11705    for(i=0; i<nCmd; i++){
11706      if( azCmd[i][0]=='.' ){
11707        rc = do_meta_command(azCmd[i], &data);
11708        if( rc ){
11709          free(azCmd);
11710          return rc==2 ? 0 : rc;
11711        }
11712      }else{
11713        open_db(&data, 0);
11714        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11715        if( zErrMsg || rc ){
11716          if( zErrMsg!=0 ){
11717            utf8_printf(stderr,"Error: %s\n", zErrMsg);
11718          }else{
11719            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11720          }
11721          sqlite3_free(zErrMsg);
11722          free(azCmd);
11723          return rc!=0 ? rc : 1;
11724        }
11725      }
11726    }
11727  }else{
11728    /* Run commands received from standard input
11729    */
11730    if( stdin_is_interactive ){
11731      char *zHome;
11732      char *zHistory;
11733      int nHistory;
11734      printf(
11735        "SQLite version %s %.19s\n" /*extra-version-info*/
11736        "Enter \".help\" for usage hints.\n",
11737        sqlite3_libversion(), sqlite3_sourceid()
11738      );
11739      if( warnInmemoryDb ){
11740        printf("Connected to a ");
11741        printBold("transient in-memory database");
11742        printf(".\nUse \".open FILENAME\" to reopen on a "
11743               "persistent database.\n");
11744      }
11745      zHistory = getenv("SQLITE_HISTORY");
11746      if( zHistory ){
11747        zHistory = strdup(zHistory);
11748      }else if( (zHome = find_home_dir(0))!=0 ){
11749        nHistory = strlen30(zHome) + 20;
11750        if( (zHistory = malloc(nHistory))!=0 ){
11751          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11752        }
11753      }
11754      if( zHistory ){ shell_read_history(zHistory); }
11755#if HAVE_READLINE || HAVE_EDITLINE
11756      rl_attempted_completion_function = readline_completion;
11757#elif HAVE_LINENOISE
11758      linenoiseSetCompletionCallback(linenoise_completion);
11759#endif
11760      data.in = 0;
11761      rc = process_input(&data);
11762      if( zHistory ){
11763        shell_stifle_history(2000);
11764        shell_write_history(zHistory);
11765        free(zHistory);
11766      }
11767    }else{
11768      data.in = stdin;
11769      rc = process_input(&data);
11770    }
11771  }
11772  free(azCmd);
11773  set_table_name(&data, 0);
11774  if( data.db ){
11775    session_close_all(&data, -1);
11776    close_db(data.db);
11777  }
11778  for(i=0; i<ArraySize(data.aAuxDb); i++){
11779    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
11780    if( data.aAuxDb[i].db ){
11781      session_close_all(&data, i);
11782      close_db(data.aAuxDb[i].db);
11783    }
11784  }
11785  find_home_dir(1);
11786  output_reset(&data);
11787  data.doXdgOpen = 0;
11788  clearTempFile(&data);
11789#if !SQLITE_SHELL_IS_UTF8
11790  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11791  free(argvToFree);
11792#endif
11793  free(data.colWidth);
11794  free(data.zNonce);
11795  /* Clear the global data structure so that valgrind will detect memory
11796  ** leaks */
11797  memset(&data, 0, sizeof(data));
11798  return rc;
11799}
11800