xref: /sqlite-3.40.0/src/shell.c.in (revision b565bee6)
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->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      j = sz;
1459    }else{
1460      /* If the file did not originally contain \r\n then convert any new
1461      ** \r\n back into \n */
1462      for(i=j=0; i<sz; i++){
1463        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1464        p[j++] = p[i];
1465      }
1466      sz = j;
1467      p[sz] = 0;
1468    }
1469    sqlite3_result_text64(context, (const char*)p, sz,
1470                          sqlite3_free, SQLITE_UTF8);
1471  }
1472  p = 0;
1473
1474edit_func_end:
1475  if( f ) fclose(f);
1476  unlink(zTempFile);
1477  sqlite3_free(zTempFile);
1478  sqlite3_free(p);
1479}
1480#endif /* SQLITE_NOHAVE_SYSTEM */
1481
1482/*
1483** Save or restore the current output mode
1484*/
1485static void outputModePush(ShellState *p){
1486  p->modePrior = p->mode;
1487  p->priorShFlgs = p->shellFlgs;
1488  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1489  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1490}
1491static void outputModePop(ShellState *p){
1492  p->mode = p->modePrior;
1493  p->shellFlgs = p->priorShFlgs;
1494  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1495  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1496}
1497
1498/*
1499** Output the given string as a hex-encoded blob (eg. X'1234' )
1500*/
1501static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1502  int i;
1503  char *zBlob = (char *)pBlob;
1504  raw_printf(out,"X'");
1505  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1506  raw_printf(out,"'");
1507}
1508
1509/*
1510** Find a string that is not found anywhere in z[].  Return a pointer
1511** to that string.
1512**
1513** Try to use zA and zB first.  If both of those are already found in z[]
1514** then make up some string and store it in the buffer zBuf.
1515*/
1516static const char *unused_string(
1517  const char *z,                    /* Result must not appear anywhere in z */
1518  const char *zA, const char *zB,   /* Try these first */
1519  char *zBuf                        /* Space to store a generated string */
1520){
1521  unsigned i = 0;
1522  if( strstr(z, zA)==0 ) return zA;
1523  if( strstr(z, zB)==0 ) return zB;
1524  do{
1525    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1526  }while( strstr(z,zBuf)!=0 );
1527  return zBuf;
1528}
1529
1530/*
1531** Output the given string as a quoted string using SQL quoting conventions.
1532**
1533** See also: output_quoted_escaped_string()
1534*/
1535static void output_quoted_string(FILE *out, const char *z){
1536  int i;
1537  char c;
1538  setBinaryMode(out, 1);
1539  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1540  if( c==0 ){
1541    utf8_printf(out,"'%s'",z);
1542  }else{
1543    raw_printf(out, "'");
1544    while( *z ){
1545      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1546      if( c=='\'' ) i++;
1547      if( i ){
1548        utf8_printf(out, "%.*s", i, z);
1549        z += i;
1550      }
1551      if( c=='\'' ){
1552        raw_printf(out, "'");
1553        continue;
1554      }
1555      if( c==0 ){
1556        break;
1557      }
1558      z++;
1559    }
1560    raw_printf(out, "'");
1561  }
1562  setTextMode(out, 1);
1563}
1564
1565/*
1566** Output the given string as a quoted string using SQL quoting conventions.
1567** Additionallly , escape the "\n" and "\r" characters so that they do not
1568** get corrupted by end-of-line translation facilities in some operating
1569** systems.
1570**
1571** This is like output_quoted_string() but with the addition of the \r\n
1572** escape mechanism.
1573*/
1574static void output_quoted_escaped_string(FILE *out, const char *z){
1575  int i;
1576  char c;
1577  setBinaryMode(out, 1);
1578  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1579  if( c==0 ){
1580    utf8_printf(out,"'%s'",z);
1581  }else{
1582    const char *zNL = 0;
1583    const char *zCR = 0;
1584    int nNL = 0;
1585    int nCR = 0;
1586    char zBuf1[20], zBuf2[20];
1587    for(i=0; z[i]; i++){
1588      if( z[i]=='\n' ) nNL++;
1589      if( z[i]=='\r' ) nCR++;
1590    }
1591    if( nNL ){
1592      raw_printf(out, "replace(");
1593      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1594    }
1595    if( nCR ){
1596      raw_printf(out, "replace(");
1597      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1598    }
1599    raw_printf(out, "'");
1600    while( *z ){
1601      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1602      if( c=='\'' ) i++;
1603      if( i ){
1604        utf8_printf(out, "%.*s", i, z);
1605        z += i;
1606      }
1607      if( c=='\'' ){
1608        raw_printf(out, "'");
1609        continue;
1610      }
1611      if( c==0 ){
1612        break;
1613      }
1614      z++;
1615      if( c=='\n' ){
1616        raw_printf(out, "%s", zNL);
1617        continue;
1618      }
1619      raw_printf(out, "%s", zCR);
1620    }
1621    raw_printf(out, "'");
1622    if( nCR ){
1623      raw_printf(out, ",'%s',char(13))", zCR);
1624    }
1625    if( nNL ){
1626      raw_printf(out, ",'%s',char(10))", zNL);
1627    }
1628  }
1629  setTextMode(out, 1);
1630}
1631
1632/*
1633** Output the given string as a quoted according to C or TCL quoting rules.
1634*/
1635static void output_c_string(FILE *out, const char *z){
1636  unsigned int c;
1637  fputc('"', out);
1638  while( (c = *(z++))!=0 ){
1639    if( c=='\\' ){
1640      fputc(c, out);
1641      fputc(c, out);
1642    }else if( c=='"' ){
1643      fputc('\\', out);
1644      fputc('"', out);
1645    }else if( c=='\t' ){
1646      fputc('\\', out);
1647      fputc('t', out);
1648    }else if( c=='\n' ){
1649      fputc('\\', out);
1650      fputc('n', out);
1651    }else if( c=='\r' ){
1652      fputc('\\', out);
1653      fputc('r', out);
1654    }else if( !isprint(c&0xff) ){
1655      raw_printf(out, "\\%03o", c&0xff);
1656    }else{
1657      fputc(c, out);
1658    }
1659  }
1660  fputc('"', out);
1661}
1662
1663/*
1664** Output the given string as a quoted according to JSON quoting rules.
1665*/
1666static void output_json_string(FILE *out, const char *z, int n){
1667  unsigned int c;
1668  if( n<0 ) n = (int)strlen(z);
1669  fputc('"', out);
1670  while( n-- ){
1671    c = *(z++);
1672    if( c=='\\' || c=='"' ){
1673      fputc('\\', out);
1674      fputc(c, out);
1675    }else if( c<=0x1f ){
1676      fputc('\\', out);
1677      if( c=='\b' ){
1678        fputc('b', out);
1679      }else if( c=='\f' ){
1680        fputc('f', out);
1681      }else if( c=='\n' ){
1682        fputc('n', out);
1683      }else if( c=='\r' ){
1684        fputc('r', out);
1685      }else if( c=='\t' ){
1686        fputc('t', out);
1687      }else{
1688         raw_printf(out, "u%04x",c);
1689      }
1690    }else{
1691      fputc(c, out);
1692    }
1693  }
1694  fputc('"', out);
1695}
1696
1697/*
1698** Output the given string with characters that are special to
1699** HTML escaped.
1700*/
1701static void output_html_string(FILE *out, const char *z){
1702  int i;
1703  if( z==0 ) z = "";
1704  while( *z ){
1705    for(i=0;   z[i]
1706            && z[i]!='<'
1707            && z[i]!='&'
1708            && z[i]!='>'
1709            && z[i]!='\"'
1710            && z[i]!='\'';
1711        i++){}
1712    if( i>0 ){
1713      utf8_printf(out,"%.*s",i,z);
1714    }
1715    if( z[i]=='<' ){
1716      raw_printf(out,"&lt;");
1717    }else if( z[i]=='&' ){
1718      raw_printf(out,"&amp;");
1719    }else if( z[i]=='>' ){
1720      raw_printf(out,"&gt;");
1721    }else if( z[i]=='\"' ){
1722      raw_printf(out,"&quot;");
1723    }else if( z[i]=='\'' ){
1724      raw_printf(out,"&#39;");
1725    }else{
1726      break;
1727    }
1728    z += i + 1;
1729  }
1730}
1731
1732/*
1733** If a field contains any character identified by a 1 in the following
1734** array, then the string must be quoted for CSV.
1735*/
1736static const char needCsvQuote[] = {
1737  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1738  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1739  1, 0, 1, 0, 0, 0, 0, 1,   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, 0,
1744  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 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  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1753};
1754
1755/*
1756** Output a single term of CSV.  Actually, p->colSeparator is used for
1757** the separator, which may or may not be a comma.  p->nullValue is
1758** the null value.  Strings are quoted if necessary.  The separator
1759** is only issued if bSep is true.
1760*/
1761static void output_csv(ShellState *p, const char *z, int bSep){
1762  FILE *out = p->out;
1763  if( z==0 ){
1764    utf8_printf(out,"%s",p->nullValue);
1765  }else{
1766    int i;
1767    int nSep = strlen30(p->colSeparator);
1768    for(i=0; z[i]; i++){
1769      if( needCsvQuote[((unsigned char*)z)[i]]
1770         || (z[i]==p->colSeparator[0] &&
1771             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1772        i = 0;
1773        break;
1774      }
1775    }
1776    if( i==0 ){
1777      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1778      utf8_printf(out, "%s", zQuoted);
1779      sqlite3_free(zQuoted);
1780    }else{
1781      utf8_printf(out, "%s", z);
1782    }
1783  }
1784  if( bSep ){
1785    utf8_printf(p->out, "%s", p->colSeparator);
1786  }
1787}
1788
1789/*
1790** This routine runs when the user presses Ctrl-C
1791*/
1792static void interrupt_handler(int NotUsed){
1793  UNUSED_PARAMETER(NotUsed);
1794  seenInterrupt++;
1795  if( seenInterrupt>2 ) exit(1);
1796  if( globalDb ) sqlite3_interrupt(globalDb);
1797}
1798
1799#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1800/*
1801** This routine runs for console events (e.g. Ctrl-C) on Win32
1802*/
1803static BOOL WINAPI ConsoleCtrlHandler(
1804  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1805){
1806  if( dwCtrlType==CTRL_C_EVENT ){
1807    interrupt_handler(0);
1808    return TRUE;
1809  }
1810  return FALSE;
1811}
1812#endif
1813
1814#ifndef SQLITE_OMIT_AUTHORIZATION
1815/*
1816** This authorizer runs in safe mode.
1817*/
1818static int safeModeAuth(
1819  void *pClientData,
1820  int op,
1821  const char *zA1,
1822  const char *zA2,
1823  const char *zA3,
1824  const char *zA4
1825){
1826  ShellState *p = (ShellState*)pClientData;
1827  static const char *azProhibitedFunctions[] = {
1828    "edit",
1829    "fts3_tokenizer",
1830    "load_extension",
1831    "readfile",
1832    "writefile",
1833    "zipfile",
1834    "zipfile_cds",
1835  };
1836  UNUSED_PARAMETER(zA2);
1837  UNUSED_PARAMETER(zA3);
1838  UNUSED_PARAMETER(zA4);
1839  switch( op ){
1840    case SQLITE_ATTACH: {
1841      failIfSafeMode(p, "cannot run ATTACH in safe mode");
1842      break;
1843    }
1844    case SQLITE_FUNCTION: {
1845      int i;
1846      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
1847        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
1848          failIfSafeMode(p, "cannot use the %s() function in safe mode",
1849                         azProhibitedFunctions[i]);
1850        }
1851      }
1852      break;
1853    }
1854  }
1855  return SQLITE_OK;
1856}
1857
1858/*
1859** When the ".auth ON" is set, the following authorizer callback is
1860** invoked.  It always returns SQLITE_OK.
1861*/
1862static int shellAuth(
1863  void *pClientData,
1864  int op,
1865  const char *zA1,
1866  const char *zA2,
1867  const char *zA3,
1868  const char *zA4
1869){
1870  ShellState *p = (ShellState*)pClientData;
1871  static const char *azAction[] = { 0,
1872     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1873     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1874     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1875     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1876     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1877     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1878     "PRAGMA",               "READ",                 "SELECT",
1879     "TRANSACTION",          "UPDATE",               "ATTACH",
1880     "DETACH",               "ALTER_TABLE",          "REINDEX",
1881     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1882     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1883  };
1884  int i;
1885  const char *az[4];
1886  az[0] = zA1;
1887  az[1] = zA2;
1888  az[2] = zA3;
1889  az[3] = zA4;
1890  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1891  for(i=0; i<4; i++){
1892    raw_printf(p->out, " ");
1893    if( az[i] ){
1894      output_c_string(p->out, az[i]);
1895    }else{
1896      raw_printf(p->out, "NULL");
1897    }
1898  }
1899  raw_printf(p->out, "\n");
1900  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1901  return SQLITE_OK;
1902}
1903#endif
1904
1905/*
1906** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1907**
1908** This routine converts some CREATE TABLE statements for shadow tables
1909** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1910*/
1911static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1912  if( z==0 ) return;
1913  if( zTail==0 ) return;
1914  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1915    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1916  }else{
1917    utf8_printf(out, "%s%s", z, zTail);
1918  }
1919}
1920static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1921  char c = z[n];
1922  z[n] = 0;
1923  printSchemaLine(out, z, zTail);
1924  z[n] = c;
1925}
1926
1927/*
1928** Return true if string z[] has nothing but whitespace and comments to the
1929** end of the first line.
1930*/
1931static int wsToEol(const char *z){
1932  int i;
1933  for(i=0; z[i]; i++){
1934    if( z[i]=='\n' ) return 1;
1935    if( IsSpace(z[i]) ) continue;
1936    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1937    return 0;
1938  }
1939  return 1;
1940}
1941
1942/*
1943** Add a new entry to the EXPLAIN QUERY PLAN data
1944*/
1945static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1946  EQPGraphRow *pNew;
1947  int nText = strlen30(zText);
1948  if( p->autoEQPtest ){
1949    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1950  }
1951  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1952  if( pNew==0 ) shell_out_of_memory();
1953  pNew->iEqpId = iEqpId;
1954  pNew->iParentId = p2;
1955  memcpy(pNew->zText, zText, nText+1);
1956  pNew->pNext = 0;
1957  if( p->sGraph.pLast ){
1958    p->sGraph.pLast->pNext = pNew;
1959  }else{
1960    p->sGraph.pRow = pNew;
1961  }
1962  p->sGraph.pLast = pNew;
1963}
1964
1965/*
1966** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1967** in p->sGraph.
1968*/
1969static void eqp_reset(ShellState *p){
1970  EQPGraphRow *pRow, *pNext;
1971  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1972    pNext = pRow->pNext;
1973    sqlite3_free(pRow);
1974  }
1975  memset(&p->sGraph, 0, sizeof(p->sGraph));
1976}
1977
1978/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1979** pOld, or return the first such line if pOld is NULL
1980*/
1981static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1982  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1983  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1984  return pRow;
1985}
1986
1987/* Render a single level of the graph that has iEqpId as its parent.  Called
1988** recursively to render sublevels.
1989*/
1990static void eqp_render_level(ShellState *p, int iEqpId){
1991  EQPGraphRow *pRow, *pNext;
1992  int n = strlen30(p->sGraph.zPrefix);
1993  char *z;
1994  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1995    pNext = eqp_next_row(p, iEqpId, pRow);
1996    z = pRow->zText;
1997    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1998                pNext ? "|--" : "`--", z);
1999    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
2000      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
2001      eqp_render_level(p, pRow->iEqpId);
2002      p->sGraph.zPrefix[n] = 0;
2003    }
2004  }
2005}
2006
2007/*
2008** Display and reset the EXPLAIN QUERY PLAN data
2009*/
2010static void eqp_render(ShellState *p){
2011  EQPGraphRow *pRow = p->sGraph.pRow;
2012  if( pRow ){
2013    if( pRow->zText[0]=='-' ){
2014      if( pRow->pNext==0 ){
2015        eqp_reset(p);
2016        return;
2017      }
2018      utf8_printf(p->out, "%s\n", pRow->zText+3);
2019      p->sGraph.pRow = pRow->pNext;
2020      sqlite3_free(pRow);
2021    }else{
2022      utf8_printf(p->out, "QUERY PLAN\n");
2023    }
2024    p->sGraph.zPrefix[0] = 0;
2025    eqp_render_level(p, 0);
2026    eqp_reset(p);
2027  }
2028}
2029
2030#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2031/*
2032** Progress handler callback.
2033*/
2034static int progress_handler(void *pClientData) {
2035  ShellState *p = (ShellState*)pClientData;
2036  p->nProgress++;
2037  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2038    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2039    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2040    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2041    return 1;
2042  }
2043  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2044    raw_printf(p->out, "Progress %u\n", p->nProgress);
2045  }
2046  return 0;
2047}
2048#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2049
2050/*
2051** Print N dashes
2052*/
2053static void print_dashes(FILE *out, int N){
2054  const char zDash[] = "--------------------------------------------------";
2055  const int nDash = sizeof(zDash) - 1;
2056  while( N>nDash ){
2057    fputs(zDash, out);
2058    N -= nDash;
2059  }
2060  raw_printf(out, "%.*s", N, zDash);
2061}
2062
2063/*
2064** Print a markdown or table-style row separator using ascii-art
2065*/
2066static void print_row_separator(
2067  ShellState *p,
2068  int nArg,
2069  const char *zSep
2070){
2071  int i;
2072  if( nArg>0 ){
2073    fputs(zSep, p->out);
2074    print_dashes(p->out, p->actualWidth[0]+2);
2075    for(i=1; i<nArg; i++){
2076      fputs(zSep, p->out);
2077      print_dashes(p->out, p->actualWidth[i]+2);
2078    }
2079    fputs(zSep, p->out);
2080  }
2081  fputs("\n", p->out);
2082}
2083
2084/*
2085** This is the callback routine that the shell
2086** invokes for each row of a query result.
2087*/
2088static int shell_callback(
2089  void *pArg,
2090  int nArg,        /* Number of result columns */
2091  char **azArg,    /* Text of each result column */
2092  char **azCol,    /* Column names */
2093  int *aiType      /* Column types.  Might be NULL */
2094){
2095  int i;
2096  ShellState *p = (ShellState*)pArg;
2097
2098  if( azArg==0 ) return 0;
2099  switch( p->cMode ){
2100    case MODE_Line: {
2101      int w = 5;
2102      if( azArg==0 ) break;
2103      for(i=0; i<nArg; i++){
2104        int len = strlen30(azCol[i] ? azCol[i] : "");
2105        if( len>w ) w = len;
2106      }
2107      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2108      for(i=0; i<nArg; i++){
2109        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2110                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2111      }
2112      break;
2113    }
2114    case MODE_Explain: {
2115      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2116      if( nArg>ArraySize(aExplainWidth) ){
2117        nArg = ArraySize(aExplainWidth);
2118      }
2119      if( p->cnt++==0 ){
2120        for(i=0; i<nArg; i++){
2121          int w = aExplainWidth[i];
2122          utf8_width_print(p->out, w, azCol[i]);
2123          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2124        }
2125        for(i=0; i<nArg; i++){
2126          int w = aExplainWidth[i];
2127          print_dashes(p->out, w);
2128          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2129        }
2130      }
2131      if( azArg==0 ) break;
2132      for(i=0; i<nArg; i++){
2133        int w = aExplainWidth[i];
2134        if( i==nArg-1 ) w = 0;
2135        if( azArg[i] && strlenChar(azArg[i])>w ){
2136          w = strlenChar(azArg[i]);
2137        }
2138        if( i==1 && p->aiIndent && p->pStmt ){
2139          if( p->iIndent<p->nIndent ){
2140            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2141          }
2142          p->iIndent++;
2143        }
2144        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2145        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2146      }
2147      break;
2148    }
2149    case MODE_Semi: {   /* .schema and .fullschema output */
2150      printSchemaLine(p->out, azArg[0], ";\n");
2151      break;
2152    }
2153    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2154      char *z;
2155      int j;
2156      int nParen = 0;
2157      char cEnd = 0;
2158      char c;
2159      int nLine = 0;
2160      assert( nArg==1 );
2161      if( azArg[0]==0 ) break;
2162      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2163       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2164      ){
2165        utf8_printf(p->out, "%s;\n", azArg[0]);
2166        break;
2167      }
2168      z = sqlite3_mprintf("%s", azArg[0]);
2169      j = 0;
2170      for(i=0; IsSpace(z[i]); i++){}
2171      for(; (c = z[i])!=0; i++){
2172        if( IsSpace(c) ){
2173          if( z[j-1]=='\r' ) z[j-1] = '\n';
2174          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2175        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2176          j--;
2177        }
2178        z[j++] = c;
2179      }
2180      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2181      z[j] = 0;
2182      if( strlen30(z)>=79 ){
2183        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2184          if( c==cEnd ){
2185            cEnd = 0;
2186          }else if( c=='"' || c=='\'' || c=='`' ){
2187            cEnd = c;
2188          }else if( c=='[' ){
2189            cEnd = ']';
2190          }else if( c=='-' && z[i+1]=='-' ){
2191            cEnd = '\n';
2192          }else if( c=='(' ){
2193            nParen++;
2194          }else if( c==')' ){
2195            nParen--;
2196            if( nLine>0 && nParen==0 && j>0 ){
2197              printSchemaLineN(p->out, z, j, "\n");
2198              j = 0;
2199            }
2200          }
2201          z[j++] = c;
2202          if( nParen==1 && cEnd==0
2203           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2204          ){
2205            if( c=='\n' ) j--;
2206            printSchemaLineN(p->out, z, j, "\n  ");
2207            j = 0;
2208            nLine++;
2209            while( IsSpace(z[i+1]) ){ i++; }
2210          }
2211        }
2212        z[j] = 0;
2213      }
2214      printSchemaLine(p->out, z, ";\n");
2215      sqlite3_free(z);
2216      break;
2217    }
2218    case MODE_List: {
2219      if( p->cnt++==0 && p->showHeader ){
2220        for(i=0; i<nArg; i++){
2221          utf8_printf(p->out,"%s%s",azCol[i],
2222                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2223        }
2224      }
2225      if( azArg==0 ) break;
2226      for(i=0; i<nArg; i++){
2227        char *z = azArg[i];
2228        if( z==0 ) z = p->nullValue;
2229        utf8_printf(p->out, "%s", z);
2230        if( i<nArg-1 ){
2231          utf8_printf(p->out, "%s", p->colSeparator);
2232        }else{
2233          utf8_printf(p->out, "%s", p->rowSeparator);
2234        }
2235      }
2236      break;
2237    }
2238    case MODE_Html: {
2239      if( p->cnt++==0 && p->showHeader ){
2240        raw_printf(p->out,"<TR>");
2241        for(i=0; i<nArg; i++){
2242          raw_printf(p->out,"<TH>");
2243          output_html_string(p->out, azCol[i]);
2244          raw_printf(p->out,"</TH>\n");
2245        }
2246        raw_printf(p->out,"</TR>\n");
2247      }
2248      if( azArg==0 ) break;
2249      raw_printf(p->out,"<TR>");
2250      for(i=0; i<nArg; i++){
2251        raw_printf(p->out,"<TD>");
2252        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2253        raw_printf(p->out,"</TD>\n");
2254      }
2255      raw_printf(p->out,"</TR>\n");
2256      break;
2257    }
2258    case MODE_Tcl: {
2259      if( p->cnt++==0 && p->showHeader ){
2260        for(i=0; i<nArg; i++){
2261          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2262          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2263        }
2264        utf8_printf(p->out, "%s", p->rowSeparator);
2265      }
2266      if( azArg==0 ) break;
2267      for(i=0; i<nArg; i++){
2268        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2269        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2270      }
2271      utf8_printf(p->out, "%s", p->rowSeparator);
2272      break;
2273    }
2274    case MODE_Csv: {
2275      setBinaryMode(p->out, 1);
2276      if( p->cnt++==0 && p->showHeader ){
2277        for(i=0; i<nArg; i++){
2278          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2279        }
2280        utf8_printf(p->out, "%s", p->rowSeparator);
2281      }
2282      if( nArg>0 ){
2283        for(i=0; i<nArg; i++){
2284          output_csv(p, azArg[i], i<nArg-1);
2285        }
2286        utf8_printf(p->out, "%s", p->rowSeparator);
2287      }
2288      setTextMode(p->out, 1);
2289      break;
2290    }
2291    case MODE_Insert: {
2292      if( azArg==0 ) break;
2293      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2294      if( p->showHeader ){
2295        raw_printf(p->out,"(");
2296        for(i=0; i<nArg; i++){
2297          if( i>0 ) raw_printf(p->out, ",");
2298          if( quoteChar(azCol[i]) ){
2299            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2300            utf8_printf(p->out, "%s", z);
2301            sqlite3_free(z);
2302          }else{
2303            raw_printf(p->out, "%s", azCol[i]);
2304          }
2305        }
2306        raw_printf(p->out,")");
2307      }
2308      p->cnt++;
2309      for(i=0; i<nArg; i++){
2310        raw_printf(p->out, i>0 ? "," : " VALUES(");
2311        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2312          utf8_printf(p->out,"NULL");
2313        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2314          if( ShellHasFlag(p, SHFLG_Newlines) ){
2315            output_quoted_string(p->out, azArg[i]);
2316          }else{
2317            output_quoted_escaped_string(p->out, azArg[i]);
2318          }
2319        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2320          utf8_printf(p->out,"%s", azArg[i]);
2321        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2322          char z[50];
2323          double r = sqlite3_column_double(p->pStmt, i);
2324          sqlite3_uint64 ur;
2325          memcpy(&ur,&r,sizeof(r));
2326          if( ur==0x7ff0000000000000LL ){
2327            raw_printf(p->out, "1e999");
2328          }else if( ur==0xfff0000000000000LL ){
2329            raw_printf(p->out, "-1e999");
2330          }else{
2331            sqlite3_snprintf(50,z,"%!.20g", r);
2332            raw_printf(p->out, "%s", z);
2333          }
2334        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2335          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2336          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2337          output_hex_blob(p->out, pBlob, nBlob);
2338        }else if( isNumber(azArg[i], 0) ){
2339          utf8_printf(p->out,"%s", azArg[i]);
2340        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2341          output_quoted_string(p->out, azArg[i]);
2342        }else{
2343          output_quoted_escaped_string(p->out, azArg[i]);
2344        }
2345      }
2346      raw_printf(p->out,");\n");
2347      break;
2348    }
2349    case MODE_Json: {
2350      if( azArg==0 ) break;
2351      if( p->cnt==0 ){
2352        fputs("[{", p->out);
2353      }else{
2354        fputs(",\n{", p->out);
2355      }
2356      p->cnt++;
2357      for(i=0; i<nArg; i++){
2358        output_json_string(p->out, azCol[i], -1);
2359        putc(':', p->out);
2360        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2361          fputs("null",p->out);
2362        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2363          char z[50];
2364          double r = sqlite3_column_double(p->pStmt, i);
2365          sqlite3_uint64 ur;
2366          memcpy(&ur,&r,sizeof(r));
2367          if( ur==0x7ff0000000000000LL ){
2368            raw_printf(p->out, "1e999");
2369          }else if( ur==0xfff0000000000000LL ){
2370            raw_printf(p->out, "-1e999");
2371          }else{
2372            sqlite3_snprintf(50,z,"%!.20g", r);
2373            raw_printf(p->out, "%s", z);
2374          }
2375        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2376          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2377          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2378          output_json_string(p->out, pBlob, nBlob);
2379        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2380          output_json_string(p->out, azArg[i], -1);
2381        }else{
2382          utf8_printf(p->out,"%s", azArg[i]);
2383        }
2384        if( i<nArg-1 ){
2385          putc(',', p->out);
2386        }
2387      }
2388      putc('}', p->out);
2389      break;
2390    }
2391    case MODE_Quote: {
2392      if( azArg==0 ) break;
2393      if( p->cnt==0 && p->showHeader ){
2394        for(i=0; i<nArg; i++){
2395          if( i>0 ) fputs(p->colSeparator, p->out);
2396          output_quoted_string(p->out, azCol[i]);
2397        }
2398        fputs(p->rowSeparator, p->out);
2399      }
2400      p->cnt++;
2401      for(i=0; i<nArg; i++){
2402        if( i>0 ) fputs(p->colSeparator, p->out);
2403        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2404          utf8_printf(p->out,"NULL");
2405        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2406          output_quoted_string(p->out, azArg[i]);
2407        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2408          utf8_printf(p->out,"%s", azArg[i]);
2409        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2410          char z[50];
2411          double r = sqlite3_column_double(p->pStmt, i);
2412          sqlite3_snprintf(50,z,"%!.20g", r);
2413          raw_printf(p->out, "%s", z);
2414        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2415          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2416          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2417          output_hex_blob(p->out, pBlob, nBlob);
2418        }else if( isNumber(azArg[i], 0) ){
2419          utf8_printf(p->out,"%s", azArg[i]);
2420        }else{
2421          output_quoted_string(p->out, azArg[i]);
2422        }
2423      }
2424      fputs(p->rowSeparator, p->out);
2425      break;
2426    }
2427    case MODE_Ascii: {
2428      if( p->cnt++==0 && p->showHeader ){
2429        for(i=0; i<nArg; i++){
2430          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2431          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2432        }
2433        utf8_printf(p->out, "%s", p->rowSeparator);
2434      }
2435      if( azArg==0 ) break;
2436      for(i=0; i<nArg; i++){
2437        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2438        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2439      }
2440      utf8_printf(p->out, "%s", p->rowSeparator);
2441      break;
2442    }
2443    case MODE_EQP: {
2444      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2445      break;
2446    }
2447  }
2448  return 0;
2449}
2450
2451/*
2452** This is the callback routine that the SQLite library
2453** invokes for each row of a query result.
2454*/
2455static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2456  /* since we don't have type info, call the shell_callback with a NULL value */
2457  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2458}
2459
2460/*
2461** This is the callback routine from sqlite3_exec() that appends all
2462** output onto the end of a ShellText object.
2463*/
2464static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2465  ShellText *p = (ShellText*)pArg;
2466  int i;
2467  UNUSED_PARAMETER(az);
2468  if( azArg==0 ) return 0;
2469  if( p->n ) appendText(p, "|", 0);
2470  for(i=0; i<nArg; i++){
2471    if( i ) appendText(p, ",", 0);
2472    if( azArg[i] ) appendText(p, azArg[i], 0);
2473  }
2474  return 0;
2475}
2476
2477/*
2478** Generate an appropriate SELFTEST table in the main database.
2479*/
2480static void createSelftestTable(ShellState *p){
2481  char *zErrMsg = 0;
2482  sqlite3_exec(p->db,
2483    "SAVEPOINT selftest_init;\n"
2484    "CREATE TABLE IF NOT EXISTS selftest(\n"
2485    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2486    "  op TEXT,\n"                   /* Operator:  memo run */
2487    "  cmd TEXT,\n"                  /* Command text */
2488    "  ans TEXT\n"                   /* Desired answer */
2489    ");"
2490    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2491    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2492    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2493    "         'memo','Tests generated by --init');\n"
2494    "INSERT INTO [_shell$self]\n"
2495    "  SELECT 'run',\n"
2496    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2497                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2498    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2499                          "FROM sqlite_schema ORDER BY 2',224));\n"
2500    "INSERT INTO [_shell$self]\n"
2501    "  SELECT 'run',"
2502    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2503    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2504    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2505    "  FROM (\n"
2506    "    SELECT name FROM sqlite_schema\n"
2507    "     WHERE type='table'\n"
2508    "       AND name<>'selftest'\n"
2509    "       AND coalesce(rootpage,0)>0\n"
2510    "  )\n"
2511    " ORDER BY name;\n"
2512    "INSERT INTO [_shell$self]\n"
2513    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2514    "INSERT INTO selftest(tno,op,cmd,ans)"
2515    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2516    "DROP TABLE [_shell$self];"
2517    ,0,0,&zErrMsg);
2518  if( zErrMsg ){
2519    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2520    sqlite3_free(zErrMsg);
2521  }
2522  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2523}
2524
2525
2526/*
2527** Set the destination table field of the ShellState structure to
2528** the name of the table given.  Escape any quote characters in the
2529** table name.
2530*/
2531static void set_table_name(ShellState *p, const char *zName){
2532  int i, n;
2533  char cQuote;
2534  char *z;
2535
2536  if( p->zDestTable ){
2537    free(p->zDestTable);
2538    p->zDestTable = 0;
2539  }
2540  if( zName==0 ) return;
2541  cQuote = quoteChar(zName);
2542  n = strlen30(zName);
2543  if( cQuote ) n += n+2;
2544  z = p->zDestTable = malloc( n+1 );
2545  if( z==0 ) shell_out_of_memory();
2546  n = 0;
2547  if( cQuote ) z[n++] = cQuote;
2548  for(i=0; zName[i]; i++){
2549    z[n++] = zName[i];
2550    if( zName[i]==cQuote ) z[n++] = cQuote;
2551  }
2552  if( cQuote ) z[n++] = cQuote;
2553  z[n] = 0;
2554}
2555
2556
2557/*
2558** Execute a query statement that will generate SQL output.  Print
2559** the result columns, comma-separated, on a line and then add a
2560** semicolon terminator to the end of that line.
2561**
2562** If the number of columns is 1 and that column contains text "--"
2563** then write the semicolon on a separate line.  That way, if a
2564** "--" comment occurs at the end of the statement, the comment
2565** won't consume the semicolon terminator.
2566*/
2567static int run_table_dump_query(
2568  ShellState *p,           /* Query context */
2569  const char *zSelect      /* SELECT statement to extract content */
2570){
2571  sqlite3_stmt *pSelect;
2572  int rc;
2573  int nResult;
2574  int i;
2575  const char *z;
2576  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2577  if( rc!=SQLITE_OK || !pSelect ){
2578    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2579                sqlite3_errmsg(p->db));
2580    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2581    return rc;
2582  }
2583  rc = sqlite3_step(pSelect);
2584  nResult = sqlite3_column_count(pSelect);
2585  while( rc==SQLITE_ROW ){
2586    z = (const char*)sqlite3_column_text(pSelect, 0);
2587    utf8_printf(p->out, "%s", z);
2588    for(i=1; i<nResult; i++){
2589      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2590    }
2591    if( z==0 ) z = "";
2592    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2593    if( z[0] ){
2594      raw_printf(p->out, "\n;\n");
2595    }else{
2596      raw_printf(p->out, ";\n");
2597    }
2598    rc = sqlite3_step(pSelect);
2599  }
2600  rc = sqlite3_finalize(pSelect);
2601  if( rc!=SQLITE_OK ){
2602    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2603                sqlite3_errmsg(p->db));
2604    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2605  }
2606  return rc;
2607}
2608
2609/*
2610** Allocate space and save off current error string.
2611*/
2612static char *save_err_msg(
2613  sqlite3 *db            /* Database to query */
2614){
2615  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2616  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2617  if( zErrMsg ){
2618    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2619  }
2620  return zErrMsg;
2621}
2622
2623#ifdef __linux__
2624/*
2625** Attempt to display I/O stats on Linux using /proc/PID/io
2626*/
2627static void displayLinuxIoStats(FILE *out){
2628  FILE *in;
2629  char z[200];
2630  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2631  in = fopen(z, "rb");
2632  if( in==0 ) return;
2633  while( fgets(z, sizeof(z), in)!=0 ){
2634    static const struct {
2635      const char *zPattern;
2636      const char *zDesc;
2637    } aTrans[] = {
2638      { "rchar: ",                  "Bytes received by read():" },
2639      { "wchar: ",                  "Bytes sent to write():"    },
2640      { "syscr: ",                  "Read() system calls:"      },
2641      { "syscw: ",                  "Write() system calls:"     },
2642      { "read_bytes: ",             "Bytes read from storage:"  },
2643      { "write_bytes: ",            "Bytes written to storage:" },
2644      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2645    };
2646    int i;
2647    for(i=0; i<ArraySize(aTrans); i++){
2648      int n = strlen30(aTrans[i].zPattern);
2649      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2650        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2651        break;
2652      }
2653    }
2654  }
2655  fclose(in);
2656}
2657#endif
2658
2659/*
2660** Display a single line of status using 64-bit values.
2661*/
2662static void displayStatLine(
2663  ShellState *p,            /* The shell context */
2664  char *zLabel,             /* Label for this one line */
2665  char *zFormat,            /* Format for the result */
2666  int iStatusCtrl,          /* Which status to display */
2667  int bReset                /* True to reset the stats */
2668){
2669  sqlite3_int64 iCur = -1;
2670  sqlite3_int64 iHiwtr = -1;
2671  int i, nPercent;
2672  char zLine[200];
2673  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2674  for(i=0, nPercent=0; zFormat[i]; i++){
2675    if( zFormat[i]=='%' ) nPercent++;
2676  }
2677  if( nPercent>1 ){
2678    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2679  }else{
2680    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2681  }
2682  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2683}
2684
2685/*
2686** Display memory stats.
2687*/
2688static int display_stats(
2689  sqlite3 *db,                /* Database to query */
2690  ShellState *pArg,           /* Pointer to ShellState */
2691  int bReset                  /* True to reset the stats */
2692){
2693  int iCur;
2694  int iHiwtr;
2695  FILE *out;
2696  if( pArg==0 || pArg->out==0 ) return 0;
2697  out = pArg->out;
2698
2699  if( pArg->pStmt && pArg->statsOn==2 ){
2700    int nCol, i, x;
2701    sqlite3_stmt *pStmt = pArg->pStmt;
2702    char z[100];
2703    nCol = sqlite3_column_count(pStmt);
2704    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2705    for(i=0; i<nCol; i++){
2706      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2707      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2708#ifndef SQLITE_OMIT_DECLTYPE
2709      sqlite3_snprintf(30, z+x, "declared type:");
2710      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2711#endif
2712#ifdef SQLITE_ENABLE_COLUMN_METADATA
2713      sqlite3_snprintf(30, z+x, "database name:");
2714      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2715      sqlite3_snprintf(30, z+x, "table name:");
2716      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2717      sqlite3_snprintf(30, z+x, "origin name:");
2718      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2719#endif
2720    }
2721  }
2722
2723  if( pArg->statsOn==3 ){
2724    if( pArg->pStmt ){
2725      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2726      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2727    }
2728    return 0;
2729  }
2730
2731  displayStatLine(pArg, "Memory Used:",
2732     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2733  displayStatLine(pArg, "Number of Outstanding Allocations:",
2734     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2735  if( pArg->shellFlgs & SHFLG_Pagecache ){
2736    displayStatLine(pArg, "Number of Pcache Pages Used:",
2737       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2738  }
2739  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2740     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2741  displayStatLine(pArg, "Largest Allocation:",
2742     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2743  displayStatLine(pArg, "Largest Pcache Allocation:",
2744     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2745#ifdef YYTRACKMAXSTACKDEPTH
2746  displayStatLine(pArg, "Deepest Parser Stack:",
2747     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2748#endif
2749
2750  if( db ){
2751    if( pArg->shellFlgs & SHFLG_Lookaside ){
2752      iHiwtr = iCur = -1;
2753      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2754                        &iCur, &iHiwtr, bReset);
2755      raw_printf(pArg->out,
2756              "Lookaside Slots Used:                %d (max %d)\n",
2757              iCur, iHiwtr);
2758      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2759                        &iCur, &iHiwtr, bReset);
2760      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2761              iHiwtr);
2762      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2763                        &iCur, &iHiwtr, bReset);
2764      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2765              iHiwtr);
2766      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2767                        &iCur, &iHiwtr, bReset);
2768      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2769              iHiwtr);
2770    }
2771    iHiwtr = iCur = -1;
2772    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2773    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2774            iCur);
2775    iHiwtr = iCur = -1;
2776    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2777    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2778    iHiwtr = iCur = -1;
2779    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2780    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2781    iHiwtr = iCur = -1;
2782    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2783    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2784    iHiwtr = iCur = -1;
2785    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2786    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2787    iHiwtr = iCur = -1;
2788    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2789    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2790            iCur);
2791    iHiwtr = iCur = -1;
2792    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2793    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2794            iCur);
2795  }
2796
2797  if( pArg->pStmt ){
2798    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2799                               bReset);
2800    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2801    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2802    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2803    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2804    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2805    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2806    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2807    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2808    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2809    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2810    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2811    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2812    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2813  }
2814
2815#ifdef __linux__
2816  displayLinuxIoStats(pArg->out);
2817#endif
2818
2819  /* Do not remove this machine readable comment: extra-stats-output-here */
2820
2821  return 0;
2822}
2823
2824/*
2825** Display scan stats.
2826*/
2827static void display_scanstats(
2828  sqlite3 *db,                    /* Database to query */
2829  ShellState *pArg                /* Pointer to ShellState */
2830){
2831#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2832  UNUSED_PARAMETER(db);
2833  UNUSED_PARAMETER(pArg);
2834#else
2835  int i, k, n, mx;
2836  raw_printf(pArg->out, "-------- scanstats --------\n");
2837  mx = 0;
2838  for(k=0; k<=mx; k++){
2839    double rEstLoop = 1.0;
2840    for(i=n=0; 1; i++){
2841      sqlite3_stmt *p = pArg->pStmt;
2842      sqlite3_int64 nLoop, nVisit;
2843      double rEst;
2844      int iSid;
2845      const char *zExplain;
2846      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2847        break;
2848      }
2849      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2850      if( iSid>mx ) mx = iSid;
2851      if( iSid!=k ) continue;
2852      if( n==0 ){
2853        rEstLoop = (double)nLoop;
2854        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2855      }
2856      n++;
2857      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2858      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2859      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2860      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2861      rEstLoop *= rEst;
2862      raw_printf(pArg->out,
2863          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2864          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2865      );
2866    }
2867  }
2868  raw_printf(pArg->out, "---------------------------\n");
2869#endif
2870}
2871
2872/*
2873** Parameter azArray points to a zero-terminated array of strings. zStr
2874** points to a single nul-terminated string. Return non-zero if zStr
2875** is equal, according to strcmp(), to any of the strings in the array.
2876** Otherwise, return zero.
2877*/
2878static int str_in_array(const char *zStr, const char **azArray){
2879  int i;
2880  for(i=0; azArray[i]; i++){
2881    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2882  }
2883  return 0;
2884}
2885
2886/*
2887** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2888** and populate the ShellState.aiIndent[] array with the number of
2889** spaces each opcode should be indented before it is output.
2890**
2891** The indenting rules are:
2892**
2893**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2894**       all opcodes that occur between the p2 jump destination and the opcode
2895**       itself by 2 spaces.
2896**
2897**     * For each "Goto", if the jump destination is earlier in the program
2898**       and ends on one of:
2899**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2900**       or if the P1 parameter is one instead of zero,
2901**       then indent all opcodes between the earlier instruction
2902**       and "Goto" by 2 spaces.
2903*/
2904static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2905  const char *zSql;               /* The text of the SQL statement */
2906  const char *z;                  /* Used to check if this is an EXPLAIN */
2907  int *abYield = 0;               /* True if op is an OP_Yield */
2908  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2909  int iOp;                        /* Index of operation in p->aiIndent[] */
2910
2911  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2912  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2913                            "Rewind", 0 };
2914  const char *azGoto[] = { "Goto", 0 };
2915
2916  /* Try to figure out if this is really an EXPLAIN statement. If this
2917  ** cannot be verified, return early.  */
2918  if( sqlite3_column_count(pSql)!=8 ){
2919    p->cMode = p->mode;
2920    return;
2921  }
2922  zSql = sqlite3_sql(pSql);
2923  if( zSql==0 ) return;
2924  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2925  if( sqlite3_strnicmp(z, "explain", 7) ){
2926    p->cMode = p->mode;
2927    return;
2928  }
2929
2930  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2931    int i;
2932    int iAddr = sqlite3_column_int(pSql, 0);
2933    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2934
2935    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2936    ** p2 is an instruction address, set variable p2op to the index of that
2937    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2938    ** the current instruction is part of a sub-program generated by an
2939    ** SQL trigger or foreign key.  */
2940    int p2 = sqlite3_column_int(pSql, 3);
2941    int p2op = (p2 + (iOp-iAddr));
2942
2943    /* Grow the p->aiIndent array as required */
2944    if( iOp>=nAlloc ){
2945      if( iOp==0 ){
2946        /* Do further verfication that this is explain output.  Abort if
2947        ** it is not */
2948        static const char *explainCols[] = {
2949           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2950        int jj;
2951        for(jj=0; jj<ArraySize(explainCols); jj++){
2952          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2953            p->cMode = p->mode;
2954            sqlite3_reset(pSql);
2955            return;
2956          }
2957        }
2958      }
2959      nAlloc += 100;
2960      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2961      if( p->aiIndent==0 ) shell_out_of_memory();
2962      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2963      if( abYield==0 ) shell_out_of_memory();
2964    }
2965    abYield[iOp] = str_in_array(zOp, azYield);
2966    p->aiIndent[iOp] = 0;
2967    p->nIndent = iOp+1;
2968
2969    if( str_in_array(zOp, azNext) ){
2970      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2971    }
2972    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2973     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2974    ){
2975      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2976    }
2977  }
2978
2979  p->iIndent = 0;
2980  sqlite3_free(abYield);
2981  sqlite3_reset(pSql);
2982}
2983
2984/*
2985** Free the array allocated by explain_data_prepare().
2986*/
2987static void explain_data_delete(ShellState *p){
2988  sqlite3_free(p->aiIndent);
2989  p->aiIndent = 0;
2990  p->nIndent = 0;
2991  p->iIndent = 0;
2992}
2993
2994/*
2995** Disable and restore .wheretrace and .selecttrace settings.
2996*/
2997static unsigned int savedSelectTrace;
2998static unsigned int savedWhereTrace;
2999static void disable_debug_trace_modes(void){
3000  unsigned int zero = 0;
3001  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3002  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3003  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3004  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3005}
3006static void restore_debug_trace_modes(void){
3007  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3008  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3009}
3010
3011/* Create the TEMP table used to store parameter bindings */
3012static void bind_table_init(ShellState *p){
3013  int wrSchema = 0;
3014  int defensiveMode = 0;
3015  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3016  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3017  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3018  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3019  sqlite3_exec(p->db,
3020    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3021    "  key TEXT PRIMARY KEY,\n"
3022    "  value\n"
3023    ") WITHOUT ROWID;",
3024    0, 0, 0);
3025  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3026  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3027}
3028
3029/*
3030** Bind parameters on a prepared statement.
3031**
3032** Parameter bindings are taken from a TEMP table of the form:
3033**
3034**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3035**    WITHOUT ROWID;
3036**
3037** No bindings occur if this table does not exist.  The name of the table
3038** begins with "sqlite_" so that it will not collide with ordinary application
3039** tables.  The table must be in the TEMP schema.
3040*/
3041static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3042  int nVar;
3043  int i;
3044  int rc;
3045  sqlite3_stmt *pQ = 0;
3046
3047  nVar = sqlite3_bind_parameter_count(pStmt);
3048  if( nVar==0 ) return;  /* Nothing to do */
3049  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3050                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3051    return; /* Parameter table does not exist */
3052  }
3053  rc = sqlite3_prepare_v2(pArg->db,
3054          "SELECT value FROM temp.sqlite_parameters"
3055          " WHERE key=?1", -1, &pQ, 0);
3056  if( rc || pQ==0 ) return;
3057  for(i=1; i<=nVar; i++){
3058    char zNum[30];
3059    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3060    if( zVar==0 ){
3061      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3062      zVar = zNum;
3063    }
3064    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3065    if( sqlite3_step(pQ)==SQLITE_ROW ){
3066      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3067    }else{
3068      sqlite3_bind_null(pStmt, i);
3069    }
3070    sqlite3_reset(pQ);
3071  }
3072  sqlite3_finalize(pQ);
3073}
3074
3075/*
3076** UTF8 box-drawing characters.  Imagine box lines like this:
3077**
3078**           1
3079**           |
3080**       4 --+-- 2
3081**           |
3082**           3
3083**
3084** Each box characters has between 2 and 4 of the lines leading from
3085** the center.  The characters are here identified by the numbers of
3086** their corresponding lines.
3087*/
3088#define BOX_24   "\342\224\200"  /* U+2500 --- */
3089#define BOX_13   "\342\224\202"  /* U+2502  |  */
3090#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3091#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3092#define BOX_12   "\342\224\224"  /* U+2514  '- */
3093#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3094#define BOX_123  "\342\224\234"  /* U+251c  |- */
3095#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3096#define BOX_234  "\342\224\254"  /* U+252c -,- */
3097#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3098#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3099
3100/* Draw horizontal line N characters long using unicode box
3101** characters
3102*/
3103static void print_box_line(FILE *out, int N){
3104  const char zDash[] =
3105      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3106      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3107  const int nDash = sizeof(zDash) - 1;
3108  N *= 3;
3109  while( N>nDash ){
3110    utf8_printf(out, zDash);
3111    N -= nDash;
3112  }
3113  utf8_printf(out, "%.*s", N, zDash);
3114}
3115
3116/*
3117** Draw a horizontal separator for a MODE_Box table.
3118*/
3119static void print_box_row_separator(
3120  ShellState *p,
3121  int nArg,
3122  const char *zSep1,
3123  const char *zSep2,
3124  const char *zSep3
3125){
3126  int i;
3127  if( nArg>0 ){
3128    utf8_printf(p->out, "%s", zSep1);
3129    print_box_line(p->out, p->actualWidth[0]+2);
3130    for(i=1; i<nArg; i++){
3131      utf8_printf(p->out, "%s", zSep2);
3132      print_box_line(p->out, p->actualWidth[i]+2);
3133    }
3134    utf8_printf(p->out, "%s", zSep3);
3135  }
3136  fputs("\n", p->out);
3137}
3138
3139
3140
3141/*
3142** Run a prepared statement and output the result in one of the
3143** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3144** or MODE_Box.
3145**
3146** This is different from ordinary exec_prepared_stmt() in that
3147** it has to run the entire query and gather the results into memory
3148** first, in order to determine column widths, before providing
3149** any output.
3150*/
3151static void exec_prepared_stmt_columnar(
3152  ShellState *p,                        /* Pointer to ShellState */
3153  sqlite3_stmt *pStmt                   /* Statment to run */
3154){
3155  sqlite3_int64 nRow = 0;
3156  int nColumn = 0;
3157  char **azData = 0;
3158  sqlite3_int64 nAlloc = 0;
3159  const char *z;
3160  int rc;
3161  sqlite3_int64 i, nData;
3162  int j, nTotal, w, n;
3163  const char *colSep = 0;
3164  const char *rowSep = 0;
3165
3166  rc = sqlite3_step(pStmt);
3167  if( rc!=SQLITE_ROW ) return;
3168  nColumn = sqlite3_column_count(pStmt);
3169  nAlloc = nColumn*4;
3170  if( nAlloc<=0 ) nAlloc = 1;
3171  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3172  if( azData==0 ) shell_out_of_memory();
3173  for(i=0; i<nColumn; i++){
3174    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3175  }
3176  do{
3177    if( (nRow+2)*nColumn >= nAlloc ){
3178      nAlloc *= 2;
3179      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3180      if( azData==0 ) shell_out_of_memory();
3181    }
3182    nRow++;
3183    for(i=0; i<nColumn; i++){
3184      z = (const char*)sqlite3_column_text(pStmt,i);
3185      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3186    }
3187  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
3188  if( nColumn>p->nWidth ){
3189    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3190    if( p->colWidth==0 ) shell_out_of_memory();
3191    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3192    p->nWidth = nColumn;
3193    p->actualWidth = &p->colWidth[nColumn];
3194  }
3195  memset(p->actualWidth, 0, nColumn*sizeof(int));
3196  for(i=0; i<nColumn; i++){
3197    w = p->colWidth[i];
3198    if( w<0 ) w = -w;
3199    p->actualWidth[i] = w;
3200  }
3201  nTotal = nColumn*(nRow+1);
3202  for(i=0; i<nTotal; i++){
3203    z = azData[i];
3204    if( z==0 ) z = p->nullValue;
3205    n = strlenChar(z);
3206    j = i%nColumn;
3207    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3208  }
3209  if( seenInterrupt ) goto columnar_end;
3210  if( nColumn==0 ) goto columnar_end;
3211  switch( p->cMode ){
3212    case MODE_Column: {
3213      colSep = "  ";
3214      rowSep = "\n";
3215      if( p->showHeader ){
3216        for(i=0; i<nColumn; i++){
3217          w = p->actualWidth[i];
3218          if( p->colWidth[i]<0 ) w = -w;
3219          utf8_width_print(p->out, w, azData[i]);
3220          fputs(i==nColumn-1?"\n":"  ", p->out);
3221        }
3222        for(i=0; i<nColumn; i++){
3223          print_dashes(p->out, p->actualWidth[i]);
3224          fputs(i==nColumn-1?"\n":"  ", p->out);
3225        }
3226      }
3227      break;
3228    }
3229    case MODE_Table: {
3230      colSep = " | ";
3231      rowSep = " |\n";
3232      print_row_separator(p, nColumn, "+");
3233      fputs("| ", p->out);
3234      for(i=0; i<nColumn; i++){
3235        w = p->actualWidth[i];
3236        n = strlenChar(azData[i]);
3237        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3238        fputs(i==nColumn-1?" |\n":" | ", p->out);
3239      }
3240      print_row_separator(p, nColumn, "+");
3241      break;
3242    }
3243    case MODE_Markdown: {
3244      colSep = " | ";
3245      rowSep = " |\n";
3246      fputs("| ", p->out);
3247      for(i=0; i<nColumn; i++){
3248        w = p->actualWidth[i];
3249        n = strlenChar(azData[i]);
3250        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3251        fputs(i==nColumn-1?" |\n":" | ", p->out);
3252      }
3253      print_row_separator(p, nColumn, "|");
3254      break;
3255    }
3256    case MODE_Box: {
3257      colSep = " " BOX_13 " ";
3258      rowSep = " " BOX_13 "\n";
3259      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3260      utf8_printf(p->out, BOX_13 " ");
3261      for(i=0; i<nColumn; i++){
3262        w = p->actualWidth[i];
3263        n = strlenChar(azData[i]);
3264        utf8_printf(p->out, "%*s%s%*s%s",
3265            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3266            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3267      }
3268      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3269      break;
3270    }
3271  }
3272  for(i=nColumn, j=0; i<nTotal; i++, j++){
3273    if( j==0 && p->cMode!=MODE_Column ){
3274      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3275    }
3276    z = azData[i];
3277    if( z==0 ) z = p->nullValue;
3278    w = p->actualWidth[j];
3279    if( p->colWidth[j]<0 ) w = -w;
3280    utf8_width_print(p->out, w, z);
3281    if( j==nColumn-1 ){
3282      utf8_printf(p->out, "%s", rowSep);
3283      j = -1;
3284      if( seenInterrupt ) goto columnar_end;
3285    }else{
3286      utf8_printf(p->out, "%s", colSep);
3287    }
3288  }
3289  if( p->cMode==MODE_Table ){
3290    print_row_separator(p, nColumn, "+");
3291  }else if( p->cMode==MODE_Box ){
3292    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3293  }
3294columnar_end:
3295  if( seenInterrupt ){
3296    utf8_printf(p->out, "Interrupt\n");
3297  }
3298  nData = (nRow+1)*nColumn;
3299  for(i=0; i<nData; i++) free(azData[i]);
3300  sqlite3_free(azData);
3301}
3302
3303/*
3304** Run a prepared statement
3305*/
3306static void exec_prepared_stmt(
3307  ShellState *pArg,                                /* Pointer to ShellState */
3308  sqlite3_stmt *pStmt                              /* Statment to run */
3309){
3310  int rc;
3311
3312  if( pArg->cMode==MODE_Column
3313   || pArg->cMode==MODE_Table
3314   || pArg->cMode==MODE_Box
3315   || pArg->cMode==MODE_Markdown
3316  ){
3317    exec_prepared_stmt_columnar(pArg, pStmt);
3318    return;
3319  }
3320
3321  /* perform the first step.  this will tell us if we
3322  ** have a result set or not and how wide it is.
3323  */
3324  rc = sqlite3_step(pStmt);
3325  /* if we have a result set... */
3326  if( SQLITE_ROW == rc ){
3327    /* allocate space for col name ptr, value ptr, and type */
3328    int nCol = sqlite3_column_count(pStmt);
3329    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3330    if( !pData ){
3331      rc = SQLITE_NOMEM;
3332    }else{
3333      char **azCols = (char **)pData;      /* Names of result columns */
3334      char **azVals = &azCols[nCol];       /* Results */
3335      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3336      int i, x;
3337      assert(sizeof(int) <= sizeof(char *));
3338      /* save off ptrs to column names */
3339      for(i=0; i<nCol; i++){
3340        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3341      }
3342      do{
3343        /* extract the data and data types */
3344        for(i=0; i<nCol; i++){
3345          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3346          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3347            azVals[i] = "";
3348          }else{
3349            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3350          }
3351          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3352            rc = SQLITE_NOMEM;
3353            break; /* from for */
3354          }
3355        } /* end for */
3356
3357        /* if data and types extracted successfully... */
3358        if( SQLITE_ROW == rc ){
3359          /* call the supplied callback with the result row data */
3360          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3361            rc = SQLITE_ABORT;
3362          }else{
3363            rc = sqlite3_step(pStmt);
3364          }
3365        }
3366      } while( SQLITE_ROW == rc );
3367      sqlite3_free(pData);
3368      if( pArg->cMode==MODE_Json ){
3369        fputs("]\n", pArg->out);
3370      }
3371    }
3372  }
3373}
3374
3375#ifndef SQLITE_OMIT_VIRTUALTABLE
3376/*
3377** This function is called to process SQL if the previous shell command
3378** was ".expert". It passes the SQL in the second argument directly to
3379** the sqlite3expert object.
3380**
3381** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3382** code. In this case, (*pzErr) may be set to point to a buffer containing
3383** an English language error message. It is the responsibility of the
3384** caller to eventually free this buffer using sqlite3_free().
3385*/
3386static int expertHandleSQL(
3387  ShellState *pState,
3388  const char *zSql,
3389  char **pzErr
3390){
3391  assert( pState->expert.pExpert );
3392  assert( pzErr==0 || *pzErr==0 );
3393  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3394}
3395
3396/*
3397** This function is called either to silently clean up the object
3398** created by the ".expert" command (if bCancel==1), or to generate a
3399** report from it and then clean it up (if bCancel==0).
3400**
3401** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3402** code. In this case, (*pzErr) may be set to point to a buffer containing
3403** an English language error message. It is the responsibility of the
3404** caller to eventually free this buffer using sqlite3_free().
3405*/
3406static int expertFinish(
3407  ShellState *pState,
3408  int bCancel,
3409  char **pzErr
3410){
3411  int rc = SQLITE_OK;
3412  sqlite3expert *p = pState->expert.pExpert;
3413  assert( p );
3414  assert( bCancel || pzErr==0 || *pzErr==0 );
3415  if( bCancel==0 ){
3416    FILE *out = pState->out;
3417    int bVerbose = pState->expert.bVerbose;
3418
3419    rc = sqlite3_expert_analyze(p, pzErr);
3420    if( rc==SQLITE_OK ){
3421      int nQuery = sqlite3_expert_count(p);
3422      int i;
3423
3424      if( bVerbose ){
3425        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3426        raw_printf(out, "-- Candidates -----------------------------\n");
3427        raw_printf(out, "%s\n", zCand);
3428      }
3429      for(i=0; i<nQuery; i++){
3430        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3431        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3432        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3433        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3434        if( bVerbose ){
3435          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3436          raw_printf(out, "%s\n\n", zSql);
3437        }
3438        raw_printf(out, "%s\n", zIdx);
3439        raw_printf(out, "%s\n", zEQP);
3440      }
3441    }
3442  }
3443  sqlite3_expert_destroy(p);
3444  pState->expert.pExpert = 0;
3445  return rc;
3446}
3447
3448/*
3449** Implementation of ".expert" dot command.
3450*/
3451static int expertDotCommand(
3452  ShellState *pState,             /* Current shell tool state */
3453  char **azArg,                   /* Array of arguments passed to dot command */
3454  int nArg                        /* Number of entries in azArg[] */
3455){
3456  int rc = SQLITE_OK;
3457  char *zErr = 0;
3458  int i;
3459  int iSample = 0;
3460
3461  assert( pState->expert.pExpert==0 );
3462  memset(&pState->expert, 0, sizeof(ExpertInfo));
3463
3464  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3465    char *z = azArg[i];
3466    int n;
3467    if( z[0]=='-' && z[1]=='-' ) z++;
3468    n = strlen30(z);
3469    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3470      pState->expert.bVerbose = 1;
3471    }
3472    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3473      if( i==(nArg-1) ){
3474        raw_printf(stderr, "option requires an argument: %s\n", z);
3475        rc = SQLITE_ERROR;
3476      }else{
3477        iSample = (int)integerValue(azArg[++i]);
3478        if( iSample<0 || iSample>100 ){
3479          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3480          rc = SQLITE_ERROR;
3481        }
3482      }
3483    }
3484    else{
3485      raw_printf(stderr, "unknown option: %s\n", z);
3486      rc = SQLITE_ERROR;
3487    }
3488  }
3489
3490  if( rc==SQLITE_OK ){
3491    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3492    if( pState->expert.pExpert==0 ){
3493      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3494      rc = SQLITE_ERROR;
3495    }else{
3496      sqlite3_expert_config(
3497          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3498      );
3499    }
3500  }
3501
3502  return rc;
3503}
3504#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3505
3506/*
3507** Execute a statement or set of statements.  Print
3508** any result rows/columns depending on the current mode
3509** set via the supplied callback.
3510**
3511** This is very similar to SQLite's built-in sqlite3_exec()
3512** function except it takes a slightly different callback
3513** and callback data argument.
3514*/
3515static int shell_exec(
3516  ShellState *pArg,                         /* Pointer to ShellState */
3517  const char *zSql,                         /* SQL to be evaluated */
3518  char **pzErrMsg                           /* Error msg written here */
3519){
3520  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3521  int rc = SQLITE_OK;             /* Return Code */
3522  int rc2;
3523  const char *zLeftover;          /* Tail of unprocessed SQL */
3524  sqlite3 *db = pArg->db;
3525
3526  if( pzErrMsg ){
3527    *pzErrMsg = NULL;
3528  }
3529
3530#ifndef SQLITE_OMIT_VIRTUALTABLE
3531  if( pArg->expert.pExpert ){
3532    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3533    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3534  }
3535#endif
3536
3537  while( zSql[0] && (SQLITE_OK == rc) ){
3538    static const char *zStmtSql;
3539    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3540    if( SQLITE_OK != rc ){
3541      if( pzErrMsg ){
3542        *pzErrMsg = save_err_msg(db);
3543      }
3544    }else{
3545      if( !pStmt ){
3546        /* this happens for a comment or white-space */
3547        zSql = zLeftover;
3548        while( IsSpace(zSql[0]) ) zSql++;
3549        continue;
3550      }
3551      zStmtSql = sqlite3_sql(pStmt);
3552      if( zStmtSql==0 ) zStmtSql = "";
3553      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3554
3555      /* save off the prepared statment handle and reset row count */
3556      if( pArg ){
3557        pArg->pStmt = pStmt;
3558        pArg->cnt = 0;
3559      }
3560
3561      /* echo the sql statement if echo on */
3562      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3563        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3564      }
3565
3566      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3567      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3568        sqlite3_stmt *pExplain;
3569        char *zEQP;
3570        int triggerEQP = 0;
3571        disable_debug_trace_modes();
3572        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3573        if( pArg->autoEQP>=AUTOEQP_trigger ){
3574          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3575        }
3576        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3577        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3578        if( rc==SQLITE_OK ){
3579          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3580            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3581            int iEqpId = sqlite3_column_int(pExplain, 0);
3582            int iParentId = sqlite3_column_int(pExplain, 1);
3583            if( zEQPLine==0 ) zEQPLine = "";
3584            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3585            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3586          }
3587          eqp_render(pArg);
3588        }
3589        sqlite3_finalize(pExplain);
3590        sqlite3_free(zEQP);
3591        if( pArg->autoEQP>=AUTOEQP_full ){
3592          /* Also do an EXPLAIN for ".eqp full" mode */
3593          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3594          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3595          if( rc==SQLITE_OK ){
3596            pArg->cMode = MODE_Explain;
3597            explain_data_prepare(pArg, pExplain);
3598            exec_prepared_stmt(pArg, pExplain);
3599            explain_data_delete(pArg);
3600          }
3601          sqlite3_finalize(pExplain);
3602          sqlite3_free(zEQP);
3603        }
3604        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3605          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3606          /* Reprepare pStmt before reactiving trace modes */
3607          sqlite3_finalize(pStmt);
3608          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3609          if( pArg ) pArg->pStmt = pStmt;
3610        }
3611        restore_debug_trace_modes();
3612      }
3613
3614      if( pArg ){
3615        pArg->cMode = pArg->mode;
3616        if( pArg->autoExplain ){
3617          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3618            pArg->cMode = MODE_Explain;
3619          }
3620          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3621            pArg->cMode = MODE_EQP;
3622          }
3623        }
3624
3625        /* If the shell is currently in ".explain" mode, gather the extra
3626        ** data required to add indents to the output.*/
3627        if( pArg->cMode==MODE_Explain ){
3628          explain_data_prepare(pArg, pStmt);
3629        }
3630      }
3631
3632      bind_prepared_stmt(pArg, pStmt);
3633      exec_prepared_stmt(pArg, pStmt);
3634      explain_data_delete(pArg);
3635      eqp_render(pArg);
3636
3637      /* print usage stats if stats on */
3638      if( pArg && pArg->statsOn ){
3639        display_stats(db, pArg, 0);
3640      }
3641
3642      /* print loop-counters if required */
3643      if( pArg && pArg->scanstatsOn ){
3644        display_scanstats(db, pArg);
3645      }
3646
3647      /* Finalize the statement just executed. If this fails, save a
3648      ** copy of the error message. Otherwise, set zSql to point to the
3649      ** next statement to execute. */
3650      rc2 = sqlite3_finalize(pStmt);
3651      if( rc!=SQLITE_NOMEM ) rc = rc2;
3652      if( rc==SQLITE_OK ){
3653        zSql = zLeftover;
3654        while( IsSpace(zSql[0]) ) zSql++;
3655      }else if( pzErrMsg ){
3656        *pzErrMsg = save_err_msg(db);
3657      }
3658
3659      /* clear saved stmt handle */
3660      if( pArg ){
3661        pArg->pStmt = NULL;
3662      }
3663    }
3664  } /* end while */
3665
3666  return rc;
3667}
3668
3669/*
3670** Release memory previously allocated by tableColumnList().
3671*/
3672static void freeColumnList(char **azCol){
3673  int i;
3674  for(i=1; azCol[i]; i++){
3675    sqlite3_free(azCol[i]);
3676  }
3677  /* azCol[0] is a static string */
3678  sqlite3_free(azCol);
3679}
3680
3681/*
3682** Return a list of pointers to strings which are the names of all
3683** columns in table zTab.   The memory to hold the names is dynamically
3684** allocated and must be released by the caller using a subsequent call
3685** to freeColumnList().
3686**
3687** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3688** value that needs to be preserved, then azCol[0] is filled in with the
3689** name of the rowid column.
3690**
3691** The first regular column in the table is azCol[1].  The list is terminated
3692** by an entry with azCol[i]==0.
3693*/
3694static char **tableColumnList(ShellState *p, const char *zTab){
3695  char **azCol = 0;
3696  sqlite3_stmt *pStmt;
3697  char *zSql;
3698  int nCol = 0;
3699  int nAlloc = 0;
3700  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3701  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3702  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3703  int rc;
3704
3705  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3706  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3707  sqlite3_free(zSql);
3708  if( rc ) return 0;
3709  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3710    if( nCol>=nAlloc-2 ){
3711      nAlloc = nAlloc*2 + nCol + 10;
3712      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3713      if( azCol==0 ) shell_out_of_memory();
3714    }
3715    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3716    if( sqlite3_column_int(pStmt, 5) ){
3717      nPK++;
3718      if( nPK==1
3719       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3720                          "INTEGER")==0
3721      ){
3722        isIPK = 1;
3723      }else{
3724        isIPK = 0;
3725      }
3726    }
3727  }
3728  sqlite3_finalize(pStmt);
3729  if( azCol==0 ) return 0;
3730  azCol[0] = 0;
3731  azCol[nCol+1] = 0;
3732
3733  /* The decision of whether or not a rowid really needs to be preserved
3734  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3735  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3736  ** rowids on tables where the rowid is inaccessible because there are other
3737  ** columns in the table named "rowid", "_rowid_", and "oid".
3738  */
3739  if( preserveRowid && isIPK ){
3740    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3741    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3742    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3743    ** ROWID aliases.  To distinguish these cases, check to see if
3744    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3745    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3746    */
3747    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3748                           " WHERE origin='pk'", zTab);
3749    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3750    sqlite3_free(zSql);
3751    if( rc ){
3752      freeColumnList(azCol);
3753      return 0;
3754    }
3755    rc = sqlite3_step(pStmt);
3756    sqlite3_finalize(pStmt);
3757    preserveRowid = rc==SQLITE_ROW;
3758  }
3759  if( preserveRowid ){
3760    /* Only preserve the rowid if we can find a name to use for the
3761    ** rowid */
3762    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3763    int i, j;
3764    for(j=0; j<3; j++){
3765      for(i=1; i<=nCol; i++){
3766        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3767      }
3768      if( i>nCol ){
3769        /* At this point, we know that azRowid[j] is not the name of any
3770        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3771        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3772        ** tables will fail this last check */
3773        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3774        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3775        break;
3776      }
3777    }
3778  }
3779  return azCol;
3780}
3781
3782/*
3783** Toggle the reverse_unordered_selects setting.
3784*/
3785static void toggleSelectOrder(sqlite3 *db){
3786  sqlite3_stmt *pStmt = 0;
3787  int iSetting = 0;
3788  char zStmt[100];
3789  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3790  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3791    iSetting = sqlite3_column_int(pStmt, 0);
3792  }
3793  sqlite3_finalize(pStmt);
3794  sqlite3_snprintf(sizeof(zStmt), zStmt,
3795       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3796  sqlite3_exec(db, zStmt, 0, 0, 0);
3797}
3798
3799/*
3800** This is a different callback routine used for dumping the database.
3801** Each row received by this callback consists of a table name,
3802** the table type ("index" or "table") and SQL to create the table.
3803** This routine should print text sufficient to recreate the table.
3804*/
3805static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3806  int rc;
3807  const char *zTable;
3808  const char *zType;
3809  const char *zSql;
3810  ShellState *p = (ShellState *)pArg;
3811  int dataOnly;
3812  int noSys;
3813
3814  UNUSED_PARAMETER(azNotUsed);
3815  if( nArg!=3 || azArg==0 ) return 0;
3816  zTable = azArg[0];
3817  zType = azArg[1];
3818  zSql = azArg[2];
3819  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
3820  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
3821
3822  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
3823    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3824  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
3825    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3826  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3827    return 0;
3828  }else if( dataOnly ){
3829    /* no-op */
3830  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3831    char *zIns;
3832    if( !p->writableSchema ){
3833      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3834      p->writableSchema = 1;
3835    }
3836    zIns = sqlite3_mprintf(
3837       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3838       "VALUES('table','%q','%q',0,'%q');",
3839       zTable, zTable, zSql);
3840    utf8_printf(p->out, "%s\n", zIns);
3841    sqlite3_free(zIns);
3842    return 0;
3843  }else{
3844    printSchemaLine(p->out, zSql, ";\n");
3845  }
3846
3847  if( strcmp(zType, "table")==0 ){
3848    ShellText sSelect;
3849    ShellText sTable;
3850    char **azCol;
3851    int i;
3852    char *savedDestTable;
3853    int savedMode;
3854
3855    azCol = tableColumnList(p, zTable);
3856    if( azCol==0 ){
3857      p->nErr++;
3858      return 0;
3859    }
3860
3861    /* Always quote the table name, even if it appears to be pure ascii,
3862    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3863    initText(&sTable);
3864    appendText(&sTable, zTable, quoteChar(zTable));
3865    /* If preserving the rowid, add a column list after the table name.
3866    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3867    ** instead of the usual "INSERT INTO tab VALUES(...)".
3868    */
3869    if( azCol[0] ){
3870      appendText(&sTable, "(", 0);
3871      appendText(&sTable, azCol[0], 0);
3872      for(i=1; azCol[i]; i++){
3873        appendText(&sTable, ",", 0);
3874        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3875      }
3876      appendText(&sTable, ")", 0);
3877    }
3878
3879    /* Build an appropriate SELECT statement */
3880    initText(&sSelect);
3881    appendText(&sSelect, "SELECT ", 0);
3882    if( azCol[0] ){
3883      appendText(&sSelect, azCol[0], 0);
3884      appendText(&sSelect, ",", 0);
3885    }
3886    for(i=1; azCol[i]; i++){
3887      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3888      if( azCol[i+1] ){
3889        appendText(&sSelect, ",", 0);
3890      }
3891    }
3892    freeColumnList(azCol);
3893    appendText(&sSelect, " FROM ", 0);
3894    appendText(&sSelect, zTable, quoteChar(zTable));
3895
3896    savedDestTable = p->zDestTable;
3897    savedMode = p->mode;
3898    p->zDestTable = sTable.z;
3899    p->mode = p->cMode = MODE_Insert;
3900    rc = shell_exec(p, sSelect.z, 0);
3901    if( (rc&0xff)==SQLITE_CORRUPT ){
3902      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3903      toggleSelectOrder(p->db);
3904      shell_exec(p, sSelect.z, 0);
3905      toggleSelectOrder(p->db);
3906    }
3907    p->zDestTable = savedDestTable;
3908    p->mode = savedMode;
3909    freeText(&sTable);
3910    freeText(&sSelect);
3911    if( rc ) p->nErr++;
3912  }
3913  return 0;
3914}
3915
3916/*
3917** Run zQuery.  Use dump_callback() as the callback routine so that
3918** the contents of the query are output as SQL statements.
3919**
3920** If we get a SQLITE_CORRUPT error, rerun the query after appending
3921** "ORDER BY rowid DESC" to the end.
3922*/
3923static int run_schema_dump_query(
3924  ShellState *p,
3925  const char *zQuery
3926){
3927  int rc;
3928  char *zErr = 0;
3929  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3930  if( rc==SQLITE_CORRUPT ){
3931    char *zQ2;
3932    int len = strlen30(zQuery);
3933    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3934    if( zErr ){
3935      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3936      sqlite3_free(zErr);
3937      zErr = 0;
3938    }
3939    zQ2 = malloc( len+100 );
3940    if( zQ2==0 ) return rc;
3941    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3942    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3943    if( rc ){
3944      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3945    }else{
3946      rc = SQLITE_CORRUPT;
3947    }
3948    sqlite3_free(zErr);
3949    free(zQ2);
3950  }
3951  return rc;
3952}
3953
3954/*
3955** Text of help messages.
3956**
3957** The help text for each individual command begins with a line that starts
3958** with ".".  Subsequent lines are supplimental information.
3959**
3960** There must be two or more spaces between the end of the command and the
3961** start of the description of what that command does.
3962*/
3963static const char *(azHelp[]) = {
3964#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3965  ".archive ...             Manage SQL archives",
3966  "   Each command must have exactly one of the following options:",
3967  "     -c, --create               Create a new archive",
3968  "     -u, --update               Add or update files with changed mtime",
3969  "     -i, --insert               Like -u but always add even if unchanged",
3970  "     -t, --list                 List contents of archive",
3971  "     -x, --extract              Extract files from archive",
3972  "   Optional arguments:",
3973  "     -v, --verbose              Print each filename as it is processed",
3974  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3975  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3976  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3977  "     -n, --dryrun               Show the SQL that would have occurred",
3978  "   Examples:",
3979  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3980  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3981  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3982  "   See also:",
3983  "      http://sqlite.org/cli.html#sqlite_archive_support",
3984#endif
3985#ifndef SQLITE_OMIT_AUTHORIZATION
3986  ".auth ON|OFF             Show authorizer callbacks",
3987#endif
3988  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3989  "       --append            Use the appendvfs",
3990  "       --async             Write to FILE without journal and fsync()",
3991  ".bail on|off             Stop after hitting an error.  Default OFF",
3992  ".binary on|off           Turn binary output on or off.  Default OFF",
3993  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3994  ".changes on|off          Show number of rows changed by SQL",
3995  ".check GLOB              Fail if output since .testcase does not match",
3996  ".clone NEWDB             Clone data into NEWDB from the existing database",
3997  ".connection [close] [#]  Open or close an auxiliary database connection",
3998  ".databases               List names and files of attached databases",
3999  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
4000  ".dbinfo ?DB?             Show status information about the database",
4001  ".dump ?OBJECTS?          Render database content as SQL",
4002  "   Options:",
4003  "     --data-only            Output only INSERT statements",
4004  "     --newlines             Allow unescaped newline characters in output",
4005  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4006  "     --preserve-rowids      Include ROWID values in the output",
4007  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4008  "   Additional LIKE patterns can be given in subsequent arguments",
4009  ".echo on|off             Turn command echo on or off",
4010  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4011  "   Other Modes:",
4012#ifdef SQLITE_DEBUG
4013  "      test                  Show raw EXPLAIN QUERY PLAN output",
4014  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4015#endif
4016  "      trigger               Like \"full\" but also show trigger bytecode",
4017  ".excel                   Display the output of next command in spreadsheet",
4018  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4019  ".exit ?CODE?             Exit this program with return-code CODE",
4020  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4021  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4022  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4023  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4024  "   --help                  Show CMD details",
4025  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4026  ".headers on|off          Turn display of headers on or off",
4027  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4028  ".import FILE TABLE       Import data from FILE into TABLE",
4029  "   Options:",
4030  "     --ascii               Use \\037 and \\036 as column and row separators",
4031  "     --csv                 Use , and \\n as column and row separators",
4032  "     --skip N              Skip the first N rows of input",
4033  "     -v                    \"Verbose\" - increase auxiliary output",
4034  "   Notes:",
4035  "     *  If TABLE does not exist, it is created.  The first row of input",
4036  "        determines the column names.",
4037  "     *  If neither --csv or --ascii are used, the input mode is derived",
4038  "        from the \".mode\" output mode",
4039  "     *  If FILE begins with \"|\" then it is a command that generates the",
4040  "        input text.",
4041#ifndef SQLITE_OMIT_TEST_CONTROL
4042  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4043#endif
4044  ".indexes ?TABLE?         Show names of indexes",
4045  "                           If TABLE is specified, only show indexes for",
4046  "                           tables matching TABLE using the LIKE operator.",
4047#ifdef SQLITE_ENABLE_IOTRACE
4048  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4049#endif
4050  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4051  ".lint OPTIONS            Report potential schema issues.",
4052  "     Options:",
4053  "        fkey-indexes     Find missing foreign key indexes",
4054#ifndef SQLITE_OMIT_LOAD_EXTENSION
4055  ".load FILE ?ENTRY?       Load an extension library",
4056#endif
4057  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4058  ".mode MODE ?TABLE?       Set output mode",
4059  "   MODE is one of:",
4060  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
4061  "     box       Tables using unicode box-drawing characters",
4062  "     csv       Comma-separated values",
4063  "     column    Output in columns.  (See .width)",
4064  "     html      HTML <table> code",
4065  "     insert    SQL insert statements for TABLE",
4066  "     json      Results in a JSON array",
4067  "     line      One value per line",
4068  "     list      Values delimited by \"|\"",
4069  "     markdown  Markdown table format",
4070  "     quote     Escape answers as for SQL",
4071  "     table     ASCII-art table",
4072  "     tabs      Tab-separated values",
4073  "     tcl       TCL list elements",
4074  ".nonce STRING            Disable safe mode for one command if the nonce matches",
4075  ".nullvalue STRING        Use STRING in place of NULL values",
4076  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4077  "     If FILE begins with '|' then open as a pipe",
4078  "       --bom  Put a UTF8 byte-order mark at the beginning",
4079  "       -e     Send output to the system text editor",
4080  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4081#ifdef SQLITE_DEBUG
4082  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
4083#endif
4084  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4085  "     Options:",
4086  "        --append        Use appendvfs to append database to the end of FILE",
4087#ifndef SQLITE_OMIT_DESERIALIZE
4088  "        --deserialize   Load into memory using sqlite3_deserialize()",
4089  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4090  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4091#endif
4092  "        --new           Initialize FILE to an empty database",
4093  "        --nofollow      Do not follow symbolic links",
4094  "        --readonly      Open FILE readonly",
4095  "        --zip           FILE is a ZIP archive",
4096  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4097  "   If FILE begins with '|' then open it as a pipe.",
4098  "   Options:",
4099  "     --bom                 Prefix output with a UTF8 byte-order mark",
4100  "     -e                    Send output to the system text editor",
4101  "     -x                    Send output as CSV to a spreadsheet",
4102  ".parameter CMD ...       Manage SQL parameter bindings",
4103  "   clear                   Erase all bindings",
4104  "   init                    Initialize the TEMP table that holds bindings",
4105  "   list                    List the current parameter bindings",
4106  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4107  "                           PARAMETER should start with one of: $ : @ ?",
4108  "   unset PARAMETER         Remove PARAMETER from the binding table",
4109  ".print STRING...         Print literal STRING",
4110#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4111  ".progress N              Invoke progress handler after every N opcodes",
4112  "   --limit N                 Interrupt after N progress callbacks",
4113  "   --once                    Do no more than one progress interrupt",
4114  "   --quiet|-q                No output except at interrupts",
4115  "   --reset                   Reset the count for each input and interrupt",
4116#endif
4117  ".prompt MAIN CONTINUE    Replace the standard prompts",
4118  ".quit                    Exit this program",
4119  ".read FILE               Read input from FILE",
4120#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4121  ".recover                 Recover as much data as possible from corrupt db.",
4122  "   --freelist-corrupt       Assume the freelist is corrupt",
4123  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4124  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4125  "   --no-rowids              Do not attempt to recover rowid values",
4126  "                            that are not also INTEGER PRIMARY KEYs",
4127#endif
4128  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4129  ".save FILE               Write in-memory database into FILE",
4130  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4131  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4132  "   Options:",
4133  "      --indent             Try to pretty-print the schema",
4134  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4135  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4136  "    Options:",
4137  "       --init               Create a new SELFTEST table",
4138  "       -v                   Verbose output",
4139  ".separator COL ?ROW?     Change the column and row separators",
4140#if defined(SQLITE_ENABLE_SESSION)
4141  ".session ?NAME? CMD ...  Create or control sessions",
4142  "   Subcommands:",
4143  "     attach TABLE             Attach TABLE",
4144  "     changeset FILE           Write a changeset into FILE",
4145  "     close                    Close one session",
4146  "     enable ?BOOLEAN?         Set or query the enable bit",
4147  "     filter GLOB...           Reject tables matching GLOBs",
4148  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4149  "     isempty                  Query whether the session is empty",
4150  "     list                     List currently open session names",
4151  "     open DB NAME             Open a new session on DB",
4152  "     patchset FILE            Write a patchset into FILE",
4153  "   If ?NAME? is omitted, the first defined session is used.",
4154#endif
4155  ".sha3sum ...             Compute a SHA3 hash of database content",
4156  "    Options:",
4157  "      --schema              Also hash the sqlite_schema table",
4158  "      --sha3-224            Use the sha3-224 algorithm",
4159  "      --sha3-256            Use the sha3-256 algorithm (default)",
4160  "      --sha3-384            Use the sha3-384 algorithm",
4161  "      --sha3-512            Use the sha3-512 algorithm",
4162  "    Any other argument is a LIKE pattern for tables to hash",
4163#ifndef SQLITE_NOHAVE_SYSTEM
4164  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4165#endif
4166  ".show                    Show the current values for various settings",
4167  ".stats ?ARG?             Show stats or turn stats on or off",
4168  "   off                      Turn off automatic stat display",
4169  "   on                       Turn on automatic stat display",
4170  "   stmt                     Show statement stats",
4171  "   vmstep                   Show the virtual machine step count only",
4172#ifndef SQLITE_NOHAVE_SYSTEM
4173  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4174#endif
4175  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4176  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4177  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4178  "                           Run \".testctrl\" with no arguments for details",
4179  ".timeout MS              Try opening locked tables for MS milliseconds",
4180  ".timer on|off            Turn SQL timer on or off",
4181#ifndef SQLITE_OMIT_TRACE
4182  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4183  "    FILE                    Send output to FILE",
4184  "    stdout                  Send output to stdout",
4185  "    stderr                  Send output to stderr",
4186  "    off                     Disable tracing",
4187  "    --expanded              Expand query parameters",
4188#ifdef SQLITE_ENABLE_NORMALIZE
4189  "    --normalized            Normal the SQL statements",
4190#endif
4191  "    --plain                 Show SQL as it is input",
4192  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4193  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4194  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4195  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4196#endif /* SQLITE_OMIT_TRACE */
4197#ifdef SQLITE_DEBUG
4198  ".unmodule NAME ...       Unregister virtual table modules",
4199  "    --allexcept             Unregister everything except those named",
4200#endif
4201  ".vfsinfo ?AUX?           Information about the top-level VFS",
4202  ".vfslist                 List all available VFSes",
4203  ".vfsname ?AUX?           Print the name of the VFS stack",
4204  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4205  "     Negative values right-justify",
4206};
4207
4208/*
4209** Output help text.
4210**
4211** zPattern describes the set of commands for which help text is provided.
4212** If zPattern is NULL, then show all commands, but only give a one-line
4213** description of each.
4214**
4215** Return the number of matches.
4216*/
4217static int showHelp(FILE *out, const char *zPattern){
4218  int i = 0;
4219  int j = 0;
4220  int n = 0;
4221  char *zPat;
4222  if( zPattern==0
4223   || zPattern[0]=='0'
4224   || strcmp(zPattern,"-a")==0
4225   || strcmp(zPattern,"-all")==0
4226   || strcmp(zPattern,"--all")==0
4227  ){
4228    /* Show all commands, but only one line per command */
4229    if( zPattern==0 ) zPattern = "";
4230    for(i=0; i<ArraySize(azHelp); i++){
4231      if( azHelp[i][0]=='.' || zPattern[0] ){
4232        utf8_printf(out, "%s\n", azHelp[i]);
4233        n++;
4234      }
4235    }
4236  }else{
4237    /* Look for commands that for which zPattern is an exact prefix */
4238    zPat = sqlite3_mprintf(".%s*", zPattern);
4239    for(i=0; i<ArraySize(azHelp); i++){
4240      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4241        utf8_printf(out, "%s\n", azHelp[i]);
4242        j = i+1;
4243        n++;
4244      }
4245    }
4246    sqlite3_free(zPat);
4247    if( n ){
4248      if( n==1 ){
4249        /* when zPattern is a prefix of exactly one command, then include the
4250        ** details of that command, which should begin at offset j */
4251        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4252          utf8_printf(out, "%s\n", azHelp[j]);
4253          j++;
4254        }
4255      }
4256      return n;
4257    }
4258    /* Look for commands that contain zPattern anywhere.  Show the complete
4259    ** text of all commands that match. */
4260    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4261    for(i=0; i<ArraySize(azHelp); i++){
4262      if( azHelp[i][0]=='.' ) j = i;
4263      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4264        utf8_printf(out, "%s\n", azHelp[j]);
4265        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4266          j++;
4267          utf8_printf(out, "%s\n", azHelp[j]);
4268        }
4269        i = j;
4270        n++;
4271      }
4272    }
4273    sqlite3_free(zPat);
4274  }
4275  return n;
4276}
4277
4278/* Forward reference */
4279static int process_input(ShellState *p);
4280
4281/*
4282** Read the content of file zName into memory obtained from sqlite3_malloc64()
4283** and return a pointer to the buffer. The caller is responsible for freeing
4284** the memory.
4285**
4286** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4287** read.
4288**
4289** For convenience, a nul-terminator byte is always appended to the data read
4290** from the file before the buffer is returned. This byte is not included in
4291** the final value of (*pnByte), if applicable.
4292**
4293** NULL is returned if any error is encountered. The final value of *pnByte
4294** is undefined in this case.
4295*/
4296static char *readFile(const char *zName, int *pnByte){
4297  FILE *in = fopen(zName, "rb");
4298  long nIn;
4299  size_t nRead;
4300  char *pBuf;
4301  if( in==0 ) return 0;
4302  fseek(in, 0, SEEK_END);
4303  nIn = ftell(in);
4304  rewind(in);
4305  pBuf = sqlite3_malloc64( nIn+1 );
4306  if( pBuf==0 ){ fclose(in); return 0; }
4307  nRead = fread(pBuf, nIn, 1, in);
4308  fclose(in);
4309  if( nRead!=1 ){
4310    sqlite3_free(pBuf);
4311    return 0;
4312  }
4313  pBuf[nIn] = 0;
4314  if( pnByte ) *pnByte = nIn;
4315  return pBuf;
4316}
4317
4318#if defined(SQLITE_ENABLE_SESSION)
4319/*
4320** Close a single OpenSession object and release all of its associated
4321** resources.
4322*/
4323static void session_close(OpenSession *pSession){
4324  int i;
4325  sqlite3session_delete(pSession->p);
4326  sqlite3_free(pSession->zName);
4327  for(i=0; i<pSession->nFilter; i++){
4328    sqlite3_free(pSession->azFilter[i]);
4329  }
4330  sqlite3_free(pSession->azFilter);
4331  memset(pSession, 0, sizeof(OpenSession));
4332}
4333#endif
4334
4335/*
4336** Close all OpenSession objects and release all associated resources.
4337*/
4338#if defined(SQLITE_ENABLE_SESSION)
4339static void session_close_all(ShellState *p, int i){
4340  int j;
4341  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4342  for(j=0; j<pAuxDb->nSession; j++){
4343    session_close(&pAuxDb->aSession[j]);
4344  }
4345  pAuxDb->nSession = 0;
4346}
4347#else
4348# define session_close_all(X,Y)
4349#endif
4350
4351/*
4352** Implementation of the xFilter function for an open session.  Omit
4353** any tables named by ".session filter" but let all other table through.
4354*/
4355#if defined(SQLITE_ENABLE_SESSION)
4356static int session_filter(void *pCtx, const char *zTab){
4357  OpenSession *pSession = (OpenSession*)pCtx;
4358  int i;
4359  for(i=0; i<pSession->nFilter; i++){
4360    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4361  }
4362  return 1;
4363}
4364#endif
4365
4366/*
4367** Try to deduce the type of file for zName based on its content.  Return
4368** one of the SHELL_OPEN_* constants.
4369**
4370** If the file does not exist or is empty but its name looks like a ZIP
4371** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4372** Otherwise, assume an ordinary database regardless of the filename if
4373** the type cannot be determined from content.
4374*/
4375int deduceDatabaseType(const char *zName, int dfltZip){
4376  FILE *f = fopen(zName, "rb");
4377  size_t n;
4378  int rc = SHELL_OPEN_UNSPEC;
4379  char zBuf[100];
4380  if( f==0 ){
4381    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4382       return SHELL_OPEN_ZIPFILE;
4383    }else{
4384       return SHELL_OPEN_NORMAL;
4385    }
4386  }
4387  n = fread(zBuf, 16, 1, f);
4388  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4389    fclose(f);
4390    return SHELL_OPEN_NORMAL;
4391  }
4392  fseek(f, -25, SEEK_END);
4393  n = fread(zBuf, 25, 1, f);
4394  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4395    rc = SHELL_OPEN_APPENDVFS;
4396  }else{
4397    fseek(f, -22, SEEK_END);
4398    n = fread(zBuf, 22, 1, f);
4399    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4400       && zBuf[3]==0x06 ){
4401      rc = SHELL_OPEN_ZIPFILE;
4402    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4403      rc = SHELL_OPEN_ZIPFILE;
4404    }
4405  }
4406  fclose(f);
4407  return rc;
4408}
4409
4410#ifndef SQLITE_OMIT_DESERIALIZE
4411/*
4412** Reconstruct an in-memory database using the output from the "dbtotxt"
4413** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4414** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4415*/
4416static unsigned char *readHexDb(ShellState *p, int *pnData){
4417  unsigned char *a = 0;
4418  int nLine;
4419  int n = 0;
4420  int pgsz = 0;
4421  int iOffset = 0;
4422  int j, k;
4423  int rc;
4424  FILE *in;
4425  const char *zDbFilename = p->pAuxDb->zDbFilename;
4426  unsigned int x[16];
4427  char zLine[1000];
4428  if( zDbFilename ){
4429    in = fopen(zDbFilename, "r");
4430    if( in==0 ){
4431      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4432      return 0;
4433    }
4434    nLine = 0;
4435  }else{
4436    in = p->in;
4437    nLine = p->lineno;
4438    if( in==0 ) in = stdin;
4439  }
4440  *pnData = 0;
4441  nLine++;
4442  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4443  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4444  if( rc!=2 ) goto readHexDb_error;
4445  if( n<0 ) goto readHexDb_error;
4446  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4447  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4448  a = sqlite3_malloc( n ? n : 1 );
4449  if( a==0 ){
4450    utf8_printf(stderr, "Out of memory!\n");
4451    goto readHexDb_error;
4452  }
4453  memset(a, 0, n);
4454  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4455    utf8_printf(stderr, "invalid pagesize\n");
4456    goto readHexDb_error;
4457  }
4458  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4459    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4460    if( rc==2 ){
4461      iOffset = k;
4462      continue;
4463    }
4464    if( strncmp(zLine, "| end ", 6)==0 ){
4465      break;
4466    }
4467    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4468                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4469                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4470    if( rc==17 ){
4471      k = iOffset+j;
4472      if( k+16<=n ){
4473        int ii;
4474        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4475      }
4476    }
4477  }
4478  *pnData = n;
4479  if( in!=p->in ){
4480    fclose(in);
4481  }else{
4482    p->lineno = nLine;
4483  }
4484  return a;
4485
4486readHexDb_error:
4487  if( in!=p->in ){
4488    fclose(in);
4489  }else{
4490    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4491      nLine++;
4492      if(strncmp(zLine, "| end ", 6)==0 ) break;
4493    }
4494    p->lineno = nLine;
4495  }
4496  sqlite3_free(a);
4497  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4498  return 0;
4499}
4500#endif /* SQLITE_OMIT_DESERIALIZE */
4501
4502/*
4503** Scalar function "shell_int32". The first argument to this function
4504** must be a blob. The second a non-negative integer. This function
4505** reads and returns a 32-bit big-endian integer from byte
4506** offset (4*<arg2>) of the blob.
4507*/
4508static void shellInt32(
4509  sqlite3_context *context,
4510  int argc,
4511  sqlite3_value **argv
4512){
4513  const unsigned char *pBlob;
4514  int nBlob;
4515  int iInt;
4516
4517  UNUSED_PARAMETER(argc);
4518  nBlob = sqlite3_value_bytes(argv[0]);
4519  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4520  iInt = sqlite3_value_int(argv[1]);
4521
4522  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4523    const unsigned char *a = &pBlob[iInt*4];
4524    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4525                       + ((sqlite3_int64)a[1]<<16)
4526                       + ((sqlite3_int64)a[2]<< 8)
4527                       + ((sqlite3_int64)a[3]<< 0);
4528    sqlite3_result_int64(context, iVal);
4529  }
4530}
4531
4532/*
4533** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4534** using "..." with internal double-quote characters doubled.
4535*/
4536static void shellIdQuote(
4537  sqlite3_context *context,
4538  int argc,
4539  sqlite3_value **argv
4540){
4541  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4542  UNUSED_PARAMETER(argc);
4543  if( zName ){
4544    char *z = sqlite3_mprintf("\"%w\"", zName);
4545    sqlite3_result_text(context, z, -1, sqlite3_free);
4546  }
4547}
4548
4549/*
4550** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4551*/
4552static void shellUSleepFunc(
4553  sqlite3_context *context,
4554  int argcUnused,
4555  sqlite3_value **argv
4556){
4557  int sleep = sqlite3_value_int(argv[0]);
4558  (void)argcUnused;
4559  sqlite3_sleep(sleep/1000);
4560  sqlite3_result_int(context, sleep);
4561}
4562
4563/*
4564** Scalar function "shell_escape_crnl" used by the .recover command.
4565** The argument passed to this function is the output of built-in
4566** function quote(). If the first character of the input is "'",
4567** indicating that the value passed to quote() was a text value,
4568** then this function searches the input for "\n" and "\r" characters
4569** and adds a wrapper similar to the following:
4570**
4571**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4572**
4573** Or, if the first character of the input is not "'", then a copy
4574** of the input is returned.
4575*/
4576static void shellEscapeCrnl(
4577  sqlite3_context *context,
4578  int argc,
4579  sqlite3_value **argv
4580){
4581  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4582  UNUSED_PARAMETER(argc);
4583  if( zText[0]=='\'' ){
4584    int nText = sqlite3_value_bytes(argv[0]);
4585    int i;
4586    char zBuf1[20];
4587    char zBuf2[20];
4588    const char *zNL = 0;
4589    const char *zCR = 0;
4590    int nCR = 0;
4591    int nNL = 0;
4592
4593    for(i=0; zText[i]; i++){
4594      if( zNL==0 && zText[i]=='\n' ){
4595        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4596        nNL = (int)strlen(zNL);
4597      }
4598      if( zCR==0 && zText[i]=='\r' ){
4599        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4600        nCR = (int)strlen(zCR);
4601      }
4602    }
4603
4604    if( zNL || zCR ){
4605      int iOut = 0;
4606      i64 nMax = (nNL > nCR) ? nNL : nCR;
4607      i64 nAlloc = nMax * nText + (nMax+64)*2;
4608      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4609      if( zOut==0 ){
4610        sqlite3_result_error_nomem(context);
4611        return;
4612      }
4613
4614      if( zNL && zCR ){
4615        memcpy(&zOut[iOut], "replace(replace(", 16);
4616        iOut += 16;
4617      }else{
4618        memcpy(&zOut[iOut], "replace(", 8);
4619        iOut += 8;
4620      }
4621      for(i=0; zText[i]; i++){
4622        if( zText[i]=='\n' ){
4623          memcpy(&zOut[iOut], zNL, nNL);
4624          iOut += nNL;
4625        }else if( zText[i]=='\r' ){
4626          memcpy(&zOut[iOut], zCR, nCR);
4627          iOut += nCR;
4628        }else{
4629          zOut[iOut] = zText[i];
4630          iOut++;
4631        }
4632      }
4633
4634      if( zNL ){
4635        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4636        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4637        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4638      }
4639      if( zCR ){
4640        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4641        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4642        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4643      }
4644
4645      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4646      sqlite3_free(zOut);
4647      return;
4648    }
4649  }
4650
4651  sqlite3_result_value(context, argv[0]);
4652}
4653
4654/* Flags for open_db().
4655**
4656** The default behavior of open_db() is to exit(1) if the database fails to
4657** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4658** but still returns without calling exit.
4659**
4660** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4661** ZIP archive if the file does not exist or is empty and its name matches
4662** the *.zip pattern.
4663*/
4664#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4665#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4666
4667/*
4668** Make sure the database is open.  If it is not, then open it.  If
4669** the database fails to open, print an error message and exit.
4670*/
4671static void open_db(ShellState *p, int openFlags){
4672  if( p->db==0 ){
4673    const char *zDbFilename = p->pAuxDb->zDbFilename;
4674    if( p->openMode==SHELL_OPEN_UNSPEC ){
4675      if( zDbFilename==0 || zDbFilename[0]==0 ){
4676        p->openMode = SHELL_OPEN_NORMAL;
4677      }else{
4678        p->openMode = (u8)deduceDatabaseType(zDbFilename,
4679                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4680      }
4681    }
4682    switch( p->openMode ){
4683      case SHELL_OPEN_APPENDVFS: {
4684        sqlite3_open_v2(zDbFilename, &p->db,
4685           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4686        break;
4687      }
4688      case SHELL_OPEN_HEXDB:
4689      case SHELL_OPEN_DESERIALIZE: {
4690        sqlite3_open(0, &p->db);
4691        break;
4692      }
4693      case SHELL_OPEN_ZIPFILE: {
4694        sqlite3_open(":memory:", &p->db);
4695        break;
4696      }
4697      case SHELL_OPEN_READONLY: {
4698        sqlite3_open_v2(zDbFilename, &p->db,
4699            SQLITE_OPEN_READONLY|p->openFlags, 0);
4700        break;
4701      }
4702      case SHELL_OPEN_UNSPEC:
4703      case SHELL_OPEN_NORMAL: {
4704        sqlite3_open_v2(zDbFilename, &p->db,
4705           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4706        break;
4707      }
4708    }
4709    globalDb = p->db;
4710    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4711      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4712          zDbFilename, sqlite3_errmsg(p->db));
4713      if( openFlags & OPEN_DB_KEEPALIVE ){
4714        sqlite3_open(":memory:", &p->db);
4715        return;
4716      }
4717      exit(1);
4718    }
4719#ifndef SQLITE_OMIT_LOAD_EXTENSION
4720    sqlite3_enable_load_extension(p->db, 1);
4721#endif
4722    sqlite3_fileio_init(p->db, 0, 0);
4723    sqlite3_shathree_init(p->db, 0, 0);
4724    sqlite3_completion_init(p->db, 0, 0);
4725    sqlite3_uint_init(p->db, 0, 0);
4726    sqlite3_decimal_init(p->db, 0, 0);
4727    sqlite3_regexp_init(p->db, 0, 0);
4728    sqlite3_ieee_init(p->db, 0, 0);
4729    sqlite3_series_init(p->db, 0, 0);
4730#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4731    sqlite3_dbdata_init(p->db, 0, 0);
4732#endif
4733#ifdef SQLITE_HAVE_ZLIB
4734    sqlite3_zipfile_init(p->db, 0, 0);
4735    sqlite3_sqlar_init(p->db, 0, 0);
4736#endif
4737    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4738                            shellAddSchemaName, 0, 0);
4739    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4740                            shellModuleSchema, 0, 0);
4741    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4742                            shellPutsFunc, 0, 0);
4743    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4744                            shellEscapeCrnl, 0, 0);
4745    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4746                            shellInt32, 0, 0);
4747    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4748                            shellIdQuote, 0, 0);
4749    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
4750                            shellUSleepFunc, 0, 0);
4751#ifndef SQLITE_NOHAVE_SYSTEM
4752    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4753                            editFunc, 0, 0);
4754    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4755                            editFunc, 0, 0);
4756#endif
4757    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4758      char *zSql = sqlite3_mprintf(
4759         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
4760      sqlite3_exec(p->db, zSql, 0, 0, 0);
4761      sqlite3_free(zSql);
4762    }
4763#ifndef SQLITE_OMIT_DESERIALIZE
4764    else
4765    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4766      int rc;
4767      int nData = 0;
4768      unsigned char *aData;
4769      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4770        aData = (unsigned char*)readFile(zDbFilename, &nData);
4771      }else{
4772        aData = readHexDb(p, &nData);
4773        if( aData==0 ){
4774          return;
4775        }
4776      }
4777      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4778                   SQLITE_DESERIALIZE_RESIZEABLE |
4779                   SQLITE_DESERIALIZE_FREEONCLOSE);
4780      if( rc ){
4781        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4782      }
4783      if( p->szMax>0 ){
4784        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4785      }
4786    }
4787#endif
4788  }
4789  if( p->bSafeModePersist && p->db!=0 ){
4790    sqlite3_set_authorizer(p->db, safeModeAuth, p);
4791  }
4792}
4793
4794/*
4795** Attempt to close the databaes connection.  Report errors.
4796*/
4797void close_db(sqlite3 *db){
4798  int rc = sqlite3_close(db);
4799  if( rc ){
4800    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4801        rc, sqlite3_errmsg(db));
4802  }
4803}
4804
4805#if HAVE_READLINE || HAVE_EDITLINE
4806/*
4807** Readline completion callbacks
4808*/
4809static char *readline_completion_generator(const char *text, int state){
4810  static sqlite3_stmt *pStmt = 0;
4811  char *zRet;
4812  if( state==0 ){
4813    char *zSql;
4814    sqlite3_finalize(pStmt);
4815    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4816                           "  FROM completion(%Q) ORDER BY 1", text);
4817    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4818    sqlite3_free(zSql);
4819  }
4820  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4821    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4822  }else{
4823    sqlite3_finalize(pStmt);
4824    pStmt = 0;
4825    zRet = 0;
4826  }
4827  return zRet;
4828}
4829static char **readline_completion(const char *zText, int iStart, int iEnd){
4830  rl_attempted_completion_over = 1;
4831  return rl_completion_matches(zText, readline_completion_generator);
4832}
4833
4834#elif HAVE_LINENOISE
4835/*
4836** Linenoise completion callback
4837*/
4838static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4839  int nLine = strlen30(zLine);
4840  int i, iStart;
4841  sqlite3_stmt *pStmt = 0;
4842  char *zSql;
4843  char zBuf[1000];
4844
4845  if( nLine>sizeof(zBuf)-30 ) return;
4846  if( zLine[0]=='.' || zLine[0]=='#') return;
4847  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4848  if( i==nLine-1 ) return;
4849  iStart = i+1;
4850  memcpy(zBuf, zLine, iStart);
4851  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4852                         "  FROM completion(%Q,%Q) ORDER BY 1",
4853                         &zLine[iStart], zLine);
4854  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4855  sqlite3_free(zSql);
4856  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4857  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4858    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4859    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4860    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4861      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4862      linenoiseAddCompletion(lc, zBuf);
4863    }
4864  }
4865  sqlite3_finalize(pStmt);
4866}
4867#endif
4868
4869/*
4870** Do C-language style dequoting.
4871**
4872**    \a    -> alarm
4873**    \b    -> backspace
4874**    \t    -> tab
4875**    \n    -> newline
4876**    \v    -> vertical tab
4877**    \f    -> form feed
4878**    \r    -> carriage return
4879**    \s    -> space
4880**    \"    -> "
4881**    \'    -> '
4882**    \\    -> backslash
4883**    \NNN  -> ascii character NNN in octal
4884*/
4885static void resolve_backslashes(char *z){
4886  int i, j;
4887  char c;
4888  while( *z && *z!='\\' ) z++;
4889  for(i=j=0; (c = z[i])!=0; i++, j++){
4890    if( c=='\\' && z[i+1]!=0 ){
4891      c = z[++i];
4892      if( c=='a' ){
4893        c = '\a';
4894      }else if( c=='b' ){
4895        c = '\b';
4896      }else if( c=='t' ){
4897        c = '\t';
4898      }else if( c=='n' ){
4899        c = '\n';
4900      }else if( c=='v' ){
4901        c = '\v';
4902      }else if( c=='f' ){
4903        c = '\f';
4904      }else if( c=='r' ){
4905        c = '\r';
4906      }else if( c=='"' ){
4907        c = '"';
4908      }else if( c=='\'' ){
4909        c = '\'';
4910      }else if( c=='\\' ){
4911        c = '\\';
4912      }else if( c>='0' && c<='7' ){
4913        c -= '0';
4914        if( z[i+1]>='0' && z[i+1]<='7' ){
4915          i++;
4916          c = (c<<3) + z[i] - '0';
4917          if( z[i+1]>='0' && z[i+1]<='7' ){
4918            i++;
4919            c = (c<<3) + z[i] - '0';
4920          }
4921        }
4922      }
4923    }
4924    z[j] = c;
4925  }
4926  if( j<i ) z[j] = 0;
4927}
4928
4929/*
4930** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4931** for TRUE and FALSE.  Return the integer value if appropriate.
4932*/
4933static int booleanValue(const char *zArg){
4934  int i;
4935  if( zArg[0]=='0' && zArg[1]=='x' ){
4936    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4937  }else{
4938    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4939  }
4940  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4941  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4942    return 1;
4943  }
4944  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4945    return 0;
4946  }
4947  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4948          zArg);
4949  return 0;
4950}
4951
4952/*
4953** Set or clear a shell flag according to a boolean value.
4954*/
4955static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4956  if( booleanValue(zArg) ){
4957    ShellSetFlag(p, mFlag);
4958  }else{
4959    ShellClearFlag(p, mFlag);
4960  }
4961}
4962
4963/*
4964** Close an output file, assuming it is not stderr or stdout
4965*/
4966static void output_file_close(FILE *f){
4967  if( f && f!=stdout && f!=stderr ) fclose(f);
4968}
4969
4970/*
4971** Try to open an output file.   The names "stdout" and "stderr" are
4972** recognized and do the right thing.  NULL is returned if the output
4973** filename is "off".
4974*/
4975static FILE *output_file_open(const char *zFile, int bTextMode){
4976  FILE *f;
4977  if( strcmp(zFile,"stdout")==0 ){
4978    f = stdout;
4979  }else if( strcmp(zFile, "stderr")==0 ){
4980    f = stderr;
4981  }else if( strcmp(zFile, "off")==0 ){
4982    f = 0;
4983  }else{
4984    f = fopen(zFile, bTextMode ? "w" : "wb");
4985    if( f==0 ){
4986      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4987    }
4988  }
4989  return f;
4990}
4991
4992#ifndef SQLITE_OMIT_TRACE
4993/*
4994** A routine for handling output from sqlite3_trace().
4995*/
4996static int sql_trace_callback(
4997  unsigned mType,         /* The trace type */
4998  void *pArg,             /* The ShellState pointer */
4999  void *pP,               /* Usually a pointer to sqlite_stmt */
5000  void *pX                /* Auxiliary output */
5001){
5002  ShellState *p = (ShellState*)pArg;
5003  sqlite3_stmt *pStmt;
5004  const char *zSql;
5005  int nSql;
5006  if( p->traceOut==0 ) return 0;
5007  if( mType==SQLITE_TRACE_CLOSE ){
5008    utf8_printf(p->traceOut, "-- closing database connection\n");
5009    return 0;
5010  }
5011  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5012    zSql = (const char*)pX;
5013  }else{
5014    pStmt = (sqlite3_stmt*)pP;
5015    switch( p->eTraceType ){
5016      case SHELL_TRACE_EXPANDED: {
5017        zSql = sqlite3_expanded_sql(pStmt);
5018        break;
5019      }
5020#ifdef SQLITE_ENABLE_NORMALIZE
5021      case SHELL_TRACE_NORMALIZED: {
5022        zSql = sqlite3_normalized_sql(pStmt);
5023        break;
5024      }
5025#endif
5026      default: {
5027        zSql = sqlite3_sql(pStmt);
5028        break;
5029      }
5030    }
5031  }
5032  if( zSql==0 ) return 0;
5033  nSql = strlen30(zSql);
5034  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5035  switch( mType ){
5036    case SQLITE_TRACE_ROW:
5037    case SQLITE_TRACE_STMT: {
5038      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
5039      break;
5040    }
5041    case SQLITE_TRACE_PROFILE: {
5042      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5043      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
5044      break;
5045    }
5046  }
5047  return 0;
5048}
5049#endif
5050
5051/*
5052** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5053** a useful spot to set a debugger breakpoint.
5054*/
5055static void test_breakpoint(void){
5056  static int nCall = 0;
5057  nCall++;
5058}
5059
5060/*
5061** An object used to read a CSV and other files for import.
5062*/
5063typedef struct ImportCtx ImportCtx;
5064struct ImportCtx {
5065  const char *zFile;  /* Name of the input file */
5066  FILE *in;           /* Read the CSV text from this input stream */
5067  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5068  char *z;            /* Accumulated text for a field */
5069  int n;              /* Number of bytes in z */
5070  int nAlloc;         /* Space allocated for z[] */
5071  int nLine;          /* Current line number */
5072  int nRow;           /* Number of rows imported */
5073  int nErr;           /* Number of errors encountered */
5074  int bNotFirst;      /* True if one or more bytes already read */
5075  int cTerm;          /* Character that terminated the most recent field */
5076  int cColSep;        /* The column separator character.  (Usually ",") */
5077  int cRowSep;        /* The row separator character.  (Usually "\n") */
5078};
5079
5080/* Clean up resourced used by an ImportCtx */
5081static void import_cleanup(ImportCtx *p){
5082  if( p->in!=0 && p->xCloser!=0 ){
5083    p->xCloser(p->in);
5084    p->in = 0;
5085  }
5086  sqlite3_free(p->z);
5087  p->z = 0;
5088}
5089
5090/* Append a single byte to z[] */
5091static void import_append_char(ImportCtx *p, int c){
5092  if( p->n+1>=p->nAlloc ){
5093    p->nAlloc += p->nAlloc + 100;
5094    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5095    if( p->z==0 ) shell_out_of_memory();
5096  }
5097  p->z[p->n++] = (char)c;
5098}
5099
5100/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5101** with the option of having a separator other than ",".
5102**
5103**   +  Input comes from p->in.
5104**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5105**      from sqlite3_malloc64().
5106**   +  Use p->cSep as the column separator.  The default is ",".
5107**   +  Use p->rSep as the row separator.  The default is "\n".
5108**   +  Keep track of the line number in p->nLine.
5109**   +  Store the character that terminates the field in p->cTerm.  Store
5110**      EOF on end-of-file.
5111**   +  Report syntax errors on stderr
5112*/
5113static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5114  int c;
5115  int cSep = p->cColSep;
5116  int rSep = p->cRowSep;
5117  p->n = 0;
5118  c = fgetc(p->in);
5119  if( c==EOF || seenInterrupt ){
5120    p->cTerm = EOF;
5121    return 0;
5122  }
5123  if( c=='"' ){
5124    int pc, ppc;
5125    int startLine = p->nLine;
5126    int cQuote = c;
5127    pc = ppc = 0;
5128    while( 1 ){
5129      c = fgetc(p->in);
5130      if( c==rSep ) p->nLine++;
5131      if( c==cQuote ){
5132        if( pc==cQuote ){
5133          pc = 0;
5134          continue;
5135        }
5136      }
5137      if( (c==cSep && pc==cQuote)
5138       || (c==rSep && pc==cQuote)
5139       || (c==rSep && pc=='\r' && ppc==cQuote)
5140       || (c==EOF && pc==cQuote)
5141      ){
5142        do{ p->n--; }while( p->z[p->n]!=cQuote );
5143        p->cTerm = c;
5144        break;
5145      }
5146      if( pc==cQuote && c!='\r' ){
5147        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5148                p->zFile, p->nLine, cQuote);
5149      }
5150      if( c==EOF ){
5151        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5152                p->zFile, startLine, cQuote);
5153        p->cTerm = c;
5154        break;
5155      }
5156      import_append_char(p, c);
5157      ppc = pc;
5158      pc = c;
5159    }
5160  }else{
5161    /* If this is the first field being parsed and it begins with the
5162    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5163    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5164      import_append_char(p, c);
5165      c = fgetc(p->in);
5166      if( (c&0xff)==0xbb ){
5167        import_append_char(p, c);
5168        c = fgetc(p->in);
5169        if( (c&0xff)==0xbf ){
5170          p->bNotFirst = 1;
5171          p->n = 0;
5172          return csv_read_one_field(p);
5173        }
5174      }
5175    }
5176    while( c!=EOF && c!=cSep && c!=rSep ){
5177      import_append_char(p, c);
5178      c = fgetc(p->in);
5179    }
5180    if( c==rSep ){
5181      p->nLine++;
5182      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5183    }
5184    p->cTerm = c;
5185  }
5186  if( p->z ) p->z[p->n] = 0;
5187  p->bNotFirst = 1;
5188  return p->z;
5189}
5190
5191/* Read a single field of ASCII delimited text.
5192**
5193**   +  Input comes from p->in.
5194**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5195**      from sqlite3_malloc64().
5196**   +  Use p->cSep as the column separator.  The default is "\x1F".
5197**   +  Use p->rSep as the row separator.  The default is "\x1E".
5198**   +  Keep track of the row number in p->nLine.
5199**   +  Store the character that terminates the field in p->cTerm.  Store
5200**      EOF on end-of-file.
5201**   +  Report syntax errors on stderr
5202*/
5203static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5204  int c;
5205  int cSep = p->cColSep;
5206  int rSep = p->cRowSep;
5207  p->n = 0;
5208  c = fgetc(p->in);
5209  if( c==EOF || seenInterrupt ){
5210    p->cTerm = EOF;
5211    return 0;
5212  }
5213  while( c!=EOF && c!=cSep && c!=rSep ){
5214    import_append_char(p, c);
5215    c = fgetc(p->in);
5216  }
5217  if( c==rSep ){
5218    p->nLine++;
5219  }
5220  p->cTerm = c;
5221  if( p->z ) p->z[p->n] = 0;
5222  return p->z;
5223}
5224
5225/*
5226** Try to transfer data for table zTable.  If an error is seen while
5227** moving forward, try to go backwards.  The backwards movement won't
5228** work for WITHOUT ROWID tables.
5229*/
5230static void tryToCloneData(
5231  ShellState *p,
5232  sqlite3 *newDb,
5233  const char *zTable
5234){
5235  sqlite3_stmt *pQuery = 0;
5236  sqlite3_stmt *pInsert = 0;
5237  char *zQuery = 0;
5238  char *zInsert = 0;
5239  int rc;
5240  int i, j, n;
5241  int nTable = strlen30(zTable);
5242  int k = 0;
5243  int cnt = 0;
5244  const int spinRate = 10000;
5245
5246  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5247  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5248  if( rc ){
5249    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5250            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5251            zQuery);
5252    goto end_data_xfer;
5253  }
5254  n = sqlite3_column_count(pQuery);
5255  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5256  if( zInsert==0 ) shell_out_of_memory();
5257  sqlite3_snprintf(200+nTable,zInsert,
5258                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5259  i = strlen30(zInsert);
5260  for(j=1; j<n; j++){
5261    memcpy(zInsert+i, ",?", 2);
5262    i += 2;
5263  }
5264  memcpy(zInsert+i, ");", 3);
5265  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5266  if( rc ){
5267    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5268            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5269            zQuery);
5270    goto end_data_xfer;
5271  }
5272  for(k=0; k<2; k++){
5273    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5274      for(i=0; i<n; i++){
5275        switch( sqlite3_column_type(pQuery, i) ){
5276          case SQLITE_NULL: {
5277            sqlite3_bind_null(pInsert, i+1);
5278            break;
5279          }
5280          case SQLITE_INTEGER: {
5281            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5282            break;
5283          }
5284          case SQLITE_FLOAT: {
5285            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5286            break;
5287          }
5288          case SQLITE_TEXT: {
5289            sqlite3_bind_text(pInsert, i+1,
5290                             (const char*)sqlite3_column_text(pQuery,i),
5291                             -1, SQLITE_STATIC);
5292            break;
5293          }
5294          case SQLITE_BLOB: {
5295            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5296                                            sqlite3_column_bytes(pQuery,i),
5297                                            SQLITE_STATIC);
5298            break;
5299          }
5300        }
5301      } /* End for */
5302      rc = sqlite3_step(pInsert);
5303      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5304        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5305                        sqlite3_errmsg(newDb));
5306      }
5307      sqlite3_reset(pInsert);
5308      cnt++;
5309      if( (cnt%spinRate)==0 ){
5310        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5311        fflush(stdout);
5312      }
5313    } /* End while */
5314    if( rc==SQLITE_DONE ) break;
5315    sqlite3_finalize(pQuery);
5316    sqlite3_free(zQuery);
5317    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5318                             zTable);
5319    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5320    if( rc ){
5321      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5322      break;
5323    }
5324  } /* End for(k=0...) */
5325
5326end_data_xfer:
5327  sqlite3_finalize(pQuery);
5328  sqlite3_finalize(pInsert);
5329  sqlite3_free(zQuery);
5330  sqlite3_free(zInsert);
5331}
5332
5333
5334/*
5335** Try to transfer all rows of the schema that match zWhere.  For
5336** each row, invoke xForEach() on the object defined by that row.
5337** If an error is encountered while moving forward through the
5338** sqlite_schema table, try again moving backwards.
5339*/
5340static void tryToCloneSchema(
5341  ShellState *p,
5342  sqlite3 *newDb,
5343  const char *zWhere,
5344  void (*xForEach)(ShellState*,sqlite3*,const char*)
5345){
5346  sqlite3_stmt *pQuery = 0;
5347  char *zQuery = 0;
5348  int rc;
5349  const unsigned char *zName;
5350  const unsigned char *zSql;
5351  char *zErrMsg = 0;
5352
5353  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5354                           " WHERE %s", zWhere);
5355  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5356  if( rc ){
5357    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5358                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5359                    zQuery);
5360    goto end_schema_xfer;
5361  }
5362  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5363    zName = sqlite3_column_text(pQuery, 0);
5364    zSql = sqlite3_column_text(pQuery, 1);
5365    printf("%s... ", zName); fflush(stdout);
5366    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5367    if( zErrMsg ){
5368      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5369      sqlite3_free(zErrMsg);
5370      zErrMsg = 0;
5371    }
5372    if( xForEach ){
5373      xForEach(p, newDb, (const char*)zName);
5374    }
5375    printf("done\n");
5376  }
5377  if( rc!=SQLITE_DONE ){
5378    sqlite3_finalize(pQuery);
5379    sqlite3_free(zQuery);
5380    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5381                             " WHERE %s ORDER BY rowid DESC", zWhere);
5382    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5383    if( rc ){
5384      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5385                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5386                      zQuery);
5387      goto end_schema_xfer;
5388    }
5389    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5390      zName = sqlite3_column_text(pQuery, 0);
5391      zSql = sqlite3_column_text(pQuery, 1);
5392      printf("%s... ", zName); fflush(stdout);
5393      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5394      if( zErrMsg ){
5395        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5396        sqlite3_free(zErrMsg);
5397        zErrMsg = 0;
5398      }
5399      if( xForEach ){
5400        xForEach(p, newDb, (const char*)zName);
5401      }
5402      printf("done\n");
5403    }
5404  }
5405end_schema_xfer:
5406  sqlite3_finalize(pQuery);
5407  sqlite3_free(zQuery);
5408}
5409
5410/*
5411** Open a new database file named "zNewDb".  Try to recover as much information
5412** as possible out of the main database (which might be corrupt) and write it
5413** into zNewDb.
5414*/
5415static void tryToClone(ShellState *p, const char *zNewDb){
5416  int rc;
5417  sqlite3 *newDb = 0;
5418  if( access(zNewDb,0)==0 ){
5419    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5420    return;
5421  }
5422  rc = sqlite3_open(zNewDb, &newDb);
5423  if( rc ){
5424    utf8_printf(stderr, "Cannot create output database: %s\n",
5425            sqlite3_errmsg(newDb));
5426  }else{
5427    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5428    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5429    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5430    tryToCloneSchema(p, newDb, "type!='table'", 0);
5431    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5432    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5433  }
5434  close_db(newDb);
5435}
5436
5437/*
5438** Change the output file back to stdout.
5439**
5440** If the p->doXdgOpen flag is set, that means the output was being
5441** redirected to a temporary file named by p->zTempFile.  In that case,
5442** launch start/open/xdg-open on that temporary file.
5443*/
5444static void output_reset(ShellState *p){
5445  if( p->outfile[0]=='|' ){
5446#ifndef SQLITE_OMIT_POPEN
5447    pclose(p->out);
5448#endif
5449  }else{
5450    output_file_close(p->out);
5451#ifndef SQLITE_NOHAVE_SYSTEM
5452    if( p->doXdgOpen ){
5453      const char *zXdgOpenCmd =
5454#if defined(_WIN32)
5455      "start";
5456#elif defined(__APPLE__)
5457      "open";
5458#else
5459      "xdg-open";
5460#endif
5461      char *zCmd;
5462      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5463      if( system(zCmd) ){
5464        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5465      }else{
5466        /* Give the start/open/xdg-open command some time to get
5467        ** going before we continue, and potential delete the
5468        ** p->zTempFile data file out from under it */
5469        sqlite3_sleep(2000);
5470      }
5471      sqlite3_free(zCmd);
5472      outputModePop(p);
5473      p->doXdgOpen = 0;
5474    }
5475#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5476  }
5477  p->outfile[0] = 0;
5478  p->out = stdout;
5479}
5480
5481/*
5482** Run an SQL command and return the single integer result.
5483*/
5484static int db_int(ShellState *p, const char *zSql){
5485  sqlite3_stmt *pStmt;
5486  int res = 0;
5487  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5488  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5489    res = sqlite3_column_int(pStmt,0);
5490  }
5491  sqlite3_finalize(pStmt);
5492  return res;
5493}
5494
5495/*
5496** Convert a 2-byte or 4-byte big-endian integer into a native integer
5497*/
5498static unsigned int get2byteInt(unsigned char *a){
5499  return (a[0]<<8) + a[1];
5500}
5501static unsigned int get4byteInt(unsigned char *a){
5502  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5503}
5504
5505/*
5506** Implementation of the ".dbinfo" command.
5507**
5508** Return 1 on error, 2 to exit, and 0 otherwise.
5509*/
5510static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5511  static const struct { const char *zName; int ofst; } aField[] = {
5512     { "file change counter:",  24  },
5513     { "database page count:",  28  },
5514     { "freelist page count:",  36  },
5515     { "schema cookie:",        40  },
5516     { "schema format:",        44  },
5517     { "default cache size:",   48  },
5518     { "autovacuum top root:",  52  },
5519     { "incremental vacuum:",   64  },
5520     { "text encoding:",        56  },
5521     { "user version:",         60  },
5522     { "application id:",       68  },
5523     { "software version:",     96  },
5524  };
5525  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5526     { "number of tables:",
5527       "SELECT count(*) FROM %s WHERE type='table'" },
5528     { "number of indexes:",
5529       "SELECT count(*) FROM %s WHERE type='index'" },
5530     { "number of triggers:",
5531       "SELECT count(*) FROM %s WHERE type='trigger'" },
5532     { "number of views:",
5533       "SELECT count(*) FROM %s WHERE type='view'" },
5534     { "schema size:",
5535       "SELECT total(length(sql)) FROM %s" },
5536  };
5537  int i, rc;
5538  unsigned iDataVersion;
5539  char *zSchemaTab;
5540  char *zDb = nArg>=2 ? azArg[1] : "main";
5541  sqlite3_stmt *pStmt = 0;
5542  unsigned char aHdr[100];
5543  open_db(p, 0);
5544  if( p->db==0 ) return 1;
5545  rc = sqlite3_prepare_v2(p->db,
5546             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5547             -1, &pStmt, 0);
5548  if( rc ){
5549    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5550    sqlite3_finalize(pStmt);
5551    return 1;
5552  }
5553  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5554  if( sqlite3_step(pStmt)==SQLITE_ROW
5555   && sqlite3_column_bytes(pStmt,0)>100
5556  ){
5557    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5558    sqlite3_finalize(pStmt);
5559  }else{
5560    raw_printf(stderr, "unable to read database header\n");
5561    sqlite3_finalize(pStmt);
5562    return 1;
5563  }
5564  i = get2byteInt(aHdr+16);
5565  if( i==1 ) i = 65536;
5566  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5567  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5568  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5569  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5570  for(i=0; i<ArraySize(aField); i++){
5571    int ofst = aField[i].ofst;
5572    unsigned int val = get4byteInt(aHdr + ofst);
5573    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5574    switch( ofst ){
5575      case 56: {
5576        if( val==1 ) raw_printf(p->out, " (utf8)");
5577        if( val==2 ) raw_printf(p->out, " (utf16le)");
5578        if( val==3 ) raw_printf(p->out, " (utf16be)");
5579      }
5580    }
5581    raw_printf(p->out, "\n");
5582  }
5583  if( zDb==0 ){
5584    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5585  }else if( strcmp(zDb,"temp")==0 ){
5586    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5587  }else{
5588    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5589  }
5590  for(i=0; i<ArraySize(aQuery); i++){
5591    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5592    int val = db_int(p, zSql);
5593    sqlite3_free(zSql);
5594    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5595  }
5596  sqlite3_free(zSchemaTab);
5597  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5598  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5599  return 0;
5600}
5601
5602/*
5603** Print the current sqlite3_errmsg() value to stderr and return 1.
5604*/
5605static int shellDatabaseError(sqlite3 *db){
5606  const char *zErr = sqlite3_errmsg(db);
5607  utf8_printf(stderr, "Error: %s\n", zErr);
5608  return 1;
5609}
5610
5611/*
5612** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5613** if they match and FALSE (0) if they do not match.
5614**
5615** Globbing rules:
5616**
5617**      '*'       Matches any sequence of zero or more characters.
5618**
5619**      '?'       Matches exactly one character.
5620**
5621**     [...]      Matches one character from the enclosed list of
5622**                characters.
5623**
5624**     [^...]     Matches one character not in the enclosed list.
5625**
5626**      '#'       Matches any sequence of one or more digits with an
5627**                optional + or - sign in front
5628**
5629**      ' '       Any span of whitespace matches any other span of
5630**                whitespace.
5631**
5632** Extra whitespace at the end of z[] is ignored.
5633*/
5634static int testcase_glob(const char *zGlob, const char *z){
5635  int c, c2;
5636  int invert;
5637  int seen;
5638
5639  while( (c = (*(zGlob++)))!=0 ){
5640    if( IsSpace(c) ){
5641      if( !IsSpace(*z) ) return 0;
5642      while( IsSpace(*zGlob) ) zGlob++;
5643      while( IsSpace(*z) ) z++;
5644    }else if( c=='*' ){
5645      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5646        if( c=='?' && (*(z++))==0 ) return 0;
5647      }
5648      if( c==0 ){
5649        return 1;
5650      }else if( c=='[' ){
5651        while( *z && testcase_glob(zGlob-1,z)==0 ){
5652          z++;
5653        }
5654        return (*z)!=0;
5655      }
5656      while( (c2 = (*(z++)))!=0 ){
5657        while( c2!=c ){
5658          c2 = *(z++);
5659          if( c2==0 ) return 0;
5660        }
5661        if( testcase_glob(zGlob,z) ) return 1;
5662      }
5663      return 0;
5664    }else if( c=='?' ){
5665      if( (*(z++))==0 ) return 0;
5666    }else if( c=='[' ){
5667      int prior_c = 0;
5668      seen = 0;
5669      invert = 0;
5670      c = *(z++);
5671      if( c==0 ) return 0;
5672      c2 = *(zGlob++);
5673      if( c2=='^' ){
5674        invert = 1;
5675        c2 = *(zGlob++);
5676      }
5677      if( c2==']' ){
5678        if( c==']' ) seen = 1;
5679        c2 = *(zGlob++);
5680      }
5681      while( c2 && c2!=']' ){
5682        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5683          c2 = *(zGlob++);
5684          if( c>=prior_c && c<=c2 ) seen = 1;
5685          prior_c = 0;
5686        }else{
5687          if( c==c2 ){
5688            seen = 1;
5689          }
5690          prior_c = c2;
5691        }
5692        c2 = *(zGlob++);
5693      }
5694      if( c2==0 || (seen ^ invert)==0 ) return 0;
5695    }else if( c=='#' ){
5696      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5697      if( !IsDigit(z[0]) ) return 0;
5698      z++;
5699      while( IsDigit(z[0]) ){ z++; }
5700    }else{
5701      if( c!=(*(z++)) ) return 0;
5702    }
5703  }
5704  while( IsSpace(*z) ){ z++; }
5705  return *z==0;
5706}
5707
5708
5709/*
5710** Compare the string as a command-line option with either one or two
5711** initial "-" characters.
5712*/
5713static int optionMatch(const char *zStr, const char *zOpt){
5714  if( zStr[0]!='-' ) return 0;
5715  zStr++;
5716  if( zStr[0]=='-' ) zStr++;
5717  return strcmp(zStr, zOpt)==0;
5718}
5719
5720/*
5721** Delete a file.
5722*/
5723int shellDeleteFile(const char *zFilename){
5724  int rc;
5725#ifdef _WIN32
5726  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5727  rc = _wunlink(z);
5728  sqlite3_free(z);
5729#else
5730  rc = unlink(zFilename);
5731#endif
5732  return rc;
5733}
5734
5735/*
5736** Try to delete the temporary file (if there is one) and free the
5737** memory used to hold the name of the temp file.
5738*/
5739static void clearTempFile(ShellState *p){
5740  if( p->zTempFile==0 ) return;
5741  if( p->doXdgOpen ) return;
5742  if( shellDeleteFile(p->zTempFile) ) return;
5743  sqlite3_free(p->zTempFile);
5744  p->zTempFile = 0;
5745}
5746
5747/*
5748** Create a new temp file name with the given suffix.
5749*/
5750static void newTempFile(ShellState *p, const char *zSuffix){
5751  clearTempFile(p);
5752  sqlite3_free(p->zTempFile);
5753  p->zTempFile = 0;
5754  if( p->db ){
5755    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5756  }
5757  if( p->zTempFile==0 ){
5758    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5759    ** will not work and we will need to fallback to guessing */
5760    char *zTemp;
5761    sqlite3_uint64 r;
5762    sqlite3_randomness(sizeof(r), &r);
5763    zTemp = getenv("TEMP");
5764    if( zTemp==0 ) zTemp = getenv("TMP");
5765    if( zTemp==0 ){
5766#ifdef _WIN32
5767      zTemp = "\\tmp";
5768#else
5769      zTemp = "/tmp";
5770#endif
5771    }
5772    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5773  }else{
5774    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5775  }
5776  if( p->zTempFile==0 ){
5777    raw_printf(stderr, "out of memory\n");
5778    exit(1);
5779  }
5780}
5781
5782
5783/*
5784** The implementation of SQL scalar function fkey_collate_clause(), used
5785** by the ".lint fkey-indexes" command. This scalar function is always
5786** called with four arguments - the parent table name, the parent column name,
5787** the child table name and the child column name.
5788**
5789**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5790**
5791** If either of the named tables or columns do not exist, this function
5792** returns an empty string. An empty string is also returned if both tables
5793** and columns exist but have the same default collation sequence. Or,
5794** if both exist but the default collation sequences are different, this
5795** function returns the string " COLLATE <parent-collation>", where
5796** <parent-collation> is the default collation sequence of the parent column.
5797*/
5798static void shellFkeyCollateClause(
5799  sqlite3_context *pCtx,
5800  int nVal,
5801  sqlite3_value **apVal
5802){
5803  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5804  const char *zParent;
5805  const char *zParentCol;
5806  const char *zParentSeq;
5807  const char *zChild;
5808  const char *zChildCol;
5809  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5810  int rc;
5811
5812  assert( nVal==4 );
5813  zParent = (const char*)sqlite3_value_text(apVal[0]);
5814  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5815  zChild = (const char*)sqlite3_value_text(apVal[2]);
5816  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5817
5818  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5819  rc = sqlite3_table_column_metadata(
5820      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5821  );
5822  if( rc==SQLITE_OK ){
5823    rc = sqlite3_table_column_metadata(
5824        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5825    );
5826  }
5827
5828  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5829    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5830    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5831    sqlite3_free(z);
5832  }
5833}
5834
5835
5836/*
5837** The implementation of dot-command ".lint fkey-indexes".
5838*/
5839static int lintFkeyIndexes(
5840  ShellState *pState,             /* Current shell tool state */
5841  char **azArg,                   /* Array of arguments passed to dot command */
5842  int nArg                        /* Number of entries in azArg[] */
5843){
5844  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5845  FILE *out = pState->out;        /* Stream to write non-error output to */
5846  int bVerbose = 0;               /* If -verbose is present */
5847  int bGroupByParent = 0;         /* If -groupbyparent is present */
5848  int i;                          /* To iterate through azArg[] */
5849  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5850  int rc;                         /* Return code */
5851  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5852
5853  /*
5854  ** This SELECT statement returns one row for each foreign key constraint
5855  ** in the schema of the main database. The column values are:
5856  **
5857  ** 0. The text of an SQL statement similar to:
5858  **
5859  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5860  **
5861  **    This SELECT is similar to the one that the foreign keys implementation
5862  **    needs to run internally on child tables. If there is an index that can
5863  **    be used to optimize this query, then it can also be used by the FK
5864  **    implementation to optimize DELETE or UPDATE statements on the parent
5865  **    table.
5866  **
5867  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5868  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5869  **    contains an index that can be used to optimize the query.
5870  **
5871  ** 2. Human readable text that describes the child table and columns. e.g.
5872  **
5873  **       "child_table(child_key1, child_key2)"
5874  **
5875  ** 3. Human readable text that describes the parent table and columns. e.g.
5876  **
5877  **       "parent_table(parent_key1, parent_key2)"
5878  **
5879  ** 4. A full CREATE INDEX statement for an index that could be used to
5880  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5881  **
5882  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5883  **
5884  ** 5. The name of the parent table.
5885  **
5886  ** These six values are used by the C logic below to generate the report.
5887  */
5888  const char *zSql =
5889  "SELECT "
5890    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5891    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5892    "  || fkey_collate_clause("
5893    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5894    ", "
5895    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
5896    "  || group_concat('*=?', ' AND ') || ')'"
5897    ", "
5898    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5899    ", "
5900    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5901    ", "
5902    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5903    "  || ' ON ' || quote(s.name) || '('"
5904    "  || group_concat(quote(f.[from]) ||"
5905    "        fkey_collate_clause("
5906    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5907    "  || ');'"
5908    ", "
5909    "     f.[table] "
5910    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5911    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5912    "GROUP BY s.name, f.id "
5913    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5914  ;
5915  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
5916
5917  for(i=2; i<nArg; i++){
5918    int n = strlen30(azArg[i]);
5919    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5920      bVerbose = 1;
5921    }
5922    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5923      bGroupByParent = 1;
5924      zIndent = "    ";
5925    }
5926    else{
5927      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5928          azArg[0], azArg[1]
5929      );
5930      return SQLITE_ERROR;
5931    }
5932  }
5933
5934  /* Register the fkey_collate_clause() SQL function */
5935  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5936      0, shellFkeyCollateClause, 0, 0
5937  );
5938
5939
5940  if( rc==SQLITE_OK ){
5941    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5942  }
5943  if( rc==SQLITE_OK ){
5944    sqlite3_bind_int(pSql, 1, bGroupByParent);
5945  }
5946
5947  if( rc==SQLITE_OK ){
5948    int rc2;
5949    char *zPrev = 0;
5950    while( SQLITE_ROW==sqlite3_step(pSql) ){
5951      int res = -1;
5952      sqlite3_stmt *pExplain = 0;
5953      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5954      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5955      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5956      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5957      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5958      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5959
5960      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5961      if( rc!=SQLITE_OK ) break;
5962      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5963        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5964        res = (
5965              0==sqlite3_strglob(zGlob, zPlan)
5966           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5967        );
5968      }
5969      rc = sqlite3_finalize(pExplain);
5970      if( rc!=SQLITE_OK ) break;
5971
5972      if( res<0 ){
5973        raw_printf(stderr, "Error: internal error");
5974        break;
5975      }else{
5976        if( bGroupByParent
5977        && (bVerbose || res==0)
5978        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5979        ){
5980          raw_printf(out, "-- Parent table %s\n", zParent);
5981          sqlite3_free(zPrev);
5982          zPrev = sqlite3_mprintf("%s", zParent);
5983        }
5984
5985        if( res==0 ){
5986          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5987        }else if( bVerbose ){
5988          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5989              zIndent, zFrom, zTarget
5990          );
5991        }
5992      }
5993    }
5994    sqlite3_free(zPrev);
5995
5996    if( rc!=SQLITE_OK ){
5997      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5998    }
5999
6000    rc2 = sqlite3_finalize(pSql);
6001    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6002      rc = rc2;
6003      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6004    }
6005  }else{
6006    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6007  }
6008
6009  return rc;
6010}
6011
6012/*
6013** Implementation of ".lint" dot command.
6014*/
6015static int lintDotCommand(
6016  ShellState *pState,             /* Current shell tool state */
6017  char **azArg,                   /* Array of arguments passed to dot command */
6018  int nArg                        /* Number of entries in azArg[] */
6019){
6020  int n;
6021  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6022  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6023  return lintFkeyIndexes(pState, azArg, nArg);
6024
6025 usage:
6026  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6027  raw_printf(stderr, "Where sub-commands are:\n");
6028  raw_printf(stderr, "    fkey-indexes\n");
6029  return SQLITE_ERROR;
6030}
6031
6032#if !defined SQLITE_OMIT_VIRTUALTABLE
6033static void shellPrepare(
6034  sqlite3 *db,
6035  int *pRc,
6036  const char *zSql,
6037  sqlite3_stmt **ppStmt
6038){
6039  *ppStmt = 0;
6040  if( *pRc==SQLITE_OK ){
6041    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6042    if( rc!=SQLITE_OK ){
6043      raw_printf(stderr, "sql error: %s (%d)\n",
6044          sqlite3_errmsg(db), sqlite3_errcode(db)
6045      );
6046      *pRc = rc;
6047    }
6048  }
6049}
6050
6051/*
6052** Create a prepared statement using printf-style arguments for the SQL.
6053**
6054** This routine is could be marked "static".  But it is not always used,
6055** depending on compile-time options.  By omitting the "static", we avoid
6056** nuisance compiler warnings about "defined but not used".
6057*/
6058void shellPreparePrintf(
6059  sqlite3 *db,
6060  int *pRc,
6061  sqlite3_stmt **ppStmt,
6062  const char *zFmt,
6063  ...
6064){
6065  *ppStmt = 0;
6066  if( *pRc==SQLITE_OK ){
6067    va_list ap;
6068    char *z;
6069    va_start(ap, zFmt);
6070    z = sqlite3_vmprintf(zFmt, ap);
6071    va_end(ap);
6072    if( z==0 ){
6073      *pRc = SQLITE_NOMEM;
6074    }else{
6075      shellPrepare(db, pRc, z, ppStmt);
6076      sqlite3_free(z);
6077    }
6078  }
6079}
6080
6081/* Finalize the prepared statement created using shellPreparePrintf().
6082**
6083** This routine is could be marked "static".  But it is not always used,
6084** depending on compile-time options.  By omitting the "static", we avoid
6085** nuisance compiler warnings about "defined but not used".
6086*/
6087void shellFinalize(
6088  int *pRc,
6089  sqlite3_stmt *pStmt
6090){
6091  if( pStmt ){
6092    sqlite3 *db = sqlite3_db_handle(pStmt);
6093    int rc = sqlite3_finalize(pStmt);
6094    if( *pRc==SQLITE_OK ){
6095      if( rc!=SQLITE_OK ){
6096        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6097      }
6098      *pRc = rc;
6099    }
6100  }
6101}
6102
6103/* Reset the prepared statement created using shellPreparePrintf().
6104**
6105** This routine is could be marked "static".  But it is not always used,
6106** depending on compile-time options.  By omitting the "static", we avoid
6107** nuisance compiler warnings about "defined but not used".
6108*/
6109void shellReset(
6110  int *pRc,
6111  sqlite3_stmt *pStmt
6112){
6113  int rc = sqlite3_reset(pStmt);
6114  if( *pRc==SQLITE_OK ){
6115    if( rc!=SQLITE_OK ){
6116      sqlite3 *db = sqlite3_db_handle(pStmt);
6117      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6118    }
6119    *pRc = rc;
6120  }
6121}
6122#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6123
6124#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6125/******************************************************************************
6126** The ".archive" or ".ar" command.
6127*/
6128/*
6129** Structure representing a single ".ar" command.
6130*/
6131typedef struct ArCommand ArCommand;
6132struct ArCommand {
6133  u8 eCmd;                        /* An AR_CMD_* value */
6134  u8 bVerbose;                    /* True if --verbose */
6135  u8 bZip;                        /* True if the archive is a ZIP */
6136  u8 bDryRun;                     /* True if --dry-run */
6137  u8 bAppend;                     /* True if --append */
6138  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6139  int nArg;                       /* Number of command arguments */
6140  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6141  const char *zFile;              /* --file argument, or NULL */
6142  const char *zDir;               /* --directory argument, or NULL */
6143  char **azArg;                   /* Array of command arguments */
6144  ShellState *p;                  /* Shell state */
6145  sqlite3 *db;                    /* Database containing the archive */
6146};
6147
6148/*
6149** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6150*/
6151static int arUsage(FILE *f){
6152  showHelp(f,"archive");
6153  return SQLITE_ERROR;
6154}
6155
6156/*
6157** Print an error message for the .ar command to stderr and return
6158** SQLITE_ERROR.
6159*/
6160static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6161  va_list ap;
6162  char *z;
6163  va_start(ap, zFmt);
6164  z = sqlite3_vmprintf(zFmt, ap);
6165  va_end(ap);
6166  utf8_printf(stderr, "Error: %s\n", z);
6167  if( pAr->fromCmdLine ){
6168    utf8_printf(stderr, "Use \"-A\" for more help\n");
6169  }else{
6170    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6171  }
6172  sqlite3_free(z);
6173  return SQLITE_ERROR;
6174}
6175
6176/*
6177** Values for ArCommand.eCmd.
6178*/
6179#define AR_CMD_CREATE       1
6180#define AR_CMD_UPDATE       2
6181#define AR_CMD_INSERT       3
6182#define AR_CMD_EXTRACT      4
6183#define AR_CMD_LIST         5
6184#define AR_CMD_HELP         6
6185
6186/*
6187** Other (non-command) switches.
6188*/
6189#define AR_SWITCH_VERBOSE     7
6190#define AR_SWITCH_FILE        8
6191#define AR_SWITCH_DIRECTORY   9
6192#define AR_SWITCH_APPEND     10
6193#define AR_SWITCH_DRYRUN     11
6194
6195static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6196  switch( eSwitch ){
6197    case AR_CMD_CREATE:
6198    case AR_CMD_EXTRACT:
6199    case AR_CMD_LIST:
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_VERBOSE:
6213      pAr->bVerbose = 1;
6214      break;
6215    case AR_SWITCH_APPEND:
6216      pAr->bAppend = 1;
6217      /* Fall thru into --file */
6218    case AR_SWITCH_FILE:
6219      pAr->zFile = zArg;
6220      break;
6221    case AR_SWITCH_DIRECTORY:
6222      pAr->zDir = zArg;
6223      break;
6224  }
6225
6226  return SQLITE_OK;
6227}
6228
6229/*
6230** Parse the command line for an ".ar" command. The results are written into
6231** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6232** successfully, otherwise an error message is written to stderr and
6233** SQLITE_ERROR returned.
6234*/
6235static int arParseCommand(
6236  char **azArg,                   /* Array of arguments passed to dot command */
6237  int nArg,                       /* Number of entries in azArg[] */
6238  ArCommand *pAr                  /* Populate this object */
6239){
6240  struct ArSwitch {
6241    const char *zLong;
6242    char cShort;
6243    u8 eSwitch;
6244    u8 bArg;
6245  } aSwitch[] = {
6246    { "create",    'c', AR_CMD_CREATE,       0 },
6247    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6248    { "insert",    'i', AR_CMD_INSERT,       0 },
6249    { "list",      't', AR_CMD_LIST,         0 },
6250    { "update",    'u', AR_CMD_UPDATE,       0 },
6251    { "help",      'h', AR_CMD_HELP,         0 },
6252    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6253    { "file",      'f', AR_SWITCH_FILE,      1 },
6254    { "append",    'a', AR_SWITCH_APPEND,    1 },
6255    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6256    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6257  };
6258  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6259  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6260
6261  if( nArg<=1 ){
6262    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6263    return arUsage(stderr);
6264  }else{
6265    char *z = azArg[1];
6266    if( z[0]!='-' ){
6267      /* Traditional style [tar] invocation */
6268      int i;
6269      int iArg = 2;
6270      for(i=0; z[i]; i++){
6271        const char *zArg = 0;
6272        struct ArSwitch *pOpt;
6273        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6274          if( z[i]==pOpt->cShort ) break;
6275        }
6276        if( pOpt==pEnd ){
6277          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6278        }
6279        if( pOpt->bArg ){
6280          if( iArg>=nArg ){
6281            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6282          }
6283          zArg = azArg[iArg++];
6284        }
6285        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6286      }
6287      pAr->nArg = nArg-iArg;
6288      if( pAr->nArg>0 ){
6289        pAr->azArg = &azArg[iArg];
6290      }
6291    }else{
6292      /* Non-traditional invocation */
6293      int iArg;
6294      for(iArg=1; iArg<nArg; iArg++){
6295        int n;
6296        z = azArg[iArg];
6297        if( z[0]!='-' ){
6298          /* All remaining command line words are command arguments. */
6299          pAr->azArg = &azArg[iArg];
6300          pAr->nArg = nArg-iArg;
6301          break;
6302        }
6303        n = strlen30(z);
6304
6305        if( z[1]!='-' ){
6306          int i;
6307          /* One or more short options */
6308          for(i=1; i<n; i++){
6309            const char *zArg = 0;
6310            struct ArSwitch *pOpt;
6311            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6312              if( z[i]==pOpt->cShort ) break;
6313            }
6314            if( pOpt==pEnd ){
6315              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6316            }
6317            if( pOpt->bArg ){
6318              if( i<(n-1) ){
6319                zArg = &z[i+1];
6320                i = n;
6321              }else{
6322                if( iArg>=(nArg-1) ){
6323                  return arErrorMsg(pAr, "option requires an argument: %c",
6324                                    z[i]);
6325                }
6326                zArg = azArg[++iArg];
6327              }
6328            }
6329            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6330          }
6331        }else if( z[2]=='\0' ){
6332          /* A -- option, indicating that all remaining command line words
6333          ** are command arguments.  */
6334          pAr->azArg = &azArg[iArg+1];
6335          pAr->nArg = nArg-iArg-1;
6336          break;
6337        }else{
6338          /* A long option */
6339          const char *zArg = 0;             /* Argument for option, if any */
6340          struct ArSwitch *pMatch = 0;      /* Matching option */
6341          struct ArSwitch *pOpt;            /* Iterator */
6342          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6343            const char *zLong = pOpt->zLong;
6344            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6345              if( pMatch ){
6346                return arErrorMsg(pAr, "ambiguous option: %s",z);
6347              }else{
6348                pMatch = pOpt;
6349              }
6350            }
6351          }
6352
6353          if( pMatch==0 ){
6354            return arErrorMsg(pAr, "unrecognized option: %s", z);
6355          }
6356          if( pMatch->bArg ){
6357            if( iArg>=(nArg-1) ){
6358              return arErrorMsg(pAr, "option requires an argument: %s", z);
6359            }
6360            zArg = azArg[++iArg];
6361          }
6362          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6363        }
6364      }
6365    }
6366  }
6367
6368  return SQLITE_OK;
6369}
6370
6371/*
6372** This function assumes that all arguments within the ArCommand.azArg[]
6373** array refer to archive members, as for the --extract or --list commands.
6374** It checks that each of them are present. If any specified file is not
6375** present in the archive, an error is printed to stderr and an error
6376** code returned. Otherwise, if all specified arguments are present in
6377** the archive, SQLITE_OK is returned.
6378**
6379** This function strips any trailing '/' characters from each argument.
6380** This is consistent with the way the [tar] command seems to work on
6381** Linux.
6382*/
6383static int arCheckEntries(ArCommand *pAr){
6384  int rc = SQLITE_OK;
6385  if( pAr->nArg ){
6386    int i, j;
6387    sqlite3_stmt *pTest = 0;
6388
6389    shellPreparePrintf(pAr->db, &rc, &pTest,
6390        "SELECT name FROM %s WHERE name=$name",
6391        pAr->zSrcTable
6392    );
6393    j = sqlite3_bind_parameter_index(pTest, "$name");
6394    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6395      char *z = pAr->azArg[i];
6396      int n = strlen30(z);
6397      int bOk = 0;
6398      while( n>0 && z[n-1]=='/' ) n--;
6399      z[n] = '\0';
6400      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6401      if( SQLITE_ROW==sqlite3_step(pTest) ){
6402        bOk = 1;
6403      }
6404      shellReset(&rc, pTest);
6405      if( rc==SQLITE_OK && bOk==0 ){
6406        utf8_printf(stderr, "not found in archive: %s\n", z);
6407        rc = SQLITE_ERROR;
6408      }
6409    }
6410    shellFinalize(&rc, pTest);
6411  }
6412  return rc;
6413}
6414
6415/*
6416** Format a WHERE clause that can be used against the "sqlar" table to
6417** identify all archive members that match the command arguments held
6418** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6419** The caller is responsible for eventually calling sqlite3_free() on
6420** any non-NULL (*pzWhere) value.
6421*/
6422static void arWhereClause(
6423  int *pRc,
6424  ArCommand *pAr,
6425  char **pzWhere                  /* OUT: New WHERE clause */
6426){
6427  char *zWhere = 0;
6428  if( *pRc==SQLITE_OK ){
6429    if( pAr->nArg==0 ){
6430      zWhere = sqlite3_mprintf("1");
6431    }else{
6432      int i;
6433      const char *zSep = "";
6434      for(i=0; i<pAr->nArg; i++){
6435        const char *z = pAr->azArg[i];
6436        zWhere = sqlite3_mprintf(
6437          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6438          zWhere, zSep, z, strlen30(z)+1, z
6439        );
6440        if( zWhere==0 ){
6441          *pRc = SQLITE_NOMEM;
6442          break;
6443        }
6444        zSep = " OR ";
6445      }
6446    }
6447  }
6448  *pzWhere = zWhere;
6449}
6450
6451/*
6452** Implementation of .ar "lisT" command.
6453*/
6454static int arListCommand(ArCommand *pAr){
6455  const char *zSql = "SELECT %s FROM %s WHERE %s";
6456  const char *azCols[] = {
6457    "name",
6458    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6459  };
6460
6461  char *zWhere = 0;
6462  sqlite3_stmt *pSql = 0;
6463  int rc;
6464
6465  rc = arCheckEntries(pAr);
6466  arWhereClause(&rc, pAr, &zWhere);
6467
6468  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6469                     pAr->zSrcTable, zWhere);
6470  if( pAr->bDryRun ){
6471    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6472  }else{
6473    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6474      if( pAr->bVerbose ){
6475        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6476            sqlite3_column_text(pSql, 0),
6477            sqlite3_column_int(pSql, 1),
6478            sqlite3_column_text(pSql, 2),
6479            sqlite3_column_text(pSql, 3)
6480        );
6481      }else{
6482        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6483      }
6484    }
6485  }
6486  shellFinalize(&rc, pSql);
6487  sqlite3_free(zWhere);
6488  return rc;
6489}
6490
6491
6492/*
6493** Implementation of .ar "eXtract" command.
6494*/
6495static int arExtractCommand(ArCommand *pAr){
6496  const char *zSql1 =
6497    "SELECT "
6498    " ($dir || name),"
6499    " writefile(($dir || name), %s, mode, mtime) "
6500    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6501    " AND name NOT GLOB '*..[/\\]*'";
6502
6503  const char *azExtraArg[] = {
6504    "sqlar_uncompress(data, sz)",
6505    "data"
6506  };
6507
6508  sqlite3_stmt *pSql = 0;
6509  int rc = SQLITE_OK;
6510  char *zDir = 0;
6511  char *zWhere = 0;
6512  int i, j;
6513
6514  /* If arguments are specified, check that they actually exist within
6515  ** the archive before proceeding. And formulate a WHERE clause to
6516  ** match them.  */
6517  rc = arCheckEntries(pAr);
6518  arWhereClause(&rc, pAr, &zWhere);
6519
6520  if( rc==SQLITE_OK ){
6521    if( pAr->zDir ){
6522      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6523    }else{
6524      zDir = sqlite3_mprintf("");
6525    }
6526    if( zDir==0 ) rc = SQLITE_NOMEM;
6527  }
6528
6529  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6530      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6531  );
6532
6533  if( rc==SQLITE_OK ){
6534    j = sqlite3_bind_parameter_index(pSql, "$dir");
6535    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6536
6537    /* Run the SELECT statement twice. The first time, writefile() is called
6538    ** for all archive members that should be extracted. The second time,
6539    ** only for the directories. This is because the timestamps for
6540    ** extracted directories must be reset after they are populated (as
6541    ** populating them changes the timestamp).  */
6542    for(i=0; i<2; i++){
6543      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6544      sqlite3_bind_int(pSql, j, i);
6545      if( pAr->bDryRun ){
6546        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6547      }else{
6548        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6549          if( i==0 && pAr->bVerbose ){
6550            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6551          }
6552        }
6553      }
6554      shellReset(&rc, pSql);
6555    }
6556    shellFinalize(&rc, pSql);
6557  }
6558
6559  sqlite3_free(zDir);
6560  sqlite3_free(zWhere);
6561  return rc;
6562}
6563
6564/*
6565** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6566*/
6567static int arExecSql(ArCommand *pAr, const char *zSql){
6568  int rc;
6569  if( pAr->bDryRun ){
6570    utf8_printf(pAr->p->out, "%s\n", zSql);
6571    rc = SQLITE_OK;
6572  }else{
6573    char *zErr = 0;
6574    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6575    if( zErr ){
6576      utf8_printf(stdout, "ERROR: %s\n", zErr);
6577      sqlite3_free(zErr);
6578    }
6579  }
6580  return rc;
6581}
6582
6583
6584/*
6585** Implementation of .ar "create", "insert", and "update" commands.
6586**
6587**     create    ->     Create a new SQL archive
6588**     insert    ->     Insert or reinsert all files listed
6589**     update    ->     Insert files that have changed or that were not
6590**                      previously in the archive
6591**
6592** Create the "sqlar" table in the database if it does not already exist.
6593** Then add each file in the azFile[] array to the archive. Directories
6594** are added recursively. If argument bVerbose is non-zero, a message is
6595** printed on stdout for each file archived.
6596**
6597** The create command is the same as update, except that it drops
6598** any existing "sqlar" table before beginning.  The "insert" command
6599** always overwrites every file named on the command-line, where as
6600** "update" only overwrites if the size or mtime or mode has changed.
6601*/
6602static int arCreateOrUpdateCommand(
6603  ArCommand *pAr,                 /* Command arguments and options */
6604  int bUpdate,                    /* true for a --create. */
6605  int bOnlyIfChanged              /* Only update if file has changed */
6606){
6607  const char *zCreate =
6608      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6609      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6610      "  mode INT,               -- access permissions\n"
6611      "  mtime INT,              -- last modification time\n"
6612      "  sz INT,                 -- original file size\n"
6613      "  data BLOB               -- compressed content\n"
6614      ")";
6615  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6616  const char *zInsertFmt[2] = {
6617     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6618     "  SELECT\n"
6619     "    %s,\n"
6620     "    mode,\n"
6621     "    mtime,\n"
6622     "    CASE substr(lsmode(mode),1,1)\n"
6623     "      WHEN '-' THEN length(data)\n"
6624     "      WHEN 'd' THEN 0\n"
6625     "      ELSE -1 END,\n"
6626     "    sqlar_compress(data)\n"
6627     "  FROM fsdir(%Q,%Q) AS disk\n"
6628     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6629     ,
6630     "REPLACE INTO %s(name,mode,mtime,data)\n"
6631     "  SELECT\n"
6632     "    %s,\n"
6633     "    mode,\n"
6634     "    mtime,\n"
6635     "    data\n"
6636     "  FROM fsdir(%Q,%Q) AS disk\n"
6637     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6638  };
6639  int i;                          /* For iterating through azFile[] */
6640  int rc;                         /* Return code */
6641  const char *zTab = 0;           /* SQL table into which to insert */
6642  char *zSql;
6643  char zTemp[50];
6644  char *zExists = 0;
6645
6646  arExecSql(pAr, "PRAGMA page_size=512");
6647  rc = arExecSql(pAr, "SAVEPOINT ar;");
6648  if( rc!=SQLITE_OK ) return rc;
6649  zTemp[0] = 0;
6650  if( pAr->bZip ){
6651    /* Initialize the zipfile virtual table, if necessary */
6652    if( pAr->zFile ){
6653      sqlite3_uint64 r;
6654      sqlite3_randomness(sizeof(r),&r);
6655      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6656      zTab = zTemp;
6657      zSql = sqlite3_mprintf(
6658         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6659         zTab, pAr->zFile
6660      );
6661      rc = arExecSql(pAr, zSql);
6662      sqlite3_free(zSql);
6663    }else{
6664      zTab = "zip";
6665    }
6666  }else{
6667    /* Initialize the table for an SQLAR */
6668    zTab = "sqlar";
6669    if( bUpdate==0 ){
6670      rc = arExecSql(pAr, zDrop);
6671      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6672    }
6673    rc = arExecSql(pAr, zCreate);
6674  }
6675  if( bOnlyIfChanged ){
6676    zExists = sqlite3_mprintf(
6677      " AND NOT EXISTS("
6678          "SELECT 1 FROM %s AS mem"
6679          " WHERE mem.name=disk.name"
6680          " AND mem.mtime=disk.mtime"
6681          " AND mem.mode=disk.mode)", zTab);
6682  }else{
6683    zExists = sqlite3_mprintf("");
6684  }
6685  if( zExists==0 ) rc = SQLITE_NOMEM;
6686  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6687    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6688        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6689        pAr->azArg[i], pAr->zDir, zExists);
6690    rc = arExecSql(pAr, zSql2);
6691    sqlite3_free(zSql2);
6692  }
6693end_ar_transaction:
6694  if( rc!=SQLITE_OK ){
6695    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6696  }else{
6697    rc = arExecSql(pAr, "RELEASE ar;");
6698    if( pAr->bZip && pAr->zFile ){
6699      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6700      arExecSql(pAr, zSql);
6701      sqlite3_free(zSql);
6702    }
6703  }
6704  sqlite3_free(zExists);
6705  return rc;
6706}
6707
6708/*
6709** Implementation of ".ar" dot command.
6710*/
6711static int arDotCommand(
6712  ShellState *pState,          /* Current shell tool state */
6713  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6714  char **azArg,                /* Array of arguments passed to dot command */
6715  int nArg                     /* Number of entries in azArg[] */
6716){
6717  ArCommand cmd;
6718  int rc;
6719  memset(&cmd, 0, sizeof(cmd));
6720  cmd.fromCmdLine = fromCmdLine;
6721  rc = arParseCommand(azArg, nArg, &cmd);
6722  if( rc==SQLITE_OK ){
6723    int eDbType = SHELL_OPEN_UNSPEC;
6724    cmd.p = pState;
6725    cmd.db = pState->db;
6726    if( cmd.zFile ){
6727      eDbType = deduceDatabaseType(cmd.zFile, 1);
6728    }else{
6729      eDbType = pState->openMode;
6730    }
6731    if( eDbType==SHELL_OPEN_ZIPFILE ){
6732      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6733        if( cmd.zFile==0 ){
6734          cmd.zSrcTable = sqlite3_mprintf("zip");
6735        }else{
6736          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6737        }
6738      }
6739      cmd.bZip = 1;
6740    }else if( cmd.zFile ){
6741      int flags;
6742      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6743      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6744           || cmd.eCmd==AR_CMD_UPDATE ){
6745        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6746      }else{
6747        flags = SQLITE_OPEN_READONLY;
6748      }
6749      cmd.db = 0;
6750      if( cmd.bDryRun ){
6751        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6752             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6753      }
6754      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6755             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6756      if( rc!=SQLITE_OK ){
6757        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6758            cmd.zFile, sqlite3_errmsg(cmd.db)
6759        );
6760        goto end_ar_command;
6761      }
6762      sqlite3_fileio_init(cmd.db, 0, 0);
6763      sqlite3_sqlar_init(cmd.db, 0, 0);
6764      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6765                              shellPutsFunc, 0, 0);
6766
6767    }
6768    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6769      if( cmd.eCmd!=AR_CMD_CREATE
6770       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6771      ){
6772        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6773        rc = SQLITE_ERROR;
6774        goto end_ar_command;
6775      }
6776      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6777    }
6778
6779    switch( cmd.eCmd ){
6780      case AR_CMD_CREATE:
6781        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6782        break;
6783
6784      case AR_CMD_EXTRACT:
6785        rc = arExtractCommand(&cmd);
6786        break;
6787
6788      case AR_CMD_LIST:
6789        rc = arListCommand(&cmd);
6790        break;
6791
6792      case AR_CMD_HELP:
6793        arUsage(pState->out);
6794        break;
6795
6796      case AR_CMD_INSERT:
6797        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6798        break;
6799
6800      default:
6801        assert( cmd.eCmd==AR_CMD_UPDATE );
6802        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6803        break;
6804    }
6805  }
6806end_ar_command:
6807  if( cmd.db!=pState->db ){
6808    close_db(cmd.db);
6809  }
6810  sqlite3_free(cmd.zSrcTable);
6811
6812  return rc;
6813}
6814/* End of the ".archive" or ".ar" command logic
6815*******************************************************************************/
6816#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6817
6818#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6819/*
6820** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6821** Otherwise, the SQL statement or statements in zSql are executed using
6822** database connection db and the error code written to *pRc before
6823** this function returns.
6824*/
6825static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6826  int rc = *pRc;
6827  if( rc==SQLITE_OK ){
6828    char *zErr = 0;
6829    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6830    if( rc!=SQLITE_OK ){
6831      raw_printf(stderr, "SQL error: %s\n", zErr);
6832    }
6833    sqlite3_free(zErr);
6834    *pRc = rc;
6835  }
6836}
6837
6838/*
6839** Like shellExec(), except that zFmt is a printf() style format string.
6840*/
6841static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6842  char *z = 0;
6843  if( *pRc==SQLITE_OK ){
6844    va_list ap;
6845    va_start(ap, zFmt);
6846    z = sqlite3_vmprintf(zFmt, ap);
6847    va_end(ap);
6848    if( z==0 ){
6849      *pRc = SQLITE_NOMEM;
6850    }else{
6851      shellExec(db, pRc, z);
6852    }
6853    sqlite3_free(z);
6854  }
6855}
6856
6857/*
6858** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6859** Otherwise, an attempt is made to allocate, zero and return a pointer
6860** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6861** to SQLITE_NOMEM and NULL returned.
6862*/
6863static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6864  void *pRet = 0;
6865  if( *pRc==SQLITE_OK ){
6866    pRet = sqlite3_malloc64(nByte);
6867    if( pRet==0 ){
6868      *pRc = SQLITE_NOMEM;
6869    }else{
6870      memset(pRet, 0, nByte);
6871    }
6872  }
6873  return pRet;
6874}
6875
6876/*
6877** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6878** Otherwise, zFmt is treated as a printf() style string. The result of
6879** formatting it along with any trailing arguments is written into a
6880** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6881** It is the responsibility of the caller to eventually free this buffer
6882** using a call to sqlite3_free().
6883**
6884** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6885** pointer returned.
6886*/
6887static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6888  char *z = 0;
6889  if( *pRc==SQLITE_OK ){
6890    va_list ap;
6891    va_start(ap, zFmt);
6892    z = sqlite3_vmprintf(zFmt, ap);
6893    va_end(ap);
6894    if( z==0 ){
6895      *pRc = SQLITE_NOMEM;
6896    }
6897  }
6898  return z;
6899}
6900
6901/*
6902** When running the ".recover" command, each output table, and the special
6903** orphaned row table if it is required, is represented by an instance
6904** of the following struct.
6905*/
6906typedef struct RecoverTable RecoverTable;
6907struct RecoverTable {
6908  char *zQuoted;                  /* Quoted version of table name */
6909  int nCol;                       /* Number of columns in table */
6910  char **azlCol;                  /* Array of column lists */
6911  int iPk;                        /* Index of IPK column */
6912};
6913
6914/*
6915** Free a RecoverTable object allocated by recoverFindTable() or
6916** recoverOrphanTable().
6917*/
6918static void recoverFreeTable(RecoverTable *pTab){
6919  if( pTab ){
6920    sqlite3_free(pTab->zQuoted);
6921    if( pTab->azlCol ){
6922      int i;
6923      for(i=0; i<=pTab->nCol; i++){
6924        sqlite3_free(pTab->azlCol[i]);
6925      }
6926      sqlite3_free(pTab->azlCol);
6927    }
6928    sqlite3_free(pTab);
6929  }
6930}
6931
6932/*
6933** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6934** Otherwise, it allocates and returns a RecoverTable object based on the
6935** final four arguments passed to this function. It is the responsibility
6936** of the caller to eventually free the returned object using
6937** recoverFreeTable().
6938*/
6939static RecoverTable *recoverNewTable(
6940  int *pRc,                       /* IN/OUT: Error code */
6941  const char *zName,              /* Name of table */
6942  const char *zSql,               /* CREATE TABLE statement */
6943  int bIntkey,
6944  int nCol
6945){
6946  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6947  int rc = *pRc;
6948  RecoverTable *pTab = 0;
6949
6950  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6951  if( rc==SQLITE_OK ){
6952    int nSqlCol = 0;
6953    int bSqlIntkey = 0;
6954    sqlite3_stmt *pStmt = 0;
6955
6956    rc = sqlite3_open("", &dbtmp);
6957    if( rc==SQLITE_OK ){
6958      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6959                              shellIdQuote, 0, 0);
6960    }
6961    if( rc==SQLITE_OK ){
6962      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6963    }
6964    if( rc==SQLITE_OK ){
6965      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6966      if( rc==SQLITE_ERROR ){
6967        rc = SQLITE_OK;
6968        goto finished;
6969      }
6970    }
6971    shellPreparePrintf(dbtmp, &rc, &pStmt,
6972        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6973    );
6974    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6975      nSqlCol = sqlite3_column_int(pStmt, 0);
6976    }
6977    shellFinalize(&rc, pStmt);
6978
6979    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6980      goto finished;
6981    }
6982
6983    shellPreparePrintf(dbtmp, &rc, &pStmt,
6984      "SELECT ("
6985      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6986      ") FROM sqlite_schema WHERE name = %Q", zName
6987    );
6988    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6989      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6990    }
6991    shellFinalize(&rc, pStmt);
6992
6993    if( bIntkey==bSqlIntkey ){
6994      int i;
6995      const char *zPk = "_rowid_";
6996      sqlite3_stmt *pPkFinder = 0;
6997
6998      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6999      ** set zPk to the name of the PK column, and pTab->iPk to the index
7000      ** of the column, where columns are 0-numbered from left to right.
7001      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
7002      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
7003      pTab->iPk = -2;
7004      if( bIntkey ){
7005        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
7006          "SELECT cid, name FROM pragma_table_info(%Q) "
7007          "  WHERE pk=1 AND type='integer' COLLATE nocase"
7008          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
7009          , zName, zName
7010        );
7011        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
7012          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
7013          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
7014        }
7015      }
7016
7017      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
7018      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
7019      pTab->nCol = nSqlCol;
7020
7021      if( bIntkey ){
7022        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
7023      }else{
7024        pTab->azlCol[0] = shellMPrintf(&rc, "");
7025      }
7026      i = 1;
7027      shellPreparePrintf(dbtmp, &rc, &pStmt,
7028          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
7029          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
7030          "FROM pragma_table_info(%Q)",
7031          bIntkey ? ", " : "", pTab->iPk,
7032          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
7033          zName
7034      );
7035      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7036        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
7037        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
7038        i++;
7039      }
7040      shellFinalize(&rc, pStmt);
7041
7042      shellFinalize(&rc, pPkFinder);
7043    }
7044  }
7045
7046 finished:
7047  sqlite3_close(dbtmp);
7048  *pRc = rc;
7049  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
7050    recoverFreeTable(pTab);
7051    pTab = 0;
7052  }
7053  return pTab;
7054}
7055
7056/*
7057** This function is called to search the schema recovered from the
7058** sqlite_schema table of the (possibly) corrupt database as part
7059** of a ".recover" command. Specifically, for a table with root page
7060** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
7061** table must be a WITHOUT ROWID table, or if non-zero, not one of
7062** those.
7063**
7064** If a table is found, a (RecoverTable*) object is returned. Or, if
7065** no such table is found, but bIntkey is false and iRoot is the
7066** root page of an index in the recovered schema, then (*pbNoop) is
7067** set to true and NULL returned. Or, if there is no such table or
7068** index, NULL is returned and (*pbNoop) set to 0, indicating that
7069** the caller should write data to the orphans table.
7070*/
7071static RecoverTable *recoverFindTable(
7072  ShellState *pState,             /* Shell state object */
7073  int *pRc,                       /* IN/OUT: Error code */
7074  int iRoot,                      /* Root page of table */
7075  int bIntkey,                    /* True for an intkey table */
7076  int nCol,                       /* Number of columns in table */
7077  int *pbNoop                     /* OUT: True if iRoot is root of index */
7078){
7079  sqlite3_stmt *pStmt = 0;
7080  RecoverTable *pRet = 0;
7081  int bNoop = 0;
7082  const char *zSql = 0;
7083  const char *zName = 0;
7084
7085  /* Search the recovered schema for an object with root page iRoot. */
7086  shellPreparePrintf(pState->db, pRc, &pStmt,
7087      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
7088  );
7089  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7090    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
7091    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
7092      bNoop = 1;
7093      break;
7094    }
7095    if( sqlite3_stricmp(zType, "table")==0 ){
7096      zName = (const char*)sqlite3_column_text(pStmt, 1);
7097      zSql = (const char*)sqlite3_column_text(pStmt, 2);
7098      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
7099      break;
7100    }
7101  }
7102
7103  shellFinalize(pRc, pStmt);
7104  *pbNoop = bNoop;
7105  return pRet;
7106}
7107
7108/*
7109** Return a RecoverTable object representing the orphans table.
7110*/
7111static RecoverTable *recoverOrphanTable(
7112  ShellState *pState,             /* Shell state object */
7113  int *pRc,                       /* IN/OUT: Error code */
7114  const char *zLostAndFound,      /* Base name for orphans table */
7115  int nCol                        /* Number of user data columns */
7116){
7117  RecoverTable *pTab = 0;
7118  if( nCol>=0 && *pRc==SQLITE_OK ){
7119    int i;
7120
7121    /* This block determines the name of the orphan table. The prefered
7122    ** name is zLostAndFound. But if that clashes with another name
7123    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7124    ** and so on until a non-clashing name is found.  */
7125    int iTab = 0;
7126    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7127    sqlite3_stmt *pTest = 0;
7128    shellPrepare(pState->db, pRc,
7129        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7130    );
7131    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7132    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7133      shellReset(pRc, pTest);
7134      sqlite3_free(zTab);
7135      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7136      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7137    }
7138    shellFinalize(pRc, pTest);
7139
7140    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7141    if( pTab ){
7142      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7143      pTab->nCol = nCol;
7144      pTab->iPk = -2;
7145      if( nCol>0 ){
7146        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7147        if( pTab->azlCol ){
7148          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7149          for(i=nCol-1; i>=0; i--){
7150            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7151          }
7152        }
7153      }
7154
7155      if( *pRc!=SQLITE_OK ){
7156        recoverFreeTable(pTab);
7157        pTab = 0;
7158      }else{
7159        raw_printf(pState->out,
7160            "CREATE TABLE %s(rootpgno INTEGER, "
7161            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7162        );
7163        for(i=0; i<nCol; i++){
7164          raw_printf(pState->out, ", c%d", i);
7165        }
7166        raw_printf(pState->out, ");\n");
7167      }
7168    }
7169    sqlite3_free(zTab);
7170  }
7171  return pTab;
7172}
7173
7174/*
7175** This function is called to recover data from the database. A script
7176** to construct a new database containing all recovered data is output
7177** on stream pState->out.
7178*/
7179static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7180  int rc = SQLITE_OK;
7181  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7182  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7183  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7184  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7185  const char *zLostAndFound = "lost_and_found";
7186  int i;
7187  int nOrphan = -1;
7188  RecoverTable *pOrphan = 0;
7189
7190  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7191  int bRowids = 1;                /* 0 if --no-rowids */
7192  for(i=1; i<nArg; i++){
7193    char *z = azArg[i];
7194    int n;
7195    if( z[0]=='-' && z[1]=='-' ) z++;
7196    n = strlen30(z);
7197    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7198      bFreelist = 0;
7199    }else
7200    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7201      i++;
7202      zRecoveryDb = azArg[i];
7203    }else
7204    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7205      i++;
7206      zLostAndFound = azArg[i];
7207    }else
7208    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7209      bRowids = 0;
7210    }
7211    else{
7212      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7213      showHelp(pState->out, azArg[0]);
7214      return 1;
7215    }
7216  }
7217
7218  shellExecPrintf(pState->db, &rc,
7219    /* Attach an in-memory database named 'recovery'. Create an indexed
7220    ** cache of the sqlite_dbptr virtual table. */
7221    "PRAGMA writable_schema = on;"
7222    "ATTACH %Q AS recovery;"
7223    "DROP TABLE IF EXISTS recovery.dbptr;"
7224    "DROP TABLE IF EXISTS recovery.freelist;"
7225    "DROP TABLE IF EXISTS recovery.map;"
7226    "DROP TABLE IF EXISTS recovery.schema;"
7227    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7228  );
7229
7230  if( bFreelist ){
7231    shellExec(pState->db, &rc,
7232      "WITH trunk(pgno) AS ("
7233      "  SELECT shell_int32("
7234      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7235      "      WHERE x>0"
7236      "    UNION"
7237      "  SELECT shell_int32("
7238      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7239      "      FROM trunk WHERE x>0"
7240      "),"
7241      "freelist(data, n, freepgno) AS ("
7242      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7243      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7244      "    UNION ALL"
7245      "  SELECT data, n-1, shell_int32(data, 2+n) "
7246      "      FROM freelist WHERE n>=0"
7247      ")"
7248      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7249    );
7250  }
7251
7252  /* If this is an auto-vacuum database, add all pointer-map pages to
7253  ** the freelist table. Do this regardless of whether or not
7254  ** --freelist-corrupt was specified.  */
7255  shellExec(pState->db, &rc,
7256    "WITH ptrmap(pgno) AS ("
7257    "  SELECT 2 WHERE shell_int32("
7258    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7259    "  )"
7260    "    UNION ALL "
7261    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7262    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7263    ")"
7264    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7265  );
7266
7267  shellExec(pState->db, &rc,
7268    "CREATE TABLE recovery.dbptr("
7269    "      pgno, child, PRIMARY KEY(child, pgno)"
7270    ") WITHOUT ROWID;"
7271    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7272    "    SELECT * FROM sqlite_dbptr"
7273    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7274
7275    /* Delete any pointer to page 1. This ensures that page 1 is considered
7276    ** a root page, regardless of how corrupt the db is. */
7277    "DELETE FROM recovery.dbptr WHERE child = 1;"
7278
7279    /* Delete all pointers to any pages that have more than one pointer
7280    ** to them. Such pages will be treated as root pages when recovering
7281    ** data.  */
7282    "DELETE FROM recovery.dbptr WHERE child IN ("
7283    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7284    ");"
7285
7286    /* Create the "map" table that will (eventually) contain instructions
7287    ** for dealing with each page in the db that contains one or more
7288    ** records. */
7289    "CREATE TABLE recovery.map("
7290      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7291    ");"
7292
7293    /* Populate table [map]. If there are circular loops of pages in the
7294    ** database, the following adds all pages in such a loop to the map
7295    ** as individual root pages. This could be handled better.  */
7296    "WITH pages(i, maxlen) AS ("
7297    "  SELECT page_count, ("
7298    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7299    "  ) FROM pragma_page_count WHERE page_count>0"
7300    "    UNION ALL"
7301    "  SELECT i-1, ("
7302    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7303    "  ) FROM pages WHERE i>=2"
7304    ")"
7305    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7306    "  SELECT i, maxlen, NULL, ("
7307    "    WITH p(orig, pgno, parent) AS ("
7308    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7309    "        UNION "
7310    "      SELECT i, p.parent, "
7311    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7312    "    )"
7313    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7314    ") "
7315    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7316    "UPDATE recovery.map AS o SET intkey = ("
7317    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7318    ");"
7319
7320    /* Extract data from page 1 and any linked pages into table
7321    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7322    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7323    "INSERT INTO recovery.schema SELECT "
7324    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7325    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7326    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7327    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7328    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7329    "FROM sqlite_dbdata WHERE pgno IN ("
7330    "  SELECT pgno FROM recovery.map WHERE root=1"
7331    ")"
7332    "GROUP BY pgno, cell;"
7333    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7334  );
7335
7336  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7337  ** CREATE TABLE statements that extracted from the existing schema.  */
7338  if( rc==SQLITE_OK ){
7339    sqlite3_stmt *pStmt = 0;
7340    /* ".recover" might output content in an order which causes immediate
7341    ** foreign key constraints to be violated. So disable foreign-key
7342    ** constraint enforcement to prevent problems when running the output
7343    ** script. */
7344    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7345    raw_printf(pState->out, "BEGIN;\n");
7346    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7347    shellPrepare(pState->db, &rc,
7348        "SELECT sql FROM recovery.schema "
7349        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7350    );
7351    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7352      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7353      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7354          &zCreateTable[12]
7355      );
7356    }
7357    shellFinalize(&rc, pStmt);
7358  }
7359
7360  /* Figure out if an orphan table will be required. And if so, how many
7361  ** user columns it should contain */
7362  shellPrepare(pState->db, &rc,
7363      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7364      , &pLoop
7365  );
7366  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7367    nOrphan = sqlite3_column_int(pLoop, 0);
7368  }
7369  shellFinalize(&rc, pLoop);
7370  pLoop = 0;
7371
7372  shellPrepare(pState->db, &rc,
7373      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7374  );
7375
7376  shellPrepare(pState->db, &rc,
7377      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7378      "(case when (? AND field<0) then NULL else value end)"
7379      "), ', ')"
7380      ", min(field) "
7381      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7382      "GROUP BY cell", &pCells
7383  );
7384
7385  /* Loop through each root page. */
7386  shellPrepare(pState->db, &rc,
7387      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7388      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7389      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7390      ")", &pLoop
7391  );
7392  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7393    int iRoot = sqlite3_column_int(pLoop, 0);
7394    int bIntkey = sqlite3_column_int(pLoop, 1);
7395    int nCol = sqlite3_column_int(pLoop, 2);
7396    int bNoop = 0;
7397    RecoverTable *pTab;
7398
7399    assert( bIntkey==0 || bIntkey==1 );
7400    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7401    if( bNoop || rc ) continue;
7402    if( pTab==0 ){
7403      if( pOrphan==0 ){
7404        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7405      }
7406      pTab = pOrphan;
7407      if( pTab==0 ) break;
7408    }
7409
7410    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7411      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7412    }
7413    sqlite3_bind_int(pPages, 1, iRoot);
7414    if( bRowids==0 && pTab->iPk<0 ){
7415      sqlite3_bind_int(pCells, 1, 1);
7416    }else{
7417      sqlite3_bind_int(pCells, 1, 0);
7418    }
7419    sqlite3_bind_int(pCells, 3, pTab->iPk);
7420
7421    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7422      int iPgno = sqlite3_column_int(pPages, 0);
7423      sqlite3_bind_int(pCells, 2, iPgno);
7424      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7425        int nField = sqlite3_column_int(pCells, 0);
7426        int iMin = sqlite3_column_int(pCells, 2);
7427        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7428
7429        RecoverTable *pTab2 = pTab;
7430        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7431          if( pOrphan==0 ){
7432            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7433          }
7434          pTab2 = pOrphan;
7435          if( pTab2==0 ) break;
7436        }
7437
7438        nField = nField+1;
7439        if( pTab2==pOrphan ){
7440          raw_printf(pState->out,
7441              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7442              pTab2->zQuoted, iRoot, iPgno, nField,
7443              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7444          );
7445        }else{
7446          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7447              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7448          );
7449        }
7450      }
7451      shellReset(&rc, pCells);
7452    }
7453    shellReset(&rc, pPages);
7454    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7455  }
7456  shellFinalize(&rc, pLoop);
7457  shellFinalize(&rc, pPages);
7458  shellFinalize(&rc, pCells);
7459  recoverFreeTable(pOrphan);
7460
7461  /* The rest of the schema */
7462  if( rc==SQLITE_OK ){
7463    sqlite3_stmt *pStmt = 0;
7464    shellPrepare(pState->db, &rc,
7465        "SELECT sql, name FROM recovery.schema "
7466        "WHERE sql NOT LIKE 'create table%'", &pStmt
7467    );
7468    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7469      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7470      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7471        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7472        char *zPrint = shellMPrintf(&rc,
7473          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7474          zName, zName, zSql
7475        );
7476        raw_printf(pState->out, "%s;\n", zPrint);
7477        sqlite3_free(zPrint);
7478      }else{
7479        raw_printf(pState->out, "%s;\n", zSql);
7480      }
7481    }
7482    shellFinalize(&rc, pStmt);
7483  }
7484
7485  if( rc==SQLITE_OK ){
7486    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7487    raw_printf(pState->out, "COMMIT;\n");
7488  }
7489  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7490  return rc;
7491}
7492#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7493
7494/*
7495** If an input line begins with "." then invoke this routine to
7496** process that line.
7497**
7498** Return 1 on error, 2 to exit, and 0 otherwise.
7499*/
7500static int do_meta_command(char *zLine, ShellState *p){
7501  int h = 1;
7502  int nArg = 0;
7503  int n, c;
7504  int rc = 0;
7505  char *azArg[52];
7506
7507#ifndef SQLITE_OMIT_VIRTUALTABLE
7508  if( p->expert.pExpert ){
7509    expertFinish(p, 1, 0);
7510  }
7511#endif
7512
7513  /* Parse the input line into tokens.
7514  */
7515  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7516    while( IsSpace(zLine[h]) ){ h++; }
7517    if( zLine[h]==0 ) break;
7518    if( zLine[h]=='\'' || zLine[h]=='"' ){
7519      int delim = zLine[h++];
7520      azArg[nArg++] = &zLine[h];
7521      while( zLine[h] && zLine[h]!=delim ){
7522        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7523        h++;
7524      }
7525      if( zLine[h]==delim ){
7526        zLine[h++] = 0;
7527      }
7528      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7529    }else{
7530      azArg[nArg++] = &zLine[h];
7531      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7532      if( zLine[h] ) zLine[h++] = 0;
7533      resolve_backslashes(azArg[nArg-1]);
7534    }
7535  }
7536  azArg[nArg] = 0;
7537
7538  /* Process the input line.
7539  */
7540  if( nArg==0 ) return 0; /* no tokens, no error */
7541  n = strlen30(azArg[0]);
7542  c = azArg[0][0];
7543  clearTempFile(p);
7544
7545#ifndef SQLITE_OMIT_AUTHORIZATION
7546  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7547    if( nArg!=2 ){
7548      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7549      rc = 1;
7550      goto meta_command_exit;
7551    }
7552    open_db(p, 0);
7553    if( booleanValue(azArg[1]) ){
7554      sqlite3_set_authorizer(p->db, shellAuth, p);
7555    }else if( p->bSafeModePersist ){
7556      sqlite3_set_authorizer(p->db, safeModeAuth, p);
7557    }else{
7558      sqlite3_set_authorizer(p->db, 0, 0);
7559    }
7560  }else
7561#endif
7562
7563#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7564  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7565    open_db(p, 0);
7566    failIfSafeMode(p, "cannot run .archive in safe mode");
7567    rc = arDotCommand(p, 0, azArg, nArg);
7568  }else
7569#endif
7570
7571  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7572   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7573  ){
7574    const char *zDestFile = 0;
7575    const char *zDb = 0;
7576    sqlite3 *pDest;
7577    sqlite3_backup *pBackup;
7578    int j;
7579    int bAsync = 0;
7580    const char *zVfs = 0;
7581    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
7582    for(j=1; j<nArg; j++){
7583      const char *z = azArg[j];
7584      if( z[0]=='-' ){
7585        if( z[1]=='-' ) z++;
7586        if( strcmp(z, "-append")==0 ){
7587          zVfs = "apndvfs";
7588        }else
7589        if( strcmp(z, "-async")==0 ){
7590          bAsync = 1;
7591        }else
7592        {
7593          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7594          return 1;
7595        }
7596      }else if( zDestFile==0 ){
7597        zDestFile = azArg[j];
7598      }else if( zDb==0 ){
7599        zDb = zDestFile;
7600        zDestFile = azArg[j];
7601      }else{
7602        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7603        return 1;
7604      }
7605    }
7606    if( zDestFile==0 ){
7607      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7608      return 1;
7609    }
7610    if( zDb==0 ) zDb = "main";
7611    rc = sqlite3_open_v2(zDestFile, &pDest,
7612                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7613    if( rc!=SQLITE_OK ){
7614      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7615      close_db(pDest);
7616      return 1;
7617    }
7618    if( bAsync ){
7619      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7620                   0, 0, 0);
7621    }
7622    open_db(p, 0);
7623    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7624    if( pBackup==0 ){
7625      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7626      close_db(pDest);
7627      return 1;
7628    }
7629    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7630    sqlite3_backup_finish(pBackup);
7631    if( rc==SQLITE_DONE ){
7632      rc = 0;
7633    }else{
7634      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7635      rc = 1;
7636    }
7637    close_db(pDest);
7638  }else
7639
7640  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7641    if( nArg==2 ){
7642      bail_on_error = booleanValue(azArg[1]);
7643    }else{
7644      raw_printf(stderr, "Usage: .bail on|off\n");
7645      rc = 1;
7646    }
7647  }else
7648
7649  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7650    if( nArg==2 ){
7651      if( booleanValue(azArg[1]) ){
7652        setBinaryMode(p->out, 1);
7653      }else{
7654        setTextMode(p->out, 1);
7655      }
7656    }else{
7657      raw_printf(stderr, "Usage: .binary on|off\n");
7658      rc = 1;
7659    }
7660  }else
7661
7662  /* The undocumented ".breakpoint" command causes a call to the no-op
7663  ** routine named test_breakpoint().
7664  */
7665  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7666    test_breakpoint();
7667  }else
7668
7669  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7670    failIfSafeMode(p, "cannot run .cd in safe mode");
7671    if( nArg==2 ){
7672#if defined(_WIN32) || defined(WIN32)
7673      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7674      rc = !SetCurrentDirectoryW(z);
7675      sqlite3_free(z);
7676#else
7677      rc = chdir(azArg[1]);
7678#endif
7679      if( rc ){
7680        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7681        rc = 1;
7682      }
7683    }else{
7684      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7685      rc = 1;
7686    }
7687  }else
7688
7689  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7690    if( nArg==2 ){
7691      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7692    }else{
7693      raw_printf(stderr, "Usage: .changes on|off\n");
7694      rc = 1;
7695    }
7696  }else
7697
7698  /* Cancel output redirection, if it is currently set (by .testcase)
7699  ** Then read the content of the testcase-out.txt file and compare against
7700  ** azArg[1].  If there are differences, report an error and exit.
7701  */
7702  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7703    char *zRes = 0;
7704    output_reset(p);
7705    if( nArg!=2 ){
7706      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7707      rc = 2;
7708    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7709      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7710      rc = 2;
7711    }else if( testcase_glob(azArg[1],zRes)==0 ){
7712      utf8_printf(stderr,
7713                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7714                 p->zTestcase, azArg[1], zRes);
7715      rc = 1;
7716    }else{
7717      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7718      p->nCheck++;
7719    }
7720    sqlite3_free(zRes);
7721  }else
7722
7723  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7724    failIfSafeMode(p, "cannot run .clone in safe mode");
7725    if( nArg==2 ){
7726      tryToClone(p, azArg[1]);
7727    }else{
7728      raw_printf(stderr, "Usage: .clone FILENAME\n");
7729      rc = 1;
7730    }
7731  }else
7732
7733  if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
7734    if( nArg==1 ){
7735      /* List available connections */
7736      int i;
7737      for(i=0; i<ArraySize(p->aAuxDb); i++){
7738        const char *zFile = p->aAuxDb[i].zDbFilename;
7739        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
7740          zFile = "(not open)";
7741        }else if( zFile==0 ){
7742          zFile = "(memory)";
7743        }else if( zFile[0]==0 ){
7744          zFile = "(temporary-file)";
7745        }
7746        if( p->pAuxDb == &p->aAuxDb[i] ){
7747          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
7748        }else if( p->aAuxDb[i].db!=0 ){
7749          utf8_printf(stdout, "       %d: %s\n", i, zFile);
7750        }
7751      }
7752    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
7753      int i = azArg[1][0] - '0';
7754      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
7755        p->pAuxDb->db = p->db;
7756        p->pAuxDb = &p->aAuxDb[i];
7757        globalDb = p->db = p->pAuxDb->db;
7758        p->pAuxDb->db = 0;
7759      }
7760    }else if( nArg==3 && strcmp(azArg[1], "close")==0
7761           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
7762      int i = azArg[2][0] - '0';
7763      if( i<0 || i>=ArraySize(p->aAuxDb) ){
7764        /* No-op */
7765      }else if( p->pAuxDb == &p->aAuxDb[i] ){
7766        raw_printf(stderr, "cannot close the active database connection\n");
7767        rc = 1;
7768      }else if( p->aAuxDb[i].db ){
7769        session_close_all(p, i);
7770        close_db(p->aAuxDb[i].db);
7771        p->aAuxDb[i].db = 0;
7772      }
7773    }else{
7774      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
7775      rc = 1;
7776    }
7777  }else
7778
7779  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7780    char **azName = 0;
7781    int nName = 0;
7782    sqlite3_stmt *pStmt;
7783    int i;
7784    open_db(p, 0);
7785    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7786    if( rc ){
7787      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7788      rc = 1;
7789    }else{
7790      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7791        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7792        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7793        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7794        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7795        azName[nName*2] = strdup(zSchema);
7796        azName[nName*2+1] = strdup(zFile);
7797        nName++;
7798      }
7799    }
7800    sqlite3_finalize(pStmt);
7801    for(i=0; i<nName; i++){
7802      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7803      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7804      const char *z = azName[i*2+1];
7805      utf8_printf(p->out, "%s: %s %s%s\n",
7806         azName[i*2],
7807         z && z[0] ? z : "\"\"",
7808         bRdonly ? "r/o" : "r/w",
7809         eTxn==SQLITE_TXN_NONE ? "" :
7810            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7811      free(azName[i*2]);
7812      free(azName[i*2+1]);
7813    }
7814    sqlite3_free(azName);
7815  }else
7816
7817  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7818    static const struct DbConfigChoices {
7819      const char *zName;
7820      int op;
7821    } aDbConfig[] = {
7822        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7823        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7824        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7825        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7826        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7827        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7828        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7829        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7830        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7831        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7832        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7833        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7834        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7835        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7836        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7837        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7838    };
7839    int ii, v;
7840    open_db(p, 0);
7841    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7842      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7843      if( nArg>=3 ){
7844        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7845      }
7846      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7847      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7848      if( nArg>1 ) break;
7849    }
7850    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7851      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7852      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7853    }
7854  }else
7855
7856  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7857    rc = shell_dbinfo_command(p, nArg, azArg);
7858  }else
7859
7860#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7861  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7862    open_db(p, 0);
7863    rc = recoverDatabaseCmd(p, nArg, azArg);
7864  }else
7865#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7866
7867  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7868    char *zLike = 0;
7869    char *zSql;
7870    int i;
7871    int savedShowHeader = p->showHeader;
7872    int savedShellFlags = p->shellFlgs;
7873    ShellClearFlag(p,
7874       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7875       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7876    for(i=1; i<nArg; i++){
7877      if( azArg[i][0]=='-' ){
7878        const char *z = azArg[i]+1;
7879        if( z[0]=='-' ) z++;
7880        if( strcmp(z,"preserve-rowids")==0 ){
7881#ifdef SQLITE_OMIT_VIRTUALTABLE
7882          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7883                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7884          rc = 1;
7885          sqlite3_free(zLike);
7886          goto meta_command_exit;
7887#else
7888          ShellSetFlag(p, SHFLG_PreserveRowid);
7889#endif
7890        }else
7891        if( strcmp(z,"newlines")==0 ){
7892          ShellSetFlag(p, SHFLG_Newlines);
7893        }else
7894        if( strcmp(z,"data-only")==0 ){
7895          ShellSetFlag(p, SHFLG_DumpDataOnly);
7896        }else
7897        if( strcmp(z,"nosys")==0 ){
7898          ShellSetFlag(p, SHFLG_DumpNoSys);
7899        }else
7900        {
7901          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7902          rc = 1;
7903          sqlite3_free(zLike);
7904          goto meta_command_exit;
7905        }
7906      }else{
7907        /* azArg[i] contains a LIKE pattern. This ".dump" request should
7908        ** only dump data for tables for which either the table name matches
7909        ** the LIKE pattern, or the table appears to be a shadow table of
7910        ** a virtual table for which the name matches the LIKE pattern.
7911        */
7912        char *zExpr = sqlite3_mprintf(
7913            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
7914            "  SELECT 1 FROM sqlite_schema WHERE "
7915            "    name LIKE %Q ESCAPE '\\' AND"
7916            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
7917            "    substr(o.name, 1, length(name)+1) == (name||'_')"
7918            ")", azArg[i], azArg[i]
7919        );
7920
7921        if( zLike ){
7922          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
7923        }else{
7924          zLike = zExpr;
7925        }
7926      }
7927    }
7928
7929    open_db(p, 0);
7930
7931    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7932      /* When playing back a "dump", the content might appear in an order
7933      ** which causes immediate foreign key constraints to be violated.
7934      ** So disable foreign-key constraint enforcement to prevent problems. */
7935      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7936      raw_printf(p->out, "BEGIN TRANSACTION;\n");
7937    }
7938    p->writableSchema = 0;
7939    p->showHeader = 0;
7940    /* Set writable_schema=ON since doing so forces SQLite to initialize
7941    ** as much of the schema as it can even if the sqlite_schema table is
7942    ** corrupt. */
7943    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7944    p->nErr = 0;
7945    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7946    zSql = sqlite3_mprintf(
7947      "SELECT name, type, sql FROM sqlite_schema AS o "
7948      "WHERE (%s) AND type=='table'"
7949      "  AND sql NOT NULL"
7950      " ORDER BY tbl_name='sqlite_sequence', rowid",
7951      zLike
7952    );
7953    run_schema_dump_query(p,zSql);
7954    sqlite3_free(zSql);
7955    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7956      zSql = sqlite3_mprintf(
7957        "SELECT sql FROM sqlite_schema AS o "
7958        "WHERE (%s) AND sql NOT NULL"
7959        "  AND type IN ('index','trigger','view')",
7960        zLike
7961      );
7962      run_table_dump_query(p, zSql);
7963      sqlite3_free(zSql);
7964    }
7965    sqlite3_free(zLike);
7966    if( p->writableSchema ){
7967      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7968      p->writableSchema = 0;
7969    }
7970    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7971    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7972    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7973      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7974    }
7975    p->showHeader = savedShowHeader;
7976    p->shellFlgs = savedShellFlags;
7977  }else
7978
7979  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7980    if( nArg==2 ){
7981      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7982    }else{
7983      raw_printf(stderr, "Usage: .echo on|off\n");
7984      rc = 1;
7985    }
7986  }else
7987
7988  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7989    if( nArg==2 ){
7990      p->autoEQPtest = 0;
7991      if( p->autoEQPtrace ){
7992        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7993        p->autoEQPtrace = 0;
7994      }
7995      if( strcmp(azArg[1],"full")==0 ){
7996        p->autoEQP = AUTOEQP_full;
7997      }else if( strcmp(azArg[1],"trigger")==0 ){
7998        p->autoEQP = AUTOEQP_trigger;
7999#ifdef SQLITE_DEBUG
8000      }else if( strcmp(azArg[1],"test")==0 ){
8001        p->autoEQP = AUTOEQP_on;
8002        p->autoEQPtest = 1;
8003      }else if( strcmp(azArg[1],"trace")==0 ){
8004        p->autoEQP = AUTOEQP_full;
8005        p->autoEQPtrace = 1;
8006        open_db(p, 0);
8007        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8008        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8009#endif
8010      }else{
8011        p->autoEQP = (u8)booleanValue(azArg[1]);
8012      }
8013    }else{
8014      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8015      rc = 1;
8016    }
8017  }else
8018
8019  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
8020    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8021    rc = 2;
8022  }else
8023
8024  /* The ".explain" command is automatic now.  It is largely pointless.  It
8025  ** retained purely for backwards compatibility */
8026  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
8027    int val = 1;
8028    if( nArg>=2 ){
8029      if( strcmp(azArg[1],"auto")==0 ){
8030        val = 99;
8031      }else{
8032        val =  booleanValue(azArg[1]);
8033      }
8034    }
8035    if( val==1 && p->mode!=MODE_Explain ){
8036      p->normalMode = p->mode;
8037      p->mode = MODE_Explain;
8038      p->autoExplain = 0;
8039    }else if( val==0 ){
8040      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8041      p->autoExplain = 0;
8042    }else if( val==99 ){
8043      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8044      p->autoExplain = 1;
8045    }
8046  }else
8047
8048#ifndef SQLITE_OMIT_VIRTUALTABLE
8049  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
8050    open_db(p, 0);
8051    expertDotCommand(p, azArg, nArg);
8052  }else
8053#endif
8054
8055  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
8056    static const struct {
8057       const char *zCtrlName;   /* Name of a test-control option */
8058       int ctrlCode;            /* Integer code for that option */
8059       const char *zUsage;      /* Usage notes */
8060    } aCtrl[] = {
8061      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8062      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8063      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8064      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8065      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8066   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8067      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8068      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8069      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8070      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8071   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8072    };
8073    int filectrl = -1;
8074    int iCtrl = -1;
8075    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8076    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8077    int n2, i;
8078    const char *zCmd = 0;
8079    const char *zSchema = 0;
8080
8081    open_db(p, 0);
8082    zCmd = nArg>=2 ? azArg[1] : "help";
8083
8084    if( zCmd[0]=='-'
8085     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
8086     && nArg>=4
8087    ){
8088      zSchema = azArg[2];
8089      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8090      nArg -= 2;
8091      zCmd = azArg[1];
8092    }
8093
8094    /* The argument can optionally begin with "-" or "--" */
8095    if( zCmd[0]=='-' && zCmd[1] ){
8096      zCmd++;
8097      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8098    }
8099
8100    /* --help lists all file-controls */
8101    if( strcmp(zCmd,"help")==0 ){
8102      utf8_printf(p->out, "Available file-controls:\n");
8103      for(i=0; i<ArraySize(aCtrl); i++){
8104        utf8_printf(p->out, "  .filectrl %s %s\n",
8105                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8106      }
8107      rc = 1;
8108      goto meta_command_exit;
8109    }
8110
8111    /* convert filectrl text option to value. allow any unique prefix
8112    ** of the option name, or a numerical value. */
8113    n2 = strlen30(zCmd);
8114    for(i=0; i<ArraySize(aCtrl); i++){
8115      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8116        if( filectrl<0 ){
8117          filectrl = aCtrl[i].ctrlCode;
8118          iCtrl = i;
8119        }else{
8120          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8121                              "Use \".filectrl --help\" for help\n", zCmd);
8122          rc = 1;
8123          goto meta_command_exit;
8124        }
8125      }
8126    }
8127    if( filectrl<0 ){
8128      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8129                         "Use \".filectrl --help\" for help\n", zCmd);
8130    }else{
8131      switch(filectrl){
8132        case SQLITE_FCNTL_SIZE_LIMIT: {
8133          if( nArg!=2 && nArg!=3 ) break;
8134          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8135          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8136          isOk = 1;
8137          break;
8138        }
8139        case SQLITE_FCNTL_LOCK_TIMEOUT:
8140        case SQLITE_FCNTL_CHUNK_SIZE: {
8141          int x;
8142          if( nArg!=3 ) break;
8143          x = (int)integerValue(azArg[2]);
8144          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8145          isOk = 2;
8146          break;
8147        }
8148        case SQLITE_FCNTL_PERSIST_WAL:
8149        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8150          int x;
8151          if( nArg!=2 && nArg!=3 ) break;
8152          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8153          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8154          iRes = x;
8155          isOk = 1;
8156          break;
8157        }
8158        case SQLITE_FCNTL_DATA_VERSION:
8159        case SQLITE_FCNTL_HAS_MOVED: {
8160          int x;
8161          if( nArg!=2 ) break;
8162          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8163          iRes = x;
8164          isOk = 1;
8165          break;
8166        }
8167        case SQLITE_FCNTL_TEMPFILENAME: {
8168          char *z = 0;
8169          if( nArg!=2 ) break;
8170          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8171          if( z ){
8172            utf8_printf(p->out, "%s\n", z);
8173            sqlite3_free(z);
8174          }
8175          isOk = 2;
8176          break;
8177        }
8178        case SQLITE_FCNTL_RESERVE_BYTES: {
8179          int x;
8180          if( nArg>=3 ){
8181            x = atoi(azArg[2]);
8182            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8183          }
8184          x = -1;
8185          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8186          utf8_printf(p->out,"%d\n", x);
8187          isOk = 2;
8188          break;
8189        }
8190      }
8191    }
8192    if( isOk==0 && iCtrl>=0 ){
8193      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8194      rc = 1;
8195    }else if( isOk==1 ){
8196      char zBuf[100];
8197      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8198      raw_printf(p->out, "%s\n", zBuf);
8199    }
8200  }else
8201
8202  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8203    ShellState data;
8204    int doStats = 0;
8205    memcpy(&data, p, sizeof(data));
8206    data.showHeader = 0;
8207    data.cMode = data.mode = MODE_Semi;
8208    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8209      data.cMode = data.mode = MODE_Pretty;
8210      nArg = 1;
8211    }
8212    if( nArg!=1 ){
8213      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8214      rc = 1;
8215      goto meta_command_exit;
8216    }
8217    open_db(p, 0);
8218    rc = sqlite3_exec(p->db,
8219       "SELECT sql FROM"
8220       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8221       "     FROM sqlite_schema UNION ALL"
8222       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8223       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8224       "ORDER BY x",
8225       callback, &data, 0
8226    );
8227    if( rc==SQLITE_OK ){
8228      sqlite3_stmt *pStmt;
8229      rc = sqlite3_prepare_v2(p->db,
8230               "SELECT rowid FROM sqlite_schema"
8231               " WHERE name GLOB 'sqlite_stat[134]'",
8232               -1, &pStmt, 0);
8233      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8234      sqlite3_finalize(pStmt);
8235    }
8236    if( doStats==0 ){
8237      raw_printf(p->out, "/* No STAT tables available */\n");
8238    }else{
8239      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8240      data.cMode = data.mode = MODE_Insert;
8241      data.zDestTable = "sqlite_stat1";
8242      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8243      data.zDestTable = "sqlite_stat4";
8244      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8245      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8246    }
8247  }else
8248
8249  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8250    if( nArg==2 ){
8251      p->showHeader = booleanValue(azArg[1]);
8252      p->shellFlgs |= SHFLG_HeaderSet;
8253    }else{
8254      raw_printf(stderr, "Usage: .headers on|off\n");
8255      rc = 1;
8256    }
8257  }else
8258
8259  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8260    if( nArg>=2 ){
8261      n = showHelp(p->out, azArg[1]);
8262      if( n==0 ){
8263        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8264      }
8265    }else{
8266      showHelp(p->out, 0);
8267    }
8268  }else
8269
8270  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8271    char *zTable = 0;           /* Insert data into this table */
8272    char *zFile = 0;            /* Name of file to extra content from */
8273    sqlite3_stmt *pStmt = NULL; /* A statement */
8274    int nCol;                   /* Number of columns in the table */
8275    int nByte;                  /* Number of bytes in an SQL string */
8276    int i, j;                   /* Loop counters */
8277    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8278    int nSep;                   /* Number of bytes in p->colSeparator[] */
8279    char *zSql;                 /* An SQL statement */
8280    ImportCtx sCtx;             /* Reader context */
8281    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8282    int eVerbose = 0;           /* Larger for more console output */
8283    int nSkip = 0;              /* Initial lines to skip */
8284    int useOutputMode = 1;      /* Use output mode to determine separators */
8285
8286    failIfSafeMode(p, "cannot run .import in safe mode");
8287    memset(&sCtx, 0, sizeof(sCtx));
8288    if( p->mode==MODE_Ascii ){
8289      xRead = ascii_read_one_field;
8290    }else{
8291      xRead = csv_read_one_field;
8292    }
8293    for(i=1; i<nArg; i++){
8294      char *z = azArg[i];
8295      if( z[0]=='-' && z[1]=='-' ) z++;
8296      if( z[0]!='-' ){
8297        if( zFile==0 ){
8298          zFile = z;
8299        }else if( zTable==0 ){
8300          zTable = z;
8301        }else{
8302          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8303          showHelp(p->out, "import");
8304          rc = 1;
8305          goto meta_command_exit;
8306        }
8307      }else if( strcmp(z,"-v")==0 ){
8308        eVerbose++;
8309      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8310        nSkip = integerValue(azArg[++i]);
8311      }else if( strcmp(z,"-ascii")==0 ){
8312        sCtx.cColSep = SEP_Unit[0];
8313        sCtx.cRowSep = SEP_Record[0];
8314        xRead = ascii_read_one_field;
8315        useOutputMode = 0;
8316      }else if( strcmp(z,"-csv")==0 ){
8317        sCtx.cColSep = ',';
8318        sCtx.cRowSep = '\n';
8319        xRead = csv_read_one_field;
8320        useOutputMode = 0;
8321      }else{
8322        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8323        showHelp(p->out, "import");
8324        rc = 1;
8325        goto meta_command_exit;
8326      }
8327    }
8328    if( zTable==0 ){
8329      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8330                  zFile==0 ? "FILE" : "TABLE");
8331      showHelp(p->out, "import");
8332      rc = 1;
8333      goto meta_command_exit;
8334    }
8335    seenInterrupt = 0;
8336    open_db(p, 0);
8337    if( useOutputMode ){
8338      /* If neither the --csv or --ascii options are specified, then set
8339      ** the column and row separator characters from the output mode. */
8340      nSep = strlen30(p->colSeparator);
8341      if( nSep==0 ){
8342        raw_printf(stderr,
8343                   "Error: non-null column separator required for import\n");
8344        rc = 1;
8345        goto meta_command_exit;
8346      }
8347      if( nSep>1 ){
8348        raw_printf(stderr,
8349              "Error: multi-character column separators not allowed"
8350              " for import\n");
8351        rc = 1;
8352        goto meta_command_exit;
8353      }
8354      nSep = strlen30(p->rowSeparator);
8355      if( nSep==0 ){
8356        raw_printf(stderr,
8357            "Error: non-null row separator required for import\n");
8358        rc = 1;
8359        goto meta_command_exit;
8360      }
8361      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8362        /* When importing CSV (only), if the row separator is set to the
8363        ** default output row separator, change it to the default input
8364        ** row separator.  This avoids having to maintain different input
8365        ** and output row separators. */
8366        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8367        nSep = strlen30(p->rowSeparator);
8368      }
8369      if( nSep>1 ){
8370        raw_printf(stderr, "Error: multi-character row separators not allowed"
8371                           " for import\n");
8372        rc = 1;
8373        goto meta_command_exit;
8374      }
8375      sCtx.cColSep = p->colSeparator[0];
8376      sCtx.cRowSep = p->rowSeparator[0];
8377    }
8378    sCtx.zFile = zFile;
8379    sCtx.nLine = 1;
8380    if( sCtx.zFile[0]=='|' ){
8381#ifdef SQLITE_OMIT_POPEN
8382      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8383      rc = 1;
8384      goto meta_command_exit;
8385#else
8386      sCtx.in = popen(sCtx.zFile+1, "r");
8387      sCtx.zFile = "<pipe>";
8388      sCtx.xCloser = pclose;
8389#endif
8390    }else{
8391      sCtx.in = fopen(sCtx.zFile, "rb");
8392      sCtx.xCloser = fclose;
8393    }
8394    if( sCtx.in==0 ){
8395      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8396      rc = 1;
8397      goto meta_command_exit;
8398    }
8399    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8400      char zSep[2];
8401      zSep[1] = 0;
8402      zSep[0] = sCtx.cColSep;
8403      utf8_printf(p->out, "Column separator ");
8404      output_c_string(p->out, zSep);
8405      utf8_printf(p->out, ", row separator ");
8406      zSep[0] = sCtx.cRowSep;
8407      output_c_string(p->out, zSep);
8408      utf8_printf(p->out, "\n");
8409    }
8410    while( (nSkip--)>0 ){
8411      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8412    }
8413    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
8414    if( zSql==0 ){
8415      import_cleanup(&sCtx);
8416      shell_out_of_memory();
8417    }
8418    nByte = strlen30(zSql);
8419    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8420    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8421    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8422      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
8423      char cSep = '(';
8424      while( xRead(&sCtx) ){
8425        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8426        cSep = ',';
8427        if( sCtx.cTerm!=sCtx.cColSep ) break;
8428      }
8429      if( cSep=='(' ){
8430        sqlite3_free(zCreate);
8431        import_cleanup(&sCtx);
8432        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8433        rc = 1;
8434        goto meta_command_exit;
8435      }
8436      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8437      if( eVerbose>=1 ){
8438        utf8_printf(p->out, "%s\n", zCreate);
8439      }
8440      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8441      sqlite3_free(zCreate);
8442      if( rc ){
8443        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
8444                sqlite3_errmsg(p->db));
8445        import_cleanup(&sCtx);
8446        rc = 1;
8447        goto meta_command_exit;
8448      }
8449      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8450    }
8451    sqlite3_free(zSql);
8452    if( rc ){
8453      if (pStmt) sqlite3_finalize(pStmt);
8454      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8455      import_cleanup(&sCtx);
8456      rc = 1;
8457      goto meta_command_exit;
8458    }
8459    nCol = sqlite3_column_count(pStmt);
8460    sqlite3_finalize(pStmt);
8461    pStmt = 0;
8462    if( nCol==0 ) return 0; /* no columns, no error */
8463    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8464    if( zSql==0 ){
8465      import_cleanup(&sCtx);
8466      shell_out_of_memory();
8467    }
8468    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8469    j = strlen30(zSql);
8470    for(i=1; i<nCol; i++){
8471      zSql[j++] = ',';
8472      zSql[j++] = '?';
8473    }
8474    zSql[j++] = ')';
8475    zSql[j] = 0;
8476    if( eVerbose>=2 ){
8477      utf8_printf(p->out, "Insert using: %s\n", zSql);
8478    }
8479    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8480    sqlite3_free(zSql);
8481    if( rc ){
8482      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8483      if (pStmt) sqlite3_finalize(pStmt);
8484      import_cleanup(&sCtx);
8485      rc = 1;
8486      goto meta_command_exit;
8487    }
8488    needCommit = sqlite3_get_autocommit(p->db);
8489    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8490    do{
8491      int startLine = sCtx.nLine;
8492      for(i=0; i<nCol; i++){
8493        char *z = xRead(&sCtx);
8494        /*
8495        ** Did we reach end-of-file before finding any columns?
8496        ** If so, stop instead of NULL filling the remaining columns.
8497        */
8498        if( z==0 && i==0 ) break;
8499        /*
8500        ** Did we reach end-of-file OR end-of-line before finding any
8501        ** columns in ASCII mode?  If so, stop instead of NULL filling
8502        ** the remaining columns.
8503        */
8504        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8505        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8506        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8507          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8508                          "filling the rest with NULL\n",
8509                          sCtx.zFile, startLine, nCol, i+1);
8510          i += 2;
8511          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8512        }
8513      }
8514      if( sCtx.cTerm==sCtx.cColSep ){
8515        do{
8516          xRead(&sCtx);
8517          i++;
8518        }while( sCtx.cTerm==sCtx.cColSep );
8519        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8520                        "extras ignored\n",
8521                        sCtx.zFile, startLine, nCol, i);
8522      }
8523      if( i>=nCol ){
8524        sqlite3_step(pStmt);
8525        rc = sqlite3_reset(pStmt);
8526        if( rc!=SQLITE_OK ){
8527          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8528                      startLine, sqlite3_errmsg(p->db));
8529          sCtx.nErr++;
8530        }else{
8531          sCtx.nRow++;
8532        }
8533      }
8534    }while( sCtx.cTerm!=EOF );
8535
8536    import_cleanup(&sCtx);
8537    sqlite3_finalize(pStmt);
8538    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8539    if( eVerbose>0 ){
8540      utf8_printf(p->out,
8541          "Added %d rows with %d errors using %d lines of input\n",
8542          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8543    }
8544  }else
8545
8546#ifndef SQLITE_UNTESTABLE
8547  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8548    char *zSql;
8549    char *zCollist = 0;
8550    sqlite3_stmt *pStmt;
8551    int tnum = 0;
8552    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8553    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8554    int i;
8555    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8556      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8557                          "       .imposter off\n");
8558      /* Also allowed, but not documented:
8559      **
8560      **    .imposter TABLE IMPOSTER
8561      **
8562      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8563      ** imposter is another WITHOUT ROWID table with the columns in
8564      ** storage order. */
8565      rc = 1;
8566      goto meta_command_exit;
8567    }
8568    open_db(p, 0);
8569    if( nArg==2 ){
8570      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8571      goto meta_command_exit;
8572    }
8573    zSql = sqlite3_mprintf(
8574      "SELECT rootpage, 0 FROM sqlite_schema"
8575      " WHERE name='%q' AND type='index'"
8576      "UNION ALL "
8577      "SELECT rootpage, 1 FROM sqlite_schema"
8578      " WHERE name='%q' AND type='table'"
8579      "   AND sql LIKE '%%without%%rowid%%'",
8580      azArg[1], azArg[1]
8581    );
8582    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8583    sqlite3_free(zSql);
8584    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8585      tnum = sqlite3_column_int(pStmt, 0);
8586      isWO = sqlite3_column_int(pStmt, 1);
8587    }
8588    sqlite3_finalize(pStmt);
8589    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8590    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8591    sqlite3_free(zSql);
8592    i = 0;
8593    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8594      char zLabel[20];
8595      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8596      i++;
8597      if( zCol==0 ){
8598        if( sqlite3_column_int(pStmt,1)==-1 ){
8599          zCol = "_ROWID_";
8600        }else{
8601          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8602          zCol = zLabel;
8603        }
8604      }
8605      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8606        lenPK = (int)strlen(zCollist);
8607      }
8608      if( zCollist==0 ){
8609        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8610      }else{
8611        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8612      }
8613    }
8614    sqlite3_finalize(pStmt);
8615    if( i==0 || tnum==0 ){
8616      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8617      rc = 1;
8618      sqlite3_free(zCollist);
8619      goto meta_command_exit;
8620    }
8621    if( lenPK==0 ) lenPK = 100000;
8622    zSql = sqlite3_mprintf(
8623          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8624          azArg[2], zCollist, lenPK, zCollist);
8625    sqlite3_free(zCollist);
8626    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8627    if( rc==SQLITE_OK ){
8628      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8629      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8630      if( rc ){
8631        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8632      }else{
8633        utf8_printf(stdout, "%s;\n", zSql);
8634        raw_printf(stdout,
8635          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8636          azArg[1], isWO ? "table" : "index"
8637        );
8638      }
8639    }else{
8640      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8641      rc = 1;
8642    }
8643    sqlite3_free(zSql);
8644  }else
8645#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8646
8647#ifdef SQLITE_ENABLE_IOTRACE
8648  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8649    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8650    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8651    iotrace = 0;
8652    if( nArg<2 ){
8653      sqlite3IoTrace = 0;
8654    }else if( strcmp(azArg[1], "-")==0 ){
8655      sqlite3IoTrace = iotracePrintf;
8656      iotrace = stdout;
8657    }else{
8658      iotrace = fopen(azArg[1], "w");
8659      if( iotrace==0 ){
8660        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8661        sqlite3IoTrace = 0;
8662        rc = 1;
8663      }else{
8664        sqlite3IoTrace = iotracePrintf;
8665      }
8666    }
8667  }else
8668#endif
8669
8670  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8671    static const struct {
8672       const char *zLimitName;   /* Name of a limit */
8673       int limitCode;            /* Integer code for that limit */
8674    } aLimit[] = {
8675      { "length",                SQLITE_LIMIT_LENGTH                    },
8676      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8677      { "column",                SQLITE_LIMIT_COLUMN                    },
8678      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8679      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8680      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8681      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8682      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8683      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8684      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8685      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8686      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8687    };
8688    int i, n2;
8689    open_db(p, 0);
8690    if( nArg==1 ){
8691      for(i=0; i<ArraySize(aLimit); i++){
8692        printf("%20s %d\n", aLimit[i].zLimitName,
8693               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8694      }
8695    }else if( nArg>3 ){
8696      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8697      rc = 1;
8698      goto meta_command_exit;
8699    }else{
8700      int iLimit = -1;
8701      n2 = strlen30(azArg[1]);
8702      for(i=0; i<ArraySize(aLimit); i++){
8703        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8704          if( iLimit<0 ){
8705            iLimit = i;
8706          }else{
8707            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8708            rc = 1;
8709            goto meta_command_exit;
8710          }
8711        }
8712      }
8713      if( iLimit<0 ){
8714        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8715                        "enter \".limits\" with no arguments for a list.\n",
8716                         azArg[1]);
8717        rc = 1;
8718        goto meta_command_exit;
8719      }
8720      if( nArg==3 ){
8721        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8722                      (int)integerValue(azArg[2]));
8723      }
8724      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8725             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8726    }
8727  }else
8728
8729  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8730    open_db(p, 0);
8731    lintDotCommand(p, azArg, nArg);
8732  }else
8733
8734#ifndef SQLITE_OMIT_LOAD_EXTENSION
8735  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8736    const char *zFile, *zProc;
8737    char *zErrMsg = 0;
8738    failIfSafeMode(p, "cannot run .load in safe mode");
8739    if( nArg<2 ){
8740      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8741      rc = 1;
8742      goto meta_command_exit;
8743    }
8744    zFile = azArg[1];
8745    zProc = nArg>=3 ? azArg[2] : 0;
8746    open_db(p, 0);
8747    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8748    if( rc!=SQLITE_OK ){
8749      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8750      sqlite3_free(zErrMsg);
8751      rc = 1;
8752    }
8753  }else
8754#endif
8755
8756  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8757    failIfSafeMode(p, "cannot run .log in safe mode");
8758    if( nArg!=2 ){
8759      raw_printf(stderr, "Usage: .log FILENAME\n");
8760      rc = 1;
8761    }else{
8762      const char *zFile = azArg[1];
8763      output_file_close(p->pLog);
8764      p->pLog = output_file_open(zFile, 0);
8765    }
8766  }else
8767
8768  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8769    const char *zMode = nArg>=2 ? azArg[1] : "";
8770    int n2 = strlen30(zMode);
8771    int c2 = zMode[0];
8772    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8773      p->mode = MODE_Line;
8774      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8775    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8776      p->mode = MODE_Column;
8777      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8778        p->showHeader = 1;
8779      }
8780      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8781    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8782      p->mode = MODE_List;
8783      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8784      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8785    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8786      p->mode = MODE_Html;
8787    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8788      p->mode = MODE_Tcl;
8789      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8790      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8791    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8792      p->mode = MODE_Csv;
8793      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8794      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8795    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8796      p->mode = MODE_List;
8797      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8798    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8799      p->mode = MODE_Insert;
8800      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8801    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8802      p->mode = MODE_Quote;
8803      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8804      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8805    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8806      p->mode = MODE_Ascii;
8807      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8808      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8809    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8810      p->mode = MODE_Markdown;
8811    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8812      p->mode = MODE_Table;
8813    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8814      p->mode = MODE_Box;
8815    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8816      p->mode = MODE_Json;
8817    }else if( nArg==1 ){
8818      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8819    }else{
8820      raw_printf(stderr, "Error: mode should be one of: "
8821         "ascii box column csv html insert json line list markdown "
8822         "quote table tabs tcl\n");
8823      rc = 1;
8824    }
8825    p->cMode = p->mode;
8826  }else
8827
8828  if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
8829    if( nArg!=2 ){
8830      raw_printf(stderr, "Usage: .nonce NONCE\n");
8831      rc = 1;
8832    }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
8833      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", p->lineno, azArg[1]);
8834      exit(1);
8835    }
8836    p->bSafeMode = 0;
8837    return 0;  /* Return immediately to bypass the safe mode reset
8838               ** at the end of this procedure */
8839  }else
8840
8841  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8842    if( nArg==2 ){
8843      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8844                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8845    }else{
8846      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8847      rc = 1;
8848    }
8849  }else
8850
8851#ifdef SQLITE_DEBUG
8852  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8853    int i;
8854    for(i=1; i<nArg; i++){
8855      const char *z = azArg[i];
8856      if( z[0]=='-' && z[1]=='-' ) z++;
8857      if( strcmp(z,"-repeat")==0 ){
8858        if( i==nArg-1 ){
8859          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8860          rc = 1;
8861        }else{
8862          oomRepeat = (int)integerValue(azArg[++i]);
8863        }
8864      }else if( IsDigit(z[0]) ){
8865        oomCounter = (int)integerValue(azArg[i]);
8866      }else{
8867        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8868        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8869        rc = 1;
8870      }
8871    }
8872    if( rc==0 ){
8873      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8874      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8875    }
8876  }else
8877#endif /* SQLITE_DEBUG */
8878
8879  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8880    char *zNewFilename = 0;  /* Name of the database file to open */
8881    int iName = 1;           /* Index in azArg[] of the filename */
8882    int newFlag = 0;         /* True to delete file before opening */
8883    /* Close the existing database */
8884    session_close_all(p, -1);
8885    close_db(p->db);
8886    p->db = 0;
8887    p->pAuxDb->zDbFilename = 0;
8888    sqlite3_free(p->pAuxDb->zFreeOnClose);
8889    p->pAuxDb->zFreeOnClose = 0;
8890    p->openMode = SHELL_OPEN_UNSPEC;
8891    p->openFlags = 0;
8892    p->szMax = 0;
8893    /* Check for command-line arguments */
8894    for(iName=1; iName<nArg; iName++){
8895      const char *z = azArg[iName];
8896      if( optionMatch(z,"new") ){
8897        newFlag = 1;
8898#ifdef SQLITE_HAVE_ZLIB
8899      }else if( optionMatch(z, "zip") ){
8900        p->openMode = SHELL_OPEN_ZIPFILE;
8901#endif
8902      }else if( optionMatch(z, "append") ){
8903        p->openMode = SHELL_OPEN_APPENDVFS;
8904      }else if( optionMatch(z, "readonly") ){
8905        p->openMode = SHELL_OPEN_READONLY;
8906      }else if( optionMatch(z, "nofollow") ){
8907        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8908      }else if( optionMatch(z, "excl") ){
8909        p->openFlags |= SQLITE_OPEN_EXCLUSIVE;
8910#ifndef SQLITE_OMIT_DESERIALIZE
8911      }else if( optionMatch(z, "deserialize") ){
8912        p->openMode = SHELL_OPEN_DESERIALIZE;
8913      }else if( optionMatch(z, "hexdb") ){
8914        p->openMode = SHELL_OPEN_HEXDB;
8915      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8916        p->szMax = integerValue(azArg[++iName]);
8917#endif /* SQLITE_OMIT_DESERIALIZE */
8918      }else if( z[0]=='-' ){
8919        utf8_printf(stderr, "unknown option: %s\n", z);
8920        rc = 1;
8921        goto meta_command_exit;
8922      }else if( zNewFilename ){
8923        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
8924        rc = 1;
8925        goto meta_command_exit;
8926      }else{
8927        zNewFilename = sqlite3_mprintf("%s", z);
8928      }
8929    }
8930    /* If a filename is specified, try to open it first */
8931    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8932      if( newFlag && !p->bSafeMode ) shellDeleteFile(zNewFilename);
8933      if( p->bSafeMode
8934       && p->openMode!=SHELL_OPEN_HEXDB
8935       && zNewFilename
8936       && strcmp(zNewFilename,":memory:")!=0
8937      ){
8938        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
8939      }
8940      p->pAuxDb->zDbFilename = zNewFilename;
8941      open_db(p, OPEN_DB_KEEPALIVE);
8942      if( p->db==0 ){
8943        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8944        sqlite3_free(zNewFilename);
8945      }else{
8946        p->pAuxDb->zFreeOnClose = zNewFilename;
8947      }
8948    }
8949    if( p->db==0 ){
8950      /* As a fall-back open a TEMP database */
8951      p->pAuxDb->zDbFilename = 0;
8952      open_db(p, 0);
8953    }
8954  }else
8955
8956  if( (c=='o'
8957        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8958   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8959  ){
8960    char *zFile = 0;
8961    int bTxtMode = 0;
8962    int i;
8963    int eMode = 0;
8964    int bBOM = 0;
8965    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8966
8967    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
8968    if( c=='e' ){
8969      eMode = 'x';
8970      bOnce = 2;
8971    }else if( strncmp(azArg[0],"once",n)==0 ){
8972      bOnce = 1;
8973    }
8974    for(i=1; i<nArg; i++){
8975      char *z = azArg[i];
8976      if( z[0]=='-' ){
8977        if( z[1]=='-' ) z++;
8978        if( strcmp(z,"-bom")==0 ){
8979          bBOM = 1;
8980        }else if( c!='e' && strcmp(z,"-x")==0 ){
8981          eMode = 'x';  /* spreadsheet */
8982        }else if( c!='e' && strcmp(z,"-e")==0 ){
8983          eMode = 'e';  /* text editor */
8984        }else{
8985          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8986                      azArg[i]);
8987          showHelp(p->out, azArg[0]);
8988          rc = 1;
8989          goto meta_command_exit;
8990        }
8991      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
8992        zFile = sqlite3_mprintf("%s", z);
8993        if( zFile[0]=='|' ){
8994          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
8995          break;
8996        }
8997      }else{
8998        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8999                    azArg[i]);
9000        showHelp(p->out, azArg[0]);
9001        rc = 1;
9002        sqlite3_free(zFile);
9003        goto meta_command_exit;
9004      }
9005    }
9006    if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
9007    if( bOnce ){
9008      p->outCount = 2;
9009    }else{
9010      p->outCount = 0;
9011    }
9012    output_reset(p);
9013#ifndef SQLITE_NOHAVE_SYSTEM
9014    if( eMode=='e' || eMode=='x' ){
9015      p->doXdgOpen = 1;
9016      outputModePush(p);
9017      if( eMode=='x' ){
9018        /* spreadsheet mode.  Output as CSV. */
9019        newTempFile(p, "csv");
9020        ShellClearFlag(p, SHFLG_Echo);
9021        p->mode = MODE_Csv;
9022        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9023        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9024      }else{
9025        /* text editor mode */
9026        newTempFile(p, "txt");
9027        bTxtMode = 1;
9028      }
9029      sqlite3_free(zFile);
9030      zFile = sqlite3_mprintf("%s", p->zTempFile);
9031    }
9032#endif /* SQLITE_NOHAVE_SYSTEM */
9033    if( zFile[0]=='|' ){
9034#ifdef SQLITE_OMIT_POPEN
9035      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9036      rc = 1;
9037      p->out = stdout;
9038#else
9039      p->out = popen(zFile + 1, "w");
9040      if( p->out==0 ){
9041        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9042        p->out = stdout;
9043        rc = 1;
9044      }else{
9045        if( bBOM ) fprintf(p->out,"\357\273\277");
9046        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9047      }
9048#endif
9049    }else{
9050      p->out = output_file_open(zFile, bTxtMode);
9051      if( p->out==0 ){
9052        if( strcmp(zFile,"off")!=0 ){
9053          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9054        }
9055        p->out = stdout;
9056        rc = 1;
9057      } else {
9058        if( bBOM ) fprintf(p->out,"\357\273\277");
9059        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9060      }
9061    }
9062    sqlite3_free(zFile);
9063  }else
9064
9065  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
9066    open_db(p,0);
9067    if( nArg<=1 ) goto parameter_syntax_error;
9068
9069    /* .parameter clear
9070    ** Clear all bind parameters by dropping the TEMP table that holds them.
9071    */
9072    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
9073      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9074                   0, 0, 0);
9075    }else
9076
9077    /* .parameter list
9078    ** List all bind parameters.
9079    */
9080    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
9081      sqlite3_stmt *pStmt = 0;
9082      int rx;
9083      int len = 0;
9084      rx = sqlite3_prepare_v2(p->db,
9085             "SELECT max(length(key)) "
9086             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9087      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9088        len = sqlite3_column_int(pStmt, 0);
9089        if( len>40 ) len = 40;
9090      }
9091      sqlite3_finalize(pStmt);
9092      pStmt = 0;
9093      if( len ){
9094        rx = sqlite3_prepare_v2(p->db,
9095             "SELECT key, quote(value) "
9096             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9097        while( sqlite3_step(pStmt)==SQLITE_ROW ){
9098          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9099                      sqlite3_column_text(pStmt,1));
9100        }
9101        sqlite3_finalize(pStmt);
9102      }
9103    }else
9104
9105    /* .parameter init
9106    ** Make sure the TEMP table used to hold bind parameters exists.
9107    ** Create it if necessary.
9108    */
9109    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
9110      bind_table_init(p);
9111    }else
9112
9113    /* .parameter set NAME VALUE
9114    ** Set or reset a bind parameter.  NAME should be the full parameter
9115    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9116    ** VALUE can be in either SQL literal notation, or if not it will be
9117    ** understood to be a text string.
9118    */
9119    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
9120      int rx;
9121      char *zSql;
9122      sqlite3_stmt *pStmt;
9123      const char *zKey = azArg[2];
9124      const char *zValue = azArg[3];
9125      bind_table_init(p);
9126      zSql = sqlite3_mprintf(
9127                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9128                  "VALUES(%Q,%s);", zKey, zValue);
9129      if( zSql==0 ) shell_out_of_memory();
9130      pStmt = 0;
9131      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9132      sqlite3_free(zSql);
9133      if( rx!=SQLITE_OK ){
9134        sqlite3_finalize(pStmt);
9135        pStmt = 0;
9136        zSql = sqlite3_mprintf(
9137                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9138                   "VALUES(%Q,%Q);", zKey, zValue);
9139        if( zSql==0 ) shell_out_of_memory();
9140        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9141        sqlite3_free(zSql);
9142        if( rx!=SQLITE_OK ){
9143          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9144          sqlite3_finalize(pStmt);
9145          pStmt = 0;
9146          rc = 1;
9147        }
9148      }
9149      sqlite3_step(pStmt);
9150      sqlite3_finalize(pStmt);
9151    }else
9152
9153    /* .parameter unset NAME
9154    ** Remove the NAME binding from the parameter binding table, if it
9155    ** exists.
9156    */
9157    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
9158      char *zSql = sqlite3_mprintf(
9159          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9160      if( zSql==0 ) shell_out_of_memory();
9161      sqlite3_exec(p->db, zSql, 0, 0, 0);
9162      sqlite3_free(zSql);
9163    }else
9164    /* If no command name matches, show a syntax error */
9165    parameter_syntax_error:
9166    showHelp(p->out, "parameter");
9167  }else
9168
9169  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
9170    int i;
9171    for(i=1; i<nArg; i++){
9172      if( i>1 ) raw_printf(p->out, " ");
9173      utf8_printf(p->out, "%s", azArg[i]);
9174    }
9175    raw_printf(p->out, "\n");
9176  }else
9177
9178#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9179  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
9180    int i;
9181    int nn = 0;
9182    p->flgProgress = 0;
9183    p->mxProgress = 0;
9184    p->nProgress = 0;
9185    for(i=1; i<nArg; i++){
9186      const char *z = azArg[i];
9187      if( z[0]=='-' ){
9188        z++;
9189        if( z[0]=='-' ) z++;
9190        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
9191          p->flgProgress |= SHELL_PROGRESS_QUIET;
9192          continue;
9193        }
9194        if( strcmp(z,"reset")==0 ){
9195          p->flgProgress |= SHELL_PROGRESS_RESET;
9196          continue;
9197        }
9198        if( strcmp(z,"once")==0 ){
9199          p->flgProgress |= SHELL_PROGRESS_ONCE;
9200          continue;
9201        }
9202        if( strcmp(z,"limit")==0 ){
9203          if( i+1>=nArg ){
9204            utf8_printf(stderr, "Error: missing argument on --limit\n");
9205            rc = 1;
9206            goto meta_command_exit;
9207          }else{
9208            p->mxProgress = (int)integerValue(azArg[++i]);
9209          }
9210          continue;
9211        }
9212        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9213        rc = 1;
9214        goto meta_command_exit;
9215      }else{
9216        nn = (int)integerValue(z);
9217      }
9218    }
9219    open_db(p, 0);
9220    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9221  }else
9222#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9223
9224  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9225    if( nArg >= 2) {
9226      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9227    }
9228    if( nArg >= 3) {
9229      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9230    }
9231  }else
9232
9233  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
9234    rc = 2;
9235  }else
9236
9237  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
9238    FILE *inSaved = p->in;
9239    int savedLineno = p->lineno;
9240    failIfSafeMode(p, "cannot run .read in safe mode");
9241    if( nArg!=2 ){
9242      raw_printf(stderr, "Usage: .read FILE\n");
9243      rc = 1;
9244      goto meta_command_exit;
9245    }
9246    if( azArg[1][0]=='|' ){
9247#ifdef SQLITE_OMIT_POPEN
9248      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9249      rc = 1;
9250      p->out = stdout;
9251#else
9252      p->in = popen(azArg[1]+1, "r");
9253      if( p->in==0 ){
9254        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9255        rc = 1;
9256      }else{
9257        rc = process_input(p);
9258        pclose(p->in);
9259      }
9260#endif
9261    }else if( (p->in = openChrSource(azArg[1]))==0 ){
9262      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9263      rc = 1;
9264    }else{
9265      rc = process_input(p);
9266      fclose(p->in);
9267    }
9268    p->in = inSaved;
9269    p->lineno = savedLineno;
9270  }else
9271
9272  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9273    const char *zSrcFile;
9274    const char *zDb;
9275    sqlite3 *pSrc;
9276    sqlite3_backup *pBackup;
9277    int nTimeout = 0;
9278
9279    failIfSafeMode(p, "cannot run .restore in safe mode");
9280    if( nArg==2 ){
9281      zSrcFile = azArg[1];
9282      zDb = "main";
9283    }else if( nArg==3 ){
9284      zSrcFile = azArg[2];
9285      zDb = azArg[1];
9286    }else{
9287      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9288      rc = 1;
9289      goto meta_command_exit;
9290    }
9291    rc = sqlite3_open(zSrcFile, &pSrc);
9292    if( rc!=SQLITE_OK ){
9293      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9294      close_db(pSrc);
9295      return 1;
9296    }
9297    open_db(p, 0);
9298    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9299    if( pBackup==0 ){
9300      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9301      close_db(pSrc);
9302      return 1;
9303    }
9304    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9305          || rc==SQLITE_BUSY  ){
9306      if( rc==SQLITE_BUSY ){
9307        if( nTimeout++ >= 3 ) break;
9308        sqlite3_sleep(100);
9309      }
9310    }
9311    sqlite3_backup_finish(pBackup);
9312    if( rc==SQLITE_DONE ){
9313      rc = 0;
9314    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9315      raw_printf(stderr, "Error: source database is busy\n");
9316      rc = 1;
9317    }else{
9318      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9319      rc = 1;
9320    }
9321    close_db(pSrc);
9322  }else
9323
9324  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9325    if( nArg==2 ){
9326      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9327#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9328      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9329#endif
9330    }else{
9331      raw_printf(stderr, "Usage: .scanstats on|off\n");
9332      rc = 1;
9333    }
9334  }else
9335
9336  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9337    ShellText sSelect;
9338    ShellState data;
9339    char *zErrMsg = 0;
9340    const char *zDiv = "(";
9341    const char *zName = 0;
9342    int iSchema = 0;
9343    int bDebug = 0;
9344    int bNoSystemTabs = 0;
9345    int ii;
9346
9347    open_db(p, 0);
9348    memcpy(&data, p, sizeof(data));
9349    data.showHeader = 0;
9350    data.cMode = data.mode = MODE_Semi;
9351    initText(&sSelect);
9352    for(ii=1; ii<nArg; ii++){
9353      if( optionMatch(azArg[ii],"indent") ){
9354        data.cMode = data.mode = MODE_Pretty;
9355      }else if( optionMatch(azArg[ii],"debug") ){
9356        bDebug = 1;
9357      }else if( optionMatch(azArg[ii],"nosys") ){
9358        bNoSystemTabs = 1;
9359      }else if( azArg[ii][0]=='-' ){
9360        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9361        rc = 1;
9362        goto meta_command_exit;
9363      }else if( zName==0 ){
9364        zName = azArg[ii];
9365      }else{
9366        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9367        rc = 1;
9368        goto meta_command_exit;
9369      }
9370    }
9371    if( zName!=0 ){
9372      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9373                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9374                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9375                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9376      if( isSchema ){
9377        char *new_argv[2], *new_colv[2];
9378        new_argv[0] = sqlite3_mprintf(
9379                      "CREATE TABLE %s (\n"
9380                      "  type text,\n"
9381                      "  name text,\n"
9382                      "  tbl_name text,\n"
9383                      "  rootpage integer,\n"
9384                      "  sql text\n"
9385                      ")", zName);
9386        new_argv[1] = 0;
9387        new_colv[0] = "sql";
9388        new_colv[1] = 0;
9389        callback(&data, 1, new_argv, new_colv);
9390        sqlite3_free(new_argv[0]);
9391      }
9392    }
9393    if( zDiv ){
9394      sqlite3_stmt *pStmt = 0;
9395      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9396                              -1, &pStmt, 0);
9397      if( rc ){
9398        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9399        sqlite3_finalize(pStmt);
9400        rc = 1;
9401        goto meta_command_exit;
9402      }
9403      appendText(&sSelect, "SELECT sql FROM", 0);
9404      iSchema = 0;
9405      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9406        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9407        char zScNum[30];
9408        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9409        appendText(&sSelect, zDiv, 0);
9410        zDiv = " UNION ALL ";
9411        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9412        if( sqlite3_stricmp(zDb, "main")!=0 ){
9413          appendText(&sSelect, zDb, '\'');
9414        }else{
9415          appendText(&sSelect, "NULL", 0);
9416        }
9417        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9418        appendText(&sSelect, zScNum, 0);
9419        appendText(&sSelect, " AS snum, ", 0);
9420        appendText(&sSelect, zDb, '\'');
9421        appendText(&sSelect, " AS sname FROM ", 0);
9422        appendText(&sSelect, zDb, quoteChar(zDb));
9423        appendText(&sSelect, ".sqlite_schema", 0);
9424      }
9425      sqlite3_finalize(pStmt);
9426#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9427      if( zName ){
9428        appendText(&sSelect,
9429           " UNION ALL SELECT shell_module_schema(name),"
9430           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9431        0);
9432      }
9433#endif
9434      appendText(&sSelect, ") WHERE ", 0);
9435      if( zName ){
9436        char *zQarg = sqlite3_mprintf("%Q", zName);
9437        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9438                    strchr(zName, '[') != 0;
9439        if( strchr(zName, '.') ){
9440          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9441        }else{
9442          appendText(&sSelect, "lower(tbl_name)", 0);
9443        }
9444        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9445        appendText(&sSelect, zQarg, 0);
9446        if( !bGlob ){
9447          appendText(&sSelect, " ESCAPE '\\' ", 0);
9448        }
9449        appendText(&sSelect, " AND ", 0);
9450        sqlite3_free(zQarg);
9451      }
9452      if( bNoSystemTabs ){
9453        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9454      }
9455      appendText(&sSelect, "sql IS NOT NULL"
9456                           " ORDER BY snum, rowid", 0);
9457      if( bDebug ){
9458        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9459      }else{
9460        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9461      }
9462      freeText(&sSelect);
9463    }
9464    if( zErrMsg ){
9465      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9466      sqlite3_free(zErrMsg);
9467      rc = 1;
9468    }else if( rc != SQLITE_OK ){
9469      raw_printf(stderr,"Error: querying schema information\n");
9470      rc = 1;
9471    }else{
9472      rc = 0;
9473    }
9474  }else
9475
9476  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9477    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
9478    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
9479  }else
9480
9481#if defined(SQLITE_ENABLE_SESSION)
9482  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9483    struct AuxDb *pAuxDb = p->pAuxDb;
9484    OpenSession *pSession = &pAuxDb->aSession[0];
9485    char **azCmd = &azArg[1];
9486    int iSes = 0;
9487    int nCmd = nArg - 1;
9488    int i;
9489    if( nArg<=1 ) goto session_syntax_error;
9490    open_db(p, 0);
9491    if( nArg>=3 ){
9492      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
9493        if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
9494      }
9495      if( iSes<pAuxDb->nSession ){
9496        pSession = &pAuxDb->aSession[iSes];
9497        azCmd++;
9498        nCmd--;
9499      }else{
9500        pSession = &pAuxDb->aSession[0];
9501        iSes = 0;
9502      }
9503    }
9504
9505    /* .session attach TABLE
9506    ** Invoke the sqlite3session_attach() interface to attach a particular
9507    ** table so that it is never filtered.
9508    */
9509    if( strcmp(azCmd[0],"attach")==0 ){
9510      if( nCmd!=2 ) goto session_syntax_error;
9511      if( pSession->p==0 ){
9512        session_not_open:
9513        raw_printf(stderr, "ERROR: No sessions are open\n");
9514      }else{
9515        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9516        if( rc ){
9517          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9518          rc = 0;
9519        }
9520      }
9521    }else
9522
9523    /* .session changeset FILE
9524    ** .session patchset FILE
9525    ** Write a changeset or patchset into a file.  The file is overwritten.
9526    */
9527    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9528      FILE *out = 0;
9529      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
9530      if( nCmd!=2 ) goto session_syntax_error;
9531      if( pSession->p==0 ) goto session_not_open;
9532      out = fopen(azCmd[1], "wb");
9533      if( out==0 ){
9534        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9535                    azCmd[1]);
9536      }else{
9537        int szChng;
9538        void *pChng;
9539        if( azCmd[0][0]=='c' ){
9540          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9541        }else{
9542          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9543        }
9544        if( rc ){
9545          printf("Error: error code %d\n", rc);
9546          rc = 0;
9547        }
9548        if( pChng
9549          && fwrite(pChng, szChng, 1, out)!=1 ){
9550          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9551                  szChng);
9552        }
9553        sqlite3_free(pChng);
9554        fclose(out);
9555      }
9556    }else
9557
9558    /* .session close
9559    ** Close the identified session
9560    */
9561    if( strcmp(azCmd[0], "close")==0 ){
9562      if( nCmd!=1 ) goto session_syntax_error;
9563      if( pAuxDb->nSession ){
9564        session_close(pSession);
9565        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
9566      }
9567    }else
9568
9569    /* .session enable ?BOOLEAN?
9570    ** Query or set the enable flag
9571    */
9572    if( strcmp(azCmd[0], "enable")==0 ){
9573      int ii;
9574      if( nCmd>2 ) goto session_syntax_error;
9575      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9576      if( pAuxDb->nSession ){
9577        ii = sqlite3session_enable(pSession->p, ii);
9578        utf8_printf(p->out, "session %s enable flag = %d\n",
9579                    pSession->zName, ii);
9580      }
9581    }else
9582
9583    /* .session filter GLOB ....
9584    ** Set a list of GLOB patterns of table names to be excluded.
9585    */
9586    if( strcmp(azCmd[0], "filter")==0 ){
9587      int ii, nByte;
9588      if( nCmd<2 ) goto session_syntax_error;
9589      if( pAuxDb->nSession ){
9590        for(ii=0; ii<pSession->nFilter; ii++){
9591          sqlite3_free(pSession->azFilter[ii]);
9592        }
9593        sqlite3_free(pSession->azFilter);
9594        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9595        pSession->azFilter = sqlite3_malloc( nByte );
9596        if( pSession->azFilter==0 ){
9597          raw_printf(stderr, "Error: out or memory\n");
9598          exit(1);
9599        }
9600        for(ii=1; ii<nCmd; ii++){
9601          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9602        }
9603        pSession->nFilter = ii-1;
9604      }
9605    }else
9606
9607    /* .session indirect ?BOOLEAN?
9608    ** Query or set the indirect flag
9609    */
9610    if( strcmp(azCmd[0], "indirect")==0 ){
9611      int ii;
9612      if( nCmd>2 ) goto session_syntax_error;
9613      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9614      if( pAuxDb->nSession ){
9615        ii = sqlite3session_indirect(pSession->p, ii);
9616        utf8_printf(p->out, "session %s indirect flag = %d\n",
9617                    pSession->zName, ii);
9618      }
9619    }else
9620
9621    /* .session isempty
9622    ** Determine if the session is empty
9623    */
9624    if( strcmp(azCmd[0], "isempty")==0 ){
9625      int ii;
9626      if( nCmd!=1 ) goto session_syntax_error;
9627      if( pAuxDb->nSession ){
9628        ii = sqlite3session_isempty(pSession->p);
9629        utf8_printf(p->out, "session %s isempty flag = %d\n",
9630                    pSession->zName, ii);
9631      }
9632    }else
9633
9634    /* .session list
9635    ** List all currently open sessions
9636    */
9637    if( strcmp(azCmd[0],"list")==0 ){
9638      for(i=0; i<pAuxDb->nSession; i++){
9639        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
9640      }
9641    }else
9642
9643    /* .session open DB NAME
9644    ** Open a new session called NAME on the attached database DB.
9645    ** DB is normally "main".
9646    */
9647    if( strcmp(azCmd[0],"open")==0 ){
9648      char *zName;
9649      if( nCmd!=3 ) goto session_syntax_error;
9650      zName = azCmd[2];
9651      if( zName[0]==0 ) goto session_syntax_error;
9652      for(i=0; i<pAuxDb->nSession; i++){
9653        if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
9654          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9655          goto meta_command_exit;
9656        }
9657      }
9658      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
9659        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
9660        goto meta_command_exit;
9661      }
9662      pSession = &pAuxDb->aSession[pAuxDb->nSession];
9663      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9664      if( rc ){
9665        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9666        rc = 0;
9667        goto meta_command_exit;
9668      }
9669      pSession->nFilter = 0;
9670      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9671      pAuxDb->nSession++;
9672      pSession->zName = sqlite3_mprintf("%s", zName);
9673    }else
9674    /* If no command name matches, show a syntax error */
9675    session_syntax_error:
9676    showHelp(p->out, "session");
9677  }else
9678#endif
9679
9680#ifdef SQLITE_DEBUG
9681  /* Undocumented commands for internal testing.  Subject to change
9682  ** without notice. */
9683  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9684    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9685      int i, v;
9686      for(i=1; i<nArg; i++){
9687        v = booleanValue(azArg[i]);
9688        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9689      }
9690    }
9691    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9692      int i; sqlite3_int64 v;
9693      for(i=1; i<nArg; i++){
9694        char zBuf[200];
9695        v = integerValue(azArg[i]);
9696        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9697        utf8_printf(p->out, "%s", zBuf);
9698      }
9699    }
9700  }else
9701#endif
9702
9703  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9704    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9705    int bVerbose = 0;        /* Verbose output */
9706    int bSelftestExists;     /* True if SELFTEST already exists */
9707    int i, k;                /* Loop counters */
9708    int nTest = 0;           /* Number of tests runs */
9709    int nErr = 0;            /* Number of errors seen */
9710    ShellText str;           /* Answer for a query */
9711    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9712
9713    open_db(p,0);
9714    for(i=1; i<nArg; i++){
9715      const char *z = azArg[i];
9716      if( z[0]=='-' && z[1]=='-' ) z++;
9717      if( strcmp(z,"-init")==0 ){
9718        bIsInit = 1;
9719      }else
9720      if( strcmp(z,"-v")==0 ){
9721        bVerbose++;
9722      }else
9723      {
9724        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9725                    azArg[i], azArg[0]);
9726        raw_printf(stderr, "Should be one of: --init -v\n");
9727        rc = 1;
9728        goto meta_command_exit;
9729      }
9730    }
9731    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9732           != SQLITE_OK ){
9733      bSelftestExists = 0;
9734    }else{
9735      bSelftestExists = 1;
9736    }
9737    if( bIsInit ){
9738      createSelftestTable(p);
9739      bSelftestExists = 1;
9740    }
9741    initText(&str);
9742    appendText(&str, "x", 0);
9743    for(k=bSelftestExists; k>=0; k--){
9744      if( k==1 ){
9745        rc = sqlite3_prepare_v2(p->db,
9746            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9747            -1, &pStmt, 0);
9748      }else{
9749        rc = sqlite3_prepare_v2(p->db,
9750          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9751          "      (1,'run','PRAGMA integrity_check','ok')",
9752          -1, &pStmt, 0);
9753      }
9754      if( rc ){
9755        raw_printf(stderr, "Error querying the selftest table\n");
9756        rc = 1;
9757        sqlite3_finalize(pStmt);
9758        goto meta_command_exit;
9759      }
9760      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9761        int tno = sqlite3_column_int(pStmt, 0);
9762        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9763        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9764        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9765
9766        k = 0;
9767        if( bVerbose>0 ){
9768          char *zQuote = sqlite3_mprintf("%q", zSql);
9769          printf("%d: %s %s\n", tno, zOp, zSql);
9770          sqlite3_free(zQuote);
9771        }
9772        if( strcmp(zOp,"memo")==0 ){
9773          utf8_printf(p->out, "%s\n", zSql);
9774        }else
9775        if( strcmp(zOp,"run")==0 ){
9776          char *zErrMsg = 0;
9777          str.n = 0;
9778          str.z[0] = 0;
9779          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9780          nTest++;
9781          if( bVerbose ){
9782            utf8_printf(p->out, "Result: %s\n", str.z);
9783          }
9784          if( rc || zErrMsg ){
9785            nErr++;
9786            rc = 1;
9787            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9788            sqlite3_free(zErrMsg);
9789          }else if( strcmp(zAns,str.z)!=0 ){
9790            nErr++;
9791            rc = 1;
9792            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9793            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9794          }
9795        }else
9796        {
9797          utf8_printf(stderr,
9798            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9799          rc = 1;
9800          break;
9801        }
9802      } /* End loop over rows of content from SELFTEST */
9803      sqlite3_finalize(pStmt);
9804    } /* End loop over k */
9805    freeText(&str);
9806    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9807  }else
9808
9809  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9810    if( nArg<2 || nArg>3 ){
9811      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9812      rc = 1;
9813    }
9814    if( nArg>=2 ){
9815      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9816                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9817    }
9818    if( nArg>=3 ){
9819      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9820                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9821    }
9822  }else
9823
9824  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9825    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9826    int i;                   /* Loop counter */
9827    int bSchema = 0;         /* Also hash the schema */
9828    int bSeparate = 0;       /* Hash each table separately */
9829    int iSize = 224;         /* Hash algorithm to use */
9830    int bDebug = 0;          /* Only show the query that would have run */
9831    sqlite3_stmt *pStmt;     /* For querying tables names */
9832    char *zSql;              /* SQL to be run */
9833    char *zSep;              /* Separator */
9834    ShellText sSql;          /* Complete SQL for the query to run the hash */
9835    ShellText sQuery;        /* Set of queries used to read all content */
9836    open_db(p, 0);
9837    for(i=1; i<nArg; i++){
9838      const char *z = azArg[i];
9839      if( z[0]=='-' ){
9840        z++;
9841        if( z[0]=='-' ) z++;
9842        if( strcmp(z,"schema")==0 ){
9843          bSchema = 1;
9844        }else
9845        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9846         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9847        ){
9848          iSize = atoi(&z[5]);
9849        }else
9850        if( strcmp(z,"debug")==0 ){
9851          bDebug = 1;
9852        }else
9853        {
9854          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9855                      azArg[i], azArg[0]);
9856          showHelp(p->out, azArg[0]);
9857          rc = 1;
9858          goto meta_command_exit;
9859        }
9860      }else if( zLike ){
9861        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9862        rc = 1;
9863        goto meta_command_exit;
9864      }else{
9865        zLike = z;
9866        bSeparate = 1;
9867        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9868      }
9869    }
9870    if( bSchema ){
9871      zSql = "SELECT lower(name) FROM sqlite_schema"
9872             " WHERE type='table' AND coalesce(rootpage,0)>1"
9873             " UNION ALL SELECT 'sqlite_schema'"
9874             " ORDER BY 1 collate nocase";
9875    }else{
9876      zSql = "SELECT lower(name) FROM sqlite_schema"
9877             " WHERE type='table' AND coalesce(rootpage,0)>1"
9878             " AND name NOT LIKE 'sqlite_%'"
9879             " ORDER BY 1 collate nocase";
9880    }
9881    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9882    initText(&sQuery);
9883    initText(&sSql);
9884    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9885    zSep = "VALUES(";
9886    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9887      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9888      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9889      if( strncmp(zTab, "sqlite_",7)!=0 ){
9890        appendText(&sQuery,"SELECT * FROM ", 0);
9891        appendText(&sQuery,zTab,'"');
9892        appendText(&sQuery," NOT INDEXED;", 0);
9893      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9894        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9895                           " ORDER BY name;", 0);
9896      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9897        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9898                           " ORDER BY name;", 0);
9899      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9900        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9901                           " ORDER BY tbl,idx;", 0);
9902      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9903        appendText(&sQuery, "SELECT * FROM ", 0);
9904        appendText(&sQuery, zTab, 0);
9905        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9906      }
9907      appendText(&sSql, zSep, 0);
9908      appendText(&sSql, sQuery.z, '\'');
9909      sQuery.n = 0;
9910      appendText(&sSql, ",", 0);
9911      appendText(&sSql, zTab, '\'');
9912      zSep = "),(";
9913    }
9914    sqlite3_finalize(pStmt);
9915    if( bSeparate ){
9916      zSql = sqlite3_mprintf(
9917          "%s))"
9918          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9919          "   FROM [sha3sum$query]",
9920          sSql.z, iSize);
9921    }else{
9922      zSql = sqlite3_mprintf(
9923          "%s))"
9924          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9925          "   FROM [sha3sum$query]",
9926          sSql.z, iSize);
9927    }
9928    freeText(&sQuery);
9929    freeText(&sSql);
9930    if( bDebug ){
9931      utf8_printf(p->out, "%s\n", zSql);
9932    }else{
9933      shell_exec(p, zSql, 0);
9934    }
9935    sqlite3_free(zSql);
9936  }else
9937
9938#ifndef SQLITE_NOHAVE_SYSTEM
9939  if( c=='s'
9940   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9941  ){
9942    char *zCmd;
9943    int i, x;
9944    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9945    if( nArg<2 ){
9946      raw_printf(stderr, "Usage: .system COMMAND\n");
9947      rc = 1;
9948      goto meta_command_exit;
9949    }
9950    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9951    for(i=2; i<nArg; i++){
9952      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9953                             zCmd, azArg[i]);
9954    }
9955    x = system(zCmd);
9956    sqlite3_free(zCmd);
9957    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9958  }else
9959#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9960
9961  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9962    static const char *azBool[] = { "off", "on", "trigger", "full"};
9963    const char *zOut;
9964    int i;
9965    if( nArg!=1 ){
9966      raw_printf(stderr, "Usage: .show\n");
9967      rc = 1;
9968      goto meta_command_exit;
9969    }
9970    utf8_printf(p->out, "%12.12s: %s\n","echo",
9971                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9972    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9973    utf8_printf(p->out, "%12.12s: %s\n","explain",
9974         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9975    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9976    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9977    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9978      output_c_string(p->out, p->nullValue);
9979      raw_printf(p->out, "\n");
9980    utf8_printf(p->out,"%12.12s: %s\n","output",
9981            strlen30(p->outfile) ? p->outfile : "stdout");
9982    utf8_printf(p->out,"%12.12s: ", "colseparator");
9983      output_c_string(p->out, p->colSeparator);
9984      raw_printf(p->out, "\n");
9985    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9986      output_c_string(p->out, p->rowSeparator);
9987      raw_printf(p->out, "\n");
9988    switch( p->statsOn ){
9989      case 0:  zOut = "off";     break;
9990      default: zOut = "on";      break;
9991      case 2:  zOut = "stmt";    break;
9992      case 3:  zOut = "vmstep";  break;
9993    }
9994    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
9995    utf8_printf(p->out, "%12.12s: ", "width");
9996    for (i=0;i<p->nWidth;i++) {
9997      raw_printf(p->out, "%d ", p->colWidth[i]);
9998    }
9999    raw_printf(p->out, "\n");
10000    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10001                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10002  }else
10003
10004  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
10005    if( nArg==2 ){
10006      if( strcmp(azArg[1],"stmt")==0 ){
10007        p->statsOn = 2;
10008      }else if( strcmp(azArg[1],"vmstep")==0 ){
10009        p->statsOn = 3;
10010      }else{
10011        p->statsOn = (u8)booleanValue(azArg[1]);
10012      }
10013    }else if( nArg==1 ){
10014      display_stats(p->db, p, 0);
10015    }else{
10016      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10017      rc = 1;
10018    }
10019  }else
10020
10021  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
10022   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
10023                 || strncmp(azArg[0], "indexes", n)==0) )
10024  ){
10025    sqlite3_stmt *pStmt;
10026    char **azResult;
10027    int nRow, nAlloc;
10028    int ii;
10029    ShellText s;
10030    initText(&s);
10031    open_db(p, 0);
10032    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10033    if( rc ){
10034      sqlite3_finalize(pStmt);
10035      return shellDatabaseError(p->db);
10036    }
10037
10038    if( nArg>2 && c=='i' ){
10039      /* It is an historical accident that the .indexes command shows an error
10040      ** when called with the wrong number of arguments whereas the .tables
10041      ** command does not. */
10042      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10043      rc = 1;
10044      sqlite3_finalize(pStmt);
10045      goto meta_command_exit;
10046    }
10047    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10048      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10049      if( zDbName==0 ) continue;
10050      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10051      if( sqlite3_stricmp(zDbName, "main")==0 ){
10052        appendText(&s, "SELECT name FROM ", 0);
10053      }else{
10054        appendText(&s, "SELECT ", 0);
10055        appendText(&s, zDbName, '\'');
10056        appendText(&s, "||'.'||name FROM ", 0);
10057      }
10058      appendText(&s, zDbName, '"');
10059      appendText(&s, ".sqlite_schema ", 0);
10060      if( c=='t' ){
10061        appendText(&s," WHERE type IN ('table','view')"
10062                      "   AND name NOT LIKE 'sqlite_%'"
10063                      "   AND name LIKE ?1", 0);
10064      }else{
10065        appendText(&s," WHERE type='index'"
10066                      "   AND tbl_name LIKE ?1", 0);
10067      }
10068    }
10069    rc = sqlite3_finalize(pStmt);
10070    appendText(&s, " ORDER BY 1", 0);
10071    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10072    freeText(&s);
10073    if( rc ) return shellDatabaseError(p->db);
10074
10075    /* Run the SQL statement prepared by the above block. Store the results
10076    ** as an array of nul-terminated strings in azResult[].  */
10077    nRow = nAlloc = 0;
10078    azResult = 0;
10079    if( nArg>1 ){
10080      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10081    }else{
10082      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10083    }
10084    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10085      if( nRow>=nAlloc ){
10086        char **azNew;
10087        int n2 = nAlloc*2 + 10;
10088        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10089        if( azNew==0 ) shell_out_of_memory();
10090        nAlloc = n2;
10091        azResult = azNew;
10092      }
10093      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10094      if( 0==azResult[nRow] ) shell_out_of_memory();
10095      nRow++;
10096    }
10097    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10098      rc = shellDatabaseError(p->db);
10099    }
10100
10101    /* Pretty-print the contents of array azResult[] to the output */
10102    if( rc==0 && nRow>0 ){
10103      int len, maxlen = 0;
10104      int i, j;
10105      int nPrintCol, nPrintRow;
10106      for(i=0; i<nRow; i++){
10107        len = strlen30(azResult[i]);
10108        if( len>maxlen ) maxlen = len;
10109      }
10110      nPrintCol = 80/(maxlen+2);
10111      if( nPrintCol<1 ) nPrintCol = 1;
10112      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10113      for(i=0; i<nPrintRow; i++){
10114        for(j=i; j<nRow; j+=nPrintRow){
10115          char *zSp = j<nPrintRow ? "" : "  ";
10116          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10117                      azResult[j] ? azResult[j]:"");
10118        }
10119        raw_printf(p->out, "\n");
10120      }
10121    }
10122
10123    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10124    sqlite3_free(azResult);
10125  }else
10126
10127  /* Begin redirecting output to the file "testcase-out.txt" */
10128  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
10129    output_reset(p);
10130    p->out = output_file_open("testcase-out.txt", 0);
10131    if( p->out==0 ){
10132      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10133    }
10134    if( nArg>=2 ){
10135      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10136    }else{
10137      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10138    }
10139  }else
10140
10141#ifndef SQLITE_UNTESTABLE
10142  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
10143    static const struct {
10144       const char *zCtrlName;   /* Name of a test-control option */
10145       int ctrlCode;            /* Integer code for that option */
10146       const char *zUsage;      /* Usage notes */
10147    } aCtrl[] = {
10148      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
10149      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
10150    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
10151    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
10152      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
10153      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
10154    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
10155      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
10156      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
10157      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
10158      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
10159      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
10160#ifdef YYCOVERAGE
10161      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
10162#endif
10163      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
10164      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
10165      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
10166      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
10167      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
10168      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP,   "NMAX"           },
10169      { "tune",               SQLITE_TESTCTRL_TUNE,          "ID VALUE"       },
10170    };
10171    int testctrl = -1;
10172    int iCtrl = -1;
10173    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10174    int isOk = 0;
10175    int i, n2;
10176    const char *zCmd = 0;
10177
10178    open_db(p, 0);
10179    zCmd = nArg>=2 ? azArg[1] : "help";
10180
10181    /* The argument can optionally begin with "-" or "--" */
10182    if( zCmd[0]=='-' && zCmd[1] ){
10183      zCmd++;
10184      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10185    }
10186
10187    /* --help lists all test-controls */
10188    if( strcmp(zCmd,"help")==0 ){
10189      utf8_printf(p->out, "Available test-controls:\n");
10190      for(i=0; i<ArraySize(aCtrl); i++){
10191        utf8_printf(p->out, "  .testctrl %s %s\n",
10192                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10193      }
10194      rc = 1;
10195      goto meta_command_exit;
10196    }
10197
10198    /* convert testctrl text option to value. allow any unique prefix
10199    ** of the option name, or a numerical value. */
10200    n2 = strlen30(zCmd);
10201    for(i=0; i<ArraySize(aCtrl); i++){
10202      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10203        if( testctrl<0 ){
10204          testctrl = aCtrl[i].ctrlCode;
10205          iCtrl = i;
10206        }else{
10207          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
10208                              "Use \".testctrl --help\" for help\n", zCmd);
10209          rc = 1;
10210          goto meta_command_exit;
10211        }
10212      }
10213    }
10214    if( testctrl<0 ){
10215      utf8_printf(stderr,"Error: unknown test-control: %s\n"
10216                         "Use \".testctrl --help\" for help\n", zCmd);
10217    }else{
10218      switch(testctrl){
10219
10220        /* sqlite3_test_control(int, db, int) */
10221        case SQLITE_TESTCTRL_OPTIMIZATIONS:
10222          if( nArg==3 ){
10223            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
10224            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10225            isOk = 3;
10226          }
10227          break;
10228
10229        /* sqlite3_test_control(int) */
10230        case SQLITE_TESTCTRL_PRNG_SAVE:
10231        case SQLITE_TESTCTRL_PRNG_RESTORE:
10232        case SQLITE_TESTCTRL_BYTEORDER:
10233          if( nArg==2 ){
10234            rc2 = sqlite3_test_control(testctrl);
10235            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
10236          }
10237          break;
10238
10239        /* sqlite3_test_control(int, uint) */
10240        case SQLITE_TESTCTRL_PENDING_BYTE:
10241          if( nArg==3 ){
10242            unsigned int opt = (unsigned int)integerValue(azArg[2]);
10243            rc2 = sqlite3_test_control(testctrl, opt);
10244            isOk = 3;
10245          }
10246          break;
10247
10248        /* sqlite3_test_control(int, int, sqlite3*) */
10249        case SQLITE_TESTCTRL_PRNG_SEED:
10250          if( nArg==3 || nArg==4 ){
10251            int ii = (int)integerValue(azArg[2]);
10252            sqlite3 *db;
10253            if( ii==0 && strcmp(azArg[2],"random")==0 ){
10254              sqlite3_randomness(sizeof(ii),&ii);
10255              printf("-- random seed: %d\n", ii);
10256            }
10257            if( nArg==3 ){
10258              db = 0;
10259            }else{
10260              db = p->db;
10261              /* Make sure the schema has been loaded */
10262              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10263            }
10264            rc2 = sqlite3_test_control(testctrl, ii, db);
10265            isOk = 3;
10266          }
10267          break;
10268
10269        /* sqlite3_test_control(int, int) */
10270        case SQLITE_TESTCTRL_ASSERT:
10271        case SQLITE_TESTCTRL_ALWAYS:
10272          if( nArg==3 ){
10273            int opt = booleanValue(azArg[2]);
10274            rc2 = sqlite3_test_control(testctrl, opt);
10275            isOk = 1;
10276          }
10277          break;
10278
10279        /* sqlite3_test_control(int, int) */
10280        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10281        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10282          if( nArg==3 ){
10283            int opt = booleanValue(azArg[2]);
10284            rc2 = sqlite3_test_control(testctrl, opt);
10285            isOk = 3;
10286          }
10287          break;
10288
10289        /* sqlite3_test_control(sqlite3*) */
10290        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10291          rc2 = sqlite3_test_control(testctrl, p->db);
10292          isOk = 3;
10293          break;
10294
10295        case SQLITE_TESTCTRL_IMPOSTER:
10296          if( nArg==5 ){
10297            rc2 = sqlite3_test_control(testctrl, p->db,
10298                          azArg[2],
10299                          integerValue(azArg[3]),
10300                          integerValue(azArg[4]));
10301            isOk = 3;
10302          }
10303          break;
10304
10305        case SQLITE_TESTCTRL_SEEK_COUNT: {
10306          u64 x = 0;
10307          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10308          utf8_printf(p->out, "%llu\n", x);
10309          isOk = 3;
10310          break;
10311        }
10312
10313#ifdef YYCOVERAGE
10314        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
10315          if( nArg==2 ){
10316            sqlite3_test_control(testctrl, p->out);
10317            isOk = 3;
10318          }
10319          break;
10320        }
10321#endif
10322#ifdef SQLITE_DEBUG
10323        case SQLITE_TESTCTRL_TUNE: {
10324          if( nArg==4 ){
10325            int id = (int)integerValue(azArg[2]);
10326            int val = (int)integerValue(azArg[3]);
10327            sqlite3_test_control(testctrl, id, &val);
10328            isOk = 3;
10329          }else if( nArg==3 ){
10330            int id = (int)integerValue(azArg[2]);
10331            sqlite3_test_control(testctrl, -id, &rc2);
10332            isOk = 1;
10333          }else if( nArg==2 ){
10334            int id = 1;
10335            while(1){
10336              int val = 0;
10337              rc2 = sqlite3_test_control(testctrl, -id, &val);
10338              if( rc2!=SQLITE_OK ) break;
10339              if( id>1 ) utf8_printf(p->out, "  ");
10340              utf8_printf(p->out, "%d: %d", id, val);
10341              id++;
10342            }
10343            if( id>1 ) utf8_printf(p->out, "\n");
10344            isOk = 3;
10345          }
10346          break;
10347        }
10348#endif
10349        case SQLITE_TESTCTRL_SORTER_MMAP:
10350          if( nArg==3 ){
10351            int opt = (unsigned int)integerValue(azArg[2]);
10352            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10353            isOk = 3;
10354          }
10355          break;
10356      }
10357    }
10358    if( isOk==0 && iCtrl>=0 ){
10359      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10360      rc = 1;
10361    }else if( isOk==1 ){
10362      raw_printf(p->out, "%d\n", rc2);
10363    }else if( isOk==2 ){
10364      raw_printf(p->out, "0x%08x\n", rc2);
10365    }
10366  }else
10367#endif /* !defined(SQLITE_UNTESTABLE) */
10368
10369  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10370    open_db(p, 0);
10371    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10372  }else
10373
10374  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10375    if( nArg==2 ){
10376      enableTimer = booleanValue(azArg[1]);
10377      if( enableTimer && !HAS_TIMER ){
10378        raw_printf(stderr, "Error: timer not available on this system.\n");
10379        enableTimer = 0;
10380      }
10381    }else{
10382      raw_printf(stderr, "Usage: .timer on|off\n");
10383      rc = 1;
10384    }
10385  }else
10386
10387#ifndef SQLITE_OMIT_TRACE
10388  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10389    int mType = 0;
10390    int jj;
10391    open_db(p, 0);
10392    for(jj=1; jj<nArg; jj++){
10393      const char *z = azArg[jj];
10394      if( z[0]=='-' ){
10395        if( optionMatch(z, "expanded") ){
10396          p->eTraceType = SHELL_TRACE_EXPANDED;
10397        }
10398#ifdef SQLITE_ENABLE_NORMALIZE
10399        else if( optionMatch(z, "normalized") ){
10400          p->eTraceType = SHELL_TRACE_NORMALIZED;
10401        }
10402#endif
10403        else if( optionMatch(z, "plain") ){
10404          p->eTraceType = SHELL_TRACE_PLAIN;
10405        }
10406        else if( optionMatch(z, "profile") ){
10407          mType |= SQLITE_TRACE_PROFILE;
10408        }
10409        else if( optionMatch(z, "row") ){
10410          mType |= SQLITE_TRACE_ROW;
10411        }
10412        else if( optionMatch(z, "stmt") ){
10413          mType |= SQLITE_TRACE_STMT;
10414        }
10415        else if( optionMatch(z, "close") ){
10416          mType |= SQLITE_TRACE_CLOSE;
10417        }
10418        else {
10419          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10420          rc = 1;
10421          goto meta_command_exit;
10422        }
10423      }else{
10424        output_file_close(p->traceOut);
10425        p->traceOut = output_file_open(azArg[1], 0);
10426      }
10427    }
10428    if( p->traceOut==0 ){
10429      sqlite3_trace_v2(p->db, 0, 0, 0);
10430    }else{
10431      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10432      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10433    }
10434  }else
10435#endif /* !defined(SQLITE_OMIT_TRACE) */
10436
10437#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10438  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10439    int ii;
10440    int lenOpt;
10441    char *zOpt;
10442    if( nArg<2 ){
10443      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10444      rc = 1;
10445      goto meta_command_exit;
10446    }
10447    open_db(p, 0);
10448    zOpt = azArg[1];
10449    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10450    lenOpt = (int)strlen(zOpt);
10451    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10452      assert( azArg[nArg]==0 );
10453      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10454    }else{
10455      for(ii=1; ii<nArg; ii++){
10456        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10457      }
10458    }
10459  }else
10460#endif
10461
10462#if SQLITE_USER_AUTHENTICATION
10463  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10464    if( nArg<2 ){
10465      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10466      rc = 1;
10467      goto meta_command_exit;
10468    }
10469    open_db(p, 0);
10470    if( strcmp(azArg[1],"login")==0 ){
10471      if( nArg!=4 ){
10472        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10473        rc = 1;
10474        goto meta_command_exit;
10475      }
10476      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10477                                     strlen30(azArg[3]));
10478      if( rc ){
10479        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10480        rc = 1;
10481      }
10482    }else if( strcmp(azArg[1],"add")==0 ){
10483      if( nArg!=5 ){
10484        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10485        rc = 1;
10486        goto meta_command_exit;
10487      }
10488      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10489                            booleanValue(azArg[4]));
10490      if( rc ){
10491        raw_printf(stderr, "User-Add failed: %d\n", rc);
10492        rc = 1;
10493      }
10494    }else if( strcmp(azArg[1],"edit")==0 ){
10495      if( nArg!=5 ){
10496        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10497        rc = 1;
10498        goto meta_command_exit;
10499      }
10500      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10501                              booleanValue(azArg[4]));
10502      if( rc ){
10503        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10504        rc = 1;
10505      }
10506    }else if( strcmp(azArg[1],"delete")==0 ){
10507      if( nArg!=3 ){
10508        raw_printf(stderr, "Usage: .user delete USER\n");
10509        rc = 1;
10510        goto meta_command_exit;
10511      }
10512      rc = sqlite3_user_delete(p->db, azArg[2]);
10513      if( rc ){
10514        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10515        rc = 1;
10516      }
10517    }else{
10518      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10519      rc = 1;
10520      goto meta_command_exit;
10521    }
10522  }else
10523#endif /* SQLITE_USER_AUTHENTICATION */
10524
10525  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10526    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10527        sqlite3_libversion(), sqlite3_sourceid());
10528#if SQLITE_HAVE_ZLIB
10529    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10530#endif
10531#define CTIMEOPT_VAL_(opt) #opt
10532#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10533#if defined(__clang__) && defined(__clang_major__)
10534    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10535                    CTIMEOPT_VAL(__clang_minor__) "."
10536                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10537#elif defined(_MSC_VER)
10538    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10539#elif defined(__GNUC__) && defined(__VERSION__)
10540    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10541#endif
10542  }else
10543
10544  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10545    const char *zDbName = nArg==2 ? azArg[1] : "main";
10546    sqlite3_vfs *pVfs = 0;
10547    if( p->db ){
10548      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10549      if( pVfs ){
10550        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10551        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10552        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10553        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10554      }
10555    }
10556  }else
10557
10558  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10559    sqlite3_vfs *pVfs;
10560    sqlite3_vfs *pCurrent = 0;
10561    if( p->db ){
10562      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10563    }
10564    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10565      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10566           pVfs==pCurrent ? "  <--- CURRENT" : "");
10567      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10568      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10569      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10570      if( pVfs->pNext ){
10571        raw_printf(p->out, "-----------------------------------\n");
10572      }
10573    }
10574  }else
10575
10576  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10577    const char *zDbName = nArg==2 ? azArg[1] : "main";
10578    char *zVfsName = 0;
10579    if( p->db ){
10580      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10581      if( zVfsName ){
10582        utf8_printf(p->out, "%s\n", zVfsName);
10583        sqlite3_free(zVfsName);
10584      }
10585    }
10586  }else
10587
10588  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10589    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10590    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
10591  }else
10592
10593  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10594    int j;
10595    assert( nArg<=ArraySize(azArg) );
10596    p->nWidth = nArg-1;
10597    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10598    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10599    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10600    for(j=1; j<nArg; j++){
10601      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10602    }
10603  }else
10604
10605  {
10606    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10607      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10608    rc = 1;
10609  }
10610
10611meta_command_exit:
10612  if( p->outCount ){
10613    p->outCount--;
10614    if( p->outCount==0 ) output_reset(p);
10615  }
10616  p->bSafeMode = p->bSafeModePersist;
10617  return rc;
10618}
10619
10620/* Line scan result and intermediate states (supporting scan resumption)
10621*/
10622typedef enum {
10623  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
10624  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
10625  QSS_Start = 0
10626} QuickScanState;
10627#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
10628#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
10629#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
10630#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
10631#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
10632
10633/*
10634** Scan line for classification to guide shell's handling.
10635** The scan is resumable for subsequent lines when prior
10636** return values are passed as the 2nd argument.
10637*/
10638static QuickScanState quickscan(char *zLine, QuickScanState qss){
10639  char cin;
10640  char cWait = (char)qss; /* intentional narrowing loss */
10641  if( cWait==0 ){
10642  PlainScan:
10643    while (cin = *zLine++){
10644      if( IsSpace(cin) )
10645        continue;
10646      switch (cin){
10647      case '-':
10648        if( *zLine!='-' )
10649          break;
10650        while((cin = *++zLine)!=0 )
10651          if( cin=='\n')
10652            goto PlainScan;
10653        return qss;
10654      case ';':
10655        qss |= QSS_EndingSemi;
10656        continue;
10657      case '/':
10658        if( *zLine=='*' ){
10659          ++zLine;
10660          cWait = '*';
10661          qss = QSS_SETV(qss, cWait);
10662          goto TermScan;
10663        }
10664        break;
10665      case '[':
10666        cin = ']';
10667        /* fall thru */
10668      case '`': case '\'': case '"':
10669        cWait = cin;
10670        qss = QSS_HasDark | cWait;
10671        goto TermScan;
10672      default:
10673        break;
10674      }
10675      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
10676    }
10677  }else{
10678  TermScan:
10679    while (cin = *zLine++){
10680      if( cin==cWait ){
10681        switch( cWait ){
10682        case '*':
10683          if( *zLine != '/' )
10684            continue;
10685          ++zLine;
10686          cWait = 0;
10687          qss = QSS_SETV(qss, 0);
10688          goto PlainScan;
10689        case '`': case '\'': case '"':
10690          if(*zLine==cWait){
10691            ++zLine;
10692            continue;
10693          }
10694          /* fall thru */
10695        case ']':
10696          cWait = 0;
10697          qss = QSS_SETV(qss, 0);
10698          goto PlainScan;
10699        default: assert(0);
10700        }
10701      }
10702    }
10703  }
10704  return qss;
10705}
10706
10707/*
10708** Return TRUE if the line typed in is an SQL command terminator other
10709** than a semi-colon.  The SQL Server style "go" command is understood
10710** as is the Oracle "/".
10711*/
10712static int line_is_command_terminator(char *zLine){
10713  while( IsSpace(zLine[0]) ){ zLine++; };
10714  if( zLine[0]=='/' )
10715    zLine += 1; /* Oracle */
10716  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
10717    zLine += 2; /* SQL Server */
10718  else
10719    return 0;
10720  return quickscan(zLine,QSS_Start)==QSS_Start;
10721}
10722
10723/*
10724** We need a default sqlite3_complete() implementation to use in case
10725** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10726** any arbitrary text is a complete SQL statement.  This is not very
10727** user-friendly, but it does seem to work.
10728*/
10729#ifdef SQLITE_OMIT_COMPLETE
10730#define sqlite3_complete(x) 1
10731#endif
10732
10733/*
10734** Return true if zSql is a complete SQL statement.  Return false if it
10735** ends in the middle of a string literal or C-style comment.
10736*/
10737static int line_is_complete(char *zSql, int nSql){
10738  int rc;
10739  if( zSql==0 ) return 1;
10740  zSql[nSql] = ';';
10741  zSql[nSql+1] = 0;
10742  rc = sqlite3_complete(zSql);
10743  zSql[nSql] = 0;
10744  return rc;
10745}
10746
10747/*
10748** Run a single line of SQL.  Return the number of errors.
10749*/
10750static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10751  int rc;
10752  char *zErrMsg = 0;
10753
10754  open_db(p, 0);
10755  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10756  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10757  BEGIN_TIMER;
10758  rc = shell_exec(p, zSql, &zErrMsg);
10759  END_TIMER;
10760  if( rc || zErrMsg ){
10761    char zPrefix[100];
10762    if( in!=0 || !stdin_is_interactive ){
10763      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10764                       "Error: near line %d:", startline);
10765    }else{
10766      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10767    }
10768    if( zErrMsg!=0 ){
10769      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10770      sqlite3_free(zErrMsg);
10771      zErrMsg = 0;
10772    }else{
10773      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10774    }
10775    return 1;
10776  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10777    raw_printf(p->out, "changes: %3lld   total_changes: %lld\n",
10778            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
10779  }
10780  return 0;
10781}
10782
10783
10784/*
10785** Read input from *in and process it.  If *in==0 then input
10786** is interactive - the user is typing it it.  Otherwise, input
10787** is coming from a file or device.  A prompt is issued and history
10788** is saved only if input is interactive.  An interrupt signal will
10789** cause this routine to exit immediately, unless input is interactive.
10790**
10791** Return the number of errors.
10792*/
10793static int process_input(ShellState *p){
10794  char *zLine = 0;          /* A single input line */
10795  char *zSql = 0;           /* Accumulated SQL text */
10796  int nLine;                /* Length of current line */
10797  int nSql = 0;             /* Bytes of zSql[] used */
10798  int nAlloc = 0;           /* Allocated zSql[] space */
10799  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10800  int rc;                   /* Error code */
10801  int errCnt = 0;           /* Number of errors seen */
10802  int startline = 0;        /* Line number for start of current input */
10803  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
10804
10805  p->lineno = 0;
10806  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10807    fflush(p->out);
10808    zLine = one_input_line(p->in, zLine, nSql>0);
10809    if( zLine==0 ){
10810      /* End of input */
10811      if( p->in==0 && stdin_is_interactive ) printf("\n");
10812      break;
10813    }
10814    if( seenInterrupt ){
10815      if( p->in!=0 ) break;
10816      seenInterrupt = 0;
10817    }
10818    p->lineno++;
10819    if( QSS_INPLAIN(qss)
10820        && line_is_command_terminator(zLine)
10821        && line_is_complete(zSql, nSql) ){
10822      memcpy(zLine,";",2);
10823    }
10824    qss = quickscan(zLine, qss);
10825    if( QSS_PLAINWHITE(qss) && nSql==0 ){
10826      if( ShellHasFlag(p, SHFLG_Echo) )
10827        printf("%s\n", zLine);
10828      /* Just swallow leading whitespace */
10829      continue;
10830    }
10831    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10832      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10833      if( zLine[0]=='.' ){
10834        rc = do_meta_command(zLine, p);
10835        if( rc==2 ){ /* exit requested */
10836          break;
10837        }else if( rc ){
10838          errCnt++;
10839        }
10840      }
10841      continue;
10842    }
10843    nLine = strlen30(zLine);
10844    if( nSql+nLine+2>=nAlloc ){
10845      /* Grow buffer by half-again increments when big. */
10846      nAlloc = nSql+(nSql>>1)+nLine+100;
10847      zSql = realloc(zSql, nAlloc);
10848      if( zSql==0 ) shell_out_of_memory();
10849    }
10850    nSqlPrior = nSql;
10851    if( nSql==0 ){
10852      int i;
10853      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10854      assert( nAlloc>0 && zSql!=0 );
10855      memcpy(zSql, zLine+i, nLine+1-i);
10856      startline = p->lineno;
10857      nSql = nLine-i;
10858    }else{
10859      zSql[nSql++] = '\n';
10860      memcpy(zSql+nSql, zLine, nLine+1);
10861      nSql += nLine;
10862    }
10863    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
10864      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10865      nSql = 0;
10866      if( p->outCount ){
10867        output_reset(p);
10868        p->outCount = 0;
10869      }else{
10870        clearTempFile(p);
10871      }
10872      p->bSafeMode = p->bSafeModePersist;
10873    }else if( nSql && QSS_PLAINWHITE(qss) ){
10874      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10875      nSql = 0;
10876    }
10877  }
10878  if( nSql && QSS_PLAINDARK(qss) ){
10879    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10880  }
10881  free(zSql);
10882  free(zLine);
10883  return errCnt>0;
10884}
10885
10886/*
10887** Return a pathname which is the user's home directory.  A
10888** 0 return indicates an error of some kind.
10889*/
10890static char *find_home_dir(int clearFlag){
10891  static char *home_dir = NULL;
10892  if( clearFlag ){
10893    free(home_dir);
10894    home_dir = 0;
10895    return 0;
10896  }
10897  if( home_dir ) return home_dir;
10898
10899#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10900     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10901  {
10902    struct passwd *pwent;
10903    uid_t uid = getuid();
10904    if( (pwent=getpwuid(uid)) != NULL) {
10905      home_dir = pwent->pw_dir;
10906    }
10907  }
10908#endif
10909
10910#if defined(_WIN32_WCE)
10911  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10912   */
10913  home_dir = "/";
10914#else
10915
10916#if defined(_WIN32) || defined(WIN32)
10917  if (!home_dir) {
10918    home_dir = getenv("USERPROFILE");
10919  }
10920#endif
10921
10922  if (!home_dir) {
10923    home_dir = getenv("HOME");
10924  }
10925
10926#if defined(_WIN32) || defined(WIN32)
10927  if (!home_dir) {
10928    char *zDrive, *zPath;
10929    int n;
10930    zDrive = getenv("HOMEDRIVE");
10931    zPath = getenv("HOMEPATH");
10932    if( zDrive && zPath ){
10933      n = strlen30(zDrive) + strlen30(zPath) + 1;
10934      home_dir = malloc( n );
10935      if( home_dir==0 ) return 0;
10936      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10937      return home_dir;
10938    }
10939    home_dir = "c:\\";
10940  }
10941#endif
10942
10943#endif /* !_WIN32_WCE */
10944
10945  if( home_dir ){
10946    int n = strlen30(home_dir) + 1;
10947    char *z = malloc( n );
10948    if( z ) memcpy(z, home_dir, n);
10949    home_dir = z;
10950  }
10951
10952  return home_dir;
10953}
10954
10955/*
10956** Read input from the file given by sqliterc_override.  Or if that
10957** parameter is NULL, take input from ~/.sqliterc
10958**
10959** Returns the number of errors.
10960*/
10961static void process_sqliterc(
10962  ShellState *p,                  /* Configuration data */
10963  const char *sqliterc_override   /* Name of config file. NULL to use default */
10964){
10965  char *home_dir = NULL;
10966  const char *sqliterc = sqliterc_override;
10967  char *zBuf = 0;
10968  FILE *inSaved = p->in;
10969  int savedLineno = p->lineno;
10970
10971  if (sqliterc == NULL) {
10972    home_dir = find_home_dir(0);
10973    if( home_dir==0 ){
10974      raw_printf(stderr, "-- warning: cannot find home directory;"
10975                      " cannot read ~/.sqliterc\n");
10976      return;
10977    }
10978    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10979    sqliterc = zBuf;
10980  }
10981  p->in = fopen(sqliterc,"rb");
10982  if( p->in ){
10983    if( stdin_is_interactive ){
10984      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10985    }
10986    if( process_input(p) && bail_on_error ) exit(1);
10987    fclose(p->in);
10988  }else if( sqliterc_override!=0 ){
10989    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
10990    if( bail_on_error ) exit(1);
10991  }
10992  p->in = inSaved;
10993  p->lineno = savedLineno;
10994  sqlite3_free(zBuf);
10995}
10996
10997/*
10998** Show available command line options
10999*/
11000static const char zOptions[] =
11001#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11002  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11003#endif
11004  "   -append              append the database to the end of the file\n"
11005  "   -ascii               set output mode to 'ascii'\n"
11006  "   -bail                stop after hitting an error\n"
11007  "   -batch               force batch I/O\n"
11008  "   -box                 set output mode to 'box'\n"
11009  "   -column              set output mode to 'column'\n"
11010  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11011  "   -csv                 set output mode to 'csv'\n"
11012#if !defined(SQLITE_OMIT_DESERIALIZE)
11013  "   -deserialize         open the database using sqlite3_deserialize()\n"
11014#endif
11015  "   -echo                print commands before execution\n"
11016  "   -init FILENAME       read/process named file\n"
11017  "   -[no]header          turn headers on or off\n"
11018#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11019  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11020#endif
11021  "   -help                show this message\n"
11022  "   -html                set output mode to HTML\n"
11023  "   -interactive         force interactive I/O\n"
11024  "   -json                set output mode to 'json'\n"
11025  "   -line                set output mode to 'line'\n"
11026  "   -list                set output mode to 'list'\n"
11027  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11028  "   -markdown            set output mode to 'markdown'\n"
11029#if !defined(SQLITE_OMIT_DESERIALIZE)
11030  "   -maxsize N           maximum size for a --deserialize database\n"
11031#endif
11032  "   -memtrace            trace all memory allocations and deallocations\n"
11033  "   -mmap N              default mmap size set to N\n"
11034#ifdef SQLITE_ENABLE_MULTIPLEX
11035  "   -multiplex           enable the multiplexor VFS\n"
11036#endif
11037  "   -newline SEP         set output row separator. Default: '\\n'\n"
11038  "   -nofollow            refuse to open symbolic links to database files\n"
11039  "   -nonce STRING        set the safe-mode escape nonce\n"
11040  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11041  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11042  "   -quote               set output mode to 'quote'\n"
11043  "   -readonly            open the database read-only\n"
11044  "   -safe                enable safe-mode\n"
11045  "   -separator SEP       set output column separator. Default: '|'\n"
11046#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11047  "   -sorterref SIZE      sorter references threshold size\n"
11048#endif
11049  "   -stats               print memory stats before each finalize\n"
11050  "   -table               set output mode to 'table'\n"
11051  "   -tabs                set output mode to 'tabs'\n"
11052  "   -version             show SQLite version\n"
11053  "   -vfs NAME            use NAME as the default VFS\n"
11054#ifdef SQLITE_ENABLE_VFSTRACE
11055  "   -vfstrace            enable tracing of all VFS calls\n"
11056#endif
11057#ifdef SQLITE_HAVE_ZLIB
11058  "   -zip                 open the file as a ZIP Archive\n"
11059#endif
11060;
11061static void usage(int showDetail){
11062  utf8_printf(stderr,
11063      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11064      "FILENAME is the name of an SQLite database. A new database is created\n"
11065      "if the file does not previously exist.\n", Argv0);
11066  if( showDetail ){
11067    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11068  }else{
11069    raw_printf(stderr, "Use the -help option for additional information\n");
11070  }
11071  exit(1);
11072}
11073
11074/*
11075** Internal check:  Verify that the SQLite is uninitialized.  Print a
11076** error message if it is initialized.
11077*/
11078static void verify_uninitialized(void){
11079  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11080    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11081                        " initialization.\n");
11082  }
11083}
11084
11085/*
11086** Initialize the state information in data
11087*/
11088static void main_init(ShellState *data) {
11089  memset(data, 0, sizeof(*data));
11090  data->normalMode = data->cMode = data->mode = MODE_List;
11091  data->autoExplain = 1;
11092  data->pAuxDb = &data->aAuxDb[0];
11093  memcpy(data->colSeparator,SEP_Column, 2);
11094  memcpy(data->rowSeparator,SEP_Row, 2);
11095  data->showHeader = 0;
11096  data->shellFlgs = SHFLG_Lookaside;
11097  verify_uninitialized();
11098  sqlite3_config(SQLITE_CONFIG_URI, 1);
11099  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11100  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11101  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11102  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11103}
11104
11105/*
11106** Output text to the console in a font that attracts extra attention.
11107*/
11108#ifdef _WIN32
11109static void printBold(const char *zText){
11110#if !SQLITE_OS_WINRT
11111  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
11112  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
11113  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
11114  SetConsoleTextAttribute(out,
11115         FOREGROUND_RED|FOREGROUND_INTENSITY
11116  );
11117#endif
11118  printf("%s", zText);
11119#if !SQLITE_OS_WINRT
11120  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
11121#endif
11122}
11123#else
11124static void printBold(const char *zText){
11125  printf("\033[1m%s\033[0m", zText);
11126}
11127#endif
11128
11129/*
11130** Get the argument to an --option.  Throw an error and die if no argument
11131** is available.
11132*/
11133static char *cmdline_option_value(int argc, char **argv, int i){
11134  if( i==argc ){
11135    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
11136            argv[0], argv[argc-1]);
11137    exit(1);
11138  }
11139  return argv[i];
11140}
11141
11142#ifndef SQLITE_SHELL_IS_UTF8
11143#  if (defined(_WIN32) || defined(WIN32)) \
11144   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
11145#    define SQLITE_SHELL_IS_UTF8          (0)
11146#  else
11147#    define SQLITE_SHELL_IS_UTF8          (1)
11148#  endif
11149#endif
11150
11151#if SQLITE_SHELL_IS_UTF8
11152int SQLITE_CDECL main(int argc, char **argv){
11153#else
11154int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
11155  char **argv;
11156#endif
11157  char *zErrMsg = 0;
11158  ShellState data;
11159  const char *zInitFile = 0;
11160  int i;
11161  int rc = 0;
11162  int warnInmemoryDb = 0;
11163  int readStdin = 1;
11164  int nCmd = 0;
11165  char **azCmd = 0;
11166  const char *zVfs = 0;           /* Value of -vfs command-line option */
11167#if !SQLITE_SHELL_IS_UTF8
11168  char **argvToFree = 0;
11169  int argcToFree = 0;
11170#endif
11171
11172  setBinaryMode(stdin, 0);
11173  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
11174  stdin_is_interactive = isatty(0);
11175  stdout_is_console = isatty(1);
11176
11177#ifdef SQLITE_DEBUG
11178  registerOomSimulator();
11179#endif
11180
11181#if !defined(_WIN32_WCE)
11182  if( getenv("SQLITE_DEBUG_BREAK") ){
11183    if( isatty(0) && isatty(2) ){
11184      fprintf(stderr,
11185          "attach debugger to process %d and press any key to continue.\n",
11186          GETPID());
11187      fgetc(stdin);
11188    }else{
11189#if defined(_WIN32) || defined(WIN32)
11190#if SQLITE_OS_WINRT
11191      __debugbreak();
11192#else
11193      DebugBreak();
11194#endif
11195#elif defined(SIGTRAP)
11196      raise(SIGTRAP);
11197#endif
11198    }
11199  }
11200#endif
11201
11202#if USE_SYSTEM_SQLITE+0!=1
11203  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
11204    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
11205            sqlite3_sourceid(), SQLITE_SOURCE_ID);
11206    exit(1);
11207  }
11208#endif
11209  main_init(&data);
11210
11211  /* On Windows, we must translate command-line arguments into UTF-8.
11212  ** The SQLite memory allocator subsystem has to be enabled in order to
11213  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
11214  ** subsequent sqlite3_config() calls will work.  So copy all results into
11215  ** memory that does not come from the SQLite memory allocator.
11216  */
11217#if !SQLITE_SHELL_IS_UTF8
11218  sqlite3_initialize();
11219  argvToFree = malloc(sizeof(argv[0])*argc*2);
11220  argcToFree = argc;
11221  argv = argvToFree + argc;
11222  if( argv==0 ) shell_out_of_memory();
11223  for(i=0; i<argc; i++){
11224    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
11225    int n;
11226    if( z==0 ) shell_out_of_memory();
11227    n = (int)strlen(z);
11228    argv[i] = malloc( n+1 );
11229    if( argv[i]==0 ) shell_out_of_memory();
11230    memcpy(argv[i], z, n+1);
11231    argvToFree[i] = argv[i];
11232    sqlite3_free(z);
11233  }
11234  sqlite3_shutdown();
11235#endif
11236
11237  assert( argc>=1 && argv && argv[0] );
11238  Argv0 = argv[0];
11239
11240  /* Make sure we have a valid signal handler early, before anything
11241  ** else is done.
11242  */
11243#ifdef SIGINT
11244  signal(SIGINT, interrupt_handler);
11245#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11246  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
11247#endif
11248
11249#ifdef SQLITE_SHELL_DBNAME_PROC
11250  {
11251    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
11252    ** of a C-function that will provide the name of the database file.  Use
11253    ** this compile-time option to embed this shell program in larger
11254    ** applications. */
11255    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
11256    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
11257    warnInmemoryDb = 0;
11258  }
11259#endif
11260
11261  /* Do an initial pass through the command-line argument to locate
11262  ** the name of the database file, the name of the initialization file,
11263  ** the size of the alternative malloc heap,
11264  ** and the first command to execute.
11265  */
11266  verify_uninitialized();
11267  for(i=1; i<argc; i++){
11268    char *z;
11269    z = argv[i];
11270    if( z[0]!='-' ){
11271      if( data.aAuxDb->zDbFilename==0 ){
11272        data.aAuxDb->zDbFilename = z;
11273      }else{
11274        /* Excesss arguments are interpreted as SQL (or dot-commands) and
11275        ** mean that nothing is read from stdin */
11276        readStdin = 0;
11277        nCmd++;
11278        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
11279        if( azCmd==0 ) shell_out_of_memory();
11280        azCmd[nCmd-1] = z;
11281      }
11282    }
11283    if( z[1]=='-' ) z++;
11284    if( strcmp(z,"-separator")==0
11285     || strcmp(z,"-nullvalue")==0
11286     || strcmp(z,"-newline")==0
11287     || strcmp(z,"-cmd")==0
11288    ){
11289      (void)cmdline_option_value(argc, argv, ++i);
11290    }else if( strcmp(z,"-init")==0 ){
11291      zInitFile = cmdline_option_value(argc, argv, ++i);
11292    }else if( strcmp(z,"-batch")==0 ){
11293      /* Need to check for batch mode here to so we can avoid printing
11294      ** informational messages (like from process_sqliterc) before
11295      ** we do the actual processing of arguments later in a second pass.
11296      */
11297      stdin_is_interactive = 0;
11298    }else if( strcmp(z,"-heap")==0 ){
11299#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11300      const char *zSize;
11301      sqlite3_int64 szHeap;
11302
11303      zSize = cmdline_option_value(argc, argv, ++i);
11304      szHeap = integerValue(zSize);
11305      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
11306      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
11307#else
11308      (void)cmdline_option_value(argc, argv, ++i);
11309#endif
11310    }else if( strcmp(z,"-pagecache")==0 ){
11311      sqlite3_int64 n, sz;
11312      sz = integerValue(cmdline_option_value(argc,argv,++i));
11313      if( sz>70000 ) sz = 70000;
11314      if( sz<0 ) sz = 0;
11315      n = integerValue(cmdline_option_value(argc,argv,++i));
11316      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
11317        n = 0xffffffffffffLL/sz;
11318      }
11319      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
11320                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
11321      data.shellFlgs |= SHFLG_Pagecache;
11322    }else if( strcmp(z,"-lookaside")==0 ){
11323      int n, sz;
11324      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
11325      if( sz<0 ) sz = 0;
11326      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11327      if( n<0 ) n = 0;
11328      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
11329      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
11330    }else if( strcmp(z,"-threadsafe")==0 ){
11331      int n;
11332      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11333      switch( n ){
11334         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
11335         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
11336         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
11337      }
11338#ifdef SQLITE_ENABLE_VFSTRACE
11339    }else if( strcmp(z,"-vfstrace")==0 ){
11340      extern int vfstrace_register(
11341         const char *zTraceName,
11342         const char *zOldVfsName,
11343         int (*xOut)(const char*,void*),
11344         void *pOutArg,
11345         int makeDefault
11346      );
11347      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
11348#endif
11349#ifdef SQLITE_ENABLE_MULTIPLEX
11350    }else if( strcmp(z,"-multiplex")==0 ){
11351      extern int sqlite3_multiple_initialize(const char*,int);
11352      sqlite3_multiplex_initialize(0, 1);
11353#endif
11354    }else if( strcmp(z,"-mmap")==0 ){
11355      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11356      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
11357#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11358    }else if( strcmp(z,"-sorterref")==0 ){
11359      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11360      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11361#endif
11362    }else if( strcmp(z,"-vfs")==0 ){
11363      zVfs = cmdline_option_value(argc, argv, ++i);
11364#ifdef SQLITE_HAVE_ZLIB
11365    }else if( strcmp(z,"-zip")==0 ){
11366      data.openMode = SHELL_OPEN_ZIPFILE;
11367#endif
11368    }else if( strcmp(z,"-append")==0 ){
11369      data.openMode = SHELL_OPEN_APPENDVFS;
11370#ifndef SQLITE_OMIT_DESERIALIZE
11371    }else if( strcmp(z,"-deserialize")==0 ){
11372      data.openMode = SHELL_OPEN_DESERIALIZE;
11373    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11374      data.szMax = integerValue(argv[++i]);
11375#endif
11376    }else if( strcmp(z,"-readonly")==0 ){
11377      data.openMode = SHELL_OPEN_READONLY;
11378    }else if( strcmp(z,"-nofollow")==0 ){
11379      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11380#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11381    }else if( strncmp(z, "-A",2)==0 ){
11382      /* All remaining command-line arguments are passed to the ".archive"
11383      ** command, so ignore them */
11384      break;
11385#endif
11386    }else if( strcmp(z, "-memtrace")==0 ){
11387      sqlite3MemTraceActivate(stderr);
11388    }else if( strcmp(z,"-bail")==0 ){
11389      bail_on_error = 1;
11390    }else if( strcmp(z,"-nonce")==0 ){
11391      free(data.zNonce);
11392      data.zNonce = strdup(argv[++i]);
11393    }else if( strcmp(z,"-safe")==0 ){
11394      /* no-op - catch this on the second pass */
11395    }
11396  }
11397  verify_uninitialized();
11398
11399
11400#ifdef SQLITE_SHELL_INIT_PROC
11401  {
11402    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11403    ** of a C-function that will perform initialization actions on SQLite that
11404    ** occur just before or after sqlite3_initialize(). Use this compile-time
11405    ** option to embed this shell program in larger applications. */
11406    extern void SQLITE_SHELL_INIT_PROC(void);
11407    SQLITE_SHELL_INIT_PROC();
11408  }
11409#else
11410  /* All the sqlite3_config() calls have now been made. So it is safe
11411  ** to call sqlite3_initialize() and process any command line -vfs option. */
11412  sqlite3_initialize();
11413#endif
11414
11415  if( zVfs ){
11416    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11417    if( pVfs ){
11418      sqlite3_vfs_register(pVfs, 1);
11419    }else{
11420      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11421      exit(1);
11422    }
11423  }
11424
11425  if( data.pAuxDb->zDbFilename==0 ){
11426#ifndef SQLITE_OMIT_MEMORYDB
11427    data.pAuxDb->zDbFilename = ":memory:";
11428    warnInmemoryDb = argc==1;
11429#else
11430    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11431    return 1;
11432#endif
11433  }
11434  data.out = stdout;
11435  sqlite3_appendvfs_init(0,0,0);
11436
11437  /* Go ahead and open the database file if it already exists.  If the
11438  ** file does not exist, delay opening it.  This prevents empty database
11439  ** files from being created if a user mistypes the database name argument
11440  ** to the sqlite command-line tool.
11441  */
11442  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
11443    open_db(&data, 0);
11444  }
11445
11446  /* Process the initialization file if there is one.  If no -init option
11447  ** is given on the command line, look for a file named ~/.sqliterc and
11448  ** try to process it.
11449  */
11450  process_sqliterc(&data,zInitFile);
11451
11452  /* Make a second pass through the command-line argument and set
11453  ** options.  This second pass is delayed until after the initialization
11454  ** file is processed so that the command-line arguments will override
11455  ** settings in the initialization file.
11456  */
11457  for(i=1; i<argc; i++){
11458    char *z = argv[i];
11459    if( z[0]!='-' ) continue;
11460    if( z[1]=='-' ){ z++; }
11461    if( strcmp(z,"-init")==0 ){
11462      i++;
11463    }else if( strcmp(z,"-html")==0 ){
11464      data.mode = MODE_Html;
11465    }else if( strcmp(z,"-list")==0 ){
11466      data.mode = MODE_List;
11467    }else if( strcmp(z,"-quote")==0 ){
11468      data.mode = MODE_Quote;
11469      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11470      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11471    }else if( strcmp(z,"-line")==0 ){
11472      data.mode = MODE_Line;
11473    }else if( strcmp(z,"-column")==0 ){
11474      data.mode = MODE_Column;
11475    }else if( strcmp(z,"-json")==0 ){
11476      data.mode = MODE_Json;
11477    }else if( strcmp(z,"-markdown")==0 ){
11478      data.mode = MODE_Markdown;
11479    }else if( strcmp(z,"-table")==0 ){
11480      data.mode = MODE_Table;
11481    }else if( strcmp(z,"-box")==0 ){
11482      data.mode = MODE_Box;
11483    }else if( strcmp(z,"-csv")==0 ){
11484      data.mode = MODE_Csv;
11485      memcpy(data.colSeparator,",",2);
11486#ifdef SQLITE_HAVE_ZLIB
11487    }else if( strcmp(z,"-zip")==0 ){
11488      data.openMode = SHELL_OPEN_ZIPFILE;
11489#endif
11490    }else if( strcmp(z,"-append")==0 ){
11491      data.openMode = SHELL_OPEN_APPENDVFS;
11492#ifndef SQLITE_OMIT_DESERIALIZE
11493    }else if( strcmp(z,"-deserialize")==0 ){
11494      data.openMode = SHELL_OPEN_DESERIALIZE;
11495    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11496      data.szMax = integerValue(argv[++i]);
11497#endif
11498    }else if( strcmp(z,"-readonly")==0 ){
11499      data.openMode = SHELL_OPEN_READONLY;
11500    }else if( strcmp(z,"-nofollow")==0 ){
11501      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11502    }else if( strcmp(z,"-ascii")==0 ){
11503      data.mode = MODE_Ascii;
11504      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
11505      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
11506    }else if( strcmp(z,"-tabs")==0 ){
11507      data.mode = MODE_List;
11508      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
11509      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11510    }else if( strcmp(z,"-separator")==0 ){
11511      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11512                       "%s",cmdline_option_value(argc,argv,++i));
11513    }else if( strcmp(z,"-newline")==0 ){
11514      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11515                       "%s",cmdline_option_value(argc,argv,++i));
11516    }else if( strcmp(z,"-nullvalue")==0 ){
11517      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11518                       "%s",cmdline_option_value(argc,argv,++i));
11519    }else if( strcmp(z,"-header")==0 ){
11520      data.showHeader = 1;
11521      ShellSetFlag(&data, SHFLG_HeaderSet);
11522     }else if( strcmp(z,"-noheader")==0 ){
11523      data.showHeader = 0;
11524      ShellSetFlag(&data, SHFLG_HeaderSet);
11525    }else if( strcmp(z,"-echo")==0 ){
11526      ShellSetFlag(&data, SHFLG_Echo);
11527    }else if( strcmp(z,"-eqp")==0 ){
11528      data.autoEQP = AUTOEQP_on;
11529    }else if( strcmp(z,"-eqpfull")==0 ){
11530      data.autoEQP = AUTOEQP_full;
11531    }else if( strcmp(z,"-stats")==0 ){
11532      data.statsOn = 1;
11533    }else if( strcmp(z,"-scanstats")==0 ){
11534      data.scanstatsOn = 1;
11535    }else if( strcmp(z,"-backslash")==0 ){
11536      /* Undocumented command-line option: -backslash
11537      ** Causes C-style backslash escapes to be evaluated in SQL statements
11538      ** prior to sending the SQL into SQLite.  Useful for injecting
11539      ** crazy bytes in the middle of SQL statements for testing and debugging.
11540      */
11541      ShellSetFlag(&data, SHFLG_Backslash);
11542    }else if( strcmp(z,"-bail")==0 ){
11543      /* No-op.  The bail_on_error flag should already be set. */
11544    }else if( strcmp(z,"-version")==0 ){
11545      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11546      return 0;
11547    }else if( strcmp(z,"-interactive")==0 ){
11548      stdin_is_interactive = 1;
11549    }else if( strcmp(z,"-batch")==0 ){
11550      stdin_is_interactive = 0;
11551    }else if( strcmp(z,"-heap")==0 ){
11552      i++;
11553    }else if( strcmp(z,"-pagecache")==0 ){
11554      i+=2;
11555    }else if( strcmp(z,"-lookaside")==0 ){
11556      i+=2;
11557    }else if( strcmp(z,"-threadsafe")==0 ){
11558      i+=2;
11559    }else if( strcmp(z,"-nonce")==0 ){
11560      i += 2;
11561    }else if( strcmp(z,"-mmap")==0 ){
11562      i++;
11563    }else if( strcmp(z,"-memtrace")==0 ){
11564      i++;
11565#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11566    }else if( strcmp(z,"-sorterref")==0 ){
11567      i++;
11568#endif
11569    }else if( strcmp(z,"-vfs")==0 ){
11570      i++;
11571#ifdef SQLITE_ENABLE_VFSTRACE
11572    }else if( strcmp(z,"-vfstrace")==0 ){
11573      i++;
11574#endif
11575#ifdef SQLITE_ENABLE_MULTIPLEX
11576    }else if( strcmp(z,"-multiplex")==0 ){
11577      i++;
11578#endif
11579    }else if( strcmp(z,"-help")==0 ){
11580      usage(1);
11581    }else if( strcmp(z,"-cmd")==0 ){
11582      /* Run commands that follow -cmd first and separately from commands
11583      ** that simply appear on the command-line.  This seems goofy.  It would
11584      ** be better if all commands ran in the order that they appear.  But
11585      ** we retain the goofy behavior for historical compatibility. */
11586      if( i==argc-1 ) break;
11587      z = cmdline_option_value(argc,argv,++i);
11588      if( z[0]=='.' ){
11589        rc = do_meta_command(z, &data);
11590        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11591      }else{
11592        open_db(&data, 0);
11593        rc = shell_exec(&data, z, &zErrMsg);
11594        if( zErrMsg!=0 ){
11595          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11596          if( bail_on_error ) return rc!=0 ? rc : 1;
11597        }else if( rc!=0 ){
11598          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11599          if( bail_on_error ) return rc;
11600        }
11601      }
11602#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11603    }else if( strncmp(z, "-A", 2)==0 ){
11604      if( nCmd>0 ){
11605        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11606                            " with \"%s\"\n", z);
11607        return 1;
11608      }
11609      open_db(&data, OPEN_DB_ZIPFILE);
11610      if( z[2] ){
11611        argv[i] = &z[2];
11612        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11613      }else{
11614        arDotCommand(&data, 1, argv+i, argc-i);
11615      }
11616      readStdin = 0;
11617      break;
11618#endif
11619    }else if( strcmp(z,"-safe")==0 ){
11620      data.bSafeMode = data.bSafeModePersist = 1;
11621    }else{
11622      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11623      raw_printf(stderr,"Use -help for a list of options.\n");
11624      return 1;
11625    }
11626    data.cMode = data.mode;
11627  }
11628
11629  if( !readStdin ){
11630    /* Run all arguments that do not begin with '-' as if they were separate
11631    ** command-line inputs, except for the argToSkip argument which contains
11632    ** the database filename.
11633    */
11634    for(i=0; i<nCmd; i++){
11635      if( azCmd[i][0]=='.' ){
11636        rc = do_meta_command(azCmd[i], &data);
11637        if( rc ){
11638          free(azCmd);
11639          return rc==2 ? 0 : rc;
11640        }
11641      }else{
11642        open_db(&data, 0);
11643        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11644        if( zErrMsg || rc ){
11645          if( zErrMsg!=0 ){
11646            utf8_printf(stderr,"Error: %s\n", zErrMsg);
11647          }else{
11648            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11649          }
11650          sqlite3_free(zErrMsg);
11651          free(azCmd);
11652          return rc!=0 ? rc : 1;
11653        }
11654      }
11655    }
11656  }else{
11657    /* Run commands received from standard input
11658    */
11659    if( stdin_is_interactive ){
11660      char *zHome;
11661      char *zHistory;
11662      int nHistory;
11663      printf(
11664        "SQLite version %s %.19s\n" /*extra-version-info*/
11665        "Enter \".help\" for usage hints.\n",
11666        sqlite3_libversion(), sqlite3_sourceid()
11667      );
11668      if( warnInmemoryDb ){
11669        printf("Connected to a ");
11670        printBold("transient in-memory database");
11671        printf(".\nUse \".open FILENAME\" to reopen on a "
11672               "persistent database.\n");
11673      }
11674      zHistory = getenv("SQLITE_HISTORY");
11675      if( zHistory ){
11676        zHistory = strdup(zHistory);
11677      }else if( (zHome = find_home_dir(0))!=0 ){
11678        nHistory = strlen30(zHome) + 20;
11679        if( (zHistory = malloc(nHistory))!=0 ){
11680          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11681        }
11682      }
11683      if( zHistory ){ shell_read_history(zHistory); }
11684#if HAVE_READLINE || HAVE_EDITLINE
11685      rl_attempted_completion_function = readline_completion;
11686#elif HAVE_LINENOISE
11687      linenoiseSetCompletionCallback(linenoise_completion);
11688#endif
11689      data.in = 0;
11690      rc = process_input(&data);
11691      if( zHistory ){
11692        shell_stifle_history(2000);
11693        shell_write_history(zHistory);
11694        free(zHistory);
11695      }
11696    }else{
11697      data.in = stdin;
11698      rc = process_input(&data);
11699    }
11700  }
11701  free(azCmd);
11702  set_table_name(&data, 0);
11703  if( data.db ){
11704    session_close_all(&data, -1);
11705    close_db(data.db);
11706  }
11707  for(i=0; i<ArraySize(data.aAuxDb); i++){
11708    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
11709    if( data.aAuxDb[i].db ){
11710      session_close_all(&data, i);
11711      close_db(data.aAuxDb[i].db);
11712    }
11713  }
11714  find_home_dir(1);
11715  output_reset(&data);
11716  data.doXdgOpen = 0;
11717  clearTempFile(&data);
11718#if !SQLITE_SHELL_IS_UTF8
11719  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11720  free(argvToFree);
11721#endif
11722  free(data.colWidth);
11723  free(data.zNonce);
11724  /* Clear the global data structure so that valgrind will detect memory
11725  ** leaks */
11726  memset(&data, 0, sizeof(data));
11727  return rc;
11728}
11729