xref: /sqlite-3.40.0/src/shell.c.in (revision c436b305)
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code.  In place of
5** a legal notice, here is a blessing:
6**
7**    May you do good and not evil.
8**    May you find forgiveness for yourself and forgive others.
9**    May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14*/
15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16/* This needs to come before any includes for MSVC compiler */
17#define _CRT_SECURE_NO_WARNINGS
18#endif
19
20/*
21** Optionally #include a user-defined header, whereby compilation options
22** may be set prior to where they take effect, but after platform setup.
23** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
24** file. Note that this macro has a like effect on sqlite3.c compilation.
25*/
26#ifdef SQLITE_CUSTOM_INCLUDE
27# define INC_STRINGIFY_(f) #f
28# define INC_STRINGIFY(f) INC_STRINGIFY_(f)
29# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
30#endif
31
32/*
33** Determine if we are dealing with WinRT, which provides only a subset of
34** the full Win32 API.
35*/
36#if !defined(SQLITE_OS_WINRT)
37# define SQLITE_OS_WINRT 0
38#endif
39
40/*
41** Warning pragmas copied from msvc.h in the core.
42*/
43#if defined(_MSC_VER)
44#pragma warning(disable : 4054)
45#pragma warning(disable : 4055)
46#pragma warning(disable : 4100)
47#pragma warning(disable : 4127)
48#pragma warning(disable : 4130)
49#pragma warning(disable : 4152)
50#pragma warning(disable : 4189)
51#pragma warning(disable : 4206)
52#pragma warning(disable : 4210)
53#pragma warning(disable : 4232)
54#pragma warning(disable : 4244)
55#pragma warning(disable : 4305)
56#pragma warning(disable : 4306)
57#pragma warning(disable : 4702)
58#pragma warning(disable : 4706)
59#endif /* defined(_MSC_VER) */
60
61/*
62** No support for loadable extensions in VxWorks.
63*/
64#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
65# define SQLITE_OMIT_LOAD_EXTENSION 1
66#endif
67
68/*
69** Enable large-file support for fopen() and friends on unix.
70*/
71#ifndef SQLITE_DISABLE_LFS
72# define _LARGE_FILE       1
73# ifndef _FILE_OFFSET_BITS
74#   define _FILE_OFFSET_BITS 64
75# endif
76# define _LARGEFILE_SOURCE 1
77#endif
78
79#include <stdlib.h>
80#include <string.h>
81#include <stdio.h>
82#include <assert.h>
83#include "sqlite3.h"
84typedef sqlite3_int64 i64;
85typedef sqlite3_uint64 u64;
86typedef unsigned char u8;
87#if SQLITE_USER_AUTHENTICATION
88# include "sqlite3userauth.h"
89#endif
90#include <ctype.h>
91#include <stdarg.h>
92
93#if !defined(_WIN32) && !defined(WIN32)
94# include <signal.h>
95# if !defined(__RTP__) && !defined(_WRS_KERNEL)
96#  include <pwd.h>
97# endif
98#endif
99#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
100# include <unistd.h>
101# include <dirent.h>
102# define GETPID getpid
103# if defined(__MINGW32__)
104#  define DIRENT dirent
105#  ifndef S_ISLNK
106#   define S_ISLNK(mode) (0)
107#  endif
108# endif
109#else
110# define GETPID (int)GetCurrentProcessId
111#endif
112#include <sys/types.h>
113#include <sys/stat.h>
114
115#if HAVE_READLINE
116# include <readline/readline.h>
117# include <readline/history.h>
118#endif
119
120#if HAVE_EDITLINE
121# include <editline/readline.h>
122#endif
123
124#if HAVE_EDITLINE || HAVE_READLINE
125
126# define shell_add_history(X) add_history(X)
127# define shell_read_history(X) read_history(X)
128# define shell_write_history(X) write_history(X)
129# define shell_stifle_history(X) stifle_history(X)
130# define shell_readline(X) readline(X)
131
132#elif HAVE_LINENOISE
133
134# include "linenoise.h"
135# define shell_add_history(X) linenoiseHistoryAdd(X)
136# define shell_read_history(X) linenoiseHistoryLoad(X)
137# define shell_write_history(X) linenoiseHistorySave(X)
138# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
139# define shell_readline(X) linenoise(X)
140
141#else
142
143# define shell_read_history(X)
144# define shell_write_history(X)
145# define shell_stifle_history(X)
146
147# define SHELL_USE_LOCAL_GETLINE 1
148#endif
149
150
151#if defined(_WIN32) || defined(WIN32)
152# if SQLITE_OS_WINRT
153#  define SQLITE_OMIT_POPEN 1
154# else
155#  include <io.h>
156#  include <fcntl.h>
157#  define isatty(h) _isatty(h)
158#  ifndef access
159#   define access(f,m) _access((f),(m))
160#  endif
161#  ifndef unlink
162#   define unlink _unlink
163#  endif
164#  ifndef strdup
165#   define strdup _strdup
166#  endif
167#  undef popen
168#  define popen _popen
169#  undef pclose
170#  define pclose _pclose
171# endif
172#else
173 /* Make sure isatty() has a prototype. */
174 extern int isatty(int);
175
176# if !defined(__RTP__) && !defined(_WRS_KERNEL)
177  /* popen and pclose are not C89 functions and so are
178  ** sometimes omitted from the <stdio.h> header */
179   extern FILE *popen(const char*,const char*);
180   extern int pclose(FILE*);
181# else
182#  define SQLITE_OMIT_POPEN 1
183# endif
184#endif
185
186#if defined(_WIN32_WCE)
187/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
188 * thus we always assume that we have a console. That can be
189 * overridden with the -batch command line option.
190 */
191#define isatty(x) 1
192#endif
193
194/* ctype macros that work with signed characters */
195#define IsSpace(X)  isspace((unsigned char)X)
196#define IsDigit(X)  isdigit((unsigned char)X)
197#define ToLower(X)  (char)tolower((unsigned char)X)
198
199#if defined(_WIN32) || defined(WIN32)
200#if SQLITE_OS_WINRT
201#include <intrin.h>
202#endif
203#include <windows.h>
204
205/* string conversion routines only needed on Win32 */
206extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
207extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
208extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
209extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
210#endif
211
212/* On Windows, we normally run with output mode of TEXT so that \n characters
213** are automatically translated into \r\n.  However, this behavior needs
214** to be disabled in some cases (ex: when generating CSV output and when
215** rendering quoted strings that contain \n characters).  The following
216** routines take care of that.
217*/
218#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
219static void setBinaryMode(FILE *file, int isOutput){
220  if( isOutput ) fflush(file);
221  _setmode(_fileno(file), _O_BINARY);
222}
223static void setTextMode(FILE *file, int isOutput){
224  if( isOutput ) fflush(file);
225  _setmode(_fileno(file), _O_TEXT);
226}
227#else
228# define setBinaryMode(X,Y)
229# define setTextMode(X,Y)
230#endif
231
232
233/* True if the timer is enabled */
234static int enableTimer = 0;
235
236/* Return the current wall-clock time */
237static sqlite3_int64 timeOfDay(void){
238  static sqlite3_vfs *clockVfs = 0;
239  sqlite3_int64 t;
240  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
241  if( clockVfs==0 ) return 0;  /* Never actually happens */
242  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
243    clockVfs->xCurrentTimeInt64(clockVfs, &t);
244  }else{
245    double r;
246    clockVfs->xCurrentTime(clockVfs, &r);
247    t = (sqlite3_int64)(r*86400000.0);
248  }
249  return t;
250}
251
252#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
253#include <sys/time.h>
254#include <sys/resource.h>
255
256/* VxWorks does not support getrusage() as far as we can determine */
257#if defined(_WRS_KERNEL) || defined(__RTP__)
258struct rusage {
259  struct timeval ru_utime; /* user CPU time used */
260  struct timeval ru_stime; /* system CPU time used */
261};
262#define getrusage(A,B) memset(B,0,sizeof(*B))
263#endif
264
265/* Saved resource information for the beginning of an operation */
266static struct rusage sBegin;  /* CPU time at start */
267static sqlite3_int64 iBegin;  /* Wall-clock time at start */
268
269/*
270** Begin timing an operation
271*/
272static void beginTimer(void){
273  if( enableTimer ){
274    getrusage(RUSAGE_SELF, &sBegin);
275    iBegin = timeOfDay();
276  }
277}
278
279/* Return the difference of two time_structs in seconds */
280static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
281  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
282         (double)(pEnd->tv_sec - pStart->tv_sec);
283}
284
285/*
286** Print the timing results.
287*/
288static void endTimer(void){
289  if( enableTimer ){
290    sqlite3_int64 iEnd = timeOfDay();
291    struct rusage sEnd;
292    getrusage(RUSAGE_SELF, &sEnd);
293    printf("Run Time: real %.3f user %f sys %f\n",
294       (iEnd - iBegin)*0.001,
295       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
296       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
297  }
298}
299
300#define BEGIN_TIMER beginTimer()
301#define END_TIMER endTimer()
302#define HAS_TIMER 1
303
304#elif (defined(_WIN32) || defined(WIN32))
305
306/* Saved resource information for the beginning of an operation */
307static HANDLE hProcess;
308static FILETIME ftKernelBegin;
309static FILETIME ftUserBegin;
310static sqlite3_int64 ftWallBegin;
311typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
312                                    LPFILETIME, LPFILETIME);
313static GETPROCTIMES getProcessTimesAddr = NULL;
314
315/*
316** Check to see if we have timer support.  Return 1 if necessary
317** support found (or found previously).
318*/
319static int hasTimer(void){
320  if( getProcessTimesAddr ){
321    return 1;
322  } else {
323#if !SQLITE_OS_WINRT
324    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
325    ** versions. See if the version we are running on has it, and if it
326    ** does, save off a pointer to it and the current process handle.
327    */
328    hProcess = GetCurrentProcess();
329    if( hProcess ){
330      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
331      if( NULL != hinstLib ){
332        getProcessTimesAddr =
333            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
334        if( NULL != getProcessTimesAddr ){
335          return 1;
336        }
337        FreeLibrary(hinstLib);
338      }
339    }
340#endif
341  }
342  return 0;
343}
344
345/*
346** Begin timing an operation
347*/
348static void beginTimer(void){
349  if( enableTimer && getProcessTimesAddr ){
350    FILETIME ftCreation, ftExit;
351    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
352                        &ftKernelBegin,&ftUserBegin);
353    ftWallBegin = timeOfDay();
354  }
355}
356
357/* Return the difference of two FILETIME structs in seconds */
358static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
359  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
360  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
361  return (double) ((i64End - i64Start) / 10000000.0);
362}
363
364/*
365** Print the timing results.
366*/
367static void endTimer(void){
368  if( enableTimer && getProcessTimesAddr){
369    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
370    sqlite3_int64 ftWallEnd = timeOfDay();
371    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
372    printf("Run Time: real %.3f user %f sys %f\n",
373       (ftWallEnd - ftWallBegin)*0.001,
374       timeDiff(&ftUserBegin, &ftUserEnd),
375       timeDiff(&ftKernelBegin, &ftKernelEnd));
376  }
377}
378
379#define BEGIN_TIMER beginTimer()
380#define END_TIMER endTimer()
381#define HAS_TIMER hasTimer()
382
383#else
384#define BEGIN_TIMER
385#define END_TIMER
386#define HAS_TIMER 0
387#endif
388
389/*
390** Used to prevent warnings about unused parameters
391*/
392#define UNUSED_PARAMETER(x) (void)(x)
393
394/*
395** Number of elements in an array
396*/
397#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
398
399/*
400** If the following flag is set, then command execution stops
401** at an error if we are not interactive.
402*/
403static int bail_on_error = 0;
404
405/*
406** Threat stdin as an interactive input if the following variable
407** is true.  Otherwise, assume stdin is connected to a file or pipe.
408*/
409static int stdin_is_interactive = 1;
410
411/*
412** On Windows systems we have to know if standard output is a console
413** in order to translate UTF-8 into MBCS.  The following variable is
414** true if translation is required.
415*/
416static int stdout_is_console = 1;
417
418/*
419** The following is the open SQLite database.  We make a pointer
420** to this database a static variable so that it can be accessed
421** by the SIGINT handler to interrupt database processing.
422*/
423static sqlite3 *globalDb = 0;
424
425/*
426** True if an interrupt (Control-C) has been received.
427*/
428static volatile int seenInterrupt = 0;
429
430#ifdef SQLITE_DEBUG
431/*
432** Out-of-memory simulator variables
433*/
434static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
435static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
436static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
437#endif /* SQLITE_DEBUG */
438
439/*
440** This is the name of our program. It is set in main(), used
441** in a number of other places, mostly for error messages.
442*/
443static char *Argv0;
444
445/*
446** Prompt strings. Initialized in main. Settable with
447**   .prompt main continue
448*/
449static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
450static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
451
452/*
453** Render output like fprintf().  Except, if the output is going to the
454** console and if this is running on a Windows machine, translate the
455** output from UTF-8 into MBCS.
456*/
457#if defined(_WIN32) || defined(WIN32)
458void utf8_printf(FILE *out, const char *zFormat, ...){
459  va_list ap;
460  va_start(ap, zFormat);
461  if( stdout_is_console && (out==stdout || out==stderr) ){
462    char *z1 = sqlite3_vmprintf(zFormat, ap);
463    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
464    sqlite3_free(z1);
465    fputs(z2, out);
466    sqlite3_free(z2);
467  }else{
468    vfprintf(out, zFormat, ap);
469  }
470  va_end(ap);
471}
472#elif !defined(utf8_printf)
473# define utf8_printf fprintf
474#endif
475
476/*
477** Render output like fprintf().  This should not be used on anything that
478** includes string formatting (e.g. "%s").
479*/
480#if !defined(raw_printf)
481# define raw_printf fprintf
482#endif
483
484/* Indicate out-of-memory and exit. */
485static void shell_out_of_memory(void){
486  raw_printf(stderr,"Error: out of memory\n");
487  exit(1);
488}
489
490#ifdef SQLITE_DEBUG
491/* This routine is called when a simulated OOM occurs.  It is broken
492** out as a separate routine to make it easy to set a breakpoint on
493** the OOM
494*/
495void shellOomFault(void){
496  if( oomRepeat>0 ){
497    oomRepeat--;
498  }else{
499    oomCounter--;
500  }
501}
502#endif /* SQLITE_DEBUG */
503
504#ifdef SQLITE_DEBUG
505/* This routine is a replacement malloc() that is used to simulate
506** Out-Of-Memory (OOM) errors for testing purposes.
507*/
508static void *oomMalloc(int nByte){
509  if( oomCounter ){
510    if( oomCounter==1 ){
511      shellOomFault();
512      return 0;
513    }else{
514      oomCounter--;
515    }
516  }
517  return defaultMalloc(nByte);
518}
519#endif /* SQLITE_DEBUG */
520
521#ifdef SQLITE_DEBUG
522/* Register the OOM simulator.  This must occur before any memory
523** allocations */
524static void registerOomSimulator(void){
525  sqlite3_mem_methods mem;
526  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
527  defaultMalloc = mem.xMalloc;
528  mem.xMalloc = oomMalloc;
529  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
530}
531#endif
532
533/*
534** Write I/O traces to the following stream.
535*/
536#ifdef SQLITE_ENABLE_IOTRACE
537static FILE *iotrace = 0;
538#endif
539
540/*
541** This routine works like printf in that its first argument is a
542** format string and subsequent arguments are values to be substituted
543** in place of % fields.  The result of formatting this string
544** is written to iotrace.
545*/
546#ifdef SQLITE_ENABLE_IOTRACE
547static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
548  va_list ap;
549  char *z;
550  if( iotrace==0 ) return;
551  va_start(ap, zFormat);
552  z = sqlite3_vmprintf(zFormat, ap);
553  va_end(ap);
554  utf8_printf(iotrace, "%s", z);
555  sqlite3_free(z);
556}
557#endif
558
559/*
560** Output string zUtf to stream pOut as w characters.  If w is negative,
561** then right-justify the text.  W is the width in UTF-8 characters, not
562** in bytes.  This is different from the %*.*s specification in printf
563** since with %*.*s the width is measured in bytes, not characters.
564*/
565static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
566  int i;
567  int n;
568  int aw = w<0 ? -w : w;
569  for(i=n=0; zUtf[i]; i++){
570    if( (zUtf[i]&0xc0)!=0x80 ){
571      n++;
572      if( n==aw ){
573        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
574        break;
575      }
576    }
577  }
578  if( n>=aw ){
579    utf8_printf(pOut, "%.*s", i, zUtf);
580  }else if( w<0 ){
581    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
582  }else{
583    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
584  }
585}
586
587
588/*
589** Determines if a string is a number of not.
590*/
591static int isNumber(const char *z, int *realnum){
592  if( *z=='-' || *z=='+' ) z++;
593  if( !IsDigit(*z) ){
594    return 0;
595  }
596  z++;
597  if( realnum ) *realnum = 0;
598  while( IsDigit(*z) ){ z++; }
599  if( *z=='.' ){
600    z++;
601    if( !IsDigit(*z) ) return 0;
602    while( IsDigit(*z) ){ z++; }
603    if( realnum ) *realnum = 1;
604  }
605  if( *z=='e' || *z=='E' ){
606    z++;
607    if( *z=='+' || *z=='-' ) z++;
608    if( !IsDigit(*z) ) return 0;
609    while( IsDigit(*z) ){ z++; }
610    if( realnum ) *realnum = 1;
611  }
612  return *z==0;
613}
614
615/*
616** Compute a string length that is limited to what can be stored in
617** lower 30 bits of a 32-bit signed integer.
618*/
619static int strlen30(const char *z){
620  const char *z2 = z;
621  while( *z2 ){ z2++; }
622  return 0x3fffffff & (int)(z2 - z);
623}
624
625/*
626** Return the length of a string in characters.  Multibyte UTF8 characters
627** count as a single character.
628*/
629static int strlenChar(const char *z){
630  int n = 0;
631  while( *z ){
632    if( (0xc0&*(z++))!=0x80 ) n++;
633  }
634  return n;
635}
636
637/*
638** Return open FILE * if zFile exists, can be opened for read
639** and is an ordinary file or a character stream source.
640** Otherwise return 0.
641*/
642static FILE * openChrSource(const char *zFile){
643#ifdef _WIN32
644  struct _stat x = {0};
645# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
646  /* On Windows, open first, then check the stream nature. This order
647  ** is necessary because _stat() and sibs, when checking a named pipe,
648  ** effectively break the pipe as its supplier sees it. */
649  FILE *rv = fopen(zFile, "rb");
650  if( rv==0 ) return 0;
651  if( _fstat(_fileno(rv), &x) != 0
652      || !STAT_CHR_SRC(x.st_mode)){
653    fclose(rv);
654    rv = 0;
655  }
656  return rv;
657#else
658  struct stat x = {0};
659  int rc = stat(zFile, &x);
660# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
661  if( rc!=0 ) return 0;
662  if( STAT_CHR_SRC(x.st_mode) ){
663    return fopen(zFile, "rb");
664  }else{
665    return 0;
666  }
667#endif
668#undef STAT_CHR_SRC
669}
670
671/*
672** This routine reads a line of text from FILE in, stores
673** the text in memory obtained from malloc() and returns a pointer
674** to the text.  NULL is returned at end of file, or if malloc()
675** fails.
676**
677** If zLine is not NULL then it is a malloced buffer returned from
678** a previous call to this routine that may be reused.
679*/
680static char *local_getline(char *zLine, FILE *in){
681  int nLine = zLine==0 ? 0 : 100;
682  int n = 0;
683
684  while( 1 ){
685    if( n+100>nLine ){
686      nLine = nLine*2 + 100;
687      zLine = realloc(zLine, nLine);
688      if( zLine==0 ) shell_out_of_memory();
689    }
690    if( fgets(&zLine[n], nLine - n, in)==0 ){
691      if( n==0 ){
692        free(zLine);
693        return 0;
694      }
695      zLine[n] = 0;
696      break;
697    }
698    while( zLine[n] ) n++;
699    if( n>0 && zLine[n-1]=='\n' ){
700      n--;
701      if( n>0 && zLine[n-1]=='\r' ) n--;
702      zLine[n] = 0;
703      break;
704    }
705  }
706#if defined(_WIN32) || defined(WIN32)
707  /* For interactive input on Windows systems, translate the
708  ** multi-byte characterset characters into UTF-8. */
709  if( stdin_is_interactive && in==stdin ){
710    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
711    if( zTrans ){
712      int nTrans = strlen30(zTrans)+1;
713      if( nTrans>nLine ){
714        zLine = realloc(zLine, nTrans);
715        if( zLine==0 ) shell_out_of_memory();
716      }
717      memcpy(zLine, zTrans, nTrans);
718      sqlite3_free(zTrans);
719    }
720  }
721#endif /* defined(_WIN32) || defined(WIN32) */
722  return zLine;
723}
724
725/*
726** Retrieve a single line of input text.
727**
728** If in==0 then read from standard input and prompt before each line.
729** If isContinuation is true, then a continuation prompt is appropriate.
730** If isContinuation is zero, then the main prompt should be used.
731**
732** If zPrior is not NULL then it is a buffer from a prior call to this
733** routine that can be reused.
734**
735** The result is stored in space obtained from malloc() and must either
736** be freed by the caller or else passed back into this routine via the
737** zPrior argument for reuse.
738*/
739static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
740  char *zPrompt;
741  char *zResult;
742  if( in!=0 ){
743    zResult = local_getline(zPrior, in);
744  }else{
745    zPrompt = isContinuation ? continuePrompt : mainPrompt;
746#if SHELL_USE_LOCAL_GETLINE
747    printf("%s", zPrompt);
748    fflush(stdout);
749    zResult = local_getline(zPrior, stdin);
750#else
751    free(zPrior);
752    zResult = shell_readline(zPrompt);
753    if( zResult && *zResult ) shell_add_history(zResult);
754#endif
755  }
756  return zResult;
757}
758
759
760/*
761** Return the value of a hexadecimal digit.  Return -1 if the input
762** is not a hex digit.
763*/
764static int hexDigitValue(char c){
765  if( c>='0' && c<='9' ) return c - '0';
766  if( c>='a' && c<='f' ) return c - 'a' + 10;
767  if( c>='A' && c<='F' ) return c - 'A' + 10;
768  return -1;
769}
770
771/*
772** Interpret zArg as an integer value, possibly with suffixes.
773*/
774static sqlite3_int64 integerValue(const char *zArg){
775  sqlite3_int64 v = 0;
776  static const struct { char *zSuffix; int iMult; } aMult[] = {
777    { "KiB", 1024 },
778    { "MiB", 1024*1024 },
779    { "GiB", 1024*1024*1024 },
780    { "KB",  1000 },
781    { "MB",  1000000 },
782    { "GB",  1000000000 },
783    { "K",   1000 },
784    { "M",   1000000 },
785    { "G",   1000000000 },
786  };
787  int i;
788  int isNeg = 0;
789  if( zArg[0]=='-' ){
790    isNeg = 1;
791    zArg++;
792  }else if( zArg[0]=='+' ){
793    zArg++;
794  }
795  if( zArg[0]=='0' && zArg[1]=='x' ){
796    int x;
797    zArg += 2;
798    while( (x = hexDigitValue(zArg[0]))>=0 ){
799      v = (v<<4) + x;
800      zArg++;
801    }
802  }else{
803    while( IsDigit(zArg[0]) ){
804      v = v*10 + zArg[0] - '0';
805      zArg++;
806    }
807  }
808  for(i=0; i<ArraySize(aMult); i++){
809    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
810      v *= aMult[i].iMult;
811      break;
812    }
813  }
814  return isNeg? -v : v;
815}
816
817/*
818** A variable length string to which one can append text.
819*/
820typedef struct ShellText ShellText;
821struct ShellText {
822  char *z;
823  int n;
824  int nAlloc;
825};
826
827/*
828** Initialize and destroy a ShellText object
829*/
830static void initText(ShellText *p){
831  memset(p, 0, sizeof(*p));
832}
833static void freeText(ShellText *p){
834  free(p->z);
835  initText(p);
836}
837
838/* zIn is either a pointer to a NULL-terminated string in memory obtained
839** from malloc(), or a NULL pointer. The string pointed to by zAppend is
840** added to zIn, and the result returned in memory obtained from malloc().
841** zIn, if it was not NULL, is freed.
842**
843** If the third argument, quote, is not '\0', then it is used as a
844** quote character for zAppend.
845*/
846static void appendText(ShellText *p, char const *zAppend, char quote){
847  int len;
848  int i;
849  int nAppend = strlen30(zAppend);
850
851  len = nAppend+p->n+1;
852  if( quote ){
853    len += 2;
854    for(i=0; i<nAppend; i++){
855      if( zAppend[i]==quote ) len++;
856    }
857  }
858
859  if( p->z==0 || p->n+len>=p->nAlloc ){
860    p->nAlloc = p->nAlloc*2 + len + 20;
861    p->z = realloc(p->z, p->nAlloc);
862    if( p->z==0 ) shell_out_of_memory();
863  }
864
865  if( quote ){
866    char *zCsr = p->z+p->n;
867    *zCsr++ = quote;
868    for(i=0; i<nAppend; i++){
869      *zCsr++ = zAppend[i];
870      if( zAppend[i]==quote ) *zCsr++ = quote;
871    }
872    *zCsr++ = quote;
873    p->n = (int)(zCsr - p->z);
874    *zCsr = '\0';
875  }else{
876    memcpy(p->z+p->n, zAppend, nAppend);
877    p->n += nAppend;
878    p->z[p->n] = '\0';
879  }
880}
881
882/*
883** Attempt to determine if identifier zName needs to be quoted, either
884** because it contains non-alphanumeric characters, or because it is an
885** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
886** that quoting is required.
887**
888** Return '"' if quoting is required.  Return 0 if no quoting is required.
889*/
890static char quoteChar(const char *zName){
891  int i;
892  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
893  for(i=0; zName[i]; i++){
894    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
895  }
896  return sqlite3_keyword_check(zName, i) ? '"' : 0;
897}
898
899/*
900** Construct a fake object name and column list to describe the structure
901** of the view, virtual table, or table valued function zSchema.zName.
902*/
903static char *shellFakeSchema(
904  sqlite3 *db,            /* The database connection containing the vtab */
905  const char *zSchema,    /* Schema of the database holding the vtab */
906  const char *zName       /* The name of the virtual table */
907){
908  sqlite3_stmt *pStmt = 0;
909  char *zSql;
910  ShellText s;
911  char cQuote;
912  char *zDiv = "(";
913  int nRow = 0;
914
915  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
916                         zSchema ? zSchema : "main", zName);
917  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
918  sqlite3_free(zSql);
919  initText(&s);
920  if( zSchema ){
921    cQuote = quoteChar(zSchema);
922    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
923    appendText(&s, zSchema, cQuote);
924    appendText(&s, ".", 0);
925  }
926  cQuote = quoteChar(zName);
927  appendText(&s, zName, cQuote);
928  while( sqlite3_step(pStmt)==SQLITE_ROW ){
929    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
930    nRow++;
931    appendText(&s, zDiv, 0);
932    zDiv = ",";
933    cQuote = quoteChar(zCol);
934    appendText(&s, zCol, cQuote);
935  }
936  appendText(&s, ")", 0);
937  sqlite3_finalize(pStmt);
938  if( nRow==0 ){
939    freeText(&s);
940    s.z = 0;
941  }
942  return s.z;
943}
944
945/*
946** SQL function:  shell_module_schema(X)
947**
948** Return a fake schema for the table-valued function or eponymous virtual
949** table X.
950*/
951static void shellModuleSchema(
952  sqlite3_context *pCtx,
953  int nVal,
954  sqlite3_value **apVal
955){
956  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
957  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
958  UNUSED_PARAMETER(nVal);
959  if( zFake ){
960    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
961                        -1, sqlite3_free);
962    free(zFake);
963  }
964}
965
966/*
967** SQL function:  shell_add_schema(S,X)
968**
969** Add the schema name X to the CREATE statement in S and return the result.
970** Examples:
971**
972**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
973**
974** Also works on
975**
976**    CREATE INDEX
977**    CREATE UNIQUE INDEX
978**    CREATE VIEW
979**    CREATE TRIGGER
980**    CREATE VIRTUAL TABLE
981**
982** This UDF is used by the .schema command to insert the schema name of
983** attached databases into the middle of the sqlite_schema.sql field.
984*/
985static void shellAddSchemaName(
986  sqlite3_context *pCtx,
987  int nVal,
988  sqlite3_value **apVal
989){
990  static const char *aPrefix[] = {
991     "TABLE",
992     "INDEX",
993     "UNIQUE INDEX",
994     "VIEW",
995     "TRIGGER",
996     "VIRTUAL TABLE"
997  };
998  int i = 0;
999  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
1000  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
1001  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
1002  sqlite3 *db = sqlite3_context_db_handle(pCtx);
1003  UNUSED_PARAMETER(nVal);
1004  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
1005    for(i=0; i<ArraySize(aPrefix); i++){
1006      int n = strlen30(aPrefix[i]);
1007      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1008        char *z = 0;
1009        char *zFake = 0;
1010        if( zSchema ){
1011          char cQuote = quoteChar(zSchema);
1012          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1013            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1014          }else{
1015            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1016          }
1017        }
1018        if( zName
1019         && aPrefix[i][0]=='V'
1020         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1021        ){
1022          if( z==0 ){
1023            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1024          }else{
1025            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1026          }
1027          free(zFake);
1028        }
1029        if( z ){
1030          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1031          return;
1032        }
1033      }
1034    }
1035  }
1036  sqlite3_result_value(pCtx, apVal[0]);
1037}
1038
1039/*
1040** The source code for several run-time loadable extensions is inserted
1041** below by the ../tool/mkshellc.tcl script.  Before processing that included
1042** code, we need to override some macros to make the included program code
1043** work here in the middle of this regular program.
1044*/
1045#define SQLITE_EXTENSION_INIT1
1046#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1047
1048#if defined(_WIN32) && defined(_MSC_VER)
1049INCLUDE test_windirent.h
1050INCLUDE test_windirent.c
1051#define dirent DIRENT
1052#endif
1053INCLUDE ../ext/misc/shathree.c
1054INCLUDE ../ext/misc/fileio.c
1055INCLUDE ../ext/misc/completion.c
1056INCLUDE ../ext/misc/appendvfs.c
1057INCLUDE ../ext/misc/memtrace.c
1058INCLUDE ../ext/misc/uint.c
1059INCLUDE ../ext/misc/decimal.c
1060INCLUDE ../ext/misc/ieee754.c
1061INCLUDE ../ext/misc/series.c
1062INCLUDE ../ext/misc/regexp.c
1063#ifdef SQLITE_HAVE_ZLIB
1064INCLUDE ../ext/misc/zipfile.c
1065INCLUDE ../ext/misc/sqlar.c
1066#endif
1067INCLUDE ../ext/expert/sqlite3expert.h
1068INCLUDE ../ext/expert/sqlite3expert.c
1069
1070#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1071INCLUDE ../ext/misc/dbdata.c
1072#endif
1073
1074#if defined(SQLITE_ENABLE_SESSION)
1075/*
1076** State information for a single open session
1077*/
1078typedef struct OpenSession OpenSession;
1079struct OpenSession {
1080  char *zName;             /* Symbolic name for this session */
1081  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1082  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1083  sqlite3_session *p;      /* The open session */
1084};
1085#endif
1086
1087typedef struct ExpertInfo ExpertInfo;
1088struct ExpertInfo {
1089  sqlite3expert *pExpert;
1090  int bVerbose;
1091};
1092
1093/* A single line in the EQP output */
1094typedef struct EQPGraphRow EQPGraphRow;
1095struct EQPGraphRow {
1096  int iEqpId;           /* ID for this row */
1097  int iParentId;        /* ID of the parent row */
1098  EQPGraphRow *pNext;   /* Next row in sequence */
1099  char zText[1];        /* Text to display for this row */
1100};
1101
1102/* All EQP output is collected into an instance of the following */
1103typedef struct EQPGraph EQPGraph;
1104struct EQPGraph {
1105  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1106  EQPGraphRow *pLast;   /* Last element of the pRow list */
1107  char zPrefix[100];    /* Graph prefix */
1108};
1109
1110/*
1111** State information about the database connection is contained in an
1112** instance of the following structure.
1113*/
1114typedef struct ShellState ShellState;
1115struct ShellState {
1116  sqlite3 *db;           /* The database */
1117  u8 autoExplain;        /* Automatically turn on .explain mode */
1118  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1119  u8 autoEQPtest;        /* autoEQP is in test mode */
1120  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1121  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1122  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1123  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1124  u8 nEqpLevel;          /* Depth of the EQP output graph */
1125  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1126  u8 bSafeMode;          /* True to prohibit unsafe operations */
1127  u8 bSafeModePersist;   /* The long-term value of bSafeMode */
1128  unsigned statsOn;      /* True to display memory stats before each finalize */
1129  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1130  int outCount;          /* Revert to stdout when reaching zero */
1131  int cnt;               /* Number of records displayed so far */
1132  int lineno;            /* Line number of last line read from in */
1133  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1134  FILE *in;              /* Read commands from this stream */
1135  FILE *out;             /* Write results here */
1136  FILE *traceOut;        /* Output for sqlite3_trace() */
1137  int nErr;              /* Number of errors seen */
1138  int mode;              /* An output mode setting */
1139  int modePrior;         /* Saved mode */
1140  int cMode;             /* temporary output mode for the current query */
1141  int normalMode;        /* Output mode before ".explain on" */
1142  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1143  int showHeader;        /* True to show column names in List or Column mode */
1144  int nCheck;            /* Number of ".check" commands run */
1145  unsigned nProgress;    /* Number of progress callbacks encountered */
1146  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1147  unsigned flgProgress;  /* Flags for the progress callback */
1148  unsigned shellFlgs;    /* Various flags */
1149  unsigned priorShFlgs;  /* Saved copy of flags */
1150  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1151  char *zDestTable;      /* Name of destination table when MODE_Insert */
1152  char *zTempFile;       /* Temporary file that might need deleting */
1153  char zTestcase[30];    /* Name of current test case */
1154  char colSeparator[20]; /* Column separator character for several modes */
1155  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1156  char colSepPrior[20];  /* Saved column separator */
1157  char rowSepPrior[20];  /* Saved row separator */
1158  int *colWidth;         /* Requested width of each column in columnar modes */
1159  int *actualWidth;      /* Actual width of each column */
1160  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1161  char nullValue[20];    /* The text to print when a NULL comes back from
1162                         ** the database */
1163  char outfile[FILENAME_MAX]; /* Filename for *out */
1164  sqlite3_stmt *pStmt;   /* Current statement if any. */
1165  FILE *pLog;            /* Write log output here */
1166  struct AuxDb {         /* Storage space for auxiliary database connections */
1167    sqlite3 *db;               /* Connection pointer */
1168    const char *zDbFilename;   /* Filename used to open the connection */
1169    char *zFreeOnClose;        /* Free this memory allocation on close */
1170#if defined(SQLITE_ENABLE_SESSION)
1171    int nSession;              /* Number of active sessions */
1172    OpenSession aSession[4];   /* Array of sessions.  [0] is in focus. */
1173#endif
1174  } aAuxDb[5],           /* Array of all database connections */
1175    *pAuxDb;             /* Currently active database connection */
1176  int *aiIndent;         /* Array of indents used in MODE_Explain */
1177  int nIndent;           /* Size of array aiIndent[] */
1178  int iIndent;           /* Index of current op in aiIndent[] */
1179  char *zNonce;          /* Nonce for temporary safe-mode excapes */
1180  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1181  ExpertInfo expert;     /* Valid if previous command was ".expert OPT..." */
1182};
1183
1184
1185/* Allowed values for ShellState.autoEQP
1186*/
1187#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1188#define AUTOEQP_on       1           /* Automatic EQP is on */
1189#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1190#define AUTOEQP_full     3           /* Show full EXPLAIN */
1191
1192/* Allowed values for ShellState.openMode
1193*/
1194#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1195#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1196#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1197#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1198#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1199#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1200#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1201
1202/* Allowed values for ShellState.eTraceType
1203*/
1204#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1205#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1206#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1207
1208/* Bits in the ShellState.flgProgress variable */
1209#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1210#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1211                                   ** callback limit is reached, and for each
1212                                   ** top-level SQL statement */
1213#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1214
1215/*
1216** These are the allowed shellFlgs values
1217*/
1218#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1219#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1220#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1221#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1222#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1223#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1224#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1225#define SHFLG_HeaderSet      0x00000080 /* showHeader has been specified */
1226#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1227#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1228
1229/*
1230** Macros for testing and setting shellFlgs
1231*/
1232#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1233#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1234#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1235
1236/*
1237** These are the allowed modes.
1238*/
1239#define MODE_Line     0  /* One column per line.  Blank line between records */
1240#define MODE_Column   1  /* One record per line in neat columns */
1241#define MODE_List     2  /* One record per line with a separator */
1242#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1243#define MODE_Html     4  /* Generate an XHTML table */
1244#define MODE_Insert   5  /* Generate SQL "insert" statements */
1245#define MODE_Quote    6  /* Quote values as for SQL */
1246#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1247#define MODE_Csv      8  /* Quote strings, numbers are plain */
1248#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1249#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1250#define MODE_Pretty  11  /* Pretty-print schemas */
1251#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1252#define MODE_Json    13  /* Output JSON */
1253#define MODE_Markdown 14 /* Markdown formatting */
1254#define MODE_Table   15  /* MySQL-style table formatting */
1255#define MODE_Box     16  /* Unicode box-drawing characters */
1256
1257static const char *modeDescr[] = {
1258  "line",
1259  "column",
1260  "list",
1261  "semi",
1262  "html",
1263  "insert",
1264  "quote",
1265  "tcl",
1266  "csv",
1267  "explain",
1268  "ascii",
1269  "prettyprint",
1270  "eqp",
1271  "json",
1272  "markdown",
1273  "table",
1274  "box"
1275};
1276
1277/*
1278** These are the column/row/line separators used by the various
1279** import/export modes.
1280*/
1281#define SEP_Column    "|"
1282#define SEP_Row       "\n"
1283#define SEP_Tab       "\t"
1284#define SEP_Space     " "
1285#define SEP_Comma     ","
1286#define SEP_CrLf      "\r\n"
1287#define SEP_Unit      "\x1F"
1288#define SEP_Record    "\x1E"
1289
1290/*
1291** A callback for the sqlite3_log() interface.
1292*/
1293static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1294  ShellState *p = (ShellState*)pArg;
1295  if( p->pLog==0 ) return;
1296  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1297  fflush(p->pLog);
1298}
1299
1300/*
1301** SQL function:  shell_putsnl(X)
1302**
1303** Write the text X to the screen (or whatever output is being directed)
1304** adding a newline at the end, and then return X.
1305*/
1306static void shellPutsFunc(
1307  sqlite3_context *pCtx,
1308  int nVal,
1309  sqlite3_value **apVal
1310){
1311  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1312  (void)nVal;
1313  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1314  sqlite3_result_value(pCtx, apVal[0]);
1315}
1316
1317/*
1318** If in safe mode, print an error message described by the arguments
1319** and exit immediately.
1320*/
1321static void failIfSafeMode(
1322  ShellState *p,
1323  const char *zErrMsg,
1324  ...
1325){
1326  if( p->bSafeMode ){
1327    va_list ap;
1328    char *zMsg;
1329    va_start(ap, zErrMsg);
1330    zMsg = sqlite3_vmprintf(zErrMsg, ap);
1331    va_end(ap);
1332    raw_printf(stderr, "line %d: ", p->lineno);
1333    utf8_printf(stderr, "%s\n", zMsg);
1334    exit(1);
1335  }
1336}
1337
1338/*
1339** SQL function:   edit(VALUE)
1340**                 edit(VALUE,EDITOR)
1341**
1342** These steps:
1343**
1344**     (1) Write VALUE into a temporary file.
1345**     (2) Run program EDITOR on that temporary file.
1346**     (3) Read the temporary file back and return its content as the result.
1347**     (4) Delete the temporary file
1348**
1349** If the EDITOR argument is omitted, use the value in the VISUAL
1350** environment variable.  If still there is no EDITOR, through an error.
1351**
1352** Also throw an error if the EDITOR program returns a non-zero exit code.
1353*/
1354#ifndef SQLITE_NOHAVE_SYSTEM
1355static void editFunc(
1356  sqlite3_context *context,
1357  int argc,
1358  sqlite3_value **argv
1359){
1360  const char *zEditor;
1361  char *zTempFile = 0;
1362  sqlite3 *db;
1363  char *zCmd = 0;
1364  int bBin;
1365  int rc;
1366  int hasCRNL = 0;
1367  FILE *f = 0;
1368  sqlite3_int64 sz;
1369  sqlite3_int64 x;
1370  unsigned char *p = 0;
1371
1372  if( argc==2 ){
1373    zEditor = (const char*)sqlite3_value_text(argv[1]);
1374  }else{
1375    zEditor = getenv("VISUAL");
1376  }
1377  if( zEditor==0 ){
1378    sqlite3_result_error(context, "no editor for edit()", -1);
1379    return;
1380  }
1381  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1382    sqlite3_result_error(context, "NULL input to edit()", -1);
1383    return;
1384  }
1385  db = sqlite3_context_db_handle(context);
1386  zTempFile = 0;
1387  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1388  if( zTempFile==0 ){
1389    sqlite3_uint64 r = 0;
1390    sqlite3_randomness(sizeof(r), &r);
1391    zTempFile = sqlite3_mprintf("temp%llx", r);
1392    if( zTempFile==0 ){
1393      sqlite3_result_error_nomem(context);
1394      return;
1395    }
1396  }
1397  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1398  /* When writing the file to be edited, do \n to \r\n conversions on systems
1399  ** that want \r\n line endings */
1400  f = fopen(zTempFile, bBin ? "wb" : "w");
1401  if( f==0 ){
1402    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1403    goto edit_func_end;
1404  }
1405  sz = sqlite3_value_bytes(argv[0]);
1406  if( bBin ){
1407    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1408  }else{
1409    const char *z = (const char*)sqlite3_value_text(argv[0]);
1410    /* Remember whether or not the value originally contained \r\n */
1411    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1412    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1413  }
1414  fclose(f);
1415  f = 0;
1416  if( x!=sz ){
1417    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1418    goto edit_func_end;
1419  }
1420  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1421  if( zCmd==0 ){
1422    sqlite3_result_error_nomem(context);
1423    goto edit_func_end;
1424  }
1425  rc = system(zCmd);
1426  sqlite3_free(zCmd);
1427  if( rc ){
1428    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1429    goto edit_func_end;
1430  }
1431  f = fopen(zTempFile, "rb");
1432  if( f==0 ){
1433    sqlite3_result_error(context,
1434      "edit() cannot reopen temp file after edit", -1);
1435    goto edit_func_end;
1436  }
1437  fseek(f, 0, SEEK_END);
1438  sz = ftell(f);
1439  rewind(f);
1440  p = sqlite3_malloc64( sz+1 );
1441  if( p==0 ){
1442    sqlite3_result_error_nomem(context);
1443    goto edit_func_end;
1444  }
1445  x = fread(p, 1, (size_t)sz, f);
1446  fclose(f);
1447  f = 0;
1448  if( x!=sz ){
1449    sqlite3_result_error(context, "could not read back the whole file", -1);
1450    goto edit_func_end;
1451  }
1452  if( bBin ){
1453    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1454  }else{
1455    sqlite3_int64 i, j;
1456    if( hasCRNL ){
1457      /* If the original contains \r\n then do no conversions back to \n */
1458    }else{
1459      /* If the file did not originally contain \r\n then convert any new
1460      ** \r\n back into \n */
1461      for(i=j=0; i<sz; i++){
1462        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1463        p[j++] = p[i];
1464      }
1465      sz = j;
1466      p[sz] = 0;
1467    }
1468    sqlite3_result_text64(context, (const char*)p, sz,
1469                          sqlite3_free, SQLITE_UTF8);
1470  }
1471  p = 0;
1472
1473edit_func_end:
1474  if( f ) fclose(f);
1475  unlink(zTempFile);
1476  sqlite3_free(zTempFile);
1477  sqlite3_free(p);
1478}
1479#endif /* SQLITE_NOHAVE_SYSTEM */
1480
1481/*
1482** Save or restore the current output mode
1483*/
1484static void outputModePush(ShellState *p){
1485  p->modePrior = p->mode;
1486  p->priorShFlgs = p->shellFlgs;
1487  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1488  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1489}
1490static void outputModePop(ShellState *p){
1491  p->mode = p->modePrior;
1492  p->shellFlgs = p->priorShFlgs;
1493  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1494  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1495}
1496
1497/*
1498** Output the given string as a hex-encoded blob (eg. X'1234' )
1499*/
1500static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1501  int i;
1502  char *zBlob = (char *)pBlob;
1503  raw_printf(out,"X'");
1504  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1505  raw_printf(out,"'");
1506}
1507
1508/*
1509** Find a string that is not found anywhere in z[].  Return a pointer
1510** to that string.
1511**
1512** Try to use zA and zB first.  If both of those are already found in z[]
1513** then make up some string and store it in the buffer zBuf.
1514*/
1515static const char *unused_string(
1516  const char *z,                    /* Result must not appear anywhere in z */
1517  const char *zA, const char *zB,   /* Try these first */
1518  char *zBuf                        /* Space to store a generated string */
1519){
1520  unsigned i = 0;
1521  if( strstr(z, zA)==0 ) return zA;
1522  if( strstr(z, zB)==0 ) return zB;
1523  do{
1524    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1525  }while( strstr(z,zBuf)!=0 );
1526  return zBuf;
1527}
1528
1529/*
1530** Output the given string as a quoted string using SQL quoting conventions.
1531**
1532** See also: output_quoted_escaped_string()
1533*/
1534static void output_quoted_string(FILE *out, const char *z){
1535  int i;
1536  char c;
1537  setBinaryMode(out, 1);
1538  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1539  if( c==0 ){
1540    utf8_printf(out,"'%s'",z);
1541  }else{
1542    raw_printf(out, "'");
1543    while( *z ){
1544      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1545      if( c=='\'' ) i++;
1546      if( i ){
1547        utf8_printf(out, "%.*s", i, z);
1548        z += i;
1549      }
1550      if( c=='\'' ){
1551        raw_printf(out, "'");
1552        continue;
1553      }
1554      if( c==0 ){
1555        break;
1556      }
1557      z++;
1558    }
1559    raw_printf(out, "'");
1560  }
1561  setTextMode(out, 1);
1562}
1563
1564/*
1565** Output the given string as a quoted string using SQL quoting conventions.
1566** Additionallly , escape the "\n" and "\r" characters so that they do not
1567** get corrupted by end-of-line translation facilities in some operating
1568** systems.
1569**
1570** This is like output_quoted_string() but with the addition of the \r\n
1571** escape mechanism.
1572*/
1573static void output_quoted_escaped_string(FILE *out, const char *z){
1574  int i;
1575  char c;
1576  setBinaryMode(out, 1);
1577  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1578  if( c==0 ){
1579    utf8_printf(out,"'%s'",z);
1580  }else{
1581    const char *zNL = 0;
1582    const char *zCR = 0;
1583    int nNL = 0;
1584    int nCR = 0;
1585    char zBuf1[20], zBuf2[20];
1586    for(i=0; z[i]; i++){
1587      if( z[i]=='\n' ) nNL++;
1588      if( z[i]=='\r' ) nCR++;
1589    }
1590    if( nNL ){
1591      raw_printf(out, "replace(");
1592      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1593    }
1594    if( nCR ){
1595      raw_printf(out, "replace(");
1596      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1597    }
1598    raw_printf(out, "'");
1599    while( *z ){
1600      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1601      if( c=='\'' ) i++;
1602      if( i ){
1603        utf8_printf(out, "%.*s", i, z);
1604        z += i;
1605      }
1606      if( c=='\'' ){
1607        raw_printf(out, "'");
1608        continue;
1609      }
1610      if( c==0 ){
1611        break;
1612      }
1613      z++;
1614      if( c=='\n' ){
1615        raw_printf(out, "%s", zNL);
1616        continue;
1617      }
1618      raw_printf(out, "%s", zCR);
1619    }
1620    raw_printf(out, "'");
1621    if( nCR ){
1622      raw_printf(out, ",'%s',char(13))", zCR);
1623    }
1624    if( nNL ){
1625      raw_printf(out, ",'%s',char(10))", zNL);
1626    }
1627  }
1628  setTextMode(out, 1);
1629}
1630
1631/*
1632** Output the given string as a quoted according to C or TCL quoting rules.
1633*/
1634static void output_c_string(FILE *out, const char *z){
1635  unsigned int c;
1636  fputc('"', out);
1637  while( (c = *(z++))!=0 ){
1638    if( c=='\\' ){
1639      fputc(c, out);
1640      fputc(c, out);
1641    }else if( c=='"' ){
1642      fputc('\\', out);
1643      fputc('"', out);
1644    }else if( c=='\t' ){
1645      fputc('\\', out);
1646      fputc('t', out);
1647    }else if( c=='\n' ){
1648      fputc('\\', out);
1649      fputc('n', out);
1650    }else if( c=='\r' ){
1651      fputc('\\', out);
1652      fputc('r', out);
1653    }else if( !isprint(c&0xff) ){
1654      raw_printf(out, "\\%03o", c&0xff);
1655    }else{
1656      fputc(c, out);
1657    }
1658  }
1659  fputc('"', out);
1660}
1661
1662/*
1663** Output the given string as a quoted according to JSON quoting rules.
1664*/
1665static void output_json_string(FILE *out, const char *z, int n){
1666  unsigned int c;
1667  if( n<0 ) n = (int)strlen(z);
1668  fputc('"', out);
1669  while( n-- ){
1670    c = *(z++);
1671    if( c=='\\' || c=='"' ){
1672      fputc('\\', out);
1673      fputc(c, out);
1674    }else if( c<=0x1f ){
1675      fputc('\\', out);
1676      if( c=='\b' ){
1677        fputc('b', out);
1678      }else if( c=='\f' ){
1679        fputc('f', out);
1680      }else if( c=='\n' ){
1681        fputc('n', out);
1682      }else if( c=='\r' ){
1683        fputc('r', out);
1684      }else if( c=='\t' ){
1685        fputc('t', out);
1686      }else{
1687         raw_printf(out, "u%04x",c);
1688      }
1689    }else{
1690      fputc(c, out);
1691    }
1692  }
1693  fputc('"', out);
1694}
1695
1696/*
1697** Output the given string with characters that are special to
1698** HTML escaped.
1699*/
1700static void output_html_string(FILE *out, const char *z){
1701  int i;
1702  if( z==0 ) z = "";
1703  while( *z ){
1704    for(i=0;   z[i]
1705            && z[i]!='<'
1706            && z[i]!='&'
1707            && z[i]!='>'
1708            && z[i]!='\"'
1709            && z[i]!='\'';
1710        i++){}
1711    if( i>0 ){
1712      utf8_printf(out,"%.*s",i,z);
1713    }
1714    if( z[i]=='<' ){
1715      raw_printf(out,"&lt;");
1716    }else if( z[i]=='&' ){
1717      raw_printf(out,"&amp;");
1718    }else if( z[i]=='>' ){
1719      raw_printf(out,"&gt;");
1720    }else if( z[i]=='\"' ){
1721      raw_printf(out,"&quot;");
1722    }else if( z[i]=='\'' ){
1723      raw_printf(out,"&#39;");
1724    }else{
1725      break;
1726    }
1727    z += i + 1;
1728  }
1729}
1730
1731/*
1732** If a field contains any character identified by a 1 in the following
1733** array, then the string must be quoted for CSV.
1734*/
1735static const char needCsvQuote[] = {
1736  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1737  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1738  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1739  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1740  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1741  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1742  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1743  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1744  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1745  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1746  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1747  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1748  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1749  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1750  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1751  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1752};
1753
1754/*
1755** Output a single term of CSV.  Actually, p->colSeparator is used for
1756** the separator, which may or may not be a comma.  p->nullValue is
1757** the null value.  Strings are quoted if necessary.  The separator
1758** is only issued if bSep is true.
1759*/
1760static void output_csv(ShellState *p, const char *z, int bSep){
1761  FILE *out = p->out;
1762  if( z==0 ){
1763    utf8_printf(out,"%s",p->nullValue);
1764  }else{
1765    unsigned i;
1766    for(i=0; z[i]; i++){
1767      if( needCsvQuote[((unsigned char*)z)[i]] ){
1768        i = 0;
1769        break;
1770      }
1771    }
1772    if( i==0 || strstr(z, p->colSeparator)!=0 ){
1773      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1774      utf8_printf(out, "%s", zQuoted);
1775      sqlite3_free(zQuoted);
1776    }else{
1777      utf8_printf(out, "%s", z);
1778    }
1779  }
1780  if( bSep ){
1781    utf8_printf(p->out, "%s", p->colSeparator);
1782  }
1783}
1784
1785/*
1786** This routine runs when the user presses Ctrl-C
1787*/
1788static void interrupt_handler(int NotUsed){
1789  UNUSED_PARAMETER(NotUsed);
1790  seenInterrupt++;
1791  if( seenInterrupt>2 ) exit(1);
1792  if( globalDb ) sqlite3_interrupt(globalDb);
1793}
1794
1795#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1796/*
1797** This routine runs for console events (e.g. Ctrl-C) on Win32
1798*/
1799static BOOL WINAPI ConsoleCtrlHandler(
1800  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1801){
1802  if( dwCtrlType==CTRL_C_EVENT ){
1803    interrupt_handler(0);
1804    return TRUE;
1805  }
1806  return FALSE;
1807}
1808#endif
1809
1810#ifndef SQLITE_OMIT_AUTHORIZATION
1811/*
1812** This authorizer runs in safe mode.
1813*/
1814static int safeModeAuth(
1815  void *pClientData,
1816  int op,
1817  const char *zA1,
1818  const char *zA2,
1819  const char *zA3,
1820  const char *zA4
1821){
1822  ShellState *p = (ShellState*)pClientData;
1823  static const char *azProhibitedFunctions[] = {
1824    "edit",
1825    "fts3_tokenizer",
1826    "load_extension",
1827    "readfile",
1828    "writefile",
1829    "zipfile",
1830    "zipfile_cds",
1831  };
1832  UNUSED_PARAMETER(zA2);
1833  UNUSED_PARAMETER(zA3);
1834  UNUSED_PARAMETER(zA4);
1835  switch( op ){
1836    case SQLITE_ATTACH: {
1837      failIfSafeMode(p, "cannot run ATTACH in safe mode");
1838      break;
1839    }
1840    case SQLITE_FUNCTION: {
1841      int i;
1842      for(i=0; i<ArraySize(azProhibitedFunctions); i++){
1843        if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
1844          failIfSafeMode(p, "cannot use the %s() function in safe mode",
1845                         azProhibitedFunctions[i]);
1846        }
1847      }
1848      break;
1849    }
1850  }
1851  return SQLITE_OK;
1852}
1853
1854/*
1855** When the ".auth ON" is set, the following authorizer callback is
1856** invoked.  It always returns SQLITE_OK.
1857*/
1858static int shellAuth(
1859  void *pClientData,
1860  int op,
1861  const char *zA1,
1862  const char *zA2,
1863  const char *zA3,
1864  const char *zA4
1865){
1866  ShellState *p = (ShellState*)pClientData;
1867  static const char *azAction[] = { 0,
1868     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1869     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1870     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1871     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1872     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1873     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1874     "PRAGMA",               "READ",                 "SELECT",
1875     "TRANSACTION",          "UPDATE",               "ATTACH",
1876     "DETACH",               "ALTER_TABLE",          "REINDEX",
1877     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1878     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1879  };
1880  int i;
1881  const char *az[4];
1882  az[0] = zA1;
1883  az[1] = zA2;
1884  az[2] = zA3;
1885  az[3] = zA4;
1886  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1887  for(i=0; i<4; i++){
1888    raw_printf(p->out, " ");
1889    if( az[i] ){
1890      output_c_string(p->out, az[i]);
1891    }else{
1892      raw_printf(p->out, "NULL");
1893    }
1894  }
1895  raw_printf(p->out, "\n");
1896  if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
1897  return SQLITE_OK;
1898}
1899#endif
1900
1901/*
1902** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1903**
1904** This routine converts some CREATE TABLE statements for shadow tables
1905** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1906*/
1907static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1908  if( z==0 ) return;
1909  if( zTail==0 ) return;
1910  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1911    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1912  }else{
1913    utf8_printf(out, "%s%s", z, zTail);
1914  }
1915}
1916static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1917  char c = z[n];
1918  z[n] = 0;
1919  printSchemaLine(out, z, zTail);
1920  z[n] = c;
1921}
1922
1923/*
1924** Return true if string z[] has nothing but whitespace and comments to the
1925** end of the first line.
1926*/
1927static int wsToEol(const char *z){
1928  int i;
1929  for(i=0; z[i]; i++){
1930    if( z[i]=='\n' ) return 1;
1931    if( IsSpace(z[i]) ) continue;
1932    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1933    return 0;
1934  }
1935  return 1;
1936}
1937
1938/*
1939** Add a new entry to the EXPLAIN QUERY PLAN data
1940*/
1941static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1942  EQPGraphRow *pNew;
1943  int nText = strlen30(zText);
1944  if( p->autoEQPtest ){
1945    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1946  }
1947  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1948  if( pNew==0 ) shell_out_of_memory();
1949  pNew->iEqpId = iEqpId;
1950  pNew->iParentId = p2;
1951  memcpy(pNew->zText, zText, nText+1);
1952  pNew->pNext = 0;
1953  if( p->sGraph.pLast ){
1954    p->sGraph.pLast->pNext = pNew;
1955  }else{
1956    p->sGraph.pRow = pNew;
1957  }
1958  p->sGraph.pLast = pNew;
1959}
1960
1961/*
1962** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1963** in p->sGraph.
1964*/
1965static void eqp_reset(ShellState *p){
1966  EQPGraphRow *pRow, *pNext;
1967  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1968    pNext = pRow->pNext;
1969    sqlite3_free(pRow);
1970  }
1971  memset(&p->sGraph, 0, sizeof(p->sGraph));
1972}
1973
1974/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1975** pOld, or return the first such line if pOld is NULL
1976*/
1977static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1978  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1979  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1980  return pRow;
1981}
1982
1983/* Render a single level of the graph that has iEqpId as its parent.  Called
1984** recursively to render sublevels.
1985*/
1986static void eqp_render_level(ShellState *p, int iEqpId){
1987  EQPGraphRow *pRow, *pNext;
1988  int n = strlen30(p->sGraph.zPrefix);
1989  char *z;
1990  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1991    pNext = eqp_next_row(p, iEqpId, pRow);
1992    z = pRow->zText;
1993    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1994                pNext ? "|--" : "`--", z);
1995    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1996      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1997      eqp_render_level(p, pRow->iEqpId);
1998      p->sGraph.zPrefix[n] = 0;
1999    }
2000  }
2001}
2002
2003/*
2004** Display and reset the EXPLAIN QUERY PLAN data
2005*/
2006static void eqp_render(ShellState *p){
2007  EQPGraphRow *pRow = p->sGraph.pRow;
2008  if( pRow ){
2009    if( pRow->zText[0]=='-' ){
2010      if( pRow->pNext==0 ){
2011        eqp_reset(p);
2012        return;
2013      }
2014      utf8_printf(p->out, "%s\n", pRow->zText+3);
2015      p->sGraph.pRow = pRow->pNext;
2016      sqlite3_free(pRow);
2017    }else{
2018      utf8_printf(p->out, "QUERY PLAN\n");
2019    }
2020    p->sGraph.zPrefix[0] = 0;
2021    eqp_render_level(p, 0);
2022    eqp_reset(p);
2023  }
2024}
2025
2026#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2027/*
2028** Progress handler callback.
2029*/
2030static int progress_handler(void *pClientData) {
2031  ShellState *p = (ShellState*)pClientData;
2032  p->nProgress++;
2033  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2034    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2035    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2036    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2037    return 1;
2038  }
2039  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2040    raw_printf(p->out, "Progress %u\n", p->nProgress);
2041  }
2042  return 0;
2043}
2044#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2045
2046/*
2047** Print N dashes
2048*/
2049static void print_dashes(FILE *out, int N){
2050  const char zDash[] = "--------------------------------------------------";
2051  const int nDash = sizeof(zDash) - 1;
2052  while( N>nDash ){
2053    fputs(zDash, out);
2054    N -= nDash;
2055  }
2056  raw_printf(out, "%.*s", N, zDash);
2057}
2058
2059/*
2060** Print a markdown or table-style row separator using ascii-art
2061*/
2062static void print_row_separator(
2063  ShellState *p,
2064  int nArg,
2065  const char *zSep
2066){
2067  int i;
2068  if( nArg>0 ){
2069    fputs(zSep, p->out);
2070    print_dashes(p->out, p->actualWidth[0]+2);
2071    for(i=1; i<nArg; i++){
2072      fputs(zSep, p->out);
2073      print_dashes(p->out, p->actualWidth[i]+2);
2074    }
2075    fputs(zSep, p->out);
2076  }
2077  fputs("\n", p->out);
2078}
2079
2080/*
2081** This is the callback routine that the shell
2082** invokes for each row of a query result.
2083*/
2084static int shell_callback(
2085  void *pArg,
2086  int nArg,        /* Number of result columns */
2087  char **azArg,    /* Text of each result column */
2088  char **azCol,    /* Column names */
2089  int *aiType      /* Column types.  Might be NULL */
2090){
2091  int i;
2092  ShellState *p = (ShellState*)pArg;
2093
2094  if( azArg==0 ) return 0;
2095  switch( p->cMode ){
2096    case MODE_Line: {
2097      int w = 5;
2098      if( azArg==0 ) break;
2099      for(i=0; i<nArg; i++){
2100        int len = strlen30(azCol[i] ? azCol[i] : "");
2101        if( len>w ) w = len;
2102      }
2103      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2104      for(i=0; i<nArg; i++){
2105        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2106                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2107      }
2108      break;
2109    }
2110    case MODE_Explain: {
2111      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2112      if( nArg>ArraySize(aExplainWidth) ){
2113        nArg = ArraySize(aExplainWidth);
2114      }
2115      if( p->cnt++==0 ){
2116        for(i=0; i<nArg; i++){
2117          int w = aExplainWidth[i];
2118          utf8_width_print(p->out, w, azCol[i]);
2119          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2120        }
2121        for(i=0; i<nArg; i++){
2122          int w = aExplainWidth[i];
2123          print_dashes(p->out, w);
2124          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2125        }
2126      }
2127      if( azArg==0 ) break;
2128      for(i=0; i<nArg; i++){
2129        int w = aExplainWidth[i];
2130        if( i==nArg-1 ) w = 0;
2131        if( azArg[i] && strlenChar(azArg[i])>w ){
2132          w = strlenChar(azArg[i]);
2133        }
2134        if( i==1 && p->aiIndent && p->pStmt ){
2135          if( p->iIndent<p->nIndent ){
2136            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2137          }
2138          p->iIndent++;
2139        }
2140        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2141        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2142      }
2143      break;
2144    }
2145    case MODE_Semi: {   /* .schema and .fullschema output */
2146      printSchemaLine(p->out, azArg[0], ";\n");
2147      break;
2148    }
2149    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2150      char *z;
2151      int j;
2152      int nParen = 0;
2153      char cEnd = 0;
2154      char c;
2155      int nLine = 0;
2156      assert( nArg==1 );
2157      if( azArg[0]==0 ) break;
2158      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2159       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2160      ){
2161        utf8_printf(p->out, "%s;\n", azArg[0]);
2162        break;
2163      }
2164      z = sqlite3_mprintf("%s", azArg[0]);
2165      j = 0;
2166      for(i=0; IsSpace(z[i]); i++){}
2167      for(; (c = z[i])!=0; i++){
2168        if( IsSpace(c) ){
2169          if( z[j-1]=='\r' ) z[j-1] = '\n';
2170          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2171        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2172          j--;
2173        }
2174        z[j++] = c;
2175      }
2176      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2177      z[j] = 0;
2178      if( strlen30(z)>=79 ){
2179        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2180          if( c==cEnd ){
2181            cEnd = 0;
2182          }else if( c=='"' || c=='\'' || c=='`' ){
2183            cEnd = c;
2184          }else if( c=='[' ){
2185            cEnd = ']';
2186          }else if( c=='-' && z[i+1]=='-' ){
2187            cEnd = '\n';
2188          }else if( c=='(' ){
2189            nParen++;
2190          }else if( c==')' ){
2191            nParen--;
2192            if( nLine>0 && nParen==0 && j>0 ){
2193              printSchemaLineN(p->out, z, j, "\n");
2194              j = 0;
2195            }
2196          }
2197          z[j++] = c;
2198          if( nParen==1 && cEnd==0
2199           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2200          ){
2201            if( c=='\n' ) j--;
2202            printSchemaLineN(p->out, z, j, "\n  ");
2203            j = 0;
2204            nLine++;
2205            while( IsSpace(z[i+1]) ){ i++; }
2206          }
2207        }
2208        z[j] = 0;
2209      }
2210      printSchemaLine(p->out, z, ";\n");
2211      sqlite3_free(z);
2212      break;
2213    }
2214    case MODE_List: {
2215      if( p->cnt++==0 && p->showHeader ){
2216        for(i=0; i<nArg; i++){
2217          utf8_printf(p->out,"%s%s",azCol[i],
2218                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2219        }
2220      }
2221      if( azArg==0 ) break;
2222      for(i=0; i<nArg; i++){
2223        char *z = azArg[i];
2224        if( z==0 ) z = p->nullValue;
2225        utf8_printf(p->out, "%s", z);
2226        if( i<nArg-1 ){
2227          utf8_printf(p->out, "%s", p->colSeparator);
2228        }else{
2229          utf8_printf(p->out, "%s", p->rowSeparator);
2230        }
2231      }
2232      break;
2233    }
2234    case MODE_Html: {
2235      if( p->cnt++==0 && p->showHeader ){
2236        raw_printf(p->out,"<TR>");
2237        for(i=0; i<nArg; i++){
2238          raw_printf(p->out,"<TH>");
2239          output_html_string(p->out, azCol[i]);
2240          raw_printf(p->out,"</TH>\n");
2241        }
2242        raw_printf(p->out,"</TR>\n");
2243      }
2244      if( azArg==0 ) break;
2245      raw_printf(p->out,"<TR>");
2246      for(i=0; i<nArg; i++){
2247        raw_printf(p->out,"<TD>");
2248        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2249        raw_printf(p->out,"</TD>\n");
2250      }
2251      raw_printf(p->out,"</TR>\n");
2252      break;
2253    }
2254    case MODE_Tcl: {
2255      if( p->cnt++==0 && p->showHeader ){
2256        for(i=0; i<nArg; i++){
2257          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2258          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2259        }
2260        utf8_printf(p->out, "%s", p->rowSeparator);
2261      }
2262      if( azArg==0 ) break;
2263      for(i=0; i<nArg; i++){
2264        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2265        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2266      }
2267      utf8_printf(p->out, "%s", p->rowSeparator);
2268      break;
2269    }
2270    case MODE_Csv: {
2271      setBinaryMode(p->out, 1);
2272      if( p->cnt++==0 && p->showHeader ){
2273        for(i=0; i<nArg; i++){
2274          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2275        }
2276        utf8_printf(p->out, "%s", p->rowSeparator);
2277      }
2278      if( nArg>0 ){
2279        for(i=0; i<nArg; i++){
2280          output_csv(p, azArg[i], i<nArg-1);
2281        }
2282        utf8_printf(p->out, "%s", p->rowSeparator);
2283      }
2284      setTextMode(p->out, 1);
2285      break;
2286    }
2287    case MODE_Insert: {
2288      if( azArg==0 ) break;
2289      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2290      if( p->showHeader ){
2291        raw_printf(p->out,"(");
2292        for(i=0; i<nArg; i++){
2293          if( i>0 ) raw_printf(p->out, ",");
2294          if( quoteChar(azCol[i]) ){
2295            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2296            utf8_printf(p->out, "%s", z);
2297            sqlite3_free(z);
2298          }else{
2299            raw_printf(p->out, "%s", azCol[i]);
2300          }
2301        }
2302        raw_printf(p->out,")");
2303      }
2304      p->cnt++;
2305      for(i=0; i<nArg; i++){
2306        raw_printf(p->out, i>0 ? "," : " VALUES(");
2307        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2308          utf8_printf(p->out,"NULL");
2309        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2310          if( ShellHasFlag(p, SHFLG_Newlines) ){
2311            output_quoted_string(p->out, azArg[i]);
2312          }else{
2313            output_quoted_escaped_string(p->out, azArg[i]);
2314          }
2315        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2316          utf8_printf(p->out,"%s", azArg[i]);
2317        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2318          char z[50];
2319          double r = sqlite3_column_double(p->pStmt, i);
2320          sqlite3_uint64 ur;
2321          memcpy(&ur,&r,sizeof(r));
2322          if( ur==0x7ff0000000000000LL ){
2323            raw_printf(p->out, "1e999");
2324          }else if( ur==0xfff0000000000000LL ){
2325            raw_printf(p->out, "-1e999");
2326          }else{
2327            sqlite3_snprintf(50,z,"%!.20g", r);
2328            raw_printf(p->out, "%s", z);
2329          }
2330        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2331          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2332          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2333          output_hex_blob(p->out, pBlob, nBlob);
2334        }else if( isNumber(azArg[i], 0) ){
2335          utf8_printf(p->out,"%s", azArg[i]);
2336        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2337          output_quoted_string(p->out, azArg[i]);
2338        }else{
2339          output_quoted_escaped_string(p->out, azArg[i]);
2340        }
2341      }
2342      raw_printf(p->out,");\n");
2343      break;
2344    }
2345    case MODE_Json: {
2346      if( azArg==0 ) break;
2347      if( p->cnt==0 ){
2348        fputs("[{", p->out);
2349      }else{
2350        fputs(",\n{", p->out);
2351      }
2352      p->cnt++;
2353      for(i=0; i<nArg; i++){
2354        output_json_string(p->out, azCol[i], -1);
2355        putc(':', p->out);
2356        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2357          fputs("null",p->out);
2358        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2359          char z[50];
2360          double r = sqlite3_column_double(p->pStmt, i);
2361          sqlite3_uint64 ur;
2362          memcpy(&ur,&r,sizeof(r));
2363          if( ur==0x7ff0000000000000LL ){
2364            raw_printf(p->out, "1e999");
2365          }else if( ur==0xfff0000000000000LL ){
2366            raw_printf(p->out, "-1e999");
2367          }else{
2368            sqlite3_snprintf(50,z,"%!.20g", r);
2369            raw_printf(p->out, "%s", z);
2370          }
2371        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2372          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2373          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2374          output_json_string(p->out, pBlob, nBlob);
2375        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2376          output_json_string(p->out, azArg[i], -1);
2377        }else{
2378          utf8_printf(p->out,"%s", azArg[i]);
2379        }
2380        if( i<nArg-1 ){
2381          putc(',', p->out);
2382        }
2383      }
2384      putc('}', p->out);
2385      break;
2386    }
2387    case MODE_Quote: {
2388      if( azArg==0 ) break;
2389      if( p->cnt==0 && p->showHeader ){
2390        for(i=0; i<nArg; i++){
2391          if( i>0 ) fputs(p->colSeparator, p->out);
2392          output_quoted_string(p->out, azCol[i]);
2393        }
2394        fputs(p->rowSeparator, p->out);
2395      }
2396      p->cnt++;
2397      for(i=0; i<nArg; i++){
2398        if( i>0 ) fputs(p->colSeparator, p->out);
2399        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2400          utf8_printf(p->out,"NULL");
2401        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2402          output_quoted_string(p->out, azArg[i]);
2403        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2404          utf8_printf(p->out,"%s", azArg[i]);
2405        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2406          char z[50];
2407          double r = sqlite3_column_double(p->pStmt, i);
2408          sqlite3_snprintf(50,z,"%!.20g", r);
2409          raw_printf(p->out, "%s", z);
2410        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2411          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2412          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2413          output_hex_blob(p->out, pBlob, nBlob);
2414        }else if( isNumber(azArg[i], 0) ){
2415          utf8_printf(p->out,"%s", azArg[i]);
2416        }else{
2417          output_quoted_string(p->out, azArg[i]);
2418        }
2419      }
2420      fputs(p->rowSeparator, p->out);
2421      break;
2422    }
2423    case MODE_Ascii: {
2424      if( p->cnt++==0 && p->showHeader ){
2425        for(i=0; i<nArg; i++){
2426          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2427          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2428        }
2429        utf8_printf(p->out, "%s", p->rowSeparator);
2430      }
2431      if( azArg==0 ) break;
2432      for(i=0; i<nArg; i++){
2433        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2434        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2435      }
2436      utf8_printf(p->out, "%s", p->rowSeparator);
2437      break;
2438    }
2439    case MODE_EQP: {
2440      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2441      break;
2442    }
2443  }
2444  return 0;
2445}
2446
2447/*
2448** This is the callback routine that the SQLite library
2449** invokes for each row of a query result.
2450*/
2451static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2452  /* since we don't have type info, call the shell_callback with a NULL value */
2453  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2454}
2455
2456/*
2457** This is the callback routine from sqlite3_exec() that appends all
2458** output onto the end of a ShellText object.
2459*/
2460static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2461  ShellText *p = (ShellText*)pArg;
2462  int i;
2463  UNUSED_PARAMETER(az);
2464  if( azArg==0 ) return 0;
2465  if( p->n ) appendText(p, "|", 0);
2466  for(i=0; i<nArg; i++){
2467    if( i ) appendText(p, ",", 0);
2468    if( azArg[i] ) appendText(p, azArg[i], 0);
2469  }
2470  return 0;
2471}
2472
2473/*
2474** Generate an appropriate SELFTEST table in the main database.
2475*/
2476static void createSelftestTable(ShellState *p){
2477  char *zErrMsg = 0;
2478  sqlite3_exec(p->db,
2479    "SAVEPOINT selftest_init;\n"
2480    "CREATE TABLE IF NOT EXISTS selftest(\n"
2481    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2482    "  op TEXT,\n"                   /* Operator:  memo run */
2483    "  cmd TEXT,\n"                  /* Command text */
2484    "  ans TEXT\n"                   /* Desired answer */
2485    ");"
2486    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2487    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2488    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2489    "         'memo','Tests generated by --init');\n"
2490    "INSERT INTO [_shell$self]\n"
2491    "  SELECT 'run',\n"
2492    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2493                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2494    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2495                          "FROM sqlite_schema ORDER BY 2',224));\n"
2496    "INSERT INTO [_shell$self]\n"
2497    "  SELECT 'run',"
2498    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2499    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2500    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2501    "  FROM (\n"
2502    "    SELECT name FROM sqlite_schema\n"
2503    "     WHERE type='table'\n"
2504    "       AND name<>'selftest'\n"
2505    "       AND coalesce(rootpage,0)>0\n"
2506    "  )\n"
2507    " ORDER BY name;\n"
2508    "INSERT INTO [_shell$self]\n"
2509    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2510    "INSERT INTO selftest(tno,op,cmd,ans)"
2511    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2512    "DROP TABLE [_shell$self];"
2513    ,0,0,&zErrMsg);
2514  if( zErrMsg ){
2515    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2516    sqlite3_free(zErrMsg);
2517  }
2518  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2519}
2520
2521
2522/*
2523** Set the destination table field of the ShellState structure to
2524** the name of the table given.  Escape any quote characters in the
2525** table name.
2526*/
2527static void set_table_name(ShellState *p, const char *zName){
2528  int i, n;
2529  char cQuote;
2530  char *z;
2531
2532  if( p->zDestTable ){
2533    free(p->zDestTable);
2534    p->zDestTable = 0;
2535  }
2536  if( zName==0 ) return;
2537  cQuote = quoteChar(zName);
2538  n = strlen30(zName);
2539  if( cQuote ) n += n+2;
2540  z = p->zDestTable = malloc( n+1 );
2541  if( z==0 ) shell_out_of_memory();
2542  n = 0;
2543  if( cQuote ) z[n++] = cQuote;
2544  for(i=0; zName[i]; i++){
2545    z[n++] = zName[i];
2546    if( zName[i]==cQuote ) z[n++] = cQuote;
2547  }
2548  if( cQuote ) z[n++] = cQuote;
2549  z[n] = 0;
2550}
2551
2552
2553/*
2554** Execute a query statement that will generate SQL output.  Print
2555** the result columns, comma-separated, on a line and then add a
2556** semicolon terminator to the end of that line.
2557**
2558** If the number of columns is 1 and that column contains text "--"
2559** then write the semicolon on a separate line.  That way, if a
2560** "--" comment occurs at the end of the statement, the comment
2561** won't consume the semicolon terminator.
2562*/
2563static int run_table_dump_query(
2564  ShellState *p,           /* Query context */
2565  const char *zSelect      /* SELECT statement to extract content */
2566){
2567  sqlite3_stmt *pSelect;
2568  int rc;
2569  int nResult;
2570  int i;
2571  const char *z;
2572  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2573  if( rc!=SQLITE_OK || !pSelect ){
2574    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2575                sqlite3_errmsg(p->db));
2576    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2577    return rc;
2578  }
2579  rc = sqlite3_step(pSelect);
2580  nResult = sqlite3_column_count(pSelect);
2581  while( rc==SQLITE_ROW ){
2582    z = (const char*)sqlite3_column_text(pSelect, 0);
2583    utf8_printf(p->out, "%s", z);
2584    for(i=1; i<nResult; i++){
2585      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2586    }
2587    if( z==0 ) z = "";
2588    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2589    if( z[0] ){
2590      raw_printf(p->out, "\n;\n");
2591    }else{
2592      raw_printf(p->out, ";\n");
2593    }
2594    rc = sqlite3_step(pSelect);
2595  }
2596  rc = sqlite3_finalize(pSelect);
2597  if( rc!=SQLITE_OK ){
2598    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2599                sqlite3_errmsg(p->db));
2600    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2601  }
2602  return rc;
2603}
2604
2605/*
2606** Allocate space and save off string indicating current error.
2607*/
2608static char *save_err_msg(
2609  sqlite3 *db,           /* Database to query */
2610  const char *zWhen,     /* Qualifier (format) wrapper */
2611  int rc                 /* Error code returned from API */
2612){
2613  if( zWhen==0 )
2614    zWhen = "%s (%d)";
2615  return sqlite3_mprintf(zWhen, sqlite3_errmsg(db), rc);
2616}
2617
2618#ifdef __linux__
2619/*
2620** Attempt to display I/O stats on Linux using /proc/PID/io
2621*/
2622static void displayLinuxIoStats(FILE *out){
2623  FILE *in;
2624  char z[200];
2625  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2626  in = fopen(z, "rb");
2627  if( in==0 ) return;
2628  while( fgets(z, sizeof(z), in)!=0 ){
2629    static const struct {
2630      const char *zPattern;
2631      const char *zDesc;
2632    } aTrans[] = {
2633      { "rchar: ",                  "Bytes received by read():" },
2634      { "wchar: ",                  "Bytes sent to write():"    },
2635      { "syscr: ",                  "Read() system calls:"      },
2636      { "syscw: ",                  "Write() system calls:"     },
2637      { "read_bytes: ",             "Bytes read from storage:"  },
2638      { "write_bytes: ",            "Bytes written to storage:" },
2639      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2640    };
2641    int i;
2642    for(i=0; i<ArraySize(aTrans); i++){
2643      int n = strlen30(aTrans[i].zPattern);
2644      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2645        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2646        break;
2647      }
2648    }
2649  }
2650  fclose(in);
2651}
2652#endif
2653
2654/*
2655** Display a single line of status using 64-bit values.
2656*/
2657static void displayStatLine(
2658  ShellState *p,            /* The shell context */
2659  char *zLabel,             /* Label for this one line */
2660  char *zFormat,            /* Format for the result */
2661  int iStatusCtrl,          /* Which status to display */
2662  int bReset                /* True to reset the stats */
2663){
2664  sqlite3_int64 iCur = -1;
2665  sqlite3_int64 iHiwtr = -1;
2666  int i, nPercent;
2667  char zLine[200];
2668  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2669  for(i=0, nPercent=0; zFormat[i]; i++){
2670    if( zFormat[i]=='%' ) nPercent++;
2671  }
2672  if( nPercent>1 ){
2673    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2674  }else{
2675    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2676  }
2677  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2678}
2679
2680/*
2681** Display memory stats.
2682*/
2683static int display_stats(
2684  sqlite3 *db,                /* Database to query */
2685  ShellState *pArg,           /* Pointer to ShellState */
2686  int bReset                  /* True to reset the stats */
2687){
2688  int iCur;
2689  int iHiwtr;
2690  FILE *out;
2691  if( pArg==0 || pArg->out==0 ) return 0;
2692  out = pArg->out;
2693
2694  if( pArg->pStmt && pArg->statsOn==2 ){
2695    int nCol, i, x;
2696    sqlite3_stmt *pStmt = pArg->pStmt;
2697    char z[100];
2698    nCol = sqlite3_column_count(pStmt);
2699    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2700    for(i=0; i<nCol; i++){
2701      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2702      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2703#ifndef SQLITE_OMIT_DECLTYPE
2704      sqlite3_snprintf(30, z+x, "declared type:");
2705      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2706#endif
2707#ifdef SQLITE_ENABLE_COLUMN_METADATA
2708      sqlite3_snprintf(30, z+x, "database name:");
2709      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2710      sqlite3_snprintf(30, z+x, "table name:");
2711      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2712      sqlite3_snprintf(30, z+x, "origin name:");
2713      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2714#endif
2715    }
2716  }
2717
2718  if( pArg->statsOn==3 ){
2719    if( pArg->pStmt ){
2720      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2721      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2722    }
2723    return 0;
2724  }
2725
2726  displayStatLine(pArg, "Memory Used:",
2727     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2728  displayStatLine(pArg, "Number of Outstanding Allocations:",
2729     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2730  if( pArg->shellFlgs & SHFLG_Pagecache ){
2731    displayStatLine(pArg, "Number of Pcache Pages Used:",
2732       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2733  }
2734  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2735     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2736  displayStatLine(pArg, "Largest Allocation:",
2737     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2738  displayStatLine(pArg, "Largest Pcache Allocation:",
2739     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2740#ifdef YYTRACKMAXSTACKDEPTH
2741  displayStatLine(pArg, "Deepest Parser Stack:",
2742     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2743#endif
2744
2745  if( db ){
2746    if( pArg->shellFlgs & SHFLG_Lookaside ){
2747      iHiwtr = iCur = -1;
2748      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2749                        &iCur, &iHiwtr, bReset);
2750      raw_printf(pArg->out,
2751              "Lookaside Slots Used:                %d (max %d)\n",
2752              iCur, iHiwtr);
2753      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2754                        &iCur, &iHiwtr, bReset);
2755      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2756              iHiwtr);
2757      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2758                        &iCur, &iHiwtr, bReset);
2759      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2760              iHiwtr);
2761      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2762                        &iCur, &iHiwtr, bReset);
2763      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2764              iHiwtr);
2765    }
2766    iHiwtr = iCur = -1;
2767    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2768    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2769            iCur);
2770    iHiwtr = iCur = -1;
2771    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2772    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2773    iHiwtr = iCur = -1;
2774    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2775    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2776    iHiwtr = iCur = -1;
2777    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2778    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2779    iHiwtr = iCur = -1;
2780    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2781    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2782    iHiwtr = iCur = -1;
2783    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2784    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2785            iCur);
2786    iHiwtr = iCur = -1;
2787    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2788    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2789            iCur);
2790  }
2791
2792  if( pArg->pStmt ){
2793    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2794                               bReset);
2795    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2796    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2797    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2798    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2799    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2800    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2801    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2802    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2803    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2804    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2805    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2806    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2807    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2808  }
2809
2810#ifdef __linux__
2811  displayLinuxIoStats(pArg->out);
2812#endif
2813
2814  /* Do not remove this machine readable comment: extra-stats-output-here */
2815
2816  return 0;
2817}
2818
2819/*
2820** Display scan stats.
2821*/
2822static void display_scanstats(
2823  sqlite3 *db,                    /* Database to query */
2824  ShellState *pArg                /* Pointer to ShellState */
2825){
2826#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2827  UNUSED_PARAMETER(db);
2828  UNUSED_PARAMETER(pArg);
2829#else
2830  int i, k, n, mx;
2831  raw_printf(pArg->out, "-------- scanstats --------\n");
2832  mx = 0;
2833  for(k=0; k<=mx; k++){
2834    double rEstLoop = 1.0;
2835    for(i=n=0; 1; i++){
2836      sqlite3_stmt *p = pArg->pStmt;
2837      sqlite3_int64 nLoop, nVisit;
2838      double rEst;
2839      int iSid;
2840      const char *zExplain;
2841      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2842        break;
2843      }
2844      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2845      if( iSid>mx ) mx = iSid;
2846      if( iSid!=k ) continue;
2847      if( n==0 ){
2848        rEstLoop = (double)nLoop;
2849        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2850      }
2851      n++;
2852      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2853      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2854      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2855      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2856      rEstLoop *= rEst;
2857      raw_printf(pArg->out,
2858          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2859          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2860      );
2861    }
2862  }
2863  raw_printf(pArg->out, "---------------------------\n");
2864#endif
2865}
2866
2867/*
2868** Parameter azArray points to a zero-terminated array of strings. zStr
2869** points to a single nul-terminated string. Return non-zero if zStr
2870** is equal, according to strcmp(), to any of the strings in the array.
2871** Otherwise, return zero.
2872*/
2873static int str_in_array(const char *zStr, const char **azArray){
2874  int i;
2875  for(i=0; azArray[i]; i++){
2876    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2877  }
2878  return 0;
2879}
2880
2881/*
2882** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2883** and populate the ShellState.aiIndent[] array with the number of
2884** spaces each opcode should be indented before it is output.
2885**
2886** The indenting rules are:
2887**
2888**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2889**       all opcodes that occur between the p2 jump destination and the opcode
2890**       itself by 2 spaces.
2891**
2892**     * For each "Goto", if the jump destination is earlier in the program
2893**       and ends on one of:
2894**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2895**       or if the P1 parameter is one instead of zero,
2896**       then indent all opcodes between the earlier instruction
2897**       and "Goto" by 2 spaces.
2898*/
2899static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2900  const char *zSql;               /* The text of the SQL statement */
2901  const char *z;                  /* Used to check if this is an EXPLAIN */
2902  int *abYield = 0;               /* True if op is an OP_Yield */
2903  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2904  int iOp;                        /* Index of operation in p->aiIndent[] */
2905
2906  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2907  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2908                            "Rewind", 0 };
2909  const char *azGoto[] = { "Goto", 0 };
2910
2911  /* Try to figure out if this is really an EXPLAIN statement. If this
2912  ** cannot be verified, return early.  */
2913  if( sqlite3_column_count(pSql)!=8 ){
2914    p->cMode = p->mode;
2915    return;
2916  }
2917  zSql = sqlite3_sql(pSql);
2918  if( zSql==0 ) return;
2919  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2920  if( sqlite3_strnicmp(z, "explain", 7) ){
2921    p->cMode = p->mode;
2922    return;
2923  }
2924
2925  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2926    int i;
2927    int iAddr = sqlite3_column_int(pSql, 0);
2928    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2929
2930    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2931    ** p2 is an instruction address, set variable p2op to the index of that
2932    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2933    ** the current instruction is part of a sub-program generated by an
2934    ** SQL trigger or foreign key.  */
2935    int p2 = sqlite3_column_int(pSql, 3);
2936    int p2op = (p2 + (iOp-iAddr));
2937
2938    /* Grow the p->aiIndent array as required */
2939    if( iOp>=nAlloc ){
2940      if( iOp==0 ){
2941        /* Do further verfication that this is explain output.  Abort if
2942        ** it is not */
2943        static const char *explainCols[] = {
2944           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2945        int jj;
2946        for(jj=0; jj<ArraySize(explainCols); jj++){
2947          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2948            p->cMode = p->mode;
2949            sqlite3_reset(pSql);
2950            return;
2951          }
2952        }
2953      }
2954      nAlloc += 100;
2955      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2956      if( p->aiIndent==0 ) shell_out_of_memory();
2957      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2958      if( abYield==0 ) shell_out_of_memory();
2959    }
2960    abYield[iOp] = str_in_array(zOp, azYield);
2961    p->aiIndent[iOp] = 0;
2962    p->nIndent = iOp+1;
2963
2964    if( str_in_array(zOp, azNext) ){
2965      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2966    }
2967    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2968     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2969    ){
2970      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2971    }
2972  }
2973
2974  p->iIndent = 0;
2975  sqlite3_free(abYield);
2976  sqlite3_reset(pSql);
2977}
2978
2979/*
2980** Free the array allocated by explain_data_prepare().
2981*/
2982static void explain_data_delete(ShellState *p){
2983  sqlite3_free(p->aiIndent);
2984  p->aiIndent = 0;
2985  p->nIndent = 0;
2986  p->iIndent = 0;
2987}
2988
2989/*
2990** Disable and restore .wheretrace and .selecttrace settings.
2991*/
2992static unsigned int savedSelectTrace;
2993static unsigned int savedWhereTrace;
2994static void disable_debug_trace_modes(void){
2995  unsigned int zero = 0;
2996  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
2997  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
2998  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
2999  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3000}
3001static void restore_debug_trace_modes(void){
3002  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3003  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3004}
3005
3006/* Create the TEMP table used to store parameter bindings */
3007static void bind_table_init(ShellState *p){
3008  int wrSchema = 0;
3009  int defensiveMode = 0;
3010  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3011  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3012  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3013  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3014  sqlite3_exec(p->db,
3015    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3016    "  key TEXT PRIMARY KEY,\n"
3017    "  value\n"
3018    ") WITHOUT ROWID;",
3019    0, 0, 0);
3020  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3021  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3022}
3023
3024/*
3025** Bind parameters on a prepared statement.
3026**
3027** Parameter bindings are taken from a TEMP table of the form:
3028**
3029**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3030**    WITHOUT ROWID;
3031**
3032** No bindings occur if this table does not exist.  The name of the table
3033** begins with "sqlite_" so that it will not collide with ordinary application
3034** tables.  The table must be in the TEMP schema.
3035*/
3036static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3037  int nVar;
3038  int i;
3039  int rc;
3040  sqlite3_stmt *pQ = 0;
3041
3042  nVar = sqlite3_bind_parameter_count(pStmt);
3043  if( nVar==0 ) return;  /* Nothing to do */
3044  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3045                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3046    return; /* Parameter table does not exist */
3047  }
3048  rc = sqlite3_prepare_v2(pArg->db,
3049          "SELECT value FROM temp.sqlite_parameters"
3050          " WHERE key=?1", -1, &pQ, 0);
3051  if( rc || pQ==0 ) return;
3052  for(i=1; i<=nVar; i++){
3053    char zNum[30];
3054    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3055    if( zVar==0 ){
3056      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3057      zVar = zNum;
3058    }
3059    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3060    if( sqlite3_step(pQ)==SQLITE_ROW ){
3061      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3062    }else{
3063      sqlite3_bind_null(pStmt, i);
3064    }
3065    sqlite3_reset(pQ);
3066  }
3067  sqlite3_finalize(pQ);
3068}
3069
3070/*
3071** UTF8 box-drawing characters.  Imagine box lines like this:
3072**
3073**           1
3074**           |
3075**       4 --+-- 2
3076**           |
3077**           3
3078**
3079** Each box characters has between 2 and 4 of the lines leading from
3080** the center.  The characters are here identified by the numbers of
3081** their corresponding lines.
3082*/
3083#define BOX_24   "\342\224\200"  /* U+2500 --- */
3084#define BOX_13   "\342\224\202"  /* U+2502  |  */
3085#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3086#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3087#define BOX_12   "\342\224\224"  /* U+2514  '- */
3088#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3089#define BOX_123  "\342\224\234"  /* U+251c  |- */
3090#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3091#define BOX_234  "\342\224\254"  /* U+252c -,- */
3092#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3093#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3094
3095/* Draw horizontal line N characters long using unicode box
3096** characters
3097*/
3098static void print_box_line(FILE *out, int N){
3099  const char zDash[] =
3100      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3101      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3102  const int nDash = sizeof(zDash) - 1;
3103  N *= 3;
3104  while( N>nDash ){
3105    utf8_printf(out, zDash);
3106    N -= nDash;
3107  }
3108  utf8_printf(out, "%.*s", N, zDash);
3109}
3110
3111/*
3112** Draw a horizontal separator for a MODE_Box table.
3113*/
3114static void print_box_row_separator(
3115  ShellState *p,
3116  int nArg,
3117  const char *zSep1,
3118  const char *zSep2,
3119  const char *zSep3
3120){
3121  int i;
3122  if( nArg>0 ){
3123    utf8_printf(p->out, "%s", zSep1);
3124    print_box_line(p->out, p->actualWidth[0]+2);
3125    for(i=1; i<nArg; i++){
3126      utf8_printf(p->out, "%s", zSep2);
3127      print_box_line(p->out, p->actualWidth[i]+2);
3128    }
3129    utf8_printf(p->out, "%s", zSep3);
3130  }
3131  fputs("\n", p->out);
3132}
3133
3134
3135
3136/*
3137** Run a prepared statement and output the result in one of the
3138** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3139** or MODE_Box.
3140**
3141** This is different from ordinary exec_prepared_stmt() in that
3142** it has to run the entire query and gather the results into memory
3143** first, in order to determine column widths, before providing
3144** any output.
3145*/
3146static void exec_prepared_stmt_columnar(
3147  ShellState *p,                        /* Pointer to ShellState */
3148  sqlite3_stmt *pStmt                   /* Statment to run */
3149){
3150  sqlite3_int64 nRow = 0;
3151  int nColumn = 0;
3152  char **azData = 0;
3153  sqlite3_int64 nAlloc = 0;
3154  const char *z;
3155  int rc;
3156  sqlite3_int64 i, nData;
3157  int j, nTotal, w, n;
3158  const char *colSep = 0;
3159  const char *rowSep = 0;
3160
3161  rc = sqlite3_step(pStmt);
3162  if( rc!=SQLITE_ROW ) return;
3163  nColumn = sqlite3_column_count(pStmt);
3164  nAlloc = nColumn*4;
3165  if( nAlloc<=0 ) nAlloc = 1;
3166  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3167  if( azData==0 ) shell_out_of_memory();
3168  for(i=0; i<nColumn; i++){
3169    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3170  }
3171  do{
3172    if( (nRow+2)*nColumn >= nAlloc ){
3173      nAlloc *= 2;
3174      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3175      if( azData==0 ) shell_out_of_memory();
3176    }
3177    nRow++;
3178    for(i=0; i<nColumn; i++){
3179      z = (const char*)sqlite3_column_text(pStmt,i);
3180      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3181    }
3182  }while( sqlite3_step(pStmt)==SQLITE_ROW );
3183  if( nColumn>p->nWidth ){
3184    p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
3185    if( p->colWidth==0 ) shell_out_of_memory();
3186    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3187    p->nWidth = nColumn;
3188    p->actualWidth = &p->colWidth[nColumn];
3189  }
3190  memset(p->actualWidth, 0, nColumn*sizeof(int));
3191  for(i=0; i<nColumn; i++){
3192    w = p->colWidth[i];
3193    if( w<0 ) w = -w;
3194    p->actualWidth[i] = w;
3195  }
3196  nTotal = nColumn*(nRow+1);
3197  for(i=0; i<nTotal; i++){
3198    z = azData[i];
3199    if( z==0 ) z = p->nullValue;
3200    n = strlenChar(z);
3201    j = i%nColumn;
3202    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3203  }
3204  if( seenInterrupt ) goto columnar_end;
3205  if( nColumn==0 ) goto columnar_end;
3206  switch( p->cMode ){
3207    case MODE_Column: {
3208      colSep = "  ";
3209      rowSep = "\n";
3210      if( p->showHeader ){
3211        for(i=0; i<nColumn; i++){
3212          w = p->actualWidth[i];
3213          if( p->colWidth[i]<0 ) w = -w;
3214          utf8_width_print(p->out, w, azData[i]);
3215          fputs(i==nColumn-1?"\n":"  ", p->out);
3216        }
3217        for(i=0; i<nColumn; i++){
3218          print_dashes(p->out, p->actualWidth[i]);
3219          fputs(i==nColumn-1?"\n":"  ", p->out);
3220        }
3221      }
3222      break;
3223    }
3224    case MODE_Table: {
3225      colSep = " | ";
3226      rowSep = " |\n";
3227      print_row_separator(p, nColumn, "+");
3228      fputs("| ", p->out);
3229      for(i=0; i<nColumn; i++){
3230        w = p->actualWidth[i];
3231        n = strlenChar(azData[i]);
3232        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3233        fputs(i==nColumn-1?" |\n":" | ", p->out);
3234      }
3235      print_row_separator(p, nColumn, "+");
3236      break;
3237    }
3238    case MODE_Markdown: {
3239      colSep = " | ";
3240      rowSep = " |\n";
3241      fputs("| ", p->out);
3242      for(i=0; i<nColumn; i++){
3243        w = p->actualWidth[i];
3244        n = strlenChar(azData[i]);
3245        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3246        fputs(i==nColumn-1?" |\n":" | ", p->out);
3247      }
3248      print_row_separator(p, nColumn, "|");
3249      break;
3250    }
3251    case MODE_Box: {
3252      colSep = " " BOX_13 " ";
3253      rowSep = " " BOX_13 "\n";
3254      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3255      utf8_printf(p->out, BOX_13 " ");
3256      for(i=0; i<nColumn; i++){
3257        w = p->actualWidth[i];
3258        n = strlenChar(azData[i]);
3259        utf8_printf(p->out, "%*s%s%*s%s",
3260            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3261            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3262      }
3263      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3264      break;
3265    }
3266  }
3267  for(i=nColumn, j=0; i<nTotal; i++, j++){
3268    if( j==0 && p->cMode!=MODE_Column ){
3269      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3270    }
3271    z = azData[i];
3272    if( z==0 ) z = p->nullValue;
3273    w = p->actualWidth[j];
3274    if( p->colWidth[j]<0 ) w = -w;
3275    utf8_width_print(p->out, w, z);
3276    if( j==nColumn-1 ){
3277      utf8_printf(p->out, "%s", rowSep);
3278      j = -1;
3279      if( seenInterrupt ) goto columnar_end;
3280    }else{
3281      utf8_printf(p->out, "%s", colSep);
3282    }
3283  }
3284  if( p->cMode==MODE_Table ){
3285    print_row_separator(p, nColumn, "+");
3286  }else if( p->cMode==MODE_Box ){
3287    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3288  }
3289columnar_end:
3290  if( seenInterrupt ){
3291    utf8_printf(p->out, "Interrupt\n");
3292  }
3293  nData = (nRow+1)*nColumn;
3294  for(i=0; i<nData; i++) free(azData[i]);
3295  sqlite3_free(azData);
3296}
3297
3298/*
3299** Run a prepared statement
3300*/
3301static void exec_prepared_stmt(
3302  ShellState *pArg,                                /* Pointer to ShellState */
3303  sqlite3_stmt *pStmt                              /* Statment to run */
3304){
3305  int rc;
3306
3307  if( pArg->cMode==MODE_Column
3308   || pArg->cMode==MODE_Table
3309   || pArg->cMode==MODE_Box
3310   || pArg->cMode==MODE_Markdown
3311  ){
3312    exec_prepared_stmt_columnar(pArg, pStmt);
3313    return;
3314  }
3315
3316  /* perform the first step.  this will tell us if we
3317  ** have a result set or not and how wide it is.
3318  */
3319  rc = sqlite3_step(pStmt);
3320  /* if we have a result set... */
3321  if( SQLITE_ROW == rc ){
3322    /* allocate space for col name ptr, value ptr, and type */
3323    int nCol = sqlite3_column_count(pStmt);
3324    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3325    if( !pData ){
3326      shell_out_of_memory();
3327    }else{
3328      char **azCols = (char **)pData;      /* Names of result columns */
3329      char **azVals = &azCols[nCol];       /* Results */
3330      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3331      int i, x;
3332      assert(sizeof(int) <= sizeof(char *));
3333      /* save off ptrs to column names */
3334      for(i=0; i<nCol; i++){
3335        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3336      }
3337      do{
3338        /* extract the data and data types */
3339        for(i=0; i<nCol; i++){
3340          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3341          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3342            azVals[i] = "";
3343          }else{
3344            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3345          }
3346          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3347            rc = SQLITE_NOMEM;
3348            break; /* from for */
3349          }
3350        } /* end for */
3351
3352        /* if data and types extracted successfully... */
3353        if( SQLITE_ROW == rc ){
3354          /* call the supplied callback with the result row data */
3355          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3356            rc = SQLITE_ABORT;
3357          }else{
3358            rc = sqlite3_step(pStmt);
3359          }
3360        }
3361      } while( SQLITE_ROW == rc );
3362      sqlite3_free(pData);
3363      if( pArg->cMode==MODE_Json ){
3364        fputs("]\n", pArg->out);
3365      }
3366    }
3367  }
3368}
3369
3370#ifndef SQLITE_OMIT_VIRTUALTABLE
3371/*
3372** This function is called to process SQL if the previous shell command
3373** was ".expert". It passes the SQL in the second argument directly to
3374** the sqlite3expert object.
3375**
3376** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3377** code. In this case, (*pzErr) may be set to point to a buffer containing
3378** an English language error message. It is the responsibility of the
3379** caller to eventually free this buffer using sqlite3_free().
3380*/
3381static int expertHandleSQL(
3382  ShellState *pState,
3383  const char *zSql,
3384  char **pzErr
3385){
3386  assert( pState->expert.pExpert );
3387  assert( pzErr==0 || *pzErr==0 );
3388  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3389}
3390
3391/*
3392** This function is called either to silently clean up the object
3393** created by the ".expert" command (if bCancel==1), or to generate a
3394** report from it and then clean it up (if bCancel==0).
3395**
3396** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3397** code. In this case, (*pzErr) may be set to point to a buffer containing
3398** an English language error message. It is the responsibility of the
3399** caller to eventually free this buffer using sqlite3_free().
3400*/
3401static int expertFinish(
3402  ShellState *pState,
3403  int bCancel,
3404  char **pzErr
3405){
3406  int rc = SQLITE_OK;
3407  sqlite3expert *p = pState->expert.pExpert;
3408  assert( p );
3409  assert( bCancel || pzErr==0 || *pzErr==0 );
3410  if( bCancel==0 ){
3411    FILE *out = pState->out;
3412    int bVerbose = pState->expert.bVerbose;
3413
3414    rc = sqlite3_expert_analyze(p, pzErr);
3415    if( rc==SQLITE_OK ){
3416      int nQuery = sqlite3_expert_count(p);
3417      int i;
3418
3419      if( bVerbose ){
3420        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3421        raw_printf(out, "-- Candidates -----------------------------\n");
3422        raw_printf(out, "%s\n", zCand);
3423      }
3424      for(i=0; i<nQuery; i++){
3425        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3426        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3427        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3428        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3429        if( bVerbose ){
3430          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3431          raw_printf(out, "%s\n\n", zSql);
3432        }
3433        raw_printf(out, "%s\n", zIdx);
3434        raw_printf(out, "%s\n", zEQP);
3435      }
3436    }
3437  }
3438  sqlite3_expert_destroy(p);
3439  pState->expert.pExpert = 0;
3440  return rc;
3441}
3442
3443/*
3444** Implementation of ".expert" dot command.
3445*/
3446static int expertDotCommand(
3447  ShellState *pState,             /* Current shell tool state */
3448  char **azArg,                   /* Array of arguments passed to dot command */
3449  int nArg                        /* Number of entries in azArg[] */
3450){
3451  int rc = SQLITE_OK;
3452  char *zErr = 0;
3453  int i;
3454  int iSample = 0;
3455
3456  assert( pState->expert.pExpert==0 );
3457  memset(&pState->expert, 0, sizeof(ExpertInfo));
3458
3459  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3460    char *z = azArg[i];
3461    int n;
3462    if( z[0]=='-' && z[1]=='-' ) z++;
3463    n = strlen30(z);
3464    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3465      pState->expert.bVerbose = 1;
3466    }
3467    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3468      if( i==(nArg-1) ){
3469        raw_printf(stderr, "option requires an argument: %s\n", z);
3470        rc = SQLITE_ERROR;
3471      }else{
3472        iSample = (int)integerValue(azArg[++i]);
3473        if( iSample<0 || iSample>100 ){
3474          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3475          rc = SQLITE_ERROR;
3476        }
3477      }
3478    }
3479    else{
3480      raw_printf(stderr, "unknown option: %s\n", z);
3481      rc = SQLITE_ERROR;
3482    }
3483  }
3484
3485  if( rc==SQLITE_OK ){
3486    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3487    if( pState->expert.pExpert==0 ){
3488      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3489      rc = SQLITE_ERROR;
3490    }else{
3491      sqlite3_expert_config(
3492          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3493      );
3494    }
3495  }
3496
3497  return rc;
3498}
3499#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3500
3501/*
3502** Execute a statement or set of statements.  Print
3503** any result rows/columns depending on the current mode
3504** set via the supplied callback.
3505**
3506** This is very similar to SQLite's built-in sqlite3_exec()
3507** function except it takes a slightly different callback
3508** and callback data argument.
3509*/
3510static int shell_exec(
3511  ShellState *pArg,                         /* Pointer to ShellState */
3512  const char *zSql,                         /* SQL to be evaluated */
3513  char **pzErrMsg                           /* Error msg written here */
3514){
3515  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3516  int rc = SQLITE_OK;             /* Return Code */
3517  int rc2;
3518  const char *zLeftover;          /* Tail of unprocessed SQL */
3519  sqlite3 *db = pArg->db;
3520
3521  if( pzErrMsg ){
3522    *pzErrMsg = NULL;
3523  }
3524
3525#ifndef SQLITE_OMIT_VIRTUALTABLE
3526  if( pArg->expert.pExpert ){
3527    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3528    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3529  }
3530#endif
3531
3532  while( zSql[0] && (SQLITE_OK == rc) ){
3533    static const char *zStmtSql;
3534    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3535    if( SQLITE_OK != rc ){
3536      if( pzErrMsg ){
3537        *pzErrMsg = save_err_msg(db, "in prepare, %s (%d)", rc);
3538      }
3539    }else{
3540      if( !pStmt ){
3541        /* this happens for a comment or white-space */
3542        zSql = zLeftover;
3543        while( IsSpace(zSql[0]) ) zSql++;
3544        continue;
3545      }
3546      zStmtSql = sqlite3_sql(pStmt);
3547      if( zStmtSql==0 ) zStmtSql = "";
3548      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3549
3550      /* save off the prepared statment handle and reset row count */
3551      if( pArg ){
3552        pArg->pStmt = pStmt;
3553        pArg->cnt = 0;
3554      }
3555
3556      /* echo the sql statement if echo on */
3557      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3558        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3559      }
3560
3561      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3562      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3563        sqlite3_stmt *pExplain;
3564        char *zEQP;
3565        int triggerEQP = 0;
3566        disable_debug_trace_modes();
3567        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3568        if( pArg->autoEQP>=AUTOEQP_trigger ){
3569          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3570        }
3571        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3572        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3573        if( rc==SQLITE_OK ){
3574          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3575            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3576            int iEqpId = sqlite3_column_int(pExplain, 0);
3577            int iParentId = sqlite3_column_int(pExplain, 1);
3578            if( zEQPLine==0 ) zEQPLine = "";
3579            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3580            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3581          }
3582          eqp_render(pArg);
3583        }
3584        sqlite3_finalize(pExplain);
3585        sqlite3_free(zEQP);
3586        if( pArg->autoEQP>=AUTOEQP_full ){
3587          /* Also do an EXPLAIN for ".eqp full" mode */
3588          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3589          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3590          if( rc==SQLITE_OK ){
3591            pArg->cMode = MODE_Explain;
3592            explain_data_prepare(pArg, pExplain);
3593            exec_prepared_stmt(pArg, pExplain);
3594            explain_data_delete(pArg);
3595          }
3596          sqlite3_finalize(pExplain);
3597          sqlite3_free(zEQP);
3598        }
3599        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3600          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3601          /* Reprepare pStmt before reactiving trace modes */
3602          sqlite3_finalize(pStmt);
3603          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3604          if( pArg ) pArg->pStmt = pStmt;
3605        }
3606        restore_debug_trace_modes();
3607      }
3608
3609      if( pArg ){
3610        pArg->cMode = pArg->mode;
3611        if( pArg->autoExplain ){
3612          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3613            pArg->cMode = MODE_Explain;
3614          }
3615          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3616            pArg->cMode = MODE_EQP;
3617          }
3618        }
3619
3620        /* If the shell is currently in ".explain" mode, gather the extra
3621        ** data required to add indents to the output.*/
3622        if( pArg->cMode==MODE_Explain ){
3623          explain_data_prepare(pArg, pStmt);
3624        }
3625      }
3626
3627      bind_prepared_stmt(pArg, pStmt);
3628      exec_prepared_stmt(pArg, pStmt);
3629      explain_data_delete(pArg);
3630      eqp_render(pArg);
3631
3632      /* print usage stats if stats on */
3633      if( pArg && pArg->statsOn ){
3634        display_stats(db, pArg, 0);
3635      }
3636
3637      /* print loop-counters if required */
3638      if( pArg && pArg->scanstatsOn ){
3639        display_scanstats(db, pArg);
3640      }
3641
3642      /* Finalize the statement just executed. If this fails, save a
3643      ** copy of the error message. Otherwise, set zSql to point to the
3644      ** next statement to execute. */
3645      rc2 = sqlite3_finalize(pStmt);
3646      if( rc!=SQLITE_NOMEM ) rc = rc2;
3647      if( rc==SQLITE_OK ){
3648        zSql = zLeftover;
3649        while( IsSpace(zSql[0]) ) zSql++;
3650      }else if( pzErrMsg ){
3651        *pzErrMsg = save_err_msg(db, "stepping, %s (%d)", rc);
3652      }
3653
3654      /* clear saved stmt handle */
3655      if( pArg ){
3656        pArg->pStmt = NULL;
3657      }
3658    }
3659  } /* end while */
3660
3661  return rc;
3662}
3663
3664/*
3665** Release memory previously allocated by tableColumnList().
3666*/
3667static void freeColumnList(char **azCol){
3668  int i;
3669  for(i=1; azCol[i]; i++){
3670    sqlite3_free(azCol[i]);
3671  }
3672  /* azCol[0] is a static string */
3673  sqlite3_free(azCol);
3674}
3675
3676/*
3677** Return a list of pointers to strings which are the names of all
3678** columns in table zTab.   The memory to hold the names is dynamically
3679** allocated and must be released by the caller using a subsequent call
3680** to freeColumnList().
3681**
3682** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3683** value that needs to be preserved, then azCol[0] is filled in with the
3684** name of the rowid column.
3685**
3686** The first regular column in the table is azCol[1].  The list is terminated
3687** by an entry with azCol[i]==0.
3688*/
3689static char **tableColumnList(ShellState *p, const char *zTab){
3690  char **azCol = 0;
3691  sqlite3_stmt *pStmt;
3692  char *zSql;
3693  int nCol = 0;
3694  int nAlloc = 0;
3695  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3696  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3697  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3698  int rc;
3699
3700  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3701  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3702  sqlite3_free(zSql);
3703  if( rc ) return 0;
3704  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3705    if( nCol>=nAlloc-2 ){
3706      nAlloc = nAlloc*2 + nCol + 10;
3707      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3708      if( azCol==0 ) shell_out_of_memory();
3709    }
3710    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3711    if( sqlite3_column_int(pStmt, 5) ){
3712      nPK++;
3713      if( nPK==1
3714       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3715                          "INTEGER")==0
3716      ){
3717        isIPK = 1;
3718      }else{
3719        isIPK = 0;
3720      }
3721    }
3722  }
3723  sqlite3_finalize(pStmt);
3724  if( azCol==0 ) return 0;
3725  azCol[0] = 0;
3726  azCol[nCol+1] = 0;
3727
3728  /* The decision of whether or not a rowid really needs to be preserved
3729  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3730  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3731  ** rowids on tables where the rowid is inaccessible because there are other
3732  ** columns in the table named "rowid", "_rowid_", and "oid".
3733  */
3734  if( preserveRowid && isIPK ){
3735    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3736    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3737    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3738    ** ROWID aliases.  To distinguish these cases, check to see if
3739    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3740    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3741    */
3742    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3743                           " WHERE origin='pk'", zTab);
3744    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3745    sqlite3_free(zSql);
3746    if( rc ){
3747      freeColumnList(azCol);
3748      return 0;
3749    }
3750    rc = sqlite3_step(pStmt);
3751    sqlite3_finalize(pStmt);
3752    preserveRowid = rc==SQLITE_ROW;
3753  }
3754  if( preserveRowid ){
3755    /* Only preserve the rowid if we can find a name to use for the
3756    ** rowid */
3757    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3758    int i, j;
3759    for(j=0; j<3; j++){
3760      for(i=1; i<=nCol; i++){
3761        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3762      }
3763      if( i>nCol ){
3764        /* At this point, we know that azRowid[j] is not the name of any
3765        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3766        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3767        ** tables will fail this last check */
3768        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3769        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3770        break;
3771      }
3772    }
3773  }
3774  return azCol;
3775}
3776
3777/*
3778** Toggle the reverse_unordered_selects setting.
3779*/
3780static void toggleSelectOrder(sqlite3 *db){
3781  sqlite3_stmt *pStmt = 0;
3782  int iSetting = 0;
3783  char zStmt[100];
3784  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3785  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3786    iSetting = sqlite3_column_int(pStmt, 0);
3787  }
3788  sqlite3_finalize(pStmt);
3789  sqlite3_snprintf(sizeof(zStmt), zStmt,
3790       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3791  sqlite3_exec(db, zStmt, 0, 0, 0);
3792}
3793
3794/*
3795** This is a different callback routine used for dumping the database.
3796** Each row received by this callback consists of a table name,
3797** the table type ("index" or "table") and SQL to create the table.
3798** This routine should print text sufficient to recreate the table.
3799*/
3800static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3801  int rc;
3802  const char *zTable;
3803  const char *zType;
3804  const char *zSql;
3805  ShellState *p = (ShellState *)pArg;
3806  int dataOnly;
3807  int noSys;
3808
3809  UNUSED_PARAMETER(azNotUsed);
3810  if( nArg!=3 || azArg==0 ) return 0;
3811  zTable = azArg[0];
3812  zType = azArg[1];
3813  zSql = azArg[2];
3814  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
3815  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
3816
3817  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
3818    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3819  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
3820    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3821  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3822    return 0;
3823  }else if( dataOnly ){
3824    /* no-op */
3825  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3826    char *zIns;
3827    if( !p->writableSchema ){
3828      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3829      p->writableSchema = 1;
3830    }
3831    zIns = sqlite3_mprintf(
3832       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3833       "VALUES('table','%q','%q',0,'%q');",
3834       zTable, zTable, zSql);
3835    utf8_printf(p->out, "%s\n", zIns);
3836    sqlite3_free(zIns);
3837    return 0;
3838  }else{
3839    printSchemaLine(p->out, zSql, ";\n");
3840  }
3841
3842  if( strcmp(zType, "table")==0 ){
3843    ShellText sSelect;
3844    ShellText sTable;
3845    char **azCol;
3846    int i;
3847    char *savedDestTable;
3848    int savedMode;
3849
3850    azCol = tableColumnList(p, zTable);
3851    if( azCol==0 ){
3852      p->nErr++;
3853      return 0;
3854    }
3855
3856    /* Always quote the table name, even if it appears to be pure ascii,
3857    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3858    initText(&sTable);
3859    appendText(&sTable, zTable, quoteChar(zTable));
3860    /* If preserving the rowid, add a column list after the table name.
3861    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3862    ** instead of the usual "INSERT INTO tab VALUES(...)".
3863    */
3864    if( azCol[0] ){
3865      appendText(&sTable, "(", 0);
3866      appendText(&sTable, azCol[0], 0);
3867      for(i=1; azCol[i]; i++){
3868        appendText(&sTable, ",", 0);
3869        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3870      }
3871      appendText(&sTable, ")", 0);
3872    }
3873
3874    /* Build an appropriate SELECT statement */
3875    initText(&sSelect);
3876    appendText(&sSelect, "SELECT ", 0);
3877    if( azCol[0] ){
3878      appendText(&sSelect, azCol[0], 0);
3879      appendText(&sSelect, ",", 0);
3880    }
3881    for(i=1; azCol[i]; i++){
3882      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3883      if( azCol[i+1] ){
3884        appendText(&sSelect, ",", 0);
3885      }
3886    }
3887    freeColumnList(azCol);
3888    appendText(&sSelect, " FROM ", 0);
3889    appendText(&sSelect, zTable, quoteChar(zTable));
3890
3891    savedDestTable = p->zDestTable;
3892    savedMode = p->mode;
3893    p->zDestTable = sTable.z;
3894    p->mode = p->cMode = MODE_Insert;
3895    rc = shell_exec(p, sSelect.z, 0);
3896    if( (rc&0xff)==SQLITE_CORRUPT ){
3897      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3898      toggleSelectOrder(p->db);
3899      shell_exec(p, sSelect.z, 0);
3900      toggleSelectOrder(p->db);
3901    }
3902    p->zDestTable = savedDestTable;
3903    p->mode = savedMode;
3904    freeText(&sTable);
3905    freeText(&sSelect);
3906    if( rc ) p->nErr++;
3907  }
3908  return 0;
3909}
3910
3911/*
3912** Run zQuery.  Use dump_callback() as the callback routine so that
3913** the contents of the query are output as SQL statements.
3914**
3915** If we get a SQLITE_CORRUPT error, rerun the query after appending
3916** "ORDER BY rowid DESC" to the end.
3917*/
3918static int run_schema_dump_query(
3919  ShellState *p,
3920  const char *zQuery
3921){
3922  int rc;
3923  char *zErr = 0;
3924  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3925  if( rc==SQLITE_CORRUPT ){
3926    char *zQ2;
3927    int len = strlen30(zQuery);
3928    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3929    if( zErr ){
3930      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3931      sqlite3_free(zErr);
3932      zErr = 0;
3933    }
3934    zQ2 = malloc( len+100 );
3935    if( zQ2==0 ) return rc;
3936    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3937    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3938    if( rc ){
3939      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3940    }else{
3941      rc = SQLITE_CORRUPT;
3942    }
3943    sqlite3_free(zErr);
3944    free(zQ2);
3945  }
3946  return rc;
3947}
3948
3949/*
3950** Text of help messages.
3951**
3952** The help text for each individual command begins with a line that starts
3953** with ".".  Subsequent lines are supplimental information.
3954**
3955** There must be two or more spaces between the end of the command and the
3956** start of the description of what that command does.
3957*/
3958static const char *(azHelp[]) = {
3959#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3960  ".archive ...             Manage SQL archives",
3961  "   Each command must have exactly one of the following options:",
3962  "     -c, --create               Create a new archive",
3963  "     -u, --update               Add or update files with changed mtime",
3964  "     -i, --insert               Like -u but always add even if unchanged",
3965  "     -r, --remove               Remove files from archive",
3966  "     -t, --list                 List contents of archive",
3967  "     -x, --extract              Extract files from archive",
3968  "   Optional arguments:",
3969  "     -v, --verbose              Print each filename as it is processed",
3970  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3971  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3972  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3973  "     -g, --glob                 Use glob matching for names in archive",
3974  "     -n, --dryrun               Show the SQL that would have occurred",
3975  "   Examples:",
3976  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3977  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3978  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3979  "   See also:",
3980  "      http://sqlite.org/cli.html#sqlite_archive_support",
3981#endif
3982#ifndef SQLITE_OMIT_AUTHORIZATION
3983  ".auth ON|OFF             Show authorizer callbacks",
3984#endif
3985  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3986  "       --append            Use the appendvfs",
3987  "       --async             Write to FILE without journal and fsync()",
3988  ".bail on|off             Stop after hitting an error.  Default OFF",
3989  ".binary on|off           Turn binary output on or off.  Default OFF",
3990  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3991  ".changes on|off          Show number of rows changed by SQL",
3992  ".check GLOB              Fail if output since .testcase does not match",
3993  ".clone NEWDB             Clone data into NEWDB from the existing database",
3994  ".connection [close] [#]  Open or close an auxiliary database connection",
3995  ".databases               List names and files of attached databases",
3996  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3997  ".dbinfo ?DB?             Show status information about the database",
3998  ".dump ?OBJECTS?          Render database content as SQL",
3999  "   Options:",
4000  "     --data-only            Output only INSERT statements",
4001  "     --newlines             Allow unescaped newline characters in output",
4002  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
4003  "     --preserve-rowids      Include ROWID values in the output",
4004  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4005  "   Additional LIKE patterns can be given in subsequent arguments",
4006  ".echo on|off             Turn command echo on or off",
4007  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
4008  "   Other Modes:",
4009#ifdef SQLITE_DEBUG
4010  "      test                  Show raw EXPLAIN QUERY PLAN output",
4011  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4012#endif
4013  "      trigger               Like \"full\" but also show trigger bytecode",
4014  ".excel                   Display the output of next command in spreadsheet",
4015  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
4016  ".exit ?CODE?             Exit this program with return-code CODE",
4017  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
4018  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
4019  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
4020  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
4021  "   --help                  Show CMD details",
4022  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
4023  ".headers on|off          Turn display of headers on or off",
4024  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
4025  ".import FILE TABLE       Import data from FILE into TABLE",
4026  "   Options:",
4027  "     --ascii               Use \\037 and \\036 as column and row separators",
4028  "     --csv                 Use , and \\n as column and row separators",
4029  "     --skip N              Skip the first N rows of input",
4030  "     -v                    \"Verbose\" - increase auxiliary output",
4031  "   Notes:",
4032  "     *  If TABLE does not exist, it is created.  The first row of input",
4033  "        determines the column names.",
4034  "     *  If neither --csv or --ascii are used, the input mode is derived",
4035  "        from the \".mode\" output mode",
4036  "     *  If FILE begins with \"|\" then it is a command that generates the",
4037  "        input text.",
4038#ifndef SQLITE_OMIT_TEST_CONTROL
4039  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
4040#endif
4041  ".indexes ?TABLE?         Show names of indexes",
4042  "                           If TABLE is specified, only show indexes for",
4043  "                           tables matching TABLE using the LIKE operator.",
4044#ifdef SQLITE_ENABLE_IOTRACE
4045  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
4046#endif
4047  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
4048  ".lint OPTIONS            Report potential schema issues.",
4049  "     Options:",
4050  "        fkey-indexes     Find missing foreign key indexes",
4051#ifndef SQLITE_OMIT_LOAD_EXTENSION
4052  ".load FILE ?ENTRY?       Load an extension library",
4053#endif
4054  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
4055  ".mode MODE ?TABLE?       Set output mode",
4056  "   MODE is one of:",
4057  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
4058  "     box       Tables using unicode box-drawing characters",
4059  "     csv       Comma-separated values",
4060  "     column    Output in columns.  (See .width)",
4061  "     html      HTML <table> code",
4062  "     insert    SQL insert statements for TABLE",
4063  "     json      Results in a JSON array",
4064  "     line      One value per line",
4065  "     list      Values delimited by \"|\"",
4066  "     markdown  Markdown table format",
4067  "     quote     Escape answers as for SQL",
4068  "     table     ASCII-art table",
4069  "     tabs      Tab-separated values",
4070  "     tcl       TCL list elements",
4071  ".nonce STRING            Disable safe mode for one command if the nonce matches",
4072  ".nullvalue STRING        Use STRING in place of NULL values",
4073  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
4074  "     If FILE begins with '|' then open as a pipe",
4075  "       --bom  Put a UTF8 byte-order mark at the beginning",
4076  "       -e     Send output to the system text editor",
4077  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
4078#ifdef SQLITE_DEBUG
4079  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
4080#endif
4081  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
4082  "     Options:",
4083  "        --append        Use appendvfs to append database to the end of FILE",
4084#ifndef SQLITE_OMIT_DESERIALIZE
4085  "        --deserialize   Load into memory using sqlite3_deserialize()",
4086  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
4087  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
4088#endif
4089  "        --new           Initialize FILE to an empty database",
4090  "        --nofollow      Do not follow symbolic links",
4091  "        --readonly      Open FILE readonly",
4092  "        --zip           FILE is a ZIP archive",
4093  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4094  "   If FILE begins with '|' then open it as a pipe.",
4095  "   Options:",
4096  "     --bom                 Prefix output with a UTF8 byte-order mark",
4097  "     -e                    Send output to the system text editor",
4098  "     -x                    Send output as CSV to a spreadsheet",
4099  ".parameter CMD ...       Manage SQL parameter bindings",
4100  "   clear                   Erase all bindings",
4101  "   init                    Initialize the TEMP table that holds bindings",
4102  "   list                    List the current parameter bindings",
4103  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4104  "                           PARAMETER should start with one of: $ : @ ?",
4105  "   unset PARAMETER         Remove PARAMETER from the binding table",
4106  ".print STRING...         Print literal STRING",
4107#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4108  ".progress N              Invoke progress handler after every N opcodes",
4109  "   --limit N                 Interrupt after N progress callbacks",
4110  "   --once                    Do no more than one progress interrupt",
4111  "   --quiet|-q                No output except at interrupts",
4112  "   --reset                   Reset the count for each input and interrupt",
4113#endif
4114  ".prompt MAIN CONTINUE    Replace the standard prompts",
4115  ".quit                    Exit this program",
4116  ".read FILE               Read input from FILE",
4117#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4118  ".recover                 Recover as much data as possible from corrupt db.",
4119  "   --freelist-corrupt       Assume the freelist is corrupt",
4120  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4121  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4122  "   --no-rowids              Do not attempt to recover rowid values",
4123  "                            that are not also INTEGER PRIMARY KEYs",
4124#endif
4125  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4126  ".save FILE               Write in-memory database into FILE",
4127  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4128  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4129  "   Options:",
4130  "      --indent             Try to pretty-print the schema",
4131  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4132  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4133  "    Options:",
4134  "       --init               Create a new SELFTEST table",
4135  "       -v                   Verbose output",
4136  ".separator COL ?ROW?     Change the column and row separators",
4137#if defined(SQLITE_ENABLE_SESSION)
4138  ".session ?NAME? CMD ...  Create or control sessions",
4139  "   Subcommands:",
4140  "     attach TABLE             Attach TABLE",
4141  "     changeset FILE           Write a changeset into FILE",
4142  "     close                    Close one session",
4143  "     enable ?BOOLEAN?         Set or query the enable bit",
4144  "     filter GLOB...           Reject tables matching GLOBs",
4145  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4146  "     isempty                  Query whether the session is empty",
4147  "     list                     List currently open session names",
4148  "     open DB NAME             Open a new session on DB",
4149  "     patchset FILE            Write a patchset into FILE",
4150  "   If ?NAME? is omitted, the first defined session is used.",
4151#endif
4152  ".sha3sum ...             Compute a SHA3 hash of database content",
4153  "    Options:",
4154  "      --schema              Also hash the sqlite_schema table",
4155  "      --sha3-224            Use the sha3-224 algorithm",
4156  "      --sha3-256            Use the sha3-256 algorithm (default)",
4157  "      --sha3-384            Use the sha3-384 algorithm",
4158  "      --sha3-512            Use the sha3-512 algorithm",
4159  "    Any other argument is a LIKE pattern for tables to hash",
4160#ifndef SQLITE_NOHAVE_SYSTEM
4161  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4162#endif
4163  ".show                    Show the current values for various settings",
4164  ".stats ?ARG?             Show stats or turn stats on or off",
4165  "   off                      Turn off automatic stat display",
4166  "   on                       Turn on automatic stat display",
4167  "   stmt                     Show statement stats",
4168  "   vmstep                   Show the virtual machine step count only",
4169#ifndef SQLITE_NOHAVE_SYSTEM
4170  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4171#endif
4172  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4173  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4174  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4175  "                           Run \".testctrl\" with no arguments for details",
4176  ".timeout MS              Try opening locked tables for MS milliseconds",
4177  ".timer on|off            Turn SQL timer on or off",
4178#ifndef SQLITE_OMIT_TRACE
4179  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4180  "    FILE                    Send output to FILE",
4181  "    stdout                  Send output to stdout",
4182  "    stderr                  Send output to stderr",
4183  "    off                     Disable tracing",
4184  "    --expanded              Expand query parameters",
4185#ifdef SQLITE_ENABLE_NORMALIZE
4186  "    --normalized            Normal the SQL statements",
4187#endif
4188  "    --plain                 Show SQL as it is input",
4189  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4190  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4191  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4192  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4193#endif /* SQLITE_OMIT_TRACE */
4194#ifdef SQLITE_DEBUG
4195  ".unmodule NAME ...       Unregister virtual table modules",
4196  "    --allexcept             Unregister everything except those named",
4197#endif
4198  ".vfsinfo ?AUX?           Information about the top-level VFS",
4199  ".vfslist                 List all available VFSes",
4200  ".vfsname ?AUX?           Print the name of the VFS stack",
4201  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4202  "     Negative values right-justify",
4203};
4204
4205/*
4206** Output help text.
4207**
4208** zPattern describes the set of commands for which help text is provided.
4209** If zPattern is NULL, then show all commands, but only give a one-line
4210** description of each.
4211**
4212** Return the number of matches.
4213*/
4214static int showHelp(FILE *out, const char *zPattern){
4215  int i = 0;
4216  int j = 0;
4217  int n = 0;
4218  char *zPat;
4219  if( zPattern==0
4220   || zPattern[0]=='0'
4221   || strcmp(zPattern,"-a")==0
4222   || strcmp(zPattern,"-all")==0
4223   || strcmp(zPattern,"--all")==0
4224  ){
4225    /* Show all commands, but only one line per command */
4226    if( zPattern==0 ) zPattern = "";
4227    for(i=0; i<ArraySize(azHelp); i++){
4228      if( azHelp[i][0]=='.' || zPattern[0] ){
4229        utf8_printf(out, "%s\n", azHelp[i]);
4230        n++;
4231      }
4232    }
4233  }else{
4234    /* Look for commands that for which zPattern is an exact prefix */
4235    zPat = sqlite3_mprintf(".%s*", zPattern);
4236    for(i=0; i<ArraySize(azHelp); i++){
4237      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4238        utf8_printf(out, "%s\n", azHelp[i]);
4239        j = i+1;
4240        n++;
4241      }
4242    }
4243    sqlite3_free(zPat);
4244    if( n ){
4245      if( n==1 ){
4246        /* when zPattern is a prefix of exactly one command, then include the
4247        ** details of that command, which should begin at offset j */
4248        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4249          utf8_printf(out, "%s\n", azHelp[j]);
4250          j++;
4251        }
4252      }
4253      return n;
4254    }
4255    /* Look for commands that contain zPattern anywhere.  Show the complete
4256    ** text of all commands that match. */
4257    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4258    for(i=0; i<ArraySize(azHelp); i++){
4259      if( azHelp[i][0]=='.' ) j = i;
4260      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4261        utf8_printf(out, "%s\n", azHelp[j]);
4262        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4263          j++;
4264          utf8_printf(out, "%s\n", azHelp[j]);
4265        }
4266        i = j;
4267        n++;
4268      }
4269    }
4270    sqlite3_free(zPat);
4271  }
4272  return n;
4273}
4274
4275/* Forward reference */
4276static int process_input(ShellState *p);
4277
4278/*
4279** Read the content of file zName into memory obtained from sqlite3_malloc64()
4280** and return a pointer to the buffer. The caller is responsible for freeing
4281** the memory.
4282**
4283** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4284** read.
4285**
4286** For convenience, a nul-terminator byte is always appended to the data read
4287** from the file before the buffer is returned. This byte is not included in
4288** the final value of (*pnByte), if applicable.
4289**
4290** NULL is returned if any error is encountered. The final value of *pnByte
4291** is undefined in this case.
4292*/
4293static char *readFile(const char *zName, int *pnByte){
4294  FILE *in = fopen(zName, "rb");
4295  long nIn;
4296  size_t nRead;
4297  char *pBuf;
4298  if( in==0 ) return 0;
4299  fseek(in, 0, SEEK_END);
4300  nIn = ftell(in);
4301  rewind(in);
4302  pBuf = sqlite3_malloc64( nIn+1 );
4303  if( pBuf==0 ){ fclose(in); return 0; }
4304  nRead = fread(pBuf, nIn, 1, in);
4305  fclose(in);
4306  if( nRead!=1 ){
4307    sqlite3_free(pBuf);
4308    return 0;
4309  }
4310  pBuf[nIn] = 0;
4311  if( pnByte ) *pnByte = nIn;
4312  return pBuf;
4313}
4314
4315#if defined(SQLITE_ENABLE_SESSION)
4316/*
4317** Close a single OpenSession object and release all of its associated
4318** resources.
4319*/
4320static void session_close(OpenSession *pSession){
4321  int i;
4322  sqlite3session_delete(pSession->p);
4323  sqlite3_free(pSession->zName);
4324  for(i=0; i<pSession->nFilter; i++){
4325    sqlite3_free(pSession->azFilter[i]);
4326  }
4327  sqlite3_free(pSession->azFilter);
4328  memset(pSession, 0, sizeof(OpenSession));
4329}
4330#endif
4331
4332/*
4333** Close all OpenSession objects and release all associated resources.
4334*/
4335#if defined(SQLITE_ENABLE_SESSION)
4336static void session_close_all(ShellState *p, int i){
4337  int j;
4338  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
4339  for(j=0; j<pAuxDb->nSession; j++){
4340    session_close(&pAuxDb->aSession[j]);
4341  }
4342  pAuxDb->nSession = 0;
4343}
4344#else
4345# define session_close_all(X,Y)
4346#endif
4347
4348/*
4349** Implementation of the xFilter function for an open session.  Omit
4350** any tables named by ".session filter" but let all other table through.
4351*/
4352#if defined(SQLITE_ENABLE_SESSION)
4353static int session_filter(void *pCtx, const char *zTab){
4354  OpenSession *pSession = (OpenSession*)pCtx;
4355  int i;
4356  for(i=0; i<pSession->nFilter; i++){
4357    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4358  }
4359  return 1;
4360}
4361#endif
4362
4363/*
4364** Try to deduce the type of file for zName based on its content.  Return
4365** one of the SHELL_OPEN_* constants.
4366**
4367** If the file does not exist or is empty but its name looks like a ZIP
4368** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4369** Otherwise, assume an ordinary database regardless of the filename if
4370** the type cannot be determined from content.
4371*/
4372int deduceDatabaseType(const char *zName, int dfltZip){
4373  FILE *f = fopen(zName, "rb");
4374  size_t n;
4375  int rc = SHELL_OPEN_UNSPEC;
4376  char zBuf[100];
4377  if( f==0 ){
4378    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4379       return SHELL_OPEN_ZIPFILE;
4380    }else{
4381       return SHELL_OPEN_NORMAL;
4382    }
4383  }
4384  n = fread(zBuf, 16, 1, f);
4385  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4386    fclose(f);
4387    return SHELL_OPEN_NORMAL;
4388  }
4389  fseek(f, -25, SEEK_END);
4390  n = fread(zBuf, 25, 1, f);
4391  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4392    rc = SHELL_OPEN_APPENDVFS;
4393  }else{
4394    fseek(f, -22, SEEK_END);
4395    n = fread(zBuf, 22, 1, f);
4396    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4397       && zBuf[3]==0x06 ){
4398      rc = SHELL_OPEN_ZIPFILE;
4399    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4400      rc = SHELL_OPEN_ZIPFILE;
4401    }
4402  }
4403  fclose(f);
4404  return rc;
4405}
4406
4407#ifndef SQLITE_OMIT_DESERIALIZE
4408/*
4409** Reconstruct an in-memory database using the output from the "dbtotxt"
4410** program.  Read content from the file in p->aAuxDb[].zDbFilename.
4411** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
4412*/
4413static unsigned char *readHexDb(ShellState *p, int *pnData){
4414  unsigned char *a = 0;
4415  int nLine;
4416  int n = 0;
4417  int pgsz = 0;
4418  int iOffset = 0;
4419  int j, k;
4420  int rc;
4421  FILE *in;
4422  const char *zDbFilename = p->pAuxDb->zDbFilename;
4423  unsigned int x[16];
4424  char zLine[1000];
4425  if( zDbFilename ){
4426    in = fopen(zDbFilename, "r");
4427    if( in==0 ){
4428      utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
4429      return 0;
4430    }
4431    nLine = 0;
4432  }else{
4433    in = p->in;
4434    nLine = p->lineno;
4435    if( in==0 ) in = stdin;
4436  }
4437  *pnData = 0;
4438  nLine++;
4439  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4440  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4441  if( rc!=2 ) goto readHexDb_error;
4442  if( n<0 ) goto readHexDb_error;
4443  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4444  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4445  a = sqlite3_malloc( n ? n : 1 );
4446  if( a==0 ){
4447    utf8_printf(stderr, "Out of memory!\n");
4448    goto readHexDb_error;
4449  }
4450  memset(a, 0, n);
4451  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4452    utf8_printf(stderr, "invalid pagesize\n");
4453    goto readHexDb_error;
4454  }
4455  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4456    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4457    if( rc==2 ){
4458      iOffset = k;
4459      continue;
4460    }
4461    if( strncmp(zLine, "| end ", 6)==0 ){
4462      break;
4463    }
4464    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4465                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4466                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4467    if( rc==17 ){
4468      k = iOffset+j;
4469      if( k+16<=n && k>=0 ){
4470        int ii;
4471        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4472      }
4473    }
4474  }
4475  *pnData = n;
4476  if( in!=p->in ){
4477    fclose(in);
4478  }else{
4479    p->lineno = nLine;
4480  }
4481  return a;
4482
4483readHexDb_error:
4484  if( in!=p->in ){
4485    fclose(in);
4486  }else{
4487    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4488      nLine++;
4489      if(strncmp(zLine, "| end ", 6)==0 ) break;
4490    }
4491    p->lineno = nLine;
4492  }
4493  sqlite3_free(a);
4494  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4495  return 0;
4496}
4497#endif /* SQLITE_OMIT_DESERIALIZE */
4498
4499/*
4500** Scalar function "shell_int32". The first argument to this function
4501** must be a blob. The second a non-negative integer. This function
4502** reads and returns a 32-bit big-endian integer from byte
4503** offset (4*<arg2>) of the blob.
4504*/
4505static void shellInt32(
4506  sqlite3_context *context,
4507  int argc,
4508  sqlite3_value **argv
4509){
4510  const unsigned char *pBlob;
4511  int nBlob;
4512  int iInt;
4513
4514  UNUSED_PARAMETER(argc);
4515  nBlob = sqlite3_value_bytes(argv[0]);
4516  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4517  iInt = sqlite3_value_int(argv[1]);
4518
4519  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4520    const unsigned char *a = &pBlob[iInt*4];
4521    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4522                       + ((sqlite3_int64)a[1]<<16)
4523                       + ((sqlite3_int64)a[2]<< 8)
4524                       + ((sqlite3_int64)a[3]<< 0);
4525    sqlite3_result_int64(context, iVal);
4526  }
4527}
4528
4529/*
4530** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4531** using "..." with internal double-quote characters doubled.
4532*/
4533static void shellIdQuote(
4534  sqlite3_context *context,
4535  int argc,
4536  sqlite3_value **argv
4537){
4538  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4539  UNUSED_PARAMETER(argc);
4540  if( zName ){
4541    char *z = sqlite3_mprintf("\"%w\"", zName);
4542    sqlite3_result_text(context, z, -1, sqlite3_free);
4543  }
4544}
4545
4546/*
4547** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4548*/
4549static void shellUSleepFunc(
4550  sqlite3_context *context,
4551  int argcUnused,
4552  sqlite3_value **argv
4553){
4554  int sleep = sqlite3_value_int(argv[0]);
4555  (void)argcUnused;
4556  sqlite3_sleep(sleep/1000);
4557  sqlite3_result_int(context, sleep);
4558}
4559
4560/*
4561** Scalar function "shell_escape_crnl" used by the .recover command.
4562** The argument passed to this function is the output of built-in
4563** function quote(). If the first character of the input is "'",
4564** indicating that the value passed to quote() was a text value,
4565** then this function searches the input for "\n" and "\r" characters
4566** and adds a wrapper similar to the following:
4567**
4568**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4569**
4570** Or, if the first character of the input is not "'", then a copy
4571** of the input is returned.
4572*/
4573static void shellEscapeCrnl(
4574  sqlite3_context *context,
4575  int argc,
4576  sqlite3_value **argv
4577){
4578  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4579  UNUSED_PARAMETER(argc);
4580  if( zText[0]=='\'' ){
4581    int nText = sqlite3_value_bytes(argv[0]);
4582    int i;
4583    char zBuf1[20];
4584    char zBuf2[20];
4585    const char *zNL = 0;
4586    const char *zCR = 0;
4587    int nCR = 0;
4588    int nNL = 0;
4589
4590    for(i=0; zText[i]; i++){
4591      if( zNL==0 && zText[i]=='\n' ){
4592        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4593        nNL = (int)strlen(zNL);
4594      }
4595      if( zCR==0 && zText[i]=='\r' ){
4596        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4597        nCR = (int)strlen(zCR);
4598      }
4599    }
4600
4601    if( zNL || zCR ){
4602      int iOut = 0;
4603      i64 nMax = (nNL > nCR) ? nNL : nCR;
4604      i64 nAlloc = nMax * nText + (nMax+64)*2;
4605      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4606      if( zOut==0 ){
4607        sqlite3_result_error_nomem(context);
4608        return;
4609      }
4610
4611      if( zNL && zCR ){
4612        memcpy(&zOut[iOut], "replace(replace(", 16);
4613        iOut += 16;
4614      }else{
4615        memcpy(&zOut[iOut], "replace(", 8);
4616        iOut += 8;
4617      }
4618      for(i=0; zText[i]; i++){
4619        if( zText[i]=='\n' ){
4620          memcpy(&zOut[iOut], zNL, nNL);
4621          iOut += nNL;
4622        }else if( zText[i]=='\r' ){
4623          memcpy(&zOut[iOut], zCR, nCR);
4624          iOut += nCR;
4625        }else{
4626          zOut[iOut] = zText[i];
4627          iOut++;
4628        }
4629      }
4630
4631      if( zNL ){
4632        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4633        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4634        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4635      }
4636      if( zCR ){
4637        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4638        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4639        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4640      }
4641
4642      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4643      sqlite3_free(zOut);
4644      return;
4645    }
4646  }
4647
4648  sqlite3_result_value(context, argv[0]);
4649}
4650
4651/* Flags for open_db().
4652**
4653** The default behavior of open_db() is to exit(1) if the database fails to
4654** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4655** but still returns without calling exit.
4656**
4657** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4658** ZIP archive if the file does not exist or is empty and its name matches
4659** the *.zip pattern.
4660*/
4661#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4662#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4663
4664/*
4665** Make sure the database is open.  If it is not, then open it.  If
4666** the database fails to open, print an error message and exit.
4667*/
4668static void open_db(ShellState *p, int openFlags){
4669  if( p->db==0 ){
4670    const char *zDbFilename = p->pAuxDb->zDbFilename;
4671    if( p->openMode==SHELL_OPEN_UNSPEC ){
4672      if( zDbFilename==0 || zDbFilename[0]==0 ){
4673        p->openMode = SHELL_OPEN_NORMAL;
4674      }else{
4675        p->openMode = (u8)deduceDatabaseType(zDbFilename,
4676                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4677      }
4678    }
4679    switch( p->openMode ){
4680      case SHELL_OPEN_APPENDVFS: {
4681        sqlite3_open_v2(zDbFilename, &p->db,
4682           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4683        break;
4684      }
4685      case SHELL_OPEN_HEXDB:
4686      case SHELL_OPEN_DESERIALIZE: {
4687        sqlite3_open(0, &p->db);
4688        break;
4689      }
4690      case SHELL_OPEN_ZIPFILE: {
4691        sqlite3_open(":memory:", &p->db);
4692        break;
4693      }
4694      case SHELL_OPEN_READONLY: {
4695        sqlite3_open_v2(zDbFilename, &p->db,
4696            SQLITE_OPEN_READONLY|p->openFlags, 0);
4697        break;
4698      }
4699      case SHELL_OPEN_UNSPEC:
4700      case SHELL_OPEN_NORMAL: {
4701        sqlite3_open_v2(zDbFilename, &p->db,
4702           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4703        break;
4704      }
4705    }
4706    globalDb = p->db;
4707    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4708      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4709          zDbFilename, sqlite3_errmsg(p->db));
4710      if( openFlags & OPEN_DB_KEEPALIVE ){
4711        sqlite3_open(":memory:", &p->db);
4712        return;
4713      }
4714      exit(1);
4715    }
4716#ifndef SQLITE_OMIT_LOAD_EXTENSION
4717    sqlite3_enable_load_extension(p->db, 1);
4718#endif
4719    sqlite3_fileio_init(p->db, 0, 0);
4720    sqlite3_shathree_init(p->db, 0, 0);
4721    sqlite3_completion_init(p->db, 0, 0);
4722    sqlite3_uint_init(p->db, 0, 0);
4723    sqlite3_decimal_init(p->db, 0, 0);
4724    sqlite3_regexp_init(p->db, 0, 0);
4725    sqlite3_ieee_init(p->db, 0, 0);
4726    sqlite3_series_init(p->db, 0, 0);
4727#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4728    sqlite3_dbdata_init(p->db, 0, 0);
4729#endif
4730#ifdef SQLITE_HAVE_ZLIB
4731    sqlite3_zipfile_init(p->db, 0, 0);
4732    sqlite3_sqlar_init(p->db, 0, 0);
4733#endif
4734    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4735                            shellAddSchemaName, 0, 0);
4736    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4737                            shellModuleSchema, 0, 0);
4738    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4739                            shellPutsFunc, 0, 0);
4740    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4741                            shellEscapeCrnl, 0, 0);
4742    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4743                            shellInt32, 0, 0);
4744    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4745                            shellIdQuote, 0, 0);
4746    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
4747                            shellUSleepFunc, 0, 0);
4748#ifndef SQLITE_NOHAVE_SYSTEM
4749    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4750                            editFunc, 0, 0);
4751    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4752                            editFunc, 0, 0);
4753#endif
4754    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4755      char *zSql = sqlite3_mprintf(
4756         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
4757      sqlite3_exec(p->db, zSql, 0, 0, 0);
4758      sqlite3_free(zSql);
4759    }
4760#ifndef SQLITE_OMIT_DESERIALIZE
4761    else
4762    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4763      int rc;
4764      int nData = 0;
4765      unsigned char *aData;
4766      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4767        aData = (unsigned char*)readFile(zDbFilename, &nData);
4768      }else{
4769        aData = readHexDb(p, &nData);
4770        if( aData==0 ){
4771          return;
4772        }
4773      }
4774      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4775                   SQLITE_DESERIALIZE_RESIZEABLE |
4776                   SQLITE_DESERIALIZE_FREEONCLOSE);
4777      if( rc ){
4778        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4779      }
4780      if( p->szMax>0 ){
4781        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4782      }
4783    }
4784#endif
4785  }
4786  if( p->bSafeModePersist && p->db!=0 ){
4787    sqlite3_set_authorizer(p->db, safeModeAuth, p);
4788  }
4789}
4790
4791/*
4792** Attempt to close the databaes connection.  Report errors.
4793*/
4794void close_db(sqlite3 *db){
4795  int rc = sqlite3_close(db);
4796  if( rc ){
4797    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4798        rc, sqlite3_errmsg(db));
4799  }
4800}
4801
4802#if HAVE_READLINE || HAVE_EDITLINE
4803/*
4804** Readline completion callbacks
4805*/
4806static char *readline_completion_generator(const char *text, int state){
4807  static sqlite3_stmt *pStmt = 0;
4808  char *zRet;
4809  if( state==0 ){
4810    char *zSql;
4811    sqlite3_finalize(pStmt);
4812    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4813                           "  FROM completion(%Q) ORDER BY 1", text);
4814    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4815    sqlite3_free(zSql);
4816  }
4817  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4818    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4819  }else{
4820    sqlite3_finalize(pStmt);
4821    pStmt = 0;
4822    zRet = 0;
4823  }
4824  return zRet;
4825}
4826static char **readline_completion(const char *zText, int iStart, int iEnd){
4827  rl_attempted_completion_over = 1;
4828  return rl_completion_matches(zText, readline_completion_generator);
4829}
4830
4831#elif HAVE_LINENOISE
4832/*
4833** Linenoise completion callback
4834*/
4835static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4836  int nLine = strlen30(zLine);
4837  int i, iStart;
4838  sqlite3_stmt *pStmt = 0;
4839  char *zSql;
4840  char zBuf[1000];
4841
4842  if( nLine>sizeof(zBuf)-30 ) return;
4843  if( zLine[0]=='.' || zLine[0]=='#') return;
4844  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4845  if( i==nLine-1 ) return;
4846  iStart = i+1;
4847  memcpy(zBuf, zLine, iStart);
4848  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4849                         "  FROM completion(%Q,%Q) ORDER BY 1",
4850                         &zLine[iStart], zLine);
4851  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4852  sqlite3_free(zSql);
4853  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4854  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4855    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4856    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4857    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4858      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4859      linenoiseAddCompletion(lc, zBuf);
4860    }
4861  }
4862  sqlite3_finalize(pStmt);
4863}
4864#endif
4865
4866/*
4867** Do C-language style dequoting.
4868**
4869**    \a    -> alarm
4870**    \b    -> backspace
4871**    \t    -> tab
4872**    \n    -> newline
4873**    \v    -> vertical tab
4874**    \f    -> form feed
4875**    \r    -> carriage return
4876**    \s    -> space
4877**    \"    -> "
4878**    \'    -> '
4879**    \\    -> backslash
4880**    \NNN  -> ascii character NNN in octal
4881*/
4882static void resolve_backslashes(char *z){
4883  int i, j;
4884  char c;
4885  while( *z && *z!='\\' ) z++;
4886  for(i=j=0; (c = z[i])!=0; i++, j++){
4887    if( c=='\\' && z[i+1]!=0 ){
4888      c = z[++i];
4889      if( c=='a' ){
4890        c = '\a';
4891      }else if( c=='b' ){
4892        c = '\b';
4893      }else if( c=='t' ){
4894        c = '\t';
4895      }else if( c=='n' ){
4896        c = '\n';
4897      }else if( c=='v' ){
4898        c = '\v';
4899      }else if( c=='f' ){
4900        c = '\f';
4901      }else if( c=='r' ){
4902        c = '\r';
4903      }else if( c=='"' ){
4904        c = '"';
4905      }else if( c=='\'' ){
4906        c = '\'';
4907      }else if( c=='\\' ){
4908        c = '\\';
4909      }else if( c>='0' && c<='7' ){
4910        c -= '0';
4911        if( z[i+1]>='0' && z[i+1]<='7' ){
4912          i++;
4913          c = (c<<3) + z[i] - '0';
4914          if( z[i+1]>='0' && z[i+1]<='7' ){
4915            i++;
4916            c = (c<<3) + z[i] - '0';
4917          }
4918        }
4919      }
4920    }
4921    z[j] = c;
4922  }
4923  if( j<i ) z[j] = 0;
4924}
4925
4926/*
4927** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4928** for TRUE and FALSE.  Return the integer value if appropriate.
4929*/
4930static int booleanValue(const char *zArg){
4931  int i;
4932  if( zArg[0]=='0' && zArg[1]=='x' ){
4933    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4934  }else{
4935    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4936  }
4937  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4938  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4939    return 1;
4940  }
4941  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4942    return 0;
4943  }
4944  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4945          zArg);
4946  return 0;
4947}
4948
4949/*
4950** Set or clear a shell flag according to a boolean value.
4951*/
4952static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4953  if( booleanValue(zArg) ){
4954    ShellSetFlag(p, mFlag);
4955  }else{
4956    ShellClearFlag(p, mFlag);
4957  }
4958}
4959
4960/*
4961** Close an output file, assuming it is not stderr or stdout
4962*/
4963static void output_file_close(FILE *f){
4964  if( f && f!=stdout && f!=stderr ) fclose(f);
4965}
4966
4967/*
4968** Try to open an output file.   The names "stdout" and "stderr" are
4969** recognized and do the right thing.  NULL is returned if the output
4970** filename is "off".
4971*/
4972static FILE *output_file_open(const char *zFile, int bTextMode){
4973  FILE *f;
4974  if( strcmp(zFile,"stdout")==0 ){
4975    f = stdout;
4976  }else if( strcmp(zFile, "stderr")==0 ){
4977    f = stderr;
4978  }else if( strcmp(zFile, "off")==0 ){
4979    f = 0;
4980  }else{
4981    f = fopen(zFile, bTextMode ? "w" : "wb");
4982    if( f==0 ){
4983      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4984    }
4985  }
4986  return f;
4987}
4988
4989#ifndef SQLITE_OMIT_TRACE
4990/*
4991** A routine for handling output from sqlite3_trace().
4992*/
4993static int sql_trace_callback(
4994  unsigned mType,         /* The trace type */
4995  void *pArg,             /* The ShellState pointer */
4996  void *pP,               /* Usually a pointer to sqlite_stmt */
4997  void *pX                /* Auxiliary output */
4998){
4999  ShellState *p = (ShellState*)pArg;
5000  sqlite3_stmt *pStmt;
5001  const char *zSql;
5002  int nSql;
5003  if( p->traceOut==0 ) return 0;
5004  if( mType==SQLITE_TRACE_CLOSE ){
5005    utf8_printf(p->traceOut, "-- closing database connection\n");
5006    return 0;
5007  }
5008  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
5009    zSql = (const char*)pX;
5010  }else{
5011    pStmt = (sqlite3_stmt*)pP;
5012    switch( p->eTraceType ){
5013      case SHELL_TRACE_EXPANDED: {
5014        zSql = sqlite3_expanded_sql(pStmt);
5015        break;
5016      }
5017#ifdef SQLITE_ENABLE_NORMALIZE
5018      case SHELL_TRACE_NORMALIZED: {
5019        zSql = sqlite3_normalized_sql(pStmt);
5020        break;
5021      }
5022#endif
5023      default: {
5024        zSql = sqlite3_sql(pStmt);
5025        break;
5026      }
5027    }
5028  }
5029  if( zSql==0 ) return 0;
5030  nSql = strlen30(zSql);
5031  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5032  switch( mType ){
5033    case SQLITE_TRACE_ROW:
5034    case SQLITE_TRACE_STMT: {
5035      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
5036      break;
5037    }
5038    case SQLITE_TRACE_PROFILE: {
5039      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
5040      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
5041      break;
5042    }
5043  }
5044  return 0;
5045}
5046#endif
5047
5048/*
5049** A no-op routine that runs with the ".breakpoint" doc-command.  This is
5050** a useful spot to set a debugger breakpoint.
5051*/
5052static void test_breakpoint(void){
5053  static int nCall = 0;
5054  nCall++;
5055}
5056
5057/*
5058** An object used to read a CSV and other files for import.
5059*/
5060typedef struct ImportCtx ImportCtx;
5061struct ImportCtx {
5062  const char *zFile;  /* Name of the input file */
5063  FILE *in;           /* Read the CSV text from this input stream */
5064  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
5065  char *z;            /* Accumulated text for a field */
5066  int n;              /* Number of bytes in z */
5067  int nAlloc;         /* Space allocated for z[] */
5068  int nLine;          /* Current line number */
5069  int nRow;           /* Number of rows imported */
5070  int nErr;           /* Number of errors encountered */
5071  int bNotFirst;      /* True if one or more bytes already read */
5072  int cTerm;          /* Character that terminated the most recent field */
5073  int cColSep;        /* The column separator character.  (Usually ",") */
5074  int cRowSep;        /* The row separator character.  (Usually "\n") */
5075};
5076
5077/* Clean up resourced used by an ImportCtx */
5078static void import_cleanup(ImportCtx *p){
5079  if( p->in!=0 && p->xCloser!=0 ){
5080    p->xCloser(p->in);
5081    p->in = 0;
5082  }
5083  sqlite3_free(p->z);
5084  p->z = 0;
5085}
5086
5087/* Append a single byte to z[] */
5088static void import_append_char(ImportCtx *p, int c){
5089  if( p->n+1>=p->nAlloc ){
5090    p->nAlloc += p->nAlloc + 100;
5091    p->z = sqlite3_realloc64(p->z, p->nAlloc);
5092    if( p->z==0 ) shell_out_of_memory();
5093  }
5094  p->z[p->n++] = (char)c;
5095}
5096
5097/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5098** with the option of having a separator other than ",".
5099**
5100**   +  Input comes from p->in.
5101**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5102**      from sqlite3_malloc64().
5103**   +  Use p->cSep as the column separator.  The default is ",".
5104**   +  Use p->rSep as the row separator.  The default is "\n".
5105**   +  Keep track of the line number in p->nLine.
5106**   +  Store the character that terminates the field in p->cTerm.  Store
5107**      EOF on end-of-file.
5108**   +  Report syntax errors on stderr
5109*/
5110static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5111  int c;
5112  int cSep = p->cColSep;
5113  int rSep = p->cRowSep;
5114  p->n = 0;
5115  c = fgetc(p->in);
5116  if( c==EOF || seenInterrupt ){
5117    p->cTerm = EOF;
5118    return 0;
5119  }
5120  if( c=='"' ){
5121    int pc, ppc;
5122    int startLine = p->nLine;
5123    int cQuote = c;
5124    pc = ppc = 0;
5125    while( 1 ){
5126      c = fgetc(p->in);
5127      if( c==rSep ) p->nLine++;
5128      if( c==cQuote ){
5129        if( pc==cQuote ){
5130          pc = 0;
5131          continue;
5132        }
5133      }
5134      if( (c==cSep && pc==cQuote)
5135       || (c==rSep && pc==cQuote)
5136       || (c==rSep && pc=='\r' && ppc==cQuote)
5137       || (c==EOF && pc==cQuote)
5138      ){
5139        do{ p->n--; }while( p->z[p->n]!=cQuote );
5140        p->cTerm = c;
5141        break;
5142      }
5143      if( pc==cQuote && c!='\r' ){
5144        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5145                p->zFile, p->nLine, cQuote);
5146      }
5147      if( c==EOF ){
5148        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5149                p->zFile, startLine, cQuote);
5150        p->cTerm = c;
5151        break;
5152      }
5153      import_append_char(p, c);
5154      ppc = pc;
5155      pc = c;
5156    }
5157  }else{
5158    /* If this is the first field being parsed and it begins with the
5159    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5160    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5161      import_append_char(p, c);
5162      c = fgetc(p->in);
5163      if( (c&0xff)==0xbb ){
5164        import_append_char(p, c);
5165        c = fgetc(p->in);
5166        if( (c&0xff)==0xbf ){
5167          p->bNotFirst = 1;
5168          p->n = 0;
5169          return csv_read_one_field(p);
5170        }
5171      }
5172    }
5173    while( c!=EOF && c!=cSep && c!=rSep ){
5174      import_append_char(p, c);
5175      c = fgetc(p->in);
5176    }
5177    if( c==rSep ){
5178      p->nLine++;
5179      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5180    }
5181    p->cTerm = c;
5182  }
5183  if( p->z ) p->z[p->n] = 0;
5184  p->bNotFirst = 1;
5185  return p->z;
5186}
5187
5188/* Read a single field of ASCII delimited text.
5189**
5190**   +  Input comes from p->in.
5191**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5192**      from sqlite3_malloc64().
5193**   +  Use p->cSep as the column separator.  The default is "\x1F".
5194**   +  Use p->rSep as the row separator.  The default is "\x1E".
5195**   +  Keep track of the row number in p->nLine.
5196**   +  Store the character that terminates the field in p->cTerm.  Store
5197**      EOF on end-of-file.
5198**   +  Report syntax errors on stderr
5199*/
5200static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5201  int c;
5202  int cSep = p->cColSep;
5203  int rSep = p->cRowSep;
5204  p->n = 0;
5205  c = fgetc(p->in);
5206  if( c==EOF || seenInterrupt ){
5207    p->cTerm = EOF;
5208    return 0;
5209  }
5210  while( c!=EOF && c!=cSep && c!=rSep ){
5211    import_append_char(p, c);
5212    c = fgetc(p->in);
5213  }
5214  if( c==rSep ){
5215    p->nLine++;
5216  }
5217  p->cTerm = c;
5218  if( p->z ) p->z[p->n] = 0;
5219  return p->z;
5220}
5221
5222/*
5223** Try to transfer data for table zTable.  If an error is seen while
5224** moving forward, try to go backwards.  The backwards movement won't
5225** work for WITHOUT ROWID tables.
5226*/
5227static void tryToCloneData(
5228  ShellState *p,
5229  sqlite3 *newDb,
5230  const char *zTable
5231){
5232  sqlite3_stmt *pQuery = 0;
5233  sqlite3_stmt *pInsert = 0;
5234  char *zQuery = 0;
5235  char *zInsert = 0;
5236  int rc;
5237  int i, j, n;
5238  int nTable = strlen30(zTable);
5239  int k = 0;
5240  int cnt = 0;
5241  const int spinRate = 10000;
5242
5243  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5244  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5245  if( rc ){
5246    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5247            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5248            zQuery);
5249    goto end_data_xfer;
5250  }
5251  n = sqlite3_column_count(pQuery);
5252  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5253  if( zInsert==0 ) shell_out_of_memory();
5254  sqlite3_snprintf(200+nTable,zInsert,
5255                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5256  i = strlen30(zInsert);
5257  for(j=1; j<n; j++){
5258    memcpy(zInsert+i, ",?", 2);
5259    i += 2;
5260  }
5261  memcpy(zInsert+i, ");", 3);
5262  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5263  if( rc ){
5264    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5265            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5266            zQuery);
5267    goto end_data_xfer;
5268  }
5269  for(k=0; k<2; k++){
5270    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5271      for(i=0; i<n; i++){
5272        switch( sqlite3_column_type(pQuery, i) ){
5273          case SQLITE_NULL: {
5274            sqlite3_bind_null(pInsert, i+1);
5275            break;
5276          }
5277          case SQLITE_INTEGER: {
5278            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5279            break;
5280          }
5281          case SQLITE_FLOAT: {
5282            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5283            break;
5284          }
5285          case SQLITE_TEXT: {
5286            sqlite3_bind_text(pInsert, i+1,
5287                             (const char*)sqlite3_column_text(pQuery,i),
5288                             -1, SQLITE_STATIC);
5289            break;
5290          }
5291          case SQLITE_BLOB: {
5292            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5293                                            sqlite3_column_bytes(pQuery,i),
5294                                            SQLITE_STATIC);
5295            break;
5296          }
5297        }
5298      } /* End for */
5299      rc = sqlite3_step(pInsert);
5300      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5301        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5302                        sqlite3_errmsg(newDb));
5303      }
5304      sqlite3_reset(pInsert);
5305      cnt++;
5306      if( (cnt%spinRate)==0 ){
5307        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5308        fflush(stdout);
5309      }
5310    } /* End while */
5311    if( rc==SQLITE_DONE ) break;
5312    sqlite3_finalize(pQuery);
5313    sqlite3_free(zQuery);
5314    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5315                             zTable);
5316    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5317    if( rc ){
5318      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5319      break;
5320    }
5321  } /* End for(k=0...) */
5322
5323end_data_xfer:
5324  sqlite3_finalize(pQuery);
5325  sqlite3_finalize(pInsert);
5326  sqlite3_free(zQuery);
5327  sqlite3_free(zInsert);
5328}
5329
5330
5331/*
5332** Try to transfer all rows of the schema that match zWhere.  For
5333** each row, invoke xForEach() on the object defined by that row.
5334** If an error is encountered while moving forward through the
5335** sqlite_schema table, try again moving backwards.
5336*/
5337static void tryToCloneSchema(
5338  ShellState *p,
5339  sqlite3 *newDb,
5340  const char *zWhere,
5341  void (*xForEach)(ShellState*,sqlite3*,const char*)
5342){
5343  sqlite3_stmt *pQuery = 0;
5344  char *zQuery = 0;
5345  int rc;
5346  const unsigned char *zName;
5347  const unsigned char *zSql;
5348  char *zErrMsg = 0;
5349
5350  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5351                           " WHERE %s", zWhere);
5352  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5353  if( rc ){
5354    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5355                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5356                    zQuery);
5357    goto end_schema_xfer;
5358  }
5359  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5360    zName = sqlite3_column_text(pQuery, 0);
5361    zSql = sqlite3_column_text(pQuery, 1);
5362    printf("%s... ", zName); fflush(stdout);
5363    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5364    if( zErrMsg ){
5365      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5366      sqlite3_free(zErrMsg);
5367      zErrMsg = 0;
5368    }
5369    if( xForEach ){
5370      xForEach(p, newDb, (const char*)zName);
5371    }
5372    printf("done\n");
5373  }
5374  if( rc!=SQLITE_DONE ){
5375    sqlite3_finalize(pQuery);
5376    sqlite3_free(zQuery);
5377    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5378                             " WHERE %s ORDER BY rowid DESC", zWhere);
5379    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5380    if( rc ){
5381      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5382                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5383                      zQuery);
5384      goto end_schema_xfer;
5385    }
5386    while( sqlite3_step(pQuery)==SQLITE_ROW ){
5387      zName = sqlite3_column_text(pQuery, 0);
5388      zSql = sqlite3_column_text(pQuery, 1);
5389      printf("%s... ", zName); fflush(stdout);
5390      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5391      if( zErrMsg ){
5392        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5393        sqlite3_free(zErrMsg);
5394        zErrMsg = 0;
5395      }
5396      if( xForEach ){
5397        xForEach(p, newDb, (const char*)zName);
5398      }
5399      printf("done\n");
5400    }
5401  }
5402end_schema_xfer:
5403  sqlite3_finalize(pQuery);
5404  sqlite3_free(zQuery);
5405}
5406
5407/*
5408** Open a new database file named "zNewDb".  Try to recover as much information
5409** as possible out of the main database (which might be corrupt) and write it
5410** into zNewDb.
5411*/
5412static void tryToClone(ShellState *p, const char *zNewDb){
5413  int rc;
5414  sqlite3 *newDb = 0;
5415  if( access(zNewDb,0)==0 ){
5416    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5417    return;
5418  }
5419  rc = sqlite3_open(zNewDb, &newDb);
5420  if( rc ){
5421    utf8_printf(stderr, "Cannot create output database: %s\n",
5422            sqlite3_errmsg(newDb));
5423  }else{
5424    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5425    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5426    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5427    tryToCloneSchema(p, newDb, "type!='table'", 0);
5428    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5429    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5430  }
5431  close_db(newDb);
5432}
5433
5434/*
5435** Change the output file back to stdout.
5436**
5437** If the p->doXdgOpen flag is set, that means the output was being
5438** redirected to a temporary file named by p->zTempFile.  In that case,
5439** launch start/open/xdg-open on that temporary file.
5440*/
5441static void output_reset(ShellState *p){
5442  if( p->outfile[0]=='|' ){
5443#ifndef SQLITE_OMIT_POPEN
5444    pclose(p->out);
5445#endif
5446  }else{
5447    output_file_close(p->out);
5448#ifndef SQLITE_NOHAVE_SYSTEM
5449    if( p->doXdgOpen ){
5450      const char *zXdgOpenCmd =
5451#if defined(_WIN32)
5452      "start";
5453#elif defined(__APPLE__)
5454      "open";
5455#else
5456      "xdg-open";
5457#endif
5458      char *zCmd;
5459      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5460      if( system(zCmd) ){
5461        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5462      }else{
5463        /* Give the start/open/xdg-open command some time to get
5464        ** going before we continue, and potential delete the
5465        ** p->zTempFile data file out from under it */
5466        sqlite3_sleep(2000);
5467      }
5468      sqlite3_free(zCmd);
5469      outputModePop(p);
5470      p->doXdgOpen = 0;
5471    }
5472#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5473  }
5474  p->outfile[0] = 0;
5475  p->out = stdout;
5476}
5477
5478/*
5479** Run an SQL command and return the single integer result.
5480*/
5481static int db_int(ShellState *p, const char *zSql){
5482  sqlite3_stmt *pStmt;
5483  int res = 0;
5484  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5485  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5486    res = sqlite3_column_int(pStmt,0);
5487  }
5488  sqlite3_finalize(pStmt);
5489  return res;
5490}
5491
5492/*
5493** Convert a 2-byte or 4-byte big-endian integer into a native integer
5494*/
5495static unsigned int get2byteInt(unsigned char *a){
5496  return (a[0]<<8) + a[1];
5497}
5498static unsigned int get4byteInt(unsigned char *a){
5499  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5500}
5501
5502/*
5503** Implementation of the ".dbinfo" command.
5504**
5505** Return 1 on error, 2 to exit, and 0 otherwise.
5506*/
5507static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5508  static const struct { const char *zName; int ofst; } aField[] = {
5509     { "file change counter:",  24  },
5510     { "database page count:",  28  },
5511     { "freelist page count:",  36  },
5512     { "schema cookie:",        40  },
5513     { "schema format:",        44  },
5514     { "default cache size:",   48  },
5515     { "autovacuum top root:",  52  },
5516     { "incremental vacuum:",   64  },
5517     { "text encoding:",        56  },
5518     { "user version:",         60  },
5519     { "application id:",       68  },
5520     { "software version:",     96  },
5521  };
5522  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5523     { "number of tables:",
5524       "SELECT count(*) FROM %s WHERE type='table'" },
5525     { "number of indexes:",
5526       "SELECT count(*) FROM %s WHERE type='index'" },
5527     { "number of triggers:",
5528       "SELECT count(*) FROM %s WHERE type='trigger'" },
5529     { "number of views:",
5530       "SELECT count(*) FROM %s WHERE type='view'" },
5531     { "schema size:",
5532       "SELECT total(length(sql)) FROM %s" },
5533  };
5534  int i, rc;
5535  unsigned iDataVersion;
5536  char *zSchemaTab;
5537  char *zDb = nArg>=2 ? azArg[1] : "main";
5538  sqlite3_stmt *pStmt = 0;
5539  unsigned char aHdr[100];
5540  open_db(p, 0);
5541  if( p->db==0 ) return 1;
5542  rc = sqlite3_prepare_v2(p->db,
5543             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5544             -1, &pStmt, 0);
5545  if( rc ){
5546    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5547    sqlite3_finalize(pStmt);
5548    return 1;
5549  }
5550  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5551  if( sqlite3_step(pStmt)==SQLITE_ROW
5552   && sqlite3_column_bytes(pStmt,0)>100
5553  ){
5554    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5555    sqlite3_finalize(pStmt);
5556  }else{
5557    raw_printf(stderr, "unable to read database header\n");
5558    sqlite3_finalize(pStmt);
5559    return 1;
5560  }
5561  i = get2byteInt(aHdr+16);
5562  if( i==1 ) i = 65536;
5563  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5564  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5565  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5566  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5567  for(i=0; i<ArraySize(aField); i++){
5568    int ofst = aField[i].ofst;
5569    unsigned int val = get4byteInt(aHdr + ofst);
5570    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5571    switch( ofst ){
5572      case 56: {
5573        if( val==1 ) raw_printf(p->out, " (utf8)");
5574        if( val==2 ) raw_printf(p->out, " (utf16le)");
5575        if( val==3 ) raw_printf(p->out, " (utf16be)");
5576      }
5577    }
5578    raw_printf(p->out, "\n");
5579  }
5580  if( zDb==0 ){
5581    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5582  }else if( strcmp(zDb,"temp")==0 ){
5583    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5584  }else{
5585    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5586  }
5587  for(i=0; i<ArraySize(aQuery); i++){
5588    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5589    int val = db_int(p, zSql);
5590    sqlite3_free(zSql);
5591    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5592  }
5593  sqlite3_free(zSchemaTab);
5594  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5595  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5596  return 0;
5597}
5598
5599/*
5600** Print the current sqlite3_errmsg() value to stderr and return 1.
5601*/
5602static int shellDatabaseError(sqlite3 *db){
5603  const char *zErr = sqlite3_errmsg(db);
5604  utf8_printf(stderr, "Error: %s\n", zErr);
5605  return 1;
5606}
5607
5608/*
5609** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5610** if they match and FALSE (0) if they do not match.
5611**
5612** Globbing rules:
5613**
5614**      '*'       Matches any sequence of zero or more characters.
5615**
5616**      '?'       Matches exactly one character.
5617**
5618**     [...]      Matches one character from the enclosed list of
5619**                characters.
5620**
5621**     [^...]     Matches one character not in the enclosed list.
5622**
5623**      '#'       Matches any sequence of one or more digits with an
5624**                optional + or - sign in front
5625**
5626**      ' '       Any span of whitespace matches any other span of
5627**                whitespace.
5628**
5629** Extra whitespace at the end of z[] is ignored.
5630*/
5631static int testcase_glob(const char *zGlob, const char *z){
5632  int c, c2;
5633  int invert;
5634  int seen;
5635
5636  while( (c = (*(zGlob++)))!=0 ){
5637    if( IsSpace(c) ){
5638      if( !IsSpace(*z) ) return 0;
5639      while( IsSpace(*zGlob) ) zGlob++;
5640      while( IsSpace(*z) ) z++;
5641    }else if( c=='*' ){
5642      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5643        if( c=='?' && (*(z++))==0 ) return 0;
5644      }
5645      if( c==0 ){
5646        return 1;
5647      }else if( c=='[' ){
5648        while( *z && testcase_glob(zGlob-1,z)==0 ){
5649          z++;
5650        }
5651        return (*z)!=0;
5652      }
5653      while( (c2 = (*(z++)))!=0 ){
5654        while( c2!=c ){
5655          c2 = *(z++);
5656          if( c2==0 ) return 0;
5657        }
5658        if( testcase_glob(zGlob,z) ) return 1;
5659      }
5660      return 0;
5661    }else if( c=='?' ){
5662      if( (*(z++))==0 ) return 0;
5663    }else if( c=='[' ){
5664      int prior_c = 0;
5665      seen = 0;
5666      invert = 0;
5667      c = *(z++);
5668      if( c==0 ) return 0;
5669      c2 = *(zGlob++);
5670      if( c2=='^' ){
5671        invert = 1;
5672        c2 = *(zGlob++);
5673      }
5674      if( c2==']' ){
5675        if( c==']' ) seen = 1;
5676        c2 = *(zGlob++);
5677      }
5678      while( c2 && c2!=']' ){
5679        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5680          c2 = *(zGlob++);
5681          if( c>=prior_c && c<=c2 ) seen = 1;
5682          prior_c = 0;
5683        }else{
5684          if( c==c2 ){
5685            seen = 1;
5686          }
5687          prior_c = c2;
5688        }
5689        c2 = *(zGlob++);
5690      }
5691      if( c2==0 || (seen ^ invert)==0 ) return 0;
5692    }else if( c=='#' ){
5693      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5694      if( !IsDigit(z[0]) ) return 0;
5695      z++;
5696      while( IsDigit(z[0]) ){ z++; }
5697    }else{
5698      if( c!=(*(z++)) ) return 0;
5699    }
5700  }
5701  while( IsSpace(*z) ){ z++; }
5702  return *z==0;
5703}
5704
5705
5706/*
5707** Compare the string as a command-line option with either one or two
5708** initial "-" characters.
5709*/
5710static int optionMatch(const char *zStr, const char *zOpt){
5711  if( zStr[0]!='-' ) return 0;
5712  zStr++;
5713  if( zStr[0]=='-' ) zStr++;
5714  return strcmp(zStr, zOpt)==0;
5715}
5716
5717/*
5718** Delete a file.
5719*/
5720int shellDeleteFile(const char *zFilename){
5721  int rc;
5722#ifdef _WIN32
5723  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5724  rc = _wunlink(z);
5725  sqlite3_free(z);
5726#else
5727  rc = unlink(zFilename);
5728#endif
5729  return rc;
5730}
5731
5732/*
5733** Try to delete the temporary file (if there is one) and free the
5734** memory used to hold the name of the temp file.
5735*/
5736static void clearTempFile(ShellState *p){
5737  if( p->zTempFile==0 ) return;
5738  if( p->doXdgOpen ) return;
5739  if( shellDeleteFile(p->zTempFile) ) return;
5740  sqlite3_free(p->zTempFile);
5741  p->zTempFile = 0;
5742}
5743
5744/*
5745** Create a new temp file name with the given suffix.
5746*/
5747static void newTempFile(ShellState *p, const char *zSuffix){
5748  clearTempFile(p);
5749  sqlite3_free(p->zTempFile);
5750  p->zTempFile = 0;
5751  if( p->db ){
5752    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5753  }
5754  if( p->zTempFile==0 ){
5755    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5756    ** will not work and we will need to fallback to guessing */
5757    char *zTemp;
5758    sqlite3_uint64 r;
5759    sqlite3_randomness(sizeof(r), &r);
5760    zTemp = getenv("TEMP");
5761    if( zTemp==0 ) zTemp = getenv("TMP");
5762    if( zTemp==0 ){
5763#ifdef _WIN32
5764      zTemp = "\\tmp";
5765#else
5766      zTemp = "/tmp";
5767#endif
5768    }
5769    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5770  }else{
5771    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5772  }
5773  if( p->zTempFile==0 ){
5774    shell_out_of_memory();
5775  }
5776}
5777
5778
5779/*
5780** The implementation of SQL scalar function fkey_collate_clause(), used
5781** by the ".lint fkey-indexes" command. This scalar function is always
5782** called with four arguments - the parent table name, the parent column name,
5783** the child table name and the child column name.
5784**
5785**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5786**
5787** If either of the named tables or columns do not exist, this function
5788** returns an empty string. An empty string is also returned if both tables
5789** and columns exist but have the same default collation sequence. Or,
5790** if both exist but the default collation sequences are different, this
5791** function returns the string " COLLATE <parent-collation>", where
5792** <parent-collation> is the default collation sequence of the parent column.
5793*/
5794static void shellFkeyCollateClause(
5795  sqlite3_context *pCtx,
5796  int nVal,
5797  sqlite3_value **apVal
5798){
5799  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5800  const char *zParent;
5801  const char *zParentCol;
5802  const char *zParentSeq;
5803  const char *zChild;
5804  const char *zChildCol;
5805  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5806  int rc;
5807
5808  assert( nVal==4 );
5809  zParent = (const char*)sqlite3_value_text(apVal[0]);
5810  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5811  zChild = (const char*)sqlite3_value_text(apVal[2]);
5812  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5813
5814  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5815  rc = sqlite3_table_column_metadata(
5816      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5817  );
5818  if( rc==SQLITE_OK ){
5819    rc = sqlite3_table_column_metadata(
5820        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5821    );
5822  }
5823
5824  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5825    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5826    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5827    sqlite3_free(z);
5828  }
5829}
5830
5831
5832/*
5833** The implementation of dot-command ".lint fkey-indexes".
5834*/
5835static int lintFkeyIndexes(
5836  ShellState *pState,             /* Current shell tool state */
5837  char **azArg,                   /* Array of arguments passed to dot command */
5838  int nArg                        /* Number of entries in azArg[] */
5839){
5840  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5841  FILE *out = pState->out;        /* Stream to write non-error output to */
5842  int bVerbose = 0;               /* If -verbose is present */
5843  int bGroupByParent = 0;         /* If -groupbyparent is present */
5844  int i;                          /* To iterate through azArg[] */
5845  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5846  int rc;                         /* Return code */
5847  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5848
5849  /*
5850  ** This SELECT statement returns one row for each foreign key constraint
5851  ** in the schema of the main database. The column values are:
5852  **
5853  ** 0. The text of an SQL statement similar to:
5854  **
5855  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5856  **
5857  **    This SELECT is similar to the one that the foreign keys implementation
5858  **    needs to run internally on child tables. If there is an index that can
5859  **    be used to optimize this query, then it can also be used by the FK
5860  **    implementation to optimize DELETE or UPDATE statements on the parent
5861  **    table.
5862  **
5863  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5864  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5865  **    contains an index that can be used to optimize the query.
5866  **
5867  ** 2. Human readable text that describes the child table and columns. e.g.
5868  **
5869  **       "child_table(child_key1, child_key2)"
5870  **
5871  ** 3. Human readable text that describes the parent table and columns. e.g.
5872  **
5873  **       "parent_table(parent_key1, parent_key2)"
5874  **
5875  ** 4. A full CREATE INDEX statement for an index that could be used to
5876  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5877  **
5878  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5879  **
5880  ** 5. The name of the parent table.
5881  **
5882  ** These six values are used by the C logic below to generate the report.
5883  */
5884  const char *zSql =
5885  "SELECT "
5886    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5887    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5888    "  || fkey_collate_clause("
5889    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5890    ", "
5891    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
5892    "  || group_concat('*=?', ' AND ') || ')'"
5893    ", "
5894    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5895    ", "
5896    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5897    ", "
5898    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5899    "  || ' ON ' || quote(s.name) || '('"
5900    "  || group_concat(quote(f.[from]) ||"
5901    "        fkey_collate_clause("
5902    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5903    "  || ');'"
5904    ", "
5905    "     f.[table] "
5906    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5907    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5908    "GROUP BY s.name, f.id "
5909    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5910  ;
5911  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
5912
5913  for(i=2; i<nArg; i++){
5914    int n = strlen30(azArg[i]);
5915    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5916      bVerbose = 1;
5917    }
5918    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5919      bGroupByParent = 1;
5920      zIndent = "    ";
5921    }
5922    else{
5923      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5924          azArg[0], azArg[1]
5925      );
5926      return SQLITE_ERROR;
5927    }
5928  }
5929
5930  /* Register the fkey_collate_clause() SQL function */
5931  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5932      0, shellFkeyCollateClause, 0, 0
5933  );
5934
5935
5936  if( rc==SQLITE_OK ){
5937    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5938  }
5939  if( rc==SQLITE_OK ){
5940    sqlite3_bind_int(pSql, 1, bGroupByParent);
5941  }
5942
5943  if( rc==SQLITE_OK ){
5944    int rc2;
5945    char *zPrev = 0;
5946    while( SQLITE_ROW==sqlite3_step(pSql) ){
5947      int res = -1;
5948      sqlite3_stmt *pExplain = 0;
5949      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5950      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5951      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5952      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5953      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5954      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5955
5956      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5957      if( rc!=SQLITE_OK ) break;
5958      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5959        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5960        res = (
5961              0==sqlite3_strglob(zGlob, zPlan)
5962           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5963        );
5964      }
5965      rc = sqlite3_finalize(pExplain);
5966      if( rc!=SQLITE_OK ) break;
5967
5968      if( res<0 ){
5969        raw_printf(stderr, "Error: internal error");
5970        break;
5971      }else{
5972        if( bGroupByParent
5973        && (bVerbose || res==0)
5974        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5975        ){
5976          raw_printf(out, "-- Parent table %s\n", zParent);
5977          sqlite3_free(zPrev);
5978          zPrev = sqlite3_mprintf("%s", zParent);
5979        }
5980
5981        if( res==0 ){
5982          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5983        }else if( bVerbose ){
5984          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5985              zIndent, zFrom, zTarget
5986          );
5987        }
5988      }
5989    }
5990    sqlite3_free(zPrev);
5991
5992    if( rc!=SQLITE_OK ){
5993      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5994    }
5995
5996    rc2 = sqlite3_finalize(pSql);
5997    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5998      rc = rc2;
5999      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6000    }
6001  }else{
6002    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6003  }
6004
6005  return rc;
6006}
6007
6008/*
6009** Implementation of ".lint" dot command.
6010*/
6011static int lintDotCommand(
6012  ShellState *pState,             /* Current shell tool state */
6013  char **azArg,                   /* Array of arguments passed to dot command */
6014  int nArg                        /* Number of entries in azArg[] */
6015){
6016  int n;
6017  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6018  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6019  return lintFkeyIndexes(pState, azArg, nArg);
6020
6021 usage:
6022  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6023  raw_printf(stderr, "Where sub-commands are:\n");
6024  raw_printf(stderr, "    fkey-indexes\n");
6025  return SQLITE_ERROR;
6026}
6027
6028#if !defined SQLITE_OMIT_VIRTUALTABLE
6029static void shellPrepare(
6030  sqlite3 *db,
6031  int *pRc,
6032  const char *zSql,
6033  sqlite3_stmt **ppStmt
6034){
6035  *ppStmt = 0;
6036  if( *pRc==SQLITE_OK ){
6037    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6038    if( rc!=SQLITE_OK ){
6039      raw_printf(stderr, "sql error: %s (%d)\n",
6040          sqlite3_errmsg(db), sqlite3_errcode(db)
6041      );
6042      *pRc = rc;
6043    }
6044  }
6045}
6046
6047/*
6048** Create a prepared statement using printf-style arguments for the SQL.
6049**
6050** This routine is could be marked "static".  But it is not always used,
6051** depending on compile-time options.  By omitting the "static", we avoid
6052** nuisance compiler warnings about "defined but not used".
6053*/
6054void shellPreparePrintf(
6055  sqlite3 *db,
6056  int *pRc,
6057  sqlite3_stmt **ppStmt,
6058  const char *zFmt,
6059  ...
6060){
6061  *ppStmt = 0;
6062  if( *pRc==SQLITE_OK ){
6063    va_list ap;
6064    char *z;
6065    va_start(ap, zFmt);
6066    z = sqlite3_vmprintf(zFmt, ap);
6067    va_end(ap);
6068    if( z==0 ){
6069      *pRc = SQLITE_NOMEM;
6070    }else{
6071      shellPrepare(db, pRc, z, ppStmt);
6072      sqlite3_free(z);
6073    }
6074  }
6075}
6076
6077/* Finalize the prepared statement created using shellPreparePrintf().
6078**
6079** This routine is could be marked "static".  But it is not always used,
6080** depending on compile-time options.  By omitting the "static", we avoid
6081** nuisance compiler warnings about "defined but not used".
6082*/
6083void shellFinalize(
6084  int *pRc,
6085  sqlite3_stmt *pStmt
6086){
6087  if( pStmt ){
6088    sqlite3 *db = sqlite3_db_handle(pStmt);
6089    int rc = sqlite3_finalize(pStmt);
6090    if( *pRc==SQLITE_OK ){
6091      if( rc!=SQLITE_OK ){
6092        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6093      }
6094      *pRc = rc;
6095    }
6096  }
6097}
6098
6099/* Reset the prepared statement created using shellPreparePrintf().
6100**
6101** This routine is could be marked "static".  But it is not always used,
6102** depending on compile-time options.  By omitting the "static", we avoid
6103** nuisance compiler warnings about "defined but not used".
6104*/
6105void shellReset(
6106  int *pRc,
6107  sqlite3_stmt *pStmt
6108){
6109  int rc = sqlite3_reset(pStmt);
6110  if( *pRc==SQLITE_OK ){
6111    if( rc!=SQLITE_OK ){
6112      sqlite3 *db = sqlite3_db_handle(pStmt);
6113      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6114    }
6115    *pRc = rc;
6116  }
6117}
6118#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6119
6120#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6121/******************************************************************************
6122** The ".archive" or ".ar" command.
6123*/
6124/*
6125** Structure representing a single ".ar" command.
6126*/
6127typedef struct ArCommand ArCommand;
6128struct ArCommand {
6129  u8 eCmd;                        /* An AR_CMD_* value */
6130  u8 bVerbose;                    /* True if --verbose */
6131  u8 bZip;                        /* True if the archive is a ZIP */
6132  u8 bDryRun;                     /* True if --dry-run */
6133  u8 bAppend;                     /* True if --append */
6134  u8 bGlob;                       /* True if --glob */
6135  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6136  int nArg;                       /* Number of command arguments */
6137  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6138  const char *zFile;              /* --file argument, or NULL */
6139  const char *zDir;               /* --directory argument, or NULL */
6140  char **azArg;                   /* Array of command arguments */
6141  ShellState *p;                  /* Shell state */
6142  sqlite3 *db;                    /* Database containing the archive */
6143};
6144
6145/*
6146** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6147*/
6148static int arUsage(FILE *f){
6149  showHelp(f,"archive");
6150  return SQLITE_ERROR;
6151}
6152
6153/*
6154** Print an error message for the .ar command to stderr and return
6155** SQLITE_ERROR.
6156*/
6157static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6158  va_list ap;
6159  char *z;
6160  va_start(ap, zFmt);
6161  z = sqlite3_vmprintf(zFmt, ap);
6162  va_end(ap);
6163  utf8_printf(stderr, "Error: %s\n", z);
6164  if( pAr->fromCmdLine ){
6165    utf8_printf(stderr, "Use \"-A\" for more help\n");
6166  }else{
6167    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6168  }
6169  sqlite3_free(z);
6170  return SQLITE_ERROR;
6171}
6172
6173/*
6174** Values for ArCommand.eCmd.
6175*/
6176#define AR_CMD_CREATE       1
6177#define AR_CMD_UPDATE       2
6178#define AR_CMD_INSERT       3
6179#define AR_CMD_EXTRACT      4
6180#define AR_CMD_LIST         5
6181#define AR_CMD_HELP         6
6182#define AR_CMD_REMOVE       7
6183
6184/*
6185** Other (non-command) switches.
6186*/
6187#define AR_SWITCH_VERBOSE     8
6188#define AR_SWITCH_FILE        9
6189#define AR_SWITCH_DIRECTORY  10
6190#define AR_SWITCH_APPEND     11
6191#define AR_SWITCH_DRYRUN     12
6192#define AR_SWITCH_GLOB       13
6193
6194static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6195  switch( eSwitch ){
6196    case AR_CMD_CREATE:
6197    case AR_CMD_EXTRACT:
6198    case AR_CMD_LIST:
6199    case AR_CMD_REMOVE:
6200    case AR_CMD_UPDATE:
6201    case AR_CMD_INSERT:
6202    case AR_CMD_HELP:
6203      if( pAr->eCmd ){
6204        return arErrorMsg(pAr, "multiple command options");
6205      }
6206      pAr->eCmd = eSwitch;
6207      break;
6208
6209    case AR_SWITCH_DRYRUN:
6210      pAr->bDryRun = 1;
6211      break;
6212    case AR_SWITCH_GLOB:
6213      pAr->bGlob = 1;
6214      break;
6215    case AR_SWITCH_VERBOSE:
6216      pAr->bVerbose = 1;
6217      break;
6218    case AR_SWITCH_APPEND:
6219      pAr->bAppend = 1;
6220      /* Fall thru into --file */
6221    case AR_SWITCH_FILE:
6222      pAr->zFile = zArg;
6223      break;
6224    case AR_SWITCH_DIRECTORY:
6225      pAr->zDir = zArg;
6226      break;
6227  }
6228
6229  return SQLITE_OK;
6230}
6231
6232/*
6233** Parse the command line for an ".ar" command. The results are written into
6234** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6235** successfully, otherwise an error message is written to stderr and
6236** SQLITE_ERROR returned.
6237*/
6238static int arParseCommand(
6239  char **azArg,                   /* Array of arguments passed to dot command */
6240  int nArg,                       /* Number of entries in azArg[] */
6241  ArCommand *pAr                  /* Populate this object */
6242){
6243  struct ArSwitch {
6244    const char *zLong;
6245    char cShort;
6246    u8 eSwitch;
6247    u8 bArg;
6248  } aSwitch[] = {
6249    { "create",    'c', AR_CMD_CREATE,       0 },
6250    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6251    { "insert",    'i', AR_CMD_INSERT,       0 },
6252    { "list",      't', AR_CMD_LIST,         0 },
6253    { "remove",    'r', AR_CMD_REMOVE,       0 },
6254    { "update",    'u', AR_CMD_UPDATE,       0 },
6255    { "help",      'h', AR_CMD_HELP,         0 },
6256    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6257    { "file",      'f', AR_SWITCH_FILE,      1 },
6258    { "append",    'a', AR_SWITCH_APPEND,    1 },
6259    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6260    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6261    { "glob",      'g', AR_SWITCH_GLOB,      0 },
6262  };
6263  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6264  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6265
6266  if( nArg<=1 ){
6267    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6268    return arUsage(stderr);
6269  }else{
6270    char *z = azArg[1];
6271    if( z[0]!='-' ){
6272      /* Traditional style [tar] invocation */
6273      int i;
6274      int iArg = 2;
6275      for(i=0; z[i]; i++){
6276        const char *zArg = 0;
6277        struct ArSwitch *pOpt;
6278        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6279          if( z[i]==pOpt->cShort ) break;
6280        }
6281        if( pOpt==pEnd ){
6282          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6283        }
6284        if( pOpt->bArg ){
6285          if( iArg>=nArg ){
6286            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6287          }
6288          zArg = azArg[iArg++];
6289        }
6290        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6291      }
6292      pAr->nArg = nArg-iArg;
6293      if( pAr->nArg>0 ){
6294        pAr->azArg = &azArg[iArg];
6295      }
6296    }else{
6297      /* Non-traditional invocation */
6298      int iArg;
6299      for(iArg=1; iArg<nArg; iArg++){
6300        int n;
6301        z = azArg[iArg];
6302        if( z[0]!='-' ){
6303          /* All remaining command line words are command arguments. */
6304          pAr->azArg = &azArg[iArg];
6305          pAr->nArg = nArg-iArg;
6306          break;
6307        }
6308        n = strlen30(z);
6309
6310        if( z[1]!='-' ){
6311          int i;
6312          /* One or more short options */
6313          for(i=1; i<n; i++){
6314            const char *zArg = 0;
6315            struct ArSwitch *pOpt;
6316            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6317              if( z[i]==pOpt->cShort ) break;
6318            }
6319            if( pOpt==pEnd ){
6320              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6321            }
6322            if( pOpt->bArg ){
6323              if( i<(n-1) ){
6324                zArg = &z[i+1];
6325                i = n;
6326              }else{
6327                if( iArg>=(nArg-1) ){
6328                  return arErrorMsg(pAr, "option requires an argument: %c",
6329                                    z[i]);
6330                }
6331                zArg = azArg[++iArg];
6332              }
6333            }
6334            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6335          }
6336        }else if( z[2]=='\0' ){
6337          /* A -- option, indicating that all remaining command line words
6338          ** are command arguments.  */
6339          pAr->azArg = &azArg[iArg+1];
6340          pAr->nArg = nArg-iArg-1;
6341          break;
6342        }else{
6343          /* A long option */
6344          const char *zArg = 0;             /* Argument for option, if any */
6345          struct ArSwitch *pMatch = 0;      /* Matching option */
6346          struct ArSwitch *pOpt;            /* Iterator */
6347          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6348            const char *zLong = pOpt->zLong;
6349            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6350              if( pMatch ){
6351                return arErrorMsg(pAr, "ambiguous option: %s",z);
6352              }else{
6353                pMatch = pOpt;
6354              }
6355            }
6356          }
6357
6358          if( pMatch==0 ){
6359            return arErrorMsg(pAr, "unrecognized option: %s", z);
6360          }
6361          if( pMatch->bArg ){
6362            if( iArg>=(nArg-1) ){
6363              return arErrorMsg(pAr, "option requires an argument: %s", z);
6364            }
6365            zArg = azArg[++iArg];
6366          }
6367          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6368        }
6369      }
6370    }
6371  }
6372
6373  return SQLITE_OK;
6374}
6375
6376/*
6377** This function assumes that all arguments within the ArCommand.azArg[]
6378** array refer to archive members, as for the --extract, --list or --remove
6379** commands. It checks that each of them are "present". If any specified
6380** file is not present in the archive, an error is printed to stderr and an
6381** error code returned. Otherwise, if all specified arguments are present
6382** in the archive, SQLITE_OK is returned. Here, "present" means either an
6383** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
6384** when pAr->bGlob is true.
6385**
6386** This function strips any trailing '/' characters from each argument.
6387** This is consistent with the way the [tar] command seems to work on
6388** Linux.
6389*/
6390static int arCheckEntries(ArCommand *pAr){
6391  int rc = SQLITE_OK;
6392  if( pAr->nArg ){
6393    int i, j;
6394    sqlite3_stmt *pTest = 0;
6395    const char *zSel = (pAr->bGlob)
6396      ? "SELECT name FROM %s WHERE glob($name,name)"
6397      : "SELECT name FROM %s WHERE name=$name";
6398
6399    shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
6400    j = sqlite3_bind_parameter_index(pTest, "$name");
6401    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6402      char *z = pAr->azArg[i];
6403      int n = strlen30(z);
6404      int bOk = 0;
6405      while( n>0 && z[n-1]=='/' ) n--;
6406      z[n] = '\0';
6407      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6408      if( SQLITE_ROW==sqlite3_step(pTest) ){
6409        bOk = 1;
6410      }
6411      shellReset(&rc, pTest);
6412      if( rc==SQLITE_OK && bOk==0 ){
6413        utf8_printf(stderr, "not found in archive: %s\n", z);
6414        rc = SQLITE_ERROR;
6415      }
6416    }
6417    shellFinalize(&rc, pTest);
6418  }
6419  return rc;
6420}
6421
6422/*
6423** Format a WHERE clause that can be used against the "sqlar" table to
6424** identify all archive members that match the command arguments held
6425** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6426** The caller is responsible for eventually calling sqlite3_free() on
6427** any non-NULL (*pzWhere) value. Here, "match" means strict equality
6428** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
6429*/
6430static void arWhereClause(
6431  int *pRc,
6432  ArCommand *pAr,
6433  char **pzWhere                  /* OUT: New WHERE clause */
6434){
6435  char *zWhere = 0;
6436  const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
6437  if( *pRc==SQLITE_OK ){
6438    if( pAr->nArg==0 ){
6439      zWhere = sqlite3_mprintf("1");
6440    }else{
6441      int i;
6442      const char *zSep = "";
6443      for(i=0; i<pAr->nArg; i++){
6444        const char *z = pAr->azArg[i];
6445        zWhere = sqlite3_mprintf(
6446          "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
6447          zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
6448        );
6449        if( zWhere==0 ){
6450          *pRc = SQLITE_NOMEM;
6451          break;
6452        }
6453        zSep = " OR ";
6454      }
6455    }
6456  }
6457  *pzWhere = zWhere;
6458}
6459
6460/*
6461** Implementation of .ar "lisT" command.
6462*/
6463static int arListCommand(ArCommand *pAr){
6464  const char *zSql = "SELECT %s FROM %s WHERE %s";
6465  const char *azCols[] = {
6466    "name",
6467    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6468  };
6469
6470  char *zWhere = 0;
6471  sqlite3_stmt *pSql = 0;
6472  int rc;
6473
6474  rc = arCheckEntries(pAr);
6475  arWhereClause(&rc, pAr, &zWhere);
6476
6477  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6478                     pAr->zSrcTable, zWhere);
6479  if( pAr->bDryRun ){
6480    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6481  }else{
6482    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6483      if( pAr->bVerbose ){
6484        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6485            sqlite3_column_text(pSql, 0),
6486            sqlite3_column_int(pSql, 1),
6487            sqlite3_column_text(pSql, 2),
6488            sqlite3_column_text(pSql, 3)
6489        );
6490      }else{
6491        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6492      }
6493    }
6494  }
6495  shellFinalize(&rc, pSql);
6496  sqlite3_free(zWhere);
6497  return rc;
6498}
6499
6500
6501/*
6502** Implementation of .ar "Remove" command.
6503*/
6504static int arRemoveCommand(ArCommand *pAr){
6505  int rc = 0;
6506  char *zSql = 0;
6507  char *zWhere = 0;
6508
6509  if( pAr->nArg ){
6510    /* Verify that args actually exist within the archive before proceeding.
6511    ** And formulate a WHERE clause to match them.  */
6512    rc = arCheckEntries(pAr);
6513    arWhereClause(&rc, pAr, &zWhere);
6514  }
6515  if( rc==SQLITE_OK ){
6516    zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
6517                           pAr->zSrcTable, zWhere);
6518    if( pAr->bDryRun ){
6519      utf8_printf(pAr->p->out, "%s\n", zSql);
6520    }else{
6521      char *zErr = 0;
6522      rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
6523      if( rc==SQLITE_OK ){
6524        rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6525        if( rc!=SQLITE_OK ){
6526          sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6527        }else{
6528          rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
6529        }
6530      }
6531      if( zErr ){
6532        utf8_printf(stdout, "ERROR: %s\n", zErr);
6533        sqlite3_free(zErr);
6534      }
6535    }
6536  }
6537  sqlite3_free(zWhere);
6538  sqlite3_free(zSql);
6539  return rc;
6540}
6541
6542/*
6543** Implementation of .ar "eXtract" command.
6544*/
6545static int arExtractCommand(ArCommand *pAr){
6546  const char *zSql1 =
6547    "SELECT "
6548    " ($dir || name),"
6549    " writefile(($dir || name), %s, mode, mtime) "
6550    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6551    " AND name NOT GLOB '*..[/\\]*'";
6552
6553  const char *azExtraArg[] = {
6554    "sqlar_uncompress(data, sz)",
6555    "data"
6556  };
6557
6558  sqlite3_stmt *pSql = 0;
6559  int rc = SQLITE_OK;
6560  char *zDir = 0;
6561  char *zWhere = 0;
6562  int i, j;
6563
6564  /* If arguments are specified, check that they actually exist within
6565  ** the archive before proceeding. And formulate a WHERE clause to
6566  ** match them.  */
6567  rc = arCheckEntries(pAr);
6568  arWhereClause(&rc, pAr, &zWhere);
6569
6570  if( rc==SQLITE_OK ){
6571    if( pAr->zDir ){
6572      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6573    }else{
6574      zDir = sqlite3_mprintf("");
6575    }
6576    if( zDir==0 ) rc = SQLITE_NOMEM;
6577  }
6578
6579  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6580      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6581  );
6582
6583  if( rc==SQLITE_OK ){
6584    j = sqlite3_bind_parameter_index(pSql, "$dir");
6585    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6586
6587    /* Run the SELECT statement twice. The first time, writefile() is called
6588    ** for all archive members that should be extracted. The second time,
6589    ** only for the directories. This is because the timestamps for
6590    ** extracted directories must be reset after they are populated (as
6591    ** populating them changes the timestamp).  */
6592    for(i=0; i<2; i++){
6593      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6594      sqlite3_bind_int(pSql, j, i);
6595      if( pAr->bDryRun ){
6596        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6597      }else{
6598        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6599          if( i==0 && pAr->bVerbose ){
6600            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6601          }
6602        }
6603      }
6604      shellReset(&rc, pSql);
6605    }
6606    shellFinalize(&rc, pSql);
6607  }
6608
6609  sqlite3_free(zDir);
6610  sqlite3_free(zWhere);
6611  return rc;
6612}
6613
6614/*
6615** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6616*/
6617static int arExecSql(ArCommand *pAr, const char *zSql){
6618  int rc;
6619  if( pAr->bDryRun ){
6620    utf8_printf(pAr->p->out, "%s\n", zSql);
6621    rc = SQLITE_OK;
6622  }else{
6623    char *zErr = 0;
6624    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6625    if( zErr ){
6626      utf8_printf(stdout, "ERROR: %s\n", zErr);
6627      sqlite3_free(zErr);
6628    }
6629  }
6630  return rc;
6631}
6632
6633
6634/*
6635** Implementation of .ar "create", "insert", and "update" commands.
6636**
6637**     create    ->     Create a new SQL archive
6638**     insert    ->     Insert or reinsert all files listed
6639**     update    ->     Insert files that have changed or that were not
6640**                      previously in the archive
6641**
6642** Create the "sqlar" table in the database if it does not already exist.
6643** Then add each file in the azFile[] array to the archive. Directories
6644** are added recursively. If argument bVerbose is non-zero, a message is
6645** printed on stdout for each file archived.
6646**
6647** The create command is the same as update, except that it drops
6648** any existing "sqlar" table before beginning.  The "insert" command
6649** always overwrites every file named on the command-line, where as
6650** "update" only overwrites if the size or mtime or mode has changed.
6651*/
6652static int arCreateOrUpdateCommand(
6653  ArCommand *pAr,                 /* Command arguments and options */
6654  int bUpdate,                    /* true for a --create. */
6655  int bOnlyIfChanged              /* Only update if file has changed */
6656){
6657  const char *zCreate =
6658      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6659      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6660      "  mode INT,               -- access permissions\n"
6661      "  mtime INT,              -- last modification time\n"
6662      "  sz INT,                 -- original file size\n"
6663      "  data BLOB               -- compressed content\n"
6664      ")";
6665  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6666  const char *zInsertFmt[2] = {
6667     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6668     "  SELECT\n"
6669     "    %s,\n"
6670     "    mode,\n"
6671     "    mtime,\n"
6672     "    CASE substr(lsmode(mode),1,1)\n"
6673     "      WHEN '-' THEN length(data)\n"
6674     "      WHEN 'd' THEN 0\n"
6675     "      ELSE -1 END,\n"
6676     "    sqlar_compress(data)\n"
6677     "  FROM fsdir(%Q,%Q) AS disk\n"
6678     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6679     ,
6680     "REPLACE INTO %s(name,mode,mtime,data)\n"
6681     "  SELECT\n"
6682     "    %s,\n"
6683     "    mode,\n"
6684     "    mtime,\n"
6685     "    data\n"
6686     "  FROM fsdir(%Q,%Q) AS disk\n"
6687     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6688  };
6689  int i;                          /* For iterating through azFile[] */
6690  int rc;                         /* Return code */
6691  const char *zTab = 0;           /* SQL table into which to insert */
6692  char *zSql;
6693  char zTemp[50];
6694  char *zExists = 0;
6695
6696  arExecSql(pAr, "PRAGMA page_size=512");
6697  rc = arExecSql(pAr, "SAVEPOINT ar;");
6698  if( rc!=SQLITE_OK ) return rc;
6699  zTemp[0] = 0;
6700  if( pAr->bZip ){
6701    /* Initialize the zipfile virtual table, if necessary */
6702    if( pAr->zFile ){
6703      sqlite3_uint64 r;
6704      sqlite3_randomness(sizeof(r),&r);
6705      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6706      zTab = zTemp;
6707      zSql = sqlite3_mprintf(
6708         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6709         zTab, pAr->zFile
6710      );
6711      rc = arExecSql(pAr, zSql);
6712      sqlite3_free(zSql);
6713    }else{
6714      zTab = "zip";
6715    }
6716  }else{
6717    /* Initialize the table for an SQLAR */
6718    zTab = "sqlar";
6719    if( bUpdate==0 ){
6720      rc = arExecSql(pAr, zDrop);
6721      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6722    }
6723    rc = arExecSql(pAr, zCreate);
6724  }
6725  if( bOnlyIfChanged ){
6726    zExists = sqlite3_mprintf(
6727      " AND NOT EXISTS("
6728          "SELECT 1 FROM %s AS mem"
6729          " WHERE mem.name=disk.name"
6730          " AND mem.mtime=disk.mtime"
6731          " AND mem.mode=disk.mode)", zTab);
6732  }else{
6733    zExists = sqlite3_mprintf("");
6734  }
6735  if( zExists==0 ) rc = SQLITE_NOMEM;
6736  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6737    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6738        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6739        pAr->azArg[i], pAr->zDir, zExists);
6740    rc = arExecSql(pAr, zSql2);
6741    sqlite3_free(zSql2);
6742  }
6743end_ar_transaction:
6744  if( rc!=SQLITE_OK ){
6745    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6746  }else{
6747    rc = arExecSql(pAr, "RELEASE ar;");
6748    if( pAr->bZip && pAr->zFile ){
6749      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6750      arExecSql(pAr, zSql);
6751      sqlite3_free(zSql);
6752    }
6753  }
6754  sqlite3_free(zExists);
6755  return rc;
6756}
6757
6758/*
6759** Implementation of ".ar" dot command.
6760*/
6761static int arDotCommand(
6762  ShellState *pState,          /* Current shell tool state */
6763  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6764  char **azArg,                /* Array of arguments passed to dot command */
6765  int nArg                     /* Number of entries in azArg[] */
6766){
6767  ArCommand cmd;
6768  int rc;
6769  memset(&cmd, 0, sizeof(cmd));
6770  cmd.fromCmdLine = fromCmdLine;
6771  rc = arParseCommand(azArg, nArg, &cmd);
6772  if( rc==SQLITE_OK ){
6773    int eDbType = SHELL_OPEN_UNSPEC;
6774    cmd.p = pState;
6775    cmd.db = pState->db;
6776    if( cmd.zFile ){
6777      eDbType = deduceDatabaseType(cmd.zFile, 1);
6778    }else{
6779      eDbType = pState->openMode;
6780    }
6781    if( eDbType==SHELL_OPEN_ZIPFILE ){
6782      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6783        if( cmd.zFile==0 ){
6784          cmd.zSrcTable = sqlite3_mprintf("zip");
6785        }else{
6786          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6787        }
6788      }
6789      cmd.bZip = 1;
6790    }else if( cmd.zFile ){
6791      int flags;
6792      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6793      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6794           || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
6795        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6796      }else{
6797        flags = SQLITE_OPEN_READONLY;
6798      }
6799      cmd.db = 0;
6800      if( cmd.bDryRun ){
6801        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6802             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6803      }
6804      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6805             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6806      if( rc!=SQLITE_OK ){
6807        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6808            cmd.zFile, sqlite3_errmsg(cmd.db)
6809        );
6810        goto end_ar_command;
6811      }
6812      sqlite3_fileio_init(cmd.db, 0, 0);
6813      sqlite3_sqlar_init(cmd.db, 0, 0);
6814      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6815                              shellPutsFunc, 0, 0);
6816
6817    }
6818    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6819      if( cmd.eCmd!=AR_CMD_CREATE
6820       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6821      ){
6822        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6823        rc = SQLITE_ERROR;
6824        goto end_ar_command;
6825      }
6826      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6827    }
6828
6829    switch( cmd.eCmd ){
6830      case AR_CMD_CREATE:
6831        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6832        break;
6833
6834      case AR_CMD_EXTRACT:
6835        rc = arExtractCommand(&cmd);
6836        break;
6837
6838      case AR_CMD_LIST:
6839        rc = arListCommand(&cmd);
6840        break;
6841
6842      case AR_CMD_HELP:
6843        arUsage(pState->out);
6844        break;
6845
6846      case AR_CMD_INSERT:
6847        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6848        break;
6849
6850      case AR_CMD_REMOVE:
6851        rc = arRemoveCommand(&cmd);
6852        break;
6853
6854      default:
6855        assert( cmd.eCmd==AR_CMD_UPDATE );
6856        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6857        break;
6858    }
6859  }
6860end_ar_command:
6861  if( cmd.db!=pState->db ){
6862    close_db(cmd.db);
6863  }
6864  sqlite3_free(cmd.zSrcTable);
6865
6866  return rc;
6867}
6868/* End of the ".archive" or ".ar" command logic
6869*******************************************************************************/
6870#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6871
6872#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6873/*
6874** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6875** Otherwise, the SQL statement or statements in zSql are executed using
6876** database connection db and the error code written to *pRc before
6877** this function returns.
6878*/
6879static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6880  int rc = *pRc;
6881  if( rc==SQLITE_OK ){
6882    char *zErr = 0;
6883    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6884    if( rc!=SQLITE_OK ){
6885      raw_printf(stderr, "SQL error: %s\n", zErr);
6886    }
6887    sqlite3_free(zErr);
6888    *pRc = rc;
6889  }
6890}
6891
6892/*
6893** Like shellExec(), except that zFmt is a printf() style format string.
6894*/
6895static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6896  char *z = 0;
6897  if( *pRc==SQLITE_OK ){
6898    va_list ap;
6899    va_start(ap, zFmt);
6900    z = sqlite3_vmprintf(zFmt, ap);
6901    va_end(ap);
6902    if( z==0 ){
6903      *pRc = SQLITE_NOMEM;
6904    }else{
6905      shellExec(db, pRc, z);
6906    }
6907    sqlite3_free(z);
6908  }
6909}
6910
6911/*
6912** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6913** Otherwise, an attempt is made to allocate, zero and return a pointer
6914** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6915** to SQLITE_NOMEM and NULL returned.
6916*/
6917static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6918  void *pRet = 0;
6919  if( *pRc==SQLITE_OK ){
6920    pRet = sqlite3_malloc64(nByte);
6921    if( pRet==0 ){
6922      *pRc = SQLITE_NOMEM;
6923    }else{
6924      memset(pRet, 0, nByte);
6925    }
6926  }
6927  return pRet;
6928}
6929
6930/*
6931** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6932** Otherwise, zFmt is treated as a printf() style string. The result of
6933** formatting it along with any trailing arguments is written into a
6934** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6935** It is the responsibility of the caller to eventually free this buffer
6936** using a call to sqlite3_free().
6937**
6938** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6939** pointer returned.
6940*/
6941static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6942  char *z = 0;
6943  if( *pRc==SQLITE_OK ){
6944    va_list ap;
6945    va_start(ap, zFmt);
6946    z = sqlite3_vmprintf(zFmt, ap);
6947    va_end(ap);
6948    if( z==0 ){
6949      *pRc = SQLITE_NOMEM;
6950    }
6951  }
6952  return z;
6953}
6954
6955/*
6956** When running the ".recover" command, each output table, and the special
6957** orphaned row table if it is required, is represented by an instance
6958** of the following struct.
6959*/
6960typedef struct RecoverTable RecoverTable;
6961struct RecoverTable {
6962  char *zQuoted;                  /* Quoted version of table name */
6963  int nCol;                       /* Number of columns in table */
6964  char **azlCol;                  /* Array of column lists */
6965  int iPk;                        /* Index of IPK column */
6966};
6967
6968/*
6969** Free a RecoverTable object allocated by recoverFindTable() or
6970** recoverOrphanTable().
6971*/
6972static void recoverFreeTable(RecoverTable *pTab){
6973  if( pTab ){
6974    sqlite3_free(pTab->zQuoted);
6975    if( pTab->azlCol ){
6976      int i;
6977      for(i=0; i<=pTab->nCol; i++){
6978        sqlite3_free(pTab->azlCol[i]);
6979      }
6980      sqlite3_free(pTab->azlCol);
6981    }
6982    sqlite3_free(pTab);
6983  }
6984}
6985
6986/*
6987** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6988** Otherwise, it allocates and returns a RecoverTable object based on the
6989** final four arguments passed to this function. It is the responsibility
6990** of the caller to eventually free the returned object using
6991** recoverFreeTable().
6992*/
6993static RecoverTable *recoverNewTable(
6994  int *pRc,                       /* IN/OUT: Error code */
6995  const char *zName,              /* Name of table */
6996  const char *zSql,               /* CREATE TABLE statement */
6997  int bIntkey,
6998  int nCol
6999){
7000  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
7001  int rc = *pRc;
7002  RecoverTable *pTab = 0;
7003
7004  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
7005  if( rc==SQLITE_OK ){
7006    int nSqlCol = 0;
7007    int bSqlIntkey = 0;
7008    sqlite3_stmt *pStmt = 0;
7009
7010    rc = sqlite3_open("", &dbtmp);
7011    if( rc==SQLITE_OK ){
7012      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
7013                              shellIdQuote, 0, 0);
7014    }
7015    if( rc==SQLITE_OK ){
7016      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
7017    }
7018    if( rc==SQLITE_OK ){
7019      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
7020      if( rc==SQLITE_ERROR ){
7021        rc = SQLITE_OK;
7022        goto finished;
7023      }
7024    }
7025    shellPreparePrintf(dbtmp, &rc, &pStmt,
7026        "SELECT count(*) FROM pragma_table_info(%Q)", zName
7027    );
7028    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7029      nSqlCol = sqlite3_column_int(pStmt, 0);
7030    }
7031    shellFinalize(&rc, pStmt);
7032
7033    if( rc!=SQLITE_OK || nSqlCol<nCol ){
7034      goto finished;
7035    }
7036
7037    shellPreparePrintf(dbtmp, &rc, &pStmt,
7038      "SELECT ("
7039      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
7040      ") FROM sqlite_schema WHERE name = %Q", zName
7041    );
7042    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7043      bSqlIntkey = sqlite3_column_int(pStmt, 0);
7044    }
7045    shellFinalize(&rc, pStmt);
7046
7047    if( bIntkey==bSqlIntkey ){
7048      int i;
7049      const char *zPk = "_rowid_";
7050      sqlite3_stmt *pPkFinder = 0;
7051
7052      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
7053      ** set zPk to the name of the PK column, and pTab->iPk to the index
7054      ** of the column, where columns are 0-numbered from left to right.
7055      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
7056      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
7057      pTab->iPk = -2;
7058      if( bIntkey ){
7059        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
7060          "SELECT cid, name FROM pragma_table_info(%Q) "
7061          "  WHERE pk=1 AND type='integer' COLLATE nocase"
7062          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
7063          , zName, zName
7064        );
7065        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
7066          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
7067          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
7068        }
7069      }
7070
7071      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
7072      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
7073      pTab->nCol = nSqlCol;
7074
7075      if( bIntkey ){
7076        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
7077      }else{
7078        pTab->azlCol[0] = shellMPrintf(&rc, "");
7079      }
7080      i = 1;
7081      shellPreparePrintf(dbtmp, &rc, &pStmt,
7082          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
7083          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
7084          "FROM pragma_table_info(%Q)",
7085          bIntkey ? ", " : "", pTab->iPk,
7086          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
7087          zName
7088      );
7089      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7090        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
7091        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
7092        i++;
7093      }
7094      shellFinalize(&rc, pStmt);
7095
7096      shellFinalize(&rc, pPkFinder);
7097    }
7098  }
7099
7100 finished:
7101  sqlite3_close(dbtmp);
7102  *pRc = rc;
7103  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
7104    recoverFreeTable(pTab);
7105    pTab = 0;
7106  }
7107  return pTab;
7108}
7109
7110/*
7111** This function is called to search the schema recovered from the
7112** sqlite_schema table of the (possibly) corrupt database as part
7113** of a ".recover" command. Specifically, for a table with root page
7114** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
7115** table must be a WITHOUT ROWID table, or if non-zero, not one of
7116** those.
7117**
7118** If a table is found, a (RecoverTable*) object is returned. Or, if
7119** no such table is found, but bIntkey is false and iRoot is the
7120** root page of an index in the recovered schema, then (*pbNoop) is
7121** set to true and NULL returned. Or, if there is no such table or
7122** index, NULL is returned and (*pbNoop) set to 0, indicating that
7123** the caller should write data to the orphans table.
7124*/
7125static RecoverTable *recoverFindTable(
7126  ShellState *pState,             /* Shell state object */
7127  int *pRc,                       /* IN/OUT: Error code */
7128  int iRoot,                      /* Root page of table */
7129  int bIntkey,                    /* True for an intkey table */
7130  int nCol,                       /* Number of columns in table */
7131  int *pbNoop                     /* OUT: True if iRoot is root of index */
7132){
7133  sqlite3_stmt *pStmt = 0;
7134  RecoverTable *pRet = 0;
7135  int bNoop = 0;
7136  const char *zSql = 0;
7137  const char *zName = 0;
7138
7139  /* Search the recovered schema for an object with root page iRoot. */
7140  shellPreparePrintf(pState->db, pRc, &pStmt,
7141      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
7142  );
7143  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7144    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
7145    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
7146      bNoop = 1;
7147      break;
7148    }
7149    if( sqlite3_stricmp(zType, "table")==0 ){
7150      zName = (const char*)sqlite3_column_text(pStmt, 1);
7151      zSql = (const char*)sqlite3_column_text(pStmt, 2);
7152      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
7153      break;
7154    }
7155  }
7156
7157  shellFinalize(pRc, pStmt);
7158  *pbNoop = bNoop;
7159  return pRet;
7160}
7161
7162/*
7163** Return a RecoverTable object representing the orphans table.
7164*/
7165static RecoverTable *recoverOrphanTable(
7166  ShellState *pState,             /* Shell state object */
7167  int *pRc,                       /* IN/OUT: Error code */
7168  const char *zLostAndFound,      /* Base name for orphans table */
7169  int nCol                        /* Number of user data columns */
7170){
7171  RecoverTable *pTab = 0;
7172  if( nCol>=0 && *pRc==SQLITE_OK ){
7173    int i;
7174
7175    /* This block determines the name of the orphan table. The prefered
7176    ** name is zLostAndFound. But if that clashes with another name
7177    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7178    ** and so on until a non-clashing name is found.  */
7179    int iTab = 0;
7180    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7181    sqlite3_stmt *pTest = 0;
7182    shellPrepare(pState->db, pRc,
7183        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7184    );
7185    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7186    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7187      shellReset(pRc, pTest);
7188      sqlite3_free(zTab);
7189      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7190      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7191    }
7192    shellFinalize(pRc, pTest);
7193
7194    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7195    if( pTab ){
7196      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7197      pTab->nCol = nCol;
7198      pTab->iPk = -2;
7199      if( nCol>0 ){
7200        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7201        if( pTab->azlCol ){
7202          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7203          for(i=nCol-1; i>=0; i--){
7204            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7205          }
7206        }
7207      }
7208
7209      if( *pRc!=SQLITE_OK ){
7210        recoverFreeTable(pTab);
7211        pTab = 0;
7212      }else{
7213        raw_printf(pState->out,
7214            "CREATE TABLE %s(rootpgno INTEGER, "
7215            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7216        );
7217        for(i=0; i<nCol; i++){
7218          raw_printf(pState->out, ", c%d", i);
7219        }
7220        raw_printf(pState->out, ");\n");
7221      }
7222    }
7223    sqlite3_free(zTab);
7224  }
7225  return pTab;
7226}
7227
7228/*
7229** This function is called to recover data from the database. A script
7230** to construct a new database containing all recovered data is output
7231** on stream pState->out.
7232*/
7233static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7234  int rc = SQLITE_OK;
7235  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7236  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7237  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7238  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7239  const char *zLostAndFound = "lost_and_found";
7240  int i;
7241  int nOrphan = -1;
7242  RecoverTable *pOrphan = 0;
7243
7244  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7245  int bRowids = 1;                /* 0 if --no-rowids */
7246  for(i=1; i<nArg; i++){
7247    char *z = azArg[i];
7248    int n;
7249    if( z[0]=='-' && z[1]=='-' ) z++;
7250    n = strlen30(z);
7251    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7252      bFreelist = 0;
7253    }else
7254    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7255      i++;
7256      zRecoveryDb = azArg[i];
7257    }else
7258    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7259      i++;
7260      zLostAndFound = azArg[i];
7261    }else
7262    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7263      bRowids = 0;
7264    }
7265    else{
7266      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7267      showHelp(pState->out, azArg[0]);
7268      return 1;
7269    }
7270  }
7271
7272  shellExecPrintf(pState->db, &rc,
7273    /* Attach an in-memory database named 'recovery'. Create an indexed
7274    ** cache of the sqlite_dbptr virtual table. */
7275    "PRAGMA writable_schema = on;"
7276    "ATTACH %Q AS recovery;"
7277    "DROP TABLE IF EXISTS recovery.dbptr;"
7278    "DROP TABLE IF EXISTS recovery.freelist;"
7279    "DROP TABLE IF EXISTS recovery.map;"
7280    "DROP TABLE IF EXISTS recovery.schema;"
7281    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7282  );
7283
7284  if( bFreelist ){
7285    shellExec(pState->db, &rc,
7286      "WITH trunk(pgno) AS ("
7287      "  SELECT shell_int32("
7288      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7289      "      WHERE x>0"
7290      "    UNION"
7291      "  SELECT shell_int32("
7292      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7293      "      FROM trunk WHERE x>0"
7294      "),"
7295      "freelist(data, n, freepgno) AS ("
7296      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7297      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7298      "    UNION ALL"
7299      "  SELECT data, n-1, shell_int32(data, 2+n) "
7300      "      FROM freelist WHERE n>=0"
7301      ")"
7302      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7303    );
7304  }
7305
7306  /* If this is an auto-vacuum database, add all pointer-map pages to
7307  ** the freelist table. Do this regardless of whether or not
7308  ** --freelist-corrupt was specified.  */
7309  shellExec(pState->db, &rc,
7310    "WITH ptrmap(pgno) AS ("
7311    "  SELECT 2 WHERE shell_int32("
7312    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7313    "  )"
7314    "    UNION ALL "
7315    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7316    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7317    ")"
7318    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7319  );
7320
7321  shellExec(pState->db, &rc,
7322    "CREATE TABLE recovery.dbptr("
7323    "      pgno, child, PRIMARY KEY(child, pgno)"
7324    ") WITHOUT ROWID;"
7325    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7326    "    SELECT * FROM sqlite_dbptr"
7327    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7328
7329    /* Delete any pointer to page 1. This ensures that page 1 is considered
7330    ** a root page, regardless of how corrupt the db is. */
7331    "DELETE FROM recovery.dbptr WHERE child = 1;"
7332
7333    /* Delete all pointers to any pages that have more than one pointer
7334    ** to them. Such pages will be treated as root pages when recovering
7335    ** data.  */
7336    "DELETE FROM recovery.dbptr WHERE child IN ("
7337    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7338    ");"
7339
7340    /* Create the "map" table that will (eventually) contain instructions
7341    ** for dealing with each page in the db that contains one or more
7342    ** records. */
7343    "CREATE TABLE recovery.map("
7344      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7345    ");"
7346
7347    /* Populate table [map]. If there are circular loops of pages in the
7348    ** database, the following adds all pages in such a loop to the map
7349    ** as individual root pages. This could be handled better.  */
7350    "WITH pages(i, maxlen) AS ("
7351    "  SELECT page_count, ("
7352    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7353    "  ) FROM pragma_page_count WHERE page_count>0"
7354    "    UNION ALL"
7355    "  SELECT i-1, ("
7356    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7357    "  ) FROM pages WHERE i>=2"
7358    ")"
7359    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7360    "  SELECT i, maxlen, NULL, ("
7361    "    WITH p(orig, pgno, parent) AS ("
7362    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7363    "        UNION "
7364    "      SELECT i, p.parent, "
7365    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7366    "    )"
7367    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7368    ") "
7369    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7370    "UPDATE recovery.map AS o SET intkey = ("
7371    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7372    ");"
7373
7374    /* Extract data from page 1 and any linked pages into table
7375    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7376    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7377    "INSERT INTO recovery.schema SELECT "
7378    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7379    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7380    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7381    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7382    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7383    "FROM sqlite_dbdata WHERE pgno IN ("
7384    "  SELECT pgno FROM recovery.map WHERE root=1"
7385    ")"
7386    "GROUP BY pgno, cell;"
7387    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7388  );
7389
7390  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7391  ** CREATE TABLE statements that extracted from the existing schema.  */
7392  if( rc==SQLITE_OK ){
7393    sqlite3_stmt *pStmt = 0;
7394    /* ".recover" might output content in an order which causes immediate
7395    ** foreign key constraints to be violated. So disable foreign-key
7396    ** constraint enforcement to prevent problems when running the output
7397    ** script. */
7398    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7399    raw_printf(pState->out, "BEGIN;\n");
7400    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7401    shellPrepare(pState->db, &rc,
7402        "SELECT sql FROM recovery.schema "
7403        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7404    );
7405    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7406      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7407      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7408          &zCreateTable[12]
7409      );
7410    }
7411    shellFinalize(&rc, pStmt);
7412  }
7413
7414  /* Figure out if an orphan table will be required. And if so, how many
7415  ** user columns it should contain */
7416  shellPrepare(pState->db, &rc,
7417      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7418      , &pLoop
7419  );
7420  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7421    nOrphan = sqlite3_column_int(pLoop, 0);
7422  }
7423  shellFinalize(&rc, pLoop);
7424  pLoop = 0;
7425
7426  shellPrepare(pState->db, &rc,
7427      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7428  );
7429
7430  shellPrepare(pState->db, &rc,
7431      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7432      "(case when (? AND field<0) then NULL else value end)"
7433      "), ', ')"
7434      ", min(field) "
7435      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7436      "GROUP BY cell", &pCells
7437  );
7438
7439  /* Loop through each root page. */
7440  shellPrepare(pState->db, &rc,
7441      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7442      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7443      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7444      ")", &pLoop
7445  );
7446  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7447    int iRoot = sqlite3_column_int(pLoop, 0);
7448    int bIntkey = sqlite3_column_int(pLoop, 1);
7449    int nCol = sqlite3_column_int(pLoop, 2);
7450    int bNoop = 0;
7451    RecoverTable *pTab;
7452
7453    assert( bIntkey==0 || bIntkey==1 );
7454    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7455    if( bNoop || rc ) continue;
7456    if( pTab==0 ){
7457      if( pOrphan==0 ){
7458        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7459      }
7460      pTab = pOrphan;
7461      if( pTab==0 ) break;
7462    }
7463
7464    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7465      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7466    }
7467    sqlite3_bind_int(pPages, 1, iRoot);
7468    if( bRowids==0 && pTab->iPk<0 ){
7469      sqlite3_bind_int(pCells, 1, 1);
7470    }else{
7471      sqlite3_bind_int(pCells, 1, 0);
7472    }
7473    sqlite3_bind_int(pCells, 3, pTab->iPk);
7474
7475    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7476      int iPgno = sqlite3_column_int(pPages, 0);
7477      sqlite3_bind_int(pCells, 2, iPgno);
7478      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7479        int nField = sqlite3_column_int(pCells, 0);
7480        int iMin = sqlite3_column_int(pCells, 2);
7481        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7482
7483        RecoverTable *pTab2 = pTab;
7484        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7485          if( pOrphan==0 ){
7486            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7487          }
7488          pTab2 = pOrphan;
7489          if( pTab2==0 ) break;
7490        }
7491
7492        nField = nField+1;
7493        if( pTab2==pOrphan ){
7494          raw_printf(pState->out,
7495              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7496              pTab2->zQuoted, iRoot, iPgno, nField,
7497              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7498          );
7499        }else{
7500          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7501              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7502          );
7503        }
7504      }
7505      shellReset(&rc, pCells);
7506    }
7507    shellReset(&rc, pPages);
7508    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7509  }
7510  shellFinalize(&rc, pLoop);
7511  shellFinalize(&rc, pPages);
7512  shellFinalize(&rc, pCells);
7513  recoverFreeTable(pOrphan);
7514
7515  /* The rest of the schema */
7516  if( rc==SQLITE_OK ){
7517    sqlite3_stmt *pStmt = 0;
7518    shellPrepare(pState->db, &rc,
7519        "SELECT sql, name FROM recovery.schema "
7520        "WHERE sql NOT LIKE 'create table%'", &pStmt
7521    );
7522    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7523      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7524      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7525        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7526        char *zPrint = shellMPrintf(&rc,
7527          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7528          zName, zName, zSql
7529        );
7530        raw_printf(pState->out, "%s;\n", zPrint);
7531        sqlite3_free(zPrint);
7532      }else{
7533        raw_printf(pState->out, "%s;\n", zSql);
7534      }
7535    }
7536    shellFinalize(&rc, pStmt);
7537  }
7538
7539  if( rc==SQLITE_OK ){
7540    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7541    raw_printf(pState->out, "COMMIT;\n");
7542  }
7543  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7544  return rc;
7545}
7546#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7547
7548/*
7549** If an input line begins with "." then invoke this routine to
7550** process that line.
7551**
7552** Return 1 on error, 2 to exit, and 0 otherwise.
7553*/
7554static int do_meta_command(char *zLine, ShellState *p){
7555  int h = 1;
7556  int nArg = 0;
7557  int n, c;
7558  int rc = 0;
7559  char *azArg[52];
7560
7561#ifndef SQLITE_OMIT_VIRTUALTABLE
7562  if( p->expert.pExpert ){
7563    expertFinish(p, 1, 0);
7564  }
7565#endif
7566
7567  /* Parse the input line into tokens.
7568  */
7569  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7570    while( IsSpace(zLine[h]) ){ h++; }
7571    if( zLine[h]==0 ) break;
7572    if( zLine[h]=='\'' || zLine[h]=='"' ){
7573      int delim = zLine[h++];
7574      azArg[nArg++] = &zLine[h];
7575      while( zLine[h] && zLine[h]!=delim ){
7576        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7577        h++;
7578      }
7579      if( zLine[h]==delim ){
7580        zLine[h++] = 0;
7581      }
7582      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7583    }else{
7584      azArg[nArg++] = &zLine[h];
7585      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7586      if( zLine[h] ) zLine[h++] = 0;
7587      resolve_backslashes(azArg[nArg-1]);
7588    }
7589  }
7590  azArg[nArg] = 0;
7591
7592  /* Process the input line.
7593  */
7594  if( nArg==0 ) return 0; /* no tokens, no error */
7595  n = strlen30(azArg[0]);
7596  c = azArg[0][0];
7597  clearTempFile(p);
7598
7599#ifndef SQLITE_OMIT_AUTHORIZATION
7600  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7601    if( nArg!=2 ){
7602      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7603      rc = 1;
7604      goto meta_command_exit;
7605    }
7606    open_db(p, 0);
7607    if( booleanValue(azArg[1]) ){
7608      sqlite3_set_authorizer(p->db, shellAuth, p);
7609    }else if( p->bSafeModePersist ){
7610      sqlite3_set_authorizer(p->db, safeModeAuth, p);
7611    }else{
7612      sqlite3_set_authorizer(p->db, 0, 0);
7613    }
7614  }else
7615#endif
7616
7617#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7618  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7619    open_db(p, 0);
7620    failIfSafeMode(p, "cannot run .archive in safe mode");
7621    rc = arDotCommand(p, 0, azArg, nArg);
7622  }else
7623#endif
7624
7625  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7626   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7627  ){
7628    const char *zDestFile = 0;
7629    const char *zDb = 0;
7630    sqlite3 *pDest;
7631    sqlite3_backup *pBackup;
7632    int j;
7633    int bAsync = 0;
7634    const char *zVfs = 0;
7635    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
7636    for(j=1; j<nArg; j++){
7637      const char *z = azArg[j];
7638      if( z[0]=='-' ){
7639        if( z[1]=='-' ) z++;
7640        if( strcmp(z, "-append")==0 ){
7641          zVfs = "apndvfs";
7642        }else
7643        if( strcmp(z, "-async")==0 ){
7644          bAsync = 1;
7645        }else
7646        {
7647          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7648          return 1;
7649        }
7650      }else if( zDestFile==0 ){
7651        zDestFile = azArg[j];
7652      }else if( zDb==0 ){
7653        zDb = zDestFile;
7654        zDestFile = azArg[j];
7655      }else{
7656        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7657        return 1;
7658      }
7659    }
7660    if( zDestFile==0 ){
7661      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7662      return 1;
7663    }
7664    if( zDb==0 ) zDb = "main";
7665    rc = sqlite3_open_v2(zDestFile, &pDest,
7666                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7667    if( rc!=SQLITE_OK ){
7668      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7669      close_db(pDest);
7670      return 1;
7671    }
7672    if( bAsync ){
7673      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7674                   0, 0, 0);
7675    }
7676    open_db(p, 0);
7677    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7678    if( pBackup==0 ){
7679      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7680      close_db(pDest);
7681      return 1;
7682    }
7683    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7684    sqlite3_backup_finish(pBackup);
7685    if( rc==SQLITE_DONE ){
7686      rc = 0;
7687    }else{
7688      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7689      rc = 1;
7690    }
7691    close_db(pDest);
7692  }else
7693
7694  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7695    if( nArg==2 ){
7696      bail_on_error = booleanValue(azArg[1]);
7697    }else{
7698      raw_printf(stderr, "Usage: .bail on|off\n");
7699      rc = 1;
7700    }
7701  }else
7702
7703  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7704    if( nArg==2 ){
7705      if( booleanValue(azArg[1]) ){
7706        setBinaryMode(p->out, 1);
7707      }else{
7708        setTextMode(p->out, 1);
7709      }
7710    }else{
7711      raw_printf(stderr, "Usage: .binary on|off\n");
7712      rc = 1;
7713    }
7714  }else
7715
7716  /* The undocumented ".breakpoint" command causes a call to the no-op
7717  ** routine named test_breakpoint().
7718  */
7719  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7720    test_breakpoint();
7721  }else
7722
7723  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7724    failIfSafeMode(p, "cannot run .cd in safe mode");
7725    if( nArg==2 ){
7726#if defined(_WIN32) || defined(WIN32)
7727      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7728      rc = !SetCurrentDirectoryW(z);
7729      sqlite3_free(z);
7730#else
7731      rc = chdir(azArg[1]);
7732#endif
7733      if( rc ){
7734        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7735        rc = 1;
7736      }
7737    }else{
7738      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7739      rc = 1;
7740    }
7741  }else
7742
7743  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7744    if( nArg==2 ){
7745      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7746    }else{
7747      raw_printf(stderr, "Usage: .changes on|off\n");
7748      rc = 1;
7749    }
7750  }else
7751
7752  /* Cancel output redirection, if it is currently set (by .testcase)
7753  ** Then read the content of the testcase-out.txt file and compare against
7754  ** azArg[1].  If there are differences, report an error and exit.
7755  */
7756  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7757    char *zRes = 0;
7758    output_reset(p);
7759    if( nArg!=2 ){
7760      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7761      rc = 2;
7762    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7763      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7764      rc = 2;
7765    }else if( testcase_glob(azArg[1],zRes)==0 ){
7766      utf8_printf(stderr,
7767                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7768                 p->zTestcase, azArg[1], zRes);
7769      rc = 1;
7770    }else{
7771      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7772      p->nCheck++;
7773    }
7774    sqlite3_free(zRes);
7775  }else
7776
7777  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7778    failIfSafeMode(p, "cannot run .clone in safe mode");
7779    if( nArg==2 ){
7780      tryToClone(p, azArg[1]);
7781    }else{
7782      raw_printf(stderr, "Usage: .clone FILENAME\n");
7783      rc = 1;
7784    }
7785  }else
7786
7787  if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
7788    if( nArg==1 ){
7789      /* List available connections */
7790      int i;
7791      for(i=0; i<ArraySize(p->aAuxDb); i++){
7792        const char *zFile = p->aAuxDb[i].zDbFilename;
7793        if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
7794          zFile = "(not open)";
7795        }else if( zFile==0 ){
7796          zFile = "(memory)";
7797        }else if( zFile[0]==0 ){
7798          zFile = "(temporary-file)";
7799        }
7800        if( p->pAuxDb == &p->aAuxDb[i] ){
7801          utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
7802        }else if( p->aAuxDb[i].db!=0 ){
7803          utf8_printf(stdout, "       %d: %s\n", i, zFile);
7804        }
7805      }
7806    }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
7807      int i = azArg[1][0] - '0';
7808      if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
7809        p->pAuxDb->db = p->db;
7810        p->pAuxDb = &p->aAuxDb[i];
7811        globalDb = p->db = p->pAuxDb->db;
7812        p->pAuxDb->db = 0;
7813      }
7814    }else if( nArg==3 && strcmp(azArg[1], "close")==0
7815           && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
7816      int i = azArg[2][0] - '0';
7817      if( i<0 || i>=ArraySize(p->aAuxDb) ){
7818        /* No-op */
7819      }else if( p->pAuxDb == &p->aAuxDb[i] ){
7820        raw_printf(stderr, "cannot close the active database connection\n");
7821        rc = 1;
7822      }else if( p->aAuxDb[i].db ){
7823        session_close_all(p, i);
7824        close_db(p->aAuxDb[i].db);
7825        p->aAuxDb[i].db = 0;
7826      }
7827    }else{
7828      raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
7829      rc = 1;
7830    }
7831  }else
7832
7833  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7834    char **azName = 0;
7835    int nName = 0;
7836    sqlite3_stmt *pStmt;
7837    int i;
7838    open_db(p, 0);
7839    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7840    if( rc ){
7841      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7842      rc = 1;
7843    }else{
7844      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7845        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7846        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7847        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7848        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7849        azName[nName*2] = strdup(zSchema);
7850        azName[nName*2+1] = strdup(zFile);
7851        nName++;
7852      }
7853    }
7854    sqlite3_finalize(pStmt);
7855    for(i=0; i<nName; i++){
7856      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7857      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7858      const char *z = azName[i*2+1];
7859      utf8_printf(p->out, "%s: %s %s%s\n",
7860         azName[i*2],
7861         z && z[0] ? z : "\"\"",
7862         bRdonly ? "r/o" : "r/w",
7863         eTxn==SQLITE_TXN_NONE ? "" :
7864            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7865      free(azName[i*2]);
7866      free(azName[i*2+1]);
7867    }
7868    sqlite3_free(azName);
7869  }else
7870
7871  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7872    static const struct DbConfigChoices {
7873      const char *zName;
7874      int op;
7875    } aDbConfig[] = {
7876        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7877        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7878        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7879        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7880        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7881        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7882        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7883        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7884        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7885        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7886        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7887        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7888        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7889        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7890        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7891        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7892    };
7893    int ii, v;
7894    open_db(p, 0);
7895    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7896      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7897      if( nArg>=3 ){
7898        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7899      }
7900      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7901      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7902      if( nArg>1 ) break;
7903    }
7904    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7905      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7906      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7907    }
7908  }else
7909
7910  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7911    rc = shell_dbinfo_command(p, nArg, azArg);
7912  }else
7913
7914#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7915  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7916    open_db(p, 0);
7917    rc = recoverDatabaseCmd(p, nArg, azArg);
7918  }else
7919#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7920
7921  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7922    char *zLike = 0;
7923    char *zSql;
7924    int i;
7925    int savedShowHeader = p->showHeader;
7926    int savedShellFlags = p->shellFlgs;
7927    ShellClearFlag(p,
7928       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7929       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7930    for(i=1; i<nArg; i++){
7931      if( azArg[i][0]=='-' ){
7932        const char *z = azArg[i]+1;
7933        if( z[0]=='-' ) z++;
7934        if( strcmp(z,"preserve-rowids")==0 ){
7935#ifdef SQLITE_OMIT_VIRTUALTABLE
7936          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7937                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7938          rc = 1;
7939          sqlite3_free(zLike);
7940          goto meta_command_exit;
7941#else
7942          ShellSetFlag(p, SHFLG_PreserveRowid);
7943#endif
7944        }else
7945        if( strcmp(z,"newlines")==0 ){
7946          ShellSetFlag(p, SHFLG_Newlines);
7947        }else
7948        if( strcmp(z,"data-only")==0 ){
7949          ShellSetFlag(p, SHFLG_DumpDataOnly);
7950        }else
7951        if( strcmp(z,"nosys")==0 ){
7952          ShellSetFlag(p, SHFLG_DumpNoSys);
7953        }else
7954        {
7955          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7956          rc = 1;
7957          sqlite3_free(zLike);
7958          goto meta_command_exit;
7959        }
7960      }else{
7961        /* azArg[i] contains a LIKE pattern. This ".dump" request should
7962        ** only dump data for tables for which either the table name matches
7963        ** the LIKE pattern, or the table appears to be a shadow table of
7964        ** a virtual table for which the name matches the LIKE pattern.
7965        */
7966        char *zExpr = sqlite3_mprintf(
7967            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
7968            "  SELECT 1 FROM sqlite_schema WHERE "
7969            "    name LIKE %Q ESCAPE '\\' AND"
7970            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
7971            "    substr(o.name, 1, length(name)+1) == (name||'_')"
7972            ")", azArg[i], azArg[i]
7973        );
7974
7975        if( zLike ){
7976          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
7977        }else{
7978          zLike = zExpr;
7979        }
7980      }
7981    }
7982
7983    open_db(p, 0);
7984
7985    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7986      /* When playing back a "dump", the content might appear in an order
7987      ** which causes immediate foreign key constraints to be violated.
7988      ** So disable foreign-key constraint enforcement to prevent problems. */
7989      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7990      raw_printf(p->out, "BEGIN TRANSACTION;\n");
7991    }
7992    p->writableSchema = 0;
7993    p->showHeader = 0;
7994    /* Set writable_schema=ON since doing so forces SQLite to initialize
7995    ** as much of the schema as it can even if the sqlite_schema table is
7996    ** corrupt. */
7997    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7998    p->nErr = 0;
7999    if( zLike==0 ) zLike = sqlite3_mprintf("true");
8000    zSql = sqlite3_mprintf(
8001      "SELECT name, type, sql FROM sqlite_schema AS o "
8002      "WHERE (%s) AND type=='table'"
8003      "  AND sql NOT NULL"
8004      " ORDER BY tbl_name='sqlite_sequence', rowid",
8005      zLike
8006    );
8007    run_schema_dump_query(p,zSql);
8008    sqlite3_free(zSql);
8009    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8010      zSql = sqlite3_mprintf(
8011        "SELECT sql FROM sqlite_schema AS o "
8012        "WHERE (%s) AND sql NOT NULL"
8013        "  AND type IN ('index','trigger','view')",
8014        zLike
8015      );
8016      run_table_dump_query(p, zSql);
8017      sqlite3_free(zSql);
8018    }
8019    sqlite3_free(zLike);
8020    if( p->writableSchema ){
8021      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8022      p->writableSchema = 0;
8023    }
8024    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8025    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8026    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8027      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8028    }
8029    p->showHeader = savedShowHeader;
8030    p->shellFlgs = savedShellFlags;
8031  }else
8032
8033  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
8034    if( nArg==2 ){
8035      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8036    }else{
8037      raw_printf(stderr, "Usage: .echo on|off\n");
8038      rc = 1;
8039    }
8040  }else
8041
8042  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
8043    if( nArg==2 ){
8044      p->autoEQPtest = 0;
8045      if( p->autoEQPtrace ){
8046        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8047        p->autoEQPtrace = 0;
8048      }
8049      if( strcmp(azArg[1],"full")==0 ){
8050        p->autoEQP = AUTOEQP_full;
8051      }else if( strcmp(azArg[1],"trigger")==0 ){
8052        p->autoEQP = AUTOEQP_trigger;
8053#ifdef SQLITE_DEBUG
8054      }else if( strcmp(azArg[1],"test")==0 ){
8055        p->autoEQP = AUTOEQP_on;
8056        p->autoEQPtest = 1;
8057      }else if( strcmp(azArg[1],"trace")==0 ){
8058        p->autoEQP = AUTOEQP_full;
8059        p->autoEQPtrace = 1;
8060        open_db(p, 0);
8061        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8062        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8063#endif
8064      }else{
8065        p->autoEQP = (u8)booleanValue(azArg[1]);
8066      }
8067    }else{
8068      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8069      rc = 1;
8070    }
8071  }else
8072
8073  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
8074    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8075    rc = 2;
8076  }else
8077
8078  /* The ".explain" command is automatic now.  It is largely pointless.  It
8079  ** retained purely for backwards compatibility */
8080  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
8081    int val = 1;
8082    if( nArg>=2 ){
8083      if( strcmp(azArg[1],"auto")==0 ){
8084        val = 99;
8085      }else{
8086        val =  booleanValue(azArg[1]);
8087      }
8088    }
8089    if( val==1 && p->mode!=MODE_Explain ){
8090      p->normalMode = p->mode;
8091      p->mode = MODE_Explain;
8092      p->autoExplain = 0;
8093    }else if( val==0 ){
8094      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8095      p->autoExplain = 0;
8096    }else if( val==99 ){
8097      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8098      p->autoExplain = 1;
8099    }
8100  }else
8101
8102#ifndef SQLITE_OMIT_VIRTUALTABLE
8103  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
8104    open_db(p, 0);
8105    expertDotCommand(p, azArg, nArg);
8106  }else
8107#endif
8108
8109  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
8110    static const struct {
8111       const char *zCtrlName;   /* Name of a test-control option */
8112       int ctrlCode;            /* Integer code for that option */
8113       const char *zUsage;      /* Usage notes */
8114    } aCtrl[] = {
8115      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
8116      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
8117      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
8118      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
8119      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
8120   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
8121      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
8122      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
8123      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
8124      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
8125   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
8126    };
8127    int filectrl = -1;
8128    int iCtrl = -1;
8129    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
8130    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
8131    int n2, i;
8132    const char *zCmd = 0;
8133    const char *zSchema = 0;
8134
8135    open_db(p, 0);
8136    zCmd = nArg>=2 ? azArg[1] : "help";
8137
8138    if( zCmd[0]=='-'
8139     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
8140     && nArg>=4
8141    ){
8142      zSchema = azArg[2];
8143      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8144      nArg -= 2;
8145      zCmd = azArg[1];
8146    }
8147
8148    /* The argument can optionally begin with "-" or "--" */
8149    if( zCmd[0]=='-' && zCmd[1] ){
8150      zCmd++;
8151      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8152    }
8153
8154    /* --help lists all file-controls */
8155    if( strcmp(zCmd,"help")==0 ){
8156      utf8_printf(p->out, "Available file-controls:\n");
8157      for(i=0; i<ArraySize(aCtrl); i++){
8158        utf8_printf(p->out, "  .filectrl %s %s\n",
8159                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8160      }
8161      rc = 1;
8162      goto meta_command_exit;
8163    }
8164
8165    /* convert filectrl text option to value. allow any unique prefix
8166    ** of the option name, or a numerical value. */
8167    n2 = strlen30(zCmd);
8168    for(i=0; i<ArraySize(aCtrl); i++){
8169      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8170        if( filectrl<0 ){
8171          filectrl = aCtrl[i].ctrlCode;
8172          iCtrl = i;
8173        }else{
8174          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8175                              "Use \".filectrl --help\" for help\n", zCmd);
8176          rc = 1;
8177          goto meta_command_exit;
8178        }
8179      }
8180    }
8181    if( filectrl<0 ){
8182      utf8_printf(stderr,"Error: unknown file-control: %s\n"
8183                         "Use \".filectrl --help\" for help\n", zCmd);
8184    }else{
8185      switch(filectrl){
8186        case SQLITE_FCNTL_SIZE_LIMIT: {
8187          if( nArg!=2 && nArg!=3 ) break;
8188          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8189          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8190          isOk = 1;
8191          break;
8192        }
8193        case SQLITE_FCNTL_LOCK_TIMEOUT:
8194        case SQLITE_FCNTL_CHUNK_SIZE: {
8195          int x;
8196          if( nArg!=3 ) break;
8197          x = (int)integerValue(azArg[2]);
8198          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8199          isOk = 2;
8200          break;
8201        }
8202        case SQLITE_FCNTL_PERSIST_WAL:
8203        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8204          int x;
8205          if( nArg!=2 && nArg!=3 ) break;
8206          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8207          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8208          iRes = x;
8209          isOk = 1;
8210          break;
8211        }
8212        case SQLITE_FCNTL_DATA_VERSION:
8213        case SQLITE_FCNTL_HAS_MOVED: {
8214          int x;
8215          if( nArg!=2 ) break;
8216          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8217          iRes = x;
8218          isOk = 1;
8219          break;
8220        }
8221        case SQLITE_FCNTL_TEMPFILENAME: {
8222          char *z = 0;
8223          if( nArg!=2 ) break;
8224          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8225          if( z ){
8226            utf8_printf(p->out, "%s\n", z);
8227            sqlite3_free(z);
8228          }
8229          isOk = 2;
8230          break;
8231        }
8232        case SQLITE_FCNTL_RESERVE_BYTES: {
8233          int x;
8234          if( nArg>=3 ){
8235            x = atoi(azArg[2]);
8236            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8237          }
8238          x = -1;
8239          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8240          utf8_printf(p->out,"%d\n", x);
8241          isOk = 2;
8242          break;
8243        }
8244      }
8245    }
8246    if( isOk==0 && iCtrl>=0 ){
8247      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8248      rc = 1;
8249    }else if( isOk==1 ){
8250      char zBuf[100];
8251      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8252      raw_printf(p->out, "%s\n", zBuf);
8253    }
8254  }else
8255
8256  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8257    ShellState data;
8258    int doStats = 0;
8259    memcpy(&data, p, sizeof(data));
8260    data.showHeader = 0;
8261    data.cMode = data.mode = MODE_Semi;
8262    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8263      data.cMode = data.mode = MODE_Pretty;
8264      nArg = 1;
8265    }
8266    if( nArg!=1 ){
8267      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8268      rc = 1;
8269      goto meta_command_exit;
8270    }
8271    open_db(p, 0);
8272    rc = sqlite3_exec(p->db,
8273       "SELECT sql FROM"
8274       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8275       "     FROM sqlite_schema UNION ALL"
8276       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8277       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8278       "ORDER BY x",
8279       callback, &data, 0
8280    );
8281    if( rc==SQLITE_OK ){
8282      sqlite3_stmt *pStmt;
8283      rc = sqlite3_prepare_v2(p->db,
8284               "SELECT rowid FROM sqlite_schema"
8285               " WHERE name GLOB 'sqlite_stat[134]'",
8286               -1, &pStmt, 0);
8287      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8288      sqlite3_finalize(pStmt);
8289    }
8290    if( doStats==0 ){
8291      raw_printf(p->out, "/* No STAT tables available */\n");
8292    }else{
8293      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8294      data.cMode = data.mode = MODE_Insert;
8295      data.zDestTable = "sqlite_stat1";
8296      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8297      data.zDestTable = "sqlite_stat4";
8298      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8299      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8300    }
8301  }else
8302
8303  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8304    if( nArg==2 ){
8305      p->showHeader = booleanValue(azArg[1]);
8306      p->shellFlgs |= SHFLG_HeaderSet;
8307    }else{
8308      raw_printf(stderr, "Usage: .headers on|off\n");
8309      rc = 1;
8310    }
8311  }else
8312
8313  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8314    if( nArg>=2 ){
8315      n = showHelp(p->out, azArg[1]);
8316      if( n==0 ){
8317        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8318      }
8319    }else{
8320      showHelp(p->out, 0);
8321    }
8322  }else
8323
8324  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8325    char *zTable = 0;           /* Insert data into this table */
8326    char *zFile = 0;            /* Name of file to extra content from */
8327    sqlite3_stmt *pStmt = NULL; /* A statement */
8328    int nCol;                   /* Number of columns in the table */
8329    int nByte;                  /* Number of bytes in an SQL string */
8330    int i, j;                   /* Loop counters */
8331    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8332    int nSep;                   /* Number of bytes in p->colSeparator[] */
8333    char *zSql;                 /* An SQL statement */
8334    ImportCtx sCtx;             /* Reader context */
8335    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8336    int eVerbose = 0;           /* Larger for more console output */
8337    int nSkip = 0;              /* Initial lines to skip */
8338    int useOutputMode = 1;      /* Use output mode to determine separators */
8339
8340    failIfSafeMode(p, "cannot run .import in safe mode");
8341    memset(&sCtx, 0, sizeof(sCtx));
8342    sCtx.z = sqlite3_malloc64(120);
8343    if( sCtx.z==0 ){
8344      import_cleanup(&sCtx);
8345      shell_out_of_memory();
8346    }
8347    if( p->mode==MODE_Ascii ){
8348      xRead = ascii_read_one_field;
8349    }else{
8350      xRead = csv_read_one_field;
8351    }
8352    for(i=1; i<nArg; i++){
8353      char *z = azArg[i];
8354      if( z[0]=='-' && z[1]=='-' ) z++;
8355      if( z[0]!='-' ){
8356        if( zFile==0 ){
8357          zFile = z;
8358        }else if( zTable==0 ){
8359          zTable = z;
8360        }else{
8361          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8362          showHelp(p->out, "import");
8363          rc = 1;
8364          goto meta_command_exit;
8365        }
8366      }else if( strcmp(z,"-v")==0 ){
8367        eVerbose++;
8368      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8369        nSkip = integerValue(azArg[++i]);
8370      }else if( strcmp(z,"-ascii")==0 ){
8371        sCtx.cColSep = SEP_Unit[0];
8372        sCtx.cRowSep = SEP_Record[0];
8373        xRead = ascii_read_one_field;
8374        useOutputMode = 0;
8375      }else if( strcmp(z,"-csv")==0 ){
8376        sCtx.cColSep = ',';
8377        sCtx.cRowSep = '\n';
8378        xRead = csv_read_one_field;
8379        useOutputMode = 0;
8380      }else{
8381        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8382        showHelp(p->out, "import");
8383        rc = 1;
8384        goto meta_command_exit;
8385      }
8386    }
8387    if( zTable==0 ){
8388      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8389                  zFile==0 ? "FILE" : "TABLE");
8390      showHelp(p->out, "import");
8391      rc = 1;
8392      goto meta_command_exit;
8393    }
8394    seenInterrupt = 0;
8395    open_db(p, 0);
8396    if( useOutputMode ){
8397      /* If neither the --csv or --ascii options are specified, then set
8398      ** the column and row separator characters from the output mode. */
8399      nSep = strlen30(p->colSeparator);
8400      if( nSep==0 ){
8401        raw_printf(stderr,
8402                   "Error: non-null column separator required for import\n");
8403        rc = 1;
8404        goto meta_command_exit;
8405      }
8406      if( nSep>1 ){
8407        raw_printf(stderr,
8408              "Error: multi-character column separators not allowed"
8409              " for import\n");
8410        rc = 1;
8411        goto meta_command_exit;
8412      }
8413      nSep = strlen30(p->rowSeparator);
8414      if( nSep==0 ){
8415        raw_printf(stderr,
8416            "Error: non-null row separator required for import\n");
8417        rc = 1;
8418        goto meta_command_exit;
8419      }
8420      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8421        /* When importing CSV (only), if the row separator is set to the
8422        ** default output row separator, change it to the default input
8423        ** row separator.  This avoids having to maintain different input
8424        ** and output row separators. */
8425        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8426        nSep = strlen30(p->rowSeparator);
8427      }
8428      if( nSep>1 ){
8429        raw_printf(stderr, "Error: multi-character row separators not allowed"
8430                           " for import\n");
8431        rc = 1;
8432        goto meta_command_exit;
8433      }
8434      sCtx.cColSep = p->colSeparator[0];
8435      sCtx.cRowSep = p->rowSeparator[0];
8436    }
8437    sCtx.zFile = zFile;
8438    sCtx.nLine = 1;
8439    if( sCtx.zFile[0]=='|' ){
8440#ifdef SQLITE_OMIT_POPEN
8441      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8442      rc = 1;
8443      goto meta_command_exit;
8444#else
8445      sCtx.in = popen(sCtx.zFile+1, "r");
8446      sCtx.zFile = "<pipe>";
8447      sCtx.xCloser = pclose;
8448#endif
8449    }else{
8450      sCtx.in = fopen(sCtx.zFile, "rb");
8451      sCtx.xCloser = fclose;
8452    }
8453    if( sCtx.in==0 ){
8454      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8455      rc = 1;
8456      import_cleanup(&sCtx);
8457      goto meta_command_exit;
8458    }
8459    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8460      char zSep[2];
8461      zSep[1] = 0;
8462      zSep[0] = sCtx.cColSep;
8463      utf8_printf(p->out, "Column separator ");
8464      output_c_string(p->out, zSep);
8465      utf8_printf(p->out, ", row separator ");
8466      zSep[0] = sCtx.cRowSep;
8467      output_c_string(p->out, zSep);
8468      utf8_printf(p->out, "\n");
8469    }
8470    while( (nSkip--)>0 ){
8471      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8472    }
8473    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
8474    if( zSql==0 ){
8475      import_cleanup(&sCtx);
8476      shell_out_of_memory();
8477    }
8478    nByte = strlen30(zSql);
8479    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8480    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8481    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8482      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
8483      char cSep = '(';
8484      while( xRead(&sCtx) ){
8485        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8486        cSep = ',';
8487        if( sCtx.cTerm!=sCtx.cColSep ) break;
8488      }
8489      if( cSep=='(' ){
8490        sqlite3_free(zCreate);
8491        import_cleanup(&sCtx);
8492        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8493        rc = 1;
8494        goto meta_command_exit;
8495      }
8496      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8497      if( eVerbose>=1 ){
8498        utf8_printf(p->out, "%s\n", zCreate);
8499      }
8500      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8501      sqlite3_free(zCreate);
8502      if( rc ){
8503        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
8504                sqlite3_errmsg(p->db));
8505        import_cleanup(&sCtx);
8506        rc = 1;
8507        goto meta_command_exit;
8508      }
8509      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8510    }
8511    sqlite3_free(zSql);
8512    if( rc ){
8513      if (pStmt) sqlite3_finalize(pStmt);
8514      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8515      import_cleanup(&sCtx);
8516      rc = 1;
8517      goto meta_command_exit;
8518    }
8519    nCol = sqlite3_column_count(pStmt);
8520    sqlite3_finalize(pStmt);
8521    pStmt = 0;
8522    if( nCol==0 ) return 0; /* no columns, no error */
8523    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8524    if( zSql==0 ){
8525      import_cleanup(&sCtx);
8526      shell_out_of_memory();
8527    }
8528    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8529    j = strlen30(zSql);
8530    for(i=1; i<nCol; i++){
8531      zSql[j++] = ',';
8532      zSql[j++] = '?';
8533    }
8534    zSql[j++] = ')';
8535    zSql[j] = 0;
8536    if( eVerbose>=2 ){
8537      utf8_printf(p->out, "Insert using: %s\n", zSql);
8538    }
8539    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8540    sqlite3_free(zSql);
8541    if( rc ){
8542      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8543      if (pStmt) sqlite3_finalize(pStmt);
8544      import_cleanup(&sCtx);
8545      rc = 1;
8546      goto meta_command_exit;
8547    }
8548    needCommit = sqlite3_get_autocommit(p->db);
8549    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8550    do{
8551      int startLine = sCtx.nLine;
8552      for(i=0; i<nCol; i++){
8553        char *z = xRead(&sCtx);
8554        /*
8555        ** Did we reach end-of-file before finding any columns?
8556        ** If so, stop instead of NULL filling the remaining columns.
8557        */
8558        if( z==0 && i==0 ) break;
8559        /*
8560        ** Did we reach end-of-file OR end-of-line before finding any
8561        ** columns in ASCII mode?  If so, stop instead of NULL filling
8562        ** the remaining columns.
8563        */
8564        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8565        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8566        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8567          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8568                          "filling the rest with NULL\n",
8569                          sCtx.zFile, startLine, nCol, i+1);
8570          i += 2;
8571          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8572        }
8573      }
8574      if( sCtx.cTerm==sCtx.cColSep ){
8575        do{
8576          xRead(&sCtx);
8577          i++;
8578        }while( sCtx.cTerm==sCtx.cColSep );
8579        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8580                        "extras ignored\n",
8581                        sCtx.zFile, startLine, nCol, i);
8582      }
8583      if( i>=nCol ){
8584        sqlite3_step(pStmt);
8585        rc = sqlite3_reset(pStmt);
8586        if( rc!=SQLITE_OK ){
8587          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8588                      startLine, sqlite3_errmsg(p->db));
8589          sCtx.nErr++;
8590        }else{
8591          sCtx.nRow++;
8592        }
8593      }
8594    }while( sCtx.cTerm!=EOF );
8595
8596    import_cleanup(&sCtx);
8597    sqlite3_finalize(pStmt);
8598    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8599    if( eVerbose>0 ){
8600      utf8_printf(p->out,
8601          "Added %d rows with %d errors using %d lines of input\n",
8602          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8603    }
8604  }else
8605
8606#ifndef SQLITE_UNTESTABLE
8607  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8608    char *zSql;
8609    char *zCollist = 0;
8610    sqlite3_stmt *pStmt;
8611    int tnum = 0;
8612    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8613    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8614    int i;
8615    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8616      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8617                          "       .imposter off\n");
8618      /* Also allowed, but not documented:
8619      **
8620      **    .imposter TABLE IMPOSTER
8621      **
8622      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8623      ** imposter is another WITHOUT ROWID table with the columns in
8624      ** storage order. */
8625      rc = 1;
8626      goto meta_command_exit;
8627    }
8628    open_db(p, 0);
8629    if( nArg==2 ){
8630      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8631      goto meta_command_exit;
8632    }
8633    zSql = sqlite3_mprintf(
8634      "SELECT rootpage, 0 FROM sqlite_schema"
8635      " WHERE name='%q' AND type='index'"
8636      "UNION ALL "
8637      "SELECT rootpage, 1 FROM sqlite_schema"
8638      " WHERE name='%q' AND type='table'"
8639      "   AND sql LIKE '%%without%%rowid%%'",
8640      azArg[1], azArg[1]
8641    );
8642    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8643    sqlite3_free(zSql);
8644    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8645      tnum = sqlite3_column_int(pStmt, 0);
8646      isWO = sqlite3_column_int(pStmt, 1);
8647    }
8648    sqlite3_finalize(pStmt);
8649    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8650    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8651    sqlite3_free(zSql);
8652    i = 0;
8653    while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8654      char zLabel[20];
8655      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8656      i++;
8657      if( zCol==0 ){
8658        if( sqlite3_column_int(pStmt,1)==-1 ){
8659          zCol = "_ROWID_";
8660        }else{
8661          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8662          zCol = zLabel;
8663        }
8664      }
8665      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8666        lenPK = (int)strlen(zCollist);
8667      }
8668      if( zCollist==0 ){
8669        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8670      }else{
8671        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8672      }
8673    }
8674    sqlite3_finalize(pStmt);
8675    if( i==0 || tnum==0 ){
8676      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8677      rc = 1;
8678      sqlite3_free(zCollist);
8679      goto meta_command_exit;
8680    }
8681    if( lenPK==0 ) lenPK = 100000;
8682    zSql = sqlite3_mprintf(
8683          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8684          azArg[2], zCollist, lenPK, zCollist);
8685    sqlite3_free(zCollist);
8686    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8687    if( rc==SQLITE_OK ){
8688      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8689      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8690      if( rc ){
8691        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8692      }else{
8693        utf8_printf(stdout, "%s;\n", zSql);
8694        raw_printf(stdout,
8695          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8696          azArg[1], isWO ? "table" : "index"
8697        );
8698      }
8699    }else{
8700      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8701      rc = 1;
8702    }
8703    sqlite3_free(zSql);
8704  }else
8705#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8706
8707#ifdef SQLITE_ENABLE_IOTRACE
8708  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8709    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8710    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8711    iotrace = 0;
8712    if( nArg<2 ){
8713      sqlite3IoTrace = 0;
8714    }else if( strcmp(azArg[1], "-")==0 ){
8715      sqlite3IoTrace = iotracePrintf;
8716      iotrace = stdout;
8717    }else{
8718      iotrace = fopen(azArg[1], "w");
8719      if( iotrace==0 ){
8720        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8721        sqlite3IoTrace = 0;
8722        rc = 1;
8723      }else{
8724        sqlite3IoTrace = iotracePrintf;
8725      }
8726    }
8727  }else
8728#endif
8729
8730  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8731    static const struct {
8732       const char *zLimitName;   /* Name of a limit */
8733       int limitCode;            /* Integer code for that limit */
8734    } aLimit[] = {
8735      { "length",                SQLITE_LIMIT_LENGTH                    },
8736      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8737      { "column",                SQLITE_LIMIT_COLUMN                    },
8738      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8739      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8740      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8741      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8742      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8743      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8744      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8745      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8746      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8747    };
8748    int i, n2;
8749    open_db(p, 0);
8750    if( nArg==1 ){
8751      for(i=0; i<ArraySize(aLimit); i++){
8752        printf("%20s %d\n", aLimit[i].zLimitName,
8753               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8754      }
8755    }else if( nArg>3 ){
8756      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8757      rc = 1;
8758      goto meta_command_exit;
8759    }else{
8760      int iLimit = -1;
8761      n2 = strlen30(azArg[1]);
8762      for(i=0; i<ArraySize(aLimit); i++){
8763        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8764          if( iLimit<0 ){
8765            iLimit = i;
8766          }else{
8767            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8768            rc = 1;
8769            goto meta_command_exit;
8770          }
8771        }
8772      }
8773      if( iLimit<0 ){
8774        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8775                        "enter \".limits\" with no arguments for a list.\n",
8776                         azArg[1]);
8777        rc = 1;
8778        goto meta_command_exit;
8779      }
8780      if( nArg==3 ){
8781        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8782                      (int)integerValue(azArg[2]));
8783      }
8784      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8785             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8786    }
8787  }else
8788
8789  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8790    open_db(p, 0);
8791    lintDotCommand(p, azArg, nArg);
8792  }else
8793
8794#ifndef SQLITE_OMIT_LOAD_EXTENSION
8795  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8796    const char *zFile, *zProc;
8797    char *zErrMsg = 0;
8798    failIfSafeMode(p, "cannot run .load in safe mode");
8799    if( nArg<2 ){
8800      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8801      rc = 1;
8802      goto meta_command_exit;
8803    }
8804    zFile = azArg[1];
8805    zProc = nArg>=3 ? azArg[2] : 0;
8806    open_db(p, 0);
8807    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8808    if( rc!=SQLITE_OK ){
8809      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8810      sqlite3_free(zErrMsg);
8811      rc = 1;
8812    }
8813  }else
8814#endif
8815
8816  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8817    failIfSafeMode(p, "cannot run .log in safe mode");
8818    if( nArg!=2 ){
8819      raw_printf(stderr, "Usage: .log FILENAME\n");
8820      rc = 1;
8821    }else{
8822      const char *zFile = azArg[1];
8823      output_file_close(p->pLog);
8824      p->pLog = output_file_open(zFile, 0);
8825    }
8826  }else
8827
8828  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8829    const char *zMode = nArg>=2 ? azArg[1] : "";
8830    int n2 = strlen30(zMode);
8831    int c2 = zMode[0];
8832    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8833      p->mode = MODE_Line;
8834      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8835    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8836      p->mode = MODE_Column;
8837      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8838        p->showHeader = 1;
8839      }
8840      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8841    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8842      p->mode = MODE_List;
8843      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8844      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8845    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8846      p->mode = MODE_Html;
8847    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8848      p->mode = MODE_Tcl;
8849      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8850      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8851    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8852      p->mode = MODE_Csv;
8853      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8854      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8855    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8856      p->mode = MODE_List;
8857      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8858    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8859      p->mode = MODE_Insert;
8860      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8861    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8862      p->mode = MODE_Quote;
8863      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8864      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8865    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8866      p->mode = MODE_Ascii;
8867      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8868      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8869    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8870      p->mode = MODE_Markdown;
8871    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8872      p->mode = MODE_Table;
8873    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8874      p->mode = MODE_Box;
8875    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8876      p->mode = MODE_Json;
8877    }else if( nArg==1 ){
8878      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8879    }else{
8880      raw_printf(stderr, "Error: mode should be one of: "
8881         "ascii box column csv html insert json line list markdown "
8882         "quote table tabs tcl\n");
8883      rc = 1;
8884    }
8885    p->cMode = p->mode;
8886  }else
8887
8888  if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
8889    if( nArg!=2 ){
8890      raw_printf(stderr, "Usage: .nonce NONCE\n");
8891      rc = 1;
8892    }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
8893      raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", p->lineno, azArg[1]);
8894      exit(1);
8895    }else{
8896      p->bSafeMode = 0;
8897      return 0;  /* Return immediately to bypass the safe mode reset
8898                 ** at the end of this procedure */
8899    }
8900  }else
8901
8902  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8903    if( nArg==2 ){
8904      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8905                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8906    }else{
8907      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8908      rc = 1;
8909    }
8910  }else
8911
8912#ifdef SQLITE_DEBUG
8913  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8914    int i;
8915    for(i=1; i<nArg; i++){
8916      const char *z = azArg[i];
8917      if( z[0]=='-' && z[1]=='-' ) z++;
8918      if( strcmp(z,"-repeat")==0 ){
8919        if( i==nArg-1 ){
8920          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8921          rc = 1;
8922        }else{
8923          oomRepeat = (int)integerValue(azArg[++i]);
8924        }
8925      }else if( IsDigit(z[0]) ){
8926        oomCounter = (int)integerValue(azArg[i]);
8927      }else{
8928        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8929        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8930        rc = 1;
8931      }
8932    }
8933    if( rc==0 ){
8934      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8935      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8936    }
8937  }else
8938#endif /* SQLITE_DEBUG */
8939
8940  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8941    char *zNewFilename = 0;  /* Name of the database file to open */
8942    int iName = 1;           /* Index in azArg[] of the filename */
8943    int newFlag = 0;         /* True to delete file before opening */
8944    int openMode = SHELL_OPEN_UNSPEC;
8945
8946    /* Check for command-line arguments */
8947    for(iName=1; iName<nArg; iName++){
8948      const char *z = azArg[iName];
8949      if( optionMatch(z,"new") ){
8950        newFlag = 1;
8951#ifdef SQLITE_HAVE_ZLIB
8952      }else if( optionMatch(z, "zip") ){
8953        openMode = SHELL_OPEN_ZIPFILE;
8954#endif
8955      }else if( optionMatch(z, "append") ){
8956        openMode = SHELL_OPEN_APPENDVFS;
8957      }else if( optionMatch(z, "readonly") ){
8958        openMode = SHELL_OPEN_READONLY;
8959      }else if( optionMatch(z, "nofollow") ){
8960        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8961#ifndef SQLITE_OMIT_DESERIALIZE
8962      }else if( optionMatch(z, "deserialize") ){
8963        openMode = SHELL_OPEN_DESERIALIZE;
8964      }else if( optionMatch(z, "hexdb") ){
8965        openMode = SHELL_OPEN_HEXDB;
8966      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8967        p->szMax = integerValue(azArg[++iName]);
8968#endif /* SQLITE_OMIT_DESERIALIZE */
8969      }else if( z[0]=='-' ){
8970        utf8_printf(stderr, "unknown option: %s\n", z);
8971        rc = 1;
8972        goto meta_command_exit;
8973      }else if( zNewFilename ){
8974        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
8975        rc = 1;
8976        goto meta_command_exit;
8977      }else{
8978        zNewFilename = sqlite3_mprintf("%s", z);
8979      }
8980    }
8981
8982    /* Close the existing database */
8983    session_close_all(p, -1);
8984    close_db(p->db);
8985    p->db = 0;
8986    p->pAuxDb->zDbFilename = 0;
8987    sqlite3_free(p->pAuxDb->zFreeOnClose);
8988    p->pAuxDb->zFreeOnClose = 0;
8989    p->openMode = openMode;
8990    p->openFlags = 0;
8991    p->szMax = 0;
8992
8993    /* If a filename is specified, try to open it first */
8994    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8995      if( newFlag && !p->bSafeMode ) shellDeleteFile(zNewFilename);
8996      if( p->bSafeMode
8997       && p->openMode!=SHELL_OPEN_HEXDB
8998       && zNewFilename
8999       && strcmp(zNewFilename,":memory:")!=0
9000      ){
9001        failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9002      }
9003      p->pAuxDb->zDbFilename = zNewFilename;
9004      open_db(p, OPEN_DB_KEEPALIVE);
9005      if( p->db==0 ){
9006        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9007        sqlite3_free(zNewFilename);
9008      }else{
9009        p->pAuxDb->zFreeOnClose = zNewFilename;
9010      }
9011    }
9012    if( p->db==0 ){
9013      /* As a fall-back open a TEMP database */
9014      p->pAuxDb->zDbFilename = 0;
9015      open_db(p, 0);
9016    }
9017  }else
9018
9019  if( (c=='o'
9020        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
9021   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
9022  ){
9023    char *zFile = 0;
9024    int bTxtMode = 0;
9025    int i;
9026    int eMode = 0;
9027    int bBOM = 0;
9028    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
9029
9030    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9031    if( c=='e' ){
9032      eMode = 'x';
9033      bOnce = 2;
9034    }else if( strncmp(azArg[0],"once",n)==0 ){
9035      bOnce = 1;
9036    }
9037    for(i=1; i<nArg; i++){
9038      char *z = azArg[i];
9039      if( z[0]=='-' ){
9040        if( z[1]=='-' ) z++;
9041        if( strcmp(z,"-bom")==0 ){
9042          bBOM = 1;
9043        }else if( c!='e' && strcmp(z,"-x")==0 ){
9044          eMode = 'x';  /* spreadsheet */
9045        }else if( c!='e' && strcmp(z,"-e")==0 ){
9046          eMode = 'e';  /* text editor */
9047        }else{
9048          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
9049                      azArg[i]);
9050          showHelp(p->out, azArg[0]);
9051          rc = 1;
9052          goto meta_command_exit;
9053        }
9054      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9055        zFile = sqlite3_mprintf("%s", z);
9056        if( zFile[0]=='|' ){
9057          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9058          break;
9059        }
9060      }else{
9061        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
9062                    azArg[i]);
9063        showHelp(p->out, azArg[0]);
9064        rc = 1;
9065        sqlite3_free(zFile);
9066        goto meta_command_exit;
9067      }
9068    }
9069    if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
9070    if( bOnce ){
9071      p->outCount = 2;
9072    }else{
9073      p->outCount = 0;
9074    }
9075    output_reset(p);
9076#ifndef SQLITE_NOHAVE_SYSTEM
9077    if( eMode=='e' || eMode=='x' ){
9078      p->doXdgOpen = 1;
9079      outputModePush(p);
9080      if( eMode=='x' ){
9081        /* spreadsheet mode.  Output as CSV. */
9082        newTempFile(p, "csv");
9083        ShellClearFlag(p, SHFLG_Echo);
9084        p->mode = MODE_Csv;
9085        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9086        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9087      }else{
9088        /* text editor mode */
9089        newTempFile(p, "txt");
9090        bTxtMode = 1;
9091      }
9092      sqlite3_free(zFile);
9093      zFile = sqlite3_mprintf("%s", p->zTempFile);
9094    }
9095#endif /* SQLITE_NOHAVE_SYSTEM */
9096    if( zFile[0]=='|' ){
9097#ifdef SQLITE_OMIT_POPEN
9098      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9099      rc = 1;
9100      p->out = stdout;
9101#else
9102      p->out = popen(zFile + 1, "w");
9103      if( p->out==0 ){
9104        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9105        p->out = stdout;
9106        rc = 1;
9107      }else{
9108        if( bBOM ) fprintf(p->out,"\357\273\277");
9109        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9110      }
9111#endif
9112    }else{
9113      p->out = output_file_open(zFile, bTxtMode);
9114      if( p->out==0 ){
9115        if( strcmp(zFile,"off")!=0 ){
9116          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9117        }
9118        p->out = stdout;
9119        rc = 1;
9120      } else {
9121        if( bBOM ) fprintf(p->out,"\357\273\277");
9122        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9123      }
9124    }
9125    sqlite3_free(zFile);
9126  }else
9127
9128  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
9129    open_db(p,0);
9130    if( nArg<=1 ) goto parameter_syntax_error;
9131
9132    /* .parameter clear
9133    ** Clear all bind parameters by dropping the TEMP table that holds them.
9134    */
9135    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
9136      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9137                   0, 0, 0);
9138    }else
9139
9140    /* .parameter list
9141    ** List all bind parameters.
9142    */
9143    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
9144      sqlite3_stmt *pStmt = 0;
9145      int rx;
9146      int len = 0;
9147      rx = sqlite3_prepare_v2(p->db,
9148             "SELECT max(length(key)) "
9149             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9150      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9151        len = sqlite3_column_int(pStmt, 0);
9152        if( len>40 ) len = 40;
9153      }
9154      sqlite3_finalize(pStmt);
9155      pStmt = 0;
9156      if( len ){
9157        rx = sqlite3_prepare_v2(p->db,
9158             "SELECT key, quote(value) "
9159             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9160        while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9161          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9162                      sqlite3_column_text(pStmt,1));
9163        }
9164        sqlite3_finalize(pStmt);
9165      }
9166    }else
9167
9168    /* .parameter init
9169    ** Make sure the TEMP table used to hold bind parameters exists.
9170    ** Create it if necessary.
9171    */
9172    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
9173      bind_table_init(p);
9174    }else
9175
9176    /* .parameter set NAME VALUE
9177    ** Set or reset a bind parameter.  NAME should be the full parameter
9178    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
9179    ** VALUE can be in either SQL literal notation, or if not it will be
9180    ** understood to be a text string.
9181    */
9182    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
9183      int rx;
9184      char *zSql;
9185      sqlite3_stmt *pStmt;
9186      const char *zKey = azArg[2];
9187      const char *zValue = azArg[3];
9188      bind_table_init(p);
9189      zSql = sqlite3_mprintf(
9190                  "REPLACE INTO temp.sqlite_parameters(key,value)"
9191                  "VALUES(%Q,%s);", zKey, zValue);
9192      if( zSql==0 ) shell_out_of_memory();
9193      pStmt = 0;
9194      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9195      sqlite3_free(zSql);
9196      if( rx!=SQLITE_OK ){
9197        sqlite3_finalize(pStmt);
9198        pStmt = 0;
9199        zSql = sqlite3_mprintf(
9200                   "REPLACE INTO temp.sqlite_parameters(key,value)"
9201                   "VALUES(%Q,%Q);", zKey, zValue);
9202        if( zSql==0 ) shell_out_of_memory();
9203        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9204        sqlite3_free(zSql);
9205        if( rx!=SQLITE_OK ){
9206          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9207          sqlite3_finalize(pStmt);
9208          pStmt = 0;
9209          rc = 1;
9210        }
9211      }
9212      sqlite3_step(pStmt);
9213      sqlite3_finalize(pStmt);
9214    }else
9215
9216    /* .parameter unset NAME
9217    ** Remove the NAME binding from the parameter binding table, if it
9218    ** exists.
9219    */
9220    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
9221      char *zSql = sqlite3_mprintf(
9222          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9223      if( zSql==0 ) shell_out_of_memory();
9224      sqlite3_exec(p->db, zSql, 0, 0, 0);
9225      sqlite3_free(zSql);
9226    }else
9227    /* If no command name matches, show a syntax error */
9228    parameter_syntax_error:
9229    showHelp(p->out, "parameter");
9230  }else
9231
9232  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
9233    int i;
9234    for(i=1; i<nArg; i++){
9235      if( i>1 ) raw_printf(p->out, " ");
9236      utf8_printf(p->out, "%s", azArg[i]);
9237    }
9238    raw_printf(p->out, "\n");
9239  }else
9240
9241#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9242  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
9243    int i;
9244    int nn = 0;
9245    p->flgProgress = 0;
9246    p->mxProgress = 0;
9247    p->nProgress = 0;
9248    for(i=1; i<nArg; i++){
9249      const char *z = azArg[i];
9250      if( z[0]=='-' ){
9251        z++;
9252        if( z[0]=='-' ) z++;
9253        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
9254          p->flgProgress |= SHELL_PROGRESS_QUIET;
9255          continue;
9256        }
9257        if( strcmp(z,"reset")==0 ){
9258          p->flgProgress |= SHELL_PROGRESS_RESET;
9259          continue;
9260        }
9261        if( strcmp(z,"once")==0 ){
9262          p->flgProgress |= SHELL_PROGRESS_ONCE;
9263          continue;
9264        }
9265        if( strcmp(z,"limit")==0 ){
9266          if( i+1>=nArg ){
9267            utf8_printf(stderr, "Error: missing argument on --limit\n");
9268            rc = 1;
9269            goto meta_command_exit;
9270          }else{
9271            p->mxProgress = (int)integerValue(azArg[++i]);
9272          }
9273          continue;
9274        }
9275        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9276        rc = 1;
9277        goto meta_command_exit;
9278      }else{
9279        nn = (int)integerValue(z);
9280      }
9281    }
9282    open_db(p, 0);
9283    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9284  }else
9285#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9286
9287  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9288    if( nArg >= 2) {
9289      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9290    }
9291    if( nArg >= 3) {
9292      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9293    }
9294  }else
9295
9296  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
9297    rc = 2;
9298  }else
9299
9300  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
9301    FILE *inSaved = p->in;
9302    int savedLineno = p->lineno;
9303    failIfSafeMode(p, "cannot run .read in safe mode");
9304    if( nArg!=2 ){
9305      raw_printf(stderr, "Usage: .read FILE\n");
9306      rc = 1;
9307      goto meta_command_exit;
9308    }
9309    if( azArg[1][0]=='|' ){
9310#ifdef SQLITE_OMIT_POPEN
9311      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9312      rc = 1;
9313      p->out = stdout;
9314#else
9315      p->in = popen(azArg[1]+1, "r");
9316      if( p->in==0 ){
9317        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9318        rc = 1;
9319      }else{
9320        rc = process_input(p);
9321        pclose(p->in);
9322      }
9323#endif
9324    }else if( (p->in = openChrSource(azArg[1]))==0 ){
9325      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9326      rc = 1;
9327    }else{
9328      rc = process_input(p);
9329      fclose(p->in);
9330    }
9331    p->in = inSaved;
9332    p->lineno = savedLineno;
9333  }else
9334
9335  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9336    const char *zSrcFile;
9337    const char *zDb;
9338    sqlite3 *pSrc;
9339    sqlite3_backup *pBackup;
9340    int nTimeout = 0;
9341
9342    failIfSafeMode(p, "cannot run .restore in safe mode");
9343    if( nArg==2 ){
9344      zSrcFile = azArg[1];
9345      zDb = "main";
9346    }else if( nArg==3 ){
9347      zSrcFile = azArg[2];
9348      zDb = azArg[1];
9349    }else{
9350      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9351      rc = 1;
9352      goto meta_command_exit;
9353    }
9354    rc = sqlite3_open(zSrcFile, &pSrc);
9355    if( rc!=SQLITE_OK ){
9356      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9357      close_db(pSrc);
9358      return 1;
9359    }
9360    open_db(p, 0);
9361    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9362    if( pBackup==0 ){
9363      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9364      close_db(pSrc);
9365      return 1;
9366    }
9367    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9368          || rc==SQLITE_BUSY  ){
9369      if( rc==SQLITE_BUSY ){
9370        if( nTimeout++ >= 3 ) break;
9371        sqlite3_sleep(100);
9372      }
9373    }
9374    sqlite3_backup_finish(pBackup);
9375    if( rc==SQLITE_DONE ){
9376      rc = 0;
9377    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9378      raw_printf(stderr, "Error: source database is busy\n");
9379      rc = 1;
9380    }else{
9381      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9382      rc = 1;
9383    }
9384    close_db(pSrc);
9385  }else
9386
9387  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9388    if( nArg==2 ){
9389      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9390#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9391      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9392#endif
9393    }else{
9394      raw_printf(stderr, "Usage: .scanstats on|off\n");
9395      rc = 1;
9396    }
9397  }else
9398
9399  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9400    ShellText sSelect;
9401    ShellState data;
9402    char *zErrMsg = 0;
9403    const char *zDiv = "(";
9404    const char *zName = 0;
9405    int iSchema = 0;
9406    int bDebug = 0;
9407    int bNoSystemTabs = 0;
9408    int ii;
9409
9410    open_db(p, 0);
9411    memcpy(&data, p, sizeof(data));
9412    data.showHeader = 0;
9413    data.cMode = data.mode = MODE_Semi;
9414    initText(&sSelect);
9415    for(ii=1; ii<nArg; ii++){
9416      if( optionMatch(azArg[ii],"indent") ){
9417        data.cMode = data.mode = MODE_Pretty;
9418      }else if( optionMatch(azArg[ii],"debug") ){
9419        bDebug = 1;
9420      }else if( optionMatch(azArg[ii],"nosys") ){
9421        bNoSystemTabs = 1;
9422      }else if( azArg[ii][0]=='-' ){
9423        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9424        rc = 1;
9425        goto meta_command_exit;
9426      }else if( zName==0 ){
9427        zName = azArg[ii];
9428      }else{
9429        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9430        rc = 1;
9431        goto meta_command_exit;
9432      }
9433    }
9434    if( zName!=0 ){
9435      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9436                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9437                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9438                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9439      if( isSchema ){
9440        char *new_argv[2], *new_colv[2];
9441        new_argv[0] = sqlite3_mprintf(
9442                      "CREATE TABLE %s (\n"
9443                      "  type text,\n"
9444                      "  name text,\n"
9445                      "  tbl_name text,\n"
9446                      "  rootpage integer,\n"
9447                      "  sql text\n"
9448                      ")", zName);
9449        new_argv[1] = 0;
9450        new_colv[0] = "sql";
9451        new_colv[1] = 0;
9452        callback(&data, 1, new_argv, new_colv);
9453        sqlite3_free(new_argv[0]);
9454      }
9455    }
9456    if( zDiv ){
9457      sqlite3_stmt *pStmt = 0;
9458      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9459                              -1, &pStmt, 0);
9460      if( rc ){
9461        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9462        sqlite3_finalize(pStmt);
9463        rc = 1;
9464        goto meta_command_exit;
9465      }
9466      appendText(&sSelect, "SELECT sql FROM", 0);
9467      iSchema = 0;
9468      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9469        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9470        char zScNum[30];
9471        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9472        appendText(&sSelect, zDiv, 0);
9473        zDiv = " UNION ALL ";
9474        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9475        if( sqlite3_stricmp(zDb, "main")!=0 ){
9476          appendText(&sSelect, zDb, '\'');
9477        }else{
9478          appendText(&sSelect, "NULL", 0);
9479        }
9480        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9481        appendText(&sSelect, zScNum, 0);
9482        appendText(&sSelect, " AS snum, ", 0);
9483        appendText(&sSelect, zDb, '\'');
9484        appendText(&sSelect, " AS sname FROM ", 0);
9485        appendText(&sSelect, zDb, quoteChar(zDb));
9486        appendText(&sSelect, ".sqlite_schema", 0);
9487      }
9488      sqlite3_finalize(pStmt);
9489#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9490      if( zName ){
9491        appendText(&sSelect,
9492           " UNION ALL SELECT shell_module_schema(name),"
9493           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9494        0);
9495      }
9496#endif
9497      appendText(&sSelect, ") WHERE ", 0);
9498      if( zName ){
9499        char *zQarg = sqlite3_mprintf("%Q", zName);
9500        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9501                    strchr(zName, '[') != 0;
9502        if( strchr(zName, '.') ){
9503          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9504        }else{
9505          appendText(&sSelect, "lower(tbl_name)", 0);
9506        }
9507        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9508        appendText(&sSelect, zQarg, 0);
9509        if( !bGlob ){
9510          appendText(&sSelect, " ESCAPE '\\' ", 0);
9511        }
9512        appendText(&sSelect, " AND ", 0);
9513        sqlite3_free(zQarg);
9514      }
9515      if( bNoSystemTabs ){
9516        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9517      }
9518      appendText(&sSelect, "sql IS NOT NULL"
9519                           " ORDER BY snum, rowid", 0);
9520      if( bDebug ){
9521        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9522      }else{
9523        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9524      }
9525      freeText(&sSelect);
9526    }
9527    if( zErrMsg ){
9528      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9529      sqlite3_free(zErrMsg);
9530      rc = 1;
9531    }else if( rc != SQLITE_OK ){
9532      raw_printf(stderr,"Error: querying schema information\n");
9533      rc = 1;
9534    }else{
9535      rc = 0;
9536    }
9537  }else
9538
9539  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9540    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
9541    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
9542  }else
9543
9544#if defined(SQLITE_ENABLE_SESSION)
9545  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9546    struct AuxDb *pAuxDb = p->pAuxDb;
9547    OpenSession *pSession = &pAuxDb->aSession[0];
9548    char **azCmd = &azArg[1];
9549    int iSes = 0;
9550    int nCmd = nArg - 1;
9551    int i;
9552    if( nArg<=1 ) goto session_syntax_error;
9553    open_db(p, 0);
9554    if( nArg>=3 ){
9555      for(iSes=0; iSes<pAuxDb->nSession; iSes++){
9556        if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
9557      }
9558      if( iSes<pAuxDb->nSession ){
9559        pSession = &pAuxDb->aSession[iSes];
9560        azCmd++;
9561        nCmd--;
9562      }else{
9563        pSession = &pAuxDb->aSession[0];
9564        iSes = 0;
9565      }
9566    }
9567
9568    /* .session attach TABLE
9569    ** Invoke the sqlite3session_attach() interface to attach a particular
9570    ** table so that it is never filtered.
9571    */
9572    if( strcmp(azCmd[0],"attach")==0 ){
9573      if( nCmd!=2 ) goto session_syntax_error;
9574      if( pSession->p==0 ){
9575        session_not_open:
9576        raw_printf(stderr, "ERROR: No sessions are open\n");
9577      }else{
9578        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9579        if( rc ){
9580          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9581          rc = 0;
9582        }
9583      }
9584    }else
9585
9586    /* .session changeset FILE
9587    ** .session patchset FILE
9588    ** Write a changeset or patchset into a file.  The file is overwritten.
9589    */
9590    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9591      FILE *out = 0;
9592      failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
9593      if( nCmd!=2 ) goto session_syntax_error;
9594      if( pSession->p==0 ) goto session_not_open;
9595      out = fopen(azCmd[1], "wb");
9596      if( out==0 ){
9597        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9598                    azCmd[1]);
9599      }else{
9600        int szChng;
9601        void *pChng;
9602        if( azCmd[0][0]=='c' ){
9603          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9604        }else{
9605          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9606        }
9607        if( rc ){
9608          printf("Error: error code %d\n", rc);
9609          rc = 0;
9610        }
9611        if( pChng
9612          && fwrite(pChng, szChng, 1, out)!=1 ){
9613          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9614                  szChng);
9615        }
9616        sqlite3_free(pChng);
9617        fclose(out);
9618      }
9619    }else
9620
9621    /* .session close
9622    ** Close the identified session
9623    */
9624    if( strcmp(azCmd[0], "close")==0 ){
9625      if( nCmd!=1 ) goto session_syntax_error;
9626      if( pAuxDb->nSession ){
9627        session_close(pSession);
9628        pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
9629      }
9630    }else
9631
9632    /* .session enable ?BOOLEAN?
9633    ** Query or set the enable flag
9634    */
9635    if( strcmp(azCmd[0], "enable")==0 ){
9636      int ii;
9637      if( nCmd>2 ) goto session_syntax_error;
9638      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9639      if( pAuxDb->nSession ){
9640        ii = sqlite3session_enable(pSession->p, ii);
9641        utf8_printf(p->out, "session %s enable flag = %d\n",
9642                    pSession->zName, ii);
9643      }
9644    }else
9645
9646    /* .session filter GLOB ....
9647    ** Set a list of GLOB patterns of table names to be excluded.
9648    */
9649    if( strcmp(azCmd[0], "filter")==0 ){
9650      int ii, nByte;
9651      if( nCmd<2 ) goto session_syntax_error;
9652      if( pAuxDb->nSession ){
9653        for(ii=0; ii<pSession->nFilter; ii++){
9654          sqlite3_free(pSession->azFilter[ii]);
9655        }
9656        sqlite3_free(pSession->azFilter);
9657        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9658        pSession->azFilter = sqlite3_malloc( nByte );
9659        if( pSession->azFilter==0 ){
9660          raw_printf(stderr, "Error: out or memory\n");
9661          exit(1);
9662        }
9663        for(ii=1; ii<nCmd; ii++){
9664          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9665        }
9666        pSession->nFilter = ii-1;
9667      }
9668    }else
9669
9670    /* .session indirect ?BOOLEAN?
9671    ** Query or set the indirect flag
9672    */
9673    if( strcmp(azCmd[0], "indirect")==0 ){
9674      int ii;
9675      if( nCmd>2 ) goto session_syntax_error;
9676      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9677      if( pAuxDb->nSession ){
9678        ii = sqlite3session_indirect(pSession->p, ii);
9679        utf8_printf(p->out, "session %s indirect flag = %d\n",
9680                    pSession->zName, ii);
9681      }
9682    }else
9683
9684    /* .session isempty
9685    ** Determine if the session is empty
9686    */
9687    if( strcmp(azCmd[0], "isempty")==0 ){
9688      int ii;
9689      if( nCmd!=1 ) goto session_syntax_error;
9690      if( pAuxDb->nSession ){
9691        ii = sqlite3session_isempty(pSession->p);
9692        utf8_printf(p->out, "session %s isempty flag = %d\n",
9693                    pSession->zName, ii);
9694      }
9695    }else
9696
9697    /* .session list
9698    ** List all currently open sessions
9699    */
9700    if( strcmp(azCmd[0],"list")==0 ){
9701      for(i=0; i<pAuxDb->nSession; i++){
9702        utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
9703      }
9704    }else
9705
9706    /* .session open DB NAME
9707    ** Open a new session called NAME on the attached database DB.
9708    ** DB is normally "main".
9709    */
9710    if( strcmp(azCmd[0],"open")==0 ){
9711      char *zName;
9712      if( nCmd!=3 ) goto session_syntax_error;
9713      zName = azCmd[2];
9714      if( zName[0]==0 ) goto session_syntax_error;
9715      for(i=0; i<pAuxDb->nSession; i++){
9716        if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
9717          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9718          goto meta_command_exit;
9719        }
9720      }
9721      if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
9722        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
9723        goto meta_command_exit;
9724      }
9725      pSession = &pAuxDb->aSession[pAuxDb->nSession];
9726      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9727      if( rc ){
9728        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9729        rc = 0;
9730        goto meta_command_exit;
9731      }
9732      pSession->nFilter = 0;
9733      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9734      pAuxDb->nSession++;
9735      pSession->zName = sqlite3_mprintf("%s", zName);
9736    }else
9737    /* If no command name matches, show a syntax error */
9738    session_syntax_error:
9739    showHelp(p->out, "session");
9740  }else
9741#endif
9742
9743#ifdef SQLITE_DEBUG
9744  /* Undocumented commands for internal testing.  Subject to change
9745  ** without notice. */
9746  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9747    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9748      int i, v;
9749      for(i=1; i<nArg; i++){
9750        v = booleanValue(azArg[i]);
9751        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9752      }
9753    }
9754    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9755      int i; sqlite3_int64 v;
9756      for(i=1; i<nArg; i++){
9757        char zBuf[200];
9758        v = integerValue(azArg[i]);
9759        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9760        utf8_printf(p->out, "%s", zBuf);
9761      }
9762    }
9763  }else
9764#endif
9765
9766  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9767    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9768    int bVerbose = 0;        /* Verbose output */
9769    int bSelftestExists;     /* True if SELFTEST already exists */
9770    int i, k;                /* Loop counters */
9771    int nTest = 0;           /* Number of tests runs */
9772    int nErr = 0;            /* Number of errors seen */
9773    ShellText str;           /* Answer for a query */
9774    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9775
9776    open_db(p,0);
9777    for(i=1; i<nArg; i++){
9778      const char *z = azArg[i];
9779      if( z[0]=='-' && z[1]=='-' ) z++;
9780      if( strcmp(z,"-init")==0 ){
9781        bIsInit = 1;
9782      }else
9783      if( strcmp(z,"-v")==0 ){
9784        bVerbose++;
9785      }else
9786      {
9787        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9788                    azArg[i], azArg[0]);
9789        raw_printf(stderr, "Should be one of: --init -v\n");
9790        rc = 1;
9791        goto meta_command_exit;
9792      }
9793    }
9794    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9795           != SQLITE_OK ){
9796      bSelftestExists = 0;
9797    }else{
9798      bSelftestExists = 1;
9799    }
9800    if( bIsInit ){
9801      createSelftestTable(p);
9802      bSelftestExists = 1;
9803    }
9804    initText(&str);
9805    appendText(&str, "x", 0);
9806    for(k=bSelftestExists; k>=0; k--){
9807      if( k==1 ){
9808        rc = sqlite3_prepare_v2(p->db,
9809            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9810            -1, &pStmt, 0);
9811      }else{
9812        rc = sqlite3_prepare_v2(p->db,
9813          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9814          "      (1,'run','PRAGMA integrity_check','ok')",
9815          -1, &pStmt, 0);
9816      }
9817      if( rc ){
9818        raw_printf(stderr, "Error querying the selftest table\n");
9819        rc = 1;
9820        sqlite3_finalize(pStmt);
9821        goto meta_command_exit;
9822      }
9823      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9824        int tno = sqlite3_column_int(pStmt, 0);
9825        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9826        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9827        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9828
9829        k = 0;
9830        if( bVerbose>0 ){
9831          char *zQuote = sqlite3_mprintf("%q", zSql);
9832          printf("%d: %s %s\n", tno, zOp, zSql);
9833          sqlite3_free(zQuote);
9834        }
9835        if( strcmp(zOp,"memo")==0 ){
9836          utf8_printf(p->out, "%s\n", zSql);
9837        }else
9838        if( strcmp(zOp,"run")==0 ){
9839          char *zErrMsg = 0;
9840          str.n = 0;
9841          str.z[0] = 0;
9842          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9843          nTest++;
9844          if( bVerbose ){
9845            utf8_printf(p->out, "Result: %s\n", str.z);
9846          }
9847          if( rc || zErrMsg ){
9848            nErr++;
9849            rc = 1;
9850            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9851            sqlite3_free(zErrMsg);
9852          }else if( strcmp(zAns,str.z)!=0 ){
9853            nErr++;
9854            rc = 1;
9855            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9856            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9857          }
9858        }else
9859        {
9860          utf8_printf(stderr,
9861            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9862          rc = 1;
9863          break;
9864        }
9865      } /* End loop over rows of content from SELFTEST */
9866      sqlite3_finalize(pStmt);
9867    } /* End loop over k */
9868    freeText(&str);
9869    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9870  }else
9871
9872  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9873    if( nArg<2 || nArg>3 ){
9874      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9875      rc = 1;
9876    }
9877    if( nArg>=2 ){
9878      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9879                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9880    }
9881    if( nArg>=3 ){
9882      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9883                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9884    }
9885  }else
9886
9887  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9888    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9889    int i;                   /* Loop counter */
9890    int bSchema = 0;         /* Also hash the schema */
9891    int bSeparate = 0;       /* Hash each table separately */
9892    int iSize = 224;         /* Hash algorithm to use */
9893    int bDebug = 0;          /* Only show the query that would have run */
9894    sqlite3_stmt *pStmt;     /* For querying tables names */
9895    char *zSql;              /* SQL to be run */
9896    char *zSep;              /* Separator */
9897    ShellText sSql;          /* Complete SQL for the query to run the hash */
9898    ShellText sQuery;        /* Set of queries used to read all content */
9899    open_db(p, 0);
9900    for(i=1; i<nArg; i++){
9901      const char *z = azArg[i];
9902      if( z[0]=='-' ){
9903        z++;
9904        if( z[0]=='-' ) z++;
9905        if( strcmp(z,"schema")==0 ){
9906          bSchema = 1;
9907        }else
9908        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9909         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9910        ){
9911          iSize = atoi(&z[5]);
9912        }else
9913        if( strcmp(z,"debug")==0 ){
9914          bDebug = 1;
9915        }else
9916        {
9917          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9918                      azArg[i], azArg[0]);
9919          showHelp(p->out, azArg[0]);
9920          rc = 1;
9921          goto meta_command_exit;
9922        }
9923      }else if( zLike ){
9924        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9925        rc = 1;
9926        goto meta_command_exit;
9927      }else{
9928        zLike = z;
9929        bSeparate = 1;
9930        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9931      }
9932    }
9933    if( bSchema ){
9934      zSql = "SELECT lower(name) FROM sqlite_schema"
9935             " WHERE type='table' AND coalesce(rootpage,0)>1"
9936             " UNION ALL SELECT 'sqlite_schema'"
9937             " ORDER BY 1 collate nocase";
9938    }else{
9939      zSql = "SELECT lower(name) FROM sqlite_schema"
9940             " WHERE type='table' AND coalesce(rootpage,0)>1"
9941             " AND name NOT LIKE 'sqlite_%'"
9942             " ORDER BY 1 collate nocase";
9943    }
9944    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9945    initText(&sQuery);
9946    initText(&sSql);
9947    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9948    zSep = "VALUES(";
9949    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9950      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9951      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9952      if( strncmp(zTab, "sqlite_",7)!=0 ){
9953        appendText(&sQuery,"SELECT * FROM ", 0);
9954        appendText(&sQuery,zTab,'"');
9955        appendText(&sQuery," NOT INDEXED;", 0);
9956      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9957        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9958                           " ORDER BY name;", 0);
9959      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9960        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9961                           " ORDER BY name;", 0);
9962      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9963        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9964                           " ORDER BY tbl,idx;", 0);
9965      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9966        appendText(&sQuery, "SELECT * FROM ", 0);
9967        appendText(&sQuery, zTab, 0);
9968        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9969      }
9970      appendText(&sSql, zSep, 0);
9971      appendText(&sSql, sQuery.z, '\'');
9972      sQuery.n = 0;
9973      appendText(&sSql, ",", 0);
9974      appendText(&sSql, zTab, '\'');
9975      zSep = "),(";
9976    }
9977    sqlite3_finalize(pStmt);
9978    if( bSeparate ){
9979      zSql = sqlite3_mprintf(
9980          "%s))"
9981          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9982          "   FROM [sha3sum$query]",
9983          sSql.z, iSize);
9984    }else{
9985      zSql = sqlite3_mprintf(
9986          "%s))"
9987          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9988          "   FROM [sha3sum$query]",
9989          sSql.z, iSize);
9990    }
9991    freeText(&sQuery);
9992    freeText(&sSql);
9993    if( bDebug ){
9994      utf8_printf(p->out, "%s\n", zSql);
9995    }else{
9996      shell_exec(p, zSql, 0);
9997    }
9998    sqlite3_free(zSql);
9999  }else
10000
10001#ifndef SQLITE_NOHAVE_SYSTEM
10002  if( c=='s'
10003   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
10004  ){
10005    char *zCmd;
10006    int i, x;
10007    failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10008    if( nArg<2 ){
10009      raw_printf(stderr, "Usage: .system COMMAND\n");
10010      rc = 1;
10011      goto meta_command_exit;
10012    }
10013    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10014    for(i=2; i<nArg; i++){
10015      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10016                             zCmd, azArg[i]);
10017    }
10018    x = system(zCmd);
10019    sqlite3_free(zCmd);
10020    if( x ) raw_printf(stderr, "System command returns %d\n", x);
10021  }else
10022#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
10023
10024  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
10025    static const char *azBool[] = { "off", "on", "trigger", "full"};
10026    const char *zOut;
10027    int i;
10028    if( nArg!=1 ){
10029      raw_printf(stderr, "Usage: .show\n");
10030      rc = 1;
10031      goto meta_command_exit;
10032    }
10033    utf8_printf(p->out, "%12.12s: %s\n","echo",
10034                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
10035    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10036    utf8_printf(p->out, "%12.12s: %s\n","explain",
10037         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10038    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10039    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10040    utf8_printf(p->out, "%12.12s: ", "nullvalue");
10041      output_c_string(p->out, p->nullValue);
10042      raw_printf(p->out, "\n");
10043    utf8_printf(p->out,"%12.12s: %s\n","output",
10044            strlen30(p->outfile) ? p->outfile : "stdout");
10045    utf8_printf(p->out,"%12.12s: ", "colseparator");
10046      output_c_string(p->out, p->colSeparator);
10047      raw_printf(p->out, "\n");
10048    utf8_printf(p->out,"%12.12s: ", "rowseparator");
10049      output_c_string(p->out, p->rowSeparator);
10050      raw_printf(p->out, "\n");
10051    switch( p->statsOn ){
10052      case 0:  zOut = "off";     break;
10053      default: zOut = "on";      break;
10054      case 2:  zOut = "stmt";    break;
10055      case 3:  zOut = "vmstep";  break;
10056    }
10057    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10058    utf8_printf(p->out, "%12.12s: ", "width");
10059    for (i=0;i<p->nWidth;i++) {
10060      raw_printf(p->out, "%d ", p->colWidth[i]);
10061    }
10062    raw_printf(p->out, "\n");
10063    utf8_printf(p->out, "%12.12s: %s\n", "filename",
10064                p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10065  }else
10066
10067  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
10068    if( nArg==2 ){
10069      if( strcmp(azArg[1],"stmt")==0 ){
10070        p->statsOn = 2;
10071      }else if( strcmp(azArg[1],"vmstep")==0 ){
10072        p->statsOn = 3;
10073      }else{
10074        p->statsOn = (u8)booleanValue(azArg[1]);
10075      }
10076    }else if( nArg==1 ){
10077      display_stats(p->db, p, 0);
10078    }else{
10079      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10080      rc = 1;
10081    }
10082  }else
10083
10084  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
10085   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
10086                 || strncmp(azArg[0], "indexes", n)==0) )
10087  ){
10088    sqlite3_stmt *pStmt;
10089    char **azResult;
10090    int nRow, nAlloc;
10091    int ii;
10092    ShellText s;
10093    initText(&s);
10094    open_db(p, 0);
10095    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10096    if( rc ){
10097      sqlite3_finalize(pStmt);
10098      return shellDatabaseError(p->db);
10099    }
10100
10101    if( nArg>2 && c=='i' ){
10102      /* It is an historical accident that the .indexes command shows an error
10103      ** when called with the wrong number of arguments whereas the .tables
10104      ** command does not. */
10105      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10106      rc = 1;
10107      sqlite3_finalize(pStmt);
10108      goto meta_command_exit;
10109    }
10110    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10111      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10112      if( zDbName==0 ) continue;
10113      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10114      if( sqlite3_stricmp(zDbName, "main")==0 ){
10115        appendText(&s, "SELECT name FROM ", 0);
10116      }else{
10117        appendText(&s, "SELECT ", 0);
10118        appendText(&s, zDbName, '\'');
10119        appendText(&s, "||'.'||name FROM ", 0);
10120      }
10121      appendText(&s, zDbName, '"');
10122      appendText(&s, ".sqlite_schema ", 0);
10123      if( c=='t' ){
10124        appendText(&s," WHERE type IN ('table','view')"
10125                      "   AND name NOT LIKE 'sqlite_%'"
10126                      "   AND name LIKE ?1", 0);
10127      }else{
10128        appendText(&s," WHERE type='index'"
10129                      "   AND tbl_name LIKE ?1", 0);
10130      }
10131    }
10132    rc = sqlite3_finalize(pStmt);
10133    if( rc==SQLITE_OK ){
10134      appendText(&s, " ORDER BY 1", 0);
10135      rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10136    }
10137    freeText(&s);
10138    if( rc ) return shellDatabaseError(p->db);
10139
10140    /* Run the SQL statement prepared by the above block. Store the results
10141    ** as an array of nul-terminated strings in azResult[].  */
10142    nRow = nAlloc = 0;
10143    azResult = 0;
10144    if( nArg>1 ){
10145      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10146    }else{
10147      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10148    }
10149    while( sqlite3_step(pStmt)==SQLITE_ROW ){
10150      if( nRow>=nAlloc ){
10151        char **azNew;
10152        int n2 = nAlloc*2 + 10;
10153        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10154        if( azNew==0 ) shell_out_of_memory();
10155        nAlloc = n2;
10156        azResult = azNew;
10157      }
10158      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10159      if( 0==azResult[nRow] ) shell_out_of_memory();
10160      nRow++;
10161    }
10162    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10163      rc = shellDatabaseError(p->db);
10164    }
10165
10166    /* Pretty-print the contents of array azResult[] to the output */
10167    if( rc==0 && nRow>0 ){
10168      int len, maxlen = 0;
10169      int i, j;
10170      int nPrintCol, nPrintRow;
10171      for(i=0; i<nRow; i++){
10172        len = strlen30(azResult[i]);
10173        if( len>maxlen ) maxlen = len;
10174      }
10175      nPrintCol = 80/(maxlen+2);
10176      if( nPrintCol<1 ) nPrintCol = 1;
10177      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10178      for(i=0; i<nPrintRow; i++){
10179        for(j=i; j<nRow; j+=nPrintRow){
10180          char *zSp = j<nPrintRow ? "" : "  ";
10181          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10182                      azResult[j] ? azResult[j]:"");
10183        }
10184        raw_printf(p->out, "\n");
10185      }
10186    }
10187
10188    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10189    sqlite3_free(azResult);
10190  }else
10191
10192  /* Begin redirecting output to the file "testcase-out.txt" */
10193  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
10194    output_reset(p);
10195    p->out = output_file_open("testcase-out.txt", 0);
10196    if( p->out==0 ){
10197      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10198    }
10199    if( nArg>=2 ){
10200      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10201    }else{
10202      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10203    }
10204  }else
10205
10206#ifndef SQLITE_UNTESTABLE
10207  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
10208    static const struct {
10209       const char *zCtrlName;   /* Name of a test-control option */
10210       int ctrlCode;            /* Integer code for that option */
10211       int unSafe;              /* Not valid for --safe mode */
10212       const char *zUsage;      /* Usage notes */
10213    } aCtrl[] = {
10214      { "always",             SQLITE_TESTCTRL_ALWAYS, 1,     "BOOLEAN"         },
10215      { "assert",             SQLITE_TESTCTRL_ASSERT, 1,     "BOOLEAN"         },
10216    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, ""        },*/
10217    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST, 1,  ""              },*/
10218      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER, 0,  ""                },
10219      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN"  },
10220    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, 1,""              },*/
10221      { "imposter",         SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
10222      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,""          },
10223      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN"      },
10224      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN"       },
10225      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK"   },
10226#ifdef YYCOVERAGE
10227      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE,0,""             },
10228#endif
10229      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET  "       },
10230      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,0, ""               },
10231      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,   0, ""               },
10232      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,   0, "SEED ?db?"      },
10233      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,  0, ""               },
10234      { "sorter_mmap",        SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX"           },
10235      { "tune",               SQLITE_TESTCTRL_TUNE,        1, "ID VALUE"       },
10236    };
10237    int testctrl = -1;
10238    int iCtrl = -1;
10239    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
10240    int isOk = 0;
10241    int i, n2;
10242    const char *zCmd = 0;
10243
10244    open_db(p, 0);
10245    zCmd = nArg>=2 ? azArg[1] : "help";
10246
10247    /* The argument can optionally begin with "-" or "--" */
10248    if( zCmd[0]=='-' && zCmd[1] ){
10249      zCmd++;
10250      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10251    }
10252
10253    /* --help lists all test-controls */
10254    if( strcmp(zCmd,"help")==0 ){
10255      utf8_printf(p->out, "Available test-controls:\n");
10256      for(i=0; i<ArraySize(aCtrl); i++){
10257        utf8_printf(p->out, "  .testctrl %s %s\n",
10258                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10259      }
10260      rc = 1;
10261      goto meta_command_exit;
10262    }
10263
10264    /* convert testctrl text option to value. allow any unique prefix
10265    ** of the option name, or a numerical value. */
10266    n2 = strlen30(zCmd);
10267    for(i=0; i<ArraySize(aCtrl); i++){
10268      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10269        if( testctrl<0 ){
10270          testctrl = aCtrl[i].ctrlCode;
10271          iCtrl = i;
10272        }else{
10273          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
10274                              "Use \".testctrl --help\" for help\n", zCmd);
10275          rc = 1;
10276          goto meta_command_exit;
10277        }
10278      }
10279    }
10280    if( testctrl<0 ){
10281      utf8_printf(stderr,"Error: unknown test-control: %s\n"
10282                         "Use \".testctrl --help\" for help\n", zCmd);
10283    }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
10284      utf8_printf(stderr,
10285         "line %d: \".testctrl %s\" may not be used in safe mode\n",
10286         p->lineno, aCtrl[iCtrl].zCtrlName);
10287      exit(1);
10288    }else{
10289      switch(testctrl){
10290
10291        /* sqlite3_test_control(int, db, int) */
10292        case SQLITE_TESTCTRL_OPTIMIZATIONS:
10293          if( nArg==3 ){
10294            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
10295            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10296            isOk = 3;
10297          }
10298          break;
10299
10300        /* sqlite3_test_control(int) */
10301        case SQLITE_TESTCTRL_PRNG_SAVE:
10302        case SQLITE_TESTCTRL_PRNG_RESTORE:
10303        case SQLITE_TESTCTRL_BYTEORDER:
10304          if( nArg==2 ){
10305            rc2 = sqlite3_test_control(testctrl);
10306            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
10307          }
10308          break;
10309
10310        /* sqlite3_test_control(int, uint) */
10311        case SQLITE_TESTCTRL_PENDING_BYTE:
10312          if( nArg==3 ){
10313            unsigned int opt = (unsigned int)integerValue(azArg[2]);
10314            rc2 = sqlite3_test_control(testctrl, opt);
10315            isOk = 3;
10316          }
10317          break;
10318
10319        /* sqlite3_test_control(int, int, sqlite3*) */
10320        case SQLITE_TESTCTRL_PRNG_SEED:
10321          if( nArg==3 || nArg==4 ){
10322            int ii = (int)integerValue(azArg[2]);
10323            sqlite3 *db;
10324            if( ii==0 && strcmp(azArg[2],"random")==0 ){
10325              sqlite3_randomness(sizeof(ii),&ii);
10326              printf("-- random seed: %d\n", ii);
10327            }
10328            if( nArg==3 ){
10329              db = 0;
10330            }else{
10331              db = p->db;
10332              /* Make sure the schema has been loaded */
10333              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10334            }
10335            rc2 = sqlite3_test_control(testctrl, ii, db);
10336            isOk = 3;
10337          }
10338          break;
10339
10340        /* sqlite3_test_control(int, int) */
10341        case SQLITE_TESTCTRL_ASSERT:
10342        case SQLITE_TESTCTRL_ALWAYS:
10343          if( nArg==3 ){
10344            int opt = booleanValue(azArg[2]);
10345            rc2 = sqlite3_test_control(testctrl, opt);
10346            isOk = 1;
10347          }
10348          break;
10349
10350        /* sqlite3_test_control(int, int) */
10351        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10352        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10353          if( nArg==3 ){
10354            int opt = booleanValue(azArg[2]);
10355            rc2 = sqlite3_test_control(testctrl, opt);
10356            isOk = 3;
10357          }
10358          break;
10359
10360        /* sqlite3_test_control(sqlite3*) */
10361        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10362          rc2 = sqlite3_test_control(testctrl, p->db);
10363          isOk = 3;
10364          break;
10365
10366        case SQLITE_TESTCTRL_IMPOSTER:
10367          if( nArg==5 ){
10368            rc2 = sqlite3_test_control(testctrl, p->db,
10369                          azArg[2],
10370                          integerValue(azArg[3]),
10371                          integerValue(azArg[4]));
10372            isOk = 3;
10373          }
10374          break;
10375
10376        case SQLITE_TESTCTRL_SEEK_COUNT: {
10377          u64 x = 0;
10378          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10379          utf8_printf(p->out, "%llu\n", x);
10380          isOk = 3;
10381          break;
10382        }
10383
10384#ifdef YYCOVERAGE
10385        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
10386          if( nArg==2 ){
10387            sqlite3_test_control(testctrl, p->out);
10388            isOk = 3;
10389          }
10390          break;
10391        }
10392#endif
10393#ifdef SQLITE_DEBUG
10394        case SQLITE_TESTCTRL_TUNE: {
10395          if( nArg==4 ){
10396            int id = (int)integerValue(azArg[2]);
10397            int val = (int)integerValue(azArg[3]);
10398            sqlite3_test_control(testctrl, id, &val);
10399            isOk = 3;
10400          }else if( nArg==3 ){
10401            int id = (int)integerValue(azArg[2]);
10402            sqlite3_test_control(testctrl, -id, &rc2);
10403            isOk = 1;
10404          }else if( nArg==2 ){
10405            int id = 1;
10406            while(1){
10407              int val = 0;
10408              rc2 = sqlite3_test_control(testctrl, -id, &val);
10409              if( rc2!=SQLITE_OK ) break;
10410              if( id>1 ) utf8_printf(p->out, "  ");
10411              utf8_printf(p->out, "%d: %d", id, val);
10412              id++;
10413            }
10414            if( id>1 ) utf8_printf(p->out, "\n");
10415            isOk = 3;
10416          }
10417          break;
10418        }
10419#endif
10420        case SQLITE_TESTCTRL_SORTER_MMAP:
10421          if( nArg==3 ){
10422            int opt = (unsigned int)integerValue(azArg[2]);
10423            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10424            isOk = 3;
10425          }
10426          break;
10427      }
10428    }
10429    if( isOk==0 && iCtrl>=0 ){
10430      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10431      rc = 1;
10432    }else if( isOk==1 ){
10433      raw_printf(p->out, "%d\n", rc2);
10434    }else if( isOk==2 ){
10435      raw_printf(p->out, "0x%08x\n", rc2);
10436    }
10437  }else
10438#endif /* !defined(SQLITE_UNTESTABLE) */
10439
10440  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10441    open_db(p, 0);
10442    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10443  }else
10444
10445  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10446    if( nArg==2 ){
10447      enableTimer = booleanValue(azArg[1]);
10448      if( enableTimer && !HAS_TIMER ){
10449        raw_printf(stderr, "Error: timer not available on this system.\n");
10450        enableTimer = 0;
10451      }
10452    }else{
10453      raw_printf(stderr, "Usage: .timer on|off\n");
10454      rc = 1;
10455    }
10456  }else
10457
10458#ifndef SQLITE_OMIT_TRACE
10459  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10460    int mType = 0;
10461    int jj;
10462    open_db(p, 0);
10463    for(jj=1; jj<nArg; jj++){
10464      const char *z = azArg[jj];
10465      if( z[0]=='-' ){
10466        if( optionMatch(z, "expanded") ){
10467          p->eTraceType = SHELL_TRACE_EXPANDED;
10468        }
10469#ifdef SQLITE_ENABLE_NORMALIZE
10470        else if( optionMatch(z, "normalized") ){
10471          p->eTraceType = SHELL_TRACE_NORMALIZED;
10472        }
10473#endif
10474        else if( optionMatch(z, "plain") ){
10475          p->eTraceType = SHELL_TRACE_PLAIN;
10476        }
10477        else if( optionMatch(z, "profile") ){
10478          mType |= SQLITE_TRACE_PROFILE;
10479        }
10480        else if( optionMatch(z, "row") ){
10481          mType |= SQLITE_TRACE_ROW;
10482        }
10483        else if( optionMatch(z, "stmt") ){
10484          mType |= SQLITE_TRACE_STMT;
10485        }
10486        else if( optionMatch(z, "close") ){
10487          mType |= SQLITE_TRACE_CLOSE;
10488        }
10489        else {
10490          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10491          rc = 1;
10492          goto meta_command_exit;
10493        }
10494      }else{
10495        output_file_close(p->traceOut);
10496        p->traceOut = output_file_open(azArg[1], 0);
10497      }
10498    }
10499    if( p->traceOut==0 ){
10500      sqlite3_trace_v2(p->db, 0, 0, 0);
10501    }else{
10502      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10503      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10504    }
10505  }else
10506#endif /* !defined(SQLITE_OMIT_TRACE) */
10507
10508#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10509  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10510    int ii;
10511    int lenOpt;
10512    char *zOpt;
10513    if( nArg<2 ){
10514      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10515      rc = 1;
10516      goto meta_command_exit;
10517    }
10518    open_db(p, 0);
10519    zOpt = azArg[1];
10520    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10521    lenOpt = (int)strlen(zOpt);
10522    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10523      assert( azArg[nArg]==0 );
10524      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10525    }else{
10526      for(ii=1; ii<nArg; ii++){
10527        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10528      }
10529    }
10530  }else
10531#endif
10532
10533#if SQLITE_USER_AUTHENTICATION
10534  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10535    if( nArg<2 ){
10536      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10537      rc = 1;
10538      goto meta_command_exit;
10539    }
10540    open_db(p, 0);
10541    if( strcmp(azArg[1],"login")==0 ){
10542      if( nArg!=4 ){
10543        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10544        rc = 1;
10545        goto meta_command_exit;
10546      }
10547      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10548                                     strlen30(azArg[3]));
10549      if( rc ){
10550        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10551        rc = 1;
10552      }
10553    }else if( strcmp(azArg[1],"add")==0 ){
10554      if( nArg!=5 ){
10555        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10556        rc = 1;
10557        goto meta_command_exit;
10558      }
10559      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10560                            booleanValue(azArg[4]));
10561      if( rc ){
10562        raw_printf(stderr, "User-Add failed: %d\n", rc);
10563        rc = 1;
10564      }
10565    }else if( strcmp(azArg[1],"edit")==0 ){
10566      if( nArg!=5 ){
10567        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10568        rc = 1;
10569        goto meta_command_exit;
10570      }
10571      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10572                              booleanValue(azArg[4]));
10573      if( rc ){
10574        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10575        rc = 1;
10576      }
10577    }else if( strcmp(azArg[1],"delete")==0 ){
10578      if( nArg!=3 ){
10579        raw_printf(stderr, "Usage: .user delete USER\n");
10580        rc = 1;
10581        goto meta_command_exit;
10582      }
10583      rc = sqlite3_user_delete(p->db, azArg[2]);
10584      if( rc ){
10585        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10586        rc = 1;
10587      }
10588    }else{
10589      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10590      rc = 1;
10591      goto meta_command_exit;
10592    }
10593  }else
10594#endif /* SQLITE_USER_AUTHENTICATION */
10595
10596  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10597    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10598        sqlite3_libversion(), sqlite3_sourceid());
10599#if SQLITE_HAVE_ZLIB
10600    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10601#endif
10602#define CTIMEOPT_VAL_(opt) #opt
10603#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10604#if defined(__clang__) && defined(__clang_major__)
10605    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10606                    CTIMEOPT_VAL(__clang_minor__) "."
10607                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10608#elif defined(_MSC_VER)
10609    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10610#elif defined(__GNUC__) && defined(__VERSION__)
10611    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10612#endif
10613  }else
10614
10615  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10616    const char *zDbName = nArg==2 ? azArg[1] : "main";
10617    sqlite3_vfs *pVfs = 0;
10618    if( p->db ){
10619      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10620      if( pVfs ){
10621        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10622        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10623        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10624        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10625      }
10626    }
10627  }else
10628
10629  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10630    sqlite3_vfs *pVfs;
10631    sqlite3_vfs *pCurrent = 0;
10632    if( p->db ){
10633      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10634    }
10635    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10636      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10637           pVfs==pCurrent ? "  <--- CURRENT" : "");
10638      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10639      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10640      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10641      if( pVfs->pNext ){
10642        raw_printf(p->out, "-----------------------------------\n");
10643      }
10644    }
10645  }else
10646
10647  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10648    const char *zDbName = nArg==2 ? azArg[1] : "main";
10649    char *zVfsName = 0;
10650    if( p->db ){
10651      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10652      if( zVfsName ){
10653        utf8_printf(p->out, "%s\n", zVfsName);
10654        sqlite3_free(zVfsName);
10655      }
10656    }
10657  }else
10658
10659  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10660    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10661    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
10662  }else
10663
10664  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10665    int j;
10666    assert( nArg<=ArraySize(azArg) );
10667    p->nWidth = nArg-1;
10668    p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
10669    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10670    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10671    for(j=1; j<nArg; j++){
10672      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10673    }
10674  }else
10675
10676  {
10677    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10678      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10679    rc = 1;
10680  }
10681
10682meta_command_exit:
10683  if( p->outCount ){
10684    p->outCount--;
10685    if( p->outCount==0 ) output_reset(p);
10686  }
10687  p->bSafeMode = p->bSafeModePersist;
10688  return rc;
10689}
10690
10691/* Line scan result and intermediate states (supporting scan resumption)
10692*/
10693#ifndef CHAR_BIT
10694# define CHAR_BIT 8
10695#endif
10696typedef enum {
10697  QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
10698  QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
10699  QSS_Start = 0
10700} QuickScanState;
10701#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
10702#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
10703#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
10704#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
10705#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
10706
10707/*
10708** Scan line for classification to guide shell's handling.
10709** The scan is resumable for subsequent lines when prior
10710** return values are passed as the 2nd argument.
10711*/
10712static QuickScanState quickscan(char *zLine, QuickScanState qss){
10713  char cin;
10714  char cWait = (char)qss; /* intentional narrowing loss */
10715  if( cWait==0 ){
10716  PlainScan:
10717    assert( cWait==0 );
10718    while( (cin = *zLine++)!=0 ){
10719      if( IsSpace(cin) )
10720        continue;
10721      switch (cin){
10722      case '-':
10723        if( *zLine!='-' )
10724          break;
10725        while((cin = *++zLine)!=0 )
10726          if( cin=='\n')
10727            goto PlainScan;
10728        return qss;
10729      case ';':
10730        qss |= QSS_EndingSemi;
10731        continue;
10732      case '/':
10733        if( *zLine=='*' ){
10734          ++zLine;
10735          cWait = '*';
10736          qss = QSS_SETV(qss, cWait);
10737          goto TermScan;
10738        }
10739        break;
10740      case '[':
10741        cin = ']';
10742        /* fall thru */
10743      case '`': case '\'': case '"':
10744        cWait = cin;
10745        qss = QSS_HasDark | cWait;
10746        goto TermScan;
10747      default:
10748        break;
10749      }
10750      qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
10751    }
10752  }else{
10753  TermScan:
10754    while( (cin = *zLine++)!=0 ){
10755      if( cin==cWait ){
10756        switch( cWait ){
10757        case '*':
10758          if( *zLine != '/' )
10759            continue;
10760          ++zLine;
10761          cWait = 0;
10762          qss = QSS_SETV(qss, 0);
10763          goto PlainScan;
10764        case '`': case '\'': case '"':
10765          if(*zLine==cWait){
10766            ++zLine;
10767            continue;
10768          }
10769          /* fall thru */
10770        case ']':
10771          cWait = 0;
10772          qss = QSS_SETV(qss, 0);
10773          goto PlainScan;
10774        default: assert(0);
10775        }
10776      }
10777    }
10778  }
10779  return qss;
10780}
10781
10782/*
10783** Return TRUE if the line typed in is an SQL command terminator other
10784** than a semi-colon.  The SQL Server style "go" command is understood
10785** as is the Oracle "/".
10786*/
10787static int line_is_command_terminator(char *zLine){
10788  while( IsSpace(zLine[0]) ){ zLine++; };
10789  if( zLine[0]=='/' )
10790    zLine += 1; /* Oracle */
10791  else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
10792    zLine += 2; /* SQL Server */
10793  else
10794    return 0;
10795  return quickscan(zLine,QSS_Start)==QSS_Start;
10796}
10797
10798/*
10799** We need a default sqlite3_complete() implementation to use in case
10800** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10801** any arbitrary text is a complete SQL statement.  This is not very
10802** user-friendly, but it does seem to work.
10803*/
10804#ifdef SQLITE_OMIT_COMPLETE
10805#define sqlite3_complete(x) 1
10806#endif
10807
10808/*
10809** Return true if zSql is a complete SQL statement.  Return false if it
10810** ends in the middle of a string literal or C-style comment.
10811*/
10812static int line_is_complete(char *zSql, int nSql){
10813  int rc;
10814  if( zSql==0 ) return 1;
10815  zSql[nSql] = ';';
10816  zSql[nSql+1] = 0;
10817  rc = sqlite3_complete(zSql);
10818  zSql[nSql] = 0;
10819  return rc;
10820}
10821
10822/*
10823** Run a single line of SQL.  Return the number of errors.
10824*/
10825static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10826  int rc;
10827  char *zErrMsg = 0;
10828
10829  open_db(p, 0);
10830  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10831  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10832  BEGIN_TIMER;
10833  rc = shell_exec(p, zSql, &zErrMsg);
10834  END_TIMER;
10835  if( rc || zErrMsg ){
10836    char zPrefix[100];
10837    if( in!=0 || !stdin_is_interactive ){
10838      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10839                       "Error: near line %d:", startline);
10840    }else{
10841      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10842    }
10843    if( zErrMsg!=0 ){
10844      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10845      sqlite3_free(zErrMsg);
10846      zErrMsg = 0;
10847    }else{
10848      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10849    }
10850    return 1;
10851  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10852    char zLineBuf[2000];
10853    sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
10854            "changes: %lld   total_changes: %lld",
10855            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
10856    raw_printf(p->out, "%s\n", zLineBuf);
10857  }
10858  return 0;
10859}
10860
10861
10862/*
10863** Read input from *in and process it.  If *in==0 then input
10864** is interactive - the user is typing it it.  Otherwise, input
10865** is coming from a file or device.  A prompt is issued and history
10866** is saved only if input is interactive.  An interrupt signal will
10867** cause this routine to exit immediately, unless input is interactive.
10868**
10869** Return the number of errors.
10870*/
10871static int process_input(ShellState *p){
10872  char *zLine = 0;          /* A single input line */
10873  char *zSql = 0;           /* Accumulated SQL text */
10874  int nLine;                /* Length of current line */
10875  int nSql = 0;             /* Bytes of zSql[] used */
10876  int nAlloc = 0;           /* Allocated zSql[] space */
10877  int rc;                   /* Error code */
10878  int errCnt = 0;           /* Number of errors seen */
10879  int startline = 0;        /* Line number for start of current input */
10880  QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
10881
10882  p->lineno = 0;
10883  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10884    fflush(p->out);
10885    zLine = one_input_line(p->in, zLine, nSql>0);
10886    if( zLine==0 ){
10887      /* End of input */
10888      if( p->in==0 && stdin_is_interactive ) printf("\n");
10889      break;
10890    }
10891    if( seenInterrupt ){
10892      if( p->in!=0 ) break;
10893      seenInterrupt = 0;
10894    }
10895    p->lineno++;
10896    if( QSS_INPLAIN(qss)
10897        && line_is_command_terminator(zLine)
10898        && line_is_complete(zSql, nSql) ){
10899      memcpy(zLine,";",2);
10900    }
10901    qss = quickscan(zLine, qss);
10902    if( QSS_PLAINWHITE(qss) && nSql==0 ){
10903      if( ShellHasFlag(p, SHFLG_Echo) )
10904        printf("%s\n", zLine);
10905      /* Just swallow single-line whitespace */
10906      qss = QSS_Start;
10907      continue;
10908    }
10909    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10910      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10911      if( zLine[0]=='.' ){
10912        rc = do_meta_command(zLine, p);
10913        if( rc==2 ){ /* exit requested */
10914          break;
10915        }else if( rc ){
10916          errCnt++;
10917        }
10918      }
10919      qss = QSS_Start;
10920      continue;
10921    }
10922    /* No single-line dispositions remain; accumulate line(s). */
10923    nLine = strlen30(zLine);
10924    if( nSql+nLine+2>=nAlloc ){
10925      /* Grow buffer by half-again increments when big. */
10926      nAlloc = nSql+(nSql>>1)+nLine+100;
10927      zSql = realloc(zSql, nAlloc);
10928      if( zSql==0 ) shell_out_of_memory();
10929    }
10930    if( nSql==0 ){
10931      int i;
10932      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10933      assert( nAlloc>0 && zSql!=0 );
10934      memcpy(zSql, zLine+i, nLine+1-i);
10935      startline = p->lineno;
10936      nSql = nLine-i;
10937    }else{
10938      zSql[nSql++] = '\n';
10939      memcpy(zSql+nSql, zLine, nLine+1);
10940      nSql += nLine;
10941    }
10942    if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
10943      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10944      nSql = 0;
10945      if( p->outCount ){
10946        output_reset(p);
10947        p->outCount = 0;
10948      }else{
10949        clearTempFile(p);
10950      }
10951      p->bSafeMode = p->bSafeModePersist;
10952      qss = QSS_Start;
10953    }else if( nSql && QSS_PLAINWHITE(qss) ){
10954      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10955      nSql = 0;
10956      qss = QSS_Start;
10957    }
10958  }
10959  if( nSql && QSS_PLAINDARK(qss) ){
10960    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10961  }
10962  free(zSql);
10963  free(zLine);
10964  return errCnt>0;
10965}
10966
10967/*
10968** Return a pathname which is the user's home directory.  A
10969** 0 return indicates an error of some kind.
10970*/
10971static char *find_home_dir(int clearFlag){
10972  static char *home_dir = NULL;
10973  if( clearFlag ){
10974    free(home_dir);
10975    home_dir = 0;
10976    return 0;
10977  }
10978  if( home_dir ) return home_dir;
10979
10980#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10981     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10982  {
10983    struct passwd *pwent;
10984    uid_t uid = getuid();
10985    if( (pwent=getpwuid(uid)) != NULL) {
10986      home_dir = pwent->pw_dir;
10987    }
10988  }
10989#endif
10990
10991#if defined(_WIN32_WCE)
10992  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10993   */
10994  home_dir = "/";
10995#else
10996
10997#if defined(_WIN32) || defined(WIN32)
10998  if (!home_dir) {
10999    home_dir = getenv("USERPROFILE");
11000  }
11001#endif
11002
11003  if (!home_dir) {
11004    home_dir = getenv("HOME");
11005  }
11006
11007#if defined(_WIN32) || defined(WIN32)
11008  if (!home_dir) {
11009    char *zDrive, *zPath;
11010    int n;
11011    zDrive = getenv("HOMEDRIVE");
11012    zPath = getenv("HOMEPATH");
11013    if( zDrive && zPath ){
11014      n = strlen30(zDrive) + strlen30(zPath) + 1;
11015      home_dir = malloc( n );
11016      if( home_dir==0 ) return 0;
11017      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11018      return home_dir;
11019    }
11020    home_dir = "c:\\";
11021  }
11022#endif
11023
11024#endif /* !_WIN32_WCE */
11025
11026  if( home_dir ){
11027    int n = strlen30(home_dir) + 1;
11028    char *z = malloc( n );
11029    if( z ) memcpy(z, home_dir, n);
11030    home_dir = z;
11031  }
11032
11033  return home_dir;
11034}
11035
11036/*
11037** Read input from the file given by sqliterc_override.  Or if that
11038** parameter is NULL, take input from ~/.sqliterc
11039**
11040** Returns the number of errors.
11041*/
11042static void process_sqliterc(
11043  ShellState *p,                  /* Configuration data */
11044  const char *sqliterc_override   /* Name of config file. NULL to use default */
11045){
11046  char *home_dir = NULL;
11047  const char *sqliterc = sqliterc_override;
11048  char *zBuf = 0;
11049  FILE *inSaved = p->in;
11050  int savedLineno = p->lineno;
11051
11052  if (sqliterc == NULL) {
11053    home_dir = find_home_dir(0);
11054    if( home_dir==0 ){
11055      raw_printf(stderr, "-- warning: cannot find home directory;"
11056                      " cannot read ~/.sqliterc\n");
11057      return;
11058    }
11059    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11060    sqliterc = zBuf;
11061  }
11062  p->in = fopen(sqliterc,"rb");
11063  if( p->in ){
11064    if( stdin_is_interactive ){
11065      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11066    }
11067    if( process_input(p) && bail_on_error ) exit(1);
11068    fclose(p->in);
11069  }else if( sqliterc_override!=0 ){
11070    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11071    if( bail_on_error ) exit(1);
11072  }
11073  p->in = inSaved;
11074  p->lineno = savedLineno;
11075  sqlite3_free(zBuf);
11076}
11077
11078/*
11079** Show available command line options
11080*/
11081static const char zOptions[] =
11082#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11083  "   -A ARGS...           run \".archive ARGS\" and exit\n"
11084#endif
11085  "   -append              append the database to the end of the file\n"
11086  "   -ascii               set output mode to 'ascii'\n"
11087  "   -bail                stop after hitting an error\n"
11088  "   -batch               force batch I/O\n"
11089  "   -box                 set output mode to 'box'\n"
11090  "   -column              set output mode to 'column'\n"
11091  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
11092  "   -csv                 set output mode to 'csv'\n"
11093#if !defined(SQLITE_OMIT_DESERIALIZE)
11094  "   -deserialize         open the database using sqlite3_deserialize()\n"
11095#endif
11096  "   -echo                print commands before execution\n"
11097  "   -init FILENAME       read/process named file\n"
11098  "   -[no]header          turn headers on or off\n"
11099#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11100  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
11101#endif
11102  "   -help                show this message\n"
11103  "   -html                set output mode to HTML\n"
11104  "   -interactive         force interactive I/O\n"
11105  "   -json                set output mode to 'json'\n"
11106  "   -line                set output mode to 'line'\n"
11107  "   -list                set output mode to 'list'\n"
11108  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
11109  "   -markdown            set output mode to 'markdown'\n"
11110#if !defined(SQLITE_OMIT_DESERIALIZE)
11111  "   -maxsize N           maximum size for a --deserialize database\n"
11112#endif
11113  "   -memtrace            trace all memory allocations and deallocations\n"
11114  "   -mmap N              default mmap size set to N\n"
11115#ifdef SQLITE_ENABLE_MULTIPLEX
11116  "   -multiplex           enable the multiplexor VFS\n"
11117#endif
11118  "   -newline SEP         set output row separator. Default: '\\n'\n"
11119  "   -nofollow            refuse to open symbolic links to database files\n"
11120  "   -nonce STRING        set the safe-mode escape nonce\n"
11121  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
11122  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
11123  "   -quote               set output mode to 'quote'\n"
11124  "   -readonly            open the database read-only\n"
11125  "   -safe                enable safe-mode\n"
11126  "   -separator SEP       set output column separator. Default: '|'\n"
11127#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11128  "   -sorterref SIZE      sorter references threshold size\n"
11129#endif
11130  "   -stats               print memory stats before each finalize\n"
11131  "   -table               set output mode to 'table'\n"
11132  "   -tabs                set output mode to 'tabs'\n"
11133  "   -version             show SQLite version\n"
11134  "   -vfs NAME            use NAME as the default VFS\n"
11135#ifdef SQLITE_ENABLE_VFSTRACE
11136  "   -vfstrace            enable tracing of all VFS calls\n"
11137#endif
11138#ifdef SQLITE_HAVE_ZLIB
11139  "   -zip                 open the file as a ZIP Archive\n"
11140#endif
11141;
11142static void usage(int showDetail){
11143  utf8_printf(stderr,
11144      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
11145      "FILENAME is the name of an SQLite database. A new database is created\n"
11146      "if the file does not previously exist.\n", Argv0);
11147  if( showDetail ){
11148    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11149  }else{
11150    raw_printf(stderr, "Use the -help option for additional information\n");
11151  }
11152  exit(1);
11153}
11154
11155/*
11156** Internal check:  Verify that the SQLite is uninitialized.  Print a
11157** error message if it is initialized.
11158*/
11159static void verify_uninitialized(void){
11160  if( sqlite3_config(-1)==SQLITE_MISUSE ){
11161    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11162                        " initialization.\n");
11163  }
11164}
11165
11166/*
11167** Initialize the state information in data
11168*/
11169static void main_init(ShellState *data) {
11170  memset(data, 0, sizeof(*data));
11171  data->normalMode = data->cMode = data->mode = MODE_List;
11172  data->autoExplain = 1;
11173  data->pAuxDb = &data->aAuxDb[0];
11174  memcpy(data->colSeparator,SEP_Column, 2);
11175  memcpy(data->rowSeparator,SEP_Row, 2);
11176  data->showHeader = 0;
11177  data->shellFlgs = SHFLG_Lookaside;
11178  verify_uninitialized();
11179  sqlite3_config(SQLITE_CONFIG_URI, 1);
11180  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11181  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11182  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11183  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
11184}
11185
11186/*
11187** Output text to the console in a font that attracts extra attention.
11188*/
11189#ifdef _WIN32
11190static void printBold(const char *zText){
11191#if !SQLITE_OS_WINRT
11192  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
11193  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
11194  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
11195  SetConsoleTextAttribute(out,
11196         FOREGROUND_RED|FOREGROUND_INTENSITY
11197  );
11198#endif
11199  printf("%s", zText);
11200#if !SQLITE_OS_WINRT
11201  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
11202#endif
11203}
11204#else
11205static void printBold(const char *zText){
11206  printf("\033[1m%s\033[0m", zText);
11207}
11208#endif
11209
11210/*
11211** Get the argument to an --option.  Throw an error and die if no argument
11212** is available.
11213*/
11214static char *cmdline_option_value(int argc, char **argv, int i){
11215  if( i==argc ){
11216    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
11217            argv[0], argv[argc-1]);
11218    exit(1);
11219  }
11220  return argv[i];
11221}
11222
11223#ifndef SQLITE_SHELL_IS_UTF8
11224#  if (defined(_WIN32) || defined(WIN32)) \
11225   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
11226#    define SQLITE_SHELL_IS_UTF8          (0)
11227#  else
11228#    define SQLITE_SHELL_IS_UTF8          (1)
11229#  endif
11230#endif
11231
11232#if SQLITE_SHELL_IS_UTF8
11233int SQLITE_CDECL main(int argc, char **argv){
11234#else
11235int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
11236  char **argv;
11237#endif
11238  char *zErrMsg = 0;
11239  ShellState data;
11240  const char *zInitFile = 0;
11241  int i;
11242  int rc = 0;
11243  int warnInmemoryDb = 0;
11244  int readStdin = 1;
11245  int nCmd = 0;
11246  char **azCmd = 0;
11247  const char *zVfs = 0;           /* Value of -vfs command-line option */
11248#if !SQLITE_SHELL_IS_UTF8
11249  char **argvToFree = 0;
11250  int argcToFree = 0;
11251#endif
11252
11253  setBinaryMode(stdin, 0);
11254  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
11255  stdin_is_interactive = isatty(0);
11256  stdout_is_console = isatty(1);
11257
11258#ifdef SQLITE_DEBUG
11259  registerOomSimulator();
11260#endif
11261
11262#if !defined(_WIN32_WCE)
11263  if( getenv("SQLITE_DEBUG_BREAK") ){
11264    if( isatty(0) && isatty(2) ){
11265      fprintf(stderr,
11266          "attach debugger to process %d and press any key to continue.\n",
11267          GETPID());
11268      fgetc(stdin);
11269    }else{
11270#if defined(_WIN32) || defined(WIN32)
11271#if SQLITE_OS_WINRT
11272      __debugbreak();
11273#else
11274      DebugBreak();
11275#endif
11276#elif defined(SIGTRAP)
11277      raise(SIGTRAP);
11278#endif
11279    }
11280  }
11281#endif
11282
11283#if USE_SYSTEM_SQLITE+0!=1
11284  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
11285    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
11286            sqlite3_sourceid(), SQLITE_SOURCE_ID);
11287    exit(1);
11288  }
11289#endif
11290  main_init(&data);
11291
11292  /* On Windows, we must translate command-line arguments into UTF-8.
11293  ** The SQLite memory allocator subsystem has to be enabled in order to
11294  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
11295  ** subsequent sqlite3_config() calls will work.  So copy all results into
11296  ** memory that does not come from the SQLite memory allocator.
11297  */
11298#if !SQLITE_SHELL_IS_UTF8
11299  sqlite3_initialize();
11300  argvToFree = malloc(sizeof(argv[0])*argc*2);
11301  argcToFree = argc;
11302  argv = argvToFree + argc;
11303  if( argv==0 ) shell_out_of_memory();
11304  for(i=0; i<argc; i++){
11305    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
11306    int n;
11307    if( z==0 ) shell_out_of_memory();
11308    n = (int)strlen(z);
11309    argv[i] = malloc( n+1 );
11310    if( argv[i]==0 ) shell_out_of_memory();
11311    memcpy(argv[i], z, n+1);
11312    argvToFree[i] = argv[i];
11313    sqlite3_free(z);
11314  }
11315  sqlite3_shutdown();
11316#endif
11317
11318  assert( argc>=1 && argv && argv[0] );
11319  Argv0 = argv[0];
11320
11321  /* Make sure we have a valid signal handler early, before anything
11322  ** else is done.
11323  */
11324#ifdef SIGINT
11325  signal(SIGINT, interrupt_handler);
11326#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11327  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
11328#endif
11329
11330#ifdef SQLITE_SHELL_DBNAME_PROC
11331  {
11332    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
11333    ** of a C-function that will provide the name of the database file.  Use
11334    ** this compile-time option to embed this shell program in larger
11335    ** applications. */
11336    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
11337    SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
11338    warnInmemoryDb = 0;
11339  }
11340#endif
11341
11342  /* Do an initial pass through the command-line argument to locate
11343  ** the name of the database file, the name of the initialization file,
11344  ** the size of the alternative malloc heap,
11345  ** and the first command to execute.
11346  */
11347  verify_uninitialized();
11348  for(i=1; i<argc; i++){
11349    char *z;
11350    z = argv[i];
11351    if( z[0]!='-' ){
11352      if( data.aAuxDb->zDbFilename==0 ){
11353        data.aAuxDb->zDbFilename = z;
11354      }else{
11355        /* Excesss arguments are interpreted as SQL (or dot-commands) and
11356        ** mean that nothing is read from stdin */
11357        readStdin = 0;
11358        nCmd++;
11359        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
11360        if( azCmd==0 ) shell_out_of_memory();
11361        azCmd[nCmd-1] = z;
11362      }
11363    }
11364    if( z[1]=='-' ) z++;
11365    if( strcmp(z,"-separator")==0
11366     || strcmp(z,"-nullvalue")==0
11367     || strcmp(z,"-newline")==0
11368     || strcmp(z,"-cmd")==0
11369    ){
11370      (void)cmdline_option_value(argc, argv, ++i);
11371    }else if( strcmp(z,"-init")==0 ){
11372      zInitFile = cmdline_option_value(argc, argv, ++i);
11373    }else if( strcmp(z,"-batch")==0 ){
11374      /* Need to check for batch mode here to so we can avoid printing
11375      ** informational messages (like from process_sqliterc) before
11376      ** we do the actual processing of arguments later in a second pass.
11377      */
11378      stdin_is_interactive = 0;
11379    }else if( strcmp(z,"-heap")==0 ){
11380#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11381      const char *zSize;
11382      sqlite3_int64 szHeap;
11383
11384      zSize = cmdline_option_value(argc, argv, ++i);
11385      szHeap = integerValue(zSize);
11386      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
11387      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
11388#else
11389      (void)cmdline_option_value(argc, argv, ++i);
11390#endif
11391    }else if( strcmp(z,"-pagecache")==0 ){
11392      sqlite3_int64 n, sz;
11393      sz = integerValue(cmdline_option_value(argc,argv,++i));
11394      if( sz>70000 ) sz = 70000;
11395      if( sz<0 ) sz = 0;
11396      n = integerValue(cmdline_option_value(argc,argv,++i));
11397      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
11398        n = 0xffffffffffffLL/sz;
11399      }
11400      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
11401                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
11402      data.shellFlgs |= SHFLG_Pagecache;
11403    }else if( strcmp(z,"-lookaside")==0 ){
11404      int n, sz;
11405      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
11406      if( sz<0 ) sz = 0;
11407      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11408      if( n<0 ) n = 0;
11409      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
11410      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
11411    }else if( strcmp(z,"-threadsafe")==0 ){
11412      int n;
11413      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11414      switch( n ){
11415         case 0:  sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);  break;
11416         case 2:  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);   break;
11417         default: sqlite3_config(SQLITE_CONFIG_SERIALIZED);    break;
11418      }
11419#ifdef SQLITE_ENABLE_VFSTRACE
11420    }else if( strcmp(z,"-vfstrace")==0 ){
11421      extern int vfstrace_register(
11422         const char *zTraceName,
11423         const char *zOldVfsName,
11424         int (*xOut)(const char*,void*),
11425         void *pOutArg,
11426         int makeDefault
11427      );
11428      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
11429#endif
11430#ifdef SQLITE_ENABLE_MULTIPLEX
11431    }else if( strcmp(z,"-multiplex")==0 ){
11432      extern int sqlite3_multiple_initialize(const char*,int);
11433      sqlite3_multiplex_initialize(0, 1);
11434#endif
11435    }else if( strcmp(z,"-mmap")==0 ){
11436      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11437      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
11438#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11439    }else if( strcmp(z,"-sorterref")==0 ){
11440      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11441      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11442#endif
11443    }else if( strcmp(z,"-vfs")==0 ){
11444      zVfs = cmdline_option_value(argc, argv, ++i);
11445#ifdef SQLITE_HAVE_ZLIB
11446    }else if( strcmp(z,"-zip")==0 ){
11447      data.openMode = SHELL_OPEN_ZIPFILE;
11448#endif
11449    }else if( strcmp(z,"-append")==0 ){
11450      data.openMode = SHELL_OPEN_APPENDVFS;
11451#ifndef SQLITE_OMIT_DESERIALIZE
11452    }else if( strcmp(z,"-deserialize")==0 ){
11453      data.openMode = SHELL_OPEN_DESERIALIZE;
11454    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11455      data.szMax = integerValue(argv[++i]);
11456#endif
11457    }else if( strcmp(z,"-readonly")==0 ){
11458      data.openMode = SHELL_OPEN_READONLY;
11459    }else if( strcmp(z,"-nofollow")==0 ){
11460      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11461#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11462    }else if( strncmp(z, "-A",2)==0 ){
11463      /* All remaining command-line arguments are passed to the ".archive"
11464      ** command, so ignore them */
11465      break;
11466#endif
11467    }else if( strcmp(z, "-memtrace")==0 ){
11468      sqlite3MemTraceActivate(stderr);
11469    }else if( strcmp(z,"-bail")==0 ){
11470      bail_on_error = 1;
11471    }else if( strcmp(z,"-nonce")==0 ){
11472      free(data.zNonce);
11473      data.zNonce = strdup(argv[++i]);
11474    }else if( strcmp(z,"-safe")==0 ){
11475      /* no-op - catch this on the second pass */
11476    }
11477  }
11478  verify_uninitialized();
11479
11480
11481#ifdef SQLITE_SHELL_INIT_PROC
11482  {
11483    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11484    ** of a C-function that will perform initialization actions on SQLite that
11485    ** occur just before or after sqlite3_initialize(). Use this compile-time
11486    ** option to embed this shell program in larger applications. */
11487    extern void SQLITE_SHELL_INIT_PROC(void);
11488    SQLITE_SHELL_INIT_PROC();
11489  }
11490#else
11491  /* All the sqlite3_config() calls have now been made. So it is safe
11492  ** to call sqlite3_initialize() and process any command line -vfs option. */
11493  sqlite3_initialize();
11494#endif
11495
11496  if( zVfs ){
11497    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11498    if( pVfs ){
11499      sqlite3_vfs_register(pVfs, 1);
11500    }else{
11501      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11502      exit(1);
11503    }
11504  }
11505
11506  if( data.pAuxDb->zDbFilename==0 ){
11507#ifndef SQLITE_OMIT_MEMORYDB
11508    data.pAuxDb->zDbFilename = ":memory:";
11509    warnInmemoryDb = argc==1;
11510#else
11511    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11512    return 1;
11513#endif
11514  }
11515  data.out = stdout;
11516  sqlite3_appendvfs_init(0,0,0);
11517
11518  /* Go ahead and open the database file if it already exists.  If the
11519  ** file does not exist, delay opening it.  This prevents empty database
11520  ** files from being created if a user mistypes the database name argument
11521  ** to the sqlite command-line tool.
11522  */
11523  if( access(data.pAuxDb->zDbFilename, 0)==0 ){
11524    open_db(&data, 0);
11525  }
11526
11527  /* Process the initialization file if there is one.  If no -init option
11528  ** is given on the command line, look for a file named ~/.sqliterc and
11529  ** try to process it.
11530  */
11531  process_sqliterc(&data,zInitFile);
11532
11533  /* Make a second pass through the command-line argument and set
11534  ** options.  This second pass is delayed until after the initialization
11535  ** file is processed so that the command-line arguments will override
11536  ** settings in the initialization file.
11537  */
11538  for(i=1; i<argc; i++){
11539    char *z = argv[i];
11540    if( z[0]!='-' ) continue;
11541    if( z[1]=='-' ){ z++; }
11542    if( strcmp(z,"-init")==0 ){
11543      i++;
11544    }else if( strcmp(z,"-html")==0 ){
11545      data.mode = MODE_Html;
11546    }else if( strcmp(z,"-list")==0 ){
11547      data.mode = MODE_List;
11548    }else if( strcmp(z,"-quote")==0 ){
11549      data.mode = MODE_Quote;
11550      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11551      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11552    }else if( strcmp(z,"-line")==0 ){
11553      data.mode = MODE_Line;
11554    }else if( strcmp(z,"-column")==0 ){
11555      data.mode = MODE_Column;
11556    }else if( strcmp(z,"-json")==0 ){
11557      data.mode = MODE_Json;
11558    }else if( strcmp(z,"-markdown")==0 ){
11559      data.mode = MODE_Markdown;
11560    }else if( strcmp(z,"-table")==0 ){
11561      data.mode = MODE_Table;
11562    }else if( strcmp(z,"-box")==0 ){
11563      data.mode = MODE_Box;
11564    }else if( strcmp(z,"-csv")==0 ){
11565      data.mode = MODE_Csv;
11566      memcpy(data.colSeparator,",",2);
11567#ifdef SQLITE_HAVE_ZLIB
11568    }else if( strcmp(z,"-zip")==0 ){
11569      data.openMode = SHELL_OPEN_ZIPFILE;
11570#endif
11571    }else if( strcmp(z,"-append")==0 ){
11572      data.openMode = SHELL_OPEN_APPENDVFS;
11573#ifndef SQLITE_OMIT_DESERIALIZE
11574    }else if( strcmp(z,"-deserialize")==0 ){
11575      data.openMode = SHELL_OPEN_DESERIALIZE;
11576    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11577      data.szMax = integerValue(argv[++i]);
11578#endif
11579    }else if( strcmp(z,"-readonly")==0 ){
11580      data.openMode = SHELL_OPEN_READONLY;
11581    }else if( strcmp(z,"-nofollow")==0 ){
11582      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11583    }else if( strcmp(z,"-ascii")==0 ){
11584      data.mode = MODE_Ascii;
11585      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
11586      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
11587    }else if( strcmp(z,"-tabs")==0 ){
11588      data.mode = MODE_List;
11589      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
11590      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11591    }else if( strcmp(z,"-separator")==0 ){
11592      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11593                       "%s",cmdline_option_value(argc,argv,++i));
11594    }else if( strcmp(z,"-newline")==0 ){
11595      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11596                       "%s",cmdline_option_value(argc,argv,++i));
11597    }else if( strcmp(z,"-nullvalue")==0 ){
11598      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11599                       "%s",cmdline_option_value(argc,argv,++i));
11600    }else if( strcmp(z,"-header")==0 ){
11601      data.showHeader = 1;
11602      ShellSetFlag(&data, SHFLG_HeaderSet);
11603     }else if( strcmp(z,"-noheader")==0 ){
11604      data.showHeader = 0;
11605      ShellSetFlag(&data, SHFLG_HeaderSet);
11606    }else if( strcmp(z,"-echo")==0 ){
11607      ShellSetFlag(&data, SHFLG_Echo);
11608    }else if( strcmp(z,"-eqp")==0 ){
11609      data.autoEQP = AUTOEQP_on;
11610    }else if( strcmp(z,"-eqpfull")==0 ){
11611      data.autoEQP = AUTOEQP_full;
11612    }else if( strcmp(z,"-stats")==0 ){
11613      data.statsOn = 1;
11614    }else if( strcmp(z,"-scanstats")==0 ){
11615      data.scanstatsOn = 1;
11616    }else if( strcmp(z,"-backslash")==0 ){
11617      /* Undocumented command-line option: -backslash
11618      ** Causes C-style backslash escapes to be evaluated in SQL statements
11619      ** prior to sending the SQL into SQLite.  Useful for injecting
11620      ** crazy bytes in the middle of SQL statements for testing and debugging.
11621      */
11622      ShellSetFlag(&data, SHFLG_Backslash);
11623    }else if( strcmp(z,"-bail")==0 ){
11624      /* No-op.  The bail_on_error flag should already be set. */
11625    }else if( strcmp(z,"-version")==0 ){
11626      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11627      return 0;
11628    }else if( strcmp(z,"-interactive")==0 ){
11629      stdin_is_interactive = 1;
11630    }else if( strcmp(z,"-batch")==0 ){
11631      stdin_is_interactive = 0;
11632    }else if( strcmp(z,"-heap")==0 ){
11633      i++;
11634    }else if( strcmp(z,"-pagecache")==0 ){
11635      i+=2;
11636    }else if( strcmp(z,"-lookaside")==0 ){
11637      i+=2;
11638    }else if( strcmp(z,"-threadsafe")==0 ){
11639      i+=2;
11640    }else if( strcmp(z,"-nonce")==0 ){
11641      i += 2;
11642    }else if( strcmp(z,"-mmap")==0 ){
11643      i++;
11644    }else if( strcmp(z,"-memtrace")==0 ){
11645      i++;
11646#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11647    }else if( strcmp(z,"-sorterref")==0 ){
11648      i++;
11649#endif
11650    }else if( strcmp(z,"-vfs")==0 ){
11651      i++;
11652#ifdef SQLITE_ENABLE_VFSTRACE
11653    }else if( strcmp(z,"-vfstrace")==0 ){
11654      i++;
11655#endif
11656#ifdef SQLITE_ENABLE_MULTIPLEX
11657    }else if( strcmp(z,"-multiplex")==0 ){
11658      i++;
11659#endif
11660    }else if( strcmp(z,"-help")==0 ){
11661      usage(1);
11662    }else if( strcmp(z,"-cmd")==0 ){
11663      /* Run commands that follow -cmd first and separately from commands
11664      ** that simply appear on the command-line.  This seems goofy.  It would
11665      ** be better if all commands ran in the order that they appear.  But
11666      ** we retain the goofy behavior for historical compatibility. */
11667      if( i==argc-1 ) break;
11668      z = cmdline_option_value(argc,argv,++i);
11669      if( z[0]=='.' ){
11670        rc = do_meta_command(z, &data);
11671        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11672      }else{
11673        open_db(&data, 0);
11674        rc = shell_exec(&data, z, &zErrMsg);
11675        if( zErrMsg!=0 ){
11676          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11677          if( bail_on_error ) return rc!=0 ? rc : 1;
11678        }else if( rc!=0 ){
11679          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11680          if( bail_on_error ) return rc;
11681        }
11682      }
11683#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11684    }else if( strncmp(z, "-A", 2)==0 ){
11685      if( nCmd>0 ){
11686        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11687                            " with \"%s\"\n", z);
11688        return 1;
11689      }
11690      open_db(&data, OPEN_DB_ZIPFILE);
11691      if( z[2] ){
11692        argv[i] = &z[2];
11693        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11694      }else{
11695        arDotCommand(&data, 1, argv+i, argc-i);
11696      }
11697      readStdin = 0;
11698      break;
11699#endif
11700    }else if( strcmp(z,"-safe")==0 ){
11701      data.bSafeMode = data.bSafeModePersist = 1;
11702    }else{
11703      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11704      raw_printf(stderr,"Use -help for a list of options.\n");
11705      return 1;
11706    }
11707    data.cMode = data.mode;
11708  }
11709
11710  if( !readStdin ){
11711    /* Run all arguments that do not begin with '-' as if they were separate
11712    ** command-line inputs, except for the argToSkip argument which contains
11713    ** the database filename.
11714    */
11715    for(i=0; i<nCmd; i++){
11716      if( azCmd[i][0]=='.' ){
11717        rc = do_meta_command(azCmd[i], &data);
11718        if( rc ){
11719          free(azCmd);
11720          return rc==2 ? 0 : rc;
11721        }
11722      }else{
11723        open_db(&data, 0);
11724        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11725        if( zErrMsg || rc ){
11726          if( zErrMsg!=0 ){
11727            utf8_printf(stderr,"Error: %s\n", zErrMsg);
11728          }else{
11729            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11730          }
11731          sqlite3_free(zErrMsg);
11732          free(azCmd);
11733          return rc!=0 ? rc : 1;
11734        }
11735      }
11736    }
11737  }else{
11738    /* Run commands received from standard input
11739    */
11740    if( stdin_is_interactive ){
11741      char *zHome;
11742      char *zHistory;
11743      int nHistory;
11744      printf(
11745        "SQLite version %s %.19s\n" /*extra-version-info*/
11746        "Enter \".help\" for usage hints.\n",
11747        sqlite3_libversion(), sqlite3_sourceid()
11748      );
11749      if( warnInmemoryDb ){
11750        printf("Connected to a ");
11751        printBold("transient in-memory database");
11752        printf(".\nUse \".open FILENAME\" to reopen on a "
11753               "persistent database.\n");
11754      }
11755      zHistory = getenv("SQLITE_HISTORY");
11756      if( zHistory ){
11757        zHistory = strdup(zHistory);
11758      }else if( (zHome = find_home_dir(0))!=0 ){
11759        nHistory = strlen30(zHome) + 20;
11760        if( (zHistory = malloc(nHistory))!=0 ){
11761          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11762        }
11763      }
11764      if( zHistory ){ shell_read_history(zHistory); }
11765#if HAVE_READLINE || HAVE_EDITLINE
11766      rl_attempted_completion_function = readline_completion;
11767#elif HAVE_LINENOISE
11768      linenoiseSetCompletionCallback(linenoise_completion);
11769#endif
11770      data.in = 0;
11771      rc = process_input(&data);
11772      if( zHistory ){
11773        shell_stifle_history(2000);
11774        shell_write_history(zHistory);
11775        free(zHistory);
11776      }
11777    }else{
11778      data.in = stdin;
11779      rc = process_input(&data);
11780    }
11781  }
11782  free(azCmd);
11783  set_table_name(&data, 0);
11784  if( data.db ){
11785    session_close_all(&data, -1);
11786    close_db(data.db);
11787  }
11788  for(i=0; i<ArraySize(data.aAuxDb); i++){
11789    sqlite3_free(data.aAuxDb[i].zFreeOnClose);
11790    if( data.aAuxDb[i].db ){
11791      session_close_all(&data, i);
11792      close_db(data.aAuxDb[i].db);
11793    }
11794  }
11795  find_home_dir(1);
11796  output_reset(&data);
11797  data.doXdgOpen = 0;
11798  clearTempFile(&data);
11799#if !SQLITE_SHELL_IS_UTF8
11800  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11801  free(argvToFree);
11802#endif
11803  free(data.colWidth);
11804  free(data.zNonce);
11805  /* Clear the global data structure so that valgrind will detect memory
11806  ** leaks */
11807  memset(&data, 0, sizeof(data));
11808  return rc;
11809}
11810