xref: /sqlite-3.40.0/src/shell.c.in (revision 4df68e0a)
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code.  In place of
5** a legal notice, here is a blessing:
6**
7**    May you do good and not evil.
8**    May you find forgiveness for yourself and forgive others.
9**    May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14*/
15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16/* This needs to come before any includes for MSVC compiler */
17#define _CRT_SECURE_NO_WARNINGS
18#endif
19
20/*
21** Determine if we are dealing with WinRT, which provides only a subset of
22** the full Win32 API.
23*/
24#if !defined(SQLITE_OS_WINRT)
25# define SQLITE_OS_WINRT 0
26#endif
27
28/*
29** Warning pragmas copied from msvc.h in the core.
30*/
31#if defined(_MSC_VER)
32#pragma warning(disable : 4054)
33#pragma warning(disable : 4055)
34#pragma warning(disable : 4100)
35#pragma warning(disable : 4127)
36#pragma warning(disable : 4130)
37#pragma warning(disable : 4152)
38#pragma warning(disable : 4189)
39#pragma warning(disable : 4206)
40#pragma warning(disable : 4210)
41#pragma warning(disable : 4232)
42#pragma warning(disable : 4244)
43#pragma warning(disable : 4305)
44#pragma warning(disable : 4306)
45#pragma warning(disable : 4702)
46#pragma warning(disable : 4706)
47#endif /* defined(_MSC_VER) */
48
49/*
50** No support for loadable extensions in VxWorks.
51*/
52#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
53# define SQLITE_OMIT_LOAD_EXTENSION 1
54#endif
55
56/*
57** Enable large-file support for fopen() and friends on unix.
58*/
59#ifndef SQLITE_DISABLE_LFS
60# define _LARGE_FILE       1
61# ifndef _FILE_OFFSET_BITS
62#   define _FILE_OFFSET_BITS 64
63# endif
64# define _LARGEFILE_SOURCE 1
65#endif
66
67#include <stdlib.h>
68#include <string.h>
69#include <stdio.h>
70#include <assert.h>
71#include "sqlite3.h"
72typedef sqlite3_int64 i64;
73typedef sqlite3_uint64 u64;
74typedef unsigned char u8;
75#if SQLITE_USER_AUTHENTICATION
76# include "sqlite3userauth.h"
77#endif
78#include <ctype.h>
79#include <stdarg.h>
80
81#if !defined(_WIN32) && !defined(WIN32)
82# include <signal.h>
83# if !defined(__RTP__) && !defined(_WRS_KERNEL)
84#  include <pwd.h>
85# endif
86#endif
87#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
88# include <unistd.h>
89# include <dirent.h>
90# define GETPID getpid
91# if defined(__MINGW32__)
92#  define DIRENT dirent
93#  ifndef S_ISLNK
94#   define S_ISLNK(mode) (0)
95#  endif
96# endif
97#else
98# define GETPID (int)GetCurrentProcessId
99#endif
100#include <sys/types.h>
101#include <sys/stat.h>
102
103#if HAVE_READLINE
104# include <readline/readline.h>
105# include <readline/history.h>
106#endif
107
108#if HAVE_EDITLINE
109# include <editline/readline.h>
110#endif
111
112#if HAVE_EDITLINE || HAVE_READLINE
113
114# define shell_add_history(X) add_history(X)
115# define shell_read_history(X) read_history(X)
116# define shell_write_history(X) write_history(X)
117# define shell_stifle_history(X) stifle_history(X)
118# define shell_readline(X) readline(X)
119
120#elif HAVE_LINENOISE
121
122# include "linenoise.h"
123# define shell_add_history(X) linenoiseHistoryAdd(X)
124# define shell_read_history(X) linenoiseHistoryLoad(X)
125# define shell_write_history(X) linenoiseHistorySave(X)
126# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
127# define shell_readline(X) linenoise(X)
128
129#else
130
131# define shell_read_history(X)
132# define shell_write_history(X)
133# define shell_stifle_history(X)
134
135# define SHELL_USE_LOCAL_GETLINE 1
136#endif
137
138
139#if defined(_WIN32) || defined(WIN32)
140# if SQLITE_OS_WINRT
141#  define SQLITE_OMIT_POPEN 1
142# else
143#  include <io.h>
144#  include <fcntl.h>
145#  define isatty(h) _isatty(h)
146#  ifndef access
147#   define access(f,m) _access((f),(m))
148#  endif
149#  ifndef unlink
150#   define unlink _unlink
151#  endif
152#  ifndef strdup
153#   define strdup _strdup
154#  endif
155#  undef popen
156#  define popen _popen
157#  undef pclose
158#  define pclose _pclose
159# endif
160#else
161 /* Make sure isatty() has a prototype. */
162 extern int isatty(int);
163
164# if !defined(__RTP__) && !defined(_WRS_KERNEL)
165  /* popen and pclose are not C89 functions and so are
166  ** sometimes omitted from the <stdio.h> header */
167   extern FILE *popen(const char*,const char*);
168   extern int pclose(FILE*);
169# else
170#  define SQLITE_OMIT_POPEN 1
171# endif
172#endif
173
174#if defined(_WIN32_WCE)
175/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
176 * thus we always assume that we have a console. That can be
177 * overridden with the -batch command line option.
178 */
179#define isatty(x) 1
180#endif
181
182/* ctype macros that work with signed characters */
183#define IsSpace(X)  isspace((unsigned char)X)
184#define IsDigit(X)  isdigit((unsigned char)X)
185#define ToLower(X)  (char)tolower((unsigned char)X)
186
187#if defined(_WIN32) || defined(WIN32)
188#if SQLITE_OS_WINRT
189#include <intrin.h>
190#endif
191#include <windows.h>
192
193/* string conversion routines only needed on Win32 */
194extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
195extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
196extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
197extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
198#endif
199
200/* On Windows, we normally run with output mode of TEXT so that \n characters
201** are automatically translated into \r\n.  However, this behavior needs
202** to be disabled in some cases (ex: when generating CSV output and when
203** rendering quoted strings that contain \n characters).  The following
204** routines take care of that.
205*/
206#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
207static void setBinaryMode(FILE *file, int isOutput){
208  if( isOutput ) fflush(file);
209  _setmode(_fileno(file), _O_BINARY);
210}
211static void setTextMode(FILE *file, int isOutput){
212  if( isOutput ) fflush(file);
213  _setmode(_fileno(file), _O_TEXT);
214}
215#else
216# define setBinaryMode(X,Y)
217# define setTextMode(X,Y)
218#endif
219
220
221/* True if the timer is enabled */
222static int enableTimer = 0;
223
224/* Return the current wall-clock time */
225static sqlite3_int64 timeOfDay(void){
226  static sqlite3_vfs *clockVfs = 0;
227  sqlite3_int64 t;
228  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
229  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
230    clockVfs->xCurrentTimeInt64(clockVfs, &t);
231  }else{
232    double r;
233    clockVfs->xCurrentTime(clockVfs, &r);
234    t = (sqlite3_int64)(r*86400000.0);
235  }
236  return t;
237}
238
239#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
240#include <sys/time.h>
241#include <sys/resource.h>
242
243/* VxWorks does not support getrusage() as far as we can determine */
244#if defined(_WRS_KERNEL) || defined(__RTP__)
245struct rusage {
246  struct timeval ru_utime; /* user CPU time used */
247  struct timeval ru_stime; /* system CPU time used */
248};
249#define getrusage(A,B) memset(B,0,sizeof(*B))
250#endif
251
252/* Saved resource information for the beginning of an operation */
253static struct rusage sBegin;  /* CPU time at start */
254static sqlite3_int64 iBegin;  /* Wall-clock time at start */
255
256/*
257** Begin timing an operation
258*/
259static void beginTimer(void){
260  if( enableTimer ){
261    getrusage(RUSAGE_SELF, &sBegin);
262    iBegin = timeOfDay();
263  }
264}
265
266/* Return the difference of two time_structs in seconds */
267static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
268  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
269         (double)(pEnd->tv_sec - pStart->tv_sec);
270}
271
272/*
273** Print the timing results.
274*/
275static void endTimer(void){
276  if( enableTimer ){
277    sqlite3_int64 iEnd = timeOfDay();
278    struct rusage sEnd;
279    getrusage(RUSAGE_SELF, &sEnd);
280    printf("Run Time: real %.3f user %f sys %f\n",
281       (iEnd - iBegin)*0.001,
282       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
283       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
284  }
285}
286
287#define BEGIN_TIMER beginTimer()
288#define END_TIMER endTimer()
289#define HAS_TIMER 1
290
291#elif (defined(_WIN32) || defined(WIN32))
292
293/* Saved resource information for the beginning of an operation */
294static HANDLE hProcess;
295static FILETIME ftKernelBegin;
296static FILETIME ftUserBegin;
297static sqlite3_int64 ftWallBegin;
298typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
299                                    LPFILETIME, LPFILETIME);
300static GETPROCTIMES getProcessTimesAddr = NULL;
301
302/*
303** Check to see if we have timer support.  Return 1 if necessary
304** support found (or found previously).
305*/
306static int hasTimer(void){
307  if( getProcessTimesAddr ){
308    return 1;
309  } else {
310#if !SQLITE_OS_WINRT
311    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
312    ** versions. See if the version we are running on has it, and if it
313    ** does, save off a pointer to it and the current process handle.
314    */
315    hProcess = GetCurrentProcess();
316    if( hProcess ){
317      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
318      if( NULL != hinstLib ){
319        getProcessTimesAddr =
320            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
321        if( NULL != getProcessTimesAddr ){
322          return 1;
323        }
324        FreeLibrary(hinstLib);
325      }
326    }
327#endif
328  }
329  return 0;
330}
331
332/*
333** Begin timing an operation
334*/
335static void beginTimer(void){
336  if( enableTimer && getProcessTimesAddr ){
337    FILETIME ftCreation, ftExit;
338    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
339                        &ftKernelBegin,&ftUserBegin);
340    ftWallBegin = timeOfDay();
341  }
342}
343
344/* Return the difference of two FILETIME structs in seconds */
345static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
346  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
347  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
348  return (double) ((i64End - i64Start) / 10000000.0);
349}
350
351/*
352** Print the timing results.
353*/
354static void endTimer(void){
355  if( enableTimer && getProcessTimesAddr){
356    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
357    sqlite3_int64 ftWallEnd = timeOfDay();
358    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
359    printf("Run Time: real %.3f user %f sys %f\n",
360       (ftWallEnd - ftWallBegin)*0.001,
361       timeDiff(&ftUserBegin, &ftUserEnd),
362       timeDiff(&ftKernelBegin, &ftKernelEnd));
363  }
364}
365
366#define BEGIN_TIMER beginTimer()
367#define END_TIMER endTimer()
368#define HAS_TIMER hasTimer()
369
370#else
371#define BEGIN_TIMER
372#define END_TIMER
373#define HAS_TIMER 0
374#endif
375
376/*
377** Used to prevent warnings about unused parameters
378*/
379#define UNUSED_PARAMETER(x) (void)(x)
380
381/*
382** Number of elements in an array
383*/
384#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
385
386/*
387** If the following flag is set, then command execution stops
388** at an error if we are not interactive.
389*/
390static int bail_on_error = 0;
391
392/*
393** Threat stdin as an interactive input if the following variable
394** is true.  Otherwise, assume stdin is connected to a file or pipe.
395*/
396static int stdin_is_interactive = 1;
397
398/*
399** On Windows systems we have to know if standard output is a console
400** in order to translate UTF-8 into MBCS.  The following variable is
401** true if translation is required.
402*/
403static int stdout_is_console = 1;
404
405/*
406** The following is the open SQLite database.  We make a pointer
407** to this database a static variable so that it can be accessed
408** by the SIGINT handler to interrupt database processing.
409*/
410static sqlite3 *globalDb = 0;
411
412/*
413** True if an interrupt (Control-C) has been received.
414*/
415static volatile int seenInterrupt = 0;
416
417#ifdef SQLITE_DEBUG
418/*
419** Out-of-memory simulator variables
420*/
421static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
422static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
423static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
424#endif /* SQLITE_DEBUG */
425
426/*
427** This is the name of our program. It is set in main(), used
428** in a number of other places, mostly for error messages.
429*/
430static char *Argv0;
431
432/*
433** Prompt strings. Initialized in main. Settable with
434**   .prompt main continue
435*/
436static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
437static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
438
439/*
440** Render output like fprintf().  Except, if the output is going to the
441** console and if this is running on a Windows machine, translate the
442** output from UTF-8 into MBCS.
443*/
444#if defined(_WIN32) || defined(WIN32)
445void utf8_printf(FILE *out, const char *zFormat, ...){
446  va_list ap;
447  va_start(ap, zFormat);
448  if( stdout_is_console && (out==stdout || out==stderr) ){
449    char *z1 = sqlite3_vmprintf(zFormat, ap);
450    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
451    sqlite3_free(z1);
452    fputs(z2, out);
453    sqlite3_free(z2);
454  }else{
455    vfprintf(out, zFormat, ap);
456  }
457  va_end(ap);
458}
459#elif !defined(utf8_printf)
460# define utf8_printf fprintf
461#endif
462
463/*
464** Render output like fprintf().  This should not be used on anything that
465** includes string formatting (e.g. "%s").
466*/
467#if !defined(raw_printf)
468# define raw_printf fprintf
469#endif
470
471/* Indicate out-of-memory and exit. */
472static void shell_out_of_memory(void){
473  raw_printf(stderr,"Error: out of memory\n");
474  exit(1);
475}
476
477#ifdef SQLITE_DEBUG
478/* This routine is called when a simulated OOM occurs.  It is broken
479** out as a separate routine to make it easy to set a breakpoint on
480** the OOM
481*/
482void shellOomFault(void){
483  if( oomRepeat>0 ){
484    oomRepeat--;
485  }else{
486    oomCounter--;
487  }
488}
489#endif /* SQLITE_DEBUG */
490
491#ifdef SQLITE_DEBUG
492/* This routine is a replacement malloc() that is used to simulate
493** Out-Of-Memory (OOM) errors for testing purposes.
494*/
495static void *oomMalloc(int nByte){
496  if( oomCounter ){
497    if( oomCounter==1 ){
498      shellOomFault();
499      return 0;
500    }else{
501      oomCounter--;
502    }
503  }
504  return defaultMalloc(nByte);
505}
506#endif /* SQLITE_DEBUG */
507
508#ifdef SQLITE_DEBUG
509/* Register the OOM simulator.  This must occur before any memory
510** allocations */
511static void registerOomSimulator(void){
512  sqlite3_mem_methods mem;
513  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
514  defaultMalloc = mem.xMalloc;
515  mem.xMalloc = oomMalloc;
516  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
517}
518#endif
519
520/*
521** Write I/O traces to the following stream.
522*/
523#ifdef SQLITE_ENABLE_IOTRACE
524static FILE *iotrace = 0;
525#endif
526
527/*
528** This routine works like printf in that its first argument is a
529** format string and subsequent arguments are values to be substituted
530** in place of % fields.  The result of formatting this string
531** is written to iotrace.
532*/
533#ifdef SQLITE_ENABLE_IOTRACE
534static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
535  va_list ap;
536  char *z;
537  if( iotrace==0 ) return;
538  va_start(ap, zFormat);
539  z = sqlite3_vmprintf(zFormat, ap);
540  va_end(ap);
541  utf8_printf(iotrace, "%s", z);
542  sqlite3_free(z);
543}
544#endif
545
546/*
547** Output string zUtf to stream pOut as w characters.  If w is negative,
548** then right-justify the text.  W is the width in UTF-8 characters, not
549** in bytes.  This is different from the %*.*s specification in printf
550** since with %*.*s the width is measured in bytes, not characters.
551*/
552static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
553  int i;
554  int n;
555  int aw = w<0 ? -w : w;
556  for(i=n=0; zUtf[i]; i++){
557    if( (zUtf[i]&0xc0)!=0x80 ){
558      n++;
559      if( n==aw ){
560        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
561        break;
562      }
563    }
564  }
565  if( n>=aw ){
566    utf8_printf(pOut, "%.*s", i, zUtf);
567  }else if( w<0 ){
568    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
569  }else{
570    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
571  }
572}
573
574
575/*
576** Determines if a string is a number of not.
577*/
578static int isNumber(const char *z, int *realnum){
579  if( *z=='-' || *z=='+' ) z++;
580  if( !IsDigit(*z) ){
581    return 0;
582  }
583  z++;
584  if( realnum ) *realnum = 0;
585  while( IsDigit(*z) ){ z++; }
586  if( *z=='.' ){
587    z++;
588    if( !IsDigit(*z) ) return 0;
589    while( IsDigit(*z) ){ z++; }
590    if( realnum ) *realnum = 1;
591  }
592  if( *z=='e' || *z=='E' ){
593    z++;
594    if( *z=='+' || *z=='-' ) z++;
595    if( !IsDigit(*z) ) return 0;
596    while( IsDigit(*z) ){ z++; }
597    if( realnum ) *realnum = 1;
598  }
599  return *z==0;
600}
601
602/*
603** Compute a string length that is limited to what can be stored in
604** lower 30 bits of a 32-bit signed integer.
605*/
606static int strlen30(const char *z){
607  const char *z2 = z;
608  while( *z2 ){ z2++; }
609  return 0x3fffffff & (int)(z2 - z);
610}
611
612/*
613** Return the length of a string in characters.  Multibyte UTF8 characters
614** count as a single character.
615*/
616static int strlenChar(const char *z){
617  int n = 0;
618  while( *z ){
619    if( (0xc0&*(z++))!=0x80 ) n++;
620  }
621  return n;
622}
623
624/*
625** Return true if zFile does not exist or if it is not an ordinary file.
626*/
627#ifdef _WIN32
628# define notNormalFile(X) 0
629#else
630static int notNormalFile(const char *zFile){
631  struct stat x;
632  int rc;
633  memset(&x, 0, sizeof(x));
634  rc = stat(zFile, &x);
635  return rc || !S_ISREG(x.st_mode);
636}
637#endif
638
639/*
640** This routine reads a line of text from FILE in, stores
641** the text in memory obtained from malloc() and returns a pointer
642** to the text.  NULL is returned at end of file, or if malloc()
643** fails.
644**
645** If zLine is not NULL then it is a malloced buffer returned from
646** a previous call to this routine that may be reused.
647*/
648static char *local_getline(char *zLine, FILE *in){
649  int nLine = zLine==0 ? 0 : 100;
650  int n = 0;
651
652  while( 1 ){
653    if( n+100>nLine ){
654      nLine = nLine*2 + 100;
655      zLine = realloc(zLine, nLine);
656      if( zLine==0 ) shell_out_of_memory();
657    }
658    if( fgets(&zLine[n], nLine - n, in)==0 ){
659      if( n==0 ){
660        free(zLine);
661        return 0;
662      }
663      zLine[n] = 0;
664      break;
665    }
666    while( zLine[n] ) n++;
667    if( n>0 && zLine[n-1]=='\n' ){
668      n--;
669      if( n>0 && zLine[n-1]=='\r' ) n--;
670      zLine[n] = 0;
671      break;
672    }
673  }
674#if defined(_WIN32) || defined(WIN32)
675  /* For interactive input on Windows systems, translate the
676  ** multi-byte characterset characters into UTF-8. */
677  if( stdin_is_interactive && in==stdin ){
678    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
679    if( zTrans ){
680      int nTrans = strlen30(zTrans)+1;
681      if( nTrans>nLine ){
682        zLine = realloc(zLine, nTrans);
683        if( zLine==0 ) shell_out_of_memory();
684      }
685      memcpy(zLine, zTrans, nTrans);
686      sqlite3_free(zTrans);
687    }
688  }
689#endif /* defined(_WIN32) || defined(WIN32) */
690  return zLine;
691}
692
693/*
694** Retrieve a single line of input text.
695**
696** If in==0 then read from standard input and prompt before each line.
697** If isContinuation is true, then a continuation prompt is appropriate.
698** If isContinuation is zero, then the main prompt should be used.
699**
700** If zPrior is not NULL then it is a buffer from a prior call to this
701** routine that can be reused.
702**
703** The result is stored in space obtained from malloc() and must either
704** be freed by the caller or else passed back into this routine via the
705** zPrior argument for reuse.
706*/
707static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
708  char *zPrompt;
709  char *zResult;
710  if( in!=0 ){
711    zResult = local_getline(zPrior, in);
712  }else{
713    zPrompt = isContinuation ? continuePrompt : mainPrompt;
714#if SHELL_USE_LOCAL_GETLINE
715    printf("%s", zPrompt);
716    fflush(stdout);
717    zResult = local_getline(zPrior, stdin);
718#else
719    free(zPrior);
720    zResult = shell_readline(zPrompt);
721    if( zResult && *zResult ) shell_add_history(zResult);
722#endif
723  }
724  return zResult;
725}
726
727
728/*
729** Return the value of a hexadecimal digit.  Return -1 if the input
730** is not a hex digit.
731*/
732static int hexDigitValue(char c){
733  if( c>='0' && c<='9' ) return c - '0';
734  if( c>='a' && c<='f' ) return c - 'a' + 10;
735  if( c>='A' && c<='F' ) return c - 'A' + 10;
736  return -1;
737}
738
739/*
740** Interpret zArg as an integer value, possibly with suffixes.
741*/
742static sqlite3_int64 integerValue(const char *zArg){
743  sqlite3_int64 v = 0;
744  static const struct { char *zSuffix; int iMult; } aMult[] = {
745    { "KiB", 1024 },
746    { "MiB", 1024*1024 },
747    { "GiB", 1024*1024*1024 },
748    { "KB",  1000 },
749    { "MB",  1000000 },
750    { "GB",  1000000000 },
751    { "K",   1000 },
752    { "M",   1000000 },
753    { "G",   1000000000 },
754  };
755  int i;
756  int isNeg = 0;
757  if( zArg[0]=='-' ){
758    isNeg = 1;
759    zArg++;
760  }else if( zArg[0]=='+' ){
761    zArg++;
762  }
763  if( zArg[0]=='0' && zArg[1]=='x' ){
764    int x;
765    zArg += 2;
766    while( (x = hexDigitValue(zArg[0]))>=0 ){
767      v = (v<<4) + x;
768      zArg++;
769    }
770  }else{
771    while( IsDigit(zArg[0]) ){
772      v = v*10 + zArg[0] - '0';
773      zArg++;
774    }
775  }
776  for(i=0; i<ArraySize(aMult); i++){
777    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
778      v *= aMult[i].iMult;
779      break;
780    }
781  }
782  return isNeg? -v : v;
783}
784
785/*
786** A variable length string to which one can append text.
787*/
788typedef struct ShellText ShellText;
789struct ShellText {
790  char *z;
791  int n;
792  int nAlloc;
793};
794
795/*
796** Initialize and destroy a ShellText object
797*/
798static void initText(ShellText *p){
799  memset(p, 0, sizeof(*p));
800}
801static void freeText(ShellText *p){
802  free(p->z);
803  initText(p);
804}
805
806/* zIn is either a pointer to a NULL-terminated string in memory obtained
807** from malloc(), or a NULL pointer. The string pointed to by zAppend is
808** added to zIn, and the result returned in memory obtained from malloc().
809** zIn, if it was not NULL, is freed.
810**
811** If the third argument, quote, is not '\0', then it is used as a
812** quote character for zAppend.
813*/
814static void appendText(ShellText *p, char const *zAppend, char quote){
815  int len;
816  int i;
817  int nAppend = strlen30(zAppend);
818
819  len = nAppend+p->n+1;
820  if( quote ){
821    len += 2;
822    for(i=0; i<nAppend; i++){
823      if( zAppend[i]==quote ) len++;
824    }
825  }
826
827  if( p->n+len>=p->nAlloc ){
828    p->nAlloc = p->nAlloc*2 + len + 20;
829    p->z = realloc(p->z, p->nAlloc);
830    if( p->z==0 ) shell_out_of_memory();
831  }
832
833  if( quote ){
834    char *zCsr = p->z+p->n;
835    *zCsr++ = quote;
836    for(i=0; i<nAppend; i++){
837      *zCsr++ = zAppend[i];
838      if( zAppend[i]==quote ) *zCsr++ = quote;
839    }
840    *zCsr++ = quote;
841    p->n = (int)(zCsr - p->z);
842    *zCsr = '\0';
843  }else{
844    memcpy(p->z+p->n, zAppend, nAppend);
845    p->n += nAppend;
846    p->z[p->n] = '\0';
847  }
848}
849
850/*
851** Attempt to determine if identifier zName needs to be quoted, either
852** because it contains non-alphanumeric characters, or because it is an
853** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
854** that quoting is required.
855**
856** Return '"' if quoting is required.  Return 0 if no quoting is required.
857*/
858static char quoteChar(const char *zName){
859  int i;
860  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
861  for(i=0; zName[i]; i++){
862    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
863  }
864  return sqlite3_keyword_check(zName, i) ? '"' : 0;
865}
866
867/*
868** Construct a fake object name and column list to describe the structure
869** of the view, virtual table, or table valued function zSchema.zName.
870*/
871static char *shellFakeSchema(
872  sqlite3 *db,            /* The database connection containing the vtab */
873  const char *zSchema,    /* Schema of the database holding the vtab */
874  const char *zName       /* The name of the virtual table */
875){
876  sqlite3_stmt *pStmt = 0;
877  char *zSql;
878  ShellText s;
879  char cQuote;
880  char *zDiv = "(";
881  int nRow = 0;
882
883  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
884                         zSchema ? zSchema : "main", zName);
885  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
886  sqlite3_free(zSql);
887  initText(&s);
888  if( zSchema ){
889    cQuote = quoteChar(zSchema);
890    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
891    appendText(&s, zSchema, cQuote);
892    appendText(&s, ".", 0);
893  }
894  cQuote = quoteChar(zName);
895  appendText(&s, zName, cQuote);
896  while( sqlite3_step(pStmt)==SQLITE_ROW ){
897    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
898    nRow++;
899    appendText(&s, zDiv, 0);
900    zDiv = ",";
901    cQuote = quoteChar(zCol);
902    appendText(&s, zCol, cQuote);
903  }
904  appendText(&s, ")", 0);
905  sqlite3_finalize(pStmt);
906  if( nRow==0 ){
907    freeText(&s);
908    s.z = 0;
909  }
910  return s.z;
911}
912
913/*
914** SQL function:  shell_module_schema(X)
915**
916** Return a fake schema for the table-valued function or eponymous virtual
917** table X.
918*/
919static void shellModuleSchema(
920  sqlite3_context *pCtx,
921  int nVal,
922  sqlite3_value **apVal
923){
924  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
925  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
926  UNUSED_PARAMETER(nVal);
927  if( zFake ){
928    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
929                        -1, sqlite3_free);
930    free(zFake);
931  }
932}
933
934/*
935** SQL function:  shell_add_schema(S,X)
936**
937** Add the schema name X to the CREATE statement in S and return the result.
938** Examples:
939**
940**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
941**
942** Also works on
943**
944**    CREATE INDEX
945**    CREATE UNIQUE INDEX
946**    CREATE VIEW
947**    CREATE TRIGGER
948**    CREATE VIRTUAL TABLE
949**
950** This UDF is used by the .schema command to insert the schema name of
951** attached databases into the middle of the sqlite_schema.sql field.
952*/
953static void shellAddSchemaName(
954  sqlite3_context *pCtx,
955  int nVal,
956  sqlite3_value **apVal
957){
958  static const char *aPrefix[] = {
959     "TABLE",
960     "INDEX",
961     "UNIQUE INDEX",
962     "VIEW",
963     "TRIGGER",
964     "VIRTUAL TABLE"
965  };
966  int i = 0;
967  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
968  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
969  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
970  sqlite3 *db = sqlite3_context_db_handle(pCtx);
971  UNUSED_PARAMETER(nVal);
972  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
973    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
974      int n = strlen30(aPrefix[i]);
975      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
976        char *z = 0;
977        char *zFake = 0;
978        if( zSchema ){
979          char cQuote = quoteChar(zSchema);
980          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
981            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
982          }else{
983            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
984          }
985        }
986        if( zName
987         && aPrefix[i][0]=='V'
988         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
989        ){
990          if( z==0 ){
991            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
992          }else{
993            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
994          }
995          free(zFake);
996        }
997        if( z ){
998          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
999          return;
1000        }
1001      }
1002    }
1003  }
1004  sqlite3_result_value(pCtx, apVal[0]);
1005}
1006
1007/*
1008** The source code for several run-time loadable extensions is inserted
1009** below by the ../tool/mkshellc.tcl script.  Before processing that included
1010** code, we need to override some macros to make the included program code
1011** work here in the middle of this regular program.
1012*/
1013#define SQLITE_EXTENSION_INIT1
1014#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1015
1016#if defined(_WIN32) && defined(_MSC_VER)
1017INCLUDE test_windirent.h
1018INCLUDE test_windirent.c
1019#define dirent DIRENT
1020#endif
1021INCLUDE ../ext/misc/shathree.c
1022INCLUDE ../ext/misc/fileio.c
1023INCLUDE ../ext/misc/completion.c
1024INCLUDE ../ext/misc/appendvfs.c
1025INCLUDE ../ext/misc/memtrace.c
1026INCLUDE ../ext/misc/uint.c
1027INCLUDE ../ext/misc/decimal.c
1028INCLUDE ../ext/misc/ieee754.c
1029INCLUDE ../ext/misc/series.c
1030#ifdef SQLITE_HAVE_ZLIB
1031INCLUDE ../ext/misc/zipfile.c
1032INCLUDE ../ext/misc/sqlar.c
1033#endif
1034INCLUDE ../ext/expert/sqlite3expert.h
1035INCLUDE ../ext/expert/sqlite3expert.c
1036
1037#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1038INCLUDE ../ext/misc/dbdata.c
1039#endif
1040
1041#if defined(SQLITE_ENABLE_SESSION)
1042/*
1043** State information for a single open session
1044*/
1045typedef struct OpenSession OpenSession;
1046struct OpenSession {
1047  char *zName;             /* Symbolic name for this session */
1048  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1049  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1050  sqlite3_session *p;      /* The open session */
1051};
1052#endif
1053
1054typedef struct ExpertInfo ExpertInfo;
1055struct ExpertInfo {
1056  sqlite3expert *pExpert;
1057  int bVerbose;
1058};
1059
1060/* A single line in the EQP output */
1061typedef struct EQPGraphRow EQPGraphRow;
1062struct EQPGraphRow {
1063  int iEqpId;           /* ID for this row */
1064  int iParentId;        /* ID of the parent row */
1065  EQPGraphRow *pNext;   /* Next row in sequence */
1066  char zText[1];        /* Text to display for this row */
1067};
1068
1069/* All EQP output is collected into an instance of the following */
1070typedef struct EQPGraph EQPGraph;
1071struct EQPGraph {
1072  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1073  EQPGraphRow *pLast;   /* Last element of the pRow list */
1074  char zPrefix[100];    /* Graph prefix */
1075};
1076
1077/*
1078** State information about the database connection is contained in an
1079** instance of the following structure.
1080*/
1081typedef struct ShellState ShellState;
1082struct ShellState {
1083  sqlite3 *db;           /* The database */
1084  u8 autoExplain;        /* Automatically turn on .explain mode */
1085  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1086  u8 autoEQPtest;        /* autoEQP is in test mode */
1087  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1088  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1089  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1090  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1091  u8 nEqpLevel;          /* Depth of the EQP output graph */
1092  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1093  unsigned statsOn;      /* True to display memory stats before each finalize */
1094  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1095  int outCount;          /* Revert to stdout when reaching zero */
1096  int cnt;               /* Number of records displayed so far */
1097  int lineno;            /* Line number of last line read from in */
1098  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1099  FILE *in;              /* Read commands from this stream */
1100  FILE *out;             /* Write results here */
1101  FILE *traceOut;        /* Output for sqlite3_trace() */
1102  int nErr;              /* Number of errors seen */
1103  int mode;              /* An output mode setting */
1104  int modePrior;         /* Saved mode */
1105  int cMode;             /* temporary output mode for the current query */
1106  int normalMode;        /* Output mode before ".explain on" */
1107  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1108  int showHeader;        /* True to show column names in List or Column mode */
1109  int nCheck;            /* Number of ".check" commands run */
1110  unsigned nProgress;    /* Number of progress callbacks encountered */
1111  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1112  unsigned flgProgress;  /* Flags for the progress callback */
1113  unsigned shellFlgs;    /* Various flags */
1114  unsigned priorShFlgs;  /* Saved copy of flags */
1115  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1116  char *zDestTable;      /* Name of destination table when MODE_Insert */
1117  char *zTempFile;       /* Temporary file that might need deleting */
1118  char zTestcase[30];    /* Name of current test case */
1119  char colSeparator[20]; /* Column separator character for several modes */
1120  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1121  char colSepPrior[20];  /* Saved column separator */
1122  char rowSepPrior[20];  /* Saved row separator */
1123  int *colWidth;         /* Requested width of each column in columnar modes */
1124  int *actualWidth;      /* Actual width of each column */
1125  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1126  char nullValue[20];    /* The text to print when a NULL comes back from
1127                         ** the database */
1128  char outfile[FILENAME_MAX]; /* Filename for *out */
1129  const char *zDbFilename;    /* name of the database file */
1130  char *zFreeOnClose;         /* Filename to free when closing */
1131  const char *zVfs;           /* Name of VFS to use */
1132  sqlite3_stmt *pStmt;   /* Current statement if any. */
1133  FILE *pLog;            /* Write log output here */
1134  int *aiIndent;         /* Array of indents used in MODE_Explain */
1135  int nIndent;           /* Size of array aiIndent[] */
1136  int iIndent;           /* Index of current op in aiIndent[] */
1137  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1138#if defined(SQLITE_ENABLE_SESSION)
1139  int nSession;             /* Number of active sessions */
1140  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1141#endif
1142  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1143};
1144
1145
1146/* Allowed values for ShellState.autoEQP
1147*/
1148#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1149#define AUTOEQP_on       1           /* Automatic EQP is on */
1150#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1151#define AUTOEQP_full     3           /* Show full EXPLAIN */
1152
1153/* Allowed values for ShellState.openMode
1154*/
1155#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1156#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1157#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1158#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1159#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1160#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1161#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1162
1163/* Allowed values for ShellState.eTraceType
1164*/
1165#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1166#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1167#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1168
1169/* Bits in the ShellState.flgProgress variable */
1170#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1171#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1172                                   ** callback limit is reached, and for each
1173                                   ** top-level SQL statement */
1174#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1175
1176/*
1177** These are the allowed shellFlgs values
1178*/
1179#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1180#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1181#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1182#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1183#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1184#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1185#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1186#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
1187#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1188#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1189
1190/*
1191** Macros for testing and setting shellFlgs
1192*/
1193#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1194#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1195#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1196
1197/*
1198** These are the allowed modes.
1199*/
1200#define MODE_Line     0  /* One column per line.  Blank line between records */
1201#define MODE_Column   1  /* One record per line in neat columns */
1202#define MODE_List     2  /* One record per line with a separator */
1203#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1204#define MODE_Html     4  /* Generate an XHTML table */
1205#define MODE_Insert   5  /* Generate SQL "insert" statements */
1206#define MODE_Quote    6  /* Quote values as for SQL */
1207#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1208#define MODE_Csv      8  /* Quote strings, numbers are plain */
1209#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1210#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1211#define MODE_Pretty  11  /* Pretty-print schemas */
1212#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1213#define MODE_Json    13  /* Output JSON */
1214#define MODE_Markdown 14 /* Markdown formatting */
1215#define MODE_Table   15  /* MySQL-style table formatting */
1216#define MODE_Box     16  /* Unicode box-drawing characters */
1217
1218static const char *modeDescr[] = {
1219  "line",
1220  "column",
1221  "list",
1222  "semi",
1223  "html",
1224  "insert",
1225  "quote",
1226  "tcl",
1227  "csv",
1228  "explain",
1229  "ascii",
1230  "prettyprint",
1231  "eqp",
1232  "json",
1233  "markdown",
1234  "table",
1235  "box"
1236};
1237
1238/*
1239** These are the column/row/line separators used by the various
1240** import/export modes.
1241*/
1242#define SEP_Column    "|"
1243#define SEP_Row       "\n"
1244#define SEP_Tab       "\t"
1245#define SEP_Space     " "
1246#define SEP_Comma     ","
1247#define SEP_CrLf      "\r\n"
1248#define SEP_Unit      "\x1F"
1249#define SEP_Record    "\x1E"
1250
1251/*
1252** A callback for the sqlite3_log() interface.
1253*/
1254static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1255  ShellState *p = (ShellState*)pArg;
1256  if( p->pLog==0 ) return;
1257  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1258  fflush(p->pLog);
1259}
1260
1261/*
1262** SQL function:  shell_putsnl(X)
1263**
1264** Write the text X to the screen (or whatever output is being directed)
1265** adding a newline at the end, and then return X.
1266*/
1267static void shellPutsFunc(
1268  sqlite3_context *pCtx,
1269  int nVal,
1270  sqlite3_value **apVal
1271){
1272  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1273  (void)nVal;
1274  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1275  sqlite3_result_value(pCtx, apVal[0]);
1276}
1277
1278/*
1279** SQL function:   edit(VALUE)
1280**                 edit(VALUE,EDITOR)
1281**
1282** These steps:
1283**
1284**     (1) Write VALUE into a temporary file.
1285**     (2) Run program EDITOR on that temporary file.
1286**     (3) Read the temporary file back and return its content as the result.
1287**     (4) Delete the temporary file
1288**
1289** If the EDITOR argument is omitted, use the value in the VISUAL
1290** environment variable.  If still there is no EDITOR, through an error.
1291**
1292** Also throw an error if the EDITOR program returns a non-zero exit code.
1293*/
1294#ifndef SQLITE_NOHAVE_SYSTEM
1295static void editFunc(
1296  sqlite3_context *context,
1297  int argc,
1298  sqlite3_value **argv
1299){
1300  const char *zEditor;
1301  char *zTempFile = 0;
1302  sqlite3 *db;
1303  char *zCmd = 0;
1304  int bBin;
1305  int rc;
1306  int hasCRNL = 0;
1307  FILE *f = 0;
1308  sqlite3_int64 sz;
1309  sqlite3_int64 x;
1310  unsigned char *p = 0;
1311
1312  if( argc==2 ){
1313    zEditor = (const char*)sqlite3_value_text(argv[1]);
1314  }else{
1315    zEditor = getenv("VISUAL");
1316  }
1317  if( zEditor==0 ){
1318    sqlite3_result_error(context, "no editor for edit()", -1);
1319    return;
1320  }
1321  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1322    sqlite3_result_error(context, "NULL input to edit()", -1);
1323    return;
1324  }
1325  db = sqlite3_context_db_handle(context);
1326  zTempFile = 0;
1327  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1328  if( zTempFile==0 ){
1329    sqlite3_uint64 r = 0;
1330    sqlite3_randomness(sizeof(r), &r);
1331    zTempFile = sqlite3_mprintf("temp%llx", r);
1332    if( zTempFile==0 ){
1333      sqlite3_result_error_nomem(context);
1334      return;
1335    }
1336  }
1337  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1338  /* When writing the file to be edited, do \n to \r\n conversions on systems
1339  ** that want \r\n line endings */
1340  f = fopen(zTempFile, bBin ? "wb" : "w");
1341  if( f==0 ){
1342    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1343    goto edit_func_end;
1344  }
1345  sz = sqlite3_value_bytes(argv[0]);
1346  if( bBin ){
1347    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1348  }else{
1349    const char *z = (const char*)sqlite3_value_text(argv[0]);
1350    /* Remember whether or not the value originally contained \r\n */
1351    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1352    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1353  }
1354  fclose(f);
1355  f = 0;
1356  if( x!=sz ){
1357    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1358    goto edit_func_end;
1359  }
1360  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1361  if( zCmd==0 ){
1362    sqlite3_result_error_nomem(context);
1363    goto edit_func_end;
1364  }
1365  rc = system(zCmd);
1366  sqlite3_free(zCmd);
1367  if( rc ){
1368    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1369    goto edit_func_end;
1370  }
1371  f = fopen(zTempFile, "rb");
1372  if( f==0 ){
1373    sqlite3_result_error(context,
1374      "edit() cannot reopen temp file after edit", -1);
1375    goto edit_func_end;
1376  }
1377  fseek(f, 0, SEEK_END);
1378  sz = ftell(f);
1379  rewind(f);
1380  p = sqlite3_malloc64( sz+1 );
1381  if( p==0 ){
1382    sqlite3_result_error_nomem(context);
1383    goto edit_func_end;
1384  }
1385  x = fread(p, 1, (size_t)sz, f);
1386  fclose(f);
1387  f = 0;
1388  if( x!=sz ){
1389    sqlite3_result_error(context, "could not read back the whole file", -1);
1390    goto edit_func_end;
1391  }
1392  if( bBin ){
1393    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1394  }else{
1395    sqlite3_int64 i, j;
1396    if( hasCRNL ){
1397      /* If the original contains \r\n then do no conversions back to \n */
1398      j = sz;
1399    }else{
1400      /* If the file did not originally contain \r\n then convert any new
1401      ** \r\n back into \n */
1402      for(i=j=0; i<sz; i++){
1403        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1404        p[j++] = p[i];
1405      }
1406      sz = j;
1407      p[sz] = 0;
1408    }
1409    sqlite3_result_text64(context, (const char*)p, sz,
1410                          sqlite3_free, SQLITE_UTF8);
1411  }
1412  p = 0;
1413
1414edit_func_end:
1415  if( f ) fclose(f);
1416  unlink(zTempFile);
1417  sqlite3_free(zTempFile);
1418  sqlite3_free(p);
1419}
1420#endif /* SQLITE_NOHAVE_SYSTEM */
1421
1422/*
1423** Save or restore the current output mode
1424*/
1425static void outputModePush(ShellState *p){
1426  p->modePrior = p->mode;
1427  p->priorShFlgs = p->shellFlgs;
1428  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1429  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1430}
1431static void outputModePop(ShellState *p){
1432  p->mode = p->modePrior;
1433  p->shellFlgs = p->priorShFlgs;
1434  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1435  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1436}
1437
1438/*
1439** Output the given string as a hex-encoded blob (eg. X'1234' )
1440*/
1441static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1442  int i;
1443  char *zBlob = (char *)pBlob;
1444  raw_printf(out,"X'");
1445  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1446  raw_printf(out,"'");
1447}
1448
1449/*
1450** Find a string that is not found anywhere in z[].  Return a pointer
1451** to that string.
1452**
1453** Try to use zA and zB first.  If both of those are already found in z[]
1454** then make up some string and store it in the buffer zBuf.
1455*/
1456static const char *unused_string(
1457  const char *z,                    /* Result must not appear anywhere in z */
1458  const char *zA, const char *zB,   /* Try these first */
1459  char *zBuf                        /* Space to store a generated string */
1460){
1461  unsigned i = 0;
1462  if( strstr(z, zA)==0 ) return zA;
1463  if( strstr(z, zB)==0 ) return zB;
1464  do{
1465    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1466  }while( strstr(z,zBuf)!=0 );
1467  return zBuf;
1468}
1469
1470/*
1471** Output the given string as a quoted string using SQL quoting conventions.
1472**
1473** See also: output_quoted_escaped_string()
1474*/
1475static void output_quoted_string(FILE *out, const char *z){
1476  int i;
1477  char c;
1478  setBinaryMode(out, 1);
1479  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1480  if( c==0 ){
1481    utf8_printf(out,"'%s'",z);
1482  }else{
1483    raw_printf(out, "'");
1484    while( *z ){
1485      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1486      if( c=='\'' ) i++;
1487      if( i ){
1488        utf8_printf(out, "%.*s", i, z);
1489        z += i;
1490      }
1491      if( c=='\'' ){
1492        raw_printf(out, "'");
1493        continue;
1494      }
1495      if( c==0 ){
1496        break;
1497      }
1498      z++;
1499    }
1500    raw_printf(out, "'");
1501  }
1502  setTextMode(out, 1);
1503}
1504
1505/*
1506** Output the given string as a quoted string using SQL quoting conventions.
1507** Additionallly , escape the "\n" and "\r" characters so that they do not
1508** get corrupted by end-of-line translation facilities in some operating
1509** systems.
1510**
1511** This is like output_quoted_string() but with the addition of the \r\n
1512** escape mechanism.
1513*/
1514static void output_quoted_escaped_string(FILE *out, const char *z){
1515  int i;
1516  char c;
1517  setBinaryMode(out, 1);
1518  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1519  if( c==0 ){
1520    utf8_printf(out,"'%s'",z);
1521  }else{
1522    const char *zNL = 0;
1523    const char *zCR = 0;
1524    int nNL = 0;
1525    int nCR = 0;
1526    char zBuf1[20], zBuf2[20];
1527    for(i=0; z[i]; i++){
1528      if( z[i]=='\n' ) nNL++;
1529      if( z[i]=='\r' ) nCR++;
1530    }
1531    if( nNL ){
1532      raw_printf(out, "replace(");
1533      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1534    }
1535    if( nCR ){
1536      raw_printf(out, "replace(");
1537      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1538    }
1539    raw_printf(out, "'");
1540    while( *z ){
1541      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1542      if( c=='\'' ) i++;
1543      if( i ){
1544        utf8_printf(out, "%.*s", i, z);
1545        z += i;
1546      }
1547      if( c=='\'' ){
1548        raw_printf(out, "'");
1549        continue;
1550      }
1551      if( c==0 ){
1552        break;
1553      }
1554      z++;
1555      if( c=='\n' ){
1556        raw_printf(out, "%s", zNL);
1557        continue;
1558      }
1559      raw_printf(out, "%s", zCR);
1560    }
1561    raw_printf(out, "'");
1562    if( nCR ){
1563      raw_printf(out, ",'%s',char(13))", zCR);
1564    }
1565    if( nNL ){
1566      raw_printf(out, ",'%s',char(10))", zNL);
1567    }
1568  }
1569  setTextMode(out, 1);
1570}
1571
1572/*
1573** Output the given string as a quoted according to C or TCL quoting rules.
1574*/
1575static void output_c_string(FILE *out, const char *z){
1576  unsigned int c;
1577  fputc('"', out);
1578  while( (c = *(z++))!=0 ){
1579    if( c=='\\' ){
1580      fputc(c, out);
1581      fputc(c, out);
1582    }else if( c=='"' ){
1583      fputc('\\', out);
1584      fputc('"', out);
1585    }else if( c=='\t' ){
1586      fputc('\\', out);
1587      fputc('t', out);
1588    }else if( c=='\n' ){
1589      fputc('\\', out);
1590      fputc('n', out);
1591    }else if( c=='\r' ){
1592      fputc('\\', out);
1593      fputc('r', out);
1594    }else if( !isprint(c&0xff) ){
1595      raw_printf(out, "\\%03o", c&0xff);
1596    }else{
1597      fputc(c, out);
1598    }
1599  }
1600  fputc('"', out);
1601}
1602
1603/*
1604** Output the given string as a quoted according to JSON quoting rules.
1605*/
1606static void output_json_string(FILE *out, const char *z, int n){
1607  unsigned int c;
1608  if( n<0 ) n = (int)strlen(z);
1609  fputc('"', out);
1610  while( n-- ){
1611    c = *(z++);
1612    if( c=='\\' || c=='"' ){
1613      fputc('\\', out);
1614      fputc(c, out);
1615    }else if( c<=0x1f ){
1616      fputc('\\', out);
1617      if( c=='\b' ){
1618        fputc('b', out);
1619      }else if( c=='\f' ){
1620        fputc('f', out);
1621      }else if( c=='\n' ){
1622        fputc('n', out);
1623      }else if( c=='\r' ){
1624        fputc('r', out);
1625      }else if( c=='\t' ){
1626        fputc('t', out);
1627      }else{
1628         raw_printf(out, "u%04x",c);
1629      }
1630    }else{
1631      fputc(c, out);
1632    }
1633  }
1634  fputc('"', out);
1635}
1636
1637/*
1638** Output the given string with characters that are special to
1639** HTML escaped.
1640*/
1641static void output_html_string(FILE *out, const char *z){
1642  int i;
1643  if( z==0 ) z = "";
1644  while( *z ){
1645    for(i=0;   z[i]
1646            && z[i]!='<'
1647            && z[i]!='&'
1648            && z[i]!='>'
1649            && z[i]!='\"'
1650            && z[i]!='\'';
1651        i++){}
1652    if( i>0 ){
1653      utf8_printf(out,"%.*s",i,z);
1654    }
1655    if( z[i]=='<' ){
1656      raw_printf(out,"&lt;");
1657    }else if( z[i]=='&' ){
1658      raw_printf(out,"&amp;");
1659    }else if( z[i]=='>' ){
1660      raw_printf(out,"&gt;");
1661    }else if( z[i]=='\"' ){
1662      raw_printf(out,"&quot;");
1663    }else if( z[i]=='\'' ){
1664      raw_printf(out,"&#39;");
1665    }else{
1666      break;
1667    }
1668    z += i + 1;
1669  }
1670}
1671
1672/*
1673** If a field contains any character identified by a 1 in the following
1674** array, then the string must be quoted for CSV.
1675*/
1676static const char needCsvQuote[] = {
1677  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1678  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1679  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1680  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1681  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1682  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1683  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1684  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1685  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1686  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1687  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1688  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1689  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1690  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1691  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1692  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1693};
1694
1695/*
1696** Output a single term of CSV.  Actually, p->colSeparator is used for
1697** the separator, which may or may not be a comma.  p->nullValue is
1698** the null value.  Strings are quoted if necessary.  The separator
1699** is only issued if bSep is true.
1700*/
1701static void output_csv(ShellState *p, const char *z, int bSep){
1702  FILE *out = p->out;
1703  if( z==0 ){
1704    utf8_printf(out,"%s",p->nullValue);
1705  }else{
1706    int i;
1707    int nSep = strlen30(p->colSeparator);
1708    for(i=0; z[i]; i++){
1709      if( needCsvQuote[((unsigned char*)z)[i]]
1710         || (z[i]==p->colSeparator[0] &&
1711             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1712        i = 0;
1713        break;
1714      }
1715    }
1716    if( i==0 ){
1717      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1718      utf8_printf(out, "%s", zQuoted);
1719      sqlite3_free(zQuoted);
1720    }else{
1721      utf8_printf(out, "%s", z);
1722    }
1723  }
1724  if( bSep ){
1725    utf8_printf(p->out, "%s", p->colSeparator);
1726  }
1727}
1728
1729/*
1730** This routine runs when the user presses Ctrl-C
1731*/
1732static void interrupt_handler(int NotUsed){
1733  UNUSED_PARAMETER(NotUsed);
1734  seenInterrupt++;
1735  if( seenInterrupt>2 ) exit(1);
1736  if( globalDb ) sqlite3_interrupt(globalDb);
1737}
1738
1739#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1740/*
1741** This routine runs for console events (e.g. Ctrl-C) on Win32
1742*/
1743static BOOL WINAPI ConsoleCtrlHandler(
1744  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1745){
1746  if( dwCtrlType==CTRL_C_EVENT ){
1747    interrupt_handler(0);
1748    return TRUE;
1749  }
1750  return FALSE;
1751}
1752#endif
1753
1754#ifndef SQLITE_OMIT_AUTHORIZATION
1755/*
1756** When the ".auth ON" is set, the following authorizer callback is
1757** invoked.  It always returns SQLITE_OK.
1758*/
1759static int shellAuth(
1760  void *pClientData,
1761  int op,
1762  const char *zA1,
1763  const char *zA2,
1764  const char *zA3,
1765  const char *zA4
1766){
1767  ShellState *p = (ShellState*)pClientData;
1768  static const char *azAction[] = { 0,
1769     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1770     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1771     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1772     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1773     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1774     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1775     "PRAGMA",               "READ",                 "SELECT",
1776     "TRANSACTION",          "UPDATE",               "ATTACH",
1777     "DETACH",               "ALTER_TABLE",          "REINDEX",
1778     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1779     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1780  };
1781  int i;
1782  const char *az[4];
1783  az[0] = zA1;
1784  az[1] = zA2;
1785  az[2] = zA3;
1786  az[3] = zA4;
1787  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1788  for(i=0; i<4; i++){
1789    raw_printf(p->out, " ");
1790    if( az[i] ){
1791      output_c_string(p->out, az[i]);
1792    }else{
1793      raw_printf(p->out, "NULL");
1794    }
1795  }
1796  raw_printf(p->out, "\n");
1797  return SQLITE_OK;
1798}
1799#endif
1800
1801/*
1802** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1803**
1804** This routine converts some CREATE TABLE statements for shadow tables
1805** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1806*/
1807static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1808  if( z==0 ) return;
1809  if( zTail==0 ) return;
1810  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1811    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1812  }else{
1813    utf8_printf(out, "%s%s", z, zTail);
1814  }
1815}
1816static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1817  char c = z[n];
1818  z[n] = 0;
1819  printSchemaLine(out, z, zTail);
1820  z[n] = c;
1821}
1822
1823/*
1824** Return true if string z[] has nothing but whitespace and comments to the
1825** end of the first line.
1826*/
1827static int wsToEol(const char *z){
1828  int i;
1829  for(i=0; z[i]; i++){
1830    if( z[i]=='\n' ) return 1;
1831    if( IsSpace(z[i]) ) continue;
1832    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1833    return 0;
1834  }
1835  return 1;
1836}
1837
1838/*
1839** Add a new entry to the EXPLAIN QUERY PLAN data
1840*/
1841static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1842  EQPGraphRow *pNew;
1843  int nText = strlen30(zText);
1844  if( p->autoEQPtest ){
1845    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1846  }
1847  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1848  if( pNew==0 ) shell_out_of_memory();
1849  pNew->iEqpId = iEqpId;
1850  pNew->iParentId = p2;
1851  memcpy(pNew->zText, zText, nText+1);
1852  pNew->pNext = 0;
1853  if( p->sGraph.pLast ){
1854    p->sGraph.pLast->pNext = pNew;
1855  }else{
1856    p->sGraph.pRow = pNew;
1857  }
1858  p->sGraph.pLast = pNew;
1859}
1860
1861/*
1862** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1863** in p->sGraph.
1864*/
1865static void eqp_reset(ShellState *p){
1866  EQPGraphRow *pRow, *pNext;
1867  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1868    pNext = pRow->pNext;
1869    sqlite3_free(pRow);
1870  }
1871  memset(&p->sGraph, 0, sizeof(p->sGraph));
1872}
1873
1874/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1875** pOld, or return the first such line if pOld is NULL
1876*/
1877static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1878  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1879  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1880  return pRow;
1881}
1882
1883/* Render a single level of the graph that has iEqpId as its parent.  Called
1884** recursively to render sublevels.
1885*/
1886static void eqp_render_level(ShellState *p, int iEqpId){
1887  EQPGraphRow *pRow, *pNext;
1888  int n = strlen30(p->sGraph.zPrefix);
1889  char *z;
1890  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1891    pNext = eqp_next_row(p, iEqpId, pRow);
1892    z = pRow->zText;
1893    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1894                pNext ? "|--" : "`--", z);
1895    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1896      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1897      eqp_render_level(p, pRow->iEqpId);
1898      p->sGraph.zPrefix[n] = 0;
1899    }
1900  }
1901}
1902
1903/*
1904** Display and reset the EXPLAIN QUERY PLAN data
1905*/
1906static void eqp_render(ShellState *p){
1907  EQPGraphRow *pRow = p->sGraph.pRow;
1908  if( pRow ){
1909    if( pRow->zText[0]=='-' ){
1910      if( pRow->pNext==0 ){
1911        eqp_reset(p);
1912        return;
1913      }
1914      utf8_printf(p->out, "%s\n", pRow->zText+3);
1915      p->sGraph.pRow = pRow->pNext;
1916      sqlite3_free(pRow);
1917    }else{
1918      utf8_printf(p->out, "QUERY PLAN\n");
1919    }
1920    p->sGraph.zPrefix[0] = 0;
1921    eqp_render_level(p, 0);
1922    eqp_reset(p);
1923  }
1924}
1925
1926#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1927/*
1928** Progress handler callback.
1929*/
1930static int progress_handler(void *pClientData) {
1931  ShellState *p = (ShellState*)pClientData;
1932  p->nProgress++;
1933  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1934    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1935    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1936    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1937    return 1;
1938  }
1939  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1940    raw_printf(p->out, "Progress %u\n", p->nProgress);
1941  }
1942  return 0;
1943}
1944#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1945
1946/*
1947** Print N dashes
1948*/
1949static void print_dashes(FILE *out, int N){
1950  const char zDash[] = "--------------------------------------------------";
1951  const int nDash = sizeof(zDash) - 1;
1952  while( N>nDash ){
1953    fputs(zDash, out);
1954    N -= nDash;
1955  }
1956  raw_printf(out, "%.*s", N, zDash);
1957}
1958
1959/*
1960** Print a markdown or table-style row separator using ascii-art
1961*/
1962static void print_row_separator(
1963  ShellState *p,
1964  int nArg,
1965  const char *zSep
1966){
1967  int i;
1968  if( nArg>0 ){
1969    fputs(zSep, p->out);
1970    print_dashes(p->out, p->actualWidth[0]+2);
1971    for(i=1; i<nArg; i++){
1972      fputs(zSep, p->out);
1973      print_dashes(p->out, p->actualWidth[i]+2);
1974    }
1975    fputs(zSep, p->out);
1976  }
1977  fputs("\n", p->out);
1978}
1979
1980/*
1981** This is the callback routine that the shell
1982** invokes for each row of a query result.
1983*/
1984static int shell_callback(
1985  void *pArg,
1986  int nArg,        /* Number of result columns */
1987  char **azArg,    /* Text of each result column */
1988  char **azCol,    /* Column names */
1989  int *aiType      /* Column types.  Might be NULL */
1990){
1991  int i;
1992  ShellState *p = (ShellState*)pArg;
1993
1994  if( azArg==0 ) return 0;
1995  switch( p->cMode ){
1996    case MODE_Line: {
1997      int w = 5;
1998      if( azArg==0 ) break;
1999      for(i=0; i<nArg; i++){
2000        int len = strlen30(azCol[i] ? azCol[i] : "");
2001        if( len>w ) w = len;
2002      }
2003      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2004      for(i=0; i<nArg; i++){
2005        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2006                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2007      }
2008      break;
2009    }
2010    case MODE_Explain: {
2011      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2012      if( nArg>ArraySize(aExplainWidth) ){
2013        nArg = ArraySize(aExplainWidth);
2014      }
2015      if( p->cnt++==0 ){
2016        for(i=0; i<nArg; i++){
2017          int w = aExplainWidth[i];
2018          utf8_width_print(p->out, w, azCol[i]);
2019          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2020        }
2021        for(i=0; i<nArg; i++){
2022          int w = aExplainWidth[i];
2023          print_dashes(p->out, w);
2024          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2025        }
2026      }
2027      if( azArg==0 ) break;
2028      for(i=0; i<nArg; i++){
2029        int w = aExplainWidth[i];
2030        if( i==nArg-1 ) w = 0;
2031        if( azArg[i] && strlenChar(azArg[i])>w ){
2032          w = strlenChar(azArg[i]);
2033        }
2034        if( i==1 && p->aiIndent && p->pStmt ){
2035          if( p->iIndent<p->nIndent ){
2036            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2037          }
2038          p->iIndent++;
2039        }
2040        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2041        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2042      }
2043      break;
2044    }
2045    case MODE_Semi: {   /* .schema and .fullschema output */
2046      printSchemaLine(p->out, azArg[0], ";\n");
2047      break;
2048    }
2049    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2050      char *z;
2051      int j;
2052      int nParen = 0;
2053      char cEnd = 0;
2054      char c;
2055      int nLine = 0;
2056      assert( nArg==1 );
2057      if( azArg[0]==0 ) break;
2058      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2059       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2060      ){
2061        utf8_printf(p->out, "%s;\n", azArg[0]);
2062        break;
2063      }
2064      z = sqlite3_mprintf("%s", azArg[0]);
2065      j = 0;
2066      for(i=0; IsSpace(z[i]); i++){}
2067      for(; (c = z[i])!=0; i++){
2068        if( IsSpace(c) ){
2069          if( z[j-1]=='\r' ) z[j-1] = '\n';
2070          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2071        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2072          j--;
2073        }
2074        z[j++] = c;
2075      }
2076      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2077      z[j] = 0;
2078      if( strlen30(z)>=79 ){
2079        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2080          if( c==cEnd ){
2081            cEnd = 0;
2082          }else if( c=='"' || c=='\'' || c=='`' ){
2083            cEnd = c;
2084          }else if( c=='[' ){
2085            cEnd = ']';
2086          }else if( c=='-' && z[i+1]=='-' ){
2087            cEnd = '\n';
2088          }else if( c=='(' ){
2089            nParen++;
2090          }else if( c==')' ){
2091            nParen--;
2092            if( nLine>0 && nParen==0 && j>0 ){
2093              printSchemaLineN(p->out, z, j, "\n");
2094              j = 0;
2095            }
2096          }
2097          z[j++] = c;
2098          if( nParen==1 && cEnd==0
2099           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2100          ){
2101            if( c=='\n' ) j--;
2102            printSchemaLineN(p->out, z, j, "\n  ");
2103            j = 0;
2104            nLine++;
2105            while( IsSpace(z[i+1]) ){ i++; }
2106          }
2107        }
2108        z[j] = 0;
2109      }
2110      printSchemaLine(p->out, z, ";\n");
2111      sqlite3_free(z);
2112      break;
2113    }
2114    case MODE_List: {
2115      if( p->cnt++==0 && p->showHeader ){
2116        for(i=0; i<nArg; i++){
2117          utf8_printf(p->out,"%s%s",azCol[i],
2118                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2119        }
2120      }
2121      if( azArg==0 ) break;
2122      for(i=0; i<nArg; i++){
2123        char *z = azArg[i];
2124        if( z==0 ) z = p->nullValue;
2125        utf8_printf(p->out, "%s", z);
2126        if( i<nArg-1 ){
2127          utf8_printf(p->out, "%s", p->colSeparator);
2128        }else{
2129          utf8_printf(p->out, "%s", p->rowSeparator);
2130        }
2131      }
2132      break;
2133    }
2134    case MODE_Html: {
2135      if( p->cnt++==0 && p->showHeader ){
2136        raw_printf(p->out,"<TR>");
2137        for(i=0; i<nArg; i++){
2138          raw_printf(p->out,"<TH>");
2139          output_html_string(p->out, azCol[i]);
2140          raw_printf(p->out,"</TH>\n");
2141        }
2142        raw_printf(p->out,"</TR>\n");
2143      }
2144      if( azArg==0 ) break;
2145      raw_printf(p->out,"<TR>");
2146      for(i=0; i<nArg; i++){
2147        raw_printf(p->out,"<TD>");
2148        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2149        raw_printf(p->out,"</TD>\n");
2150      }
2151      raw_printf(p->out,"</TR>\n");
2152      break;
2153    }
2154    case MODE_Tcl: {
2155      if( p->cnt++==0 && p->showHeader ){
2156        for(i=0; i<nArg; i++){
2157          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2158          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2159        }
2160        utf8_printf(p->out, "%s", p->rowSeparator);
2161      }
2162      if( azArg==0 ) break;
2163      for(i=0; i<nArg; i++){
2164        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2165        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2166      }
2167      utf8_printf(p->out, "%s", p->rowSeparator);
2168      break;
2169    }
2170    case MODE_Csv: {
2171      setBinaryMode(p->out, 1);
2172      if( p->cnt++==0 && p->showHeader ){
2173        for(i=0; i<nArg; i++){
2174          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2175        }
2176        utf8_printf(p->out, "%s", p->rowSeparator);
2177      }
2178      if( nArg>0 ){
2179        for(i=0; i<nArg; i++){
2180          output_csv(p, azArg[i], i<nArg-1);
2181        }
2182        utf8_printf(p->out, "%s", p->rowSeparator);
2183      }
2184      setTextMode(p->out, 1);
2185      break;
2186    }
2187    case MODE_Insert: {
2188      if( azArg==0 ) break;
2189      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2190      if( p->showHeader ){
2191        raw_printf(p->out,"(");
2192        for(i=0; i<nArg; i++){
2193          if( i>0 ) raw_printf(p->out, ",");
2194          if( quoteChar(azCol[i]) ){
2195            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2196            utf8_printf(p->out, "%s", z);
2197            sqlite3_free(z);
2198          }else{
2199            raw_printf(p->out, "%s", azCol[i]);
2200          }
2201        }
2202        raw_printf(p->out,")");
2203      }
2204      p->cnt++;
2205      for(i=0; i<nArg; i++){
2206        raw_printf(p->out, i>0 ? "," : " VALUES(");
2207        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2208          utf8_printf(p->out,"NULL");
2209        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2210          if( ShellHasFlag(p, SHFLG_Newlines) ){
2211            output_quoted_string(p->out, azArg[i]);
2212          }else{
2213            output_quoted_escaped_string(p->out, azArg[i]);
2214          }
2215        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2216          utf8_printf(p->out,"%s", azArg[i]);
2217        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2218          char z[50];
2219          double r = sqlite3_column_double(p->pStmt, i);
2220          sqlite3_uint64 ur;
2221          memcpy(&ur,&r,sizeof(r));
2222          if( ur==0x7ff0000000000000LL ){
2223            raw_printf(p->out, "1e999");
2224          }else if( ur==0xfff0000000000000LL ){
2225            raw_printf(p->out, "-1e999");
2226          }else{
2227            sqlite3_snprintf(50,z,"%!.20g", r);
2228            raw_printf(p->out, "%s", z);
2229          }
2230        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2231          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2232          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2233          output_hex_blob(p->out, pBlob, nBlob);
2234        }else if( isNumber(azArg[i], 0) ){
2235          utf8_printf(p->out,"%s", azArg[i]);
2236        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2237          output_quoted_string(p->out, azArg[i]);
2238        }else{
2239          output_quoted_escaped_string(p->out, azArg[i]);
2240        }
2241      }
2242      raw_printf(p->out,");\n");
2243      break;
2244    }
2245    case MODE_Json: {
2246      if( azArg==0 ) break;
2247      if( p->cnt==0 ){
2248        fputs("[{", p->out);
2249      }else{
2250        fputs(",\n{", p->out);
2251      }
2252      p->cnt++;
2253      for(i=0; i<nArg; i++){
2254        output_json_string(p->out, azCol[i], -1);
2255        putc(':', p->out);
2256        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2257          fputs("null",p->out);
2258        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2259          char z[50];
2260          double r = sqlite3_column_double(p->pStmt, i);
2261          sqlite3_uint64 ur;
2262          memcpy(&ur,&r,sizeof(r));
2263          if( ur==0x7ff0000000000000LL ){
2264            raw_printf(p->out, "1e999");
2265          }else if( ur==0xfff0000000000000LL ){
2266            raw_printf(p->out, "-1e999");
2267          }else{
2268            sqlite3_snprintf(50,z,"%!.20g", r);
2269            raw_printf(p->out, "%s", z);
2270          }
2271        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2272          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2273          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2274          output_json_string(p->out, pBlob, nBlob);
2275        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2276          output_json_string(p->out, azArg[i], -1);
2277        }else{
2278          utf8_printf(p->out,"%s", azArg[i]);
2279        }
2280        if( i<nArg-1 ){
2281          putc(',', p->out);
2282        }
2283      }
2284      putc('}', p->out);
2285      break;
2286    }
2287    case MODE_Quote: {
2288      if( azArg==0 ) break;
2289      if( p->cnt==0 && p->showHeader ){
2290        for(i=0; i<nArg; i++){
2291          if( i>0 ) fputs(p->colSeparator, p->out);
2292          output_quoted_string(p->out, azCol[i]);
2293        }
2294        fputs(p->rowSeparator, p->out);
2295      }
2296      p->cnt++;
2297      for(i=0; i<nArg; i++){
2298        if( i>0 ) fputs(p->colSeparator, p->out);
2299        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2300          utf8_printf(p->out,"NULL");
2301        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2302          output_quoted_string(p->out, azArg[i]);
2303        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2304          utf8_printf(p->out,"%s", azArg[i]);
2305        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2306          char z[50];
2307          double r = sqlite3_column_double(p->pStmt, i);
2308          sqlite3_snprintf(50,z,"%!.20g", r);
2309          raw_printf(p->out, "%s", z);
2310        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2311          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2312          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2313          output_hex_blob(p->out, pBlob, nBlob);
2314        }else if( isNumber(azArg[i], 0) ){
2315          utf8_printf(p->out,"%s", azArg[i]);
2316        }else{
2317          output_quoted_string(p->out, azArg[i]);
2318        }
2319      }
2320      fputs(p->rowSeparator, p->out);
2321      break;
2322    }
2323    case MODE_Ascii: {
2324      if( p->cnt++==0 && p->showHeader ){
2325        for(i=0; i<nArg; i++){
2326          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2327          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2328        }
2329        utf8_printf(p->out, "%s", p->rowSeparator);
2330      }
2331      if( azArg==0 ) break;
2332      for(i=0; i<nArg; i++){
2333        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2334        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2335      }
2336      utf8_printf(p->out, "%s", p->rowSeparator);
2337      break;
2338    }
2339    case MODE_EQP: {
2340      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2341      break;
2342    }
2343  }
2344  return 0;
2345}
2346
2347/*
2348** This is the callback routine that the SQLite library
2349** invokes for each row of a query result.
2350*/
2351static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2352  /* since we don't have type info, call the shell_callback with a NULL value */
2353  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2354}
2355
2356/*
2357** This is the callback routine from sqlite3_exec() that appends all
2358** output onto the end of a ShellText object.
2359*/
2360static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2361  ShellText *p = (ShellText*)pArg;
2362  int i;
2363  UNUSED_PARAMETER(az);
2364  if( azArg==0 ) return 0;
2365  if( p->n ) appendText(p, "|", 0);
2366  for(i=0; i<nArg; i++){
2367    if( i ) appendText(p, ",", 0);
2368    if( azArg[i] ) appendText(p, azArg[i], 0);
2369  }
2370  return 0;
2371}
2372
2373/*
2374** Generate an appropriate SELFTEST table in the main database.
2375*/
2376static void createSelftestTable(ShellState *p){
2377  char *zErrMsg = 0;
2378  sqlite3_exec(p->db,
2379    "SAVEPOINT selftest_init;\n"
2380    "CREATE TABLE IF NOT EXISTS selftest(\n"
2381    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2382    "  op TEXT,\n"                   /* Operator:  memo run */
2383    "  cmd TEXT,\n"                  /* Command text */
2384    "  ans TEXT\n"                   /* Desired answer */
2385    ");"
2386    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2387    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2388    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2389    "         'memo','Tests generated by --init');\n"
2390    "INSERT INTO [_shell$self]\n"
2391    "  SELECT 'run',\n"
2392    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2393                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2394    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2395                          "FROM sqlite_schema ORDER BY 2',224));\n"
2396    "INSERT INTO [_shell$self]\n"
2397    "  SELECT 'run',"
2398    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2399    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2400    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2401    "  FROM (\n"
2402    "    SELECT name FROM sqlite_schema\n"
2403    "     WHERE type='table'\n"
2404    "       AND name<>'selftest'\n"
2405    "       AND coalesce(rootpage,0)>0\n"
2406    "  )\n"
2407    " ORDER BY name;\n"
2408    "INSERT INTO [_shell$self]\n"
2409    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2410    "INSERT INTO selftest(tno,op,cmd,ans)"
2411    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2412    "DROP TABLE [_shell$self];"
2413    ,0,0,&zErrMsg);
2414  if( zErrMsg ){
2415    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2416    sqlite3_free(zErrMsg);
2417  }
2418  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2419}
2420
2421
2422/*
2423** Set the destination table field of the ShellState structure to
2424** the name of the table given.  Escape any quote characters in the
2425** table name.
2426*/
2427static void set_table_name(ShellState *p, const char *zName){
2428  int i, n;
2429  char cQuote;
2430  char *z;
2431
2432  if( p->zDestTable ){
2433    free(p->zDestTable);
2434    p->zDestTable = 0;
2435  }
2436  if( zName==0 ) return;
2437  cQuote = quoteChar(zName);
2438  n = strlen30(zName);
2439  if( cQuote ) n += n+2;
2440  z = p->zDestTable = malloc( n+1 );
2441  if( z==0 ) shell_out_of_memory();
2442  n = 0;
2443  if( cQuote ) z[n++] = cQuote;
2444  for(i=0; zName[i]; i++){
2445    z[n++] = zName[i];
2446    if( zName[i]==cQuote ) z[n++] = cQuote;
2447  }
2448  if( cQuote ) z[n++] = cQuote;
2449  z[n] = 0;
2450}
2451
2452
2453/*
2454** Execute a query statement that will generate SQL output.  Print
2455** the result columns, comma-separated, on a line and then add a
2456** semicolon terminator to the end of that line.
2457**
2458** If the number of columns is 1 and that column contains text "--"
2459** then write the semicolon on a separate line.  That way, if a
2460** "--" comment occurs at the end of the statement, the comment
2461** won't consume the semicolon terminator.
2462*/
2463static int run_table_dump_query(
2464  ShellState *p,           /* Query context */
2465  const char *zSelect      /* SELECT statement to extract content */
2466){
2467  sqlite3_stmt *pSelect;
2468  int rc;
2469  int nResult;
2470  int i;
2471  const char *z;
2472  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2473  if( rc!=SQLITE_OK || !pSelect ){
2474    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2475                sqlite3_errmsg(p->db));
2476    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2477    return rc;
2478  }
2479  rc = sqlite3_step(pSelect);
2480  nResult = sqlite3_column_count(pSelect);
2481  while( rc==SQLITE_ROW ){
2482    z = (const char*)sqlite3_column_text(pSelect, 0);
2483    utf8_printf(p->out, "%s", z);
2484    for(i=1; i<nResult; i++){
2485      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2486    }
2487    if( z==0 ) z = "";
2488    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2489    if( z[0] ){
2490      raw_printf(p->out, "\n;\n");
2491    }else{
2492      raw_printf(p->out, ";\n");
2493    }
2494    rc = sqlite3_step(pSelect);
2495  }
2496  rc = sqlite3_finalize(pSelect);
2497  if( rc!=SQLITE_OK ){
2498    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2499                sqlite3_errmsg(p->db));
2500    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2501  }
2502  return rc;
2503}
2504
2505/*
2506** Allocate space and save off current error string.
2507*/
2508static char *save_err_msg(
2509  sqlite3 *db            /* Database to query */
2510){
2511  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2512  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2513  if( zErrMsg ){
2514    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2515  }
2516  return zErrMsg;
2517}
2518
2519#ifdef __linux__
2520/*
2521** Attempt to display I/O stats on Linux using /proc/PID/io
2522*/
2523static void displayLinuxIoStats(FILE *out){
2524  FILE *in;
2525  char z[200];
2526  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2527  in = fopen(z, "rb");
2528  if( in==0 ) return;
2529  while( fgets(z, sizeof(z), in)!=0 ){
2530    static const struct {
2531      const char *zPattern;
2532      const char *zDesc;
2533    } aTrans[] = {
2534      { "rchar: ",                  "Bytes received by read():" },
2535      { "wchar: ",                  "Bytes sent to write():"    },
2536      { "syscr: ",                  "Read() system calls:"      },
2537      { "syscw: ",                  "Write() system calls:"     },
2538      { "read_bytes: ",             "Bytes read from storage:"  },
2539      { "write_bytes: ",            "Bytes written to storage:" },
2540      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2541    };
2542    int i;
2543    for(i=0; i<ArraySize(aTrans); i++){
2544      int n = strlen30(aTrans[i].zPattern);
2545      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2546        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2547        break;
2548      }
2549    }
2550  }
2551  fclose(in);
2552}
2553#endif
2554
2555/*
2556** Display a single line of status using 64-bit values.
2557*/
2558static void displayStatLine(
2559  ShellState *p,            /* The shell context */
2560  char *zLabel,             /* Label for this one line */
2561  char *zFormat,            /* Format for the result */
2562  int iStatusCtrl,          /* Which status to display */
2563  int bReset                /* True to reset the stats */
2564){
2565  sqlite3_int64 iCur = -1;
2566  sqlite3_int64 iHiwtr = -1;
2567  int i, nPercent;
2568  char zLine[200];
2569  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2570  for(i=0, nPercent=0; zFormat[i]; i++){
2571    if( zFormat[i]=='%' ) nPercent++;
2572  }
2573  if( nPercent>1 ){
2574    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2575  }else{
2576    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2577  }
2578  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2579}
2580
2581/*
2582** Display memory stats.
2583*/
2584static int display_stats(
2585  sqlite3 *db,                /* Database to query */
2586  ShellState *pArg,           /* Pointer to ShellState */
2587  int bReset                  /* True to reset the stats */
2588){
2589  int iCur;
2590  int iHiwtr;
2591  FILE *out;
2592  if( pArg==0 || pArg->out==0 ) return 0;
2593  out = pArg->out;
2594
2595  if( pArg->pStmt && pArg->statsOn==2 ){
2596    int nCol, i, x;
2597    sqlite3_stmt *pStmt = pArg->pStmt;
2598    char z[100];
2599    nCol = sqlite3_column_count(pStmt);
2600    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2601    for(i=0; i<nCol; i++){
2602      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2603      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2604#ifndef SQLITE_OMIT_DECLTYPE
2605      sqlite3_snprintf(30, z+x, "declared type:");
2606      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2607#endif
2608#ifdef SQLITE_ENABLE_COLUMN_METADATA
2609      sqlite3_snprintf(30, z+x, "database name:");
2610      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2611      sqlite3_snprintf(30, z+x, "table name:");
2612      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2613      sqlite3_snprintf(30, z+x, "origin name:");
2614      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2615#endif
2616    }
2617  }
2618
2619  if( pArg->statsOn==3 ){
2620    if( pArg->pStmt ){
2621      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2622      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2623    }
2624    return 0;
2625  }
2626
2627  displayStatLine(pArg, "Memory Used:",
2628     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2629  displayStatLine(pArg, "Number of Outstanding Allocations:",
2630     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2631  if( pArg->shellFlgs & SHFLG_Pagecache ){
2632    displayStatLine(pArg, "Number of Pcache Pages Used:",
2633       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2634  }
2635  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2636     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2637  displayStatLine(pArg, "Largest Allocation:",
2638     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2639  displayStatLine(pArg, "Largest Pcache Allocation:",
2640     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2641#ifdef YYTRACKMAXSTACKDEPTH
2642  displayStatLine(pArg, "Deepest Parser Stack:",
2643     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2644#endif
2645
2646  if( db ){
2647    if( pArg->shellFlgs & SHFLG_Lookaside ){
2648      iHiwtr = iCur = -1;
2649      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2650                        &iCur, &iHiwtr, bReset);
2651      raw_printf(pArg->out,
2652              "Lookaside Slots Used:                %d (max %d)\n",
2653              iCur, iHiwtr);
2654      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2655                        &iCur, &iHiwtr, bReset);
2656      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2657              iHiwtr);
2658      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2659                        &iCur, &iHiwtr, bReset);
2660      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2661              iHiwtr);
2662      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2663                        &iCur, &iHiwtr, bReset);
2664      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2665              iHiwtr);
2666    }
2667    iHiwtr = iCur = -1;
2668    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2669    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2670            iCur);
2671    iHiwtr = iCur = -1;
2672    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2673    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2674    iHiwtr = iCur = -1;
2675    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2676    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2677    iHiwtr = iCur = -1;
2678    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2679    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2680    iHiwtr = iCur = -1;
2681    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2682    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2683    iHiwtr = iCur = -1;
2684    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2685    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2686            iCur);
2687    iHiwtr = iCur = -1;
2688    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2689    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2690            iCur);
2691  }
2692
2693  if( pArg->pStmt ){
2694    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2695                               bReset);
2696    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2697    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2698    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2699    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2700    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2701    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2702    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2703    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2704    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2705    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2706    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2707    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2708    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2709  }
2710
2711#ifdef __linux__
2712  displayLinuxIoStats(pArg->out);
2713#endif
2714
2715  /* Do not remove this machine readable comment: extra-stats-output-here */
2716
2717  return 0;
2718}
2719
2720/*
2721** Display scan stats.
2722*/
2723static void display_scanstats(
2724  sqlite3 *db,                    /* Database to query */
2725  ShellState *pArg                /* Pointer to ShellState */
2726){
2727#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2728  UNUSED_PARAMETER(db);
2729  UNUSED_PARAMETER(pArg);
2730#else
2731  int i, k, n, mx;
2732  raw_printf(pArg->out, "-------- scanstats --------\n");
2733  mx = 0;
2734  for(k=0; k<=mx; k++){
2735    double rEstLoop = 1.0;
2736    for(i=n=0; 1; i++){
2737      sqlite3_stmt *p = pArg->pStmt;
2738      sqlite3_int64 nLoop, nVisit;
2739      double rEst;
2740      int iSid;
2741      const char *zExplain;
2742      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2743        break;
2744      }
2745      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2746      if( iSid>mx ) mx = iSid;
2747      if( iSid!=k ) continue;
2748      if( n==0 ){
2749        rEstLoop = (double)nLoop;
2750        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2751      }
2752      n++;
2753      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2754      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2755      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2756      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2757      rEstLoop *= rEst;
2758      raw_printf(pArg->out,
2759          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2760          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2761      );
2762    }
2763  }
2764  raw_printf(pArg->out, "---------------------------\n");
2765#endif
2766}
2767
2768/*
2769** Parameter azArray points to a zero-terminated array of strings. zStr
2770** points to a single nul-terminated string. Return non-zero if zStr
2771** is equal, according to strcmp(), to any of the strings in the array.
2772** Otherwise, return zero.
2773*/
2774static int str_in_array(const char *zStr, const char **azArray){
2775  int i;
2776  for(i=0; azArray[i]; i++){
2777    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2778  }
2779  return 0;
2780}
2781
2782/*
2783** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2784** and populate the ShellState.aiIndent[] array with the number of
2785** spaces each opcode should be indented before it is output.
2786**
2787** The indenting rules are:
2788**
2789**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2790**       all opcodes that occur between the p2 jump destination and the opcode
2791**       itself by 2 spaces.
2792**
2793**     * For each "Goto", if the jump destination is earlier in the program
2794**       and ends on one of:
2795**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2796**       or if the P1 parameter is one instead of zero,
2797**       then indent all opcodes between the earlier instruction
2798**       and "Goto" by 2 spaces.
2799*/
2800static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2801  const char *zSql;               /* The text of the SQL statement */
2802  const char *z;                  /* Used to check if this is an EXPLAIN */
2803  int *abYield = 0;               /* True if op is an OP_Yield */
2804  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2805  int iOp;                        /* Index of operation in p->aiIndent[] */
2806
2807  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2808  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2809                            "Rewind", 0 };
2810  const char *azGoto[] = { "Goto", 0 };
2811
2812  /* Try to figure out if this is really an EXPLAIN statement. If this
2813  ** cannot be verified, return early.  */
2814  if( sqlite3_column_count(pSql)!=8 ){
2815    p->cMode = p->mode;
2816    return;
2817  }
2818  zSql = sqlite3_sql(pSql);
2819  if( zSql==0 ) return;
2820  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2821  if( sqlite3_strnicmp(z, "explain", 7) ){
2822    p->cMode = p->mode;
2823    return;
2824  }
2825
2826  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2827    int i;
2828    int iAddr = sqlite3_column_int(pSql, 0);
2829    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2830
2831    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2832    ** p2 is an instruction address, set variable p2op to the index of that
2833    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2834    ** the current instruction is part of a sub-program generated by an
2835    ** SQL trigger or foreign key.  */
2836    int p2 = sqlite3_column_int(pSql, 3);
2837    int p2op = (p2 + (iOp-iAddr));
2838
2839    /* Grow the p->aiIndent array as required */
2840    if( iOp>=nAlloc ){
2841      if( iOp==0 ){
2842        /* Do further verfication that this is explain output.  Abort if
2843        ** it is not */
2844        static const char *explainCols[] = {
2845           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2846        int jj;
2847        for(jj=0; jj<ArraySize(explainCols); jj++){
2848          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2849            p->cMode = p->mode;
2850            sqlite3_reset(pSql);
2851            return;
2852          }
2853        }
2854      }
2855      nAlloc += 100;
2856      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2857      if( p->aiIndent==0 ) shell_out_of_memory();
2858      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2859      if( abYield==0 ) shell_out_of_memory();
2860    }
2861    abYield[iOp] = str_in_array(zOp, azYield);
2862    p->aiIndent[iOp] = 0;
2863    p->nIndent = iOp+1;
2864
2865    if( str_in_array(zOp, azNext) ){
2866      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2867    }
2868    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2869     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2870    ){
2871      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2872    }
2873  }
2874
2875  p->iIndent = 0;
2876  sqlite3_free(abYield);
2877  sqlite3_reset(pSql);
2878}
2879
2880/*
2881** Free the array allocated by explain_data_prepare().
2882*/
2883static void explain_data_delete(ShellState *p){
2884  sqlite3_free(p->aiIndent);
2885  p->aiIndent = 0;
2886  p->nIndent = 0;
2887  p->iIndent = 0;
2888}
2889
2890/*
2891** Disable and restore .wheretrace and .selecttrace settings.
2892*/
2893static unsigned int savedSelectTrace;
2894static unsigned int savedWhereTrace;
2895static void disable_debug_trace_modes(void){
2896  unsigned int zero = 0;
2897  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
2898  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
2899  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
2900  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
2901}
2902static void restore_debug_trace_modes(void){
2903  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
2904  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
2905}
2906
2907/* Create the TEMP table used to store parameter bindings */
2908static void bind_table_init(ShellState *p){
2909  int wrSchema = 0;
2910  int defensiveMode = 0;
2911  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2912  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2913  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2914  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2915  sqlite3_exec(p->db,
2916    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2917    "  key TEXT PRIMARY KEY,\n"
2918    "  value\n"
2919    ") WITHOUT ROWID;",
2920    0, 0, 0);
2921  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2922  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2923}
2924
2925/*
2926** Bind parameters on a prepared statement.
2927**
2928** Parameter bindings are taken from a TEMP table of the form:
2929**
2930**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2931**    WITHOUT ROWID;
2932**
2933** No bindings occur if this table does not exist.  The name of the table
2934** begins with "sqlite_" so that it will not collide with ordinary application
2935** tables.  The table must be in the TEMP schema.
2936*/
2937static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2938  int nVar;
2939  int i;
2940  int rc;
2941  sqlite3_stmt *pQ = 0;
2942
2943  nVar = sqlite3_bind_parameter_count(pStmt);
2944  if( nVar==0 ) return;  /* Nothing to do */
2945  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2946                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2947    return; /* Parameter table does not exist */
2948  }
2949  rc = sqlite3_prepare_v2(pArg->db,
2950          "SELECT value FROM temp.sqlite_parameters"
2951          " WHERE key=?1", -1, &pQ, 0);
2952  if( rc || pQ==0 ) return;
2953  for(i=1; i<=nVar; i++){
2954    char zNum[30];
2955    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2956    if( zVar==0 ){
2957      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2958      zVar = zNum;
2959    }
2960    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2961    if( sqlite3_step(pQ)==SQLITE_ROW ){
2962      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2963    }else{
2964      sqlite3_bind_null(pStmt, i);
2965    }
2966    sqlite3_reset(pQ);
2967  }
2968  sqlite3_finalize(pQ);
2969}
2970
2971/*
2972** UTF8 box-drawing characters.  Imagine box lines like this:
2973**
2974**           1
2975**           |
2976**       4 --+-- 2
2977**           |
2978**           3
2979**
2980** Each box characters has between 2 and 4 of the lines leading from
2981** the center.  The characters are here identified by the numbers of
2982** their corresponding lines.
2983*/
2984#define BOX_24   "\342\224\200"  /* U+2500 --- */
2985#define BOX_13   "\342\224\202"  /* U+2502  |  */
2986#define BOX_23   "\342\224\214"  /* U+250c  ,- */
2987#define BOX_34   "\342\224\220"  /* U+2510 -,  */
2988#define BOX_12   "\342\224\224"  /* U+2514  '- */
2989#define BOX_14   "\342\224\230"  /* U+2518 -'  */
2990#define BOX_123  "\342\224\234"  /* U+251c  |- */
2991#define BOX_134  "\342\224\244"  /* U+2524 -|  */
2992#define BOX_234  "\342\224\254"  /* U+252c -,- */
2993#define BOX_124  "\342\224\264"  /* U+2534 -'- */
2994#define BOX_1234 "\342\224\274"  /* U+253c -|- */
2995
2996/* Draw horizontal line N characters long using unicode box
2997** characters
2998*/
2999static void print_box_line(FILE *out, int N){
3000  const char zDash[] =
3001      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3002      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3003  const int nDash = sizeof(zDash) - 1;
3004  N *= 3;
3005  while( N>nDash ){
3006    utf8_printf(out, zDash);
3007    N -= nDash;
3008  }
3009  utf8_printf(out, "%.*s", N, zDash);
3010}
3011
3012/*
3013** Draw a horizontal separator for a MODE_Box table.
3014*/
3015static void print_box_row_separator(
3016  ShellState *p,
3017  int nArg,
3018  const char *zSep1,
3019  const char *zSep2,
3020  const char *zSep3
3021){
3022  int i;
3023  if( nArg>0 ){
3024    utf8_printf(p->out, "%s", zSep1);
3025    print_box_line(p->out, p->actualWidth[0]+2);
3026    for(i=1; i<nArg; i++){
3027      utf8_printf(p->out, "%s", zSep2);
3028      print_box_line(p->out, p->actualWidth[i]+2);
3029    }
3030    utf8_printf(p->out, "%s", zSep3);
3031  }
3032  fputs("\n", p->out);
3033}
3034
3035
3036
3037/*
3038** Run a prepared statement and output the result in one of the
3039** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3040** or MODE_Box.
3041**
3042** This is different from ordinary exec_prepared_stmt() in that
3043** it has to run the entire query and gather the results into memory
3044** first, in order to determine column widths, before providing
3045** any output.
3046*/
3047static void exec_prepared_stmt_columnar(
3048  ShellState *p,                        /* Pointer to ShellState */
3049  sqlite3_stmt *pStmt                   /* Statment to run */
3050){
3051  sqlite3_int64 nRow = 0;
3052  int nColumn = 0;
3053  char **azData = 0;
3054  sqlite3_int64 nAlloc = 0;
3055  const char *z;
3056  int rc;
3057  sqlite3_int64 i, nData;
3058  int j, nTotal, w, n;
3059  const char *colSep = 0;
3060  const char *rowSep = 0;
3061
3062  rc = sqlite3_step(pStmt);
3063  if( rc!=SQLITE_ROW ) return;
3064  nColumn = sqlite3_column_count(pStmt);
3065  nAlloc = nColumn*4;
3066  if( nAlloc<=0 ) nAlloc = 1;
3067  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3068  if( azData==0 ) shell_out_of_memory();
3069  for(i=0; i<nColumn; i++){
3070    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3071  }
3072  do{
3073    if( (nRow+2)*nColumn >= nAlloc ){
3074      nAlloc *= 2;
3075      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3076      if( azData==0 ) shell_out_of_memory();
3077    }
3078    nRow++;
3079    for(i=0; i<nColumn; i++){
3080      z = (const char*)sqlite3_column_text(pStmt,i);
3081      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3082    }
3083  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
3084  if( nColumn>p->nWidth ){
3085    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3086    if( p->colWidth==0 ) shell_out_of_memory();
3087    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3088    p->nWidth = nColumn;
3089    p->actualWidth = &p->colWidth[nColumn];
3090  }
3091  memset(p->actualWidth, 0, nColumn*sizeof(int));
3092  for(i=0; i<nColumn; i++){
3093    w = p->colWidth[i];
3094    if( w<0 ) w = -w;
3095    p->actualWidth[i] = w;
3096  }
3097  nTotal = nColumn*(nRow+1);
3098  for(i=0; i<nTotal; i++){
3099    z = azData[i];
3100    if( z==0 ) z = p->nullValue;
3101    n = strlenChar(z);
3102    j = i%nColumn;
3103    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3104  }
3105  if( seenInterrupt ) goto columnar_end;
3106  if( nColumn==0 ) goto columnar_end;
3107  switch( p->cMode ){
3108    case MODE_Column: {
3109      colSep = "  ";
3110      rowSep = "\n";
3111      if( p->showHeader ){
3112        for(i=0; i<nColumn; i++){
3113          w = p->actualWidth[i];
3114          if( p->colWidth[i]<0 ) w = -w;
3115          utf8_width_print(p->out, w, azData[i]);
3116          fputs(i==nColumn-1?"\n":"  ", p->out);
3117        }
3118        for(i=0; i<nColumn; i++){
3119          print_dashes(p->out, p->actualWidth[i]);
3120          fputs(i==nColumn-1?"\n":"  ", p->out);
3121        }
3122      }
3123      break;
3124    }
3125    case MODE_Table: {
3126      colSep = " | ";
3127      rowSep = " |\n";
3128      print_row_separator(p, nColumn, "+");
3129      fputs("| ", p->out);
3130      for(i=0; i<nColumn; i++){
3131        w = p->actualWidth[i];
3132        n = strlenChar(azData[i]);
3133        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3134        fputs(i==nColumn-1?" |\n":" | ", p->out);
3135      }
3136      print_row_separator(p, nColumn, "+");
3137      break;
3138    }
3139    case MODE_Markdown: {
3140      colSep = " | ";
3141      rowSep = " |\n";
3142      fputs("| ", p->out);
3143      for(i=0; i<nColumn; i++){
3144        w = p->actualWidth[i];
3145        n = strlenChar(azData[i]);
3146        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3147        fputs(i==nColumn-1?" |\n":" | ", p->out);
3148      }
3149      print_row_separator(p, nColumn, "|");
3150      break;
3151    }
3152    case MODE_Box: {
3153      colSep = " " BOX_13 " ";
3154      rowSep = " " BOX_13 "\n";
3155      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3156      utf8_printf(p->out, BOX_13 " ");
3157      for(i=0; i<nColumn; i++){
3158        w = p->actualWidth[i];
3159        n = strlenChar(azData[i]);
3160        utf8_printf(p->out, "%*s%s%*s%s",
3161            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3162            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3163      }
3164      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3165      break;
3166    }
3167  }
3168  for(i=nColumn, j=0; i<nTotal; i++, j++){
3169    if( j==0 && p->cMode!=MODE_Column ){
3170      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3171    }
3172    z = azData[i];
3173    if( z==0 ) z = p->nullValue;
3174    w = p->actualWidth[j];
3175    if( p->colWidth[j]<0 ) w = -w;
3176    utf8_width_print(p->out, w, z);
3177    if( j==nColumn-1 ){
3178      utf8_printf(p->out, "%s", rowSep);
3179      j = -1;
3180      if( seenInterrupt ) goto columnar_end;
3181    }else{
3182      utf8_printf(p->out, "%s", colSep);
3183    }
3184  }
3185  if( p->cMode==MODE_Table ){
3186    print_row_separator(p, nColumn, "+");
3187  }else if( p->cMode==MODE_Box ){
3188    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3189  }
3190columnar_end:
3191  if( seenInterrupt ){
3192    utf8_printf(p->out, "Interrupt\n");
3193  }
3194  nData = (nRow+1)*nColumn;
3195  for(i=0; i<nData; i++) free(azData[i]);
3196  sqlite3_free(azData);
3197}
3198
3199/*
3200** Run a prepared statement
3201*/
3202static void exec_prepared_stmt(
3203  ShellState *pArg,                                /* Pointer to ShellState */
3204  sqlite3_stmt *pStmt                              /* Statment to run */
3205){
3206  int rc;
3207
3208  if( pArg->cMode==MODE_Column
3209   || pArg->cMode==MODE_Table
3210   || pArg->cMode==MODE_Box
3211   || pArg->cMode==MODE_Markdown
3212  ){
3213    exec_prepared_stmt_columnar(pArg, pStmt);
3214    return;
3215  }
3216
3217  /* perform the first step.  this will tell us if we
3218  ** have a result set or not and how wide it is.
3219  */
3220  rc = sqlite3_step(pStmt);
3221  /* if we have a result set... */
3222  if( SQLITE_ROW == rc ){
3223    /* allocate space for col name ptr, value ptr, and type */
3224    int nCol = sqlite3_column_count(pStmt);
3225    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3226    if( !pData ){
3227      rc = SQLITE_NOMEM;
3228    }else{
3229      char **azCols = (char **)pData;      /* Names of result columns */
3230      char **azVals = &azCols[nCol];       /* Results */
3231      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3232      int i, x;
3233      assert(sizeof(int) <= sizeof(char *));
3234      /* save off ptrs to column names */
3235      for(i=0; i<nCol; i++){
3236        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3237      }
3238      do{
3239        /* extract the data and data types */
3240        for(i=0; i<nCol; i++){
3241          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3242          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3243            azVals[i] = "";
3244          }else{
3245            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3246          }
3247          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3248            rc = SQLITE_NOMEM;
3249            break; /* from for */
3250          }
3251        } /* end for */
3252
3253        /* if data and types extracted successfully... */
3254        if( SQLITE_ROW == rc ){
3255          /* call the supplied callback with the result row data */
3256          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3257            rc = SQLITE_ABORT;
3258          }else{
3259            rc = sqlite3_step(pStmt);
3260          }
3261        }
3262      } while( SQLITE_ROW == rc );
3263      sqlite3_free(pData);
3264      if( pArg->cMode==MODE_Json ){
3265        fputs("]\n", pArg->out);
3266      }
3267    }
3268  }
3269}
3270
3271#ifndef SQLITE_OMIT_VIRTUALTABLE
3272/*
3273** This function is called to process SQL if the previous shell command
3274** was ".expert". It passes the SQL in the second argument directly to
3275** the sqlite3expert object.
3276**
3277** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3278** code. In this case, (*pzErr) may be set to point to a buffer containing
3279** an English language error message. It is the responsibility of the
3280** caller to eventually free this buffer using sqlite3_free().
3281*/
3282static int expertHandleSQL(
3283  ShellState *pState,
3284  const char *zSql,
3285  char **pzErr
3286){
3287  assert( pState->expert.pExpert );
3288  assert( pzErr==0 || *pzErr==0 );
3289  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3290}
3291
3292/*
3293** This function is called either to silently clean up the object
3294** created by the ".expert" command (if bCancel==1), or to generate a
3295** report from it and then clean it up (if bCancel==0).
3296**
3297** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3298** code. In this case, (*pzErr) may be set to point to a buffer containing
3299** an English language error message. It is the responsibility of the
3300** caller to eventually free this buffer using sqlite3_free().
3301*/
3302static int expertFinish(
3303  ShellState *pState,
3304  int bCancel,
3305  char **pzErr
3306){
3307  int rc = SQLITE_OK;
3308  sqlite3expert *p = pState->expert.pExpert;
3309  assert( p );
3310  assert( bCancel || pzErr==0 || *pzErr==0 );
3311  if( bCancel==0 ){
3312    FILE *out = pState->out;
3313    int bVerbose = pState->expert.bVerbose;
3314
3315    rc = sqlite3_expert_analyze(p, pzErr);
3316    if( rc==SQLITE_OK ){
3317      int nQuery = sqlite3_expert_count(p);
3318      int i;
3319
3320      if( bVerbose ){
3321        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3322        raw_printf(out, "-- Candidates -----------------------------\n");
3323        raw_printf(out, "%s\n", zCand);
3324      }
3325      for(i=0; i<nQuery; i++){
3326        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3327        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3328        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3329        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3330        if( bVerbose ){
3331          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3332          raw_printf(out, "%s\n\n", zSql);
3333        }
3334        raw_printf(out, "%s\n", zIdx);
3335        raw_printf(out, "%s\n", zEQP);
3336      }
3337    }
3338  }
3339  sqlite3_expert_destroy(p);
3340  pState->expert.pExpert = 0;
3341  return rc;
3342}
3343
3344/*
3345** Implementation of ".expert" dot command.
3346*/
3347static int expertDotCommand(
3348  ShellState *pState,             /* Current shell tool state */
3349  char **azArg,                   /* Array of arguments passed to dot command */
3350  int nArg                        /* Number of entries in azArg[] */
3351){
3352  int rc = SQLITE_OK;
3353  char *zErr = 0;
3354  int i;
3355  int iSample = 0;
3356
3357  assert( pState->expert.pExpert==0 );
3358  memset(&pState->expert, 0, sizeof(ExpertInfo));
3359
3360  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3361    char *z = azArg[i];
3362    int n;
3363    if( z[0]=='-' && z[1]=='-' ) z++;
3364    n = strlen30(z);
3365    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3366      pState->expert.bVerbose = 1;
3367    }
3368    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3369      if( i==(nArg-1) ){
3370        raw_printf(stderr, "option requires an argument: %s\n", z);
3371        rc = SQLITE_ERROR;
3372      }else{
3373        iSample = (int)integerValue(azArg[++i]);
3374        if( iSample<0 || iSample>100 ){
3375          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3376          rc = SQLITE_ERROR;
3377        }
3378      }
3379    }
3380    else{
3381      raw_printf(stderr, "unknown option: %s\n", z);
3382      rc = SQLITE_ERROR;
3383    }
3384  }
3385
3386  if( rc==SQLITE_OK ){
3387    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3388    if( pState->expert.pExpert==0 ){
3389      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3390      rc = SQLITE_ERROR;
3391    }else{
3392      sqlite3_expert_config(
3393          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3394      );
3395    }
3396  }
3397
3398  return rc;
3399}
3400#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3401
3402/*
3403** Execute a statement or set of statements.  Print
3404** any result rows/columns depending on the current mode
3405** set via the supplied callback.
3406**
3407** This is very similar to SQLite's built-in sqlite3_exec()
3408** function except it takes a slightly different callback
3409** and callback data argument.
3410*/
3411static int shell_exec(
3412  ShellState *pArg,                         /* Pointer to ShellState */
3413  const char *zSql,                         /* SQL to be evaluated */
3414  char **pzErrMsg                           /* Error msg written here */
3415){
3416  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3417  int rc = SQLITE_OK;             /* Return Code */
3418  int rc2;
3419  const char *zLeftover;          /* Tail of unprocessed SQL */
3420  sqlite3 *db = pArg->db;
3421
3422  if( pzErrMsg ){
3423    *pzErrMsg = NULL;
3424  }
3425
3426#ifndef SQLITE_OMIT_VIRTUALTABLE
3427  if( pArg->expert.pExpert ){
3428    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3429    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3430  }
3431#endif
3432
3433  while( zSql[0] && (SQLITE_OK == rc) ){
3434    static const char *zStmtSql;
3435    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3436    if( SQLITE_OK != rc ){
3437      if( pzErrMsg ){
3438        *pzErrMsg = save_err_msg(db);
3439      }
3440    }else{
3441      if( !pStmt ){
3442        /* this happens for a comment or white-space */
3443        zSql = zLeftover;
3444        while( IsSpace(zSql[0]) ) zSql++;
3445        continue;
3446      }
3447      zStmtSql = sqlite3_sql(pStmt);
3448      if( zStmtSql==0 ) zStmtSql = "";
3449      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3450
3451      /* save off the prepared statment handle and reset row count */
3452      if( pArg ){
3453        pArg->pStmt = pStmt;
3454        pArg->cnt = 0;
3455      }
3456
3457      /* echo the sql statement if echo on */
3458      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3459        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3460      }
3461
3462      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3463      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3464        sqlite3_stmt *pExplain;
3465        char *zEQP;
3466        int triggerEQP = 0;
3467        disable_debug_trace_modes();
3468        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3469        if( pArg->autoEQP>=AUTOEQP_trigger ){
3470          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3471        }
3472        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3473        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3474        if( rc==SQLITE_OK ){
3475          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3476            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3477            int iEqpId = sqlite3_column_int(pExplain, 0);
3478            int iParentId = sqlite3_column_int(pExplain, 1);
3479            if( zEQPLine==0 ) zEQPLine = "";
3480            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3481            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3482          }
3483          eqp_render(pArg);
3484        }
3485        sqlite3_finalize(pExplain);
3486        sqlite3_free(zEQP);
3487        if( pArg->autoEQP>=AUTOEQP_full ){
3488          /* Also do an EXPLAIN for ".eqp full" mode */
3489          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3490          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3491          if( rc==SQLITE_OK ){
3492            pArg->cMode = MODE_Explain;
3493            explain_data_prepare(pArg, pExplain);
3494            exec_prepared_stmt(pArg, pExplain);
3495            explain_data_delete(pArg);
3496          }
3497          sqlite3_finalize(pExplain);
3498          sqlite3_free(zEQP);
3499        }
3500        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3501          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3502          /* Reprepare pStmt before reactiving trace modes */
3503          sqlite3_finalize(pStmt);
3504          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3505          if( pArg ) pArg->pStmt = pStmt;
3506        }
3507        restore_debug_trace_modes();
3508      }
3509
3510      if( pArg ){
3511        pArg->cMode = pArg->mode;
3512        if( pArg->autoExplain ){
3513          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3514            pArg->cMode = MODE_Explain;
3515          }
3516          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3517            pArg->cMode = MODE_EQP;
3518          }
3519        }
3520
3521        /* If the shell is currently in ".explain" mode, gather the extra
3522        ** data required to add indents to the output.*/
3523        if( pArg->cMode==MODE_Explain ){
3524          explain_data_prepare(pArg, pStmt);
3525        }
3526      }
3527
3528      bind_prepared_stmt(pArg, pStmt);
3529      exec_prepared_stmt(pArg, pStmt);
3530      explain_data_delete(pArg);
3531      eqp_render(pArg);
3532
3533      /* print usage stats if stats on */
3534      if( pArg && pArg->statsOn ){
3535        display_stats(db, pArg, 0);
3536      }
3537
3538      /* print loop-counters if required */
3539      if( pArg && pArg->scanstatsOn ){
3540        display_scanstats(db, pArg);
3541      }
3542
3543      /* Finalize the statement just executed. If this fails, save a
3544      ** copy of the error message. Otherwise, set zSql to point to the
3545      ** next statement to execute. */
3546      rc2 = sqlite3_finalize(pStmt);
3547      if( rc!=SQLITE_NOMEM ) rc = rc2;
3548      if( rc==SQLITE_OK ){
3549        zSql = zLeftover;
3550        while( IsSpace(zSql[0]) ) zSql++;
3551      }else if( pzErrMsg ){
3552        *pzErrMsg = save_err_msg(db);
3553      }
3554
3555      /* clear saved stmt handle */
3556      if( pArg ){
3557        pArg->pStmt = NULL;
3558      }
3559    }
3560  } /* end while */
3561
3562  return rc;
3563}
3564
3565/*
3566** Release memory previously allocated by tableColumnList().
3567*/
3568static void freeColumnList(char **azCol){
3569  int i;
3570  for(i=1; azCol[i]; i++){
3571    sqlite3_free(azCol[i]);
3572  }
3573  /* azCol[0] is a static string */
3574  sqlite3_free(azCol);
3575}
3576
3577/*
3578** Return a list of pointers to strings which are the names of all
3579** columns in table zTab.   The memory to hold the names is dynamically
3580** allocated and must be released by the caller using a subsequent call
3581** to freeColumnList().
3582**
3583** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3584** value that needs to be preserved, then azCol[0] is filled in with the
3585** name of the rowid column.
3586**
3587** The first regular column in the table is azCol[1].  The list is terminated
3588** by an entry with azCol[i]==0.
3589*/
3590static char **tableColumnList(ShellState *p, const char *zTab){
3591  char **azCol = 0;
3592  sqlite3_stmt *pStmt;
3593  char *zSql;
3594  int nCol = 0;
3595  int nAlloc = 0;
3596  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3597  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3598  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3599  int rc;
3600
3601  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3602  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3603  sqlite3_free(zSql);
3604  if( rc ) return 0;
3605  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3606    if( nCol>=nAlloc-2 ){
3607      nAlloc = nAlloc*2 + nCol + 10;
3608      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3609      if( azCol==0 ) shell_out_of_memory();
3610    }
3611    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3612    if( sqlite3_column_int(pStmt, 5) ){
3613      nPK++;
3614      if( nPK==1
3615       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3616                          "INTEGER")==0
3617      ){
3618        isIPK = 1;
3619      }else{
3620        isIPK = 0;
3621      }
3622    }
3623  }
3624  sqlite3_finalize(pStmt);
3625  if( azCol==0 ) return 0;
3626  azCol[0] = 0;
3627  azCol[nCol+1] = 0;
3628
3629  /* The decision of whether or not a rowid really needs to be preserved
3630  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3631  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3632  ** rowids on tables where the rowid is inaccessible because there are other
3633  ** columns in the table named "rowid", "_rowid_", and "oid".
3634  */
3635  if( preserveRowid && isIPK ){
3636    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3637    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3638    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3639    ** ROWID aliases.  To distinguish these cases, check to see if
3640    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3641    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3642    */
3643    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3644                           " WHERE origin='pk'", zTab);
3645    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3646    sqlite3_free(zSql);
3647    if( rc ){
3648      freeColumnList(azCol);
3649      return 0;
3650    }
3651    rc = sqlite3_step(pStmt);
3652    sqlite3_finalize(pStmt);
3653    preserveRowid = rc==SQLITE_ROW;
3654  }
3655  if( preserveRowid ){
3656    /* Only preserve the rowid if we can find a name to use for the
3657    ** rowid */
3658    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3659    int i, j;
3660    for(j=0; j<3; j++){
3661      for(i=1; i<=nCol; i++){
3662        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3663      }
3664      if( i>nCol ){
3665        /* At this point, we know that azRowid[j] is not the name of any
3666        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3667        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3668        ** tables will fail this last check */
3669        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3670        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3671        break;
3672      }
3673    }
3674  }
3675  return azCol;
3676}
3677
3678/*
3679** Toggle the reverse_unordered_selects setting.
3680*/
3681static void toggleSelectOrder(sqlite3 *db){
3682  sqlite3_stmt *pStmt = 0;
3683  int iSetting = 0;
3684  char zStmt[100];
3685  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3686  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3687    iSetting = sqlite3_column_int(pStmt, 0);
3688  }
3689  sqlite3_finalize(pStmt);
3690  sqlite3_snprintf(sizeof(zStmt), zStmt,
3691       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3692  sqlite3_exec(db, zStmt, 0, 0, 0);
3693}
3694
3695/*
3696** This is a different callback routine used for dumping the database.
3697** Each row received by this callback consists of a table name,
3698** the table type ("index" or "table") and SQL to create the table.
3699** This routine should print text sufficient to recreate the table.
3700*/
3701static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3702  int rc;
3703  const char *zTable;
3704  const char *zType;
3705  const char *zSql;
3706  ShellState *p = (ShellState *)pArg;
3707  int dataOnly;
3708  int noSys;
3709
3710  UNUSED_PARAMETER(azNotUsed);
3711  if( nArg!=3 || azArg==0 ) return 0;
3712  zTable = azArg[0];
3713  zType = azArg[1];
3714  zSql = azArg[2];
3715  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
3716  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
3717
3718  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
3719    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3720  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
3721    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3722  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3723    return 0;
3724  }else if( dataOnly ){
3725    /* no-op */
3726  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3727    char *zIns;
3728    if( !p->writableSchema ){
3729      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3730      p->writableSchema = 1;
3731    }
3732    zIns = sqlite3_mprintf(
3733       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3734       "VALUES('table','%q','%q',0,'%q');",
3735       zTable, zTable, zSql);
3736    utf8_printf(p->out, "%s\n", zIns);
3737    sqlite3_free(zIns);
3738    return 0;
3739  }else{
3740    printSchemaLine(p->out, zSql, ";\n");
3741  }
3742
3743  if( strcmp(zType, "table")==0 ){
3744    ShellText sSelect;
3745    ShellText sTable;
3746    char **azCol;
3747    int i;
3748    char *savedDestTable;
3749    int savedMode;
3750
3751    azCol = tableColumnList(p, zTable);
3752    if( azCol==0 ){
3753      p->nErr++;
3754      return 0;
3755    }
3756
3757    /* Always quote the table name, even if it appears to be pure ascii,
3758    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3759    initText(&sTable);
3760    appendText(&sTable, zTable, quoteChar(zTable));
3761    /* If preserving the rowid, add a column list after the table name.
3762    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3763    ** instead of the usual "INSERT INTO tab VALUES(...)".
3764    */
3765    if( azCol[0] ){
3766      appendText(&sTable, "(", 0);
3767      appendText(&sTable, azCol[0], 0);
3768      for(i=1; azCol[i]; i++){
3769        appendText(&sTable, ",", 0);
3770        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3771      }
3772      appendText(&sTable, ")", 0);
3773    }
3774
3775    /* Build an appropriate SELECT statement */
3776    initText(&sSelect);
3777    appendText(&sSelect, "SELECT ", 0);
3778    if( azCol[0] ){
3779      appendText(&sSelect, azCol[0], 0);
3780      appendText(&sSelect, ",", 0);
3781    }
3782    for(i=1; azCol[i]; i++){
3783      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3784      if( azCol[i+1] ){
3785        appendText(&sSelect, ",", 0);
3786      }
3787    }
3788    freeColumnList(azCol);
3789    appendText(&sSelect, " FROM ", 0);
3790    appendText(&sSelect, zTable, quoteChar(zTable));
3791
3792    savedDestTable = p->zDestTable;
3793    savedMode = p->mode;
3794    p->zDestTable = sTable.z;
3795    p->mode = p->cMode = MODE_Insert;
3796    rc = shell_exec(p, sSelect.z, 0);
3797    if( (rc&0xff)==SQLITE_CORRUPT ){
3798      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3799      toggleSelectOrder(p->db);
3800      shell_exec(p, sSelect.z, 0);
3801      toggleSelectOrder(p->db);
3802    }
3803    p->zDestTable = savedDestTable;
3804    p->mode = savedMode;
3805    freeText(&sTable);
3806    freeText(&sSelect);
3807    if( rc ) p->nErr++;
3808  }
3809  return 0;
3810}
3811
3812/*
3813** Run zQuery.  Use dump_callback() as the callback routine so that
3814** the contents of the query are output as SQL statements.
3815**
3816** If we get a SQLITE_CORRUPT error, rerun the query after appending
3817** "ORDER BY rowid DESC" to the end.
3818*/
3819static int run_schema_dump_query(
3820  ShellState *p,
3821  const char *zQuery
3822){
3823  int rc;
3824  char *zErr = 0;
3825  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3826  if( rc==SQLITE_CORRUPT ){
3827    char *zQ2;
3828    int len = strlen30(zQuery);
3829    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3830    if( zErr ){
3831      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3832      sqlite3_free(zErr);
3833      zErr = 0;
3834    }
3835    zQ2 = malloc( len+100 );
3836    if( zQ2==0 ) return rc;
3837    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3838    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3839    if( rc ){
3840      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3841    }else{
3842      rc = SQLITE_CORRUPT;
3843    }
3844    sqlite3_free(zErr);
3845    free(zQ2);
3846  }
3847  return rc;
3848}
3849
3850/*
3851** Text of help messages.
3852**
3853** The help text for each individual command begins with a line that starts
3854** with ".".  Subsequent lines are supplimental information.
3855**
3856** There must be two or more spaces between the end of the command and the
3857** start of the description of what that command does.
3858*/
3859static const char *(azHelp[]) = {
3860#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3861  ".archive ...             Manage SQL archives",
3862  "   Each command must have exactly one of the following options:",
3863  "     -c, --create               Create a new archive",
3864  "     -u, --update               Add or update files with changed mtime",
3865  "     -i, --insert               Like -u but always add even if unchanged",
3866  "     -t, --list                 List contents of archive",
3867  "     -x, --extract              Extract files from archive",
3868  "   Optional arguments:",
3869  "     -v, --verbose              Print each filename as it is processed",
3870  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3871  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3872  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3873  "     -n, --dryrun               Show the SQL that would have occurred",
3874  "   Examples:",
3875  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3876  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3877  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3878  "   See also:",
3879  "      http://sqlite.org/cli.html#sqlar_archive_support",
3880#endif
3881#ifndef SQLITE_OMIT_AUTHORIZATION
3882  ".auth ON|OFF             Show authorizer callbacks",
3883#endif
3884  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3885  "       --append            Use the appendvfs",
3886  "       --async             Write to FILE without journal and fsync()",
3887  ".bail on|off             Stop after hitting an error.  Default OFF",
3888  ".binary on|off           Turn binary output on or off.  Default OFF",
3889  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3890  ".changes on|off          Show number of rows changed by SQL",
3891  ".check GLOB              Fail if output since .testcase does not match",
3892  ".clone NEWDB             Clone data into NEWDB from the existing database",
3893  ".databases               List names and files of attached databases",
3894  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3895  ".dbinfo ?DB?             Show status information about the database",
3896  ".dump ?OBJECTS?          Render database content as SQL",
3897  "   Options:",
3898  "     --data-only            Output only INSERT statements",
3899  "     --newlines             Allow unescaped newline characters in output",
3900  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
3901  "     --preserve-rowids      Include ROWID values in the output",
3902  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
3903  "   Additional LIKE patterns can be given in subsequent arguments",
3904  ".echo on|off             Turn command echo on or off",
3905  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3906  "   Other Modes:",
3907#ifdef SQLITE_DEBUG
3908  "      test                  Show raw EXPLAIN QUERY PLAN output",
3909  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3910#endif
3911  "      trigger               Like \"full\" but also show trigger bytecode",
3912  ".excel                   Display the output of next command in spreadsheet",
3913  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
3914  ".exit ?CODE?             Exit this program with return-code CODE",
3915  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3916  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3917  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3918  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
3919  "   --help                  Show CMD details",
3920  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3921  ".headers on|off          Turn display of headers on or off",
3922  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3923  ".import FILE TABLE       Import data from FILE into TABLE",
3924  "   Options:",
3925  "     --ascii               Use \\037 and \\036 as column and row separators",
3926  "     --csv                 Use , and \\n as column and row separators",
3927  "     --skip N              Skip the first N rows of input",
3928  "     -v                    \"Verbose\" - increase auxiliary output",
3929  "   Notes:",
3930  "     *  If TABLE does not exist, it is created.  The first row of input",
3931  "        determines the column names.",
3932  "     *  If neither --csv or --ascii are used, the input mode is derived",
3933  "        from the \".mode\" output mode",
3934  "     *  If FILE begins with \"|\" then it is a command that generates the",
3935  "        input text.",
3936#ifndef SQLITE_OMIT_TEST_CONTROL
3937  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3938#endif
3939  ".indexes ?TABLE?         Show names of indexes",
3940  "                           If TABLE is specified, only show indexes for",
3941  "                           tables matching TABLE using the LIKE operator.",
3942#ifdef SQLITE_ENABLE_IOTRACE
3943  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3944#endif
3945  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3946  ".lint OPTIONS            Report potential schema issues.",
3947  "     Options:",
3948  "        fkey-indexes     Find missing foreign key indexes",
3949#ifndef SQLITE_OMIT_LOAD_EXTENSION
3950  ".load FILE ?ENTRY?       Load an extension library",
3951#endif
3952  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3953  ".mode MODE ?TABLE?       Set output mode",
3954  "   MODE is one of:",
3955  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
3956  "     box       Tables using unicode box-drawing characters",
3957  "     csv       Comma-separated values",
3958  "     column    Output in columns.  (See .width)",
3959  "     html      HTML <table> code",
3960  "     insert    SQL insert statements for TABLE",
3961  "     json      Results in a JSON array",
3962  "     line      One value per line",
3963  "     list      Values delimited by \"|\"",
3964  "     markdown  Markdown table format",
3965  "     quote     Escape answers as for SQL",
3966  "     table     ASCII-art table",
3967  "     tabs      Tab-separated values",
3968  "     tcl       TCL list elements",
3969  ".nullvalue STRING        Use STRING in place of NULL values",
3970  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
3971  "     If FILE begins with '|' then open as a pipe",
3972  "       --bom  Put a UTF8 byte-order mark at the beginning",
3973  "       -e     Send output to the system text editor",
3974  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
3975#ifdef SQLITE_DEBUG
3976  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
3977#endif
3978  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3979  "     Options:",
3980  "        --append        Use appendvfs to append database to the end of FILE",
3981#ifndef SQLITE_OMIT_DESERIALIZE
3982  "        --deserialize   Load into memory using sqlite3_deserialize()",
3983  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3984  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3985#endif
3986  "        --new           Initialize FILE to an empty database",
3987  "        --nofollow      Do not follow symbolic links",
3988  "        --readonly      Open FILE readonly",
3989  "        --zip           FILE is a ZIP archive",
3990  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
3991  "   If FILE begins with '|' then open it as a pipe.",
3992  "   Options:",
3993  "     --bom                 Prefix output with a UTF8 byte-order mark",
3994  "     -e                    Send output to the system text editor",
3995  "     -x                    Send output as CSV to a spreadsheet",
3996  ".parameter CMD ...       Manage SQL parameter bindings",
3997  "   clear                   Erase all bindings",
3998  "   init                    Initialize the TEMP table that holds bindings",
3999  "   list                    List the current parameter bindings",
4000  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4001  "                           PARAMETER should start with one of: $ : @ ?",
4002  "   unset PARAMETER         Remove PARAMETER from the binding table",
4003  ".print STRING...         Print literal STRING",
4004#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4005  ".progress N              Invoke progress handler after every N opcodes",
4006  "   --limit N                 Interrupt after N progress callbacks",
4007  "   --once                    Do no more than one progress interrupt",
4008  "   --quiet|-q                No output except at interrupts",
4009  "   --reset                   Reset the count for each input and interrupt",
4010#endif
4011  ".prompt MAIN CONTINUE    Replace the standard prompts",
4012  ".quit                    Exit this program",
4013  ".read FILE               Read input from FILE",
4014#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4015  ".recover                 Recover as much data as possible from corrupt db.",
4016  "   --freelist-corrupt       Assume the freelist is corrupt",
4017  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4018  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4019  "   --no-rowids              Do not attempt to recover rowid values",
4020  "                            that are not also INTEGER PRIMARY KEYs",
4021#endif
4022  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4023  ".save FILE               Write in-memory database into FILE",
4024  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4025  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4026  "   Options:",
4027  "      --indent             Try to pretty-print the schema",
4028  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4029  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4030  "    Options:",
4031  "       --init               Create a new SELFTEST table",
4032  "       -v                   Verbose output",
4033  ".separator COL ?ROW?     Change the column and row separators",
4034#if defined(SQLITE_ENABLE_SESSION)
4035  ".session ?NAME? CMD ...  Create or control sessions",
4036  "   Subcommands:",
4037  "     attach TABLE             Attach TABLE",
4038  "     changeset FILE           Write a changeset into FILE",
4039  "     close                    Close one session",
4040  "     enable ?BOOLEAN?         Set or query the enable bit",
4041  "     filter GLOB...           Reject tables matching GLOBs",
4042  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4043  "     isempty                  Query whether the session is empty",
4044  "     list                     List currently open session names",
4045  "     open DB NAME             Open a new session on DB",
4046  "     patchset FILE            Write a patchset into FILE",
4047  "   If ?NAME? is omitted, the first defined session is used.",
4048#endif
4049  ".sha3sum ...             Compute a SHA3 hash of database content",
4050  "    Options:",
4051  "      --schema              Also hash the sqlite_schema table",
4052  "      --sha3-224            Use the sha3-224 algorithm",
4053  "      --sha3-256            Use the sha3-256 algorithm (default)",
4054  "      --sha3-384            Use the sha3-384 algorithm",
4055  "      --sha3-512            Use the sha3-512 algorithm",
4056  "    Any other argument is a LIKE pattern for tables to hash",
4057#ifndef SQLITE_NOHAVE_SYSTEM
4058  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4059#endif
4060  ".show                    Show the current values for various settings",
4061  ".stats ?ARG?             Show stats or turn stats on or off",
4062  "   off                      Turn off automatic stat display",
4063  "   on                       Turn on automatic stat display",
4064  "   stmt                     Show statement stats",
4065  "   vmstep                   Show the virtual machine step count only",
4066#ifndef SQLITE_NOHAVE_SYSTEM
4067  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4068#endif
4069  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4070  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4071  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4072  "                           Run \".testctrl\" with no arguments for details",
4073  ".timeout MS              Try opening locked tables for MS milliseconds",
4074  ".timer on|off            Turn SQL timer on or off",
4075#ifndef SQLITE_OMIT_TRACE
4076  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4077  "    FILE                    Send output to FILE",
4078  "    stdout                  Send output to stdout",
4079  "    stderr                  Send output to stderr",
4080  "    off                     Disable tracing",
4081  "    --expanded              Expand query parameters",
4082#ifdef SQLITE_ENABLE_NORMALIZE
4083  "    --normalized            Normal the SQL statements",
4084#endif
4085  "    --plain                 Show SQL as it is input",
4086  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4087  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4088  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4089  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4090#endif /* SQLITE_OMIT_TRACE */
4091#ifdef SQLITE_DEBUG
4092  ".unmodule NAME ...       Unregister virtual table modules",
4093  "    --allexcept             Unregister everything except those named",
4094#endif
4095  ".vfsinfo ?AUX?           Information about the top-level VFS",
4096  ".vfslist                 List all available VFSes",
4097  ".vfsname ?AUX?           Print the name of the VFS stack",
4098  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4099  "     Negative values right-justify",
4100};
4101
4102/*
4103** Output help text.
4104**
4105** zPattern describes the set of commands for which help text is provided.
4106** If zPattern is NULL, then show all commands, but only give a one-line
4107** description of each.
4108**
4109** Return the number of matches.
4110*/
4111static int showHelp(FILE *out, const char *zPattern){
4112  int i = 0;
4113  int j = 0;
4114  int n = 0;
4115  char *zPat;
4116  if( zPattern==0
4117   || zPattern[0]=='0'
4118   || strcmp(zPattern,"-a")==0
4119   || strcmp(zPattern,"-all")==0
4120   || strcmp(zPattern,"--all")==0
4121  ){
4122    /* Show all commands, but only one line per command */
4123    if( zPattern==0 ) zPattern = "";
4124    for(i=0; i<ArraySize(azHelp); i++){
4125      if( azHelp[i][0]=='.' || zPattern[0] ){
4126        utf8_printf(out, "%s\n", azHelp[i]);
4127        n++;
4128      }
4129    }
4130  }else{
4131    /* Look for commands that for which zPattern is an exact prefix */
4132    zPat = sqlite3_mprintf(".%s*", zPattern);
4133    for(i=0; i<ArraySize(azHelp); i++){
4134      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4135        utf8_printf(out, "%s\n", azHelp[i]);
4136        j = i+1;
4137        n++;
4138      }
4139    }
4140    sqlite3_free(zPat);
4141    if( n ){
4142      if( n==1 ){
4143        /* when zPattern is a prefix of exactly one command, then include the
4144        ** details of that command, which should begin at offset j */
4145        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4146          utf8_printf(out, "%s\n", azHelp[j]);
4147          j++;
4148        }
4149      }
4150      return n;
4151    }
4152    /* Look for commands that contain zPattern anywhere.  Show the complete
4153    ** text of all commands that match. */
4154    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4155    for(i=0; i<ArraySize(azHelp); i++){
4156      if( azHelp[i][0]=='.' ) j = i;
4157      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4158        utf8_printf(out, "%s\n", azHelp[j]);
4159        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4160          j++;
4161          utf8_printf(out, "%s\n", azHelp[j]);
4162        }
4163        i = j;
4164        n++;
4165      }
4166    }
4167    sqlite3_free(zPat);
4168  }
4169  return n;
4170}
4171
4172/* Forward reference */
4173static int process_input(ShellState *p);
4174
4175/*
4176** Read the content of file zName into memory obtained from sqlite3_malloc64()
4177** and return a pointer to the buffer. The caller is responsible for freeing
4178** the memory.
4179**
4180** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4181** read.
4182**
4183** For convenience, a nul-terminator byte is always appended to the data read
4184** from the file before the buffer is returned. This byte is not included in
4185** the final value of (*pnByte), if applicable.
4186**
4187** NULL is returned if any error is encountered. The final value of *pnByte
4188** is undefined in this case.
4189*/
4190static char *readFile(const char *zName, int *pnByte){
4191  FILE *in = fopen(zName, "rb");
4192  long nIn;
4193  size_t nRead;
4194  char *pBuf;
4195  if( in==0 ) return 0;
4196  fseek(in, 0, SEEK_END);
4197  nIn = ftell(in);
4198  rewind(in);
4199  pBuf = sqlite3_malloc64( nIn+1 );
4200  if( pBuf==0 ){ fclose(in); return 0; }
4201  nRead = fread(pBuf, nIn, 1, in);
4202  fclose(in);
4203  if( nRead!=1 ){
4204    sqlite3_free(pBuf);
4205    return 0;
4206  }
4207  pBuf[nIn] = 0;
4208  if( pnByte ) *pnByte = nIn;
4209  return pBuf;
4210}
4211
4212#if defined(SQLITE_ENABLE_SESSION)
4213/*
4214** Close a single OpenSession object and release all of its associated
4215** resources.
4216*/
4217static void session_close(OpenSession *pSession){
4218  int i;
4219  sqlite3session_delete(pSession->p);
4220  sqlite3_free(pSession->zName);
4221  for(i=0; i<pSession->nFilter; i++){
4222    sqlite3_free(pSession->azFilter[i]);
4223  }
4224  sqlite3_free(pSession->azFilter);
4225  memset(pSession, 0, sizeof(OpenSession));
4226}
4227#endif
4228
4229/*
4230** Close all OpenSession objects and release all associated resources.
4231*/
4232#if defined(SQLITE_ENABLE_SESSION)
4233static void session_close_all(ShellState *p){
4234  int i;
4235  for(i=0; i<p->nSession; i++){
4236    session_close(&p->aSession[i]);
4237  }
4238  p->nSession = 0;
4239}
4240#else
4241# define session_close_all(X)
4242#endif
4243
4244/*
4245** Implementation of the xFilter function for an open session.  Omit
4246** any tables named by ".session filter" but let all other table through.
4247*/
4248#if defined(SQLITE_ENABLE_SESSION)
4249static int session_filter(void *pCtx, const char *zTab){
4250  OpenSession *pSession = (OpenSession*)pCtx;
4251  int i;
4252  for(i=0; i<pSession->nFilter; i++){
4253    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4254  }
4255  return 1;
4256}
4257#endif
4258
4259/*
4260** Try to deduce the type of file for zName based on its content.  Return
4261** one of the SHELL_OPEN_* constants.
4262**
4263** If the file does not exist or is empty but its name looks like a ZIP
4264** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4265** Otherwise, assume an ordinary database regardless of the filename if
4266** the type cannot be determined from content.
4267*/
4268int deduceDatabaseType(const char *zName, int dfltZip){
4269  FILE *f = fopen(zName, "rb");
4270  size_t n;
4271  int rc = SHELL_OPEN_UNSPEC;
4272  char zBuf[100];
4273  if( f==0 ){
4274    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4275       return SHELL_OPEN_ZIPFILE;
4276    }else{
4277       return SHELL_OPEN_NORMAL;
4278    }
4279  }
4280  n = fread(zBuf, 16, 1, f);
4281  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4282    fclose(f);
4283    return SHELL_OPEN_NORMAL;
4284  }
4285  fseek(f, -25, SEEK_END);
4286  n = fread(zBuf, 25, 1, f);
4287  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4288    rc = SHELL_OPEN_APPENDVFS;
4289  }else{
4290    fseek(f, -22, SEEK_END);
4291    n = fread(zBuf, 22, 1, f);
4292    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4293       && zBuf[3]==0x06 ){
4294      rc = SHELL_OPEN_ZIPFILE;
4295    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4296      rc = SHELL_OPEN_ZIPFILE;
4297    }
4298  }
4299  fclose(f);
4300  return rc;
4301}
4302
4303#ifndef SQLITE_OMIT_DESERIALIZE
4304/*
4305** Reconstruct an in-memory database using the output from the "dbtotxt"
4306** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
4307** is 0, then read from standard input.
4308*/
4309static unsigned char *readHexDb(ShellState *p, int *pnData){
4310  unsigned char *a = 0;
4311  int nLine;
4312  int n = 0;
4313  int pgsz = 0;
4314  int iOffset = 0;
4315  int j, k;
4316  int rc;
4317  FILE *in;
4318  unsigned int x[16];
4319  char zLine[1000];
4320  if( p->zDbFilename ){
4321    in = fopen(p->zDbFilename, "r");
4322    if( in==0 ){
4323      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
4324      return 0;
4325    }
4326    nLine = 0;
4327  }else{
4328    in = p->in;
4329    nLine = p->lineno;
4330    if( in==0 ) in = stdin;
4331  }
4332  *pnData = 0;
4333  nLine++;
4334  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4335  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4336  if( rc!=2 ) goto readHexDb_error;
4337  if( n<0 ) goto readHexDb_error;
4338  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4339  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4340  a = sqlite3_malloc( n ? n : 1 );
4341  if( a==0 ){
4342    utf8_printf(stderr, "Out of memory!\n");
4343    goto readHexDb_error;
4344  }
4345  memset(a, 0, n);
4346  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4347    utf8_printf(stderr, "invalid pagesize\n");
4348    goto readHexDb_error;
4349  }
4350  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4351    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4352    if( rc==2 ){
4353      iOffset = k;
4354      continue;
4355    }
4356    if( strncmp(zLine, "| end ", 6)==0 ){
4357      break;
4358    }
4359    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4360                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4361                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4362    if( rc==17 ){
4363      k = iOffset+j;
4364      if( k+16<=n ){
4365        int ii;
4366        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4367      }
4368    }
4369  }
4370  *pnData = n;
4371  if( in!=p->in ){
4372    fclose(in);
4373  }else{
4374    p->lineno = nLine;
4375  }
4376  return a;
4377
4378readHexDb_error:
4379  if( in!=p->in ){
4380    fclose(in);
4381  }else{
4382    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4383      nLine++;
4384      if(strncmp(zLine, "| end ", 6)==0 ) break;
4385    }
4386    p->lineno = nLine;
4387  }
4388  sqlite3_free(a);
4389  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4390  return 0;
4391}
4392#endif /* SQLITE_OMIT_DESERIALIZE */
4393
4394/*
4395** Scalar function "shell_int32". The first argument to this function
4396** must be a blob. The second a non-negative integer. This function
4397** reads and returns a 32-bit big-endian integer from byte
4398** offset (4*<arg2>) of the blob.
4399*/
4400static void shellInt32(
4401  sqlite3_context *context,
4402  int argc,
4403  sqlite3_value **argv
4404){
4405  const unsigned char *pBlob;
4406  int nBlob;
4407  int iInt;
4408
4409  UNUSED_PARAMETER(argc);
4410  nBlob = sqlite3_value_bytes(argv[0]);
4411  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4412  iInt = sqlite3_value_int(argv[1]);
4413
4414  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4415    const unsigned char *a = &pBlob[iInt*4];
4416    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4417                       + ((sqlite3_int64)a[1]<<16)
4418                       + ((sqlite3_int64)a[2]<< 8)
4419                       + ((sqlite3_int64)a[3]<< 0);
4420    sqlite3_result_int64(context, iVal);
4421  }
4422}
4423
4424/*
4425** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4426** using "..." with internal double-quote characters doubled.
4427*/
4428static void shellIdQuote(
4429  sqlite3_context *context,
4430  int argc,
4431  sqlite3_value **argv
4432){
4433  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4434  UNUSED_PARAMETER(argc);
4435  if( zName ){
4436    char *z = sqlite3_mprintf("\"%w\"", zName);
4437    sqlite3_result_text(context, z, -1, sqlite3_free);
4438  }
4439}
4440
4441/*
4442** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4443*/
4444static void shellUSleepFunc(
4445  sqlite3_context *context,
4446  int argcUnused,
4447  sqlite3_value **argv
4448){
4449  int sleep = sqlite3_value_int(argv[0]);
4450  (void)argcUnused;
4451  sqlite3_sleep(sleep/1000);
4452  sqlite3_result_int(context, sleep);
4453}
4454
4455/*
4456** Scalar function "shell_escape_crnl" used by the .recover command.
4457** The argument passed to this function is the output of built-in
4458** function quote(). If the first character of the input is "'",
4459** indicating that the value passed to quote() was a text value,
4460** then this function searches the input for "\n" and "\r" characters
4461** and adds a wrapper similar to the following:
4462**
4463**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4464**
4465** Or, if the first character of the input is not "'", then a copy
4466** of the input is returned.
4467*/
4468static void shellEscapeCrnl(
4469  sqlite3_context *context,
4470  int argc,
4471  sqlite3_value **argv
4472){
4473  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4474  UNUSED_PARAMETER(argc);
4475  if( zText[0]=='\'' ){
4476    int nText = sqlite3_value_bytes(argv[0]);
4477    int i;
4478    char zBuf1[20];
4479    char zBuf2[20];
4480    const char *zNL = 0;
4481    const char *zCR = 0;
4482    int nCR = 0;
4483    int nNL = 0;
4484
4485    for(i=0; zText[i]; i++){
4486      if( zNL==0 && zText[i]=='\n' ){
4487        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4488        nNL = (int)strlen(zNL);
4489      }
4490      if( zCR==0 && zText[i]=='\r' ){
4491        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4492        nCR = (int)strlen(zCR);
4493      }
4494    }
4495
4496    if( zNL || zCR ){
4497      int iOut = 0;
4498      i64 nMax = (nNL > nCR) ? nNL : nCR;
4499      i64 nAlloc = nMax * nText + (nMax+64)*2;
4500      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4501      if( zOut==0 ){
4502        sqlite3_result_error_nomem(context);
4503        return;
4504      }
4505
4506      if( zNL && zCR ){
4507        memcpy(&zOut[iOut], "replace(replace(", 16);
4508        iOut += 16;
4509      }else{
4510        memcpy(&zOut[iOut], "replace(", 8);
4511        iOut += 8;
4512      }
4513      for(i=0; zText[i]; i++){
4514        if( zText[i]=='\n' ){
4515          memcpy(&zOut[iOut], zNL, nNL);
4516          iOut += nNL;
4517        }else if( zText[i]=='\r' ){
4518          memcpy(&zOut[iOut], zCR, nCR);
4519          iOut += nCR;
4520        }else{
4521          zOut[iOut] = zText[i];
4522          iOut++;
4523        }
4524      }
4525
4526      if( zNL ){
4527        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4528        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4529        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4530      }
4531      if( zCR ){
4532        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4533        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4534        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4535      }
4536
4537      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4538      sqlite3_free(zOut);
4539      return;
4540    }
4541  }
4542
4543  sqlite3_result_value(context, argv[0]);
4544}
4545
4546/* Flags for open_db().
4547**
4548** The default behavior of open_db() is to exit(1) if the database fails to
4549** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4550** but still returns without calling exit.
4551**
4552** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4553** ZIP archive if the file does not exist or is empty and its name matches
4554** the *.zip pattern.
4555*/
4556#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4557#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4558
4559/*
4560** Make sure the database is open.  If it is not, then open it.  If
4561** the database fails to open, print an error message and exit.
4562*/
4563static void open_db(ShellState *p, int openFlags){
4564  if( p->db==0 ){
4565    if( p->openMode==SHELL_OPEN_UNSPEC ){
4566      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4567        p->openMode = SHELL_OPEN_NORMAL;
4568      }else{
4569        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4570                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4571      }
4572    }
4573    switch( p->openMode ){
4574      case SHELL_OPEN_APPENDVFS: {
4575        sqlite3_open_v2(p->zDbFilename, &p->db,
4576           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4577        break;
4578      }
4579      case SHELL_OPEN_HEXDB:
4580      case SHELL_OPEN_DESERIALIZE: {
4581        sqlite3_open(0, &p->db);
4582        break;
4583      }
4584      case SHELL_OPEN_ZIPFILE: {
4585        sqlite3_open(":memory:", &p->db);
4586        break;
4587      }
4588      case SHELL_OPEN_READONLY: {
4589        sqlite3_open_v2(p->zDbFilename, &p->db,
4590            SQLITE_OPEN_READONLY|p->openFlags, 0);
4591        break;
4592      }
4593      case SHELL_OPEN_UNSPEC:
4594      case SHELL_OPEN_NORMAL: {
4595        sqlite3_open_v2(p->zDbFilename, &p->db,
4596           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4597        break;
4598      }
4599    }
4600    globalDb = p->db;
4601    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4602      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4603          p->zDbFilename, sqlite3_errmsg(p->db));
4604      if( openFlags & OPEN_DB_KEEPALIVE ){
4605        sqlite3_open(":memory:", &p->db);
4606        return;
4607      }
4608      exit(1);
4609    }
4610#ifndef SQLITE_OMIT_LOAD_EXTENSION
4611    sqlite3_enable_load_extension(p->db, 1);
4612#endif
4613    sqlite3_fileio_init(p->db, 0, 0);
4614    sqlite3_shathree_init(p->db, 0, 0);
4615    sqlite3_completion_init(p->db, 0, 0);
4616    sqlite3_uint_init(p->db, 0, 0);
4617    sqlite3_decimal_init(p->db, 0, 0);
4618    sqlite3_ieee_init(p->db, 0, 0);
4619    sqlite3_series_init(p->db, 0, 0);
4620#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4621    sqlite3_dbdata_init(p->db, 0, 0);
4622#endif
4623#ifdef SQLITE_HAVE_ZLIB
4624    sqlite3_zipfile_init(p->db, 0, 0);
4625    sqlite3_sqlar_init(p->db, 0, 0);
4626#endif
4627    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4628                            shellAddSchemaName, 0, 0);
4629    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4630                            shellModuleSchema, 0, 0);
4631    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4632                            shellPutsFunc, 0, 0);
4633    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4634                            shellEscapeCrnl, 0, 0);
4635    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4636                            shellInt32, 0, 0);
4637    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4638                            shellIdQuote, 0, 0);
4639    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
4640                            shellUSleepFunc, 0, 0);
4641#ifndef SQLITE_NOHAVE_SYSTEM
4642    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4643                            editFunc, 0, 0);
4644    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4645                            editFunc, 0, 0);
4646#endif
4647    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4648      char *zSql = sqlite3_mprintf(
4649         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4650      sqlite3_exec(p->db, zSql, 0, 0, 0);
4651      sqlite3_free(zSql);
4652    }
4653#ifndef SQLITE_OMIT_DESERIALIZE
4654    else
4655    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4656      int rc;
4657      int nData = 0;
4658      unsigned char *aData;
4659      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4660        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4661      }else{
4662        aData = readHexDb(p, &nData);
4663        if( aData==0 ){
4664          return;
4665        }
4666      }
4667      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4668                   SQLITE_DESERIALIZE_RESIZEABLE |
4669                   SQLITE_DESERIALIZE_FREEONCLOSE);
4670      if( rc ){
4671        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4672      }
4673      if( p->szMax>0 ){
4674        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4675      }
4676    }
4677#endif
4678  }
4679}
4680
4681/*
4682** Attempt to close the databaes connection.  Report errors.
4683*/
4684void close_db(sqlite3 *db){
4685  int rc = sqlite3_close(db);
4686  if( rc ){
4687    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4688        rc, sqlite3_errmsg(db));
4689  }
4690}
4691
4692#if HAVE_READLINE || HAVE_EDITLINE
4693/*
4694** Readline completion callbacks
4695*/
4696static char *readline_completion_generator(const char *text, int state){
4697  static sqlite3_stmt *pStmt = 0;
4698  char *zRet;
4699  if( state==0 ){
4700    char *zSql;
4701    sqlite3_finalize(pStmt);
4702    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4703                           "  FROM completion(%Q) ORDER BY 1", text);
4704    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4705    sqlite3_free(zSql);
4706  }
4707  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4708    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4709  }else{
4710    sqlite3_finalize(pStmt);
4711    pStmt = 0;
4712    zRet = 0;
4713  }
4714  return zRet;
4715}
4716static char **readline_completion(const char *zText, int iStart, int iEnd){
4717  rl_attempted_completion_over = 1;
4718  return rl_completion_matches(zText, readline_completion_generator);
4719}
4720
4721#elif HAVE_LINENOISE
4722/*
4723** Linenoise completion callback
4724*/
4725static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4726  int nLine = strlen30(zLine);
4727  int i, iStart;
4728  sqlite3_stmt *pStmt = 0;
4729  char *zSql;
4730  char zBuf[1000];
4731
4732  if( nLine>sizeof(zBuf)-30 ) return;
4733  if( zLine[0]=='.' || zLine[0]=='#') return;
4734  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4735  if( i==nLine-1 ) return;
4736  iStart = i+1;
4737  memcpy(zBuf, zLine, iStart);
4738  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4739                         "  FROM completion(%Q,%Q) ORDER BY 1",
4740                         &zLine[iStart], zLine);
4741  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4742  sqlite3_free(zSql);
4743  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4744  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4745    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4746    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4747    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4748      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4749      linenoiseAddCompletion(lc, zBuf);
4750    }
4751  }
4752  sqlite3_finalize(pStmt);
4753}
4754#endif
4755
4756/*
4757** Do C-language style dequoting.
4758**
4759**    \a    -> alarm
4760**    \b    -> backspace
4761**    \t    -> tab
4762**    \n    -> newline
4763**    \v    -> vertical tab
4764**    \f    -> form feed
4765**    \r    -> carriage return
4766**    \s    -> space
4767**    \"    -> "
4768**    \'    -> '
4769**    \\    -> backslash
4770**    \NNN  -> ascii character NNN in octal
4771*/
4772static void resolve_backslashes(char *z){
4773  int i, j;
4774  char c;
4775  while( *z && *z!='\\' ) z++;
4776  for(i=j=0; (c = z[i])!=0; i++, j++){
4777    if( c=='\\' && z[i+1]!=0 ){
4778      c = z[++i];
4779      if( c=='a' ){
4780        c = '\a';
4781      }else if( c=='b' ){
4782        c = '\b';
4783      }else if( c=='t' ){
4784        c = '\t';
4785      }else if( c=='n' ){
4786        c = '\n';
4787      }else if( c=='v' ){
4788        c = '\v';
4789      }else if( c=='f' ){
4790        c = '\f';
4791      }else if( c=='r' ){
4792        c = '\r';
4793      }else if( c=='"' ){
4794        c = '"';
4795      }else if( c=='\'' ){
4796        c = '\'';
4797      }else if( c=='\\' ){
4798        c = '\\';
4799      }else if( c>='0' && c<='7' ){
4800        c -= '0';
4801        if( z[i+1]>='0' && z[i+1]<='7' ){
4802          i++;
4803          c = (c<<3) + z[i] - '0';
4804          if( z[i+1]>='0' && z[i+1]<='7' ){
4805            i++;
4806            c = (c<<3) + z[i] - '0';
4807          }
4808        }
4809      }
4810    }
4811    z[j] = c;
4812  }
4813  if( j<i ) z[j] = 0;
4814}
4815
4816/*
4817** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4818** for TRUE and FALSE.  Return the integer value if appropriate.
4819*/
4820static int booleanValue(const char *zArg){
4821  int i;
4822  if( zArg[0]=='0' && zArg[1]=='x' ){
4823    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4824  }else{
4825    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4826  }
4827  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4828  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4829    return 1;
4830  }
4831  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4832    return 0;
4833  }
4834  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4835          zArg);
4836  return 0;
4837}
4838
4839/*
4840** Set or clear a shell flag according to a boolean value.
4841*/
4842static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4843  if( booleanValue(zArg) ){
4844    ShellSetFlag(p, mFlag);
4845  }else{
4846    ShellClearFlag(p, mFlag);
4847  }
4848}
4849
4850/*
4851** Close an output file, assuming it is not stderr or stdout
4852*/
4853static void output_file_close(FILE *f){
4854  if( f && f!=stdout && f!=stderr ) fclose(f);
4855}
4856
4857/*
4858** Try to open an output file.   The names "stdout" and "stderr" are
4859** recognized and do the right thing.  NULL is returned if the output
4860** filename is "off".
4861*/
4862static FILE *output_file_open(const char *zFile, int bTextMode){
4863  FILE *f;
4864  if( strcmp(zFile,"stdout")==0 ){
4865    f = stdout;
4866  }else if( strcmp(zFile, "stderr")==0 ){
4867    f = stderr;
4868  }else if( strcmp(zFile, "off")==0 ){
4869    f = 0;
4870  }else{
4871    f = fopen(zFile, bTextMode ? "w" : "wb");
4872    if( f==0 ){
4873      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4874    }
4875  }
4876  return f;
4877}
4878
4879#ifndef SQLITE_OMIT_TRACE
4880/*
4881** A routine for handling output from sqlite3_trace().
4882*/
4883static int sql_trace_callback(
4884  unsigned mType,         /* The trace type */
4885  void *pArg,             /* The ShellState pointer */
4886  void *pP,               /* Usually a pointer to sqlite_stmt */
4887  void *pX                /* Auxiliary output */
4888){
4889  ShellState *p = (ShellState*)pArg;
4890  sqlite3_stmt *pStmt;
4891  const char *zSql;
4892  int nSql;
4893  if( p->traceOut==0 ) return 0;
4894  if( mType==SQLITE_TRACE_CLOSE ){
4895    utf8_printf(p->traceOut, "-- closing database connection\n");
4896    return 0;
4897  }
4898  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4899    zSql = (const char*)pX;
4900  }else{
4901    pStmt = (sqlite3_stmt*)pP;
4902    switch( p->eTraceType ){
4903      case SHELL_TRACE_EXPANDED: {
4904        zSql = sqlite3_expanded_sql(pStmt);
4905        break;
4906      }
4907#ifdef SQLITE_ENABLE_NORMALIZE
4908      case SHELL_TRACE_NORMALIZED: {
4909        zSql = sqlite3_normalized_sql(pStmt);
4910        break;
4911      }
4912#endif
4913      default: {
4914        zSql = sqlite3_sql(pStmt);
4915        break;
4916      }
4917    }
4918  }
4919  if( zSql==0 ) return 0;
4920  nSql = strlen30(zSql);
4921  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4922  switch( mType ){
4923    case SQLITE_TRACE_ROW:
4924    case SQLITE_TRACE_STMT: {
4925      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4926      break;
4927    }
4928    case SQLITE_TRACE_PROFILE: {
4929      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4930      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4931      break;
4932    }
4933  }
4934  return 0;
4935}
4936#endif
4937
4938/*
4939** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4940** a useful spot to set a debugger breakpoint.
4941*/
4942static void test_breakpoint(void){
4943  static int nCall = 0;
4944  nCall++;
4945}
4946
4947/*
4948** An object used to read a CSV and other files for import.
4949*/
4950typedef struct ImportCtx ImportCtx;
4951struct ImportCtx {
4952  const char *zFile;  /* Name of the input file */
4953  FILE *in;           /* Read the CSV text from this input stream */
4954  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
4955  char *z;            /* Accumulated text for a field */
4956  int n;              /* Number of bytes in z */
4957  int nAlloc;         /* Space allocated for z[] */
4958  int nLine;          /* Current line number */
4959  int nRow;           /* Number of rows imported */
4960  int nErr;           /* Number of errors encountered */
4961  int bNotFirst;      /* True if one or more bytes already read */
4962  int cTerm;          /* Character that terminated the most recent field */
4963  int cColSep;        /* The column separator character.  (Usually ",") */
4964  int cRowSep;        /* The row separator character.  (Usually "\n") */
4965};
4966
4967/* Clean up resourced used by an ImportCtx */
4968static void import_cleanup(ImportCtx *p){
4969  if( p->in!=0 && p->xCloser!=0 ){
4970    p->xCloser(p->in);
4971    p->in = 0;
4972  }
4973  sqlite3_free(p->z);
4974  p->z = 0;
4975}
4976
4977/* Append a single byte to z[] */
4978static void import_append_char(ImportCtx *p, int c){
4979  if( p->n+1>=p->nAlloc ){
4980    p->nAlloc += p->nAlloc + 100;
4981    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4982    if( p->z==0 ) shell_out_of_memory();
4983  }
4984  p->z[p->n++] = (char)c;
4985}
4986
4987/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4988** with the option of having a separator other than ",".
4989**
4990**   +  Input comes from p->in.
4991**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4992**      from sqlite3_malloc64().
4993**   +  Use p->cSep as the column separator.  The default is ",".
4994**   +  Use p->rSep as the row separator.  The default is "\n".
4995**   +  Keep track of the line number in p->nLine.
4996**   +  Store the character that terminates the field in p->cTerm.  Store
4997**      EOF on end-of-file.
4998**   +  Report syntax errors on stderr
4999*/
5000static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5001  int c;
5002  int cSep = p->cColSep;
5003  int rSep = p->cRowSep;
5004  p->n = 0;
5005  c = fgetc(p->in);
5006  if( c==EOF || seenInterrupt ){
5007    p->cTerm = EOF;
5008    return 0;
5009  }
5010  if( c=='"' ){
5011    int pc, ppc;
5012    int startLine = p->nLine;
5013    int cQuote = c;
5014    pc = ppc = 0;
5015    while( 1 ){
5016      c = fgetc(p->in);
5017      if( c==rSep ) p->nLine++;
5018      if( c==cQuote ){
5019        if( pc==cQuote ){
5020          pc = 0;
5021          continue;
5022        }
5023      }
5024      if( (c==cSep && pc==cQuote)
5025       || (c==rSep && pc==cQuote)
5026       || (c==rSep && pc=='\r' && ppc==cQuote)
5027       || (c==EOF && pc==cQuote)
5028      ){
5029        do{ p->n--; }while( p->z[p->n]!=cQuote );
5030        p->cTerm = c;
5031        break;
5032      }
5033      if( pc==cQuote && c!='\r' ){
5034        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5035                p->zFile, p->nLine, cQuote);
5036      }
5037      if( c==EOF ){
5038        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5039                p->zFile, startLine, cQuote);
5040        p->cTerm = c;
5041        break;
5042      }
5043      import_append_char(p, c);
5044      ppc = pc;
5045      pc = c;
5046    }
5047  }else{
5048    /* If this is the first field being parsed and it begins with the
5049    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5050    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5051      import_append_char(p, c);
5052      c = fgetc(p->in);
5053      if( (c&0xff)==0xbb ){
5054        import_append_char(p, c);
5055        c = fgetc(p->in);
5056        if( (c&0xff)==0xbf ){
5057          p->bNotFirst = 1;
5058          p->n = 0;
5059          return csv_read_one_field(p);
5060        }
5061      }
5062    }
5063    while( c!=EOF && c!=cSep && c!=rSep ){
5064      import_append_char(p, c);
5065      c = fgetc(p->in);
5066    }
5067    if( c==rSep ){
5068      p->nLine++;
5069      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5070    }
5071    p->cTerm = c;
5072  }
5073  if( p->z ) p->z[p->n] = 0;
5074  p->bNotFirst = 1;
5075  return p->z;
5076}
5077
5078/* Read a single field of ASCII delimited text.
5079**
5080**   +  Input comes from p->in.
5081**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5082**      from sqlite3_malloc64().
5083**   +  Use p->cSep as the column separator.  The default is "\x1F".
5084**   +  Use p->rSep as the row separator.  The default is "\x1E".
5085**   +  Keep track of the row number in p->nLine.
5086**   +  Store the character that terminates the field in p->cTerm.  Store
5087**      EOF on end-of-file.
5088**   +  Report syntax errors on stderr
5089*/
5090static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5091  int c;
5092  int cSep = p->cColSep;
5093  int rSep = p->cRowSep;
5094  p->n = 0;
5095  c = fgetc(p->in);
5096  if( c==EOF || seenInterrupt ){
5097    p->cTerm = EOF;
5098    return 0;
5099  }
5100  while( c!=EOF && c!=cSep && c!=rSep ){
5101    import_append_char(p, c);
5102    c = fgetc(p->in);
5103  }
5104  if( c==rSep ){
5105    p->nLine++;
5106  }
5107  p->cTerm = c;
5108  if( p->z ) p->z[p->n] = 0;
5109  return p->z;
5110}
5111
5112/*
5113** Try to transfer data for table zTable.  If an error is seen while
5114** moving forward, try to go backwards.  The backwards movement won't
5115** work for WITHOUT ROWID tables.
5116*/
5117static void tryToCloneData(
5118  ShellState *p,
5119  sqlite3 *newDb,
5120  const char *zTable
5121){
5122  sqlite3_stmt *pQuery = 0;
5123  sqlite3_stmt *pInsert = 0;
5124  char *zQuery = 0;
5125  char *zInsert = 0;
5126  int rc;
5127  int i, j, n;
5128  int nTable = strlen30(zTable);
5129  int k = 0;
5130  int cnt = 0;
5131  const int spinRate = 10000;
5132
5133  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5134  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5135  if( rc ){
5136    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5137            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5138            zQuery);
5139    goto end_data_xfer;
5140  }
5141  n = sqlite3_column_count(pQuery);
5142  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5143  if( zInsert==0 ) shell_out_of_memory();
5144  sqlite3_snprintf(200+nTable,zInsert,
5145                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5146  i = strlen30(zInsert);
5147  for(j=1; j<n; j++){
5148    memcpy(zInsert+i, ",?", 2);
5149    i += 2;
5150  }
5151  memcpy(zInsert+i, ");", 3);
5152  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5153  if( rc ){
5154    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5155            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5156            zQuery);
5157    goto end_data_xfer;
5158  }
5159  for(k=0; k<2; k++){
5160    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5161      for(i=0; i<n; i++){
5162        switch( sqlite3_column_type(pQuery, i) ){
5163          case SQLITE_NULL: {
5164            sqlite3_bind_null(pInsert, i+1);
5165            break;
5166          }
5167          case SQLITE_INTEGER: {
5168            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5169            break;
5170          }
5171          case SQLITE_FLOAT: {
5172            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5173            break;
5174          }
5175          case SQLITE_TEXT: {
5176            sqlite3_bind_text(pInsert, i+1,
5177                             (const char*)sqlite3_column_text(pQuery,i),
5178                             -1, SQLITE_STATIC);
5179            break;
5180          }
5181          case SQLITE_BLOB: {
5182            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5183                                            sqlite3_column_bytes(pQuery,i),
5184                                            SQLITE_STATIC);
5185            break;
5186          }
5187        }
5188      } /* End for */
5189      rc = sqlite3_step(pInsert);
5190      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5191        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5192                        sqlite3_errmsg(newDb));
5193      }
5194      sqlite3_reset(pInsert);
5195      cnt++;
5196      if( (cnt%spinRate)==0 ){
5197        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5198        fflush(stdout);
5199      }
5200    } /* End while */
5201    if( rc==SQLITE_DONE ) break;
5202    sqlite3_finalize(pQuery);
5203    sqlite3_free(zQuery);
5204    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5205                             zTable);
5206    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5207    if( rc ){
5208      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5209      break;
5210    }
5211  } /* End for(k=0...) */
5212
5213end_data_xfer:
5214  sqlite3_finalize(pQuery);
5215  sqlite3_finalize(pInsert);
5216  sqlite3_free(zQuery);
5217  sqlite3_free(zInsert);
5218}
5219
5220
5221/*
5222** Try to transfer all rows of the schema that match zWhere.  For
5223** each row, invoke xForEach() on the object defined by that row.
5224** If an error is encountered while moving forward through the
5225** sqlite_schema table, try again moving backwards.
5226*/
5227static void tryToCloneSchema(
5228  ShellState *p,
5229  sqlite3 *newDb,
5230  const char *zWhere,
5231  void (*xForEach)(ShellState*,sqlite3*,const char*)
5232){
5233  sqlite3_stmt *pQuery = 0;
5234  char *zQuery = 0;
5235  int rc;
5236  const unsigned char *zName;
5237  const unsigned char *zSql;
5238  char *zErrMsg = 0;
5239
5240  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5241                           " WHERE %s", zWhere);
5242  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5243  if( rc ){
5244    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5245                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5246                    zQuery);
5247    goto end_schema_xfer;
5248  }
5249  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5250    zName = sqlite3_column_text(pQuery, 0);
5251    zSql = sqlite3_column_text(pQuery, 1);
5252    printf("%s... ", zName); fflush(stdout);
5253    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5254    if( zErrMsg ){
5255      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5256      sqlite3_free(zErrMsg);
5257      zErrMsg = 0;
5258    }
5259    if( xForEach ){
5260      xForEach(p, newDb, (const char*)zName);
5261    }
5262    printf("done\n");
5263  }
5264  if( rc!=SQLITE_DONE ){
5265    sqlite3_finalize(pQuery);
5266    sqlite3_free(zQuery);
5267    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5268                             " WHERE %s ORDER BY rowid DESC", zWhere);
5269    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5270    if( rc ){
5271      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5272                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5273                      zQuery);
5274      goto end_schema_xfer;
5275    }
5276    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5277      zName = sqlite3_column_text(pQuery, 0);
5278      zSql = sqlite3_column_text(pQuery, 1);
5279      printf("%s... ", zName); fflush(stdout);
5280      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5281      if( zErrMsg ){
5282        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5283        sqlite3_free(zErrMsg);
5284        zErrMsg = 0;
5285      }
5286      if( xForEach ){
5287        xForEach(p, newDb, (const char*)zName);
5288      }
5289      printf("done\n");
5290    }
5291  }
5292end_schema_xfer:
5293  sqlite3_finalize(pQuery);
5294  sqlite3_free(zQuery);
5295}
5296
5297/*
5298** Open a new database file named "zNewDb".  Try to recover as much information
5299** as possible out of the main database (which might be corrupt) and write it
5300** into zNewDb.
5301*/
5302static void tryToClone(ShellState *p, const char *zNewDb){
5303  int rc;
5304  sqlite3 *newDb = 0;
5305  if( access(zNewDb,0)==0 ){
5306    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5307    return;
5308  }
5309  rc = sqlite3_open(zNewDb, &newDb);
5310  if( rc ){
5311    utf8_printf(stderr, "Cannot create output database: %s\n",
5312            sqlite3_errmsg(newDb));
5313  }else{
5314    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5315    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5316    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5317    tryToCloneSchema(p, newDb, "type!='table'", 0);
5318    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5319    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5320  }
5321  close_db(newDb);
5322}
5323
5324/*
5325** Change the output file back to stdout.
5326**
5327** If the p->doXdgOpen flag is set, that means the output was being
5328** redirected to a temporary file named by p->zTempFile.  In that case,
5329** launch start/open/xdg-open on that temporary file.
5330*/
5331static void output_reset(ShellState *p){
5332  if( p->outfile[0]=='|' ){
5333#ifndef SQLITE_OMIT_POPEN
5334    pclose(p->out);
5335#endif
5336  }else{
5337    output_file_close(p->out);
5338#ifndef SQLITE_NOHAVE_SYSTEM
5339    if( p->doXdgOpen ){
5340      const char *zXdgOpenCmd =
5341#if defined(_WIN32)
5342      "start";
5343#elif defined(__APPLE__)
5344      "open";
5345#else
5346      "xdg-open";
5347#endif
5348      char *zCmd;
5349      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5350      if( system(zCmd) ){
5351        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5352      }else{
5353        /* Give the start/open/xdg-open command some time to get
5354        ** going before we continue, and potential delete the
5355        ** p->zTempFile data file out from under it */
5356        sqlite3_sleep(2000);
5357      }
5358      sqlite3_free(zCmd);
5359      outputModePop(p);
5360      p->doXdgOpen = 0;
5361    }
5362#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5363  }
5364  p->outfile[0] = 0;
5365  p->out = stdout;
5366}
5367
5368/*
5369** Run an SQL command and return the single integer result.
5370*/
5371static int db_int(ShellState *p, const char *zSql){
5372  sqlite3_stmt *pStmt;
5373  int res = 0;
5374  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5375  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5376    res = sqlite3_column_int(pStmt,0);
5377  }
5378  sqlite3_finalize(pStmt);
5379  return res;
5380}
5381
5382/*
5383** Convert a 2-byte or 4-byte big-endian integer into a native integer
5384*/
5385static unsigned int get2byteInt(unsigned char *a){
5386  return (a[0]<<8) + a[1];
5387}
5388static unsigned int get4byteInt(unsigned char *a){
5389  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5390}
5391
5392/*
5393** Implementation of the ".dbinfo" command.
5394**
5395** Return 1 on error, 2 to exit, and 0 otherwise.
5396*/
5397static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5398  static const struct { const char *zName; int ofst; } aField[] = {
5399     { "file change counter:",  24  },
5400     { "database page count:",  28  },
5401     { "freelist page count:",  36  },
5402     { "schema cookie:",        40  },
5403     { "schema format:",        44  },
5404     { "default cache size:",   48  },
5405     { "autovacuum top root:",  52  },
5406     { "incremental vacuum:",   64  },
5407     { "text encoding:",        56  },
5408     { "user version:",         60  },
5409     { "application id:",       68  },
5410     { "software version:",     96  },
5411  };
5412  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5413     { "number of tables:",
5414       "SELECT count(*) FROM %s WHERE type='table'" },
5415     { "number of indexes:",
5416       "SELECT count(*) FROM %s WHERE type='index'" },
5417     { "number of triggers:",
5418       "SELECT count(*) FROM %s WHERE type='trigger'" },
5419     { "number of views:",
5420       "SELECT count(*) FROM %s WHERE type='view'" },
5421     { "schema size:",
5422       "SELECT total(length(sql)) FROM %s" },
5423  };
5424  int i, rc;
5425  unsigned iDataVersion;
5426  char *zSchemaTab;
5427  char *zDb = nArg>=2 ? azArg[1] : "main";
5428  sqlite3_stmt *pStmt = 0;
5429  unsigned char aHdr[100];
5430  open_db(p, 0);
5431  if( p->db==0 ) return 1;
5432  rc = sqlite3_prepare_v2(p->db,
5433             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5434             -1, &pStmt, 0);
5435  if( rc ){
5436    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5437    sqlite3_finalize(pStmt);
5438    return 1;
5439  }
5440  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5441  if( sqlite3_step(pStmt)==SQLITE_ROW
5442   && sqlite3_column_bytes(pStmt,0)>100
5443  ){
5444    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5445    sqlite3_finalize(pStmt);
5446  }else{
5447    raw_printf(stderr, "unable to read database header\n");
5448    sqlite3_finalize(pStmt);
5449    return 1;
5450  }
5451  i = get2byteInt(aHdr+16);
5452  if( i==1 ) i = 65536;
5453  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5454  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5455  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5456  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5457  for(i=0; i<ArraySize(aField); i++){
5458    int ofst = aField[i].ofst;
5459    unsigned int val = get4byteInt(aHdr + ofst);
5460    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5461    switch( ofst ){
5462      case 56: {
5463        if( val==1 ) raw_printf(p->out, " (utf8)");
5464        if( val==2 ) raw_printf(p->out, " (utf16le)");
5465        if( val==3 ) raw_printf(p->out, " (utf16be)");
5466      }
5467    }
5468    raw_printf(p->out, "\n");
5469  }
5470  if( zDb==0 ){
5471    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5472  }else if( strcmp(zDb,"temp")==0 ){
5473    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5474  }else{
5475    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5476  }
5477  for(i=0; i<ArraySize(aQuery); i++){
5478    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5479    int val = db_int(p, zSql);
5480    sqlite3_free(zSql);
5481    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5482  }
5483  sqlite3_free(zSchemaTab);
5484  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5485  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5486  return 0;
5487}
5488
5489/*
5490** Print the current sqlite3_errmsg() value to stderr and return 1.
5491*/
5492static int shellDatabaseError(sqlite3 *db){
5493  const char *zErr = sqlite3_errmsg(db);
5494  utf8_printf(stderr, "Error: %s\n", zErr);
5495  return 1;
5496}
5497
5498/*
5499** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5500** if they match and FALSE (0) if they do not match.
5501**
5502** Globbing rules:
5503**
5504**      '*'       Matches any sequence of zero or more characters.
5505**
5506**      '?'       Matches exactly one character.
5507**
5508**     [...]      Matches one character from the enclosed list of
5509**                characters.
5510**
5511**     [^...]     Matches one character not in the enclosed list.
5512**
5513**      '#'       Matches any sequence of one or more digits with an
5514**                optional + or - sign in front
5515**
5516**      ' '       Any span of whitespace matches any other span of
5517**                whitespace.
5518**
5519** Extra whitespace at the end of z[] is ignored.
5520*/
5521static int testcase_glob(const char *zGlob, const char *z){
5522  int c, c2;
5523  int invert;
5524  int seen;
5525
5526  while( (c = (*(zGlob++)))!=0 ){
5527    if( IsSpace(c) ){
5528      if( !IsSpace(*z) ) return 0;
5529      while( IsSpace(*zGlob) ) zGlob++;
5530      while( IsSpace(*z) ) z++;
5531    }else if( c=='*' ){
5532      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5533        if( c=='?' && (*(z++))==0 ) return 0;
5534      }
5535      if( c==0 ){
5536        return 1;
5537      }else if( c=='[' ){
5538        while( *z && testcase_glob(zGlob-1,z)==0 ){
5539          z++;
5540        }
5541        return (*z)!=0;
5542      }
5543      while( (c2 = (*(z++)))!=0 ){
5544        while( c2!=c ){
5545          c2 = *(z++);
5546          if( c2==0 ) return 0;
5547        }
5548        if( testcase_glob(zGlob,z) ) return 1;
5549      }
5550      return 0;
5551    }else if( c=='?' ){
5552      if( (*(z++))==0 ) return 0;
5553    }else if( c=='[' ){
5554      int prior_c = 0;
5555      seen = 0;
5556      invert = 0;
5557      c = *(z++);
5558      if( c==0 ) return 0;
5559      c2 = *(zGlob++);
5560      if( c2=='^' ){
5561        invert = 1;
5562        c2 = *(zGlob++);
5563      }
5564      if( c2==']' ){
5565        if( c==']' ) seen = 1;
5566        c2 = *(zGlob++);
5567      }
5568      while( c2 && c2!=']' ){
5569        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5570          c2 = *(zGlob++);
5571          if( c>=prior_c && c<=c2 ) seen = 1;
5572          prior_c = 0;
5573        }else{
5574          if( c==c2 ){
5575            seen = 1;
5576          }
5577          prior_c = c2;
5578        }
5579        c2 = *(zGlob++);
5580      }
5581      if( c2==0 || (seen ^ invert)==0 ) return 0;
5582    }else if( c=='#' ){
5583      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5584      if( !IsDigit(z[0]) ) return 0;
5585      z++;
5586      while( IsDigit(z[0]) ){ z++; }
5587    }else{
5588      if( c!=(*(z++)) ) return 0;
5589    }
5590  }
5591  while( IsSpace(*z) ){ z++; }
5592  return *z==0;
5593}
5594
5595
5596/*
5597** Compare the string as a command-line option with either one or two
5598** initial "-" characters.
5599*/
5600static int optionMatch(const char *zStr, const char *zOpt){
5601  if( zStr[0]!='-' ) return 0;
5602  zStr++;
5603  if( zStr[0]=='-' ) zStr++;
5604  return strcmp(zStr, zOpt)==0;
5605}
5606
5607/*
5608** Delete a file.
5609*/
5610int shellDeleteFile(const char *zFilename){
5611  int rc;
5612#ifdef _WIN32
5613  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5614  rc = _wunlink(z);
5615  sqlite3_free(z);
5616#else
5617  rc = unlink(zFilename);
5618#endif
5619  return rc;
5620}
5621
5622/*
5623** Try to delete the temporary file (if there is one) and free the
5624** memory used to hold the name of the temp file.
5625*/
5626static void clearTempFile(ShellState *p){
5627  if( p->zTempFile==0 ) return;
5628  if( p->doXdgOpen ) return;
5629  if( shellDeleteFile(p->zTempFile) ) return;
5630  sqlite3_free(p->zTempFile);
5631  p->zTempFile = 0;
5632}
5633
5634/*
5635** Create a new temp file name with the given suffix.
5636*/
5637static void newTempFile(ShellState *p, const char *zSuffix){
5638  clearTempFile(p);
5639  sqlite3_free(p->zTempFile);
5640  p->zTempFile = 0;
5641  if( p->db ){
5642    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5643  }
5644  if( p->zTempFile==0 ){
5645    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5646    ** will not work and we will need to fallback to guessing */
5647    char *zTemp;
5648    sqlite3_uint64 r;
5649    sqlite3_randomness(sizeof(r), &r);
5650    zTemp = getenv("TEMP");
5651    if( zTemp==0 ) zTemp = getenv("TMP");
5652    if( zTemp==0 ){
5653#ifdef _WIN32
5654      zTemp = "\\tmp";
5655#else
5656      zTemp = "/tmp";
5657#endif
5658    }
5659    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5660  }else{
5661    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5662  }
5663  if( p->zTempFile==0 ){
5664    raw_printf(stderr, "out of memory\n");
5665    exit(1);
5666  }
5667}
5668
5669
5670/*
5671** The implementation of SQL scalar function fkey_collate_clause(), used
5672** by the ".lint fkey-indexes" command. This scalar function is always
5673** called with four arguments - the parent table name, the parent column name,
5674** the child table name and the child column name.
5675**
5676**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5677**
5678** If either of the named tables or columns do not exist, this function
5679** returns an empty string. An empty string is also returned if both tables
5680** and columns exist but have the same default collation sequence. Or,
5681** if both exist but the default collation sequences are different, this
5682** function returns the string " COLLATE <parent-collation>", where
5683** <parent-collation> is the default collation sequence of the parent column.
5684*/
5685static void shellFkeyCollateClause(
5686  sqlite3_context *pCtx,
5687  int nVal,
5688  sqlite3_value **apVal
5689){
5690  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5691  const char *zParent;
5692  const char *zParentCol;
5693  const char *zParentSeq;
5694  const char *zChild;
5695  const char *zChildCol;
5696  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5697  int rc;
5698
5699  assert( nVal==4 );
5700  zParent = (const char*)sqlite3_value_text(apVal[0]);
5701  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5702  zChild = (const char*)sqlite3_value_text(apVal[2]);
5703  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5704
5705  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5706  rc = sqlite3_table_column_metadata(
5707      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5708  );
5709  if( rc==SQLITE_OK ){
5710    rc = sqlite3_table_column_metadata(
5711        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5712    );
5713  }
5714
5715  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5716    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5717    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5718    sqlite3_free(z);
5719  }
5720}
5721
5722
5723/*
5724** The implementation of dot-command ".lint fkey-indexes".
5725*/
5726static int lintFkeyIndexes(
5727  ShellState *pState,             /* Current shell tool state */
5728  char **azArg,                   /* Array of arguments passed to dot command */
5729  int nArg                        /* Number of entries in azArg[] */
5730){
5731  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5732  FILE *out = pState->out;        /* Stream to write non-error output to */
5733  int bVerbose = 0;               /* If -verbose is present */
5734  int bGroupByParent = 0;         /* If -groupbyparent is present */
5735  int i;                          /* To iterate through azArg[] */
5736  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5737  int rc;                         /* Return code */
5738  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5739
5740  /*
5741  ** This SELECT statement returns one row for each foreign key constraint
5742  ** in the schema of the main database. The column values are:
5743  **
5744  ** 0. The text of an SQL statement similar to:
5745  **
5746  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5747  **
5748  **    This SELECT is similar to the one that the foreign keys implementation
5749  **    needs to run internally on child tables. If there is an index that can
5750  **    be used to optimize this query, then it can also be used by the FK
5751  **    implementation to optimize DELETE or UPDATE statements on the parent
5752  **    table.
5753  **
5754  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5755  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5756  **    contains an index that can be used to optimize the query.
5757  **
5758  ** 2. Human readable text that describes the child table and columns. e.g.
5759  **
5760  **       "child_table(child_key1, child_key2)"
5761  **
5762  ** 3. Human readable text that describes the parent table and columns. e.g.
5763  **
5764  **       "parent_table(parent_key1, parent_key2)"
5765  **
5766  ** 4. A full CREATE INDEX statement for an index that could be used to
5767  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5768  **
5769  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5770  **
5771  ** 5. The name of the parent table.
5772  **
5773  ** These six values are used by the C logic below to generate the report.
5774  */
5775  const char *zSql =
5776  "SELECT "
5777    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5778    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5779    "  || fkey_collate_clause("
5780    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5781    ", "
5782    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
5783    "  || group_concat('*=?', ' AND ') || ')'"
5784    ", "
5785    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5786    ", "
5787    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5788    ", "
5789    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5790    "  || ' ON ' || quote(s.name) || '('"
5791    "  || group_concat(quote(f.[from]) ||"
5792    "        fkey_collate_clause("
5793    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5794    "  || ');'"
5795    ", "
5796    "     f.[table] "
5797    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5798    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5799    "GROUP BY s.name, f.id "
5800    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5801  ;
5802  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
5803
5804  for(i=2; i<nArg; i++){
5805    int n = strlen30(azArg[i]);
5806    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5807      bVerbose = 1;
5808    }
5809    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5810      bGroupByParent = 1;
5811      zIndent = "    ";
5812    }
5813    else{
5814      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5815          azArg[0], azArg[1]
5816      );
5817      return SQLITE_ERROR;
5818    }
5819  }
5820
5821  /* Register the fkey_collate_clause() SQL function */
5822  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5823      0, shellFkeyCollateClause, 0, 0
5824  );
5825
5826
5827  if( rc==SQLITE_OK ){
5828    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5829  }
5830  if( rc==SQLITE_OK ){
5831    sqlite3_bind_int(pSql, 1, bGroupByParent);
5832  }
5833
5834  if( rc==SQLITE_OK ){
5835    int rc2;
5836    char *zPrev = 0;
5837    while( SQLITE_ROW==sqlite3_step(pSql) ){
5838      int res = -1;
5839      sqlite3_stmt *pExplain = 0;
5840      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5841      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5842      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5843      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5844      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5845      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5846
5847      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5848      if( rc!=SQLITE_OK ) break;
5849      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5850        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5851        res = (
5852              0==sqlite3_strglob(zGlob, zPlan)
5853           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5854        );
5855      }
5856      rc = sqlite3_finalize(pExplain);
5857      if( rc!=SQLITE_OK ) break;
5858
5859      if( res<0 ){
5860        raw_printf(stderr, "Error: internal error");
5861        break;
5862      }else{
5863        if( bGroupByParent
5864        && (bVerbose || res==0)
5865        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5866        ){
5867          raw_printf(out, "-- Parent table %s\n", zParent);
5868          sqlite3_free(zPrev);
5869          zPrev = sqlite3_mprintf("%s", zParent);
5870        }
5871
5872        if( res==0 ){
5873          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5874        }else if( bVerbose ){
5875          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5876              zIndent, zFrom, zTarget
5877          );
5878        }
5879      }
5880    }
5881    sqlite3_free(zPrev);
5882
5883    if( rc!=SQLITE_OK ){
5884      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5885    }
5886
5887    rc2 = sqlite3_finalize(pSql);
5888    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5889      rc = rc2;
5890      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5891    }
5892  }else{
5893    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5894  }
5895
5896  return rc;
5897}
5898
5899/*
5900** Implementation of ".lint" dot command.
5901*/
5902static int lintDotCommand(
5903  ShellState *pState,             /* Current shell tool state */
5904  char **azArg,                   /* Array of arguments passed to dot command */
5905  int nArg                        /* Number of entries in azArg[] */
5906){
5907  int n;
5908  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5909  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5910  return lintFkeyIndexes(pState, azArg, nArg);
5911
5912 usage:
5913  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5914  raw_printf(stderr, "Where sub-commands are:\n");
5915  raw_printf(stderr, "    fkey-indexes\n");
5916  return SQLITE_ERROR;
5917}
5918
5919#if !defined SQLITE_OMIT_VIRTUALTABLE
5920static void shellPrepare(
5921  sqlite3 *db,
5922  int *pRc,
5923  const char *zSql,
5924  sqlite3_stmt **ppStmt
5925){
5926  *ppStmt = 0;
5927  if( *pRc==SQLITE_OK ){
5928    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5929    if( rc!=SQLITE_OK ){
5930      raw_printf(stderr, "sql error: %s (%d)\n",
5931          sqlite3_errmsg(db), sqlite3_errcode(db)
5932      );
5933      *pRc = rc;
5934    }
5935  }
5936}
5937
5938/*
5939** Create a prepared statement using printf-style arguments for the SQL.
5940**
5941** This routine is could be marked "static".  But it is not always used,
5942** depending on compile-time options.  By omitting the "static", we avoid
5943** nuisance compiler warnings about "defined but not used".
5944*/
5945void shellPreparePrintf(
5946  sqlite3 *db,
5947  int *pRc,
5948  sqlite3_stmt **ppStmt,
5949  const char *zFmt,
5950  ...
5951){
5952  *ppStmt = 0;
5953  if( *pRc==SQLITE_OK ){
5954    va_list ap;
5955    char *z;
5956    va_start(ap, zFmt);
5957    z = sqlite3_vmprintf(zFmt, ap);
5958    va_end(ap);
5959    if( z==0 ){
5960      *pRc = SQLITE_NOMEM;
5961    }else{
5962      shellPrepare(db, pRc, z, ppStmt);
5963      sqlite3_free(z);
5964    }
5965  }
5966}
5967
5968/* Finalize the prepared statement created using shellPreparePrintf().
5969**
5970** This routine is could be marked "static".  But it is not always used,
5971** depending on compile-time options.  By omitting the "static", we avoid
5972** nuisance compiler warnings about "defined but not used".
5973*/
5974void shellFinalize(
5975  int *pRc,
5976  sqlite3_stmt *pStmt
5977){
5978  if( pStmt ){
5979    sqlite3 *db = sqlite3_db_handle(pStmt);
5980    int rc = sqlite3_finalize(pStmt);
5981    if( *pRc==SQLITE_OK ){
5982      if( rc!=SQLITE_OK ){
5983        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5984      }
5985      *pRc = rc;
5986    }
5987  }
5988}
5989
5990/* Reset the prepared statement created using shellPreparePrintf().
5991**
5992** This routine is could be marked "static".  But it is not always used,
5993** depending on compile-time options.  By omitting the "static", we avoid
5994** nuisance compiler warnings about "defined but not used".
5995*/
5996void shellReset(
5997  int *pRc,
5998  sqlite3_stmt *pStmt
5999){
6000  int rc = sqlite3_reset(pStmt);
6001  if( *pRc==SQLITE_OK ){
6002    if( rc!=SQLITE_OK ){
6003      sqlite3 *db = sqlite3_db_handle(pStmt);
6004      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6005    }
6006    *pRc = rc;
6007  }
6008}
6009#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6010
6011#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6012/******************************************************************************
6013** The ".archive" or ".ar" command.
6014*/
6015/*
6016** Structure representing a single ".ar" command.
6017*/
6018typedef struct ArCommand ArCommand;
6019struct ArCommand {
6020  u8 eCmd;                        /* An AR_CMD_* value */
6021  u8 bVerbose;                    /* True if --verbose */
6022  u8 bZip;                        /* True if the archive is a ZIP */
6023  u8 bDryRun;                     /* True if --dry-run */
6024  u8 bAppend;                     /* True if --append */
6025  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6026  int nArg;                       /* Number of command arguments */
6027  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6028  const char *zFile;              /* --file argument, or NULL */
6029  const char *zDir;               /* --directory argument, or NULL */
6030  char **azArg;                   /* Array of command arguments */
6031  ShellState *p;                  /* Shell state */
6032  sqlite3 *db;                    /* Database containing the archive */
6033};
6034
6035/*
6036** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6037*/
6038static int arUsage(FILE *f){
6039  showHelp(f,"archive");
6040  return SQLITE_ERROR;
6041}
6042
6043/*
6044** Print an error message for the .ar command to stderr and return
6045** SQLITE_ERROR.
6046*/
6047static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6048  va_list ap;
6049  char *z;
6050  va_start(ap, zFmt);
6051  z = sqlite3_vmprintf(zFmt, ap);
6052  va_end(ap);
6053  utf8_printf(stderr, "Error: %s\n", z);
6054  if( pAr->fromCmdLine ){
6055    utf8_printf(stderr, "Use \"-A\" for more help\n");
6056  }else{
6057    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6058  }
6059  sqlite3_free(z);
6060  return SQLITE_ERROR;
6061}
6062
6063/*
6064** Values for ArCommand.eCmd.
6065*/
6066#define AR_CMD_CREATE       1
6067#define AR_CMD_UPDATE       2
6068#define AR_CMD_INSERT       3
6069#define AR_CMD_EXTRACT      4
6070#define AR_CMD_LIST         5
6071#define AR_CMD_HELP         6
6072
6073/*
6074** Other (non-command) switches.
6075*/
6076#define AR_SWITCH_VERBOSE     7
6077#define AR_SWITCH_FILE        8
6078#define AR_SWITCH_DIRECTORY   9
6079#define AR_SWITCH_APPEND     10
6080#define AR_SWITCH_DRYRUN     11
6081
6082static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6083  switch( eSwitch ){
6084    case AR_CMD_CREATE:
6085    case AR_CMD_EXTRACT:
6086    case AR_CMD_LIST:
6087    case AR_CMD_UPDATE:
6088    case AR_CMD_INSERT:
6089    case AR_CMD_HELP:
6090      if( pAr->eCmd ){
6091        return arErrorMsg(pAr, "multiple command options");
6092      }
6093      pAr->eCmd = eSwitch;
6094      break;
6095
6096    case AR_SWITCH_DRYRUN:
6097      pAr->bDryRun = 1;
6098      break;
6099    case AR_SWITCH_VERBOSE:
6100      pAr->bVerbose = 1;
6101      break;
6102    case AR_SWITCH_APPEND:
6103      pAr->bAppend = 1;
6104      /* Fall thru into --file */
6105    case AR_SWITCH_FILE:
6106      pAr->zFile = zArg;
6107      break;
6108    case AR_SWITCH_DIRECTORY:
6109      pAr->zDir = zArg;
6110      break;
6111  }
6112
6113  return SQLITE_OK;
6114}
6115
6116/*
6117** Parse the command line for an ".ar" command. The results are written into
6118** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6119** successfully, otherwise an error message is written to stderr and
6120** SQLITE_ERROR returned.
6121*/
6122static int arParseCommand(
6123  char **azArg,                   /* Array of arguments passed to dot command */
6124  int nArg,                       /* Number of entries in azArg[] */
6125  ArCommand *pAr                  /* Populate this object */
6126){
6127  struct ArSwitch {
6128    const char *zLong;
6129    char cShort;
6130    u8 eSwitch;
6131    u8 bArg;
6132  } aSwitch[] = {
6133    { "create",    'c', AR_CMD_CREATE,       0 },
6134    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6135    { "insert",    'i', AR_CMD_INSERT,       0 },
6136    { "list",      't', AR_CMD_LIST,         0 },
6137    { "update",    'u', AR_CMD_UPDATE,       0 },
6138    { "help",      'h', AR_CMD_HELP,         0 },
6139    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6140    { "file",      'f', AR_SWITCH_FILE,      1 },
6141    { "append",    'a', AR_SWITCH_APPEND,    1 },
6142    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6143    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6144  };
6145  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6146  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6147
6148  if( nArg<=1 ){
6149    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6150    return arUsage(stderr);
6151  }else{
6152    char *z = azArg[1];
6153    if( z[0]!='-' ){
6154      /* Traditional style [tar] invocation */
6155      int i;
6156      int iArg = 2;
6157      for(i=0; z[i]; i++){
6158        const char *zArg = 0;
6159        struct ArSwitch *pOpt;
6160        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6161          if( z[i]==pOpt->cShort ) break;
6162        }
6163        if( pOpt==pEnd ){
6164          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6165        }
6166        if( pOpt->bArg ){
6167          if( iArg>=nArg ){
6168            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6169          }
6170          zArg = azArg[iArg++];
6171        }
6172        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6173      }
6174      pAr->nArg = nArg-iArg;
6175      if( pAr->nArg>0 ){
6176        pAr->azArg = &azArg[iArg];
6177      }
6178    }else{
6179      /* Non-traditional invocation */
6180      int iArg;
6181      for(iArg=1; iArg<nArg; iArg++){
6182        int n;
6183        z = azArg[iArg];
6184        if( z[0]!='-' ){
6185          /* All remaining command line words are command arguments. */
6186          pAr->azArg = &azArg[iArg];
6187          pAr->nArg = nArg-iArg;
6188          break;
6189        }
6190        n = strlen30(z);
6191
6192        if( z[1]!='-' ){
6193          int i;
6194          /* One or more short options */
6195          for(i=1; i<n; i++){
6196            const char *zArg = 0;
6197            struct ArSwitch *pOpt;
6198            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6199              if( z[i]==pOpt->cShort ) break;
6200            }
6201            if( pOpt==pEnd ){
6202              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6203            }
6204            if( pOpt->bArg ){
6205              if( i<(n-1) ){
6206                zArg = &z[i+1];
6207                i = n;
6208              }else{
6209                if( iArg>=(nArg-1) ){
6210                  return arErrorMsg(pAr, "option requires an argument: %c",
6211                                    z[i]);
6212                }
6213                zArg = azArg[++iArg];
6214              }
6215            }
6216            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6217          }
6218        }else if( z[2]=='\0' ){
6219          /* A -- option, indicating that all remaining command line words
6220          ** are command arguments.  */
6221          pAr->azArg = &azArg[iArg+1];
6222          pAr->nArg = nArg-iArg-1;
6223          break;
6224        }else{
6225          /* A long option */
6226          const char *zArg = 0;             /* Argument for option, if any */
6227          struct ArSwitch *pMatch = 0;      /* Matching option */
6228          struct ArSwitch *pOpt;            /* Iterator */
6229          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6230            const char *zLong = pOpt->zLong;
6231            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6232              if( pMatch ){
6233                return arErrorMsg(pAr, "ambiguous option: %s",z);
6234              }else{
6235                pMatch = pOpt;
6236              }
6237            }
6238          }
6239
6240          if( pMatch==0 ){
6241            return arErrorMsg(pAr, "unrecognized option: %s", z);
6242          }
6243          if( pMatch->bArg ){
6244            if( iArg>=(nArg-1) ){
6245              return arErrorMsg(pAr, "option requires an argument: %s", z);
6246            }
6247            zArg = azArg[++iArg];
6248          }
6249          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6250        }
6251      }
6252    }
6253  }
6254
6255  return SQLITE_OK;
6256}
6257
6258/*
6259** This function assumes that all arguments within the ArCommand.azArg[]
6260** array refer to archive members, as for the --extract or --list commands.
6261** It checks that each of them are present. If any specified file is not
6262** present in the archive, an error is printed to stderr and an error
6263** code returned. Otherwise, if all specified arguments are present in
6264** the archive, SQLITE_OK is returned.
6265**
6266** This function strips any trailing '/' characters from each argument.
6267** This is consistent with the way the [tar] command seems to work on
6268** Linux.
6269*/
6270static int arCheckEntries(ArCommand *pAr){
6271  int rc = SQLITE_OK;
6272  if( pAr->nArg ){
6273    int i, j;
6274    sqlite3_stmt *pTest = 0;
6275
6276    shellPreparePrintf(pAr->db, &rc, &pTest,
6277        "SELECT name FROM %s WHERE name=$name",
6278        pAr->zSrcTable
6279    );
6280    j = sqlite3_bind_parameter_index(pTest, "$name");
6281    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6282      char *z = pAr->azArg[i];
6283      int n = strlen30(z);
6284      int bOk = 0;
6285      while( n>0 && z[n-1]=='/' ) n--;
6286      z[n] = '\0';
6287      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6288      if( SQLITE_ROW==sqlite3_step(pTest) ){
6289        bOk = 1;
6290      }
6291      shellReset(&rc, pTest);
6292      if( rc==SQLITE_OK && bOk==0 ){
6293        utf8_printf(stderr, "not found in archive: %s\n", z);
6294        rc = SQLITE_ERROR;
6295      }
6296    }
6297    shellFinalize(&rc, pTest);
6298  }
6299  return rc;
6300}
6301
6302/*
6303** Format a WHERE clause that can be used against the "sqlar" table to
6304** identify all archive members that match the command arguments held
6305** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6306** The caller is responsible for eventually calling sqlite3_free() on
6307** any non-NULL (*pzWhere) value.
6308*/
6309static void arWhereClause(
6310  int *pRc,
6311  ArCommand *pAr,
6312  char **pzWhere                  /* OUT: New WHERE clause */
6313){
6314  char *zWhere = 0;
6315  if( *pRc==SQLITE_OK ){
6316    if( pAr->nArg==0 ){
6317      zWhere = sqlite3_mprintf("1");
6318    }else{
6319      int i;
6320      const char *zSep = "";
6321      for(i=0; i<pAr->nArg; i++){
6322        const char *z = pAr->azArg[i];
6323        zWhere = sqlite3_mprintf(
6324          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6325          zWhere, zSep, z, strlen30(z)+1, z
6326        );
6327        if( zWhere==0 ){
6328          *pRc = SQLITE_NOMEM;
6329          break;
6330        }
6331        zSep = " OR ";
6332      }
6333    }
6334  }
6335  *pzWhere = zWhere;
6336}
6337
6338/*
6339** Implementation of .ar "lisT" command.
6340*/
6341static int arListCommand(ArCommand *pAr){
6342  const char *zSql = "SELECT %s FROM %s WHERE %s";
6343  const char *azCols[] = {
6344    "name",
6345    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6346  };
6347
6348  char *zWhere = 0;
6349  sqlite3_stmt *pSql = 0;
6350  int rc;
6351
6352  rc = arCheckEntries(pAr);
6353  arWhereClause(&rc, pAr, &zWhere);
6354
6355  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6356                     pAr->zSrcTable, zWhere);
6357  if( pAr->bDryRun ){
6358    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6359  }else{
6360    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6361      if( pAr->bVerbose ){
6362        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6363            sqlite3_column_text(pSql, 0),
6364            sqlite3_column_int(pSql, 1),
6365            sqlite3_column_text(pSql, 2),
6366            sqlite3_column_text(pSql, 3)
6367        );
6368      }else{
6369        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6370      }
6371    }
6372  }
6373  shellFinalize(&rc, pSql);
6374  sqlite3_free(zWhere);
6375  return rc;
6376}
6377
6378
6379/*
6380** Implementation of .ar "eXtract" command.
6381*/
6382static int arExtractCommand(ArCommand *pAr){
6383  const char *zSql1 =
6384    "SELECT "
6385    " ($dir || name),"
6386    " writefile(($dir || name), %s, mode, mtime) "
6387    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6388    " AND name NOT GLOB '*..[/\\]*'";
6389
6390  const char *azExtraArg[] = {
6391    "sqlar_uncompress(data, sz)",
6392    "data"
6393  };
6394
6395  sqlite3_stmt *pSql = 0;
6396  int rc = SQLITE_OK;
6397  char *zDir = 0;
6398  char *zWhere = 0;
6399  int i, j;
6400
6401  /* If arguments are specified, check that they actually exist within
6402  ** the archive before proceeding. And formulate a WHERE clause to
6403  ** match them.  */
6404  rc = arCheckEntries(pAr);
6405  arWhereClause(&rc, pAr, &zWhere);
6406
6407  if( rc==SQLITE_OK ){
6408    if( pAr->zDir ){
6409      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6410    }else{
6411      zDir = sqlite3_mprintf("");
6412    }
6413    if( zDir==0 ) rc = SQLITE_NOMEM;
6414  }
6415
6416  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6417      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6418  );
6419
6420  if( rc==SQLITE_OK ){
6421    j = sqlite3_bind_parameter_index(pSql, "$dir");
6422    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6423
6424    /* Run the SELECT statement twice. The first time, writefile() is called
6425    ** for all archive members that should be extracted. The second time,
6426    ** only for the directories. This is because the timestamps for
6427    ** extracted directories must be reset after they are populated (as
6428    ** populating them changes the timestamp).  */
6429    for(i=0; i<2; i++){
6430      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6431      sqlite3_bind_int(pSql, j, i);
6432      if( pAr->bDryRun ){
6433        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6434      }else{
6435        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6436          if( i==0 && pAr->bVerbose ){
6437            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6438          }
6439        }
6440      }
6441      shellReset(&rc, pSql);
6442    }
6443    shellFinalize(&rc, pSql);
6444  }
6445
6446  sqlite3_free(zDir);
6447  sqlite3_free(zWhere);
6448  return rc;
6449}
6450
6451/*
6452** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6453*/
6454static int arExecSql(ArCommand *pAr, const char *zSql){
6455  int rc;
6456  if( pAr->bDryRun ){
6457    utf8_printf(pAr->p->out, "%s\n", zSql);
6458    rc = SQLITE_OK;
6459  }else{
6460    char *zErr = 0;
6461    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6462    if( zErr ){
6463      utf8_printf(stdout, "ERROR: %s\n", zErr);
6464      sqlite3_free(zErr);
6465    }
6466  }
6467  return rc;
6468}
6469
6470
6471/*
6472** Implementation of .ar "create", "insert", and "update" commands.
6473**
6474**     create    ->     Create a new SQL archive
6475**     insert    ->     Insert or reinsert all files listed
6476**     update    ->     Insert files that have changed or that were not
6477**                      previously in the archive
6478**
6479** Create the "sqlar" table in the database if it does not already exist.
6480** Then add each file in the azFile[] array to the archive. Directories
6481** are added recursively. If argument bVerbose is non-zero, a message is
6482** printed on stdout for each file archived.
6483**
6484** The create command is the same as update, except that it drops
6485** any existing "sqlar" table before beginning.  The "insert" command
6486** always overwrites every file named on the command-line, where as
6487** "update" only overwrites if the size or mtime or mode has changed.
6488*/
6489static int arCreateOrUpdateCommand(
6490  ArCommand *pAr,                 /* Command arguments and options */
6491  int bUpdate,                    /* true for a --create. */
6492  int bOnlyIfChanged              /* Only update if file has changed */
6493){
6494  const char *zCreate =
6495      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6496      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6497      "  mode INT,               -- access permissions\n"
6498      "  mtime INT,              -- last modification time\n"
6499      "  sz INT,                 -- original file size\n"
6500      "  data BLOB               -- compressed content\n"
6501      ")";
6502  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6503  const char *zInsertFmt[2] = {
6504     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6505     "  SELECT\n"
6506     "    %s,\n"
6507     "    mode,\n"
6508     "    mtime,\n"
6509     "    CASE substr(lsmode(mode),1,1)\n"
6510     "      WHEN '-' THEN length(data)\n"
6511     "      WHEN 'd' THEN 0\n"
6512     "      ELSE -1 END,\n"
6513     "    sqlar_compress(data)\n"
6514     "  FROM fsdir(%Q,%Q) AS disk\n"
6515     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6516     ,
6517     "REPLACE INTO %s(name,mode,mtime,data)\n"
6518     "  SELECT\n"
6519     "    %s,\n"
6520     "    mode,\n"
6521     "    mtime,\n"
6522     "    data\n"
6523     "  FROM fsdir(%Q,%Q) AS disk\n"
6524     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6525  };
6526  int i;                          /* For iterating through azFile[] */
6527  int rc;                         /* Return code */
6528  const char *zTab = 0;           /* SQL table into which to insert */
6529  char *zSql;
6530  char zTemp[50];
6531  char *zExists = 0;
6532
6533  arExecSql(pAr, "PRAGMA page_size=512");
6534  rc = arExecSql(pAr, "SAVEPOINT ar;");
6535  if( rc!=SQLITE_OK ) return rc;
6536  zTemp[0] = 0;
6537  if( pAr->bZip ){
6538    /* Initialize the zipfile virtual table, if necessary */
6539    if( pAr->zFile ){
6540      sqlite3_uint64 r;
6541      sqlite3_randomness(sizeof(r),&r);
6542      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6543      zTab = zTemp;
6544      zSql = sqlite3_mprintf(
6545         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6546         zTab, pAr->zFile
6547      );
6548      rc = arExecSql(pAr, zSql);
6549      sqlite3_free(zSql);
6550    }else{
6551      zTab = "zip";
6552    }
6553  }else{
6554    /* Initialize the table for an SQLAR */
6555    zTab = "sqlar";
6556    if( bUpdate==0 ){
6557      rc = arExecSql(pAr, zDrop);
6558      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6559    }
6560    rc = arExecSql(pAr, zCreate);
6561  }
6562  if( bOnlyIfChanged ){
6563    zExists = sqlite3_mprintf(
6564      " AND NOT EXISTS("
6565          "SELECT 1 FROM %s AS mem"
6566          " WHERE mem.name=disk.name"
6567          " AND mem.mtime=disk.mtime"
6568          " AND mem.mode=disk.mode)", zTab);
6569  }else{
6570    zExists = sqlite3_mprintf("");
6571  }
6572  if( zExists==0 ) rc = SQLITE_NOMEM;
6573  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6574    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6575        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6576        pAr->azArg[i], pAr->zDir, zExists);
6577    rc = arExecSql(pAr, zSql2);
6578    sqlite3_free(zSql2);
6579  }
6580end_ar_transaction:
6581  if( rc!=SQLITE_OK ){
6582    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6583  }else{
6584    rc = arExecSql(pAr, "RELEASE ar;");
6585    if( pAr->bZip && pAr->zFile ){
6586      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6587      arExecSql(pAr, zSql);
6588      sqlite3_free(zSql);
6589    }
6590  }
6591  sqlite3_free(zExists);
6592  return rc;
6593}
6594
6595/*
6596** Implementation of ".ar" dot command.
6597*/
6598static int arDotCommand(
6599  ShellState *pState,          /* Current shell tool state */
6600  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6601  char **azArg,                /* Array of arguments passed to dot command */
6602  int nArg                     /* Number of entries in azArg[] */
6603){
6604  ArCommand cmd;
6605  int rc;
6606  memset(&cmd, 0, sizeof(cmd));
6607  cmd.fromCmdLine = fromCmdLine;
6608  rc = arParseCommand(azArg, nArg, &cmd);
6609  if( rc==SQLITE_OK ){
6610    int eDbType = SHELL_OPEN_UNSPEC;
6611    cmd.p = pState;
6612    cmd.db = pState->db;
6613    if( cmd.zFile ){
6614      eDbType = deduceDatabaseType(cmd.zFile, 1);
6615    }else{
6616      eDbType = pState->openMode;
6617    }
6618    if( eDbType==SHELL_OPEN_ZIPFILE ){
6619      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6620        if( cmd.zFile==0 ){
6621          cmd.zSrcTable = sqlite3_mprintf("zip");
6622        }else{
6623          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6624        }
6625      }
6626      cmd.bZip = 1;
6627    }else if( cmd.zFile ){
6628      int flags;
6629      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6630      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6631           || cmd.eCmd==AR_CMD_UPDATE ){
6632        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6633      }else{
6634        flags = SQLITE_OPEN_READONLY;
6635      }
6636      cmd.db = 0;
6637      if( cmd.bDryRun ){
6638        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6639             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6640      }
6641      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6642             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6643      if( rc!=SQLITE_OK ){
6644        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6645            cmd.zFile, sqlite3_errmsg(cmd.db)
6646        );
6647        goto end_ar_command;
6648      }
6649      sqlite3_fileio_init(cmd.db, 0, 0);
6650      sqlite3_sqlar_init(cmd.db, 0, 0);
6651      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6652                              shellPutsFunc, 0, 0);
6653
6654    }
6655    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6656      if( cmd.eCmd!=AR_CMD_CREATE
6657       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6658      ){
6659        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6660        rc = SQLITE_ERROR;
6661        goto end_ar_command;
6662      }
6663      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6664    }
6665
6666    switch( cmd.eCmd ){
6667      case AR_CMD_CREATE:
6668        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6669        break;
6670
6671      case AR_CMD_EXTRACT:
6672        rc = arExtractCommand(&cmd);
6673        break;
6674
6675      case AR_CMD_LIST:
6676        rc = arListCommand(&cmd);
6677        break;
6678
6679      case AR_CMD_HELP:
6680        arUsage(pState->out);
6681        break;
6682
6683      case AR_CMD_INSERT:
6684        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6685        break;
6686
6687      default:
6688        assert( cmd.eCmd==AR_CMD_UPDATE );
6689        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6690        break;
6691    }
6692  }
6693end_ar_command:
6694  if( cmd.db!=pState->db ){
6695    close_db(cmd.db);
6696  }
6697  sqlite3_free(cmd.zSrcTable);
6698
6699  return rc;
6700}
6701/* End of the ".archive" or ".ar" command logic
6702*******************************************************************************/
6703#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6704
6705#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6706/*
6707** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6708** Otherwise, the SQL statement or statements in zSql are executed using
6709** database connection db and the error code written to *pRc before
6710** this function returns.
6711*/
6712static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6713  int rc = *pRc;
6714  if( rc==SQLITE_OK ){
6715    char *zErr = 0;
6716    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6717    if( rc!=SQLITE_OK ){
6718      raw_printf(stderr, "SQL error: %s\n", zErr);
6719    }
6720    sqlite3_free(zErr);
6721    *pRc = rc;
6722  }
6723}
6724
6725/*
6726** Like shellExec(), except that zFmt is a printf() style format string.
6727*/
6728static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6729  char *z = 0;
6730  if( *pRc==SQLITE_OK ){
6731    va_list ap;
6732    va_start(ap, zFmt);
6733    z = sqlite3_vmprintf(zFmt, ap);
6734    va_end(ap);
6735    if( z==0 ){
6736      *pRc = SQLITE_NOMEM;
6737    }else{
6738      shellExec(db, pRc, z);
6739    }
6740    sqlite3_free(z);
6741  }
6742}
6743
6744/*
6745** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6746** Otherwise, an attempt is made to allocate, zero and return a pointer
6747** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6748** to SQLITE_NOMEM and NULL returned.
6749*/
6750static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6751  void *pRet = 0;
6752  if( *pRc==SQLITE_OK ){
6753    pRet = sqlite3_malloc64(nByte);
6754    if( pRet==0 ){
6755      *pRc = SQLITE_NOMEM;
6756    }else{
6757      memset(pRet, 0, nByte);
6758    }
6759  }
6760  return pRet;
6761}
6762
6763/*
6764** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6765** Otherwise, zFmt is treated as a printf() style string. The result of
6766** formatting it along with any trailing arguments is written into a
6767** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6768** It is the responsibility of the caller to eventually free this buffer
6769** using a call to sqlite3_free().
6770**
6771** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6772** pointer returned.
6773*/
6774static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6775  char *z = 0;
6776  if( *pRc==SQLITE_OK ){
6777    va_list ap;
6778    va_start(ap, zFmt);
6779    z = sqlite3_vmprintf(zFmt, ap);
6780    va_end(ap);
6781    if( z==0 ){
6782      *pRc = SQLITE_NOMEM;
6783    }
6784  }
6785  return z;
6786}
6787
6788/*
6789** When running the ".recover" command, each output table, and the special
6790** orphaned row table if it is required, is represented by an instance
6791** of the following struct.
6792*/
6793typedef struct RecoverTable RecoverTable;
6794struct RecoverTable {
6795  char *zQuoted;                  /* Quoted version of table name */
6796  int nCol;                       /* Number of columns in table */
6797  char **azlCol;                  /* Array of column lists */
6798  int iPk;                        /* Index of IPK column */
6799};
6800
6801/*
6802** Free a RecoverTable object allocated by recoverFindTable() or
6803** recoverOrphanTable().
6804*/
6805static void recoverFreeTable(RecoverTable *pTab){
6806  if( pTab ){
6807    sqlite3_free(pTab->zQuoted);
6808    if( pTab->azlCol ){
6809      int i;
6810      for(i=0; i<=pTab->nCol; i++){
6811        sqlite3_free(pTab->azlCol[i]);
6812      }
6813      sqlite3_free(pTab->azlCol);
6814    }
6815    sqlite3_free(pTab);
6816  }
6817}
6818
6819/*
6820** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6821** Otherwise, it allocates and returns a RecoverTable object based on the
6822** final four arguments passed to this function. It is the responsibility
6823** of the caller to eventually free the returned object using
6824** recoverFreeTable().
6825*/
6826static RecoverTable *recoverNewTable(
6827  int *pRc,                       /* IN/OUT: Error code */
6828  const char *zName,              /* Name of table */
6829  const char *zSql,               /* CREATE TABLE statement */
6830  int bIntkey,
6831  int nCol
6832){
6833  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6834  int rc = *pRc;
6835  RecoverTable *pTab = 0;
6836
6837  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6838  if( rc==SQLITE_OK ){
6839    int nSqlCol = 0;
6840    int bSqlIntkey = 0;
6841    sqlite3_stmt *pStmt = 0;
6842
6843    rc = sqlite3_open("", &dbtmp);
6844    if( rc==SQLITE_OK ){
6845      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6846                              shellIdQuote, 0, 0);
6847    }
6848    if( rc==SQLITE_OK ){
6849      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6850    }
6851    if( rc==SQLITE_OK ){
6852      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6853      if( rc==SQLITE_ERROR ){
6854        rc = SQLITE_OK;
6855        goto finished;
6856      }
6857    }
6858    shellPreparePrintf(dbtmp, &rc, &pStmt,
6859        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6860    );
6861    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6862      nSqlCol = sqlite3_column_int(pStmt, 0);
6863    }
6864    shellFinalize(&rc, pStmt);
6865
6866    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6867      goto finished;
6868    }
6869
6870    shellPreparePrintf(dbtmp, &rc, &pStmt,
6871      "SELECT ("
6872      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6873      ") FROM sqlite_schema WHERE name = %Q", zName
6874    );
6875    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6876      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6877    }
6878    shellFinalize(&rc, pStmt);
6879
6880    if( bIntkey==bSqlIntkey ){
6881      int i;
6882      const char *zPk = "_rowid_";
6883      sqlite3_stmt *pPkFinder = 0;
6884
6885      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6886      ** set zPk to the name of the PK column, and pTab->iPk to the index
6887      ** of the column, where columns are 0-numbered from left to right.
6888      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6889      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6890      pTab->iPk = -2;
6891      if( bIntkey ){
6892        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6893          "SELECT cid, name FROM pragma_table_info(%Q) "
6894          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6895          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6896          , zName, zName
6897        );
6898        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6899          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6900          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6901        }
6902      }
6903
6904      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6905      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6906      pTab->nCol = nSqlCol;
6907
6908      if( bIntkey ){
6909        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6910      }else{
6911        pTab->azlCol[0] = shellMPrintf(&rc, "");
6912      }
6913      i = 1;
6914      shellPreparePrintf(dbtmp, &rc, &pStmt,
6915          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6916          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6917          "FROM pragma_table_info(%Q)",
6918          bIntkey ? ", " : "", pTab->iPk,
6919          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6920          zName
6921      );
6922      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6923        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6924        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6925        i++;
6926      }
6927      shellFinalize(&rc, pStmt);
6928
6929      shellFinalize(&rc, pPkFinder);
6930    }
6931  }
6932
6933 finished:
6934  sqlite3_close(dbtmp);
6935  *pRc = rc;
6936  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6937    recoverFreeTable(pTab);
6938    pTab = 0;
6939  }
6940  return pTab;
6941}
6942
6943/*
6944** This function is called to search the schema recovered from the
6945** sqlite_schema table of the (possibly) corrupt database as part
6946** of a ".recover" command. Specifically, for a table with root page
6947** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6948** table must be a WITHOUT ROWID table, or if non-zero, not one of
6949** those.
6950**
6951** If a table is found, a (RecoverTable*) object is returned. Or, if
6952** no such table is found, but bIntkey is false and iRoot is the
6953** root page of an index in the recovered schema, then (*pbNoop) is
6954** set to true and NULL returned. Or, if there is no such table or
6955** index, NULL is returned and (*pbNoop) set to 0, indicating that
6956** the caller should write data to the orphans table.
6957*/
6958static RecoverTable *recoverFindTable(
6959  ShellState *pState,             /* Shell state object */
6960  int *pRc,                       /* IN/OUT: Error code */
6961  int iRoot,                      /* Root page of table */
6962  int bIntkey,                    /* True for an intkey table */
6963  int nCol,                       /* Number of columns in table */
6964  int *pbNoop                     /* OUT: True if iRoot is root of index */
6965){
6966  sqlite3_stmt *pStmt = 0;
6967  RecoverTable *pRet = 0;
6968  int bNoop = 0;
6969  const char *zSql = 0;
6970  const char *zName = 0;
6971
6972  /* Search the recovered schema for an object with root page iRoot. */
6973  shellPreparePrintf(pState->db, pRc, &pStmt,
6974      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6975  );
6976  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6977    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6978    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6979      bNoop = 1;
6980      break;
6981    }
6982    if( sqlite3_stricmp(zType, "table")==0 ){
6983      zName = (const char*)sqlite3_column_text(pStmt, 1);
6984      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6985      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6986      break;
6987    }
6988  }
6989
6990  shellFinalize(pRc, pStmt);
6991  *pbNoop = bNoop;
6992  return pRet;
6993}
6994
6995/*
6996** Return a RecoverTable object representing the orphans table.
6997*/
6998static RecoverTable *recoverOrphanTable(
6999  ShellState *pState,             /* Shell state object */
7000  int *pRc,                       /* IN/OUT: Error code */
7001  const char *zLostAndFound,      /* Base name for orphans table */
7002  int nCol                        /* Number of user data columns */
7003){
7004  RecoverTable *pTab = 0;
7005  if( nCol>=0 && *pRc==SQLITE_OK ){
7006    int i;
7007
7008    /* This block determines the name of the orphan table. The prefered
7009    ** name is zLostAndFound. But if that clashes with another name
7010    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7011    ** and so on until a non-clashing name is found.  */
7012    int iTab = 0;
7013    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7014    sqlite3_stmt *pTest = 0;
7015    shellPrepare(pState->db, pRc,
7016        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7017    );
7018    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7019    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7020      shellReset(pRc, pTest);
7021      sqlite3_free(zTab);
7022      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7023      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7024    }
7025    shellFinalize(pRc, pTest);
7026
7027    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7028    if( pTab ){
7029      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7030      pTab->nCol = nCol;
7031      pTab->iPk = -2;
7032      if( nCol>0 ){
7033        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7034        if( pTab->azlCol ){
7035          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7036          for(i=nCol-1; i>=0; i--){
7037            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7038          }
7039        }
7040      }
7041
7042      if( *pRc!=SQLITE_OK ){
7043        recoverFreeTable(pTab);
7044        pTab = 0;
7045      }else{
7046        raw_printf(pState->out,
7047            "CREATE TABLE %s(rootpgno INTEGER, "
7048            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7049        );
7050        for(i=0; i<nCol; i++){
7051          raw_printf(pState->out, ", c%d", i);
7052        }
7053        raw_printf(pState->out, ");\n");
7054      }
7055    }
7056    sqlite3_free(zTab);
7057  }
7058  return pTab;
7059}
7060
7061/*
7062** This function is called to recover data from the database. A script
7063** to construct a new database containing all recovered data is output
7064** on stream pState->out.
7065*/
7066static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7067  int rc = SQLITE_OK;
7068  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7069  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7070  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7071  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7072  const char *zLostAndFound = "lost_and_found";
7073  int i;
7074  int nOrphan = -1;
7075  RecoverTable *pOrphan = 0;
7076
7077  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7078  int bRowids = 1;                /* 0 if --no-rowids */
7079  for(i=1; i<nArg; i++){
7080    char *z = azArg[i];
7081    int n;
7082    if( z[0]=='-' && z[1]=='-' ) z++;
7083    n = strlen30(z);
7084    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7085      bFreelist = 0;
7086    }else
7087    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7088      i++;
7089      zRecoveryDb = azArg[i];
7090    }else
7091    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7092      i++;
7093      zLostAndFound = azArg[i];
7094    }else
7095    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7096      bRowids = 0;
7097    }
7098    else{
7099      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7100      showHelp(pState->out, azArg[0]);
7101      return 1;
7102    }
7103  }
7104
7105  shellExecPrintf(pState->db, &rc,
7106    /* Attach an in-memory database named 'recovery'. Create an indexed
7107    ** cache of the sqlite_dbptr virtual table. */
7108    "PRAGMA writable_schema = on;"
7109    "ATTACH %Q AS recovery;"
7110    "DROP TABLE IF EXISTS recovery.dbptr;"
7111    "DROP TABLE IF EXISTS recovery.freelist;"
7112    "DROP TABLE IF EXISTS recovery.map;"
7113    "DROP TABLE IF EXISTS recovery.schema;"
7114    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7115  );
7116
7117  if( bFreelist ){
7118    shellExec(pState->db, &rc,
7119      "WITH trunk(pgno) AS ("
7120      "  SELECT shell_int32("
7121      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7122      "      WHERE x>0"
7123      "    UNION"
7124      "  SELECT shell_int32("
7125      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7126      "      FROM trunk WHERE x>0"
7127      "),"
7128      "freelist(data, n, freepgno) AS ("
7129      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7130      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7131      "    UNION ALL"
7132      "  SELECT data, n-1, shell_int32(data, 2+n) "
7133      "      FROM freelist WHERE n>=0"
7134      ")"
7135      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7136    );
7137  }
7138
7139  /* If this is an auto-vacuum database, add all pointer-map pages to
7140  ** the freelist table. Do this regardless of whether or not
7141  ** --freelist-corrupt was specified.  */
7142  shellExec(pState->db, &rc,
7143    "WITH ptrmap(pgno) AS ("
7144    "  SELECT 2 WHERE shell_int32("
7145    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7146    "  )"
7147    "    UNION ALL "
7148    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7149    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7150    ")"
7151    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7152  );
7153
7154  shellExec(pState->db, &rc,
7155    "CREATE TABLE recovery.dbptr("
7156    "      pgno, child, PRIMARY KEY(child, pgno)"
7157    ") WITHOUT ROWID;"
7158    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7159    "    SELECT * FROM sqlite_dbptr"
7160    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7161
7162    /* Delete any pointer to page 1. This ensures that page 1 is considered
7163    ** a root page, regardless of how corrupt the db is. */
7164    "DELETE FROM recovery.dbptr WHERE child = 1;"
7165
7166    /* Delete all pointers to any pages that have more than one pointer
7167    ** to them. Such pages will be treated as root pages when recovering
7168    ** data.  */
7169    "DELETE FROM recovery.dbptr WHERE child IN ("
7170    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7171    ");"
7172
7173    /* Create the "map" table that will (eventually) contain instructions
7174    ** for dealing with each page in the db that contains one or more
7175    ** records. */
7176    "CREATE TABLE recovery.map("
7177      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7178    ");"
7179
7180    /* Populate table [map]. If there are circular loops of pages in the
7181    ** database, the following adds all pages in such a loop to the map
7182    ** as individual root pages. This could be handled better.  */
7183    "WITH pages(i, maxlen) AS ("
7184    "  SELECT page_count, ("
7185    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7186    "  ) FROM pragma_page_count WHERE page_count>0"
7187    "    UNION ALL"
7188    "  SELECT i-1, ("
7189    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7190    "  ) FROM pages WHERE i>=2"
7191    ")"
7192    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7193    "  SELECT i, maxlen, NULL, ("
7194    "    WITH p(orig, pgno, parent) AS ("
7195    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7196    "        UNION "
7197    "      SELECT i, p.parent, "
7198    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7199    "    )"
7200    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7201    ") "
7202    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7203    "UPDATE recovery.map AS o SET intkey = ("
7204    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7205    ");"
7206
7207    /* Extract data from page 1 and any linked pages into table
7208    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7209    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7210    "INSERT INTO recovery.schema SELECT "
7211    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7212    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7213    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7214    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7215    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7216    "FROM sqlite_dbdata WHERE pgno IN ("
7217    "  SELECT pgno FROM recovery.map WHERE root=1"
7218    ")"
7219    "GROUP BY pgno, cell;"
7220    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7221  );
7222
7223  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7224  ** CREATE TABLE statements that extracted from the existing schema.  */
7225  if( rc==SQLITE_OK ){
7226    sqlite3_stmt *pStmt = 0;
7227    /* ".recover" might output content in an order which causes immediate
7228    ** foreign key constraints to be violated. So disable foreign-key
7229    ** constraint enforcement to prevent problems when running the output
7230    ** script. */
7231    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7232    raw_printf(pState->out, "BEGIN;\n");
7233    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7234    shellPrepare(pState->db, &rc,
7235        "SELECT sql FROM recovery.schema "
7236        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7237    );
7238    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7239      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7240      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7241          &zCreateTable[12]
7242      );
7243    }
7244    shellFinalize(&rc, pStmt);
7245  }
7246
7247  /* Figure out if an orphan table will be required. And if so, how many
7248  ** user columns it should contain */
7249  shellPrepare(pState->db, &rc,
7250      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7251      , &pLoop
7252  );
7253  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7254    nOrphan = sqlite3_column_int(pLoop, 0);
7255  }
7256  shellFinalize(&rc, pLoop);
7257  pLoop = 0;
7258
7259  shellPrepare(pState->db, &rc,
7260      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7261  );
7262
7263  shellPrepare(pState->db, &rc,
7264      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7265      "(case when (? AND field<0) then NULL else value end)"
7266      "), ', ')"
7267      ", min(field) "
7268      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7269      "GROUP BY cell", &pCells
7270  );
7271
7272  /* Loop through each root page. */
7273  shellPrepare(pState->db, &rc,
7274      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7275      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7276      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7277      ")", &pLoop
7278  );
7279  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7280    int iRoot = sqlite3_column_int(pLoop, 0);
7281    int bIntkey = sqlite3_column_int(pLoop, 1);
7282    int nCol = sqlite3_column_int(pLoop, 2);
7283    int bNoop = 0;
7284    RecoverTable *pTab;
7285
7286    assert( bIntkey==0 || bIntkey==1 );
7287    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7288    if( bNoop || rc ) continue;
7289    if( pTab==0 ){
7290      if( pOrphan==0 ){
7291        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7292      }
7293      pTab = pOrphan;
7294      if( pTab==0 ) break;
7295    }
7296
7297    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7298      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7299    }
7300    sqlite3_bind_int(pPages, 1, iRoot);
7301    if( bRowids==0 && pTab->iPk<0 ){
7302      sqlite3_bind_int(pCells, 1, 1);
7303    }else{
7304      sqlite3_bind_int(pCells, 1, 0);
7305    }
7306    sqlite3_bind_int(pCells, 3, pTab->iPk);
7307
7308    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7309      int iPgno = sqlite3_column_int(pPages, 0);
7310      sqlite3_bind_int(pCells, 2, iPgno);
7311      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7312        int nField = sqlite3_column_int(pCells, 0);
7313        int iMin = sqlite3_column_int(pCells, 2);
7314        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7315
7316        RecoverTable *pTab2 = pTab;
7317        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7318          if( pOrphan==0 ){
7319            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7320          }
7321          pTab2 = pOrphan;
7322          if( pTab2==0 ) break;
7323        }
7324
7325        nField = nField+1;
7326        if( pTab2==pOrphan ){
7327          raw_printf(pState->out,
7328              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7329              pTab2->zQuoted, iRoot, iPgno, nField,
7330              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7331          );
7332        }else{
7333          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7334              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7335          );
7336        }
7337      }
7338      shellReset(&rc, pCells);
7339    }
7340    shellReset(&rc, pPages);
7341    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7342  }
7343  shellFinalize(&rc, pLoop);
7344  shellFinalize(&rc, pPages);
7345  shellFinalize(&rc, pCells);
7346  recoverFreeTable(pOrphan);
7347
7348  /* The rest of the schema */
7349  if( rc==SQLITE_OK ){
7350    sqlite3_stmt *pStmt = 0;
7351    shellPrepare(pState->db, &rc,
7352        "SELECT sql, name FROM recovery.schema "
7353        "WHERE sql NOT LIKE 'create table%'", &pStmt
7354    );
7355    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7356      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7357      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7358        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7359        char *zPrint = shellMPrintf(&rc,
7360          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7361          zName, zName, zSql
7362        );
7363        raw_printf(pState->out, "%s;\n", zPrint);
7364        sqlite3_free(zPrint);
7365      }else{
7366        raw_printf(pState->out, "%s;\n", zSql);
7367      }
7368    }
7369    shellFinalize(&rc, pStmt);
7370  }
7371
7372  if( rc==SQLITE_OK ){
7373    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7374    raw_printf(pState->out, "COMMIT;\n");
7375  }
7376  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7377  return rc;
7378}
7379#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7380
7381
7382/*
7383** If an input line begins with "." then invoke this routine to
7384** process that line.
7385**
7386** Return 1 on error, 2 to exit, and 0 otherwise.
7387*/
7388static int do_meta_command(char *zLine, ShellState *p){
7389  int h = 1;
7390  int nArg = 0;
7391  int n, c;
7392  int rc = 0;
7393  char *azArg[52];
7394
7395#ifndef SQLITE_OMIT_VIRTUALTABLE
7396  if( p->expert.pExpert ){
7397    expertFinish(p, 1, 0);
7398  }
7399#endif
7400
7401  /* Parse the input line into tokens.
7402  */
7403  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7404    while( IsSpace(zLine[h]) ){ h++; }
7405    if( zLine[h]==0 ) break;
7406    if( zLine[h]=='\'' || zLine[h]=='"' ){
7407      int delim = zLine[h++];
7408      azArg[nArg++] = &zLine[h];
7409      while( zLine[h] && zLine[h]!=delim ){
7410        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7411        h++;
7412      }
7413      if( zLine[h]==delim ){
7414        zLine[h++] = 0;
7415      }
7416      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7417    }else{
7418      azArg[nArg++] = &zLine[h];
7419      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7420      if( zLine[h] ) zLine[h++] = 0;
7421      resolve_backslashes(azArg[nArg-1]);
7422    }
7423  }
7424  azArg[nArg] = 0;
7425
7426  /* Process the input line.
7427  */
7428  if( nArg==0 ) return 0; /* no tokens, no error */
7429  n = strlen30(azArg[0]);
7430  c = azArg[0][0];
7431  clearTempFile(p);
7432
7433#ifndef SQLITE_OMIT_AUTHORIZATION
7434  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7435    if( nArg!=2 ){
7436      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7437      rc = 1;
7438      goto meta_command_exit;
7439    }
7440    open_db(p, 0);
7441    if( booleanValue(azArg[1]) ){
7442      sqlite3_set_authorizer(p->db, shellAuth, p);
7443    }else{
7444      sqlite3_set_authorizer(p->db, 0, 0);
7445    }
7446  }else
7447#endif
7448
7449#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7450  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7451    open_db(p, 0);
7452    rc = arDotCommand(p, 0, azArg, nArg);
7453  }else
7454#endif
7455
7456  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7457   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7458  ){
7459    const char *zDestFile = 0;
7460    const char *zDb = 0;
7461    sqlite3 *pDest;
7462    sqlite3_backup *pBackup;
7463    int j;
7464    int bAsync = 0;
7465    const char *zVfs = 0;
7466    for(j=1; j<nArg; j++){
7467      const char *z = azArg[j];
7468      if( z[0]=='-' ){
7469        if( z[1]=='-' ) z++;
7470        if( strcmp(z, "-append")==0 ){
7471          zVfs = "apndvfs";
7472        }else
7473        if( strcmp(z, "-async")==0 ){
7474          bAsync = 1;
7475        }else
7476        {
7477          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7478          return 1;
7479        }
7480      }else if( zDestFile==0 ){
7481        zDestFile = azArg[j];
7482      }else if( zDb==0 ){
7483        zDb = zDestFile;
7484        zDestFile = azArg[j];
7485      }else{
7486        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7487        return 1;
7488      }
7489    }
7490    if( zDestFile==0 ){
7491      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7492      return 1;
7493    }
7494    if( zDb==0 ) zDb = "main";
7495    rc = sqlite3_open_v2(zDestFile, &pDest,
7496                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7497    if( rc!=SQLITE_OK ){
7498      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7499      close_db(pDest);
7500      return 1;
7501    }
7502    if( bAsync ){
7503      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7504                   0, 0, 0);
7505    }
7506    open_db(p, 0);
7507    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7508    if( pBackup==0 ){
7509      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7510      close_db(pDest);
7511      return 1;
7512    }
7513    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7514    sqlite3_backup_finish(pBackup);
7515    if( rc==SQLITE_DONE ){
7516      rc = 0;
7517    }else{
7518      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7519      rc = 1;
7520    }
7521    close_db(pDest);
7522  }else
7523
7524  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7525    if( nArg==2 ){
7526      bail_on_error = booleanValue(azArg[1]);
7527    }else{
7528      raw_printf(stderr, "Usage: .bail on|off\n");
7529      rc = 1;
7530    }
7531  }else
7532
7533  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7534    if( nArg==2 ){
7535      if( booleanValue(azArg[1]) ){
7536        setBinaryMode(p->out, 1);
7537      }else{
7538        setTextMode(p->out, 1);
7539      }
7540    }else{
7541      raw_printf(stderr, "Usage: .binary on|off\n");
7542      rc = 1;
7543    }
7544  }else
7545
7546  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7547    if( nArg==2 ){
7548#if defined(_WIN32) || defined(WIN32)
7549      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7550      rc = !SetCurrentDirectoryW(z);
7551      sqlite3_free(z);
7552#else
7553      rc = chdir(azArg[1]);
7554#endif
7555      if( rc ){
7556        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7557        rc = 1;
7558      }
7559    }else{
7560      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7561      rc = 1;
7562    }
7563  }else
7564
7565  /* The undocumented ".breakpoint" command causes a call to the no-op
7566  ** routine named test_breakpoint().
7567  */
7568  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7569    test_breakpoint();
7570  }else
7571
7572  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7573    if( nArg==2 ){
7574      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7575    }else{
7576      raw_printf(stderr, "Usage: .changes on|off\n");
7577      rc = 1;
7578    }
7579  }else
7580
7581  /* Cancel output redirection, if it is currently set (by .testcase)
7582  ** Then read the content of the testcase-out.txt file and compare against
7583  ** azArg[1].  If there are differences, report an error and exit.
7584  */
7585  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7586    char *zRes = 0;
7587    output_reset(p);
7588    if( nArg!=2 ){
7589      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7590      rc = 2;
7591    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7592      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7593      rc = 2;
7594    }else if( testcase_glob(azArg[1],zRes)==0 ){
7595      utf8_printf(stderr,
7596                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7597                 p->zTestcase, azArg[1], zRes);
7598      rc = 1;
7599    }else{
7600      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7601      p->nCheck++;
7602    }
7603    sqlite3_free(zRes);
7604  }else
7605
7606  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7607    if( nArg==2 ){
7608      tryToClone(p, azArg[1]);
7609    }else{
7610      raw_printf(stderr, "Usage: .clone FILENAME\n");
7611      rc = 1;
7612    }
7613  }else
7614
7615  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7616    char **azName = 0;
7617    int nName = 0;
7618    sqlite3_stmt *pStmt;
7619    int i;
7620    open_db(p, 0);
7621    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7622    if( rc ){
7623      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7624      rc = 1;
7625    }else{
7626      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7627        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7628        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7629        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7630        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7631        azName[nName*2] = strdup(zSchema);
7632        azName[nName*2+1] = strdup(zFile);
7633        nName++;
7634      }
7635    }
7636    sqlite3_finalize(pStmt);
7637    for(i=0; i<nName; i++){
7638      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7639      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7640      const char *z = azName[i*2+1];
7641      utf8_printf(p->out, "%s: %s %s%s\n",
7642         azName[i*2],
7643         z && z[0] ? z : "\"\"",
7644         bRdonly ? "r/o" : "r/w",
7645         eTxn==SQLITE_TXN_NONE ? "" :
7646            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7647      free(azName[i*2]);
7648      free(azName[i*2+1]);
7649    }
7650    sqlite3_free(azName);
7651  }else
7652
7653  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7654    static const struct DbConfigChoices {
7655      const char *zName;
7656      int op;
7657    } aDbConfig[] = {
7658        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7659        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7660        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7661        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7662        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7663        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7664        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7665        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7666        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7667        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7668        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7669        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7670        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7671        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7672        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7673        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7674    };
7675    int ii, v;
7676    open_db(p, 0);
7677    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7678      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7679      if( nArg>=3 ){
7680        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7681      }
7682      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7683      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7684      if( nArg>1 ) break;
7685    }
7686    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7687      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7688      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7689    }
7690  }else
7691
7692  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7693    rc = shell_dbinfo_command(p, nArg, azArg);
7694  }else
7695
7696#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7697  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7698    open_db(p, 0);
7699    rc = recoverDatabaseCmd(p, nArg, azArg);
7700  }else
7701#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7702
7703  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7704    char *zLike = 0;
7705    char *zSql;
7706    int i;
7707    int savedShowHeader = p->showHeader;
7708    int savedShellFlags = p->shellFlgs;
7709    ShellClearFlag(p,
7710       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7711       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7712    for(i=1; i<nArg; i++){
7713      if( azArg[i][0]=='-' ){
7714        const char *z = azArg[i]+1;
7715        if( z[0]=='-' ) z++;
7716        if( strcmp(z,"preserve-rowids")==0 ){
7717#ifdef SQLITE_OMIT_VIRTUALTABLE
7718          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7719                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7720          rc = 1;
7721          sqlite3_free(zLike);
7722          goto meta_command_exit;
7723#else
7724          ShellSetFlag(p, SHFLG_PreserveRowid);
7725#endif
7726        }else
7727        if( strcmp(z,"newlines")==0 ){
7728          ShellSetFlag(p, SHFLG_Newlines);
7729        }else
7730        if( strcmp(z,"data-only")==0 ){
7731          ShellSetFlag(p, SHFLG_DumpDataOnly);
7732        }else
7733        if( strcmp(z,"nosys")==0 ){
7734          ShellSetFlag(p, SHFLG_DumpNoSys);
7735        }else
7736        {
7737          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7738          rc = 1;
7739          sqlite3_free(zLike);
7740          goto meta_command_exit;
7741        }
7742      }else{
7743        /* azArg[i] contains a LIKE pattern. This ".dump" request should
7744        ** only dump data for tables for which either the table name matches
7745        ** the LIKE pattern, or the table appears to be a shadow table of
7746        ** a virtual table for which the name matches the LIKE pattern.
7747        */
7748        char *zExpr = sqlite3_mprintf(
7749            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
7750            "  SELECT 1 FROM sqlite_schema WHERE "
7751            "    name LIKE %Q ESCAPE '\\' AND"
7752            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
7753            "    substr(o.name, 1, length(name)+1) == (name||'_')"
7754            ")", azArg[i], azArg[i]
7755        );
7756
7757        if( zLike ){
7758          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
7759        }else{
7760          zLike = zExpr;
7761        }
7762      }
7763    }
7764
7765    open_db(p, 0);
7766
7767    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7768      /* When playing back a "dump", the content might appear in an order
7769      ** which causes immediate foreign key constraints to be violated.
7770      ** So disable foreign-key constraint enforcement to prevent problems. */
7771      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7772      raw_printf(p->out, "BEGIN TRANSACTION;\n");
7773    }
7774    p->writableSchema = 0;
7775    p->showHeader = 0;
7776    /* Set writable_schema=ON since doing so forces SQLite to initialize
7777    ** as much of the schema as it can even if the sqlite_schema table is
7778    ** corrupt. */
7779    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7780    p->nErr = 0;
7781    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7782    zSql = sqlite3_mprintf(
7783      "SELECT name, type, sql FROM sqlite_schema AS o "
7784      "WHERE (%s) AND type=='table'"
7785      "  AND sql NOT NULL"
7786      " ORDER BY tbl_name='sqlite_sequence', rowid",
7787      zLike
7788    );
7789    run_schema_dump_query(p,zSql);
7790    sqlite3_free(zSql);
7791    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7792      zSql = sqlite3_mprintf(
7793        "SELECT sql FROM sqlite_schema AS o "
7794        "WHERE (%s) AND sql NOT NULL"
7795        "  AND type IN ('index','trigger','view')",
7796        zLike
7797      );
7798      run_table_dump_query(p, zSql);
7799      sqlite3_free(zSql);
7800    }
7801    sqlite3_free(zLike);
7802    if( p->writableSchema ){
7803      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7804      p->writableSchema = 0;
7805    }
7806    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7807    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7808    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7809      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7810    }
7811    p->showHeader = savedShowHeader;
7812    p->shellFlgs = savedShellFlags;
7813  }else
7814
7815  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7816    if( nArg==2 ){
7817      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7818    }else{
7819      raw_printf(stderr, "Usage: .echo on|off\n");
7820      rc = 1;
7821    }
7822  }else
7823
7824  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7825    if( nArg==2 ){
7826      p->autoEQPtest = 0;
7827      if( p->autoEQPtrace ){
7828        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7829        p->autoEQPtrace = 0;
7830      }
7831      if( strcmp(azArg[1],"full")==0 ){
7832        p->autoEQP = AUTOEQP_full;
7833      }else if( strcmp(azArg[1],"trigger")==0 ){
7834        p->autoEQP = AUTOEQP_trigger;
7835#ifdef SQLITE_DEBUG
7836      }else if( strcmp(azArg[1],"test")==0 ){
7837        p->autoEQP = AUTOEQP_on;
7838        p->autoEQPtest = 1;
7839      }else if( strcmp(azArg[1],"trace")==0 ){
7840        p->autoEQP = AUTOEQP_full;
7841        p->autoEQPtrace = 1;
7842        open_db(p, 0);
7843        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
7844        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7845#endif
7846      }else{
7847        p->autoEQP = (u8)booleanValue(azArg[1]);
7848      }
7849    }else{
7850      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7851      rc = 1;
7852    }
7853  }else
7854
7855  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7856    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7857    rc = 2;
7858  }else
7859
7860  /* The ".explain" command is automatic now.  It is largely pointless.  It
7861  ** retained purely for backwards compatibility */
7862  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7863    int val = 1;
7864    if( nArg>=2 ){
7865      if( strcmp(azArg[1],"auto")==0 ){
7866        val = 99;
7867      }else{
7868        val =  booleanValue(azArg[1]);
7869      }
7870    }
7871    if( val==1 && p->mode!=MODE_Explain ){
7872      p->normalMode = p->mode;
7873      p->mode = MODE_Explain;
7874      p->autoExplain = 0;
7875    }else if( val==0 ){
7876      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7877      p->autoExplain = 0;
7878    }else if( val==99 ){
7879      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7880      p->autoExplain = 1;
7881    }
7882  }else
7883
7884#ifndef SQLITE_OMIT_VIRTUALTABLE
7885  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7886    open_db(p, 0);
7887    expertDotCommand(p, azArg, nArg);
7888  }else
7889#endif
7890
7891  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7892    static const struct {
7893       const char *zCtrlName;   /* Name of a test-control option */
7894       int ctrlCode;            /* Integer code for that option */
7895       const char *zUsage;      /* Usage notes */
7896    } aCtrl[] = {
7897      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7898      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
7899      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7900      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7901      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7902   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7903      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7904      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
7905      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7906      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7907   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7908    };
7909    int filectrl = -1;
7910    int iCtrl = -1;
7911    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7912    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7913    int n2, i;
7914    const char *zCmd = 0;
7915    const char *zSchema = 0;
7916
7917    open_db(p, 0);
7918    zCmd = nArg>=2 ? azArg[1] : "help";
7919
7920    if( zCmd[0]=='-'
7921     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7922     && nArg>=4
7923    ){
7924      zSchema = azArg[2];
7925      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7926      nArg -= 2;
7927      zCmd = azArg[1];
7928    }
7929
7930    /* The argument can optionally begin with "-" or "--" */
7931    if( zCmd[0]=='-' && zCmd[1] ){
7932      zCmd++;
7933      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7934    }
7935
7936    /* --help lists all file-controls */
7937    if( strcmp(zCmd,"help")==0 ){
7938      utf8_printf(p->out, "Available file-controls:\n");
7939      for(i=0; i<ArraySize(aCtrl); i++){
7940        utf8_printf(p->out, "  .filectrl %s %s\n",
7941                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7942      }
7943      rc = 1;
7944      goto meta_command_exit;
7945    }
7946
7947    /* convert filectrl text option to value. allow any unique prefix
7948    ** of the option name, or a numerical value. */
7949    n2 = strlen30(zCmd);
7950    for(i=0; i<ArraySize(aCtrl); i++){
7951      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7952        if( filectrl<0 ){
7953          filectrl = aCtrl[i].ctrlCode;
7954          iCtrl = i;
7955        }else{
7956          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7957                              "Use \".filectrl --help\" for help\n", zCmd);
7958          rc = 1;
7959          goto meta_command_exit;
7960        }
7961      }
7962    }
7963    if( filectrl<0 ){
7964      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7965                         "Use \".filectrl --help\" for help\n", zCmd);
7966    }else{
7967      switch(filectrl){
7968        case SQLITE_FCNTL_SIZE_LIMIT: {
7969          if( nArg!=2 && nArg!=3 ) break;
7970          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7971          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7972          isOk = 1;
7973          break;
7974        }
7975        case SQLITE_FCNTL_LOCK_TIMEOUT:
7976        case SQLITE_FCNTL_CHUNK_SIZE: {
7977          int x;
7978          if( nArg!=3 ) break;
7979          x = (int)integerValue(azArg[2]);
7980          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7981          isOk = 2;
7982          break;
7983        }
7984        case SQLITE_FCNTL_PERSIST_WAL:
7985        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7986          int x;
7987          if( nArg!=2 && nArg!=3 ) break;
7988          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7989          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7990          iRes = x;
7991          isOk = 1;
7992          break;
7993        }
7994        case SQLITE_FCNTL_DATA_VERSION:
7995        case SQLITE_FCNTL_HAS_MOVED: {
7996          int x;
7997          if( nArg!=2 ) break;
7998          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7999          iRes = x;
8000          isOk = 1;
8001          break;
8002        }
8003        case SQLITE_FCNTL_TEMPFILENAME: {
8004          char *z = 0;
8005          if( nArg!=2 ) break;
8006          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8007          if( z ){
8008            utf8_printf(p->out, "%s\n", z);
8009            sqlite3_free(z);
8010          }
8011          isOk = 2;
8012          break;
8013        }
8014        case SQLITE_FCNTL_RESERVE_BYTES: {
8015          int x;
8016          if( nArg>=3 ){
8017            x = atoi(azArg[2]);
8018            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8019          }
8020          x = -1;
8021          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8022          utf8_printf(p->out,"%d\n", x);
8023          isOk = 2;
8024          break;
8025        }
8026      }
8027    }
8028    if( isOk==0 && iCtrl>=0 ){
8029      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8030      rc = 1;
8031    }else if( isOk==1 ){
8032      char zBuf[100];
8033      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8034      raw_printf(p->out, "%s\n", zBuf);
8035    }
8036  }else
8037
8038  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8039    ShellState data;
8040    int doStats = 0;
8041    memcpy(&data, p, sizeof(data));
8042    data.showHeader = 0;
8043    data.cMode = data.mode = MODE_Semi;
8044    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8045      data.cMode = data.mode = MODE_Pretty;
8046      nArg = 1;
8047    }
8048    if( nArg!=1 ){
8049      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8050      rc = 1;
8051      goto meta_command_exit;
8052    }
8053    open_db(p, 0);
8054    rc = sqlite3_exec(p->db,
8055       "SELECT sql FROM"
8056       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8057       "     FROM sqlite_schema UNION ALL"
8058       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8059       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8060       "ORDER BY rowid",
8061       callback, &data, 0
8062    );
8063    if( rc==SQLITE_OK ){
8064      sqlite3_stmt *pStmt;
8065      rc = sqlite3_prepare_v2(p->db,
8066               "SELECT rowid FROM sqlite_schema"
8067               " WHERE name GLOB 'sqlite_stat[134]'",
8068               -1, &pStmt, 0);
8069      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8070      sqlite3_finalize(pStmt);
8071    }
8072    if( doStats==0 ){
8073      raw_printf(p->out, "/* No STAT tables available */\n");
8074    }else{
8075      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8076      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
8077                   callback, &data, 0);
8078      data.cMode = data.mode = MODE_Insert;
8079      data.zDestTable = "sqlite_stat1";
8080      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8081      data.zDestTable = "sqlite_stat4";
8082      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8083      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8084    }
8085  }else
8086
8087  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8088    if( nArg==2 ){
8089      p->showHeader = booleanValue(azArg[1]);
8090      p->shellFlgs |= SHFLG_HeaderSet;
8091    }else{
8092      raw_printf(stderr, "Usage: .headers on|off\n");
8093      rc = 1;
8094    }
8095  }else
8096
8097  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8098    if( nArg>=2 ){
8099      n = showHelp(p->out, azArg[1]);
8100      if( n==0 ){
8101        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8102      }
8103    }else{
8104      showHelp(p->out, 0);
8105    }
8106  }else
8107
8108  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8109    char *zTable = 0;           /* Insert data into this table */
8110    char *zFile = 0;            /* Name of file to extra content from */
8111    sqlite3_stmt *pStmt = NULL; /* A statement */
8112    int nCol;                   /* Number of columns in the table */
8113    int nByte;                  /* Number of bytes in an SQL string */
8114    int i, j;                   /* Loop counters */
8115    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8116    int nSep;                   /* Number of bytes in p->colSeparator[] */
8117    char *zSql;                 /* An SQL statement */
8118    ImportCtx sCtx;             /* Reader context */
8119    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8120    int eVerbose = 0;           /* Larger for more console output */
8121    int nSkip = 0;              /* Initial lines to skip */
8122    int useOutputMode = 1;      /* Use output mode to determine separators */
8123
8124    memset(&sCtx, 0, sizeof(sCtx));
8125    if( p->mode==MODE_Ascii ){
8126      xRead = ascii_read_one_field;
8127    }else{
8128      xRead = csv_read_one_field;
8129    }
8130    for(i=1; i<nArg; i++){
8131      char *z = azArg[i];
8132      if( z[0]=='-' && z[1]=='-' ) z++;
8133      if( z[0]!='-' ){
8134        if( zFile==0 ){
8135          zFile = z;
8136        }else if( zTable==0 ){
8137          zTable = z;
8138        }else{
8139          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8140          showHelp(p->out, "import");
8141          rc = 1;
8142          goto meta_command_exit;
8143        }
8144      }else if( strcmp(z,"-v")==0 ){
8145        eVerbose++;
8146      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8147        nSkip = integerValue(azArg[++i]);
8148      }else if( strcmp(z,"-ascii")==0 ){
8149        sCtx.cColSep = SEP_Unit[0];
8150        sCtx.cRowSep = SEP_Record[0];
8151        xRead = ascii_read_one_field;
8152        useOutputMode = 0;
8153      }else if( strcmp(z,"-csv")==0 ){
8154        sCtx.cColSep = ',';
8155        sCtx.cRowSep = '\n';
8156        xRead = csv_read_one_field;
8157        useOutputMode = 0;
8158      }else{
8159        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8160        showHelp(p->out, "import");
8161        rc = 1;
8162        goto meta_command_exit;
8163      }
8164    }
8165    if( zTable==0 ){
8166      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8167                  zFile==0 ? "FILE" : "TABLE");
8168      showHelp(p->out, "import");
8169      rc = 1;
8170      goto meta_command_exit;
8171    }
8172    seenInterrupt = 0;
8173    open_db(p, 0);
8174    if( useOutputMode ){
8175      /* If neither the --csv or --ascii options are specified, then set
8176      ** the column and row separator characters from the output mode. */
8177      nSep = strlen30(p->colSeparator);
8178      if( nSep==0 ){
8179        raw_printf(stderr,
8180                   "Error: non-null column separator required for import\n");
8181        rc = 1;
8182        goto meta_command_exit;
8183      }
8184      if( nSep>1 ){
8185        raw_printf(stderr,
8186              "Error: multi-character column separators not allowed"
8187              " for import\n");
8188        rc = 1;
8189        goto meta_command_exit;
8190      }
8191      nSep = strlen30(p->rowSeparator);
8192      if( nSep==0 ){
8193        raw_printf(stderr,
8194            "Error: non-null row separator required for import\n");
8195        rc = 1;
8196        goto meta_command_exit;
8197      }
8198      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8199        /* When importing CSV (only), if the row separator is set to the
8200        ** default output row separator, change it to the default input
8201        ** row separator.  This avoids having to maintain different input
8202        ** and output row separators. */
8203        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8204        nSep = strlen30(p->rowSeparator);
8205      }
8206      if( nSep>1 ){
8207        raw_printf(stderr, "Error: multi-character row separators not allowed"
8208                           " for import\n");
8209        rc = 1;
8210        goto meta_command_exit;
8211      }
8212      sCtx.cColSep = p->colSeparator[0];
8213      sCtx.cRowSep = p->rowSeparator[0];
8214    }
8215    sCtx.zFile = zFile;
8216    sCtx.nLine = 1;
8217    if( sCtx.zFile[0]=='|' ){
8218#ifdef SQLITE_OMIT_POPEN
8219      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8220      rc = 1;
8221      goto meta_command_exit;
8222#else
8223      sCtx.in = popen(sCtx.zFile+1, "r");
8224      sCtx.zFile = "<pipe>";
8225      sCtx.xCloser = pclose;
8226#endif
8227    }else{
8228      sCtx.in = fopen(sCtx.zFile, "rb");
8229      sCtx.xCloser = fclose;
8230    }
8231    if( sCtx.in==0 ){
8232      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8233      rc = 1;
8234      goto meta_command_exit;
8235    }
8236    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8237      char zSep[2];
8238      zSep[1] = 0;
8239      zSep[0] = sCtx.cColSep;
8240      utf8_printf(p->out, "Column separator ");
8241      output_c_string(p->out, zSep);
8242      utf8_printf(p->out, ", row separator ");
8243      zSep[0] = sCtx.cRowSep;
8244      output_c_string(p->out, zSep);
8245      utf8_printf(p->out, "\n");
8246    }
8247    while( (nSkip--)>0 ){
8248      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8249    }
8250    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
8251    if( zSql==0 ){
8252      import_cleanup(&sCtx);
8253      shell_out_of_memory();
8254    }
8255    nByte = strlen30(zSql);
8256    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8257    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8258    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8259      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
8260      char cSep = '(';
8261      while( xRead(&sCtx) ){
8262        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8263        cSep = ',';
8264        if( sCtx.cTerm!=sCtx.cColSep ) break;
8265      }
8266      if( cSep=='(' ){
8267        sqlite3_free(zCreate);
8268        import_cleanup(&sCtx);
8269        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8270        rc = 1;
8271        goto meta_command_exit;
8272      }
8273      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8274      if( eVerbose>=1 ){
8275        utf8_printf(p->out, "%s\n", zCreate);
8276      }
8277      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8278      sqlite3_free(zCreate);
8279      if( rc ){
8280        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
8281                sqlite3_errmsg(p->db));
8282        import_cleanup(&sCtx);
8283        rc = 1;
8284        goto meta_command_exit;
8285      }
8286      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8287    }
8288    sqlite3_free(zSql);
8289    if( rc ){
8290      if (pStmt) sqlite3_finalize(pStmt);
8291      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8292      import_cleanup(&sCtx);
8293      rc = 1;
8294      goto meta_command_exit;
8295    }
8296    nCol = sqlite3_column_count(pStmt);
8297    sqlite3_finalize(pStmt);
8298    pStmt = 0;
8299    if( nCol==0 ) return 0; /* no columns, no error */
8300    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8301    if( zSql==0 ){
8302      import_cleanup(&sCtx);
8303      shell_out_of_memory();
8304    }
8305    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8306    j = strlen30(zSql);
8307    for(i=1; i<nCol; i++){
8308      zSql[j++] = ',';
8309      zSql[j++] = '?';
8310    }
8311    zSql[j++] = ')';
8312    zSql[j] = 0;
8313    if( eVerbose>=2 ){
8314      utf8_printf(p->out, "Insert using: %s\n", zSql);
8315    }
8316    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8317    sqlite3_free(zSql);
8318    if( rc ){
8319      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8320      if (pStmt) sqlite3_finalize(pStmt);
8321      import_cleanup(&sCtx);
8322      rc = 1;
8323      goto meta_command_exit;
8324    }
8325    needCommit = sqlite3_get_autocommit(p->db);
8326    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8327    do{
8328      int startLine = sCtx.nLine;
8329      for(i=0; i<nCol; i++){
8330        char *z = xRead(&sCtx);
8331        /*
8332        ** Did we reach end-of-file before finding any columns?
8333        ** If so, stop instead of NULL filling the remaining columns.
8334        */
8335        if( z==0 && i==0 ) break;
8336        /*
8337        ** Did we reach end-of-file OR end-of-line before finding any
8338        ** columns in ASCII mode?  If so, stop instead of NULL filling
8339        ** the remaining columns.
8340        */
8341        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8342        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8343        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8344          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8345                          "filling the rest with NULL\n",
8346                          sCtx.zFile, startLine, nCol, i+1);
8347          i += 2;
8348          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8349        }
8350      }
8351      if( sCtx.cTerm==sCtx.cColSep ){
8352        do{
8353          xRead(&sCtx);
8354          i++;
8355        }while( sCtx.cTerm==sCtx.cColSep );
8356        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8357                        "extras ignored\n",
8358                        sCtx.zFile, startLine, nCol, i);
8359      }
8360      if( i>=nCol ){
8361        sqlite3_step(pStmt);
8362        rc = sqlite3_reset(pStmt);
8363        if( rc!=SQLITE_OK ){
8364          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8365                      startLine, sqlite3_errmsg(p->db));
8366          sCtx.nErr++;
8367        }else{
8368          sCtx.nRow++;
8369        }
8370      }
8371    }while( sCtx.cTerm!=EOF );
8372
8373    import_cleanup(&sCtx);
8374    sqlite3_finalize(pStmt);
8375    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8376    if( eVerbose>0 ){
8377      utf8_printf(p->out,
8378          "Added %d rows with %d errors using %d lines of input\n",
8379          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8380    }
8381  }else
8382
8383#ifndef SQLITE_UNTESTABLE
8384  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8385    char *zSql;
8386    char *zCollist = 0;
8387    sqlite3_stmt *pStmt;
8388    int tnum = 0;
8389    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8390    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8391    int i;
8392    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8393      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8394                          "       .imposter off\n");
8395      /* Also allowed, but not documented:
8396      **
8397      **    .imposter TABLE IMPOSTER
8398      **
8399      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8400      ** imposter is another WITHOUT ROWID table with the columns in
8401      ** storage order. */
8402      rc = 1;
8403      goto meta_command_exit;
8404    }
8405    open_db(p, 0);
8406    if( nArg==2 ){
8407      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8408      goto meta_command_exit;
8409    }
8410    zSql = sqlite3_mprintf(
8411      "SELECT rootpage, 0 FROM sqlite_schema"
8412      " WHERE name='%q' AND type='index'"
8413      "UNION ALL "
8414      "SELECT rootpage, 1 FROM sqlite_schema"
8415      " WHERE name='%q' AND type='table'"
8416      "   AND sql LIKE '%%without%%rowid%%'",
8417      azArg[1], azArg[1]
8418    );
8419    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8420    sqlite3_free(zSql);
8421    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8422      tnum = sqlite3_column_int(pStmt, 0);
8423      isWO = sqlite3_column_int(pStmt, 1);
8424    }
8425    sqlite3_finalize(pStmt);
8426    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8427    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8428    sqlite3_free(zSql);
8429    i = 0;
8430    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8431      char zLabel[20];
8432      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8433      i++;
8434      if( zCol==0 ){
8435        if( sqlite3_column_int(pStmt,1)==-1 ){
8436          zCol = "_ROWID_";
8437        }else{
8438          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8439          zCol = zLabel;
8440        }
8441      }
8442      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8443        lenPK = (int)strlen(zCollist);
8444      }
8445      if( zCollist==0 ){
8446        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8447      }else{
8448        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8449      }
8450    }
8451    sqlite3_finalize(pStmt);
8452    if( i==0 || tnum==0 ){
8453      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8454      rc = 1;
8455      sqlite3_free(zCollist);
8456      goto meta_command_exit;
8457    }
8458    if( lenPK==0 ) lenPK = 100000;
8459    zSql = sqlite3_mprintf(
8460          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8461          azArg[2], zCollist, lenPK, zCollist);
8462    sqlite3_free(zCollist);
8463    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8464    if( rc==SQLITE_OK ){
8465      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8466      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8467      if( rc ){
8468        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8469      }else{
8470        utf8_printf(stdout, "%s;\n", zSql);
8471        raw_printf(stdout,
8472          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8473          azArg[1], isWO ? "table" : "index"
8474        );
8475      }
8476    }else{
8477      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8478      rc = 1;
8479    }
8480    sqlite3_free(zSql);
8481  }else
8482#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8483
8484#ifdef SQLITE_ENABLE_IOTRACE
8485  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8486    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8487    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8488    iotrace = 0;
8489    if( nArg<2 ){
8490      sqlite3IoTrace = 0;
8491    }else if( strcmp(azArg[1], "-")==0 ){
8492      sqlite3IoTrace = iotracePrintf;
8493      iotrace = stdout;
8494    }else{
8495      iotrace = fopen(azArg[1], "w");
8496      if( iotrace==0 ){
8497        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8498        sqlite3IoTrace = 0;
8499        rc = 1;
8500      }else{
8501        sqlite3IoTrace = iotracePrintf;
8502      }
8503    }
8504  }else
8505#endif
8506
8507  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8508    static const struct {
8509       const char *zLimitName;   /* Name of a limit */
8510       int limitCode;            /* Integer code for that limit */
8511    } aLimit[] = {
8512      { "length",                SQLITE_LIMIT_LENGTH                    },
8513      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8514      { "column",                SQLITE_LIMIT_COLUMN                    },
8515      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8516      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8517      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8518      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8519      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8520      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8521      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8522      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8523      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8524    };
8525    int i, n2;
8526    open_db(p, 0);
8527    if( nArg==1 ){
8528      for(i=0; i<ArraySize(aLimit); i++){
8529        printf("%20s %d\n", aLimit[i].zLimitName,
8530               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8531      }
8532    }else if( nArg>3 ){
8533      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8534      rc = 1;
8535      goto meta_command_exit;
8536    }else{
8537      int iLimit = -1;
8538      n2 = strlen30(azArg[1]);
8539      for(i=0; i<ArraySize(aLimit); i++){
8540        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8541          if( iLimit<0 ){
8542            iLimit = i;
8543          }else{
8544            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8545            rc = 1;
8546            goto meta_command_exit;
8547          }
8548        }
8549      }
8550      if( iLimit<0 ){
8551        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8552                        "enter \".limits\" with no arguments for a list.\n",
8553                         azArg[1]);
8554        rc = 1;
8555        goto meta_command_exit;
8556      }
8557      if( nArg==3 ){
8558        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8559                      (int)integerValue(azArg[2]));
8560      }
8561      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8562             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8563    }
8564  }else
8565
8566  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8567    open_db(p, 0);
8568    lintDotCommand(p, azArg, nArg);
8569  }else
8570
8571#ifndef SQLITE_OMIT_LOAD_EXTENSION
8572  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8573    const char *zFile, *zProc;
8574    char *zErrMsg = 0;
8575    if( nArg<2 ){
8576      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8577      rc = 1;
8578      goto meta_command_exit;
8579    }
8580    zFile = azArg[1];
8581    zProc = nArg>=3 ? azArg[2] : 0;
8582    open_db(p, 0);
8583    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8584    if( rc!=SQLITE_OK ){
8585      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8586      sqlite3_free(zErrMsg);
8587      rc = 1;
8588    }
8589  }else
8590#endif
8591
8592  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8593    if( nArg!=2 ){
8594      raw_printf(stderr, "Usage: .log FILENAME\n");
8595      rc = 1;
8596    }else{
8597      const char *zFile = azArg[1];
8598      output_file_close(p->pLog);
8599      p->pLog = output_file_open(zFile, 0);
8600    }
8601  }else
8602
8603  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8604    const char *zMode = nArg>=2 ? azArg[1] : "";
8605    int n2 = strlen30(zMode);
8606    int c2 = zMode[0];
8607    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8608      p->mode = MODE_Line;
8609      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8610    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8611      p->mode = MODE_Column;
8612      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8613        p->showHeader = 1;
8614      }
8615      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8616    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8617      p->mode = MODE_List;
8618      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8619      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8620    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8621      p->mode = MODE_Html;
8622    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8623      p->mode = MODE_Tcl;
8624      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8625      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8626    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8627      p->mode = MODE_Csv;
8628      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8629      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8630    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8631      p->mode = MODE_List;
8632      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8633    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8634      p->mode = MODE_Insert;
8635      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8636    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8637      p->mode = MODE_Quote;
8638      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8639      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8640    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8641      p->mode = MODE_Ascii;
8642      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8643      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8644    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8645      p->mode = MODE_Markdown;
8646    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8647      p->mode = MODE_Table;
8648    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8649      p->mode = MODE_Box;
8650    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8651      p->mode = MODE_Json;
8652    }else if( nArg==1 ){
8653      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8654    }else{
8655      raw_printf(stderr, "Error: mode should be one of: "
8656         "ascii box column csv html insert json line list markdown "
8657         "quote table tabs tcl\n");
8658      rc = 1;
8659    }
8660    p->cMode = p->mode;
8661  }else
8662
8663  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8664    if( nArg==2 ){
8665      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8666                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8667    }else{
8668      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8669      rc = 1;
8670    }
8671  }else
8672
8673#ifdef SQLITE_DEBUG
8674  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8675    int i;
8676    for(i=1; i<nArg; i++){
8677      const char *z = azArg[i];
8678      if( z[0]=='-' && z[1]=='-' ) z++;
8679      if( strcmp(z,"-repeat")==0 ){
8680        if( i==nArg-1 ){
8681          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8682          rc = 1;
8683        }else{
8684          oomRepeat = (int)integerValue(azArg[++i]);
8685        }
8686      }else if( IsDigit(z[0]) ){
8687        oomCounter = (int)integerValue(azArg[i]);
8688      }else{
8689        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8690        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8691        rc = 1;
8692      }
8693    }
8694    if( rc==0 ){
8695      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8696      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8697    }
8698  }else
8699#endif /* SQLITE_DEBUG */
8700
8701  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8702    char *zNewFilename = 0;  /* Name of the database file to open */
8703    int iName = 1;           /* Index in azArg[] of the filename */
8704    int newFlag = 0;         /* True to delete file before opening */
8705    /* Close the existing database */
8706    session_close_all(p);
8707    close_db(p->db);
8708    p->db = 0;
8709    p->zDbFilename = 0;
8710    sqlite3_free(p->zFreeOnClose);
8711    p->zFreeOnClose = 0;
8712    p->openMode = SHELL_OPEN_UNSPEC;
8713    p->openFlags = 0;
8714    p->szMax = 0;
8715    /* Check for command-line arguments */
8716    for(iName=1; iName<nArg; iName++){
8717      const char *z = azArg[iName];
8718      if( optionMatch(z,"new") ){
8719        newFlag = 1;
8720#ifdef SQLITE_HAVE_ZLIB
8721      }else if( optionMatch(z, "zip") ){
8722        p->openMode = SHELL_OPEN_ZIPFILE;
8723#endif
8724      }else if( optionMatch(z, "append") ){
8725        p->openMode = SHELL_OPEN_APPENDVFS;
8726      }else if( optionMatch(z, "readonly") ){
8727        p->openMode = SHELL_OPEN_READONLY;
8728      }else if( optionMatch(z, "nofollow") ){
8729        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8730#ifndef SQLITE_OMIT_DESERIALIZE
8731      }else if( optionMatch(z, "deserialize") ){
8732        p->openMode = SHELL_OPEN_DESERIALIZE;
8733      }else if( optionMatch(z, "hexdb") ){
8734        p->openMode = SHELL_OPEN_HEXDB;
8735      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8736        p->szMax = integerValue(azArg[++iName]);
8737#endif /* SQLITE_OMIT_DESERIALIZE */
8738      }else if( z[0]=='-' ){
8739        utf8_printf(stderr, "unknown option: %s\n", z);
8740        rc = 1;
8741        goto meta_command_exit;
8742      }else if( zNewFilename ){
8743        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
8744        rc = 1;
8745        goto meta_command_exit;
8746      }else{
8747        zNewFilename = sqlite3_mprintf("%s", z);
8748      }
8749    }
8750    /* If a filename is specified, try to open it first */
8751    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8752      if( newFlag ) shellDeleteFile(zNewFilename);
8753      p->zDbFilename = zNewFilename;
8754      open_db(p, OPEN_DB_KEEPALIVE);
8755      if( p->db==0 ){
8756        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8757        sqlite3_free(zNewFilename);
8758      }else{
8759        p->zFreeOnClose = zNewFilename;
8760      }
8761    }
8762    if( p->db==0 ){
8763      /* As a fall-back open a TEMP database */
8764      p->zDbFilename = 0;
8765      open_db(p, 0);
8766    }
8767  }else
8768
8769  if( (c=='o'
8770        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8771   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8772  ){
8773    char *zFile = 0;
8774    int bTxtMode = 0;
8775    int i;
8776    int eMode = 0;
8777    int bBOM = 0;
8778    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8779
8780    if( c=='e' ){
8781      eMode = 'x';
8782      bOnce = 2;
8783    }else if( strncmp(azArg[0],"once",n)==0 ){
8784      bOnce = 1;
8785    }
8786    for(i=1; i<nArg; i++){
8787      char *z = azArg[i];
8788      if( z[0]=='-' ){
8789        if( z[1]=='-' ) z++;
8790        if( strcmp(z,"-bom")==0 ){
8791          bBOM = 1;
8792        }else if( c!='e' && strcmp(z,"-x")==0 ){
8793          eMode = 'x';  /* spreadsheet */
8794        }else if( c!='e' && strcmp(z,"-e")==0 ){
8795          eMode = 'e';  /* text editor */
8796        }else{
8797          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8798                      azArg[i]);
8799          showHelp(p->out, azArg[0]);
8800          rc = 1;
8801          goto meta_command_exit;
8802        }
8803      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
8804        zFile = sqlite3_mprintf("%s", z);
8805        if( zFile[0]=='|' ){
8806          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
8807          break;
8808        }
8809      }else{
8810        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8811                    azArg[i]);
8812        showHelp(p->out, azArg[0]);
8813        rc = 1;
8814        sqlite3_free(zFile);
8815        goto meta_command_exit;
8816      }
8817    }
8818    if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
8819    if( bOnce ){
8820      p->outCount = 2;
8821    }else{
8822      p->outCount = 0;
8823    }
8824    output_reset(p);
8825#ifndef SQLITE_NOHAVE_SYSTEM
8826    if( eMode=='e' || eMode=='x' ){
8827      p->doXdgOpen = 1;
8828      outputModePush(p);
8829      if( eMode=='x' ){
8830        /* spreadsheet mode.  Output as CSV. */
8831        newTempFile(p, "csv");
8832        ShellClearFlag(p, SHFLG_Echo);
8833        p->mode = MODE_Csv;
8834        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8835        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8836      }else{
8837        /* text editor mode */
8838        newTempFile(p, "txt");
8839        bTxtMode = 1;
8840      }
8841      sqlite3_free(zFile);
8842      zFile = sqlite3_mprintf("%s", p->zTempFile);
8843    }
8844#endif /* SQLITE_NOHAVE_SYSTEM */
8845    if( zFile[0]=='|' ){
8846#ifdef SQLITE_OMIT_POPEN
8847      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8848      rc = 1;
8849      p->out = stdout;
8850#else
8851      p->out = popen(zFile + 1, "w");
8852      if( p->out==0 ){
8853        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8854        p->out = stdout;
8855        rc = 1;
8856      }else{
8857        if( bBOM ) fprintf(p->out,"\357\273\277");
8858        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8859      }
8860#endif
8861    }else{
8862      p->out = output_file_open(zFile, bTxtMode);
8863      if( p->out==0 ){
8864        if( strcmp(zFile,"off")!=0 ){
8865          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8866        }
8867        p->out = stdout;
8868        rc = 1;
8869      } else {
8870        if( bBOM ) fprintf(p->out,"\357\273\277");
8871        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8872      }
8873    }
8874    sqlite3_free(zFile);
8875  }else
8876
8877  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8878    open_db(p,0);
8879    if( nArg<=1 ) goto parameter_syntax_error;
8880
8881    /* .parameter clear
8882    ** Clear all bind parameters by dropping the TEMP table that holds them.
8883    */
8884    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8885      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8886                   0, 0, 0);
8887    }else
8888
8889    /* .parameter list
8890    ** List all bind parameters.
8891    */
8892    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8893      sqlite3_stmt *pStmt = 0;
8894      int rx;
8895      int len = 0;
8896      rx = sqlite3_prepare_v2(p->db,
8897             "SELECT max(length(key)) "
8898             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8899      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8900        len = sqlite3_column_int(pStmt, 0);
8901        if( len>40 ) len = 40;
8902      }
8903      sqlite3_finalize(pStmt);
8904      pStmt = 0;
8905      if( len ){
8906        rx = sqlite3_prepare_v2(p->db,
8907             "SELECT key, quote(value) "
8908             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8909        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8910          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8911                      sqlite3_column_text(pStmt,1));
8912        }
8913        sqlite3_finalize(pStmt);
8914      }
8915    }else
8916
8917    /* .parameter init
8918    ** Make sure the TEMP table used to hold bind parameters exists.
8919    ** Create it if necessary.
8920    */
8921    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8922      bind_table_init(p);
8923    }else
8924
8925    /* .parameter set NAME VALUE
8926    ** Set or reset a bind parameter.  NAME should be the full parameter
8927    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8928    ** VALUE can be in either SQL literal notation, or if not it will be
8929    ** understood to be a text string.
8930    */
8931    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8932      int rx;
8933      char *zSql;
8934      sqlite3_stmt *pStmt;
8935      const char *zKey = azArg[2];
8936      const char *zValue = azArg[3];
8937      bind_table_init(p);
8938      zSql = sqlite3_mprintf(
8939                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8940                  "VALUES(%Q,%s);", zKey, zValue);
8941      if( zSql==0 ) shell_out_of_memory();
8942      pStmt = 0;
8943      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8944      sqlite3_free(zSql);
8945      if( rx!=SQLITE_OK ){
8946        sqlite3_finalize(pStmt);
8947        pStmt = 0;
8948        zSql = sqlite3_mprintf(
8949                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8950                   "VALUES(%Q,%Q);", zKey, zValue);
8951        if( zSql==0 ) shell_out_of_memory();
8952        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8953        sqlite3_free(zSql);
8954        if( rx!=SQLITE_OK ){
8955          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8956          sqlite3_finalize(pStmt);
8957          pStmt = 0;
8958          rc = 1;
8959        }
8960      }
8961      sqlite3_step(pStmt);
8962      sqlite3_finalize(pStmt);
8963    }else
8964
8965    /* .parameter unset NAME
8966    ** Remove the NAME binding from the parameter binding table, if it
8967    ** exists.
8968    */
8969    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8970      char *zSql = sqlite3_mprintf(
8971          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8972      if( zSql==0 ) shell_out_of_memory();
8973      sqlite3_exec(p->db, zSql, 0, 0, 0);
8974      sqlite3_free(zSql);
8975    }else
8976    /* If no command name matches, show a syntax error */
8977    parameter_syntax_error:
8978    showHelp(p->out, "parameter");
8979  }else
8980
8981  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8982    int i;
8983    for(i=1; i<nArg; i++){
8984      if( i>1 ) raw_printf(p->out, " ");
8985      utf8_printf(p->out, "%s", azArg[i]);
8986    }
8987    raw_printf(p->out, "\n");
8988  }else
8989
8990#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8991  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8992    int i;
8993    int nn = 0;
8994    p->flgProgress = 0;
8995    p->mxProgress = 0;
8996    p->nProgress = 0;
8997    for(i=1; i<nArg; i++){
8998      const char *z = azArg[i];
8999      if( z[0]=='-' ){
9000        z++;
9001        if( z[0]=='-' ) z++;
9002        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
9003          p->flgProgress |= SHELL_PROGRESS_QUIET;
9004          continue;
9005        }
9006        if( strcmp(z,"reset")==0 ){
9007          p->flgProgress |= SHELL_PROGRESS_RESET;
9008          continue;
9009        }
9010        if( strcmp(z,"once")==0 ){
9011          p->flgProgress |= SHELL_PROGRESS_ONCE;
9012          continue;
9013        }
9014        if( strcmp(z,"limit")==0 ){
9015          if( i+1>=nArg ){
9016            utf8_printf(stderr, "Error: missing argument on --limit\n");
9017            rc = 1;
9018            goto meta_command_exit;
9019          }else{
9020            p->mxProgress = (int)integerValue(azArg[++i]);
9021          }
9022          continue;
9023        }
9024        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9025        rc = 1;
9026        goto meta_command_exit;
9027      }else{
9028        nn = (int)integerValue(z);
9029      }
9030    }
9031    open_db(p, 0);
9032    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9033  }else
9034#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9035
9036  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9037    if( nArg >= 2) {
9038      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9039    }
9040    if( nArg >= 3) {
9041      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9042    }
9043  }else
9044
9045  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
9046    rc = 2;
9047  }else
9048
9049  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
9050    FILE *inSaved = p->in;
9051    int savedLineno = p->lineno;
9052    if( nArg!=2 ){
9053      raw_printf(stderr, "Usage: .read FILE\n");
9054      rc = 1;
9055      goto meta_command_exit;
9056    }
9057    if( azArg[1][0]=='|' ){
9058#ifdef SQLITE_OMIT_POPEN
9059      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9060      rc = 1;
9061      p->out = stdout;
9062#else
9063      p->in = popen(azArg[1]+1, "r");
9064      if( p->in==0 ){
9065        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9066        rc = 1;
9067      }else{
9068        rc = process_input(p);
9069        pclose(p->in);
9070      }
9071#endif
9072    }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
9073      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9074      rc = 1;
9075    }else{
9076      rc = process_input(p);
9077      fclose(p->in);
9078    }
9079    p->in = inSaved;
9080    p->lineno = savedLineno;
9081  }else
9082
9083  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9084    const char *zSrcFile;
9085    const char *zDb;
9086    sqlite3 *pSrc;
9087    sqlite3_backup *pBackup;
9088    int nTimeout = 0;
9089
9090    if( nArg==2 ){
9091      zSrcFile = azArg[1];
9092      zDb = "main";
9093    }else if( nArg==3 ){
9094      zSrcFile = azArg[2];
9095      zDb = azArg[1];
9096    }else{
9097      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9098      rc = 1;
9099      goto meta_command_exit;
9100    }
9101    rc = sqlite3_open(zSrcFile, &pSrc);
9102    if( rc!=SQLITE_OK ){
9103      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9104      close_db(pSrc);
9105      return 1;
9106    }
9107    open_db(p, 0);
9108    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9109    if( pBackup==0 ){
9110      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9111      close_db(pSrc);
9112      return 1;
9113    }
9114    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9115          || rc==SQLITE_BUSY  ){
9116      if( rc==SQLITE_BUSY ){
9117        if( nTimeout++ >= 3 ) break;
9118        sqlite3_sleep(100);
9119      }
9120    }
9121    sqlite3_backup_finish(pBackup);
9122    if( rc==SQLITE_DONE ){
9123      rc = 0;
9124    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9125      raw_printf(stderr, "Error: source database is busy\n");
9126      rc = 1;
9127    }else{
9128      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9129      rc = 1;
9130    }
9131    close_db(pSrc);
9132  }else
9133
9134  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9135    if( nArg==2 ){
9136      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9137#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9138      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9139#endif
9140    }else{
9141      raw_printf(stderr, "Usage: .scanstats on|off\n");
9142      rc = 1;
9143    }
9144  }else
9145
9146  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9147    ShellText sSelect;
9148    ShellState data;
9149    char *zErrMsg = 0;
9150    const char *zDiv = "(";
9151    const char *zName = 0;
9152    int iSchema = 0;
9153    int bDebug = 0;
9154    int bNoSystemTabs = 0;
9155    int ii;
9156
9157    open_db(p, 0);
9158    memcpy(&data, p, sizeof(data));
9159    data.showHeader = 0;
9160    data.cMode = data.mode = MODE_Semi;
9161    initText(&sSelect);
9162    for(ii=1; ii<nArg; ii++){
9163      if( optionMatch(azArg[ii],"indent") ){
9164        data.cMode = data.mode = MODE_Pretty;
9165      }else if( optionMatch(azArg[ii],"debug") ){
9166        bDebug = 1;
9167      }else if( optionMatch(azArg[ii],"nosys") ){
9168        bNoSystemTabs = 1;
9169      }else if( azArg[ii][0]=='-' ){
9170        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9171        rc = 1;
9172        goto meta_command_exit;
9173      }else if( zName==0 ){
9174        zName = azArg[ii];
9175      }else{
9176        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9177        rc = 1;
9178        goto meta_command_exit;
9179      }
9180    }
9181    if( zName!=0 ){
9182      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9183                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9184                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9185                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9186      if( isSchema ){
9187        char *new_argv[2], *new_colv[2];
9188        new_argv[0] = sqlite3_mprintf(
9189                      "CREATE TABLE %s (\n"
9190                      "  type text,\n"
9191                      "  name text,\n"
9192                      "  tbl_name text,\n"
9193                      "  rootpage integer,\n"
9194                      "  sql text\n"
9195                      ")", zName);
9196        new_argv[1] = 0;
9197        new_colv[0] = "sql";
9198        new_colv[1] = 0;
9199        callback(&data, 1, new_argv, new_colv);
9200        sqlite3_free(new_argv[0]);
9201      }
9202    }
9203    if( zDiv ){
9204      sqlite3_stmt *pStmt = 0;
9205      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9206                              -1, &pStmt, 0);
9207      if( rc ){
9208        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9209        sqlite3_finalize(pStmt);
9210        rc = 1;
9211        goto meta_command_exit;
9212      }
9213      appendText(&sSelect, "SELECT sql FROM", 0);
9214      iSchema = 0;
9215      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9216        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9217        char zScNum[30];
9218        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9219        appendText(&sSelect, zDiv, 0);
9220        zDiv = " UNION ALL ";
9221        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9222        if( sqlite3_stricmp(zDb, "main")!=0 ){
9223          appendText(&sSelect, zDb, '\'');
9224        }else{
9225          appendText(&sSelect, "NULL", 0);
9226        }
9227        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9228        appendText(&sSelect, zScNum, 0);
9229        appendText(&sSelect, " AS snum, ", 0);
9230        appendText(&sSelect, zDb, '\'');
9231        appendText(&sSelect, " AS sname FROM ", 0);
9232        appendText(&sSelect, zDb, quoteChar(zDb));
9233        appendText(&sSelect, ".sqlite_schema", 0);
9234      }
9235      sqlite3_finalize(pStmt);
9236#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9237      if( zName ){
9238        appendText(&sSelect,
9239           " UNION ALL SELECT shell_module_schema(name),"
9240           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9241        0);
9242      }
9243#endif
9244      appendText(&sSelect, ") WHERE ", 0);
9245      if( zName ){
9246        char *zQarg = sqlite3_mprintf("%Q", zName);
9247        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9248                    strchr(zName, '[') != 0;
9249        if( strchr(zName, '.') ){
9250          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9251        }else{
9252          appendText(&sSelect, "lower(tbl_name)", 0);
9253        }
9254        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9255        appendText(&sSelect, zQarg, 0);
9256        if( !bGlob ){
9257          appendText(&sSelect, " ESCAPE '\\' ", 0);
9258        }
9259        appendText(&sSelect, " AND ", 0);
9260        sqlite3_free(zQarg);
9261      }
9262      if( bNoSystemTabs ){
9263        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9264      }
9265      appendText(&sSelect, "sql IS NOT NULL"
9266                           " ORDER BY snum, rowid", 0);
9267      if( bDebug ){
9268        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9269      }else{
9270        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9271      }
9272      freeText(&sSelect);
9273    }
9274    if( zErrMsg ){
9275      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9276      sqlite3_free(zErrMsg);
9277      rc = 1;
9278    }else if( rc != SQLITE_OK ){
9279      raw_printf(stderr,"Error: querying schema information\n");
9280      rc = 1;
9281    }else{
9282      rc = 0;
9283    }
9284  }else
9285
9286  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9287    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
9288    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
9289  }else
9290
9291#if defined(SQLITE_ENABLE_SESSION)
9292  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9293    OpenSession *pSession = &p->aSession[0];
9294    char **azCmd = &azArg[1];
9295    int iSes = 0;
9296    int nCmd = nArg - 1;
9297    int i;
9298    if( nArg<=1 ) goto session_syntax_error;
9299    open_db(p, 0);
9300    if( nArg>=3 ){
9301      for(iSes=0; iSes<p->nSession; iSes++){
9302        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
9303      }
9304      if( iSes<p->nSession ){
9305        pSession = &p->aSession[iSes];
9306        azCmd++;
9307        nCmd--;
9308      }else{
9309        pSession = &p->aSession[0];
9310        iSes = 0;
9311      }
9312    }
9313
9314    /* .session attach TABLE
9315    ** Invoke the sqlite3session_attach() interface to attach a particular
9316    ** table so that it is never filtered.
9317    */
9318    if( strcmp(azCmd[0],"attach")==0 ){
9319      if( nCmd!=2 ) goto session_syntax_error;
9320      if( pSession->p==0 ){
9321        session_not_open:
9322        raw_printf(stderr, "ERROR: No sessions are open\n");
9323      }else{
9324        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9325        if( rc ){
9326          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9327          rc = 0;
9328        }
9329      }
9330    }else
9331
9332    /* .session changeset FILE
9333    ** .session patchset FILE
9334    ** Write a changeset or patchset into a file.  The file is overwritten.
9335    */
9336    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9337      FILE *out = 0;
9338      if( nCmd!=2 ) goto session_syntax_error;
9339      if( pSession->p==0 ) goto session_not_open;
9340      out = fopen(azCmd[1], "wb");
9341      if( out==0 ){
9342        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9343                    azCmd[1]);
9344      }else{
9345        int szChng;
9346        void *pChng;
9347        if( azCmd[0][0]=='c' ){
9348          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9349        }else{
9350          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9351        }
9352        if( rc ){
9353          printf("Error: error code %d\n", rc);
9354          rc = 0;
9355        }
9356        if( pChng
9357          && fwrite(pChng, szChng, 1, out)!=1 ){
9358          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9359                  szChng);
9360        }
9361        sqlite3_free(pChng);
9362        fclose(out);
9363      }
9364    }else
9365
9366    /* .session close
9367    ** Close the identified session
9368    */
9369    if( strcmp(azCmd[0], "close")==0 ){
9370      if( nCmd!=1 ) goto session_syntax_error;
9371      if( p->nSession ){
9372        session_close(pSession);
9373        p->aSession[iSes] = p->aSession[--p->nSession];
9374      }
9375    }else
9376
9377    /* .session enable ?BOOLEAN?
9378    ** Query or set the enable flag
9379    */
9380    if( strcmp(azCmd[0], "enable")==0 ){
9381      int ii;
9382      if( nCmd>2 ) goto session_syntax_error;
9383      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9384      if( p->nSession ){
9385        ii = sqlite3session_enable(pSession->p, ii);
9386        utf8_printf(p->out, "session %s enable flag = %d\n",
9387                    pSession->zName, ii);
9388      }
9389    }else
9390
9391    /* .session filter GLOB ....
9392    ** Set a list of GLOB patterns of table names to be excluded.
9393    */
9394    if( strcmp(azCmd[0], "filter")==0 ){
9395      int ii, nByte;
9396      if( nCmd<2 ) goto session_syntax_error;
9397      if( p->nSession ){
9398        for(ii=0; ii<pSession->nFilter; ii++){
9399          sqlite3_free(pSession->azFilter[ii]);
9400        }
9401        sqlite3_free(pSession->azFilter);
9402        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9403        pSession->azFilter = sqlite3_malloc( nByte );
9404        if( pSession->azFilter==0 ){
9405          raw_printf(stderr, "Error: out or memory\n");
9406          exit(1);
9407        }
9408        for(ii=1; ii<nCmd; ii++){
9409          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9410        }
9411        pSession->nFilter = ii-1;
9412      }
9413    }else
9414
9415    /* .session indirect ?BOOLEAN?
9416    ** Query or set the indirect flag
9417    */
9418    if( strcmp(azCmd[0], "indirect")==0 ){
9419      int ii;
9420      if( nCmd>2 ) goto session_syntax_error;
9421      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9422      if( p->nSession ){
9423        ii = sqlite3session_indirect(pSession->p, ii);
9424        utf8_printf(p->out, "session %s indirect flag = %d\n",
9425                    pSession->zName, ii);
9426      }
9427    }else
9428
9429    /* .session isempty
9430    ** Determine if the session is empty
9431    */
9432    if( strcmp(azCmd[0], "isempty")==0 ){
9433      int ii;
9434      if( nCmd!=1 ) goto session_syntax_error;
9435      if( p->nSession ){
9436        ii = sqlite3session_isempty(pSession->p);
9437        utf8_printf(p->out, "session %s isempty flag = %d\n",
9438                    pSession->zName, ii);
9439      }
9440    }else
9441
9442    /* .session list
9443    ** List all currently open sessions
9444    */
9445    if( strcmp(azCmd[0],"list")==0 ){
9446      for(i=0; i<p->nSession; i++){
9447        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
9448      }
9449    }else
9450
9451    /* .session open DB NAME
9452    ** Open a new session called NAME on the attached database DB.
9453    ** DB is normally "main".
9454    */
9455    if( strcmp(azCmd[0],"open")==0 ){
9456      char *zName;
9457      if( nCmd!=3 ) goto session_syntax_error;
9458      zName = azCmd[2];
9459      if( zName[0]==0 ) goto session_syntax_error;
9460      for(i=0; i<p->nSession; i++){
9461        if( strcmp(p->aSession[i].zName,zName)==0 ){
9462          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9463          goto meta_command_exit;
9464        }
9465      }
9466      if( p->nSession>=ArraySize(p->aSession) ){
9467        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9468        goto meta_command_exit;
9469      }
9470      pSession = &p->aSession[p->nSession];
9471      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9472      if( rc ){
9473        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9474        rc = 0;
9475        goto meta_command_exit;
9476      }
9477      pSession->nFilter = 0;
9478      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9479      p->nSession++;
9480      pSession->zName = sqlite3_mprintf("%s", zName);
9481    }else
9482    /* If no command name matches, show a syntax error */
9483    session_syntax_error:
9484    showHelp(p->out, "session");
9485  }else
9486#endif
9487
9488#ifdef SQLITE_DEBUG
9489  /* Undocumented commands for internal testing.  Subject to change
9490  ** without notice. */
9491  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9492    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9493      int i, v;
9494      for(i=1; i<nArg; i++){
9495        v = booleanValue(azArg[i]);
9496        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9497      }
9498    }
9499    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9500      int i; sqlite3_int64 v;
9501      for(i=1; i<nArg; i++){
9502        char zBuf[200];
9503        v = integerValue(azArg[i]);
9504        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9505        utf8_printf(p->out, "%s", zBuf);
9506      }
9507    }
9508  }else
9509#endif
9510
9511  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9512    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9513    int bVerbose = 0;        /* Verbose output */
9514    int bSelftestExists;     /* True if SELFTEST already exists */
9515    int i, k;                /* Loop counters */
9516    int nTest = 0;           /* Number of tests runs */
9517    int nErr = 0;            /* Number of errors seen */
9518    ShellText str;           /* Answer for a query */
9519    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9520
9521    open_db(p,0);
9522    for(i=1; i<nArg; i++){
9523      const char *z = azArg[i];
9524      if( z[0]=='-' && z[1]=='-' ) z++;
9525      if( strcmp(z,"-init")==0 ){
9526        bIsInit = 1;
9527      }else
9528      if( strcmp(z,"-v")==0 ){
9529        bVerbose++;
9530      }else
9531      {
9532        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9533                    azArg[i], azArg[0]);
9534        raw_printf(stderr, "Should be one of: --init -v\n");
9535        rc = 1;
9536        goto meta_command_exit;
9537      }
9538    }
9539    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9540           != SQLITE_OK ){
9541      bSelftestExists = 0;
9542    }else{
9543      bSelftestExists = 1;
9544    }
9545    if( bIsInit ){
9546      createSelftestTable(p);
9547      bSelftestExists = 1;
9548    }
9549    initText(&str);
9550    appendText(&str, "x", 0);
9551    for(k=bSelftestExists; k>=0; k--){
9552      if( k==1 ){
9553        rc = sqlite3_prepare_v2(p->db,
9554            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9555            -1, &pStmt, 0);
9556      }else{
9557        rc = sqlite3_prepare_v2(p->db,
9558          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9559          "      (1,'run','PRAGMA integrity_check','ok')",
9560          -1, &pStmt, 0);
9561      }
9562      if( rc ){
9563        raw_printf(stderr, "Error querying the selftest table\n");
9564        rc = 1;
9565        sqlite3_finalize(pStmt);
9566        goto meta_command_exit;
9567      }
9568      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9569        int tno = sqlite3_column_int(pStmt, 0);
9570        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9571        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9572        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9573
9574        k = 0;
9575        if( bVerbose>0 ){
9576          char *zQuote = sqlite3_mprintf("%q", zSql);
9577          printf("%d: %s %s\n", tno, zOp, zSql);
9578          sqlite3_free(zQuote);
9579        }
9580        if( strcmp(zOp,"memo")==0 ){
9581          utf8_printf(p->out, "%s\n", zSql);
9582        }else
9583        if( strcmp(zOp,"run")==0 ){
9584          char *zErrMsg = 0;
9585          str.n = 0;
9586          str.z[0] = 0;
9587          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9588          nTest++;
9589          if( bVerbose ){
9590            utf8_printf(p->out, "Result: %s\n", str.z);
9591          }
9592          if( rc || zErrMsg ){
9593            nErr++;
9594            rc = 1;
9595            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9596            sqlite3_free(zErrMsg);
9597          }else if( strcmp(zAns,str.z)!=0 ){
9598            nErr++;
9599            rc = 1;
9600            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9601            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9602          }
9603        }else
9604        {
9605          utf8_printf(stderr,
9606            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9607          rc = 1;
9608          break;
9609        }
9610      } /* End loop over rows of content from SELFTEST */
9611      sqlite3_finalize(pStmt);
9612    } /* End loop over k */
9613    freeText(&str);
9614    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9615  }else
9616
9617  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9618    if( nArg<2 || nArg>3 ){
9619      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9620      rc = 1;
9621    }
9622    if( nArg>=2 ){
9623      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9624                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9625    }
9626    if( nArg>=3 ){
9627      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9628                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9629    }
9630  }else
9631
9632  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9633    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9634    int i;                   /* Loop counter */
9635    int bSchema = 0;         /* Also hash the schema */
9636    int bSeparate = 0;       /* Hash each table separately */
9637    int iSize = 224;         /* Hash algorithm to use */
9638    int bDebug = 0;          /* Only show the query that would have run */
9639    sqlite3_stmt *pStmt;     /* For querying tables names */
9640    char *zSql;              /* SQL to be run */
9641    char *zSep;              /* Separator */
9642    ShellText sSql;          /* Complete SQL for the query to run the hash */
9643    ShellText sQuery;        /* Set of queries used to read all content */
9644    open_db(p, 0);
9645    for(i=1; i<nArg; i++){
9646      const char *z = azArg[i];
9647      if( z[0]=='-' ){
9648        z++;
9649        if( z[0]=='-' ) z++;
9650        if( strcmp(z,"schema")==0 ){
9651          bSchema = 1;
9652        }else
9653        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9654         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9655        ){
9656          iSize = atoi(&z[5]);
9657        }else
9658        if( strcmp(z,"debug")==0 ){
9659          bDebug = 1;
9660        }else
9661        {
9662          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9663                      azArg[i], azArg[0]);
9664          showHelp(p->out, azArg[0]);
9665          rc = 1;
9666          goto meta_command_exit;
9667        }
9668      }else if( zLike ){
9669        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9670        rc = 1;
9671        goto meta_command_exit;
9672      }else{
9673        zLike = z;
9674        bSeparate = 1;
9675        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9676      }
9677    }
9678    if( bSchema ){
9679      zSql = "SELECT lower(name) FROM sqlite_schema"
9680             " WHERE type='table' AND coalesce(rootpage,0)>1"
9681             " UNION ALL SELECT 'sqlite_schema'"
9682             " ORDER BY 1 collate nocase";
9683    }else{
9684      zSql = "SELECT lower(name) FROM sqlite_schema"
9685             " WHERE type='table' AND coalesce(rootpage,0)>1"
9686             " AND name NOT LIKE 'sqlite_%'"
9687             " ORDER BY 1 collate nocase";
9688    }
9689    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9690    initText(&sQuery);
9691    initText(&sSql);
9692    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9693    zSep = "VALUES(";
9694    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9695      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9696      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9697      if( strncmp(zTab, "sqlite_",7)!=0 ){
9698        appendText(&sQuery,"SELECT * FROM ", 0);
9699        appendText(&sQuery,zTab,'"');
9700        appendText(&sQuery," NOT INDEXED;", 0);
9701      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9702        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9703                           " ORDER BY name;", 0);
9704      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9705        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9706                           " ORDER BY name;", 0);
9707      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9708        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9709                           " ORDER BY tbl,idx;", 0);
9710      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9711        appendText(&sQuery, "SELECT * FROM ", 0);
9712        appendText(&sQuery, zTab, 0);
9713        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9714      }
9715      appendText(&sSql, zSep, 0);
9716      appendText(&sSql, sQuery.z, '\'');
9717      sQuery.n = 0;
9718      appendText(&sSql, ",", 0);
9719      appendText(&sSql, zTab, '\'');
9720      zSep = "),(";
9721    }
9722    sqlite3_finalize(pStmt);
9723    if( bSeparate ){
9724      zSql = sqlite3_mprintf(
9725          "%s))"
9726          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9727          "   FROM [sha3sum$query]",
9728          sSql.z, iSize);
9729    }else{
9730      zSql = sqlite3_mprintf(
9731          "%s))"
9732          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9733          "   FROM [sha3sum$query]",
9734          sSql.z, iSize);
9735    }
9736    freeText(&sQuery);
9737    freeText(&sSql);
9738    if( bDebug ){
9739      utf8_printf(p->out, "%s\n", zSql);
9740    }else{
9741      shell_exec(p, zSql, 0);
9742    }
9743    sqlite3_free(zSql);
9744  }else
9745
9746#ifndef SQLITE_NOHAVE_SYSTEM
9747  if( c=='s'
9748   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9749  ){
9750    char *zCmd;
9751    int i, x;
9752    if( nArg<2 ){
9753      raw_printf(stderr, "Usage: .system COMMAND\n");
9754      rc = 1;
9755      goto meta_command_exit;
9756    }
9757    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9758    for(i=2; i<nArg; i++){
9759      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9760                             zCmd, azArg[i]);
9761    }
9762    x = system(zCmd);
9763    sqlite3_free(zCmd);
9764    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9765  }else
9766#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9767
9768  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9769    static const char *azBool[] = { "off", "on", "trigger", "full"};
9770    const char *zOut;
9771    int i;
9772    if( nArg!=1 ){
9773      raw_printf(stderr, "Usage: .show\n");
9774      rc = 1;
9775      goto meta_command_exit;
9776    }
9777    utf8_printf(p->out, "%12.12s: %s\n","echo",
9778                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9779    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9780    utf8_printf(p->out, "%12.12s: %s\n","explain",
9781         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9782    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9783    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9784    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9785      output_c_string(p->out, p->nullValue);
9786      raw_printf(p->out, "\n");
9787    utf8_printf(p->out,"%12.12s: %s\n","output",
9788            strlen30(p->outfile) ? p->outfile : "stdout");
9789    utf8_printf(p->out,"%12.12s: ", "colseparator");
9790      output_c_string(p->out, p->colSeparator);
9791      raw_printf(p->out, "\n");
9792    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9793      output_c_string(p->out, p->rowSeparator);
9794      raw_printf(p->out, "\n");
9795    switch( p->statsOn ){
9796      case 0:  zOut = "off";     break;
9797      default: zOut = "on";      break;
9798      case 2:  zOut = "stmt";    break;
9799      case 3:  zOut = "vmstep";  break;
9800    }
9801    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
9802    utf8_printf(p->out, "%12.12s: ", "width");
9803    for (i=0;i<p->nWidth;i++) {
9804      raw_printf(p->out, "%d ", p->colWidth[i]);
9805    }
9806    raw_printf(p->out, "\n");
9807    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9808                p->zDbFilename ? p->zDbFilename : "");
9809  }else
9810
9811  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9812    if( nArg==2 ){
9813      if( strcmp(azArg[1],"stmt")==0 ){
9814        p->statsOn = 2;
9815      }else if( strcmp(azArg[1],"vmstep")==0 ){
9816        p->statsOn = 3;
9817      }else{
9818        p->statsOn = (u8)booleanValue(azArg[1]);
9819      }
9820    }else if( nArg==1 ){
9821      display_stats(p->db, p, 0);
9822    }else{
9823      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
9824      rc = 1;
9825    }
9826  }else
9827
9828  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9829   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9830                 || strncmp(azArg[0], "indexes", n)==0) )
9831  ){
9832    sqlite3_stmt *pStmt;
9833    char **azResult;
9834    int nRow, nAlloc;
9835    int ii;
9836    ShellText s;
9837    initText(&s);
9838    open_db(p, 0);
9839    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9840    if( rc ){
9841      sqlite3_finalize(pStmt);
9842      return shellDatabaseError(p->db);
9843    }
9844
9845    if( nArg>2 && c=='i' ){
9846      /* It is an historical accident that the .indexes command shows an error
9847      ** when called with the wrong number of arguments whereas the .tables
9848      ** command does not. */
9849      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9850      rc = 1;
9851      sqlite3_finalize(pStmt);
9852      goto meta_command_exit;
9853    }
9854    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9855      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9856      if( zDbName==0 ) continue;
9857      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9858      if( sqlite3_stricmp(zDbName, "main")==0 ){
9859        appendText(&s, "SELECT name FROM ", 0);
9860      }else{
9861        appendText(&s, "SELECT ", 0);
9862        appendText(&s, zDbName, '\'');
9863        appendText(&s, "||'.'||name FROM ", 0);
9864      }
9865      appendText(&s, zDbName, '"');
9866      appendText(&s, ".sqlite_schema ", 0);
9867      if( c=='t' ){
9868        appendText(&s," WHERE type IN ('table','view')"
9869                      "   AND name NOT LIKE 'sqlite_%'"
9870                      "   AND name LIKE ?1", 0);
9871      }else{
9872        appendText(&s," WHERE type='index'"
9873                      "   AND tbl_name LIKE ?1", 0);
9874      }
9875    }
9876    rc = sqlite3_finalize(pStmt);
9877    appendText(&s, " ORDER BY 1", 0);
9878    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9879    freeText(&s);
9880    if( rc ) return shellDatabaseError(p->db);
9881
9882    /* Run the SQL statement prepared by the above block. Store the results
9883    ** as an array of nul-terminated strings in azResult[].  */
9884    nRow = nAlloc = 0;
9885    azResult = 0;
9886    if( nArg>1 ){
9887      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9888    }else{
9889      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9890    }
9891    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9892      if( nRow>=nAlloc ){
9893        char **azNew;
9894        int n2 = nAlloc*2 + 10;
9895        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9896        if( azNew==0 ) shell_out_of_memory();
9897        nAlloc = n2;
9898        azResult = azNew;
9899      }
9900      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9901      if( 0==azResult[nRow] ) shell_out_of_memory();
9902      nRow++;
9903    }
9904    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9905      rc = shellDatabaseError(p->db);
9906    }
9907
9908    /* Pretty-print the contents of array azResult[] to the output */
9909    if( rc==0 && nRow>0 ){
9910      int len, maxlen = 0;
9911      int i, j;
9912      int nPrintCol, nPrintRow;
9913      for(i=0; i<nRow; i++){
9914        len = strlen30(azResult[i]);
9915        if( len>maxlen ) maxlen = len;
9916      }
9917      nPrintCol = 80/(maxlen+2);
9918      if( nPrintCol<1 ) nPrintCol = 1;
9919      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9920      for(i=0; i<nPrintRow; i++){
9921        for(j=i; j<nRow; j+=nPrintRow){
9922          char *zSp = j<nPrintRow ? "" : "  ";
9923          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9924                      azResult[j] ? azResult[j]:"");
9925        }
9926        raw_printf(p->out, "\n");
9927      }
9928    }
9929
9930    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9931    sqlite3_free(azResult);
9932  }else
9933
9934  /* Begin redirecting output to the file "testcase-out.txt" */
9935  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9936    output_reset(p);
9937    p->out = output_file_open("testcase-out.txt", 0);
9938    if( p->out==0 ){
9939      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9940    }
9941    if( nArg>=2 ){
9942      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9943    }else{
9944      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9945    }
9946  }else
9947
9948#ifndef SQLITE_UNTESTABLE
9949  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9950    static const struct {
9951       const char *zCtrlName;   /* Name of a test-control option */
9952       int ctrlCode;            /* Integer code for that option */
9953       const char *zUsage;      /* Usage notes */
9954    } aCtrl[] = {
9955      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9956      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9957    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9958    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9959      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9960      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9961    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9962      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9963      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9964      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9965      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9966      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9967#ifdef YYCOVERAGE
9968      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9969#endif
9970      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9971      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9972      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9973      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9974      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
9975    };
9976    int testctrl = -1;
9977    int iCtrl = -1;
9978    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9979    int isOk = 0;
9980    int i, n2;
9981    const char *zCmd = 0;
9982
9983    open_db(p, 0);
9984    zCmd = nArg>=2 ? azArg[1] : "help";
9985
9986    /* The argument can optionally begin with "-" or "--" */
9987    if( zCmd[0]=='-' && zCmd[1] ){
9988      zCmd++;
9989      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9990    }
9991
9992    /* --help lists all test-controls */
9993    if( strcmp(zCmd,"help")==0 ){
9994      utf8_printf(p->out, "Available test-controls:\n");
9995      for(i=0; i<ArraySize(aCtrl); i++){
9996        utf8_printf(p->out, "  .testctrl %s %s\n",
9997                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
9998      }
9999      rc = 1;
10000      goto meta_command_exit;
10001    }
10002
10003    /* convert testctrl text option to value. allow any unique prefix
10004    ** of the option name, or a numerical value. */
10005    n2 = strlen30(zCmd);
10006    for(i=0; i<ArraySize(aCtrl); i++){
10007      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10008        if( testctrl<0 ){
10009          testctrl = aCtrl[i].ctrlCode;
10010          iCtrl = i;
10011        }else{
10012          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
10013                              "Use \".testctrl --help\" for help\n", zCmd);
10014          rc = 1;
10015          goto meta_command_exit;
10016        }
10017      }
10018    }
10019    if( testctrl<0 ){
10020      utf8_printf(stderr,"Error: unknown test-control: %s\n"
10021                         "Use \".testctrl --help\" for help\n", zCmd);
10022    }else{
10023      switch(testctrl){
10024
10025        /* sqlite3_test_control(int, db, int) */
10026        case SQLITE_TESTCTRL_OPTIMIZATIONS:
10027          if( nArg==3 ){
10028            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
10029            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10030            isOk = 3;
10031          }
10032          break;
10033
10034        /* sqlite3_test_control(int) */
10035        case SQLITE_TESTCTRL_PRNG_SAVE:
10036        case SQLITE_TESTCTRL_PRNG_RESTORE:
10037        case SQLITE_TESTCTRL_BYTEORDER:
10038          if( nArg==2 ){
10039            rc2 = sqlite3_test_control(testctrl);
10040            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
10041          }
10042          break;
10043
10044        /* sqlite3_test_control(int, uint) */
10045        case SQLITE_TESTCTRL_PENDING_BYTE:
10046          if( nArg==3 ){
10047            unsigned int opt = (unsigned int)integerValue(azArg[2]);
10048            rc2 = sqlite3_test_control(testctrl, opt);
10049            isOk = 3;
10050          }
10051          break;
10052
10053        /* sqlite3_test_control(int, int, sqlite3*) */
10054        case SQLITE_TESTCTRL_PRNG_SEED:
10055          if( nArg==3 || nArg==4 ){
10056            int ii = (int)integerValue(azArg[2]);
10057            sqlite3 *db;
10058            if( ii==0 && strcmp(azArg[2],"random")==0 ){
10059              sqlite3_randomness(sizeof(ii),&ii);
10060              printf("-- random seed: %d\n", ii);
10061            }
10062            if( nArg==3 ){
10063              db = 0;
10064            }else{
10065              db = p->db;
10066              /* Make sure the schema has been loaded */
10067              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10068            }
10069            rc2 = sqlite3_test_control(testctrl, ii, db);
10070            isOk = 3;
10071          }
10072          break;
10073
10074        /* sqlite3_test_control(int, int) */
10075        case SQLITE_TESTCTRL_ASSERT:
10076        case SQLITE_TESTCTRL_ALWAYS:
10077          if( nArg==3 ){
10078            int opt = booleanValue(azArg[2]);
10079            rc2 = sqlite3_test_control(testctrl, opt);
10080            isOk = 1;
10081          }
10082          break;
10083
10084        /* sqlite3_test_control(int, int) */
10085        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10086        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10087          if( nArg==3 ){
10088            int opt = booleanValue(azArg[2]);
10089            rc2 = sqlite3_test_control(testctrl, opt);
10090            isOk = 3;
10091          }
10092          break;
10093
10094        /* sqlite3_test_control(sqlite3*) */
10095        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10096          rc2 = sqlite3_test_control(testctrl, p->db);
10097          isOk = 3;
10098          break;
10099
10100        case SQLITE_TESTCTRL_IMPOSTER:
10101          if( nArg==5 ){
10102            rc2 = sqlite3_test_control(testctrl, p->db,
10103                          azArg[2],
10104                          integerValue(azArg[3]),
10105                          integerValue(azArg[4]));
10106            isOk = 3;
10107          }
10108          break;
10109
10110        case SQLITE_TESTCTRL_SEEK_COUNT: {
10111          u64 x = 0;
10112          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10113          utf8_printf(p->out, "%llu\n", x);
10114          isOk = 3;
10115          break;
10116        }
10117
10118#ifdef YYCOVERAGE
10119        case SQLITE_TESTCTRL_PARSER_COVERAGE:
10120          if( nArg==2 ){
10121            sqlite3_test_control(testctrl, p->out);
10122            isOk = 3;
10123          }
10124#endif
10125      }
10126    }
10127    if( isOk==0 && iCtrl>=0 ){
10128      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10129      rc = 1;
10130    }else if( isOk==1 ){
10131      raw_printf(p->out, "%d\n", rc2);
10132    }else if( isOk==2 ){
10133      raw_printf(p->out, "0x%08x\n", rc2);
10134    }
10135  }else
10136#endif /* !defined(SQLITE_UNTESTABLE) */
10137
10138  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10139    open_db(p, 0);
10140    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10141  }else
10142
10143  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10144    if( nArg==2 ){
10145      enableTimer = booleanValue(azArg[1]);
10146      if( enableTimer && !HAS_TIMER ){
10147        raw_printf(stderr, "Error: timer not available on this system.\n");
10148        enableTimer = 0;
10149      }
10150    }else{
10151      raw_printf(stderr, "Usage: .timer on|off\n");
10152      rc = 1;
10153    }
10154  }else
10155
10156#ifndef SQLITE_OMIT_TRACE
10157  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10158    int mType = 0;
10159    int jj;
10160    open_db(p, 0);
10161    for(jj=1; jj<nArg; jj++){
10162      const char *z = azArg[jj];
10163      if( z[0]=='-' ){
10164        if( optionMatch(z, "expanded") ){
10165          p->eTraceType = SHELL_TRACE_EXPANDED;
10166        }
10167#ifdef SQLITE_ENABLE_NORMALIZE
10168        else if( optionMatch(z, "normalized") ){
10169          p->eTraceType = SHELL_TRACE_NORMALIZED;
10170        }
10171#endif
10172        else if( optionMatch(z, "plain") ){
10173          p->eTraceType = SHELL_TRACE_PLAIN;
10174        }
10175        else if( optionMatch(z, "profile") ){
10176          mType |= SQLITE_TRACE_PROFILE;
10177        }
10178        else if( optionMatch(z, "row") ){
10179          mType |= SQLITE_TRACE_ROW;
10180        }
10181        else if( optionMatch(z, "stmt") ){
10182          mType |= SQLITE_TRACE_STMT;
10183        }
10184        else if( optionMatch(z, "close") ){
10185          mType |= SQLITE_TRACE_CLOSE;
10186        }
10187        else {
10188          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10189          rc = 1;
10190          goto meta_command_exit;
10191        }
10192      }else{
10193        output_file_close(p->traceOut);
10194        p->traceOut = output_file_open(azArg[1], 0);
10195      }
10196    }
10197    if( p->traceOut==0 ){
10198      sqlite3_trace_v2(p->db, 0, 0, 0);
10199    }else{
10200      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10201      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10202    }
10203  }else
10204#endif /* !defined(SQLITE_OMIT_TRACE) */
10205
10206#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10207  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10208    int ii;
10209    int lenOpt;
10210    char *zOpt;
10211    if( nArg<2 ){
10212      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10213      rc = 1;
10214      goto meta_command_exit;
10215    }
10216    open_db(p, 0);
10217    zOpt = azArg[1];
10218    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10219    lenOpt = (int)strlen(zOpt);
10220    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10221      assert( azArg[nArg]==0 );
10222      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10223    }else{
10224      for(ii=1; ii<nArg; ii++){
10225        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10226      }
10227    }
10228  }else
10229#endif
10230
10231#if SQLITE_USER_AUTHENTICATION
10232  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10233    if( nArg<2 ){
10234      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10235      rc = 1;
10236      goto meta_command_exit;
10237    }
10238    open_db(p, 0);
10239    if( strcmp(azArg[1],"login")==0 ){
10240      if( nArg!=4 ){
10241        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10242        rc = 1;
10243        goto meta_command_exit;
10244      }
10245      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10246                                     strlen30(azArg[3]));
10247      if( rc ){
10248        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10249        rc = 1;
10250      }
10251    }else if( strcmp(azArg[1],"add")==0 ){
10252      if( nArg!=5 ){
10253        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10254        rc = 1;
10255        goto meta_command_exit;
10256      }
10257      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10258                            booleanValue(azArg[4]));
10259      if( rc ){
10260        raw_printf(stderr, "User-Add failed: %d\n", rc);
10261        rc = 1;
10262      }
10263    }else if( strcmp(azArg[1],"edit")==0 ){
10264      if( nArg!=5 ){
10265        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10266        rc = 1;
10267        goto meta_command_exit;
10268      }
10269      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10270                              booleanValue(azArg[4]));
10271      if( rc ){
10272        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10273        rc = 1;
10274      }
10275    }else if( strcmp(azArg[1],"delete")==0 ){
10276      if( nArg!=3 ){
10277        raw_printf(stderr, "Usage: .user delete USER\n");
10278        rc = 1;
10279        goto meta_command_exit;
10280      }
10281      rc = sqlite3_user_delete(p->db, azArg[2]);
10282      if( rc ){
10283        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10284        rc = 1;
10285      }
10286    }else{
10287      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10288      rc = 1;
10289      goto meta_command_exit;
10290    }
10291  }else
10292#endif /* SQLITE_USER_AUTHENTICATION */
10293
10294  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10295    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10296        sqlite3_libversion(), sqlite3_sourceid());
10297#if SQLITE_HAVE_ZLIB
10298    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10299#endif
10300#define CTIMEOPT_VAL_(opt) #opt
10301#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10302#if defined(__clang__) && defined(__clang_major__)
10303    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10304                    CTIMEOPT_VAL(__clang_minor__) "."
10305                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10306#elif defined(_MSC_VER)
10307    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10308#elif defined(__GNUC__) && defined(__VERSION__)
10309    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10310#endif
10311  }else
10312
10313  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10314    const char *zDbName = nArg==2 ? azArg[1] : "main";
10315    sqlite3_vfs *pVfs = 0;
10316    if( p->db ){
10317      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10318      if( pVfs ){
10319        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10320        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10321        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10322        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10323      }
10324    }
10325  }else
10326
10327  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10328    sqlite3_vfs *pVfs;
10329    sqlite3_vfs *pCurrent = 0;
10330    if( p->db ){
10331      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10332    }
10333    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10334      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10335           pVfs==pCurrent ? "  <--- CURRENT" : "");
10336      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10337      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10338      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10339      if( pVfs->pNext ){
10340        raw_printf(p->out, "-----------------------------------\n");
10341      }
10342    }
10343  }else
10344
10345  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10346    const char *zDbName = nArg==2 ? azArg[1] : "main";
10347    char *zVfsName = 0;
10348    if( p->db ){
10349      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10350      if( zVfsName ){
10351        utf8_printf(p->out, "%s\n", zVfsName);
10352        sqlite3_free(zVfsName);
10353      }
10354    }
10355  }else
10356
10357  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10358    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10359    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
10360  }else
10361
10362  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10363    int j;
10364    assert( nArg<=ArraySize(azArg) );
10365    p->nWidth = nArg-1;
10366    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10367    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10368    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10369    for(j=1; j<nArg; j++){
10370      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10371    }
10372  }else
10373
10374  {
10375    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10376      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10377    rc = 1;
10378  }
10379
10380meta_command_exit:
10381  if( p->outCount ){
10382    p->outCount--;
10383    if( p->outCount==0 ) output_reset(p);
10384  }
10385  return rc;
10386}
10387
10388/*
10389** Return TRUE if a semicolon occurs anywhere in the first N characters
10390** of string z[].
10391*/
10392static int line_contains_semicolon(const char *z, int N){
10393  int i;
10394  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
10395  return 0;
10396}
10397
10398/*
10399** Test to see if a line consists entirely of whitespace.
10400*/
10401static int _all_whitespace(const char *z){
10402  for(; *z; z++){
10403    if( IsSpace(z[0]) ) continue;
10404    if( *z=='/' && z[1]=='*' ){
10405      z += 2;
10406      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
10407      if( *z==0 ) return 0;
10408      z++;
10409      continue;
10410    }
10411    if( *z=='-' && z[1]=='-' ){
10412      z += 2;
10413      while( *z && *z!='\n' ){ z++; }
10414      if( *z==0 ) return 1;
10415      continue;
10416    }
10417    return 0;
10418  }
10419  return 1;
10420}
10421
10422/*
10423** Return TRUE if the line typed in is an SQL command terminator other
10424** than a semi-colon.  The SQL Server style "go" command is understood
10425** as is the Oracle "/".
10426*/
10427static int line_is_command_terminator(const char *zLine){
10428  while( IsSpace(zLine[0]) ){ zLine++; };
10429  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
10430    return 1;  /* Oracle */
10431  }
10432  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
10433         && _all_whitespace(&zLine[2]) ){
10434    return 1;  /* SQL Server */
10435  }
10436  return 0;
10437}
10438
10439/*
10440** We need a default sqlite3_complete() implementation to use in case
10441** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10442** any arbitrary text is a complete SQL statement.  This is not very
10443** user-friendly, but it does seem to work.
10444*/
10445#ifdef SQLITE_OMIT_COMPLETE
10446#define sqlite3_complete(x) 1
10447#endif
10448
10449/*
10450** Return true if zSql is a complete SQL statement.  Return false if it
10451** ends in the middle of a string literal or C-style comment.
10452*/
10453static int line_is_complete(char *zSql, int nSql){
10454  int rc;
10455  if( zSql==0 ) return 1;
10456  zSql[nSql] = ';';
10457  zSql[nSql+1] = 0;
10458  rc = sqlite3_complete(zSql);
10459  zSql[nSql] = 0;
10460  return rc;
10461}
10462
10463/*
10464** Run a single line of SQL.  Return the number of errors.
10465*/
10466static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10467  int rc;
10468  char *zErrMsg = 0;
10469
10470  open_db(p, 0);
10471  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10472  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10473  BEGIN_TIMER;
10474  rc = shell_exec(p, zSql, &zErrMsg);
10475  END_TIMER;
10476  if( rc || zErrMsg ){
10477    char zPrefix[100];
10478    if( in!=0 || !stdin_is_interactive ){
10479      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10480                       "Error: near line %d:", startline);
10481    }else{
10482      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10483    }
10484    if( zErrMsg!=0 ){
10485      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10486      sqlite3_free(zErrMsg);
10487      zErrMsg = 0;
10488    }else{
10489      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10490    }
10491    return 1;
10492  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10493    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
10494            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
10495  }
10496  return 0;
10497}
10498
10499
10500/*
10501** Read input from *in and process it.  If *in==0 then input
10502** is interactive - the user is typing it it.  Otherwise, input
10503** is coming from a file or device.  A prompt is issued and history
10504** is saved only if input is interactive.  An interrupt signal will
10505** cause this routine to exit immediately, unless input is interactive.
10506**
10507** Return the number of errors.
10508*/
10509static int process_input(ShellState *p){
10510  char *zLine = 0;          /* A single input line */
10511  char *zSql = 0;           /* Accumulated SQL text */
10512  int nLine;                /* Length of current line */
10513  int nSql = 0;             /* Bytes of zSql[] used */
10514  int nAlloc = 0;           /* Allocated zSql[] space */
10515  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10516  int rc;                   /* Error code */
10517  int errCnt = 0;           /* Number of errors seen */
10518  int startline = 0;        /* Line number for start of current input */
10519
10520  p->lineno = 0;
10521  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10522    fflush(p->out);
10523    zLine = one_input_line(p->in, zLine, nSql>0);
10524    if( zLine==0 ){
10525      /* End of input */
10526      if( p->in==0 && stdin_is_interactive ) printf("\n");
10527      break;
10528    }
10529    if( seenInterrupt ){
10530      if( p->in!=0 ) break;
10531      seenInterrupt = 0;
10532    }
10533    p->lineno++;
10534    if( nSql==0 && _all_whitespace(zLine) ){
10535      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10536      continue;
10537    }
10538    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10539      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10540      if( zLine[0]=='.' ){
10541        rc = do_meta_command(zLine, p);
10542        if( rc==2 ){ /* exit requested */
10543          break;
10544        }else if( rc ){
10545          errCnt++;
10546        }
10547      }
10548      continue;
10549    }
10550    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10551      memcpy(zLine,";",2);
10552    }
10553    nLine = strlen30(zLine);
10554    if( nSql+nLine+2>=nAlloc ){
10555      nAlloc = nSql+nLine+100;
10556      zSql = realloc(zSql, nAlloc);
10557      if( zSql==0 ) shell_out_of_memory();
10558    }
10559    nSqlPrior = nSql;
10560    if( nSql==0 ){
10561      int i;
10562      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10563      assert( nAlloc>0 && zSql!=0 );
10564      memcpy(zSql, zLine+i, nLine+1-i);
10565      startline = p->lineno;
10566      nSql = nLine-i;
10567    }else{
10568      zSql[nSql++] = '\n';
10569      memcpy(zSql+nSql, zLine, nLine+1);
10570      nSql += nLine;
10571    }
10572    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10573                && sqlite3_complete(zSql) ){
10574      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10575      nSql = 0;
10576      if( p->outCount ){
10577        output_reset(p);
10578        p->outCount = 0;
10579      }else{
10580        clearTempFile(p);
10581      }
10582    }else if( nSql && _all_whitespace(zSql) ){
10583      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10584      nSql = 0;
10585    }
10586  }
10587  if( nSql && !_all_whitespace(zSql) ){
10588    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10589  }
10590  free(zSql);
10591  free(zLine);
10592  return errCnt>0;
10593}
10594
10595/*
10596** Return a pathname which is the user's home directory.  A
10597** 0 return indicates an error of some kind.
10598*/
10599static char *find_home_dir(int clearFlag){
10600  static char *home_dir = NULL;
10601  if( clearFlag ){
10602    free(home_dir);
10603    home_dir = 0;
10604    return 0;
10605  }
10606  if( home_dir ) return home_dir;
10607
10608#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10609     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10610  {
10611    struct passwd *pwent;
10612    uid_t uid = getuid();
10613    if( (pwent=getpwuid(uid)) != NULL) {
10614      home_dir = pwent->pw_dir;
10615    }
10616  }
10617#endif
10618
10619#if defined(_WIN32_WCE)
10620  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10621   */
10622  home_dir = "/";
10623#else
10624
10625#if defined(_WIN32) || defined(WIN32)
10626  if (!home_dir) {
10627    home_dir = getenv("USERPROFILE");
10628  }
10629#endif
10630
10631  if (!home_dir) {
10632    home_dir = getenv("HOME");
10633  }
10634
10635#if defined(_WIN32) || defined(WIN32)
10636  if (!home_dir) {
10637    char *zDrive, *zPath;
10638    int n;
10639    zDrive = getenv("HOMEDRIVE");
10640    zPath = getenv("HOMEPATH");
10641    if( zDrive && zPath ){
10642      n = strlen30(zDrive) + strlen30(zPath) + 1;
10643      home_dir = malloc( n );
10644      if( home_dir==0 ) return 0;
10645      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10646      return home_dir;
10647    }
10648    home_dir = "c:\\";
10649  }
10650#endif
10651
10652#endif /* !_WIN32_WCE */
10653
10654  if( home_dir ){
10655    int n = strlen30(home_dir) + 1;
10656    char *z = malloc( n );
10657    if( z ) memcpy(z, home_dir, n);
10658    home_dir = z;
10659  }
10660
10661  return home_dir;
10662}
10663
10664/*
10665** Read input from the file given by sqliterc_override.  Or if that
10666** parameter is NULL, take input from ~/.sqliterc
10667**
10668** Returns the number of errors.
10669*/
10670static void process_sqliterc(
10671  ShellState *p,                  /* Configuration data */
10672  const char *sqliterc_override   /* Name of config file. NULL to use default */
10673){
10674  char *home_dir = NULL;
10675  const char *sqliterc = sqliterc_override;
10676  char *zBuf = 0;
10677  FILE *inSaved = p->in;
10678  int savedLineno = p->lineno;
10679
10680  if (sqliterc == NULL) {
10681    home_dir = find_home_dir(0);
10682    if( home_dir==0 ){
10683      raw_printf(stderr, "-- warning: cannot find home directory;"
10684                      " cannot read ~/.sqliterc\n");
10685      return;
10686    }
10687    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10688    sqliterc = zBuf;
10689  }
10690  p->in = fopen(sqliterc,"rb");
10691  if( p->in ){
10692    if( stdin_is_interactive ){
10693      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10694    }
10695    if( process_input(p) && bail_on_error ) exit(1);
10696    fclose(p->in);
10697  }else if( sqliterc_override!=0 ){
10698    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
10699    if( bail_on_error ) exit(1);
10700  }
10701  p->in = inSaved;
10702  p->lineno = savedLineno;
10703  sqlite3_free(zBuf);
10704}
10705
10706/*
10707** Show available command line options
10708*/
10709static const char zOptions[] =
10710#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10711  "   -A ARGS...           run \".archive ARGS\" and exit\n"
10712#endif
10713  "   -append              append the database to the end of the file\n"
10714  "   -ascii               set output mode to 'ascii'\n"
10715  "   -bail                stop after hitting an error\n"
10716  "   -batch               force batch I/O\n"
10717  "   -box                 set output mode to 'box'\n"
10718  "   -column              set output mode to 'column'\n"
10719  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
10720  "   -csv                 set output mode to 'csv'\n"
10721#if !defined(SQLITE_OMIT_DESERIALIZE)
10722  "   -deserialize         open the database using sqlite3_deserialize()\n"
10723#endif
10724  "   -echo                print commands before execution\n"
10725  "   -init FILENAME       read/process named file\n"
10726  "   -[no]header          turn headers on or off\n"
10727#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10728  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
10729#endif
10730  "   -help                show this message\n"
10731  "   -html                set output mode to HTML\n"
10732  "   -interactive         force interactive I/O\n"
10733  "   -json                set output mode to 'json'\n"
10734  "   -line                set output mode to 'line'\n"
10735  "   -list                set output mode to 'list'\n"
10736  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
10737  "   -markdown            set output mode to 'markdown'\n"
10738#if !defined(SQLITE_OMIT_DESERIALIZE)
10739  "   -maxsize N           maximum size for a --deserialize database\n"
10740#endif
10741  "   -memtrace            trace all memory allocations and deallocations\n"
10742  "   -mmap N              default mmap size set to N\n"
10743#ifdef SQLITE_ENABLE_MULTIPLEX
10744  "   -multiplex           enable the multiplexor VFS\n"
10745#endif
10746  "   -newline SEP         set output row separator. Default: '\\n'\n"
10747  "   -nofollow            refuse to open symbolic links to database files\n"
10748  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
10749  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
10750  "   -quote               set output mode to 'quote'\n"
10751  "   -readonly            open the database read-only\n"
10752  "   -separator SEP       set output column separator. Default: '|'\n"
10753#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10754  "   -sorterref SIZE      sorter references threshold size\n"
10755#endif
10756  "   -stats               print memory stats before each finalize\n"
10757  "   -table               set output mode to 'table'\n"
10758  "   -tabs                set output mode to 'tabs'\n"
10759  "   -version             show SQLite version\n"
10760  "   -vfs NAME            use NAME as the default VFS\n"
10761#ifdef SQLITE_ENABLE_VFSTRACE
10762  "   -vfstrace            enable tracing of all VFS calls\n"
10763#endif
10764#ifdef SQLITE_HAVE_ZLIB
10765  "   -zip                 open the file as a ZIP Archive\n"
10766#endif
10767;
10768static void usage(int showDetail){
10769  utf8_printf(stderr,
10770      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10771      "FILENAME is the name of an SQLite database. A new database is created\n"
10772      "if the file does not previously exist.\n", Argv0);
10773  if( showDetail ){
10774    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10775  }else{
10776    raw_printf(stderr, "Use the -help option for additional information\n");
10777  }
10778  exit(1);
10779}
10780
10781/*
10782** Internal check:  Verify that the SQLite is uninitialized.  Print a
10783** error message if it is initialized.
10784*/
10785static void verify_uninitialized(void){
10786  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10787    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10788                        " initialization.\n");
10789  }
10790}
10791
10792/*
10793** Initialize the state information in data
10794*/
10795static void main_init(ShellState *data) {
10796  memset(data, 0, sizeof(*data));
10797  data->normalMode = data->cMode = data->mode = MODE_List;
10798  data->autoExplain = 1;
10799  memcpy(data->colSeparator,SEP_Column, 2);
10800  memcpy(data->rowSeparator,SEP_Row, 2);
10801  data->showHeader = 0;
10802  data->shellFlgs = SHFLG_Lookaside;
10803  verify_uninitialized();
10804  sqlite3_config(SQLITE_CONFIG_URI, 1);
10805  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10806  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10807  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10808  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10809}
10810
10811/*
10812** Output text to the console in a font that attracts extra attention.
10813*/
10814#ifdef _WIN32
10815static void printBold(const char *zText){
10816#if !SQLITE_OS_WINRT
10817  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10818  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10819  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10820  SetConsoleTextAttribute(out,
10821         FOREGROUND_RED|FOREGROUND_INTENSITY
10822  );
10823#endif
10824  printf("%s", zText);
10825#if !SQLITE_OS_WINRT
10826  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10827#endif
10828}
10829#else
10830static void printBold(const char *zText){
10831  printf("\033[1m%s\033[0m", zText);
10832}
10833#endif
10834
10835/*
10836** Get the argument to an --option.  Throw an error and die if no argument
10837** is available.
10838*/
10839static char *cmdline_option_value(int argc, char **argv, int i){
10840  if( i==argc ){
10841    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10842            argv[0], argv[argc-1]);
10843    exit(1);
10844  }
10845  return argv[i];
10846}
10847
10848#ifndef SQLITE_SHELL_IS_UTF8
10849#  if (defined(_WIN32) || defined(WIN32)) \
10850   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
10851#    define SQLITE_SHELL_IS_UTF8          (0)
10852#  else
10853#    define SQLITE_SHELL_IS_UTF8          (1)
10854#  endif
10855#endif
10856
10857#if SQLITE_SHELL_IS_UTF8
10858int SQLITE_CDECL main(int argc, char **argv){
10859#else
10860int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10861  char **argv;
10862#endif
10863  char *zErrMsg = 0;
10864  ShellState data;
10865  const char *zInitFile = 0;
10866  int i;
10867  int rc = 0;
10868  int warnInmemoryDb = 0;
10869  int readStdin = 1;
10870  int nCmd = 0;
10871  char **azCmd = 0;
10872  const char *zVfs = 0;           /* Value of -vfs command-line option */
10873#if !SQLITE_SHELL_IS_UTF8
10874  char **argvToFree = 0;
10875  int argcToFree = 0;
10876#endif
10877
10878  setBinaryMode(stdin, 0);
10879  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10880  stdin_is_interactive = isatty(0);
10881  stdout_is_console = isatty(1);
10882
10883#ifdef SQLITE_DEBUG
10884  registerOomSimulator();
10885#endif
10886
10887#if !defined(_WIN32_WCE)
10888  if( getenv("SQLITE_DEBUG_BREAK") ){
10889    if( isatty(0) && isatty(2) ){
10890      fprintf(stderr,
10891          "attach debugger to process %d and press any key to continue.\n",
10892          GETPID());
10893      fgetc(stdin);
10894    }else{
10895#if defined(_WIN32) || defined(WIN32)
10896#if SQLITE_OS_WINRT
10897      __debugbreak();
10898#else
10899      DebugBreak();
10900#endif
10901#elif defined(SIGTRAP)
10902      raise(SIGTRAP);
10903#endif
10904    }
10905  }
10906#endif
10907
10908#if USE_SYSTEM_SQLITE+0!=1
10909  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10910    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10911            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10912    exit(1);
10913  }
10914#endif
10915  main_init(&data);
10916
10917  /* On Windows, we must translate command-line arguments into UTF-8.
10918  ** The SQLite memory allocator subsystem has to be enabled in order to
10919  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10920  ** subsequent sqlite3_config() calls will work.  So copy all results into
10921  ** memory that does not come from the SQLite memory allocator.
10922  */
10923#if !SQLITE_SHELL_IS_UTF8
10924  sqlite3_initialize();
10925  argvToFree = malloc(sizeof(argv[0])*argc*2);
10926  argcToFree = argc;
10927  argv = argvToFree + argc;
10928  if( argv==0 ) shell_out_of_memory();
10929  for(i=0; i<argc; i++){
10930    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10931    int n;
10932    if( z==0 ) shell_out_of_memory();
10933    n = (int)strlen(z);
10934    argv[i] = malloc( n+1 );
10935    if( argv[i]==0 ) shell_out_of_memory();
10936    memcpy(argv[i], z, n+1);
10937    argvToFree[i] = argv[i];
10938    sqlite3_free(z);
10939  }
10940  sqlite3_shutdown();
10941#endif
10942
10943  assert( argc>=1 && argv && argv[0] );
10944  Argv0 = argv[0];
10945
10946  /* Make sure we have a valid signal handler early, before anything
10947  ** else is done.
10948  */
10949#ifdef SIGINT
10950  signal(SIGINT, interrupt_handler);
10951#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10952  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10953#endif
10954
10955#ifdef SQLITE_SHELL_DBNAME_PROC
10956  {
10957    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10958    ** of a C-function that will provide the name of the database file.  Use
10959    ** this compile-time option to embed this shell program in larger
10960    ** applications. */
10961    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10962    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10963    warnInmemoryDb = 0;
10964  }
10965#endif
10966
10967  /* Do an initial pass through the command-line argument to locate
10968  ** the name of the database file, the name of the initialization file,
10969  ** the size of the alternative malloc heap,
10970  ** and the first command to execute.
10971  */
10972  verify_uninitialized();
10973  for(i=1; i<argc; i++){
10974    char *z;
10975    z = argv[i];
10976    if( z[0]!='-' ){
10977      if( data.zDbFilename==0 ){
10978        data.zDbFilename = z;
10979      }else{
10980        /* Excesss arguments are interpreted as SQL (or dot-commands) and
10981        ** mean that nothing is read from stdin */
10982        readStdin = 0;
10983        nCmd++;
10984        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
10985        if( azCmd==0 ) shell_out_of_memory();
10986        azCmd[nCmd-1] = z;
10987      }
10988    }
10989    if( z[1]=='-' ) z++;
10990    if( strcmp(z,"-separator")==0
10991     || strcmp(z,"-nullvalue")==0
10992     || strcmp(z,"-newline")==0
10993     || strcmp(z,"-cmd")==0
10994    ){
10995      (void)cmdline_option_value(argc, argv, ++i);
10996    }else if( strcmp(z,"-init")==0 ){
10997      zInitFile = cmdline_option_value(argc, argv, ++i);
10998    }else if( strcmp(z,"-batch")==0 ){
10999      /* Need to check for batch mode here to so we can avoid printing
11000      ** informational messages (like from process_sqliterc) before
11001      ** we do the actual processing of arguments later in a second pass.
11002      */
11003      stdin_is_interactive = 0;
11004    }else if( strcmp(z,"-heap")==0 ){
11005#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11006      const char *zSize;
11007      sqlite3_int64 szHeap;
11008
11009      zSize = cmdline_option_value(argc, argv, ++i);
11010      szHeap = integerValue(zSize);
11011      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
11012      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
11013#else
11014      (void)cmdline_option_value(argc, argv, ++i);
11015#endif
11016    }else if( strcmp(z,"-pagecache")==0 ){
11017      sqlite3_int64 n, sz;
11018      sz = integerValue(cmdline_option_value(argc,argv,++i));
11019      if( sz>70000 ) sz = 70000;
11020      if( sz<0 ) sz = 0;
11021      n = integerValue(cmdline_option_value(argc,argv,++i));
11022      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
11023        n = 0xffffffffffffLL/sz;
11024      }
11025      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
11026                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
11027      data.shellFlgs |= SHFLG_Pagecache;
11028    }else if( strcmp(z,"-lookaside")==0 ){
11029      int n, sz;
11030      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
11031      if( sz<0 ) sz = 0;
11032      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11033      if( n<0 ) n = 0;
11034      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
11035      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
11036#ifdef SQLITE_ENABLE_VFSTRACE
11037    }else if( strcmp(z,"-vfstrace")==0 ){
11038      extern int vfstrace_register(
11039         const char *zTraceName,
11040         const char *zOldVfsName,
11041         int (*xOut)(const char*,void*),
11042         void *pOutArg,
11043         int makeDefault
11044      );
11045      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
11046#endif
11047#ifdef SQLITE_ENABLE_MULTIPLEX
11048    }else if( strcmp(z,"-multiplex")==0 ){
11049      extern int sqlite3_multiple_initialize(const char*,int);
11050      sqlite3_multiplex_initialize(0, 1);
11051#endif
11052    }else if( strcmp(z,"-mmap")==0 ){
11053      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11054      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
11055#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11056    }else if( strcmp(z,"-sorterref")==0 ){
11057      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11058      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11059#endif
11060    }else if( strcmp(z,"-vfs")==0 ){
11061      zVfs = cmdline_option_value(argc, argv, ++i);
11062#ifdef SQLITE_HAVE_ZLIB
11063    }else if( strcmp(z,"-zip")==0 ){
11064      data.openMode = SHELL_OPEN_ZIPFILE;
11065#endif
11066    }else if( strcmp(z,"-append")==0 ){
11067      data.openMode = SHELL_OPEN_APPENDVFS;
11068#ifndef SQLITE_OMIT_DESERIALIZE
11069    }else if( strcmp(z,"-deserialize")==0 ){
11070      data.openMode = SHELL_OPEN_DESERIALIZE;
11071    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11072      data.szMax = integerValue(argv[++i]);
11073#endif
11074    }else if( strcmp(z,"-readonly")==0 ){
11075      data.openMode = SHELL_OPEN_READONLY;
11076    }else if( strcmp(z,"-nofollow")==0 ){
11077      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11078#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11079    }else if( strncmp(z, "-A",2)==0 ){
11080      /* All remaining command-line arguments are passed to the ".archive"
11081      ** command, so ignore them */
11082      break;
11083#endif
11084    }else if( strcmp(z, "-memtrace")==0 ){
11085      sqlite3MemTraceActivate(stderr);
11086    }else if( strcmp(z,"-bail")==0 ){
11087      bail_on_error = 1;
11088    }
11089  }
11090  verify_uninitialized();
11091
11092
11093#ifdef SQLITE_SHELL_INIT_PROC
11094  {
11095    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11096    ** of a C-function that will perform initialization actions on SQLite that
11097    ** occur just before or after sqlite3_initialize(). Use this compile-time
11098    ** option to embed this shell program in larger applications. */
11099    extern void SQLITE_SHELL_INIT_PROC(void);
11100    SQLITE_SHELL_INIT_PROC();
11101  }
11102#else
11103  /* All the sqlite3_config() calls have now been made. So it is safe
11104  ** to call sqlite3_initialize() and process any command line -vfs option. */
11105  sqlite3_initialize();
11106#endif
11107
11108  if( zVfs ){
11109    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11110    if( pVfs ){
11111      sqlite3_vfs_register(pVfs, 1);
11112    }else{
11113      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11114      exit(1);
11115    }
11116  }
11117
11118  if( data.zDbFilename==0 ){
11119#ifndef SQLITE_OMIT_MEMORYDB
11120    data.zDbFilename = ":memory:";
11121    warnInmemoryDb = argc==1;
11122#else
11123    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11124    return 1;
11125#endif
11126  }
11127  data.out = stdout;
11128  sqlite3_appendvfs_init(0,0,0);
11129
11130  /* Go ahead and open the database file if it already exists.  If the
11131  ** file does not exist, delay opening it.  This prevents empty database
11132  ** files from being created if a user mistypes the database name argument
11133  ** to the sqlite command-line tool.
11134  */
11135  if( access(data.zDbFilename, 0)==0 ){
11136    open_db(&data, 0);
11137  }
11138
11139  /* Process the initialization file if there is one.  If no -init option
11140  ** is given on the command line, look for a file named ~/.sqliterc and
11141  ** try to process it.
11142  */
11143  process_sqliterc(&data,zInitFile);
11144
11145  /* Make a second pass through the command-line argument and set
11146  ** options.  This second pass is delayed until after the initialization
11147  ** file is processed so that the command-line arguments will override
11148  ** settings in the initialization file.
11149  */
11150  for(i=1; i<argc; i++){
11151    char *z = argv[i];
11152    if( z[0]!='-' ) continue;
11153    if( z[1]=='-' ){ z++; }
11154    if( strcmp(z,"-init")==0 ){
11155      i++;
11156    }else if( strcmp(z,"-html")==0 ){
11157      data.mode = MODE_Html;
11158    }else if( strcmp(z,"-list")==0 ){
11159      data.mode = MODE_List;
11160    }else if( strcmp(z,"-quote")==0 ){
11161      data.mode = MODE_Quote;
11162      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11163      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11164    }else if( strcmp(z,"-line")==0 ){
11165      data.mode = MODE_Line;
11166    }else if( strcmp(z,"-column")==0 ){
11167      data.mode = MODE_Column;
11168    }else if( strcmp(z,"-json")==0 ){
11169      data.mode = MODE_Json;
11170    }else if( strcmp(z,"-markdown")==0 ){
11171      data.mode = MODE_Markdown;
11172    }else if( strcmp(z,"-table")==0 ){
11173      data.mode = MODE_Table;
11174    }else if( strcmp(z,"-box")==0 ){
11175      data.mode = MODE_Box;
11176    }else if( strcmp(z,"-csv")==0 ){
11177      data.mode = MODE_Csv;
11178      memcpy(data.colSeparator,",",2);
11179#ifdef SQLITE_HAVE_ZLIB
11180    }else if( strcmp(z,"-zip")==0 ){
11181      data.openMode = SHELL_OPEN_ZIPFILE;
11182#endif
11183    }else if( strcmp(z,"-append")==0 ){
11184      data.openMode = SHELL_OPEN_APPENDVFS;
11185#ifndef SQLITE_OMIT_DESERIALIZE
11186    }else if( strcmp(z,"-deserialize")==0 ){
11187      data.openMode = SHELL_OPEN_DESERIALIZE;
11188    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11189      data.szMax = integerValue(argv[++i]);
11190#endif
11191    }else if( strcmp(z,"-readonly")==0 ){
11192      data.openMode = SHELL_OPEN_READONLY;
11193    }else if( strcmp(z,"-nofollow")==0 ){
11194      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11195    }else if( strcmp(z,"-ascii")==0 ){
11196      data.mode = MODE_Ascii;
11197      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
11198      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
11199    }else if( strcmp(z,"-tabs")==0 ){
11200      data.mode = MODE_List;
11201      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
11202      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11203    }else if( strcmp(z,"-separator")==0 ){
11204      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11205                       "%s",cmdline_option_value(argc,argv,++i));
11206    }else if( strcmp(z,"-newline")==0 ){
11207      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11208                       "%s",cmdline_option_value(argc,argv,++i));
11209    }else if( strcmp(z,"-nullvalue")==0 ){
11210      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11211                       "%s",cmdline_option_value(argc,argv,++i));
11212    }else if( strcmp(z,"-header")==0 ){
11213      data.showHeader = 1;
11214    }else if( strcmp(z,"-noheader")==0 ){
11215      data.showHeader = 0;
11216    }else if( strcmp(z,"-echo")==0 ){
11217      ShellSetFlag(&data, SHFLG_Echo);
11218    }else if( strcmp(z,"-eqp")==0 ){
11219      data.autoEQP = AUTOEQP_on;
11220    }else if( strcmp(z,"-eqpfull")==0 ){
11221      data.autoEQP = AUTOEQP_full;
11222    }else if( strcmp(z,"-stats")==0 ){
11223      data.statsOn = 1;
11224    }else if( strcmp(z,"-scanstats")==0 ){
11225      data.scanstatsOn = 1;
11226    }else if( strcmp(z,"-backslash")==0 ){
11227      /* Undocumented command-line option: -backslash
11228      ** Causes C-style backslash escapes to be evaluated in SQL statements
11229      ** prior to sending the SQL into SQLite.  Useful for injecting
11230      ** crazy bytes in the middle of SQL statements for testing and debugging.
11231      */
11232      ShellSetFlag(&data, SHFLG_Backslash);
11233    }else if( strcmp(z,"-bail")==0 ){
11234      /* No-op.  The bail_on_error flag should already be set. */
11235    }else if( strcmp(z,"-version")==0 ){
11236      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11237      return 0;
11238    }else if( strcmp(z,"-interactive")==0 ){
11239      stdin_is_interactive = 1;
11240    }else if( strcmp(z,"-batch")==0 ){
11241      stdin_is_interactive = 0;
11242    }else if( strcmp(z,"-heap")==0 ){
11243      i++;
11244    }else if( strcmp(z,"-pagecache")==0 ){
11245      i+=2;
11246    }else if( strcmp(z,"-lookaside")==0 ){
11247      i+=2;
11248    }else if( strcmp(z,"-mmap")==0 ){
11249      i++;
11250    }else if( strcmp(z,"-memtrace")==0 ){
11251      i++;
11252#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11253    }else if( strcmp(z,"-sorterref")==0 ){
11254      i++;
11255#endif
11256    }else if( strcmp(z,"-vfs")==0 ){
11257      i++;
11258#ifdef SQLITE_ENABLE_VFSTRACE
11259    }else if( strcmp(z,"-vfstrace")==0 ){
11260      i++;
11261#endif
11262#ifdef SQLITE_ENABLE_MULTIPLEX
11263    }else if( strcmp(z,"-multiplex")==0 ){
11264      i++;
11265#endif
11266    }else if( strcmp(z,"-help")==0 ){
11267      usage(1);
11268    }else if( strcmp(z,"-cmd")==0 ){
11269      /* Run commands that follow -cmd first and separately from commands
11270      ** that simply appear on the command-line.  This seems goofy.  It would
11271      ** be better if all commands ran in the order that they appear.  But
11272      ** we retain the goofy behavior for historical compatibility. */
11273      if( i==argc-1 ) break;
11274      z = cmdline_option_value(argc,argv,++i);
11275      if( z[0]=='.' ){
11276        rc = do_meta_command(z, &data);
11277        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11278      }else{
11279        open_db(&data, 0);
11280        rc = shell_exec(&data, z, &zErrMsg);
11281        if( zErrMsg!=0 ){
11282          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11283          if( bail_on_error ) return rc!=0 ? rc : 1;
11284        }else if( rc!=0 ){
11285          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11286          if( bail_on_error ) return rc;
11287        }
11288      }
11289#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11290    }else if( strncmp(z, "-A", 2)==0 ){
11291      if( nCmd>0 ){
11292        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11293                            " with \"%s\"\n", z);
11294        return 1;
11295      }
11296      open_db(&data, OPEN_DB_ZIPFILE);
11297      if( z[2] ){
11298        argv[i] = &z[2];
11299        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11300      }else{
11301        arDotCommand(&data, 1, argv+i, argc-i);
11302      }
11303      readStdin = 0;
11304      break;
11305#endif
11306    }else{
11307      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11308      raw_printf(stderr,"Use -help for a list of options.\n");
11309      return 1;
11310    }
11311    data.cMode = data.mode;
11312  }
11313
11314  if( !readStdin ){
11315    /* Run all arguments that do not begin with '-' as if they were separate
11316    ** command-line inputs, except for the argToSkip argument which contains
11317    ** the database filename.
11318    */
11319    for(i=0; i<nCmd; i++){
11320      if( azCmd[i][0]=='.' ){
11321        rc = do_meta_command(azCmd[i], &data);
11322        if( rc ){
11323          free(azCmd);
11324          return rc==2 ? 0 : rc;
11325        }
11326      }else{
11327        open_db(&data, 0);
11328        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11329        if( zErrMsg || rc ){
11330          if( zErrMsg!=0 ){
11331            utf8_printf(stderr,"Error: %s\n", zErrMsg);
11332          }else{
11333            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11334          }
11335          sqlite3_free(zErrMsg);
11336          free(azCmd);
11337          return rc!=0 ? rc : 1;
11338        }
11339      }
11340    }
11341  }else{
11342    /* Run commands received from standard input
11343    */
11344    if( stdin_is_interactive ){
11345      char *zHome;
11346      char *zHistory;
11347      int nHistory;
11348      printf(
11349        "SQLite version %s %.19s\n" /*extra-version-info*/
11350        "Enter \".help\" for usage hints.\n",
11351        sqlite3_libversion(), sqlite3_sourceid()
11352      );
11353      if( warnInmemoryDb ){
11354        printf("Connected to a ");
11355        printBold("transient in-memory database");
11356        printf(".\nUse \".open FILENAME\" to reopen on a "
11357               "persistent database.\n");
11358      }
11359      zHistory = getenv("SQLITE_HISTORY");
11360      if( zHistory ){
11361        zHistory = strdup(zHistory);
11362      }else if( (zHome = find_home_dir(0))!=0 ){
11363        nHistory = strlen30(zHome) + 20;
11364        if( (zHistory = malloc(nHistory))!=0 ){
11365          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11366        }
11367      }
11368      if( zHistory ){ shell_read_history(zHistory); }
11369#if HAVE_READLINE || HAVE_EDITLINE
11370      rl_attempted_completion_function = readline_completion;
11371#elif HAVE_LINENOISE
11372      linenoiseSetCompletionCallback(linenoise_completion);
11373#endif
11374      data.in = 0;
11375      rc = process_input(&data);
11376      if( zHistory ){
11377        shell_stifle_history(2000);
11378        shell_write_history(zHistory);
11379        free(zHistory);
11380      }
11381    }else{
11382      data.in = stdin;
11383      rc = process_input(&data);
11384    }
11385  }
11386  free(azCmd);
11387  set_table_name(&data, 0);
11388  if( data.db ){
11389    session_close_all(&data);
11390    close_db(data.db);
11391  }
11392  sqlite3_free(data.zFreeOnClose);
11393  find_home_dir(1);
11394  output_reset(&data);
11395  data.doXdgOpen = 0;
11396  clearTempFile(&data);
11397#if !SQLITE_SHELL_IS_UTF8
11398  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11399  free(argvToFree);
11400#endif
11401  free(data.colWidth);
11402  /* Clear the global data structure so that valgrind will detect memory
11403  ** leaks */
11404  memset(&data, 0, sizeof(data));
11405  return rc;
11406}
11407