xref: /sqlite-3.40.0/src/shell.c.in (revision d0a05382)
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code.  In place of
5** a legal notice, here is a blessing:
6**
7**    May you do good and not evil.
8**    May you find forgiveness for yourself and forgive others.
9**    May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14*/
15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16/* This needs to come before any includes for MSVC compiler */
17#define _CRT_SECURE_NO_WARNINGS
18#endif
19
20/*
21** Determine if we are dealing with WinRT, which provides only a subset of
22** the full Win32 API.
23*/
24#if !defined(SQLITE_OS_WINRT)
25# define SQLITE_OS_WINRT 0
26#endif
27
28/*
29** Warning pragmas copied from msvc.h in the core.
30*/
31#if defined(_MSC_VER)
32#pragma warning(disable : 4054)
33#pragma warning(disable : 4055)
34#pragma warning(disable : 4100)
35#pragma warning(disable : 4127)
36#pragma warning(disable : 4130)
37#pragma warning(disable : 4152)
38#pragma warning(disable : 4189)
39#pragma warning(disable : 4206)
40#pragma warning(disable : 4210)
41#pragma warning(disable : 4232)
42#pragma warning(disable : 4244)
43#pragma warning(disable : 4305)
44#pragma warning(disable : 4306)
45#pragma warning(disable : 4702)
46#pragma warning(disable : 4706)
47#endif /* defined(_MSC_VER) */
48
49/*
50** No support for loadable extensions in VxWorks.
51*/
52#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
53# define SQLITE_OMIT_LOAD_EXTENSION 1
54#endif
55
56/*
57** Enable large-file support for fopen() and friends on unix.
58*/
59#ifndef SQLITE_DISABLE_LFS
60# define _LARGE_FILE       1
61# ifndef _FILE_OFFSET_BITS
62#   define _FILE_OFFSET_BITS 64
63# endif
64# define _LARGEFILE_SOURCE 1
65#endif
66
67#include <stdlib.h>
68#include <string.h>
69#include <stdio.h>
70#include <assert.h>
71#include "sqlite3.h"
72typedef sqlite3_int64 i64;
73typedef sqlite3_uint64 u64;
74typedef unsigned char u8;
75#if SQLITE_USER_AUTHENTICATION
76# include "sqlite3userauth.h"
77#endif
78#include <ctype.h>
79#include <stdarg.h>
80
81#if !defined(_WIN32) && !defined(WIN32)
82# include <signal.h>
83# if !defined(__RTP__) && !defined(_WRS_KERNEL)
84#  include <pwd.h>
85# endif
86#endif
87#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
88# include <unistd.h>
89# include <dirent.h>
90# define GETPID getpid
91# if defined(__MINGW32__)
92#  define DIRENT dirent
93#  ifndef S_ISLNK
94#   define S_ISLNK(mode) (0)
95#  endif
96# endif
97#else
98# define GETPID (int)GetCurrentProcessId
99#endif
100#include <sys/types.h>
101#include <sys/stat.h>
102
103#if HAVE_READLINE
104# include <readline/readline.h>
105# include <readline/history.h>
106#endif
107
108#if HAVE_EDITLINE
109# include <editline/readline.h>
110#endif
111
112#if HAVE_EDITLINE || HAVE_READLINE
113
114# define shell_add_history(X) add_history(X)
115# define shell_read_history(X) read_history(X)
116# define shell_write_history(X) write_history(X)
117# define shell_stifle_history(X) stifle_history(X)
118# define shell_readline(X) readline(X)
119
120#elif HAVE_LINENOISE
121
122# include "linenoise.h"
123# define shell_add_history(X) linenoiseHistoryAdd(X)
124# define shell_read_history(X) linenoiseHistoryLoad(X)
125# define shell_write_history(X) linenoiseHistorySave(X)
126# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
127# define shell_readline(X) linenoise(X)
128
129#else
130
131# define shell_read_history(X)
132# define shell_write_history(X)
133# define shell_stifle_history(X)
134
135# define SHELL_USE_LOCAL_GETLINE 1
136#endif
137
138
139#if defined(_WIN32) || defined(WIN32)
140# if SQLITE_OS_WINRT
141#  define SQLITE_OMIT_POPEN 1
142# else
143#  include <io.h>
144#  include <fcntl.h>
145#  define isatty(h) _isatty(h)
146#  ifndef access
147#   define access(f,m) _access((f),(m))
148#  endif
149#  ifndef unlink
150#   define unlink _unlink
151#  endif
152#  ifndef strdup
153#   define strdup _strdup
154#  endif
155#  undef popen
156#  define popen _popen
157#  undef pclose
158#  define pclose _pclose
159# endif
160#else
161 /* Make sure isatty() has a prototype. */
162 extern int isatty(int);
163
164# if !defined(__RTP__) && !defined(_WRS_KERNEL)
165  /* popen and pclose are not C89 functions and so are
166  ** sometimes omitted from the <stdio.h> header */
167   extern FILE *popen(const char*,const char*);
168   extern int pclose(FILE*);
169# else
170#  define SQLITE_OMIT_POPEN 1
171# endif
172#endif
173
174#if defined(_WIN32_WCE)
175/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
176 * thus we always assume that we have a console. That can be
177 * overridden with the -batch command line option.
178 */
179#define isatty(x) 1
180#endif
181
182/* ctype macros that work with signed characters */
183#define IsSpace(X)  isspace((unsigned char)X)
184#define IsDigit(X)  isdigit((unsigned char)X)
185#define ToLower(X)  (char)tolower((unsigned char)X)
186
187#if defined(_WIN32) || defined(WIN32)
188#if SQLITE_OS_WINRT
189#include <intrin.h>
190#endif
191#include <windows.h>
192
193/* string conversion routines only needed on Win32 */
194extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
195extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
196extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
197extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
198#endif
199
200/* On Windows, we normally run with output mode of TEXT so that \n characters
201** are automatically translated into \r\n.  However, this behavior needs
202** to be disabled in some cases (ex: when generating CSV output and when
203** rendering quoted strings that contain \n characters).  The following
204** routines take care of that.
205*/
206#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
207static void setBinaryMode(FILE *file, int isOutput){
208  if( isOutput ) fflush(file);
209  _setmode(_fileno(file), _O_BINARY);
210}
211static void setTextMode(FILE *file, int isOutput){
212  if( isOutput ) fflush(file);
213  _setmode(_fileno(file), _O_TEXT);
214}
215#else
216# define setBinaryMode(X,Y)
217# define setTextMode(X,Y)
218#endif
219
220
221/* True if the timer is enabled */
222static int enableTimer = 0;
223
224/* Return the current wall-clock time */
225static sqlite3_int64 timeOfDay(void){
226  static sqlite3_vfs *clockVfs = 0;
227  sqlite3_int64 t;
228  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
229  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
230    clockVfs->xCurrentTimeInt64(clockVfs, &t);
231  }else{
232    double r;
233    clockVfs->xCurrentTime(clockVfs, &r);
234    t = (sqlite3_int64)(r*86400000.0);
235  }
236  return t;
237}
238
239#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
240#include <sys/time.h>
241#include <sys/resource.h>
242
243/* VxWorks does not support getrusage() as far as we can determine */
244#if defined(_WRS_KERNEL) || defined(__RTP__)
245struct rusage {
246  struct timeval ru_utime; /* user CPU time used */
247  struct timeval ru_stime; /* system CPU time used */
248};
249#define getrusage(A,B) memset(B,0,sizeof(*B))
250#endif
251
252/* Saved resource information for the beginning of an operation */
253static struct rusage sBegin;  /* CPU time at start */
254static sqlite3_int64 iBegin;  /* Wall-clock time at start */
255
256/*
257** Begin timing an operation
258*/
259static void beginTimer(void){
260  if( enableTimer ){
261    getrusage(RUSAGE_SELF, &sBegin);
262    iBegin = timeOfDay();
263  }
264}
265
266/* Return the difference of two time_structs in seconds */
267static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
268  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
269         (double)(pEnd->tv_sec - pStart->tv_sec);
270}
271
272/*
273** Print the timing results.
274*/
275static void endTimer(void){
276  if( enableTimer ){
277    sqlite3_int64 iEnd = timeOfDay();
278    struct rusage sEnd;
279    getrusage(RUSAGE_SELF, &sEnd);
280    printf("Run Time: real %.3f user %f sys %f\n",
281       (iEnd - iBegin)*0.001,
282       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
283       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
284  }
285}
286
287#define BEGIN_TIMER beginTimer()
288#define END_TIMER endTimer()
289#define HAS_TIMER 1
290
291#elif (defined(_WIN32) || defined(WIN32))
292
293/* Saved resource information for the beginning of an operation */
294static HANDLE hProcess;
295static FILETIME ftKernelBegin;
296static FILETIME ftUserBegin;
297static sqlite3_int64 ftWallBegin;
298typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
299                                    LPFILETIME, LPFILETIME);
300static GETPROCTIMES getProcessTimesAddr = NULL;
301
302/*
303** Check to see if we have timer support.  Return 1 if necessary
304** support found (or found previously).
305*/
306static int hasTimer(void){
307  if( getProcessTimesAddr ){
308    return 1;
309  } else {
310#if !SQLITE_OS_WINRT
311    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
312    ** versions. See if the version we are running on has it, and if it
313    ** does, save off a pointer to it and the current process handle.
314    */
315    hProcess = GetCurrentProcess();
316    if( hProcess ){
317      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
318      if( NULL != hinstLib ){
319        getProcessTimesAddr =
320            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
321        if( NULL != getProcessTimesAddr ){
322          return 1;
323        }
324        FreeLibrary(hinstLib);
325      }
326    }
327#endif
328  }
329  return 0;
330}
331
332/*
333** Begin timing an operation
334*/
335static void beginTimer(void){
336  if( enableTimer && getProcessTimesAddr ){
337    FILETIME ftCreation, ftExit;
338    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
339                        &ftKernelBegin,&ftUserBegin);
340    ftWallBegin = timeOfDay();
341  }
342}
343
344/* Return the difference of two FILETIME structs in seconds */
345static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
346  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
347  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
348  return (double) ((i64End - i64Start) / 10000000.0);
349}
350
351/*
352** Print the timing results.
353*/
354static void endTimer(void){
355  if( enableTimer && getProcessTimesAddr){
356    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
357    sqlite3_int64 ftWallEnd = timeOfDay();
358    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
359    printf("Run Time: real %.3f user %f sys %f\n",
360       (ftWallEnd - ftWallBegin)*0.001,
361       timeDiff(&ftUserBegin, &ftUserEnd),
362       timeDiff(&ftKernelBegin, &ftKernelEnd));
363  }
364}
365
366#define BEGIN_TIMER beginTimer()
367#define END_TIMER endTimer()
368#define HAS_TIMER hasTimer()
369
370#else
371#define BEGIN_TIMER
372#define END_TIMER
373#define HAS_TIMER 0
374#endif
375
376/*
377** Used to prevent warnings about unused parameters
378*/
379#define UNUSED_PARAMETER(x) (void)(x)
380
381/*
382** Number of elements in an array
383*/
384#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
385
386/*
387** If the following flag is set, then command execution stops
388** at an error if we are not interactive.
389*/
390static int bail_on_error = 0;
391
392/*
393** Threat stdin as an interactive input if the following variable
394** is true.  Otherwise, assume stdin is connected to a file or pipe.
395*/
396static int stdin_is_interactive = 1;
397
398/*
399** On Windows systems we have to know if standard output is a console
400** in order to translate UTF-8 into MBCS.  The following variable is
401** true if translation is required.
402*/
403static int stdout_is_console = 1;
404
405/*
406** The following is the open SQLite database.  We make a pointer
407** to this database a static variable so that it can be accessed
408** by the SIGINT handler to interrupt database processing.
409*/
410static sqlite3 *globalDb = 0;
411
412/*
413** True if an interrupt (Control-C) has been received.
414*/
415static volatile int seenInterrupt = 0;
416
417#ifdef SQLITE_DEBUG
418/*
419** Out-of-memory simulator variables
420*/
421static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
422static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
423static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
424#endif /* SQLITE_DEBUG */
425
426/*
427** This is the name of our program. It is set in main(), used
428** in a number of other places, mostly for error messages.
429*/
430static char *Argv0;
431
432/*
433** Prompt strings. Initialized in main. Settable with
434**   .prompt main continue
435*/
436static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
437static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
438
439/*
440** Render output like fprintf().  Except, if the output is going to the
441** console and if this is running on a Windows machine, translate the
442** output from UTF-8 into MBCS.
443*/
444#if defined(_WIN32) || defined(WIN32)
445void utf8_printf(FILE *out, const char *zFormat, ...){
446  va_list ap;
447  va_start(ap, zFormat);
448  if( stdout_is_console && (out==stdout || out==stderr) ){
449    char *z1 = sqlite3_vmprintf(zFormat, ap);
450    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
451    sqlite3_free(z1);
452    fputs(z2, out);
453    sqlite3_free(z2);
454  }else{
455    vfprintf(out, zFormat, ap);
456  }
457  va_end(ap);
458}
459#elif !defined(utf8_printf)
460# define utf8_printf fprintf
461#endif
462
463/*
464** Render output like fprintf().  This should not be used on anything that
465** includes string formatting (e.g. "%s").
466*/
467#if !defined(raw_printf)
468# define raw_printf fprintf
469#endif
470
471/* Indicate out-of-memory and exit. */
472static void shell_out_of_memory(void){
473  raw_printf(stderr,"Error: out of memory\n");
474  exit(1);
475}
476
477#ifdef SQLITE_DEBUG
478/* This routine is called when a simulated OOM occurs.  It is broken
479** out as a separate routine to make it easy to set a breakpoint on
480** the OOM
481*/
482void shellOomFault(void){
483  if( oomRepeat>0 ){
484    oomRepeat--;
485  }else{
486    oomCounter--;
487  }
488}
489#endif /* SQLITE_DEBUG */
490
491#ifdef SQLITE_DEBUG
492/* This routine is a replacement malloc() that is used to simulate
493** Out-Of-Memory (OOM) errors for testing purposes.
494*/
495static void *oomMalloc(int nByte){
496  if( oomCounter ){
497    if( oomCounter==1 ){
498      shellOomFault();
499      return 0;
500    }else{
501      oomCounter--;
502    }
503  }
504  return defaultMalloc(nByte);
505}
506#endif /* SQLITE_DEBUG */
507
508#ifdef SQLITE_DEBUG
509/* Register the OOM simulator.  This must occur before any memory
510** allocations */
511static void registerOomSimulator(void){
512  sqlite3_mem_methods mem;
513  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
514  defaultMalloc = mem.xMalloc;
515  mem.xMalloc = oomMalloc;
516  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
517}
518#endif
519
520/*
521** Write I/O traces to the following stream.
522*/
523#ifdef SQLITE_ENABLE_IOTRACE
524static FILE *iotrace = 0;
525#endif
526
527/*
528** This routine works like printf in that its first argument is a
529** format string and subsequent arguments are values to be substituted
530** in place of % fields.  The result of formatting this string
531** is written to iotrace.
532*/
533#ifdef SQLITE_ENABLE_IOTRACE
534static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
535  va_list ap;
536  char *z;
537  if( iotrace==0 ) return;
538  va_start(ap, zFormat);
539  z = sqlite3_vmprintf(zFormat, ap);
540  va_end(ap);
541  utf8_printf(iotrace, "%s", z);
542  sqlite3_free(z);
543}
544#endif
545
546/*
547** Output string zUtf to stream pOut as w characters.  If w is negative,
548** then right-justify the text.  W is the width in UTF-8 characters, not
549** in bytes.  This is different from the %*.*s specification in printf
550** since with %*.*s the width is measured in bytes, not characters.
551*/
552static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
553  int i;
554  int n;
555  int aw = w<0 ? -w : w;
556  for(i=n=0; zUtf[i]; i++){
557    if( (zUtf[i]&0xc0)!=0x80 ){
558      n++;
559      if( n==aw ){
560        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
561        break;
562      }
563    }
564  }
565  if( n>=aw ){
566    utf8_printf(pOut, "%.*s", i, zUtf);
567  }else if( w<0 ){
568    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
569  }else{
570    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
571  }
572}
573
574
575/*
576** Determines if a string is a number of not.
577*/
578static int isNumber(const char *z, int *realnum){
579  if( *z=='-' || *z=='+' ) z++;
580  if( !IsDigit(*z) ){
581    return 0;
582  }
583  z++;
584  if( realnum ) *realnum = 0;
585  while( IsDigit(*z) ){ z++; }
586  if( *z=='.' ){
587    z++;
588    if( !IsDigit(*z) ) return 0;
589    while( IsDigit(*z) ){ z++; }
590    if( realnum ) *realnum = 1;
591  }
592  if( *z=='e' || *z=='E' ){
593    z++;
594    if( *z=='+' || *z=='-' ) z++;
595    if( !IsDigit(*z) ) return 0;
596    while( IsDigit(*z) ){ z++; }
597    if( realnum ) *realnum = 1;
598  }
599  return *z==0;
600}
601
602/*
603** Compute a string length that is limited to what can be stored in
604** lower 30 bits of a 32-bit signed integer.
605*/
606static int strlen30(const char *z){
607  const char *z2 = z;
608  while( *z2 ){ z2++; }
609  return 0x3fffffff & (int)(z2 - z);
610}
611
612/*
613** Return the length of a string in characters.  Multibyte UTF8 characters
614** count as a single character.
615*/
616static int strlenChar(const char *z){
617  int n = 0;
618  while( *z ){
619    if( (0xc0&*(z++))!=0x80 ) n++;
620  }
621  return n;
622}
623
624/*
625** Return true if zFile does not exist or if it is not an ordinary file.
626*/
627#ifdef _WIN32
628# define notNormalFile(X) 0
629#else
630static int notNormalFile(const char *zFile){
631  struct stat x;
632  int rc;
633  memset(&x, 0, sizeof(x));
634  rc = stat(zFile, &x);
635  return rc || !S_ISREG(x.st_mode);
636}
637#endif
638
639/*
640** This routine reads a line of text from FILE in, stores
641** the text in memory obtained from malloc() and returns a pointer
642** to the text.  NULL is returned at end of file, or if malloc()
643** fails.
644**
645** If zLine is not NULL then it is a malloced buffer returned from
646** a previous call to this routine that may be reused.
647*/
648static char *local_getline(char *zLine, FILE *in){
649  int nLine = zLine==0 ? 0 : 100;
650  int n = 0;
651
652  while( 1 ){
653    if( n+100>nLine ){
654      nLine = nLine*2 + 100;
655      zLine = realloc(zLine, nLine);
656      if( zLine==0 ) shell_out_of_memory();
657    }
658    if( fgets(&zLine[n], nLine - n, in)==0 ){
659      if( n==0 ){
660        free(zLine);
661        return 0;
662      }
663      zLine[n] = 0;
664      break;
665    }
666    while( zLine[n] ) n++;
667    if( n>0 && zLine[n-1]=='\n' ){
668      n--;
669      if( n>0 && zLine[n-1]=='\r' ) n--;
670      zLine[n] = 0;
671      break;
672    }
673  }
674#if defined(_WIN32) || defined(WIN32)
675  /* For interactive input on Windows systems, translate the
676  ** multi-byte characterset characters into UTF-8. */
677  if( stdin_is_interactive && in==stdin ){
678    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
679    if( zTrans ){
680      int nTrans = strlen30(zTrans)+1;
681      if( nTrans>nLine ){
682        zLine = realloc(zLine, nTrans);
683        if( zLine==0 ) shell_out_of_memory();
684      }
685      memcpy(zLine, zTrans, nTrans);
686      sqlite3_free(zTrans);
687    }
688  }
689#endif /* defined(_WIN32) || defined(WIN32) */
690  return zLine;
691}
692
693/*
694** Retrieve a single line of input text.
695**
696** If in==0 then read from standard input and prompt before each line.
697** If isContinuation is true, then a continuation prompt is appropriate.
698** If isContinuation is zero, then the main prompt should be used.
699**
700** If zPrior is not NULL then it is a buffer from a prior call to this
701** routine that can be reused.
702**
703** The result is stored in space obtained from malloc() and must either
704** be freed by the caller or else passed back into this routine via the
705** zPrior argument for reuse.
706*/
707static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
708  char *zPrompt;
709  char *zResult;
710  if( in!=0 ){
711    zResult = local_getline(zPrior, in);
712  }else{
713    zPrompt = isContinuation ? continuePrompt : mainPrompt;
714#if SHELL_USE_LOCAL_GETLINE
715    printf("%s", zPrompt);
716    fflush(stdout);
717    zResult = local_getline(zPrior, stdin);
718#else
719    free(zPrior);
720    zResult = shell_readline(zPrompt);
721    if( zResult && *zResult ) shell_add_history(zResult);
722#endif
723  }
724  return zResult;
725}
726
727
728/*
729** Return the value of a hexadecimal digit.  Return -1 if the input
730** is not a hex digit.
731*/
732static int hexDigitValue(char c){
733  if( c>='0' && c<='9' ) return c - '0';
734  if( c>='a' && c<='f' ) return c - 'a' + 10;
735  if( c>='A' && c<='F' ) return c - 'A' + 10;
736  return -1;
737}
738
739/*
740** Interpret zArg as an integer value, possibly with suffixes.
741*/
742static sqlite3_int64 integerValue(const char *zArg){
743  sqlite3_int64 v = 0;
744  static const struct { char *zSuffix; int iMult; } aMult[] = {
745    { "KiB", 1024 },
746    { "MiB", 1024*1024 },
747    { "GiB", 1024*1024*1024 },
748    { "KB",  1000 },
749    { "MB",  1000000 },
750    { "GB",  1000000000 },
751    { "K",   1000 },
752    { "M",   1000000 },
753    { "G",   1000000000 },
754  };
755  int i;
756  int isNeg = 0;
757  if( zArg[0]=='-' ){
758    isNeg = 1;
759    zArg++;
760  }else if( zArg[0]=='+' ){
761    zArg++;
762  }
763  if( zArg[0]=='0' && zArg[1]=='x' ){
764    int x;
765    zArg += 2;
766    while( (x = hexDigitValue(zArg[0]))>=0 ){
767      v = (v<<4) + x;
768      zArg++;
769    }
770  }else{
771    while( IsDigit(zArg[0]) ){
772      v = v*10 + zArg[0] - '0';
773      zArg++;
774    }
775  }
776  for(i=0; i<ArraySize(aMult); i++){
777    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
778      v *= aMult[i].iMult;
779      break;
780    }
781  }
782  return isNeg? -v : v;
783}
784
785/*
786** A variable length string to which one can append text.
787*/
788typedef struct ShellText ShellText;
789struct ShellText {
790  char *z;
791  int n;
792  int nAlloc;
793};
794
795/*
796** Initialize and destroy a ShellText object
797*/
798static void initText(ShellText *p){
799  memset(p, 0, sizeof(*p));
800}
801static void freeText(ShellText *p){
802  free(p->z);
803  initText(p);
804}
805
806/* zIn is either a pointer to a NULL-terminated string in memory obtained
807** from malloc(), or a NULL pointer. The string pointed to by zAppend is
808** added to zIn, and the result returned in memory obtained from malloc().
809** zIn, if it was not NULL, is freed.
810**
811** If the third argument, quote, is not '\0', then it is used as a
812** quote character for zAppend.
813*/
814static void appendText(ShellText *p, char const *zAppend, char quote){
815  int len;
816  int i;
817  int nAppend = strlen30(zAppend);
818
819  len = nAppend+p->n+1;
820  if( quote ){
821    len += 2;
822    for(i=0; i<nAppend; i++){
823      if( zAppend[i]==quote ) len++;
824    }
825  }
826
827  if( p->n+len>=p->nAlloc ){
828    p->nAlloc = p->nAlloc*2 + len + 20;
829    p->z = realloc(p->z, p->nAlloc);
830    if( p->z==0 ) shell_out_of_memory();
831  }
832
833  if( quote ){
834    char *zCsr = p->z+p->n;
835    *zCsr++ = quote;
836    for(i=0; i<nAppend; i++){
837      *zCsr++ = zAppend[i];
838      if( zAppend[i]==quote ) *zCsr++ = quote;
839    }
840    *zCsr++ = quote;
841    p->n = (int)(zCsr - p->z);
842    *zCsr = '\0';
843  }else{
844    memcpy(p->z+p->n, zAppend, nAppend);
845    p->n += nAppend;
846    p->z[p->n] = '\0';
847  }
848}
849
850/*
851** Attempt to determine if identifier zName needs to be quoted, either
852** because it contains non-alphanumeric characters, or because it is an
853** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
854** that quoting is required.
855**
856** Return '"' if quoting is required.  Return 0 if no quoting is required.
857*/
858static char quoteChar(const char *zName){
859  int i;
860  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
861  for(i=0; zName[i]; i++){
862    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
863  }
864  return sqlite3_keyword_check(zName, i) ? '"' : 0;
865}
866
867/*
868** Construct a fake object name and column list to describe the structure
869** of the view, virtual table, or table valued function zSchema.zName.
870*/
871static char *shellFakeSchema(
872  sqlite3 *db,            /* The database connection containing the vtab */
873  const char *zSchema,    /* Schema of the database holding the vtab */
874  const char *zName       /* The name of the virtual table */
875){
876  sqlite3_stmt *pStmt = 0;
877  char *zSql;
878  ShellText s;
879  char cQuote;
880  char *zDiv = "(";
881  int nRow = 0;
882
883  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
884                         zSchema ? zSchema : "main", zName);
885  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
886  sqlite3_free(zSql);
887  initText(&s);
888  if( zSchema ){
889    cQuote = quoteChar(zSchema);
890    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
891    appendText(&s, zSchema, cQuote);
892    appendText(&s, ".", 0);
893  }
894  cQuote = quoteChar(zName);
895  appendText(&s, zName, cQuote);
896  while( sqlite3_step(pStmt)==SQLITE_ROW ){
897    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
898    nRow++;
899    appendText(&s, zDiv, 0);
900    zDiv = ",";
901    cQuote = quoteChar(zCol);
902    appendText(&s, zCol, cQuote);
903  }
904  appendText(&s, ")", 0);
905  sqlite3_finalize(pStmt);
906  if( nRow==0 ){
907    freeText(&s);
908    s.z = 0;
909  }
910  return s.z;
911}
912
913/*
914** SQL function:  shell_module_schema(X)
915**
916** Return a fake schema for the table-valued function or eponymous virtual
917** table X.
918*/
919static void shellModuleSchema(
920  sqlite3_context *pCtx,
921  int nVal,
922  sqlite3_value **apVal
923){
924  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
925  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
926  UNUSED_PARAMETER(nVal);
927  if( zFake ){
928    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
929                        -1, sqlite3_free);
930    free(zFake);
931  }
932}
933
934/*
935** SQL function:  shell_add_schema(S,X)
936**
937** Add the schema name X to the CREATE statement in S and return the result.
938** Examples:
939**
940**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
941**
942** Also works on
943**
944**    CREATE INDEX
945**    CREATE UNIQUE INDEX
946**    CREATE VIEW
947**    CREATE TRIGGER
948**    CREATE VIRTUAL TABLE
949**
950** This UDF is used by the .schema command to insert the schema name of
951** attached databases into the middle of the sqlite_schema.sql field.
952*/
953static void shellAddSchemaName(
954  sqlite3_context *pCtx,
955  int nVal,
956  sqlite3_value **apVal
957){
958  static const char *aPrefix[] = {
959     "TABLE",
960     "INDEX",
961     "UNIQUE INDEX",
962     "VIEW",
963     "TRIGGER",
964     "VIRTUAL TABLE"
965  };
966  int i = 0;
967  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
968  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
969  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
970  sqlite3 *db = sqlite3_context_db_handle(pCtx);
971  UNUSED_PARAMETER(nVal);
972  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
973    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
974      int n = strlen30(aPrefix[i]);
975      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
976        char *z = 0;
977        char *zFake = 0;
978        if( zSchema ){
979          char cQuote = quoteChar(zSchema);
980          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
981            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
982          }else{
983            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
984          }
985        }
986        if( zName
987         && aPrefix[i][0]=='V'
988         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
989        ){
990          if( z==0 ){
991            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
992          }else{
993            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
994          }
995          free(zFake);
996        }
997        if( z ){
998          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
999          return;
1000        }
1001      }
1002    }
1003  }
1004  sqlite3_result_value(pCtx, apVal[0]);
1005}
1006
1007/*
1008** The source code for several run-time loadable extensions is inserted
1009** below by the ../tool/mkshellc.tcl script.  Before processing that included
1010** code, we need to override some macros to make the included program code
1011** work here in the middle of this regular program.
1012*/
1013#define SQLITE_EXTENSION_INIT1
1014#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1015
1016#if defined(_WIN32) && defined(_MSC_VER)
1017INCLUDE test_windirent.h
1018INCLUDE test_windirent.c
1019#define dirent DIRENT
1020#endif
1021INCLUDE ../ext/misc/shathree.c
1022INCLUDE ../ext/misc/fileio.c
1023INCLUDE ../ext/misc/completion.c
1024INCLUDE ../ext/misc/appendvfs.c
1025INCLUDE ../ext/misc/memtrace.c
1026INCLUDE ../ext/misc/uint.c
1027INCLUDE ../ext/misc/decimal.c
1028INCLUDE ../ext/misc/ieee754.c
1029INCLUDE ../ext/misc/series.c
1030#ifdef SQLITE_HAVE_ZLIB
1031INCLUDE ../ext/misc/zipfile.c
1032INCLUDE ../ext/misc/sqlar.c
1033#endif
1034INCLUDE ../ext/expert/sqlite3expert.h
1035INCLUDE ../ext/expert/sqlite3expert.c
1036
1037#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1038INCLUDE ../ext/misc/dbdata.c
1039#endif
1040
1041#if defined(SQLITE_ENABLE_SESSION)
1042/*
1043** State information for a single open session
1044*/
1045typedef struct OpenSession OpenSession;
1046struct OpenSession {
1047  char *zName;             /* Symbolic name for this session */
1048  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1049  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1050  sqlite3_session *p;      /* The open session */
1051};
1052#endif
1053
1054typedef struct ExpertInfo ExpertInfo;
1055struct ExpertInfo {
1056  sqlite3expert *pExpert;
1057  int bVerbose;
1058};
1059
1060/* A single line in the EQP output */
1061typedef struct EQPGraphRow EQPGraphRow;
1062struct EQPGraphRow {
1063  int iEqpId;           /* ID for this row */
1064  int iParentId;        /* ID of the parent row */
1065  EQPGraphRow *pNext;   /* Next row in sequence */
1066  char zText[1];        /* Text to display for this row */
1067};
1068
1069/* All EQP output is collected into an instance of the following */
1070typedef struct EQPGraph EQPGraph;
1071struct EQPGraph {
1072  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1073  EQPGraphRow *pLast;   /* Last element of the pRow list */
1074  char zPrefix[100];    /* Graph prefix */
1075};
1076
1077/*
1078** State information about the database connection is contained in an
1079** instance of the following structure.
1080*/
1081typedef struct ShellState ShellState;
1082struct ShellState {
1083  sqlite3 *db;           /* The database */
1084  u8 autoExplain;        /* Automatically turn on .explain mode */
1085  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1086  u8 autoEQPtest;        /* autoEQP is in test mode */
1087  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1088  u8 statsOn;            /* True to display memory stats before each finalize */
1089  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1090  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1091  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1092  u8 nEqpLevel;          /* Depth of the EQP output graph */
1093  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1094  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1095  int outCount;          /* Revert to stdout when reaching zero */
1096  int cnt;               /* Number of records displayed so far */
1097  int lineno;            /* Line number of last line read from in */
1098  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1099  FILE *in;              /* Read commands from this stream */
1100  FILE *out;             /* Write results here */
1101  FILE *traceOut;        /* Output for sqlite3_trace() */
1102  int nErr;              /* Number of errors seen */
1103  int mode;              /* An output mode setting */
1104  int modePrior;         /* Saved mode */
1105  int cMode;             /* temporary output mode for the current query */
1106  int normalMode;        /* Output mode before ".explain on" */
1107  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1108  int showHeader;        /* True to show column names in List or Column mode */
1109  int nCheck;            /* Number of ".check" commands run */
1110  unsigned nProgress;    /* Number of progress callbacks encountered */
1111  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1112  unsigned flgProgress;  /* Flags for the progress callback */
1113  unsigned shellFlgs;    /* Various flags */
1114  unsigned priorShFlgs;  /* Saved copy of flags */
1115  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1116  char *zDestTable;      /* Name of destination table when MODE_Insert */
1117  char *zTempFile;       /* Temporary file that might need deleting */
1118  char zTestcase[30];    /* Name of current test case */
1119  char colSeparator[20]; /* Column separator character for several modes */
1120  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1121  char colSepPrior[20];  /* Saved column separator */
1122  char rowSepPrior[20];  /* Saved row separator */
1123  int *colWidth;         /* Requested width of each column in columnar modes */
1124  int *actualWidth;      /* Actual width of each column */
1125  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1126  char nullValue[20];    /* The text to print when a NULL comes back from
1127                         ** the database */
1128  char outfile[FILENAME_MAX]; /* Filename for *out */
1129  const char *zDbFilename;    /* name of the database file */
1130  char *zFreeOnClose;         /* Filename to free when closing */
1131  const char *zVfs;           /* Name of VFS to use */
1132  sqlite3_stmt *pStmt;   /* Current statement if any. */
1133  FILE *pLog;            /* Write log output here */
1134  int *aiIndent;         /* Array of indents used in MODE_Explain */
1135  int nIndent;           /* Size of array aiIndent[] */
1136  int iIndent;           /* Index of current op in aiIndent[] */
1137  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1138#if defined(SQLITE_ENABLE_SESSION)
1139  int nSession;             /* Number of active sessions */
1140  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1141#endif
1142  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1143};
1144
1145
1146/* Allowed values for ShellState.autoEQP
1147*/
1148#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1149#define AUTOEQP_on       1           /* Automatic EQP is on */
1150#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1151#define AUTOEQP_full     3           /* Show full EXPLAIN */
1152
1153/* Allowed values for ShellState.openMode
1154*/
1155#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1156#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1157#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1158#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1159#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1160#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1161#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1162
1163/* Allowed values for ShellState.eTraceType
1164*/
1165#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1166#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1167#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1168
1169/* Bits in the ShellState.flgProgress variable */
1170#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1171#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1172                                   ** callback limit is reached, and for each
1173                                   ** top-level SQL statement */
1174#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1175
1176/*
1177** These are the allowed shellFlgs values
1178*/
1179#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1180#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1181#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1182#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1183#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1184#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1185#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1186#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
1187#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1188#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1189
1190/*
1191** Macros for testing and setting shellFlgs
1192*/
1193#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1194#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1195#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1196
1197/*
1198** These are the allowed modes.
1199*/
1200#define MODE_Line     0  /* One column per line.  Blank line between records */
1201#define MODE_Column   1  /* One record per line in neat columns */
1202#define MODE_List     2  /* One record per line with a separator */
1203#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1204#define MODE_Html     4  /* Generate an XHTML table */
1205#define MODE_Insert   5  /* Generate SQL "insert" statements */
1206#define MODE_Quote    6  /* Quote values as for SQL */
1207#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1208#define MODE_Csv      8  /* Quote strings, numbers are plain */
1209#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1210#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1211#define MODE_Pretty  11  /* Pretty-print schemas */
1212#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1213#define MODE_Json    13  /* Output JSON */
1214#define MODE_Markdown 14 /* Markdown formatting */
1215#define MODE_Table   15  /* MySQL-style table formatting */
1216#define MODE_Box     16  /* Unicode box-drawing characters */
1217
1218static const char *modeDescr[] = {
1219  "line",
1220  "column",
1221  "list",
1222  "semi",
1223  "html",
1224  "insert",
1225  "quote",
1226  "tcl",
1227  "csv",
1228  "explain",
1229  "ascii",
1230  "prettyprint",
1231  "eqp",
1232  "json",
1233  "markdown",
1234  "table",
1235  "box"
1236};
1237
1238/*
1239** These are the column/row/line separators used by the various
1240** import/export modes.
1241*/
1242#define SEP_Column    "|"
1243#define SEP_Row       "\n"
1244#define SEP_Tab       "\t"
1245#define SEP_Space     " "
1246#define SEP_Comma     ","
1247#define SEP_CrLf      "\r\n"
1248#define SEP_Unit      "\x1F"
1249#define SEP_Record    "\x1E"
1250
1251/*
1252** A callback for the sqlite3_log() interface.
1253*/
1254static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1255  ShellState *p = (ShellState*)pArg;
1256  if( p->pLog==0 ) return;
1257  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1258  fflush(p->pLog);
1259}
1260
1261/*
1262** SQL function:  shell_putsnl(X)
1263**
1264** Write the text X to the screen (or whatever output is being directed)
1265** adding a newline at the end, and then return X.
1266*/
1267static void shellPutsFunc(
1268  sqlite3_context *pCtx,
1269  int nVal,
1270  sqlite3_value **apVal
1271){
1272  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1273  (void)nVal;
1274  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1275  sqlite3_result_value(pCtx, apVal[0]);
1276}
1277
1278/*
1279** SQL function:   edit(VALUE)
1280**                 edit(VALUE,EDITOR)
1281**
1282** These steps:
1283**
1284**     (1) Write VALUE into a temporary file.
1285**     (2) Run program EDITOR on that temporary file.
1286**     (3) Read the temporary file back and return its content as the result.
1287**     (4) Delete the temporary file
1288**
1289** If the EDITOR argument is omitted, use the value in the VISUAL
1290** environment variable.  If still there is no EDITOR, through an error.
1291**
1292** Also throw an error if the EDITOR program returns a non-zero exit code.
1293*/
1294#ifndef SQLITE_NOHAVE_SYSTEM
1295static void editFunc(
1296  sqlite3_context *context,
1297  int argc,
1298  sqlite3_value **argv
1299){
1300  const char *zEditor;
1301  char *zTempFile = 0;
1302  sqlite3 *db;
1303  char *zCmd = 0;
1304  int bBin;
1305  int rc;
1306  int hasCRNL = 0;
1307  FILE *f = 0;
1308  sqlite3_int64 sz;
1309  sqlite3_int64 x;
1310  unsigned char *p = 0;
1311
1312  if( argc==2 ){
1313    zEditor = (const char*)sqlite3_value_text(argv[1]);
1314  }else{
1315    zEditor = getenv("VISUAL");
1316  }
1317  if( zEditor==0 ){
1318    sqlite3_result_error(context, "no editor for edit()", -1);
1319    return;
1320  }
1321  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1322    sqlite3_result_error(context, "NULL input to edit()", -1);
1323    return;
1324  }
1325  db = sqlite3_context_db_handle(context);
1326  zTempFile = 0;
1327  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1328  if( zTempFile==0 ){
1329    sqlite3_uint64 r = 0;
1330    sqlite3_randomness(sizeof(r), &r);
1331    zTempFile = sqlite3_mprintf("temp%llx", r);
1332    if( zTempFile==0 ){
1333      sqlite3_result_error_nomem(context);
1334      return;
1335    }
1336  }
1337  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1338  /* When writing the file to be edited, do \n to \r\n conversions on systems
1339  ** that want \r\n line endings */
1340  f = fopen(zTempFile, bBin ? "wb" : "w");
1341  if( f==0 ){
1342    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1343    goto edit_func_end;
1344  }
1345  sz = sqlite3_value_bytes(argv[0]);
1346  if( bBin ){
1347    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1348  }else{
1349    const char *z = (const char*)sqlite3_value_text(argv[0]);
1350    /* Remember whether or not the value originally contained \r\n */
1351    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1352    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1353  }
1354  fclose(f);
1355  f = 0;
1356  if( x!=sz ){
1357    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1358    goto edit_func_end;
1359  }
1360  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1361  if( zCmd==0 ){
1362    sqlite3_result_error_nomem(context);
1363    goto edit_func_end;
1364  }
1365  rc = system(zCmd);
1366  sqlite3_free(zCmd);
1367  if( rc ){
1368    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1369    goto edit_func_end;
1370  }
1371  f = fopen(zTempFile, "rb");
1372  if( f==0 ){
1373    sqlite3_result_error(context,
1374      "edit() cannot reopen temp file after edit", -1);
1375    goto edit_func_end;
1376  }
1377  fseek(f, 0, SEEK_END);
1378  sz = ftell(f);
1379  rewind(f);
1380  p = sqlite3_malloc64( sz+1 );
1381  if( p==0 ){
1382    sqlite3_result_error_nomem(context);
1383    goto edit_func_end;
1384  }
1385  x = fread(p, 1, (size_t)sz, f);
1386  fclose(f);
1387  f = 0;
1388  if( x!=sz ){
1389    sqlite3_result_error(context, "could not read back the whole file", -1);
1390    goto edit_func_end;
1391  }
1392  if( bBin ){
1393    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1394  }else{
1395    sqlite3_int64 i, j;
1396    if( hasCRNL ){
1397      /* If the original contains \r\n then do no conversions back to \n */
1398      j = sz;
1399    }else{
1400      /* If the file did not originally contain \r\n then convert any new
1401      ** \r\n back into \n */
1402      for(i=j=0; i<sz; i++){
1403        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1404        p[j++] = p[i];
1405      }
1406      sz = j;
1407      p[sz] = 0;
1408    }
1409    sqlite3_result_text64(context, (const char*)p, sz,
1410                          sqlite3_free, SQLITE_UTF8);
1411  }
1412  p = 0;
1413
1414edit_func_end:
1415  if( f ) fclose(f);
1416  unlink(zTempFile);
1417  sqlite3_free(zTempFile);
1418  sqlite3_free(p);
1419}
1420#endif /* SQLITE_NOHAVE_SYSTEM */
1421
1422/*
1423** Save or restore the current output mode
1424*/
1425static void outputModePush(ShellState *p){
1426  p->modePrior = p->mode;
1427  p->priorShFlgs = p->shellFlgs;
1428  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1429  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1430}
1431static void outputModePop(ShellState *p){
1432  p->mode = p->modePrior;
1433  p->shellFlgs = p->priorShFlgs;
1434  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1435  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1436}
1437
1438/*
1439** Output the given string as a hex-encoded blob (eg. X'1234' )
1440*/
1441static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1442  int i;
1443  char *zBlob = (char *)pBlob;
1444  raw_printf(out,"X'");
1445  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1446  raw_printf(out,"'");
1447}
1448
1449/*
1450** Find a string that is not found anywhere in z[].  Return a pointer
1451** to that string.
1452**
1453** Try to use zA and zB first.  If both of those are already found in z[]
1454** then make up some string and store it in the buffer zBuf.
1455*/
1456static const char *unused_string(
1457  const char *z,                    /* Result must not appear anywhere in z */
1458  const char *zA, const char *zB,   /* Try these first */
1459  char *zBuf                        /* Space to store a generated string */
1460){
1461  unsigned i = 0;
1462  if( strstr(z, zA)==0 ) return zA;
1463  if( strstr(z, zB)==0 ) return zB;
1464  do{
1465    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1466  }while( strstr(z,zBuf)!=0 );
1467  return zBuf;
1468}
1469
1470/*
1471** Output the given string as a quoted string using SQL quoting conventions.
1472**
1473** See also: output_quoted_escaped_string()
1474*/
1475static void output_quoted_string(FILE *out, const char *z){
1476  int i;
1477  char c;
1478  setBinaryMode(out, 1);
1479  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1480  if( c==0 ){
1481    utf8_printf(out,"'%s'",z);
1482  }else{
1483    raw_printf(out, "'");
1484    while( *z ){
1485      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1486      if( c=='\'' ) i++;
1487      if( i ){
1488        utf8_printf(out, "%.*s", i, z);
1489        z += i;
1490      }
1491      if( c=='\'' ){
1492        raw_printf(out, "'");
1493        continue;
1494      }
1495      if( c==0 ){
1496        break;
1497      }
1498      z++;
1499    }
1500    raw_printf(out, "'");
1501  }
1502  setTextMode(out, 1);
1503}
1504
1505/*
1506** Output the given string as a quoted string using SQL quoting conventions.
1507** Additionallly , escape the "\n" and "\r" characters so that they do not
1508** get corrupted by end-of-line translation facilities in some operating
1509** systems.
1510**
1511** This is like output_quoted_string() but with the addition of the \r\n
1512** escape mechanism.
1513*/
1514static void output_quoted_escaped_string(FILE *out, const char *z){
1515  int i;
1516  char c;
1517  setBinaryMode(out, 1);
1518  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1519  if( c==0 ){
1520    utf8_printf(out,"'%s'",z);
1521  }else{
1522    const char *zNL = 0;
1523    const char *zCR = 0;
1524    int nNL = 0;
1525    int nCR = 0;
1526    char zBuf1[20], zBuf2[20];
1527    for(i=0; z[i]; i++){
1528      if( z[i]=='\n' ) nNL++;
1529      if( z[i]=='\r' ) nCR++;
1530    }
1531    if( nNL ){
1532      raw_printf(out, "replace(");
1533      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1534    }
1535    if( nCR ){
1536      raw_printf(out, "replace(");
1537      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1538    }
1539    raw_printf(out, "'");
1540    while( *z ){
1541      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1542      if( c=='\'' ) i++;
1543      if( i ){
1544        utf8_printf(out, "%.*s", i, z);
1545        z += i;
1546      }
1547      if( c=='\'' ){
1548        raw_printf(out, "'");
1549        continue;
1550      }
1551      if( c==0 ){
1552        break;
1553      }
1554      z++;
1555      if( c=='\n' ){
1556        raw_printf(out, "%s", zNL);
1557        continue;
1558      }
1559      raw_printf(out, "%s", zCR);
1560    }
1561    raw_printf(out, "'");
1562    if( nCR ){
1563      raw_printf(out, ",'%s',char(13))", zCR);
1564    }
1565    if( nNL ){
1566      raw_printf(out, ",'%s',char(10))", zNL);
1567    }
1568  }
1569  setTextMode(out, 1);
1570}
1571
1572/*
1573** Output the given string as a quoted according to C or TCL quoting rules.
1574*/
1575static void output_c_string(FILE *out, const char *z){
1576  unsigned int c;
1577  fputc('"', out);
1578  while( (c = *(z++))!=0 ){
1579    if( c=='\\' ){
1580      fputc(c, out);
1581      fputc(c, out);
1582    }else if( c=='"' ){
1583      fputc('\\', out);
1584      fputc('"', out);
1585    }else if( c=='\t' ){
1586      fputc('\\', out);
1587      fputc('t', out);
1588    }else if( c=='\n' ){
1589      fputc('\\', out);
1590      fputc('n', out);
1591    }else if( c=='\r' ){
1592      fputc('\\', out);
1593      fputc('r', out);
1594    }else if( !isprint(c&0xff) ){
1595      raw_printf(out, "\\%03o", c&0xff);
1596    }else{
1597      fputc(c, out);
1598    }
1599  }
1600  fputc('"', out);
1601}
1602
1603/*
1604** Output the given string as a quoted according to JSON quoting rules.
1605*/
1606static void output_json_string(FILE *out, const char *z, int n){
1607  unsigned int c;
1608  if( n<0 ) n = (int)strlen(z);
1609  fputc('"', out);
1610  while( n-- ){
1611    c = *(z++);
1612    if( c=='\\' || c=='"' ){
1613      fputc('\\', out);
1614      fputc(c, out);
1615    }else if( c<=0x1f ){
1616      fputc('\\', out);
1617      if( c=='\b' ){
1618        fputc('b', out);
1619      }else if( c=='\f' ){
1620        fputc('f', out);
1621      }else if( c=='\n' ){
1622        fputc('n', out);
1623      }else if( c=='\r' ){
1624        fputc('r', out);
1625      }else if( c=='\t' ){
1626        fputc('t', out);
1627      }else{
1628         raw_printf(out, "u%04x",c);
1629      }
1630    }else{
1631      fputc(c, out);
1632    }
1633  }
1634  fputc('"', out);
1635}
1636
1637/*
1638** Output the given string with characters that are special to
1639** HTML escaped.
1640*/
1641static void output_html_string(FILE *out, const char *z){
1642  int i;
1643  if( z==0 ) z = "";
1644  while( *z ){
1645    for(i=0;   z[i]
1646            && z[i]!='<'
1647            && z[i]!='&'
1648            && z[i]!='>'
1649            && z[i]!='\"'
1650            && z[i]!='\'';
1651        i++){}
1652    if( i>0 ){
1653      utf8_printf(out,"%.*s",i,z);
1654    }
1655    if( z[i]=='<' ){
1656      raw_printf(out,"&lt;");
1657    }else if( z[i]=='&' ){
1658      raw_printf(out,"&amp;");
1659    }else if( z[i]=='>' ){
1660      raw_printf(out,"&gt;");
1661    }else if( z[i]=='\"' ){
1662      raw_printf(out,"&quot;");
1663    }else if( z[i]=='\'' ){
1664      raw_printf(out,"&#39;");
1665    }else{
1666      break;
1667    }
1668    z += i + 1;
1669  }
1670}
1671
1672/*
1673** If a field contains any character identified by a 1 in the following
1674** array, then the string must be quoted for CSV.
1675*/
1676static const char needCsvQuote[] = {
1677  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1678  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1679  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1680  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1681  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1682  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1683  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1684  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1685  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1686  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1687  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1688  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1689  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1690  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1691  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1692  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1693};
1694
1695/*
1696** Output a single term of CSV.  Actually, p->colSeparator is used for
1697** the separator, which may or may not be a comma.  p->nullValue is
1698** the null value.  Strings are quoted if necessary.  The separator
1699** is only issued if bSep is true.
1700*/
1701static void output_csv(ShellState *p, const char *z, int bSep){
1702  FILE *out = p->out;
1703  if( z==0 ){
1704    utf8_printf(out,"%s",p->nullValue);
1705  }else{
1706    int i;
1707    int nSep = strlen30(p->colSeparator);
1708    for(i=0; z[i]; i++){
1709      if( needCsvQuote[((unsigned char*)z)[i]]
1710         || (z[i]==p->colSeparator[0] &&
1711             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1712        i = 0;
1713        break;
1714      }
1715    }
1716    if( i==0 ){
1717      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1718      utf8_printf(out, "%s", zQuoted);
1719      sqlite3_free(zQuoted);
1720    }else{
1721      utf8_printf(out, "%s", z);
1722    }
1723  }
1724  if( bSep ){
1725    utf8_printf(p->out, "%s", p->colSeparator);
1726  }
1727}
1728
1729/*
1730** This routine runs when the user presses Ctrl-C
1731*/
1732static void interrupt_handler(int NotUsed){
1733  UNUSED_PARAMETER(NotUsed);
1734  seenInterrupt++;
1735  if( seenInterrupt>2 ) exit(1);
1736  if( globalDb ) sqlite3_interrupt(globalDb);
1737}
1738
1739#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1740/*
1741** This routine runs for console events (e.g. Ctrl-C) on Win32
1742*/
1743static BOOL WINAPI ConsoleCtrlHandler(
1744  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1745){
1746  if( dwCtrlType==CTRL_C_EVENT ){
1747    interrupt_handler(0);
1748    return TRUE;
1749  }
1750  return FALSE;
1751}
1752#endif
1753
1754#ifndef SQLITE_OMIT_AUTHORIZATION
1755/*
1756** When the ".auth ON" is set, the following authorizer callback is
1757** invoked.  It always returns SQLITE_OK.
1758*/
1759static int shellAuth(
1760  void *pClientData,
1761  int op,
1762  const char *zA1,
1763  const char *zA2,
1764  const char *zA3,
1765  const char *zA4
1766){
1767  ShellState *p = (ShellState*)pClientData;
1768  static const char *azAction[] = { 0,
1769     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1770     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1771     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1772     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1773     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1774     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1775     "PRAGMA",               "READ",                 "SELECT",
1776     "TRANSACTION",          "UPDATE",               "ATTACH",
1777     "DETACH",               "ALTER_TABLE",          "REINDEX",
1778     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1779     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1780  };
1781  int i;
1782  const char *az[4];
1783  az[0] = zA1;
1784  az[1] = zA2;
1785  az[2] = zA3;
1786  az[3] = zA4;
1787  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1788  for(i=0; i<4; i++){
1789    raw_printf(p->out, " ");
1790    if( az[i] ){
1791      output_c_string(p->out, az[i]);
1792    }else{
1793      raw_printf(p->out, "NULL");
1794    }
1795  }
1796  raw_printf(p->out, "\n");
1797  return SQLITE_OK;
1798}
1799#endif
1800
1801/*
1802** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1803**
1804** This routine converts some CREATE TABLE statements for shadow tables
1805** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1806*/
1807static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1808  if( z==0 ) return;
1809  if( zTail==0 ) return;
1810  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1811    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1812  }else{
1813    utf8_printf(out, "%s%s", z, zTail);
1814  }
1815}
1816static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1817  char c = z[n];
1818  z[n] = 0;
1819  printSchemaLine(out, z, zTail);
1820  z[n] = c;
1821}
1822
1823/*
1824** Return true if string z[] has nothing but whitespace and comments to the
1825** end of the first line.
1826*/
1827static int wsToEol(const char *z){
1828  int i;
1829  for(i=0; z[i]; i++){
1830    if( z[i]=='\n' ) return 1;
1831    if( IsSpace(z[i]) ) continue;
1832    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1833    return 0;
1834  }
1835  return 1;
1836}
1837
1838/*
1839** Add a new entry to the EXPLAIN QUERY PLAN data
1840*/
1841static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1842  EQPGraphRow *pNew;
1843  int nText = strlen30(zText);
1844  if( p->autoEQPtest ){
1845    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1846  }
1847  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1848  if( pNew==0 ) shell_out_of_memory();
1849  pNew->iEqpId = iEqpId;
1850  pNew->iParentId = p2;
1851  memcpy(pNew->zText, zText, nText+1);
1852  pNew->pNext = 0;
1853  if( p->sGraph.pLast ){
1854    p->sGraph.pLast->pNext = pNew;
1855  }else{
1856    p->sGraph.pRow = pNew;
1857  }
1858  p->sGraph.pLast = pNew;
1859}
1860
1861/*
1862** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1863** in p->sGraph.
1864*/
1865static void eqp_reset(ShellState *p){
1866  EQPGraphRow *pRow, *pNext;
1867  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1868    pNext = pRow->pNext;
1869    sqlite3_free(pRow);
1870  }
1871  memset(&p->sGraph, 0, sizeof(p->sGraph));
1872}
1873
1874/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1875** pOld, or return the first such line if pOld is NULL
1876*/
1877static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1878  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1879  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1880  return pRow;
1881}
1882
1883/* Render a single level of the graph that has iEqpId as its parent.  Called
1884** recursively to render sublevels.
1885*/
1886static void eqp_render_level(ShellState *p, int iEqpId){
1887  EQPGraphRow *pRow, *pNext;
1888  int n = strlen30(p->sGraph.zPrefix);
1889  char *z;
1890  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1891    pNext = eqp_next_row(p, iEqpId, pRow);
1892    z = pRow->zText;
1893    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1894                pNext ? "|--" : "`--", z);
1895    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1896      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1897      eqp_render_level(p, pRow->iEqpId);
1898      p->sGraph.zPrefix[n] = 0;
1899    }
1900  }
1901}
1902
1903/*
1904** Display and reset the EXPLAIN QUERY PLAN data
1905*/
1906static void eqp_render(ShellState *p){
1907  EQPGraphRow *pRow = p->sGraph.pRow;
1908  if( pRow ){
1909    if( pRow->zText[0]=='-' ){
1910      if( pRow->pNext==0 ){
1911        eqp_reset(p);
1912        return;
1913      }
1914      utf8_printf(p->out, "%s\n", pRow->zText+3);
1915      p->sGraph.pRow = pRow->pNext;
1916      sqlite3_free(pRow);
1917    }else{
1918      utf8_printf(p->out, "QUERY PLAN\n");
1919    }
1920    p->sGraph.zPrefix[0] = 0;
1921    eqp_render_level(p, 0);
1922    eqp_reset(p);
1923  }
1924}
1925
1926#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1927/*
1928** Progress handler callback.
1929*/
1930static int progress_handler(void *pClientData) {
1931  ShellState *p = (ShellState*)pClientData;
1932  p->nProgress++;
1933  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1934    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1935    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1936    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1937    return 1;
1938  }
1939  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1940    raw_printf(p->out, "Progress %u\n", p->nProgress);
1941  }
1942  return 0;
1943}
1944#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1945
1946/*
1947** Print N dashes
1948*/
1949static void print_dashes(FILE *out, int N){
1950  const char zDash[] = "--------------------------------------------------";
1951  const int nDash = sizeof(zDash) - 1;
1952  while( N>nDash ){
1953    fputs(zDash, out);
1954    N -= nDash;
1955  }
1956  raw_printf(out, "%.*s", N, zDash);
1957}
1958
1959/*
1960** Print a markdown or table-style row separator using ascii-art
1961*/
1962static void print_row_separator(
1963  ShellState *p,
1964  int nArg,
1965  const char *zSep
1966){
1967  int i;
1968  if( nArg>0 ){
1969    fputs(zSep, p->out);
1970    print_dashes(p->out, p->actualWidth[0]+2);
1971    for(i=1; i<nArg; i++){
1972      fputs(zSep, p->out);
1973      print_dashes(p->out, p->actualWidth[i]+2);
1974    }
1975    fputs(zSep, p->out);
1976  }
1977  fputs("\n", p->out);
1978}
1979
1980/*
1981** This is the callback routine that the shell
1982** invokes for each row of a query result.
1983*/
1984static int shell_callback(
1985  void *pArg,
1986  int nArg,        /* Number of result columns */
1987  char **azArg,    /* Text of each result column */
1988  char **azCol,    /* Column names */
1989  int *aiType      /* Column types.  Might be NULL */
1990){
1991  int i;
1992  ShellState *p = (ShellState*)pArg;
1993
1994  if( azArg==0 ) return 0;
1995  switch( p->cMode ){
1996    case MODE_Line: {
1997      int w = 5;
1998      if( azArg==0 ) break;
1999      for(i=0; i<nArg; i++){
2000        int len = strlen30(azCol[i] ? azCol[i] : "");
2001        if( len>w ) w = len;
2002      }
2003      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2004      for(i=0; i<nArg; i++){
2005        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2006                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2007      }
2008      break;
2009    }
2010    case MODE_Explain: {
2011      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2012      if( nArg>ArraySize(aExplainWidth) ){
2013        nArg = ArraySize(aExplainWidth);
2014      }
2015      if( p->cnt++==0 ){
2016        for(i=0; i<nArg; i++){
2017          int w = aExplainWidth[i];
2018          utf8_width_print(p->out, w, azCol[i]);
2019          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2020        }
2021        for(i=0; i<nArg; i++){
2022          int w = aExplainWidth[i];
2023          print_dashes(p->out, w);
2024          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2025        }
2026      }
2027      if( azArg==0 ) break;
2028      for(i=0; i<nArg; i++){
2029        int w = aExplainWidth[i];
2030        if( azArg[i] && strlenChar(azArg[i])>w ){
2031          w = strlenChar(azArg[i]);
2032        }
2033        if( i==1 && p->aiIndent && p->pStmt ){
2034          if( p->iIndent<p->nIndent ){
2035            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2036          }
2037          p->iIndent++;
2038        }
2039        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2040        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2041      }
2042      break;
2043    }
2044    case MODE_Semi: {   /* .schema and .fullschema output */
2045      printSchemaLine(p->out, azArg[0], ";\n");
2046      break;
2047    }
2048    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2049      char *z;
2050      int j;
2051      int nParen = 0;
2052      char cEnd = 0;
2053      char c;
2054      int nLine = 0;
2055      assert( nArg==1 );
2056      if( azArg[0]==0 ) break;
2057      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2058       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2059      ){
2060        utf8_printf(p->out, "%s;\n", azArg[0]);
2061        break;
2062      }
2063      z = sqlite3_mprintf("%s", azArg[0]);
2064      j = 0;
2065      for(i=0; IsSpace(z[i]); i++){}
2066      for(; (c = z[i])!=0; i++){
2067        if( IsSpace(c) ){
2068          if( z[j-1]=='\r' ) z[j-1] = '\n';
2069          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2070        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2071          j--;
2072        }
2073        z[j++] = c;
2074      }
2075      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2076      z[j] = 0;
2077      if( strlen30(z)>=79 ){
2078        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2079          if( c==cEnd ){
2080            cEnd = 0;
2081          }else if( c=='"' || c=='\'' || c=='`' ){
2082            cEnd = c;
2083          }else if( c=='[' ){
2084            cEnd = ']';
2085          }else if( c=='-' && z[i+1]=='-' ){
2086            cEnd = '\n';
2087          }else if( c=='(' ){
2088            nParen++;
2089          }else if( c==')' ){
2090            nParen--;
2091            if( nLine>0 && nParen==0 && j>0 ){
2092              printSchemaLineN(p->out, z, j, "\n");
2093              j = 0;
2094            }
2095          }
2096          z[j++] = c;
2097          if( nParen==1 && cEnd==0
2098           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2099          ){
2100            if( c=='\n' ) j--;
2101            printSchemaLineN(p->out, z, j, "\n  ");
2102            j = 0;
2103            nLine++;
2104            while( IsSpace(z[i+1]) ){ i++; }
2105          }
2106        }
2107        z[j] = 0;
2108      }
2109      printSchemaLine(p->out, z, ";\n");
2110      sqlite3_free(z);
2111      break;
2112    }
2113    case MODE_List: {
2114      if( p->cnt++==0 && p->showHeader ){
2115        for(i=0; i<nArg; i++){
2116          utf8_printf(p->out,"%s%s",azCol[i],
2117                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2118        }
2119      }
2120      if( azArg==0 ) break;
2121      for(i=0; i<nArg; i++){
2122        char *z = azArg[i];
2123        if( z==0 ) z = p->nullValue;
2124        utf8_printf(p->out, "%s", z);
2125        if( i<nArg-1 ){
2126          utf8_printf(p->out, "%s", p->colSeparator);
2127        }else{
2128          utf8_printf(p->out, "%s", p->rowSeparator);
2129        }
2130      }
2131      break;
2132    }
2133    case MODE_Html: {
2134      if( p->cnt++==0 && p->showHeader ){
2135        raw_printf(p->out,"<TR>");
2136        for(i=0; i<nArg; i++){
2137          raw_printf(p->out,"<TH>");
2138          output_html_string(p->out, azCol[i]);
2139          raw_printf(p->out,"</TH>\n");
2140        }
2141        raw_printf(p->out,"</TR>\n");
2142      }
2143      if( azArg==0 ) break;
2144      raw_printf(p->out,"<TR>");
2145      for(i=0; i<nArg; i++){
2146        raw_printf(p->out,"<TD>");
2147        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2148        raw_printf(p->out,"</TD>\n");
2149      }
2150      raw_printf(p->out,"</TR>\n");
2151      break;
2152    }
2153    case MODE_Tcl: {
2154      if( p->cnt++==0 && p->showHeader ){
2155        for(i=0; i<nArg; i++){
2156          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2157          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2158        }
2159        utf8_printf(p->out, "%s", p->rowSeparator);
2160      }
2161      if( azArg==0 ) break;
2162      for(i=0; i<nArg; i++){
2163        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2164        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2165      }
2166      utf8_printf(p->out, "%s", p->rowSeparator);
2167      break;
2168    }
2169    case MODE_Csv: {
2170      setBinaryMode(p->out, 1);
2171      if( p->cnt++==0 && p->showHeader ){
2172        for(i=0; i<nArg; i++){
2173          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2174        }
2175        utf8_printf(p->out, "%s", p->rowSeparator);
2176      }
2177      if( nArg>0 ){
2178        for(i=0; i<nArg; i++){
2179          output_csv(p, azArg[i], i<nArg-1);
2180        }
2181        utf8_printf(p->out, "%s", p->rowSeparator);
2182      }
2183      setTextMode(p->out, 1);
2184      break;
2185    }
2186    case MODE_Insert: {
2187      if( azArg==0 ) break;
2188      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2189      if( p->showHeader ){
2190        raw_printf(p->out,"(");
2191        for(i=0; i<nArg; i++){
2192          if( i>0 ) raw_printf(p->out, ",");
2193          if( quoteChar(azCol[i]) ){
2194            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2195            utf8_printf(p->out, "%s", z);
2196            sqlite3_free(z);
2197          }else{
2198            raw_printf(p->out, "%s", azCol[i]);
2199          }
2200        }
2201        raw_printf(p->out,")");
2202      }
2203      p->cnt++;
2204      for(i=0; i<nArg; i++){
2205        raw_printf(p->out, i>0 ? "," : " VALUES(");
2206        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2207          utf8_printf(p->out,"NULL");
2208        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2209          if( ShellHasFlag(p, SHFLG_Newlines) ){
2210            output_quoted_string(p->out, azArg[i]);
2211          }else{
2212            output_quoted_escaped_string(p->out, azArg[i]);
2213          }
2214        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2215          utf8_printf(p->out,"%s", azArg[i]);
2216        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2217          char z[50];
2218          double r = sqlite3_column_double(p->pStmt, i);
2219          sqlite3_uint64 ur;
2220          memcpy(&ur,&r,sizeof(r));
2221          if( ur==0x7ff0000000000000LL ){
2222            raw_printf(p->out, "1e999");
2223          }else if( ur==0xfff0000000000000LL ){
2224            raw_printf(p->out, "-1e999");
2225          }else{
2226            sqlite3_snprintf(50,z,"%!.20g", r);
2227            raw_printf(p->out, "%s", z);
2228          }
2229        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2230          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2231          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2232          output_hex_blob(p->out, pBlob, nBlob);
2233        }else if( isNumber(azArg[i], 0) ){
2234          utf8_printf(p->out,"%s", azArg[i]);
2235        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2236          output_quoted_string(p->out, azArg[i]);
2237        }else{
2238          output_quoted_escaped_string(p->out, azArg[i]);
2239        }
2240      }
2241      raw_printf(p->out,");\n");
2242      break;
2243    }
2244    case MODE_Json: {
2245      if( azArg==0 ) break;
2246      if( p->cnt==0 ){
2247        fputs("[{", p->out);
2248      }else{
2249        fputs(",\n{", p->out);
2250      }
2251      p->cnt++;
2252      for(i=0; i<nArg; i++){
2253        output_json_string(p->out, azCol[i], -1);
2254        putc(':', p->out);
2255        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2256          fputs("null",p->out);
2257        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2258          char z[50];
2259          double r = sqlite3_column_double(p->pStmt, i);
2260          sqlite3_uint64 ur;
2261          memcpy(&ur,&r,sizeof(r));
2262          if( ur==0x7ff0000000000000LL ){
2263            raw_printf(p->out, "1e999");
2264          }else if( ur==0xfff0000000000000LL ){
2265            raw_printf(p->out, "-1e999");
2266          }else{
2267            sqlite3_snprintf(50,z,"%!.20g", r);
2268            raw_printf(p->out, "%s", z);
2269          }
2270        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2271          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2272          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2273          output_json_string(p->out, pBlob, nBlob);
2274        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2275          output_json_string(p->out, azArg[i], -1);
2276        }else{
2277          utf8_printf(p->out,"%s", azArg[i]);
2278        }
2279        if( i<nArg-1 ){
2280          putc(',', p->out);
2281        }
2282      }
2283      putc('}', p->out);
2284      break;
2285    }
2286    case MODE_Quote: {
2287      if( azArg==0 ) break;
2288      if( p->cnt==0 && p->showHeader ){
2289        for(i=0; i<nArg; i++){
2290          if( i>0 ) fputs(p->colSeparator, p->out);
2291          output_quoted_string(p->out, azCol[i]);
2292        }
2293        fputs(p->rowSeparator, p->out);
2294      }
2295      p->cnt++;
2296      for(i=0; i<nArg; i++){
2297        if( i>0 ) fputs(p->colSeparator, p->out);
2298        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2299          utf8_printf(p->out,"NULL");
2300        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2301          output_quoted_string(p->out, azArg[i]);
2302        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2303          utf8_printf(p->out,"%s", azArg[i]);
2304        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2305          char z[50];
2306          double r = sqlite3_column_double(p->pStmt, i);
2307          sqlite3_snprintf(50,z,"%!.20g", r);
2308          raw_printf(p->out, "%s", z);
2309        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2310          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2311          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2312          output_hex_blob(p->out, pBlob, nBlob);
2313        }else if( isNumber(azArg[i], 0) ){
2314          utf8_printf(p->out,"%s", azArg[i]);
2315        }else{
2316          output_quoted_string(p->out, azArg[i]);
2317        }
2318      }
2319      fputs(p->rowSeparator, p->out);
2320      break;
2321    }
2322    case MODE_Ascii: {
2323      if( p->cnt++==0 && p->showHeader ){
2324        for(i=0; i<nArg; i++){
2325          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2326          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2327        }
2328        utf8_printf(p->out, "%s", p->rowSeparator);
2329      }
2330      if( azArg==0 ) break;
2331      for(i=0; i<nArg; i++){
2332        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2333        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2334      }
2335      utf8_printf(p->out, "%s", p->rowSeparator);
2336      break;
2337    }
2338    case MODE_EQP: {
2339      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2340      break;
2341    }
2342  }
2343  return 0;
2344}
2345
2346/*
2347** This is the callback routine that the SQLite library
2348** invokes for each row of a query result.
2349*/
2350static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2351  /* since we don't have type info, call the shell_callback with a NULL value */
2352  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2353}
2354
2355/*
2356** This is the callback routine from sqlite3_exec() that appends all
2357** output onto the end of a ShellText object.
2358*/
2359static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2360  ShellText *p = (ShellText*)pArg;
2361  int i;
2362  UNUSED_PARAMETER(az);
2363  if( azArg==0 ) return 0;
2364  if( p->n ) appendText(p, "|", 0);
2365  for(i=0; i<nArg; i++){
2366    if( i ) appendText(p, ",", 0);
2367    if( azArg[i] ) appendText(p, azArg[i], 0);
2368  }
2369  return 0;
2370}
2371
2372/*
2373** Generate an appropriate SELFTEST table in the main database.
2374*/
2375static void createSelftestTable(ShellState *p){
2376  char *zErrMsg = 0;
2377  sqlite3_exec(p->db,
2378    "SAVEPOINT selftest_init;\n"
2379    "CREATE TABLE IF NOT EXISTS selftest(\n"
2380    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2381    "  op TEXT,\n"                   /* Operator:  memo run */
2382    "  cmd TEXT,\n"                  /* Command text */
2383    "  ans TEXT\n"                   /* Desired answer */
2384    ");"
2385    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2386    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2387    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2388    "         'memo','Tests generated by --init');\n"
2389    "INSERT INTO [_shell$self]\n"
2390    "  SELECT 'run',\n"
2391    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2392                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2393    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2394                          "FROM sqlite_schema ORDER BY 2',224));\n"
2395    "INSERT INTO [_shell$self]\n"
2396    "  SELECT 'run',"
2397    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2398    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2399    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2400    "  FROM (\n"
2401    "    SELECT name FROM sqlite_schema\n"
2402    "     WHERE type='table'\n"
2403    "       AND name<>'selftest'\n"
2404    "       AND coalesce(rootpage,0)>0\n"
2405    "  )\n"
2406    " ORDER BY name;\n"
2407    "INSERT INTO [_shell$self]\n"
2408    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2409    "INSERT INTO selftest(tno,op,cmd,ans)"
2410    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2411    "DROP TABLE [_shell$self];"
2412    ,0,0,&zErrMsg);
2413  if( zErrMsg ){
2414    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2415    sqlite3_free(zErrMsg);
2416  }
2417  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2418}
2419
2420
2421/*
2422** Set the destination table field of the ShellState structure to
2423** the name of the table given.  Escape any quote characters in the
2424** table name.
2425*/
2426static void set_table_name(ShellState *p, const char *zName){
2427  int i, n;
2428  char cQuote;
2429  char *z;
2430
2431  if( p->zDestTable ){
2432    free(p->zDestTable);
2433    p->zDestTable = 0;
2434  }
2435  if( zName==0 ) return;
2436  cQuote = quoteChar(zName);
2437  n = strlen30(zName);
2438  if( cQuote ) n += n+2;
2439  z = p->zDestTable = malloc( n+1 );
2440  if( z==0 ) shell_out_of_memory();
2441  n = 0;
2442  if( cQuote ) z[n++] = cQuote;
2443  for(i=0; zName[i]; i++){
2444    z[n++] = zName[i];
2445    if( zName[i]==cQuote ) z[n++] = cQuote;
2446  }
2447  if( cQuote ) z[n++] = cQuote;
2448  z[n] = 0;
2449}
2450
2451
2452/*
2453** Execute a query statement that will generate SQL output.  Print
2454** the result columns, comma-separated, on a line and then add a
2455** semicolon terminator to the end of that line.
2456**
2457** If the number of columns is 1 and that column contains text "--"
2458** then write the semicolon on a separate line.  That way, if a
2459** "--" comment occurs at the end of the statement, the comment
2460** won't consume the semicolon terminator.
2461*/
2462static int run_table_dump_query(
2463  ShellState *p,           /* Query context */
2464  const char *zSelect      /* SELECT statement to extract content */
2465){
2466  sqlite3_stmt *pSelect;
2467  int rc;
2468  int nResult;
2469  int i;
2470  const char *z;
2471  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2472  if( rc!=SQLITE_OK || !pSelect ){
2473    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2474                sqlite3_errmsg(p->db));
2475    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2476    return rc;
2477  }
2478  rc = sqlite3_step(pSelect);
2479  nResult = sqlite3_column_count(pSelect);
2480  while( rc==SQLITE_ROW ){
2481    z = (const char*)sqlite3_column_text(pSelect, 0);
2482    utf8_printf(p->out, "%s", z);
2483    for(i=1; i<nResult; i++){
2484      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2485    }
2486    if( z==0 ) z = "";
2487    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2488    if( z[0] ){
2489      raw_printf(p->out, "\n;\n");
2490    }else{
2491      raw_printf(p->out, ";\n");
2492    }
2493    rc = sqlite3_step(pSelect);
2494  }
2495  rc = sqlite3_finalize(pSelect);
2496  if( rc!=SQLITE_OK ){
2497    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2498                sqlite3_errmsg(p->db));
2499    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2500  }
2501  return rc;
2502}
2503
2504/*
2505** Allocate space and save off current error string.
2506*/
2507static char *save_err_msg(
2508  sqlite3 *db            /* Database to query */
2509){
2510  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2511  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2512  if( zErrMsg ){
2513    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2514  }
2515  return zErrMsg;
2516}
2517
2518#ifdef __linux__
2519/*
2520** Attempt to display I/O stats on Linux using /proc/PID/io
2521*/
2522static void displayLinuxIoStats(FILE *out){
2523  FILE *in;
2524  char z[200];
2525  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2526  in = fopen(z, "rb");
2527  if( in==0 ) return;
2528  while( fgets(z, sizeof(z), in)!=0 ){
2529    static const struct {
2530      const char *zPattern;
2531      const char *zDesc;
2532    } aTrans[] = {
2533      { "rchar: ",                  "Bytes received by read():" },
2534      { "wchar: ",                  "Bytes sent to write():"    },
2535      { "syscr: ",                  "Read() system calls:"      },
2536      { "syscw: ",                  "Write() system calls:"     },
2537      { "read_bytes: ",             "Bytes read from storage:"  },
2538      { "write_bytes: ",            "Bytes written to storage:" },
2539      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2540    };
2541    int i;
2542    for(i=0; i<ArraySize(aTrans); i++){
2543      int n = strlen30(aTrans[i].zPattern);
2544      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2545        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2546        break;
2547      }
2548    }
2549  }
2550  fclose(in);
2551}
2552#endif
2553
2554/*
2555** Display a single line of status using 64-bit values.
2556*/
2557static void displayStatLine(
2558  ShellState *p,            /* The shell context */
2559  char *zLabel,             /* Label for this one line */
2560  char *zFormat,            /* Format for the result */
2561  int iStatusCtrl,          /* Which status to display */
2562  int bReset                /* True to reset the stats */
2563){
2564  sqlite3_int64 iCur = -1;
2565  sqlite3_int64 iHiwtr = -1;
2566  int i, nPercent;
2567  char zLine[200];
2568  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2569  for(i=0, nPercent=0; zFormat[i]; i++){
2570    if( zFormat[i]=='%' ) nPercent++;
2571  }
2572  if( nPercent>1 ){
2573    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2574  }else{
2575    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2576  }
2577  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2578}
2579
2580/*
2581** Display memory stats.
2582*/
2583static int display_stats(
2584  sqlite3 *db,                /* Database to query */
2585  ShellState *pArg,           /* Pointer to ShellState */
2586  int bReset                  /* True to reset the stats */
2587){
2588  int iCur;
2589  int iHiwtr;
2590  FILE *out;
2591  if( pArg==0 || pArg->out==0 ) return 0;
2592  out = pArg->out;
2593
2594  if( pArg->pStmt && (pArg->statsOn & 2) ){
2595    int nCol, i, x;
2596    sqlite3_stmt *pStmt = pArg->pStmt;
2597    char z[100];
2598    nCol = sqlite3_column_count(pStmt);
2599    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2600    for(i=0; i<nCol; i++){
2601      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2602      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2603#ifndef SQLITE_OMIT_DECLTYPE
2604      sqlite3_snprintf(30, z+x, "declared type:");
2605      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2606#endif
2607#ifdef SQLITE_ENABLE_COLUMN_METADATA
2608      sqlite3_snprintf(30, z+x, "database name:");
2609      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2610      sqlite3_snprintf(30, z+x, "table name:");
2611      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2612      sqlite3_snprintf(30, z+x, "origin name:");
2613      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2614#endif
2615    }
2616  }
2617
2618  displayStatLine(pArg, "Memory Used:",
2619     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2620  displayStatLine(pArg, "Number of Outstanding Allocations:",
2621     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2622  if( pArg->shellFlgs & SHFLG_Pagecache ){
2623    displayStatLine(pArg, "Number of Pcache Pages Used:",
2624       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2625  }
2626  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2627     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2628  displayStatLine(pArg, "Largest Allocation:",
2629     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2630  displayStatLine(pArg, "Largest Pcache Allocation:",
2631     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2632#ifdef YYTRACKMAXSTACKDEPTH
2633  displayStatLine(pArg, "Deepest Parser Stack:",
2634     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2635#endif
2636
2637  if( db ){
2638    if( pArg->shellFlgs & SHFLG_Lookaside ){
2639      iHiwtr = iCur = -1;
2640      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2641                        &iCur, &iHiwtr, bReset);
2642      raw_printf(pArg->out,
2643              "Lookaside Slots Used:                %d (max %d)\n",
2644              iCur, iHiwtr);
2645      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2646                        &iCur, &iHiwtr, bReset);
2647      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2648              iHiwtr);
2649      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2650                        &iCur, &iHiwtr, bReset);
2651      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2652              iHiwtr);
2653      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2654                        &iCur, &iHiwtr, bReset);
2655      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2656              iHiwtr);
2657    }
2658    iHiwtr = iCur = -1;
2659    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2660    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2661            iCur);
2662    iHiwtr = iCur = -1;
2663    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2664    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2665    iHiwtr = iCur = -1;
2666    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2667    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2668    iHiwtr = iCur = -1;
2669    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2670    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2671    iHiwtr = iCur = -1;
2672    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2673    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2674    iHiwtr = iCur = -1;
2675    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2676    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2677            iCur);
2678    iHiwtr = iCur = -1;
2679    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2680    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2681            iCur);
2682  }
2683
2684  if( pArg->pStmt ){
2685    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2686                               bReset);
2687    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2688    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2689    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2690    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2691    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2692    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2693    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2694    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2695    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2696    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2697    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2698    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2699    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2700  }
2701
2702#ifdef __linux__
2703  displayLinuxIoStats(pArg->out);
2704#endif
2705
2706  /* Do not remove this machine readable comment: extra-stats-output-here */
2707
2708  return 0;
2709}
2710
2711/*
2712** Display scan stats.
2713*/
2714static void display_scanstats(
2715  sqlite3 *db,                    /* Database to query */
2716  ShellState *pArg                /* Pointer to ShellState */
2717){
2718#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2719  UNUSED_PARAMETER(db);
2720  UNUSED_PARAMETER(pArg);
2721#else
2722  int i, k, n, mx;
2723  raw_printf(pArg->out, "-------- scanstats --------\n");
2724  mx = 0;
2725  for(k=0; k<=mx; k++){
2726    double rEstLoop = 1.0;
2727    for(i=n=0; 1; i++){
2728      sqlite3_stmt *p = pArg->pStmt;
2729      sqlite3_int64 nLoop, nVisit;
2730      double rEst;
2731      int iSid;
2732      const char *zExplain;
2733      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2734        break;
2735      }
2736      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2737      if( iSid>mx ) mx = iSid;
2738      if( iSid!=k ) continue;
2739      if( n==0 ){
2740        rEstLoop = (double)nLoop;
2741        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2742      }
2743      n++;
2744      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2745      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2746      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2747      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2748      rEstLoop *= rEst;
2749      raw_printf(pArg->out,
2750          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2751          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2752      );
2753    }
2754  }
2755  raw_printf(pArg->out, "---------------------------\n");
2756#endif
2757}
2758
2759/*
2760** Parameter azArray points to a zero-terminated array of strings. zStr
2761** points to a single nul-terminated string. Return non-zero if zStr
2762** is equal, according to strcmp(), to any of the strings in the array.
2763** Otherwise, return zero.
2764*/
2765static int str_in_array(const char *zStr, const char **azArray){
2766  int i;
2767  for(i=0; azArray[i]; i++){
2768    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2769  }
2770  return 0;
2771}
2772
2773/*
2774** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2775** and populate the ShellState.aiIndent[] array with the number of
2776** spaces each opcode should be indented before it is output.
2777**
2778** The indenting rules are:
2779**
2780**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2781**       all opcodes that occur between the p2 jump destination and the opcode
2782**       itself by 2 spaces.
2783**
2784**     * For each "Goto", if the jump destination is earlier in the program
2785**       and ends on one of:
2786**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2787**       or if the P1 parameter is one instead of zero,
2788**       then indent all opcodes between the earlier instruction
2789**       and "Goto" by 2 spaces.
2790*/
2791static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2792  const char *zSql;               /* The text of the SQL statement */
2793  const char *z;                  /* Used to check if this is an EXPLAIN */
2794  int *abYield = 0;               /* True if op is an OP_Yield */
2795  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2796  int iOp;                        /* Index of operation in p->aiIndent[] */
2797
2798  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2799  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2800                            "Rewind", 0 };
2801  const char *azGoto[] = { "Goto", 0 };
2802
2803  /* Try to figure out if this is really an EXPLAIN statement. If this
2804  ** cannot be verified, return early.  */
2805  if( sqlite3_column_count(pSql)!=8 ){
2806    p->cMode = p->mode;
2807    return;
2808  }
2809  zSql = sqlite3_sql(pSql);
2810  if( zSql==0 ) return;
2811  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2812  if( sqlite3_strnicmp(z, "explain", 7) ){
2813    p->cMode = p->mode;
2814    return;
2815  }
2816
2817  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2818    int i;
2819    int iAddr = sqlite3_column_int(pSql, 0);
2820    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2821
2822    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2823    ** p2 is an instruction address, set variable p2op to the index of that
2824    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2825    ** the current instruction is part of a sub-program generated by an
2826    ** SQL trigger or foreign key.  */
2827    int p2 = sqlite3_column_int(pSql, 3);
2828    int p2op = (p2 + (iOp-iAddr));
2829
2830    /* Grow the p->aiIndent array as required */
2831    if( iOp>=nAlloc ){
2832      if( iOp==0 ){
2833        /* Do further verfication that this is explain output.  Abort if
2834        ** it is not */
2835        static const char *explainCols[] = {
2836           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2837        int jj;
2838        for(jj=0; jj<ArraySize(explainCols); jj++){
2839          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2840            p->cMode = p->mode;
2841            sqlite3_reset(pSql);
2842            return;
2843          }
2844        }
2845      }
2846      nAlloc += 100;
2847      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2848      if( p->aiIndent==0 ) shell_out_of_memory();
2849      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2850      if( abYield==0 ) shell_out_of_memory();
2851    }
2852    abYield[iOp] = str_in_array(zOp, azYield);
2853    p->aiIndent[iOp] = 0;
2854    p->nIndent = iOp+1;
2855
2856    if( str_in_array(zOp, azNext) ){
2857      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2858    }
2859    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2860     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2861    ){
2862      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2863    }
2864  }
2865
2866  p->iIndent = 0;
2867  sqlite3_free(abYield);
2868  sqlite3_reset(pSql);
2869}
2870
2871/*
2872** Free the array allocated by explain_data_prepare().
2873*/
2874static void explain_data_delete(ShellState *p){
2875  sqlite3_free(p->aiIndent);
2876  p->aiIndent = 0;
2877  p->nIndent = 0;
2878  p->iIndent = 0;
2879}
2880
2881/*
2882** Disable and restore .wheretrace and .selecttrace settings.
2883*/
2884static unsigned int savedSelectTrace;
2885static unsigned int savedWhereTrace;
2886static void disable_debug_trace_modes(void){
2887  unsigned int zero = 0;
2888  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
2889  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
2890  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
2891  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
2892}
2893static void restore_debug_trace_modes(void){
2894  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
2895  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
2896}
2897
2898/* Create the TEMP table used to store parameter bindings */
2899static void bind_table_init(ShellState *p){
2900  int wrSchema = 0;
2901  int defensiveMode = 0;
2902  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2903  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2904  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2905  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2906  sqlite3_exec(p->db,
2907    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2908    "  key TEXT PRIMARY KEY,\n"
2909    "  value ANY\n"
2910    ") WITHOUT ROWID;",
2911    0, 0, 0);
2912  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2913  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2914}
2915
2916/*
2917** Bind parameters on a prepared statement.
2918**
2919** Parameter bindings are taken from a TEMP table of the form:
2920**
2921**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2922**    WITHOUT ROWID;
2923**
2924** No bindings occur if this table does not exist.  The name of the table
2925** begins with "sqlite_" so that it will not collide with ordinary application
2926** tables.  The table must be in the TEMP schema.
2927*/
2928static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2929  int nVar;
2930  int i;
2931  int rc;
2932  sqlite3_stmt *pQ = 0;
2933
2934  nVar = sqlite3_bind_parameter_count(pStmt);
2935  if( nVar==0 ) return;  /* Nothing to do */
2936  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2937                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2938    return; /* Parameter table does not exist */
2939  }
2940  rc = sqlite3_prepare_v2(pArg->db,
2941          "SELECT value FROM temp.sqlite_parameters"
2942          " WHERE key=?1", -1, &pQ, 0);
2943  if( rc || pQ==0 ) return;
2944  for(i=1; i<=nVar; i++){
2945    char zNum[30];
2946    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2947    if( zVar==0 ){
2948      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2949      zVar = zNum;
2950    }
2951    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2952    if( sqlite3_step(pQ)==SQLITE_ROW ){
2953      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2954    }else{
2955      sqlite3_bind_null(pStmt, i);
2956    }
2957    sqlite3_reset(pQ);
2958  }
2959  sqlite3_finalize(pQ);
2960}
2961
2962/*
2963** UTF8 box-drawing characters.  Imagine box lines like this:
2964**
2965**           1
2966**           |
2967**       4 --+-- 2
2968**           |
2969**           3
2970**
2971** Each box characters has between 2 and 4 of the lines leading from
2972** the center.  The characters are here identified by the numbers of
2973** their corresponding lines.
2974*/
2975#define BOX_24   "\342\224\200"  /* U+2500 --- */
2976#define BOX_13   "\342\224\202"  /* U+2502  |  */
2977#define BOX_23   "\342\224\214"  /* U+250c  ,- */
2978#define BOX_34   "\342\224\220"  /* U+2510 -,  */
2979#define BOX_12   "\342\224\224"  /* U+2514  '- */
2980#define BOX_14   "\342\224\230"  /* U+2518 -'  */
2981#define BOX_123  "\342\224\234"  /* U+251c  |- */
2982#define BOX_134  "\342\224\244"  /* U+2524 -|  */
2983#define BOX_234  "\342\224\254"  /* U+252c -,- */
2984#define BOX_124  "\342\224\264"  /* U+2534 -'- */
2985#define BOX_1234 "\342\224\274"  /* U+253c -|- */
2986
2987/* Draw horizontal line N characters long using unicode box
2988** characters
2989*/
2990static void print_box_line(FILE *out, int N){
2991  const char zDash[] =
2992      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
2993      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
2994  const int nDash = sizeof(zDash) - 1;
2995  N *= 3;
2996  while( N>nDash ){
2997    utf8_printf(out, zDash);
2998    N -= nDash;
2999  }
3000  utf8_printf(out, "%.*s", N, zDash);
3001}
3002
3003/*
3004** Draw a horizontal separator for a MODE_Box table.
3005*/
3006static void print_box_row_separator(
3007  ShellState *p,
3008  int nArg,
3009  const char *zSep1,
3010  const char *zSep2,
3011  const char *zSep3
3012){
3013  int i;
3014  if( nArg>0 ){
3015    utf8_printf(p->out, "%s", zSep1);
3016    print_box_line(p->out, p->actualWidth[0]+2);
3017    for(i=1; i<nArg; i++){
3018      utf8_printf(p->out, "%s", zSep2);
3019      print_box_line(p->out, p->actualWidth[i]+2);
3020    }
3021    utf8_printf(p->out, "%s", zSep3);
3022  }
3023  fputs("\n", p->out);
3024}
3025
3026
3027
3028/*
3029** Run a prepared statement and output the result in one of the
3030** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3031** or MODE_Box.
3032**
3033** This is different from ordinary exec_prepared_stmt() in that
3034** it has to run the entire query and gather the results into memory
3035** first, in order to determine column widths, before providing
3036** any output.
3037*/
3038static void exec_prepared_stmt_columnar(
3039  ShellState *p,                        /* Pointer to ShellState */
3040  sqlite3_stmt *pStmt                   /* Statment to run */
3041){
3042  sqlite3_int64 nRow = 0;
3043  int nColumn = 0;
3044  char **azData = 0;
3045  sqlite3_int64 nAlloc = 0;
3046  const char *z;
3047  int rc;
3048  sqlite3_int64 i, nData;
3049  int j, nTotal, w, n;
3050  const char *colSep = 0;
3051  const char *rowSep = 0;
3052
3053  rc = sqlite3_step(pStmt);
3054  if( rc!=SQLITE_ROW ) return;
3055  nColumn = sqlite3_column_count(pStmt);
3056  nAlloc = nColumn*4;
3057  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3058  if( azData==0 ) shell_out_of_memory();
3059  for(i=0; i<nColumn; i++){
3060    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3061  }
3062  do{
3063    if( (nRow+2)*nColumn >= nAlloc ){
3064      nAlloc *= 2;
3065      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3066      if( azData==0 ) shell_out_of_memory();
3067    }
3068    nRow++;
3069    for(i=0; i<nColumn; i++){
3070      z = (const char*)sqlite3_column_text(pStmt,i);
3071      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3072    }
3073  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
3074  if( nColumn>p->nWidth ){
3075    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3076    if( p->colWidth==0 ) shell_out_of_memory();
3077    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3078    p->nWidth = nColumn;
3079    p->actualWidth = &p->colWidth[nColumn];
3080  }
3081  memset(p->actualWidth, 0, nColumn*sizeof(int));
3082  for(i=0; i<nColumn; i++){
3083    w = p->colWidth[i];
3084    if( w<0 ) w = -w;
3085    p->actualWidth[i] = w;
3086  }
3087  nTotal = nColumn*(nRow+1);
3088  for(i=0; i<nTotal; i++){
3089    z = azData[i];
3090    if( z==0 ) z = p->nullValue;
3091    n = strlenChar(z);
3092    j = i%nColumn;
3093    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3094  }
3095  if( seenInterrupt ) goto columnar_end;
3096  switch( p->cMode ){
3097    case MODE_Column: {
3098      colSep = "  ";
3099      rowSep = "\n";
3100      if( p->showHeader ){
3101        for(i=0; i<nColumn; i++){
3102          w = p->actualWidth[i];
3103          if( p->colWidth[i]<0 ) w = -w;
3104          utf8_width_print(p->out, w, azData[i]);
3105          fputs(i==nColumn-1?"\n":"  ", p->out);
3106        }
3107        for(i=0; i<nColumn; i++){
3108          print_dashes(p->out, p->actualWidth[i]);
3109          fputs(i==nColumn-1?"\n":"  ", p->out);
3110        }
3111      }
3112      break;
3113    }
3114    case MODE_Table: {
3115      colSep = " | ";
3116      rowSep = " |\n";
3117      print_row_separator(p, nColumn, "+");
3118      fputs("| ", p->out);
3119      for(i=0; i<nColumn; i++){
3120        w = p->actualWidth[i];
3121        n = strlenChar(azData[i]);
3122        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3123        fputs(i==nColumn-1?" |\n":" | ", p->out);
3124      }
3125      print_row_separator(p, nColumn, "+");
3126      break;
3127    }
3128    case MODE_Markdown: {
3129      colSep = " | ";
3130      rowSep = " |\n";
3131      fputs("| ", p->out);
3132      for(i=0; i<nColumn; i++){
3133        w = p->actualWidth[i];
3134        n = strlenChar(azData[i]);
3135        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3136        fputs(i==nColumn-1?" |\n":" | ", p->out);
3137      }
3138      print_row_separator(p, nColumn, "|");
3139      break;
3140    }
3141    case MODE_Box: {
3142      colSep = " " BOX_13 " ";
3143      rowSep = " " BOX_13 "\n";
3144      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3145      utf8_printf(p->out, BOX_13 " ");
3146      for(i=0; i<nColumn; i++){
3147        w = p->actualWidth[i];
3148        n = strlenChar(azData[i]);
3149        utf8_printf(p->out, "%*s%s%*s%s",
3150            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3151            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3152      }
3153      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3154      break;
3155    }
3156  }
3157  for(i=nColumn, j=0; i<nTotal; i++, j++){
3158    if( j==0 && p->cMode!=MODE_Column ){
3159      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3160    }
3161    z = azData[i];
3162    if( z==0 ) z = p->nullValue;
3163    w = p->actualWidth[j];
3164    if( p->colWidth[j]<0 ) w = -w;
3165    utf8_width_print(p->out, w, z);
3166    if( j==nColumn-1 ){
3167      utf8_printf(p->out, "%s", rowSep);
3168      j = -1;
3169      if( seenInterrupt ) goto columnar_end;
3170    }else{
3171      utf8_printf(p->out, "%s", colSep);
3172    }
3173  }
3174  if( p->cMode==MODE_Table ){
3175    print_row_separator(p, nColumn, "+");
3176  }else if( p->cMode==MODE_Box ){
3177    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3178  }
3179columnar_end:
3180  if( seenInterrupt ){
3181    utf8_printf(p->out, "Interrupt\n");
3182  }
3183  nData = (nRow+1)*nColumn;
3184  for(i=0; i<nData; i++) free(azData[i]);
3185  sqlite3_free(azData);
3186}
3187
3188/*
3189** Run a prepared statement
3190*/
3191static void exec_prepared_stmt(
3192  ShellState *pArg,                                /* Pointer to ShellState */
3193  sqlite3_stmt *pStmt                              /* Statment to run */
3194){
3195  int rc;
3196
3197  if( pArg->cMode==MODE_Column
3198   || pArg->cMode==MODE_Table
3199   || pArg->cMode==MODE_Box
3200   || pArg->cMode==MODE_Markdown
3201  ){
3202    exec_prepared_stmt_columnar(pArg, pStmt);
3203    return;
3204  }
3205
3206  /* perform the first step.  this will tell us if we
3207  ** have a result set or not and how wide it is.
3208  */
3209  rc = sqlite3_step(pStmt);
3210  /* if we have a result set... */
3211  if( SQLITE_ROW == rc ){
3212    /* allocate space for col name ptr, value ptr, and type */
3213    int nCol = sqlite3_column_count(pStmt);
3214    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3215    if( !pData ){
3216      rc = SQLITE_NOMEM;
3217    }else{
3218      char **azCols = (char **)pData;      /* Names of result columns */
3219      char **azVals = &azCols[nCol];       /* Results */
3220      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3221      int i, x;
3222      assert(sizeof(int) <= sizeof(char *));
3223      /* save off ptrs to column names */
3224      for(i=0; i<nCol; i++){
3225        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3226      }
3227      do{
3228        /* extract the data and data types */
3229        for(i=0; i<nCol; i++){
3230          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3231          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3232            azVals[i] = "";
3233          }else{
3234            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3235          }
3236          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3237            rc = SQLITE_NOMEM;
3238            break; /* from for */
3239          }
3240        } /* end for */
3241
3242        /* if data and types extracted successfully... */
3243        if( SQLITE_ROW == rc ){
3244          /* call the supplied callback with the result row data */
3245          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3246            rc = SQLITE_ABORT;
3247          }else{
3248            rc = sqlite3_step(pStmt);
3249          }
3250        }
3251      } while( SQLITE_ROW == rc );
3252      sqlite3_free(pData);
3253      if( pArg->cMode==MODE_Json ){
3254        fputs("]\n", pArg->out);
3255      }
3256    }
3257  }
3258}
3259
3260#ifndef SQLITE_OMIT_VIRTUALTABLE
3261/*
3262** This function is called to process SQL if the previous shell command
3263** was ".expert". It passes the SQL in the second argument directly to
3264** the sqlite3expert object.
3265**
3266** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3267** code. In this case, (*pzErr) may be set to point to a buffer containing
3268** an English language error message. It is the responsibility of the
3269** caller to eventually free this buffer using sqlite3_free().
3270*/
3271static int expertHandleSQL(
3272  ShellState *pState,
3273  const char *zSql,
3274  char **pzErr
3275){
3276  assert( pState->expert.pExpert );
3277  assert( pzErr==0 || *pzErr==0 );
3278  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3279}
3280
3281/*
3282** This function is called either to silently clean up the object
3283** created by the ".expert" command (if bCancel==1), or to generate a
3284** report from it and then clean it up (if bCancel==0).
3285**
3286** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3287** code. In this case, (*pzErr) may be set to point to a buffer containing
3288** an English language error message. It is the responsibility of the
3289** caller to eventually free this buffer using sqlite3_free().
3290*/
3291static int expertFinish(
3292  ShellState *pState,
3293  int bCancel,
3294  char **pzErr
3295){
3296  int rc = SQLITE_OK;
3297  sqlite3expert *p = pState->expert.pExpert;
3298  assert( p );
3299  assert( bCancel || pzErr==0 || *pzErr==0 );
3300  if( bCancel==0 ){
3301    FILE *out = pState->out;
3302    int bVerbose = pState->expert.bVerbose;
3303
3304    rc = sqlite3_expert_analyze(p, pzErr);
3305    if( rc==SQLITE_OK ){
3306      int nQuery = sqlite3_expert_count(p);
3307      int i;
3308
3309      if( bVerbose ){
3310        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3311        raw_printf(out, "-- Candidates -----------------------------\n");
3312        raw_printf(out, "%s\n", zCand);
3313      }
3314      for(i=0; i<nQuery; i++){
3315        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3316        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3317        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3318        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3319        if( bVerbose ){
3320          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3321          raw_printf(out, "%s\n\n", zSql);
3322        }
3323        raw_printf(out, "%s\n", zIdx);
3324        raw_printf(out, "%s\n", zEQP);
3325      }
3326    }
3327  }
3328  sqlite3_expert_destroy(p);
3329  pState->expert.pExpert = 0;
3330  return rc;
3331}
3332
3333/*
3334** Implementation of ".expert" dot command.
3335*/
3336static int expertDotCommand(
3337  ShellState *pState,             /* Current shell tool state */
3338  char **azArg,                   /* Array of arguments passed to dot command */
3339  int nArg                        /* Number of entries in azArg[] */
3340){
3341  int rc = SQLITE_OK;
3342  char *zErr = 0;
3343  int i;
3344  int iSample = 0;
3345
3346  assert( pState->expert.pExpert==0 );
3347  memset(&pState->expert, 0, sizeof(ExpertInfo));
3348
3349  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3350    char *z = azArg[i];
3351    int n;
3352    if( z[0]=='-' && z[1]=='-' ) z++;
3353    n = strlen30(z);
3354    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3355      pState->expert.bVerbose = 1;
3356    }
3357    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3358      if( i==(nArg-1) ){
3359        raw_printf(stderr, "option requires an argument: %s\n", z);
3360        rc = SQLITE_ERROR;
3361      }else{
3362        iSample = (int)integerValue(azArg[++i]);
3363        if( iSample<0 || iSample>100 ){
3364          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3365          rc = SQLITE_ERROR;
3366        }
3367      }
3368    }
3369    else{
3370      raw_printf(stderr, "unknown option: %s\n", z);
3371      rc = SQLITE_ERROR;
3372    }
3373  }
3374
3375  if( rc==SQLITE_OK ){
3376    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3377    if( pState->expert.pExpert==0 ){
3378      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3379      rc = SQLITE_ERROR;
3380    }else{
3381      sqlite3_expert_config(
3382          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3383      );
3384    }
3385  }
3386
3387  return rc;
3388}
3389#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3390
3391/*
3392** Execute a statement or set of statements.  Print
3393** any result rows/columns depending on the current mode
3394** set via the supplied callback.
3395**
3396** This is very similar to SQLite's built-in sqlite3_exec()
3397** function except it takes a slightly different callback
3398** and callback data argument.
3399*/
3400static int shell_exec(
3401  ShellState *pArg,                         /* Pointer to ShellState */
3402  const char *zSql,                         /* SQL to be evaluated */
3403  char **pzErrMsg                           /* Error msg written here */
3404){
3405  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3406  int rc = SQLITE_OK;             /* Return Code */
3407  int rc2;
3408  const char *zLeftover;          /* Tail of unprocessed SQL */
3409  sqlite3 *db = pArg->db;
3410
3411  if( pzErrMsg ){
3412    *pzErrMsg = NULL;
3413  }
3414
3415#ifndef SQLITE_OMIT_VIRTUALTABLE
3416  if( pArg->expert.pExpert ){
3417    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3418    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3419  }
3420#endif
3421
3422  while( zSql[0] && (SQLITE_OK == rc) ){
3423    static const char *zStmtSql;
3424    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3425    if( SQLITE_OK != rc ){
3426      if( pzErrMsg ){
3427        *pzErrMsg = save_err_msg(db);
3428      }
3429    }else{
3430      if( !pStmt ){
3431        /* this happens for a comment or white-space */
3432        zSql = zLeftover;
3433        while( IsSpace(zSql[0]) ) zSql++;
3434        continue;
3435      }
3436      zStmtSql = sqlite3_sql(pStmt);
3437      if( zStmtSql==0 ) zStmtSql = "";
3438      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3439
3440      /* save off the prepared statment handle and reset row count */
3441      if( pArg ){
3442        pArg->pStmt = pStmt;
3443        pArg->cnt = 0;
3444      }
3445
3446      /* echo the sql statement if echo on */
3447      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3448        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3449      }
3450
3451      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3452      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3453        sqlite3_stmt *pExplain;
3454        char *zEQP;
3455        int triggerEQP = 0;
3456        disable_debug_trace_modes();
3457        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3458        if( pArg->autoEQP>=AUTOEQP_trigger ){
3459          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3460        }
3461        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3462        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3463        if( rc==SQLITE_OK ){
3464          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3465            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3466            int iEqpId = sqlite3_column_int(pExplain, 0);
3467            int iParentId = sqlite3_column_int(pExplain, 1);
3468            if( zEQPLine==0 ) zEQPLine = "";
3469            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3470            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3471          }
3472          eqp_render(pArg);
3473        }
3474        sqlite3_finalize(pExplain);
3475        sqlite3_free(zEQP);
3476        if( pArg->autoEQP>=AUTOEQP_full ){
3477          /* Also do an EXPLAIN for ".eqp full" mode */
3478          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3479          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3480          if( rc==SQLITE_OK ){
3481            pArg->cMode = MODE_Explain;
3482            explain_data_prepare(pArg, pExplain);
3483            exec_prepared_stmt(pArg, pExplain);
3484            explain_data_delete(pArg);
3485          }
3486          sqlite3_finalize(pExplain);
3487          sqlite3_free(zEQP);
3488        }
3489        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3490          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3491          /* Reprepare pStmt before reactiving trace modes */
3492          sqlite3_finalize(pStmt);
3493          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3494          if( pArg ) pArg->pStmt = pStmt;
3495        }
3496        restore_debug_trace_modes();
3497      }
3498
3499      if( pArg ){
3500        pArg->cMode = pArg->mode;
3501        if( pArg->autoExplain ){
3502          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3503            pArg->cMode = MODE_Explain;
3504          }
3505          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3506            pArg->cMode = MODE_EQP;
3507          }
3508        }
3509
3510        /* If the shell is currently in ".explain" mode, gather the extra
3511        ** data required to add indents to the output.*/
3512        if( pArg->cMode==MODE_Explain ){
3513          explain_data_prepare(pArg, pStmt);
3514        }
3515      }
3516
3517      bind_prepared_stmt(pArg, pStmt);
3518      exec_prepared_stmt(pArg, pStmt);
3519      explain_data_delete(pArg);
3520      eqp_render(pArg);
3521
3522      /* print usage stats if stats on */
3523      if( pArg && pArg->statsOn ){
3524        display_stats(db, pArg, 0);
3525      }
3526
3527      /* print loop-counters if required */
3528      if( pArg && pArg->scanstatsOn ){
3529        display_scanstats(db, pArg);
3530      }
3531
3532      /* Finalize the statement just executed. If this fails, save a
3533      ** copy of the error message. Otherwise, set zSql to point to the
3534      ** next statement to execute. */
3535      rc2 = sqlite3_finalize(pStmt);
3536      if( rc!=SQLITE_NOMEM ) rc = rc2;
3537      if( rc==SQLITE_OK ){
3538        zSql = zLeftover;
3539        while( IsSpace(zSql[0]) ) zSql++;
3540      }else if( pzErrMsg ){
3541        *pzErrMsg = save_err_msg(db);
3542      }
3543
3544      /* clear saved stmt handle */
3545      if( pArg ){
3546        pArg->pStmt = NULL;
3547      }
3548    }
3549  } /* end while */
3550
3551  return rc;
3552}
3553
3554/*
3555** Release memory previously allocated by tableColumnList().
3556*/
3557static void freeColumnList(char **azCol){
3558  int i;
3559  for(i=1; azCol[i]; i++){
3560    sqlite3_free(azCol[i]);
3561  }
3562  /* azCol[0] is a static string */
3563  sqlite3_free(azCol);
3564}
3565
3566/*
3567** Return a list of pointers to strings which are the names of all
3568** columns in table zTab.   The memory to hold the names is dynamically
3569** allocated and must be released by the caller using a subsequent call
3570** to freeColumnList().
3571**
3572** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3573** value that needs to be preserved, then azCol[0] is filled in with the
3574** name of the rowid column.
3575**
3576** The first regular column in the table is azCol[1].  The list is terminated
3577** by an entry with azCol[i]==0.
3578*/
3579static char **tableColumnList(ShellState *p, const char *zTab){
3580  char **azCol = 0;
3581  sqlite3_stmt *pStmt;
3582  char *zSql;
3583  int nCol = 0;
3584  int nAlloc = 0;
3585  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3586  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3587  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3588  int rc;
3589
3590  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3591  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3592  sqlite3_free(zSql);
3593  if( rc ) return 0;
3594  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3595    if( nCol>=nAlloc-2 ){
3596      nAlloc = nAlloc*2 + nCol + 10;
3597      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3598      if( azCol==0 ) shell_out_of_memory();
3599    }
3600    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3601    if( sqlite3_column_int(pStmt, 5) ){
3602      nPK++;
3603      if( nPK==1
3604       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3605                          "INTEGER")==0
3606      ){
3607        isIPK = 1;
3608      }else{
3609        isIPK = 0;
3610      }
3611    }
3612  }
3613  sqlite3_finalize(pStmt);
3614  if( azCol==0 ) return 0;
3615  azCol[0] = 0;
3616  azCol[nCol+1] = 0;
3617
3618  /* The decision of whether or not a rowid really needs to be preserved
3619  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3620  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3621  ** rowids on tables where the rowid is inaccessible because there are other
3622  ** columns in the table named "rowid", "_rowid_", and "oid".
3623  */
3624  if( preserveRowid && isIPK ){
3625    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3626    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3627    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3628    ** ROWID aliases.  To distinguish these cases, check to see if
3629    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3630    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3631    */
3632    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3633                           " WHERE origin='pk'", zTab);
3634    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3635    sqlite3_free(zSql);
3636    if( rc ){
3637      freeColumnList(azCol);
3638      return 0;
3639    }
3640    rc = sqlite3_step(pStmt);
3641    sqlite3_finalize(pStmt);
3642    preserveRowid = rc==SQLITE_ROW;
3643  }
3644  if( preserveRowid ){
3645    /* Only preserve the rowid if we can find a name to use for the
3646    ** rowid */
3647    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3648    int i, j;
3649    for(j=0; j<3; j++){
3650      for(i=1; i<=nCol; i++){
3651        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3652      }
3653      if( i>nCol ){
3654        /* At this point, we know that azRowid[j] is not the name of any
3655        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3656        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3657        ** tables will fail this last check */
3658        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3659        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3660        break;
3661      }
3662    }
3663  }
3664  return azCol;
3665}
3666
3667/*
3668** Toggle the reverse_unordered_selects setting.
3669*/
3670static void toggleSelectOrder(sqlite3 *db){
3671  sqlite3_stmt *pStmt = 0;
3672  int iSetting = 0;
3673  char zStmt[100];
3674  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3675  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3676    iSetting = sqlite3_column_int(pStmt, 0);
3677  }
3678  sqlite3_finalize(pStmt);
3679  sqlite3_snprintf(sizeof(zStmt), zStmt,
3680       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3681  sqlite3_exec(db, zStmt, 0, 0, 0);
3682}
3683
3684/*
3685** This is a different callback routine used for dumping the database.
3686** Each row received by this callback consists of a table name,
3687** the table type ("index" or "table") and SQL to create the table.
3688** This routine should print text sufficient to recreate the table.
3689*/
3690static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3691  int rc;
3692  const char *zTable;
3693  const char *zType;
3694  const char *zSql;
3695  ShellState *p = (ShellState *)pArg;
3696  int dataOnly;
3697  int noSys;
3698
3699  UNUSED_PARAMETER(azNotUsed);
3700  if( nArg!=3 || azArg==0 ) return 0;
3701  zTable = azArg[0];
3702  zType = azArg[1];
3703  zSql = azArg[2];
3704  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
3705  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
3706
3707  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
3708    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3709  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
3710    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3711  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3712    return 0;
3713  }else if( dataOnly ){
3714    /* no-op */
3715  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3716    char *zIns;
3717    if( !p->writableSchema ){
3718      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3719      p->writableSchema = 1;
3720    }
3721    zIns = sqlite3_mprintf(
3722       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3723       "VALUES('table','%q','%q',0,'%q');",
3724       zTable, zTable, zSql);
3725    utf8_printf(p->out, "%s\n", zIns);
3726    sqlite3_free(zIns);
3727    return 0;
3728  }else{
3729    printSchemaLine(p->out, zSql, ";\n");
3730  }
3731
3732  if( strcmp(zType, "table")==0 ){
3733    ShellText sSelect;
3734    ShellText sTable;
3735    char **azCol;
3736    int i;
3737    char *savedDestTable;
3738    int savedMode;
3739
3740    azCol = tableColumnList(p, zTable);
3741    if( azCol==0 ){
3742      p->nErr++;
3743      return 0;
3744    }
3745
3746    /* Always quote the table name, even if it appears to be pure ascii,
3747    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3748    initText(&sTable);
3749    appendText(&sTable, zTable, quoteChar(zTable));
3750    /* If preserving the rowid, add a column list after the table name.
3751    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3752    ** instead of the usual "INSERT INTO tab VALUES(...)".
3753    */
3754    if( azCol[0] ){
3755      appendText(&sTable, "(", 0);
3756      appendText(&sTable, azCol[0], 0);
3757      for(i=1; azCol[i]; i++){
3758        appendText(&sTable, ",", 0);
3759        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3760      }
3761      appendText(&sTable, ")", 0);
3762    }
3763
3764    /* Build an appropriate SELECT statement */
3765    initText(&sSelect);
3766    appendText(&sSelect, "SELECT ", 0);
3767    if( azCol[0] ){
3768      appendText(&sSelect, azCol[0], 0);
3769      appendText(&sSelect, ",", 0);
3770    }
3771    for(i=1; azCol[i]; i++){
3772      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3773      if( azCol[i+1] ){
3774        appendText(&sSelect, ",", 0);
3775      }
3776    }
3777    freeColumnList(azCol);
3778    appendText(&sSelect, " FROM ", 0);
3779    appendText(&sSelect, zTable, quoteChar(zTable));
3780
3781    savedDestTable = p->zDestTable;
3782    savedMode = p->mode;
3783    p->zDestTable = sTable.z;
3784    p->mode = p->cMode = MODE_Insert;
3785    rc = shell_exec(p, sSelect.z, 0);
3786    if( (rc&0xff)==SQLITE_CORRUPT ){
3787      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3788      toggleSelectOrder(p->db);
3789      shell_exec(p, sSelect.z, 0);
3790      toggleSelectOrder(p->db);
3791    }
3792    p->zDestTable = savedDestTable;
3793    p->mode = savedMode;
3794    freeText(&sTable);
3795    freeText(&sSelect);
3796    if( rc ) p->nErr++;
3797  }
3798  return 0;
3799}
3800
3801/*
3802** Run zQuery.  Use dump_callback() as the callback routine so that
3803** the contents of the query are output as SQL statements.
3804**
3805** If we get a SQLITE_CORRUPT error, rerun the query after appending
3806** "ORDER BY rowid DESC" to the end.
3807*/
3808static int run_schema_dump_query(
3809  ShellState *p,
3810  const char *zQuery
3811){
3812  int rc;
3813  char *zErr = 0;
3814  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3815  if( rc==SQLITE_CORRUPT ){
3816    char *zQ2;
3817    int len = strlen30(zQuery);
3818    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3819    if( zErr ){
3820      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3821      sqlite3_free(zErr);
3822      zErr = 0;
3823    }
3824    zQ2 = malloc( len+100 );
3825    if( zQ2==0 ) return rc;
3826    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3827    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3828    if( rc ){
3829      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3830    }else{
3831      rc = SQLITE_CORRUPT;
3832    }
3833    sqlite3_free(zErr);
3834    free(zQ2);
3835  }
3836  return rc;
3837}
3838
3839/*
3840** Text of help messages.
3841**
3842** The help text for each individual command begins with a line that starts
3843** with ".".  Subsequent lines are supplimental information.
3844**
3845** There must be two or more spaces between the end of the command and the
3846** start of the description of what that command does.
3847*/
3848static const char *(azHelp[]) = {
3849#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3850  ".archive ...             Manage SQL archives",
3851  "   Each command must have exactly one of the following options:",
3852  "     -c, --create               Create a new archive",
3853  "     -u, --update               Add or update files with changed mtime",
3854  "     -i, --insert               Like -u but always add even if unchanged",
3855  "     -t, --list                 List contents of archive",
3856  "     -x, --extract              Extract files from archive",
3857  "   Optional arguments:",
3858  "     -v, --verbose              Print each filename as it is processed",
3859  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3860  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3861  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3862  "     -n, --dryrun               Show the SQL that would have occurred",
3863  "   Examples:",
3864  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3865  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3866  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3867  "   See also:",
3868  "      http://sqlite.org/cli.html#sqlar_archive_support",
3869#endif
3870#ifndef SQLITE_OMIT_AUTHORIZATION
3871  ".auth ON|OFF             Show authorizer callbacks",
3872#endif
3873  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3874  "       --append            Use the appendvfs",
3875  "       --async             Write to FILE without journal and fsync()",
3876  ".bail on|off             Stop after hitting an error.  Default OFF",
3877  ".binary on|off           Turn binary output on or off.  Default OFF",
3878  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3879  ".changes on|off          Show number of rows changed by SQL",
3880  ".check GLOB              Fail if output since .testcase does not match",
3881  ".clone NEWDB             Clone data into NEWDB from the existing database",
3882  ".databases               List names and files of attached databases",
3883  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3884  ".dbinfo ?DB?             Show status information about the database",
3885  ".dump ?TABLE?            Render database content as SQL",
3886  "   Options:",
3887  "     --data-only            Output only INSERT statements",
3888  "     --newlines             Allow unescaped newline characters in output",
3889  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
3890  "     --preserve-rowids      Include ROWID values in the output",
3891  "   TABLE is a LIKE pattern for the tables to dump",
3892  "   Additional LIKE patterns can be given in subsequent arguments",
3893  ".echo on|off             Turn command echo on or off",
3894  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3895  "   Other Modes:",
3896#ifdef SQLITE_DEBUG
3897  "      test                  Show raw EXPLAIN QUERY PLAN output",
3898  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3899#endif
3900  "      trigger               Like \"full\" but also show trigger bytecode",
3901  ".excel                   Display the output of next command in spreadsheet",
3902  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
3903  ".exit ?CODE?             Exit this program with return-code CODE",
3904  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3905  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3906  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3907  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
3908  "   --help                  Show CMD details",
3909  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3910  ".headers on|off          Turn display of headers on or off",
3911  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3912  ".import FILE TABLE       Import data from FILE into TABLE",
3913  "   Options:",
3914  "     --ascii               Use \\037 and \\036 as column and row separators",
3915  "     --csv                 Use , and \\n as column and row separators",
3916  "     --skip N              Skip the first N rows of input",
3917  "     -v                    \"Verbose\" - increase auxiliary output",
3918  "   Notes:",
3919  "     *  If TABLE does not exist, it is created.  The first row of input",
3920  "        determines the column names.",
3921  "     *  If neither --csv or --ascii are used, the input mode is derived",
3922  "        from the \".mode\" output mode",
3923  "     *  If FILE begins with \"|\" then it is a command that generates the",
3924  "        input text.",
3925#ifndef SQLITE_OMIT_TEST_CONTROL
3926  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3927#endif
3928  ".indexes ?TABLE?         Show names of indexes",
3929  "                           If TABLE is specified, only show indexes for",
3930  "                           tables matching TABLE using the LIKE operator.",
3931#ifdef SQLITE_ENABLE_IOTRACE
3932  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3933#endif
3934  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3935  ".lint OPTIONS            Report potential schema issues.",
3936  "     Options:",
3937  "        fkey-indexes     Find missing foreign key indexes",
3938#ifndef SQLITE_OMIT_LOAD_EXTENSION
3939  ".load FILE ?ENTRY?       Load an extension library",
3940#endif
3941  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3942  ".mode MODE ?TABLE?       Set output mode",
3943  "   MODE is one of:",
3944  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
3945  "     box       Tables using unicode box-drawing characters",
3946  "     csv       Comma-separated values",
3947  "     column    Output in columns.  (See .width)",
3948  "     html      HTML <table> code",
3949  "     insert    SQL insert statements for TABLE",
3950  "     json      Results in a JSON array",
3951  "     line      One value per line",
3952  "     list      Values delimited by \"|\"",
3953  "     markdown  Markdown table format",
3954  "     quote     Escape answers as for SQL",
3955  "     table     ASCII-art table",
3956  "     tabs      Tab-separated values",
3957  "     tcl       TCL list elements",
3958  ".nullvalue STRING        Use STRING in place of NULL values",
3959  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
3960  "     If FILE begins with '|' then open as a pipe",
3961  "       --bom  Put a UTF8 byte-order mark at the beginning",
3962  "       -e     Send output to the system text editor",
3963  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
3964#ifdef SQLITE_DEBUG
3965  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
3966#endif
3967  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3968  "     Options:",
3969  "        --append        Use appendvfs to append database to the end of FILE",
3970#ifdef SQLITE_ENABLE_DESERIALIZE
3971  "        --deserialize   Load into memory useing sqlite3_deserialize()",
3972  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3973  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3974#endif
3975  "        --new           Initialize FILE to an empty database",
3976  "        --nofollow      Do not follow symbolic links",
3977  "        --readonly      Open FILE readonly",
3978  "        --zip           FILE is a ZIP archive",
3979  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
3980  "   If FILE begins with '|' then open it as a pipe.",
3981  "   Options:",
3982  "     --bom                 Prefix output with a UTF8 byte-order mark",
3983  "     -e                    Send output to the system text editor",
3984  "     -x                    Send output as CSV to a spreadsheet",
3985  ".parameter CMD ...       Manage SQL parameter bindings",
3986  "   clear                   Erase all bindings",
3987  "   init                    Initialize the TEMP table that holds bindings",
3988  "   list                    List the current parameter bindings",
3989  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
3990  "                           PARAMETER should start with one of: $ : @ ?",
3991  "   unset PARAMETER         Remove PARAMETER from the binding table",
3992  ".print STRING...         Print literal STRING",
3993#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
3994  ".progress N              Invoke progress handler after every N opcodes",
3995  "   --limit N                 Interrupt after N progress callbacks",
3996  "   --once                    Do no more than one progress interrupt",
3997  "   --quiet|-q                No output except at interrupts",
3998  "   --reset                   Reset the count for each input and interrupt",
3999#endif
4000  ".prompt MAIN CONTINUE    Replace the standard prompts",
4001  ".quit                    Exit this program",
4002  ".read FILE               Read input from FILE",
4003#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4004  ".recover                 Recover as much data as possible from corrupt db.",
4005  "   --freelist-corrupt       Assume the freelist is corrupt",
4006  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4007  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4008  "   --no-rowids              Do not attempt to recover rowid values",
4009  "                            that are not also INTEGER PRIMARY KEYs",
4010#endif
4011  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4012  ".save FILE               Write in-memory database into FILE",
4013  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4014  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4015  "   Options:",
4016  "      --indent             Try to pretty-print the schema",
4017  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4018  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4019  "    Options:",
4020  "       --init               Create a new SELFTEST table",
4021  "       -v                   Verbose output",
4022  ".separator COL ?ROW?     Change the column and row separators",
4023#if defined(SQLITE_ENABLE_SESSION)
4024  ".session ?NAME? CMD ...  Create or control sessions",
4025  "   Subcommands:",
4026  "     attach TABLE             Attach TABLE",
4027  "     changeset FILE           Write a changeset into FILE",
4028  "     close                    Close one session",
4029  "     enable ?BOOLEAN?         Set or query the enable bit",
4030  "     filter GLOB...           Reject tables matching GLOBs",
4031  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4032  "     isempty                  Query whether the session is empty",
4033  "     list                     List currently open session names",
4034  "     open DB NAME             Open a new session on DB",
4035  "     patchset FILE            Write a patchset into FILE",
4036  "   If ?NAME? is omitted, the first defined session is used.",
4037#endif
4038  ".sha3sum ...             Compute a SHA3 hash of database content",
4039  "    Options:",
4040  "      --schema              Also hash the sqlite_schema table",
4041  "      --sha3-224            Use the sha3-224 algorithm",
4042  "      --sha3-256            Use the sha3-256 algorithm (default)",
4043  "      --sha3-384            Use the sha3-384 algorithm",
4044  "      --sha3-512            Use the sha3-512 algorithm",
4045  "    Any other argument is a LIKE pattern for tables to hash",
4046#ifndef SQLITE_NOHAVE_SYSTEM
4047  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4048#endif
4049  ".show                    Show the current values for various settings",
4050  ".stats ?on|off?          Show stats or turn stats on or off",
4051#ifndef SQLITE_NOHAVE_SYSTEM
4052  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4053#endif
4054  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4055  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4056  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4057  "                           Run \".testctrl\" with no arguments for details",
4058  ".timeout MS              Try opening locked tables for MS milliseconds",
4059  ".timer on|off            Turn SQL timer on or off",
4060#ifndef SQLITE_OMIT_TRACE
4061  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4062  "    FILE                    Send output to FILE",
4063  "    stdout                  Send output to stdout",
4064  "    stderr                  Send output to stderr",
4065  "    off                     Disable tracing",
4066  "    --expanded              Expand query parameters",
4067#ifdef SQLITE_ENABLE_NORMALIZE
4068  "    --normalized            Normal the SQL statements",
4069#endif
4070  "    --plain                 Show SQL as it is input",
4071  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4072  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4073  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4074  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4075#endif /* SQLITE_OMIT_TRACE */
4076#ifdef SQLITE_DEBUG
4077  ".unmodule NAME ...       Unregister virtual table modules",
4078  "    --allexcept             Unregister everything except those named",
4079#endif
4080  ".vfsinfo ?AUX?           Information about the top-level VFS",
4081  ".vfslist                 List all available VFSes",
4082  ".vfsname ?AUX?           Print the name of the VFS stack",
4083  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4084  "     Negative values right-justify",
4085};
4086
4087/*
4088** Output help text.
4089**
4090** zPattern describes the set of commands for which help text is provided.
4091** If zPattern is NULL, then show all commands, but only give a one-line
4092** description of each.
4093**
4094** Return the number of matches.
4095*/
4096static int showHelp(FILE *out, const char *zPattern){
4097  int i = 0;
4098  int j = 0;
4099  int n = 0;
4100  char *zPat;
4101  if( zPattern==0
4102   || zPattern[0]=='0'
4103   || strcmp(zPattern,"-a")==0
4104   || strcmp(zPattern,"-all")==0
4105   || strcmp(zPattern,"--all")==0
4106  ){
4107    /* Show all commands, but only one line per command */
4108    if( zPattern==0 ) zPattern = "";
4109    for(i=0; i<ArraySize(azHelp); i++){
4110      if( azHelp[i][0]=='.' || zPattern[0] ){
4111        utf8_printf(out, "%s\n", azHelp[i]);
4112        n++;
4113      }
4114    }
4115  }else{
4116    /* Look for commands that for which zPattern is an exact prefix */
4117    zPat = sqlite3_mprintf(".%s*", zPattern);
4118    for(i=0; i<ArraySize(azHelp); i++){
4119      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4120        utf8_printf(out, "%s\n", azHelp[i]);
4121        j = i+1;
4122        n++;
4123      }
4124    }
4125    sqlite3_free(zPat);
4126    if( n ){
4127      if( n==1 ){
4128        /* when zPattern is a prefix of exactly one command, then include the
4129        ** details of that command, which should begin at offset j */
4130        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4131          utf8_printf(out, "%s\n", azHelp[j]);
4132          j++;
4133        }
4134      }
4135      return n;
4136    }
4137    /* Look for commands that contain zPattern anywhere.  Show the complete
4138    ** text of all commands that match. */
4139    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4140    for(i=0; i<ArraySize(azHelp); i++){
4141      if( azHelp[i][0]=='.' ) j = i;
4142      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4143        utf8_printf(out, "%s\n", azHelp[j]);
4144        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4145          j++;
4146          utf8_printf(out, "%s\n", azHelp[j]);
4147        }
4148        i = j;
4149        n++;
4150      }
4151    }
4152    sqlite3_free(zPat);
4153  }
4154  return n;
4155}
4156
4157/* Forward reference */
4158static int process_input(ShellState *p);
4159
4160/*
4161** Read the content of file zName into memory obtained from sqlite3_malloc64()
4162** and return a pointer to the buffer. The caller is responsible for freeing
4163** the memory.
4164**
4165** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4166** read.
4167**
4168** For convenience, a nul-terminator byte is always appended to the data read
4169** from the file before the buffer is returned. This byte is not included in
4170** the final value of (*pnByte), if applicable.
4171**
4172** NULL is returned if any error is encountered. The final value of *pnByte
4173** is undefined in this case.
4174*/
4175static char *readFile(const char *zName, int *pnByte){
4176  FILE *in = fopen(zName, "rb");
4177  long nIn;
4178  size_t nRead;
4179  char *pBuf;
4180  if( in==0 ) return 0;
4181  fseek(in, 0, SEEK_END);
4182  nIn = ftell(in);
4183  rewind(in);
4184  pBuf = sqlite3_malloc64( nIn+1 );
4185  if( pBuf==0 ){ fclose(in); return 0; }
4186  nRead = fread(pBuf, nIn, 1, in);
4187  fclose(in);
4188  if( nRead!=1 ){
4189    sqlite3_free(pBuf);
4190    return 0;
4191  }
4192  pBuf[nIn] = 0;
4193  if( pnByte ) *pnByte = nIn;
4194  return pBuf;
4195}
4196
4197#if defined(SQLITE_ENABLE_SESSION)
4198/*
4199** Close a single OpenSession object and release all of its associated
4200** resources.
4201*/
4202static void session_close(OpenSession *pSession){
4203  int i;
4204  sqlite3session_delete(pSession->p);
4205  sqlite3_free(pSession->zName);
4206  for(i=0; i<pSession->nFilter; i++){
4207    sqlite3_free(pSession->azFilter[i]);
4208  }
4209  sqlite3_free(pSession->azFilter);
4210  memset(pSession, 0, sizeof(OpenSession));
4211}
4212#endif
4213
4214/*
4215** Close all OpenSession objects and release all associated resources.
4216*/
4217#if defined(SQLITE_ENABLE_SESSION)
4218static void session_close_all(ShellState *p){
4219  int i;
4220  for(i=0; i<p->nSession; i++){
4221    session_close(&p->aSession[i]);
4222  }
4223  p->nSession = 0;
4224}
4225#else
4226# define session_close_all(X)
4227#endif
4228
4229/*
4230** Implementation of the xFilter function for an open session.  Omit
4231** any tables named by ".session filter" but let all other table through.
4232*/
4233#if defined(SQLITE_ENABLE_SESSION)
4234static int session_filter(void *pCtx, const char *zTab){
4235  OpenSession *pSession = (OpenSession*)pCtx;
4236  int i;
4237  for(i=0; i<pSession->nFilter; i++){
4238    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4239  }
4240  return 1;
4241}
4242#endif
4243
4244/*
4245** Try to deduce the type of file for zName based on its content.  Return
4246** one of the SHELL_OPEN_* constants.
4247**
4248** If the file does not exist or is empty but its name looks like a ZIP
4249** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4250** Otherwise, assume an ordinary database regardless of the filename if
4251** the type cannot be determined from content.
4252*/
4253int deduceDatabaseType(const char *zName, int dfltZip){
4254  FILE *f = fopen(zName, "rb");
4255  size_t n;
4256  int rc = SHELL_OPEN_UNSPEC;
4257  char zBuf[100];
4258  if( f==0 ){
4259    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4260       return SHELL_OPEN_ZIPFILE;
4261    }else{
4262       return SHELL_OPEN_NORMAL;
4263    }
4264  }
4265  n = fread(zBuf, 16, 1, f);
4266  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4267    fclose(f);
4268    return SHELL_OPEN_NORMAL;
4269  }
4270  fseek(f, -25, SEEK_END);
4271  n = fread(zBuf, 25, 1, f);
4272  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4273    rc = SHELL_OPEN_APPENDVFS;
4274  }else{
4275    fseek(f, -22, SEEK_END);
4276    n = fread(zBuf, 22, 1, f);
4277    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4278       && zBuf[3]==0x06 ){
4279      rc = SHELL_OPEN_ZIPFILE;
4280    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4281      rc = SHELL_OPEN_ZIPFILE;
4282    }
4283  }
4284  fclose(f);
4285  return rc;
4286}
4287
4288#ifdef SQLITE_ENABLE_DESERIALIZE
4289/*
4290** Reconstruct an in-memory database using the output from the "dbtotxt"
4291** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
4292** is 0, then read from standard input.
4293*/
4294static unsigned char *readHexDb(ShellState *p, int *pnData){
4295  unsigned char *a = 0;
4296  int nLine;
4297  int n = 0;
4298  int pgsz = 0;
4299  int iOffset = 0;
4300  int j, k;
4301  int rc;
4302  FILE *in;
4303  unsigned int x[16];
4304  char zLine[1000];
4305  if( p->zDbFilename ){
4306    in = fopen(p->zDbFilename, "r");
4307    if( in==0 ){
4308      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
4309      return 0;
4310    }
4311    nLine = 0;
4312  }else{
4313    in = p->in;
4314    nLine = p->lineno;
4315    if( in==0 ) in = stdin;
4316  }
4317  *pnData = 0;
4318  nLine++;
4319  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4320  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4321  if( rc!=2 ) goto readHexDb_error;
4322  if( n<0 ) goto readHexDb_error;
4323  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4324  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4325  a = sqlite3_malloc( n ? n : 1 );
4326  if( a==0 ){
4327    utf8_printf(stderr, "Out of memory!\n");
4328    goto readHexDb_error;
4329  }
4330  memset(a, 0, n);
4331  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4332    utf8_printf(stderr, "invalid pagesize\n");
4333    goto readHexDb_error;
4334  }
4335  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4336    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4337    if( rc==2 ){
4338      iOffset = k;
4339      continue;
4340    }
4341    if( strncmp(zLine, "| end ", 6)==0 ){
4342      break;
4343    }
4344    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4345                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4346                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4347    if( rc==17 ){
4348      k = iOffset+j;
4349      if( k+16<=n ){
4350        int ii;
4351        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4352      }
4353    }
4354  }
4355  *pnData = n;
4356  if( in!=p->in ){
4357    fclose(in);
4358  }else{
4359    p->lineno = nLine;
4360  }
4361  return a;
4362
4363readHexDb_error:
4364  if( in!=p->in ){
4365    fclose(in);
4366  }else{
4367    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4368      nLine++;
4369      if(strncmp(zLine, "| end ", 6)==0 ) break;
4370    }
4371    p->lineno = nLine;
4372  }
4373  sqlite3_free(a);
4374  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4375  return 0;
4376}
4377#endif /* SQLITE_ENABLE_DESERIALIZE */
4378
4379/*
4380** Scalar function "shell_int32". The first argument to this function
4381** must be a blob. The second a non-negative integer. This function
4382** reads and returns a 32-bit big-endian integer from byte
4383** offset (4*<arg2>) of the blob.
4384*/
4385static void shellInt32(
4386  sqlite3_context *context,
4387  int argc,
4388  sqlite3_value **argv
4389){
4390  const unsigned char *pBlob;
4391  int nBlob;
4392  int iInt;
4393
4394  UNUSED_PARAMETER(argc);
4395  nBlob = sqlite3_value_bytes(argv[0]);
4396  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4397  iInt = sqlite3_value_int(argv[1]);
4398
4399  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4400    const unsigned char *a = &pBlob[iInt*4];
4401    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4402                       + ((sqlite3_int64)a[1]<<16)
4403                       + ((sqlite3_int64)a[2]<< 8)
4404                       + ((sqlite3_int64)a[3]<< 0);
4405    sqlite3_result_int64(context, iVal);
4406  }
4407}
4408
4409/*
4410** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4411** using "..." with internal double-quote characters doubled.
4412*/
4413static void shellIdQuote(
4414  sqlite3_context *context,
4415  int argc,
4416  sqlite3_value **argv
4417){
4418  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4419  UNUSED_PARAMETER(argc);
4420  if( zName ){
4421    char *z = sqlite3_mprintf("\"%w\"", zName);
4422    sqlite3_result_text(context, z, -1, sqlite3_free);
4423  }
4424}
4425
4426/*
4427** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4428*/
4429static void shellUSleepFunc(
4430  sqlite3_context *context,
4431  int argcUnused,
4432  sqlite3_value **argv
4433){
4434  int sleep = sqlite3_value_int(argv[0]);
4435  (void)argcUnused;
4436  sqlite3_sleep(sleep/1000);
4437  sqlite3_result_int(context, sleep);
4438}
4439
4440/*
4441** Scalar function "shell_escape_crnl" used by the .recover command.
4442** The argument passed to this function is the output of built-in
4443** function quote(). If the first character of the input is "'",
4444** indicating that the value passed to quote() was a text value,
4445** then this function searches the input for "\n" and "\r" characters
4446** and adds a wrapper similar to the following:
4447**
4448**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4449**
4450** Or, if the first character of the input is not "'", then a copy
4451** of the input is returned.
4452*/
4453static void shellEscapeCrnl(
4454  sqlite3_context *context,
4455  int argc,
4456  sqlite3_value **argv
4457){
4458  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4459  UNUSED_PARAMETER(argc);
4460  if( zText[0]=='\'' ){
4461    int nText = sqlite3_value_bytes(argv[0]);
4462    int i;
4463    char zBuf1[20];
4464    char zBuf2[20];
4465    const char *zNL = 0;
4466    const char *zCR = 0;
4467    int nCR = 0;
4468    int nNL = 0;
4469
4470    for(i=0; zText[i]; i++){
4471      if( zNL==0 && zText[i]=='\n' ){
4472        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4473        nNL = (int)strlen(zNL);
4474      }
4475      if( zCR==0 && zText[i]=='\r' ){
4476        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4477        nCR = (int)strlen(zCR);
4478      }
4479    }
4480
4481    if( zNL || zCR ){
4482      int iOut = 0;
4483      i64 nMax = (nNL > nCR) ? nNL : nCR;
4484      i64 nAlloc = nMax * nText + (nMax+64)*2;
4485      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4486      if( zOut==0 ){
4487        sqlite3_result_error_nomem(context);
4488        return;
4489      }
4490
4491      if( zNL && zCR ){
4492        memcpy(&zOut[iOut], "replace(replace(", 16);
4493        iOut += 16;
4494      }else{
4495        memcpy(&zOut[iOut], "replace(", 8);
4496        iOut += 8;
4497      }
4498      for(i=0; zText[i]; i++){
4499        if( zText[i]=='\n' ){
4500          memcpy(&zOut[iOut], zNL, nNL);
4501          iOut += nNL;
4502        }else if( zText[i]=='\r' ){
4503          memcpy(&zOut[iOut], zCR, nCR);
4504          iOut += nCR;
4505        }else{
4506          zOut[iOut] = zText[i];
4507          iOut++;
4508        }
4509      }
4510
4511      if( zNL ){
4512        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4513        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4514        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4515      }
4516      if( zCR ){
4517        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4518        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4519        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4520      }
4521
4522      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4523      sqlite3_free(zOut);
4524      return;
4525    }
4526  }
4527
4528  sqlite3_result_value(context, argv[0]);
4529}
4530
4531/* Flags for open_db().
4532**
4533** The default behavior of open_db() is to exit(1) if the database fails to
4534** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4535** but still returns without calling exit.
4536**
4537** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4538** ZIP archive if the file does not exist or is empty and its name matches
4539** the *.zip pattern.
4540*/
4541#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4542#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4543
4544/*
4545** Make sure the database is open.  If it is not, then open it.  If
4546** the database fails to open, print an error message and exit.
4547*/
4548static void open_db(ShellState *p, int openFlags){
4549  if( p->db==0 ){
4550    if( p->openMode==SHELL_OPEN_UNSPEC ){
4551      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4552        p->openMode = SHELL_OPEN_NORMAL;
4553      }else{
4554        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4555                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4556      }
4557    }
4558    switch( p->openMode ){
4559      case SHELL_OPEN_APPENDVFS: {
4560        sqlite3_open_v2(p->zDbFilename, &p->db,
4561           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4562        break;
4563      }
4564      case SHELL_OPEN_HEXDB:
4565      case SHELL_OPEN_DESERIALIZE: {
4566        sqlite3_open(0, &p->db);
4567        break;
4568      }
4569      case SHELL_OPEN_ZIPFILE: {
4570        sqlite3_open(":memory:", &p->db);
4571        break;
4572      }
4573      case SHELL_OPEN_READONLY: {
4574        sqlite3_open_v2(p->zDbFilename, &p->db,
4575            SQLITE_OPEN_READONLY|p->openFlags, 0);
4576        break;
4577      }
4578      case SHELL_OPEN_UNSPEC:
4579      case SHELL_OPEN_NORMAL: {
4580        sqlite3_open_v2(p->zDbFilename, &p->db,
4581           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4582        break;
4583      }
4584    }
4585    globalDb = p->db;
4586    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4587      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4588          p->zDbFilename, sqlite3_errmsg(p->db));
4589      if( openFlags & OPEN_DB_KEEPALIVE ){
4590        sqlite3_open(":memory:", &p->db);
4591        return;
4592      }
4593      exit(1);
4594    }
4595#ifndef SQLITE_OMIT_LOAD_EXTENSION
4596    sqlite3_enable_load_extension(p->db, 1);
4597#endif
4598    sqlite3_fileio_init(p->db, 0, 0);
4599    sqlite3_shathree_init(p->db, 0, 0);
4600    sqlite3_completion_init(p->db, 0, 0);
4601    sqlite3_uint_init(p->db, 0, 0);
4602    sqlite3_decimal_init(p->db, 0, 0);
4603    sqlite3_ieee_init(p->db, 0, 0);
4604    sqlite3_series_init(p->db, 0, 0);
4605#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4606    sqlite3_dbdata_init(p->db, 0, 0);
4607#endif
4608#ifdef SQLITE_HAVE_ZLIB
4609    sqlite3_zipfile_init(p->db, 0, 0);
4610    sqlite3_sqlar_init(p->db, 0, 0);
4611#endif
4612    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4613                            shellAddSchemaName, 0, 0);
4614    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4615                            shellModuleSchema, 0, 0);
4616    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4617                            shellPutsFunc, 0, 0);
4618    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4619                            shellEscapeCrnl, 0, 0);
4620    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4621                            shellInt32, 0, 0);
4622    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4623                            shellIdQuote, 0, 0);
4624    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
4625                            shellUSleepFunc, 0, 0);
4626#ifndef SQLITE_NOHAVE_SYSTEM
4627    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4628                            editFunc, 0, 0);
4629    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4630                            editFunc, 0, 0);
4631#endif
4632    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4633      char *zSql = sqlite3_mprintf(
4634         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4635      sqlite3_exec(p->db, zSql, 0, 0, 0);
4636      sqlite3_free(zSql);
4637    }
4638#ifdef SQLITE_ENABLE_DESERIALIZE
4639    else
4640    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4641      int rc;
4642      int nData = 0;
4643      unsigned char *aData;
4644      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4645        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4646      }else{
4647        aData = readHexDb(p, &nData);
4648        if( aData==0 ){
4649          return;
4650        }
4651      }
4652      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4653                   SQLITE_DESERIALIZE_RESIZEABLE |
4654                   SQLITE_DESERIALIZE_FREEONCLOSE);
4655      if( rc ){
4656        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4657      }
4658      if( p->szMax>0 ){
4659        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4660      }
4661    }
4662#endif
4663  }
4664}
4665
4666/*
4667** Attempt to close the databaes connection.  Report errors.
4668*/
4669void close_db(sqlite3 *db){
4670  int rc = sqlite3_close(db);
4671  if( rc ){
4672    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4673        rc, sqlite3_errmsg(db));
4674  }
4675}
4676
4677#if HAVE_READLINE || HAVE_EDITLINE
4678/*
4679** Readline completion callbacks
4680*/
4681static char *readline_completion_generator(const char *text, int state){
4682  static sqlite3_stmt *pStmt = 0;
4683  char *zRet;
4684  if( state==0 ){
4685    char *zSql;
4686    sqlite3_finalize(pStmt);
4687    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4688                           "  FROM completion(%Q) ORDER BY 1", text);
4689    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4690    sqlite3_free(zSql);
4691  }
4692  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4693    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4694  }else{
4695    sqlite3_finalize(pStmt);
4696    pStmt = 0;
4697    zRet = 0;
4698  }
4699  return zRet;
4700}
4701static char **readline_completion(const char *zText, int iStart, int iEnd){
4702  rl_attempted_completion_over = 1;
4703  return rl_completion_matches(zText, readline_completion_generator);
4704}
4705
4706#elif HAVE_LINENOISE
4707/*
4708** Linenoise completion callback
4709*/
4710static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4711  int nLine = strlen30(zLine);
4712  int i, iStart;
4713  sqlite3_stmt *pStmt = 0;
4714  char *zSql;
4715  char zBuf[1000];
4716
4717  if( nLine>sizeof(zBuf)-30 ) return;
4718  if( zLine[0]=='.' || zLine[0]=='#') return;
4719  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4720  if( i==nLine-1 ) return;
4721  iStart = i+1;
4722  memcpy(zBuf, zLine, iStart);
4723  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4724                         "  FROM completion(%Q,%Q) ORDER BY 1",
4725                         &zLine[iStart], zLine);
4726  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4727  sqlite3_free(zSql);
4728  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4729  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4730    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4731    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4732    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4733      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4734      linenoiseAddCompletion(lc, zBuf);
4735    }
4736  }
4737  sqlite3_finalize(pStmt);
4738}
4739#endif
4740
4741/*
4742** Do C-language style dequoting.
4743**
4744**    \a    -> alarm
4745**    \b    -> backspace
4746**    \t    -> tab
4747**    \n    -> newline
4748**    \v    -> vertical tab
4749**    \f    -> form feed
4750**    \r    -> carriage return
4751**    \s    -> space
4752**    \"    -> "
4753**    \'    -> '
4754**    \\    -> backslash
4755**    \NNN  -> ascii character NNN in octal
4756*/
4757static void resolve_backslashes(char *z){
4758  int i, j;
4759  char c;
4760  while( *z && *z!='\\' ) z++;
4761  for(i=j=0; (c = z[i])!=0; i++, j++){
4762    if( c=='\\' && z[i+1]!=0 ){
4763      c = z[++i];
4764      if( c=='a' ){
4765        c = '\a';
4766      }else if( c=='b' ){
4767        c = '\b';
4768      }else if( c=='t' ){
4769        c = '\t';
4770      }else if( c=='n' ){
4771        c = '\n';
4772      }else if( c=='v' ){
4773        c = '\v';
4774      }else if( c=='f' ){
4775        c = '\f';
4776      }else if( c=='r' ){
4777        c = '\r';
4778      }else if( c=='"' ){
4779        c = '"';
4780      }else if( c=='\'' ){
4781        c = '\'';
4782      }else if( c=='\\' ){
4783        c = '\\';
4784      }else if( c>='0' && c<='7' ){
4785        c -= '0';
4786        if( z[i+1]>='0' && z[i+1]<='7' ){
4787          i++;
4788          c = (c<<3) + z[i] - '0';
4789          if( z[i+1]>='0' && z[i+1]<='7' ){
4790            i++;
4791            c = (c<<3) + z[i] - '0';
4792          }
4793        }
4794      }
4795    }
4796    z[j] = c;
4797  }
4798  if( j<i ) z[j] = 0;
4799}
4800
4801/*
4802** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4803** for TRUE and FALSE.  Return the integer value if appropriate.
4804*/
4805static int booleanValue(const char *zArg){
4806  int i;
4807  if( zArg[0]=='0' && zArg[1]=='x' ){
4808    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4809  }else{
4810    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4811  }
4812  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4813  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4814    return 1;
4815  }
4816  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4817    return 0;
4818  }
4819  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4820          zArg);
4821  return 0;
4822}
4823
4824/*
4825** Set or clear a shell flag according to a boolean value.
4826*/
4827static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4828  if( booleanValue(zArg) ){
4829    ShellSetFlag(p, mFlag);
4830  }else{
4831    ShellClearFlag(p, mFlag);
4832  }
4833}
4834
4835/*
4836** Close an output file, assuming it is not stderr or stdout
4837*/
4838static void output_file_close(FILE *f){
4839  if( f && f!=stdout && f!=stderr ) fclose(f);
4840}
4841
4842/*
4843** Try to open an output file.   The names "stdout" and "stderr" are
4844** recognized and do the right thing.  NULL is returned if the output
4845** filename is "off".
4846*/
4847static FILE *output_file_open(const char *zFile, int bTextMode){
4848  FILE *f;
4849  if( strcmp(zFile,"stdout")==0 ){
4850    f = stdout;
4851  }else if( strcmp(zFile, "stderr")==0 ){
4852    f = stderr;
4853  }else if( strcmp(zFile, "off")==0 ){
4854    f = 0;
4855  }else{
4856    f = fopen(zFile, bTextMode ? "w" : "wb");
4857    if( f==0 ){
4858      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4859    }
4860  }
4861  return f;
4862}
4863
4864#ifndef SQLITE_OMIT_TRACE
4865/*
4866** A routine for handling output from sqlite3_trace().
4867*/
4868static int sql_trace_callback(
4869  unsigned mType,         /* The trace type */
4870  void *pArg,             /* The ShellState pointer */
4871  void *pP,               /* Usually a pointer to sqlite_stmt */
4872  void *pX                /* Auxiliary output */
4873){
4874  ShellState *p = (ShellState*)pArg;
4875  sqlite3_stmt *pStmt;
4876  const char *zSql;
4877  int nSql;
4878  if( p->traceOut==0 ) return 0;
4879  if( mType==SQLITE_TRACE_CLOSE ){
4880    utf8_printf(p->traceOut, "-- closing database connection\n");
4881    return 0;
4882  }
4883  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4884    zSql = (const char*)pX;
4885  }else{
4886    pStmt = (sqlite3_stmt*)pP;
4887    switch( p->eTraceType ){
4888      case SHELL_TRACE_EXPANDED: {
4889        zSql = sqlite3_expanded_sql(pStmt);
4890        break;
4891      }
4892#ifdef SQLITE_ENABLE_NORMALIZE
4893      case SHELL_TRACE_NORMALIZED: {
4894        zSql = sqlite3_normalized_sql(pStmt);
4895        break;
4896      }
4897#endif
4898      default: {
4899        zSql = sqlite3_sql(pStmt);
4900        break;
4901      }
4902    }
4903  }
4904  if( zSql==0 ) return 0;
4905  nSql = strlen30(zSql);
4906  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4907  switch( mType ){
4908    case SQLITE_TRACE_ROW:
4909    case SQLITE_TRACE_STMT: {
4910      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4911      break;
4912    }
4913    case SQLITE_TRACE_PROFILE: {
4914      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4915      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4916      break;
4917    }
4918  }
4919  return 0;
4920}
4921#endif
4922
4923/*
4924** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4925** a useful spot to set a debugger breakpoint.
4926*/
4927static void test_breakpoint(void){
4928  static int nCall = 0;
4929  nCall++;
4930}
4931
4932/*
4933** An object used to read a CSV and other files for import.
4934*/
4935typedef struct ImportCtx ImportCtx;
4936struct ImportCtx {
4937  const char *zFile;  /* Name of the input file */
4938  FILE *in;           /* Read the CSV text from this input stream */
4939  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
4940  char *z;            /* Accumulated text for a field */
4941  int n;              /* Number of bytes in z */
4942  int nAlloc;         /* Space allocated for z[] */
4943  int nLine;          /* Current line number */
4944  int nRow;           /* Number of rows imported */
4945  int nErr;           /* Number of errors encountered */
4946  int bNotFirst;      /* True if one or more bytes already read */
4947  int cTerm;          /* Character that terminated the most recent field */
4948  int cColSep;        /* The column separator character.  (Usually ",") */
4949  int cRowSep;        /* The row separator character.  (Usually "\n") */
4950};
4951
4952/* Clean up resourced used by an ImportCtx */
4953static void import_cleanup(ImportCtx *p){
4954  if( p->in!=0 && p->xCloser!=0 ){
4955    p->xCloser(p->in);
4956    p->in = 0;
4957  }
4958  sqlite3_free(p->z);
4959  p->z = 0;
4960}
4961
4962/* Append a single byte to z[] */
4963static void import_append_char(ImportCtx *p, int c){
4964  if( p->n+1>=p->nAlloc ){
4965    p->nAlloc += p->nAlloc + 100;
4966    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4967    if( p->z==0 ) shell_out_of_memory();
4968  }
4969  p->z[p->n++] = (char)c;
4970}
4971
4972/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4973** with the option of having a separator other than ",".
4974**
4975**   +  Input comes from p->in.
4976**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4977**      from sqlite3_malloc64().
4978**   +  Use p->cSep as the column separator.  The default is ",".
4979**   +  Use p->rSep as the row separator.  The default is "\n".
4980**   +  Keep track of the line number in p->nLine.
4981**   +  Store the character that terminates the field in p->cTerm.  Store
4982**      EOF on end-of-file.
4983**   +  Report syntax errors on stderr
4984*/
4985static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4986  int c;
4987  int cSep = p->cColSep;
4988  int rSep = p->cRowSep;
4989  p->n = 0;
4990  c = fgetc(p->in);
4991  if( c==EOF || seenInterrupt ){
4992    p->cTerm = EOF;
4993    return 0;
4994  }
4995  if( c=='"' ){
4996    int pc, ppc;
4997    int startLine = p->nLine;
4998    int cQuote = c;
4999    pc = ppc = 0;
5000    while( 1 ){
5001      c = fgetc(p->in);
5002      if( c==rSep ) p->nLine++;
5003      if( c==cQuote ){
5004        if( pc==cQuote ){
5005          pc = 0;
5006          continue;
5007        }
5008      }
5009      if( (c==cSep && pc==cQuote)
5010       || (c==rSep && pc==cQuote)
5011       || (c==rSep && pc=='\r' && ppc==cQuote)
5012       || (c==EOF && pc==cQuote)
5013      ){
5014        do{ p->n--; }while( p->z[p->n]!=cQuote );
5015        p->cTerm = c;
5016        break;
5017      }
5018      if( pc==cQuote && c!='\r' ){
5019        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5020                p->zFile, p->nLine, cQuote);
5021      }
5022      if( c==EOF ){
5023        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5024                p->zFile, startLine, cQuote);
5025        p->cTerm = c;
5026        break;
5027      }
5028      import_append_char(p, c);
5029      ppc = pc;
5030      pc = c;
5031    }
5032  }else{
5033    /* If this is the first field being parsed and it begins with the
5034    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5035    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5036      import_append_char(p, c);
5037      c = fgetc(p->in);
5038      if( (c&0xff)==0xbb ){
5039        import_append_char(p, c);
5040        c = fgetc(p->in);
5041        if( (c&0xff)==0xbf ){
5042          p->bNotFirst = 1;
5043          p->n = 0;
5044          return csv_read_one_field(p);
5045        }
5046      }
5047    }
5048    while( c!=EOF && c!=cSep && c!=rSep ){
5049      import_append_char(p, c);
5050      c = fgetc(p->in);
5051    }
5052    if( c==rSep ){
5053      p->nLine++;
5054      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5055    }
5056    p->cTerm = c;
5057  }
5058  if( p->z ) p->z[p->n] = 0;
5059  p->bNotFirst = 1;
5060  return p->z;
5061}
5062
5063/* Read a single field of ASCII delimited text.
5064**
5065**   +  Input comes from p->in.
5066**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5067**      from sqlite3_malloc64().
5068**   +  Use p->cSep as the column separator.  The default is "\x1F".
5069**   +  Use p->rSep as the row separator.  The default is "\x1E".
5070**   +  Keep track of the row number in p->nLine.
5071**   +  Store the character that terminates the field in p->cTerm.  Store
5072**      EOF on end-of-file.
5073**   +  Report syntax errors on stderr
5074*/
5075static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5076  int c;
5077  int cSep = p->cColSep;
5078  int rSep = p->cRowSep;
5079  p->n = 0;
5080  c = fgetc(p->in);
5081  if( c==EOF || seenInterrupt ){
5082    p->cTerm = EOF;
5083    return 0;
5084  }
5085  while( c!=EOF && c!=cSep && c!=rSep ){
5086    import_append_char(p, c);
5087    c = fgetc(p->in);
5088  }
5089  if( c==rSep ){
5090    p->nLine++;
5091  }
5092  p->cTerm = c;
5093  if( p->z ) p->z[p->n] = 0;
5094  return p->z;
5095}
5096
5097/*
5098** Try to transfer data for table zTable.  If an error is seen while
5099** moving forward, try to go backwards.  The backwards movement won't
5100** work for WITHOUT ROWID tables.
5101*/
5102static void tryToCloneData(
5103  ShellState *p,
5104  sqlite3 *newDb,
5105  const char *zTable
5106){
5107  sqlite3_stmt *pQuery = 0;
5108  sqlite3_stmt *pInsert = 0;
5109  char *zQuery = 0;
5110  char *zInsert = 0;
5111  int rc;
5112  int i, j, n;
5113  int nTable = strlen30(zTable);
5114  int k = 0;
5115  int cnt = 0;
5116  const int spinRate = 10000;
5117
5118  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5119  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5120  if( rc ){
5121    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5122            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5123            zQuery);
5124    goto end_data_xfer;
5125  }
5126  n = sqlite3_column_count(pQuery);
5127  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5128  if( zInsert==0 ) shell_out_of_memory();
5129  sqlite3_snprintf(200+nTable,zInsert,
5130                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5131  i = strlen30(zInsert);
5132  for(j=1; j<n; j++){
5133    memcpy(zInsert+i, ",?", 2);
5134    i += 2;
5135  }
5136  memcpy(zInsert+i, ");", 3);
5137  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5138  if( rc ){
5139    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5140            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5141            zQuery);
5142    goto end_data_xfer;
5143  }
5144  for(k=0; k<2; k++){
5145    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5146      for(i=0; i<n; i++){
5147        switch( sqlite3_column_type(pQuery, i) ){
5148          case SQLITE_NULL: {
5149            sqlite3_bind_null(pInsert, i+1);
5150            break;
5151          }
5152          case SQLITE_INTEGER: {
5153            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5154            break;
5155          }
5156          case SQLITE_FLOAT: {
5157            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5158            break;
5159          }
5160          case SQLITE_TEXT: {
5161            sqlite3_bind_text(pInsert, i+1,
5162                             (const char*)sqlite3_column_text(pQuery,i),
5163                             -1, SQLITE_STATIC);
5164            break;
5165          }
5166          case SQLITE_BLOB: {
5167            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5168                                            sqlite3_column_bytes(pQuery,i),
5169                                            SQLITE_STATIC);
5170            break;
5171          }
5172        }
5173      } /* End for */
5174      rc = sqlite3_step(pInsert);
5175      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5176        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5177                        sqlite3_errmsg(newDb));
5178      }
5179      sqlite3_reset(pInsert);
5180      cnt++;
5181      if( (cnt%spinRate)==0 ){
5182        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5183        fflush(stdout);
5184      }
5185    } /* End while */
5186    if( rc==SQLITE_DONE ) break;
5187    sqlite3_finalize(pQuery);
5188    sqlite3_free(zQuery);
5189    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5190                             zTable);
5191    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5192    if( rc ){
5193      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5194      break;
5195    }
5196  } /* End for(k=0...) */
5197
5198end_data_xfer:
5199  sqlite3_finalize(pQuery);
5200  sqlite3_finalize(pInsert);
5201  sqlite3_free(zQuery);
5202  sqlite3_free(zInsert);
5203}
5204
5205
5206/*
5207** Try to transfer all rows of the schema that match zWhere.  For
5208** each row, invoke xForEach() on the object defined by that row.
5209** If an error is encountered while moving forward through the
5210** sqlite_schema table, try again moving backwards.
5211*/
5212static void tryToCloneSchema(
5213  ShellState *p,
5214  sqlite3 *newDb,
5215  const char *zWhere,
5216  void (*xForEach)(ShellState*,sqlite3*,const char*)
5217){
5218  sqlite3_stmt *pQuery = 0;
5219  char *zQuery = 0;
5220  int rc;
5221  const unsigned char *zName;
5222  const unsigned char *zSql;
5223  char *zErrMsg = 0;
5224
5225  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5226                           " WHERE %s", zWhere);
5227  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5228  if( rc ){
5229    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5230                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5231                    zQuery);
5232    goto end_schema_xfer;
5233  }
5234  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5235    zName = sqlite3_column_text(pQuery, 0);
5236    zSql = sqlite3_column_text(pQuery, 1);
5237    printf("%s... ", zName); fflush(stdout);
5238    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5239    if( zErrMsg ){
5240      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5241      sqlite3_free(zErrMsg);
5242      zErrMsg = 0;
5243    }
5244    if( xForEach ){
5245      xForEach(p, newDb, (const char*)zName);
5246    }
5247    printf("done\n");
5248  }
5249  if( rc!=SQLITE_DONE ){
5250    sqlite3_finalize(pQuery);
5251    sqlite3_free(zQuery);
5252    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5253                             " WHERE %s ORDER BY rowid DESC", zWhere);
5254    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5255    if( rc ){
5256      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5257                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5258                      zQuery);
5259      goto end_schema_xfer;
5260    }
5261    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5262      zName = sqlite3_column_text(pQuery, 0);
5263      zSql = sqlite3_column_text(pQuery, 1);
5264      printf("%s... ", zName); fflush(stdout);
5265      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5266      if( zErrMsg ){
5267        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5268        sqlite3_free(zErrMsg);
5269        zErrMsg = 0;
5270      }
5271      if( xForEach ){
5272        xForEach(p, newDb, (const char*)zName);
5273      }
5274      printf("done\n");
5275    }
5276  }
5277end_schema_xfer:
5278  sqlite3_finalize(pQuery);
5279  sqlite3_free(zQuery);
5280}
5281
5282/*
5283** Open a new database file named "zNewDb".  Try to recover as much information
5284** as possible out of the main database (which might be corrupt) and write it
5285** into zNewDb.
5286*/
5287static void tryToClone(ShellState *p, const char *zNewDb){
5288  int rc;
5289  sqlite3 *newDb = 0;
5290  if( access(zNewDb,0)==0 ){
5291    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5292    return;
5293  }
5294  rc = sqlite3_open(zNewDb, &newDb);
5295  if( rc ){
5296    utf8_printf(stderr, "Cannot create output database: %s\n",
5297            sqlite3_errmsg(newDb));
5298  }else{
5299    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5300    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5301    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5302    tryToCloneSchema(p, newDb, "type!='table'", 0);
5303    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5304    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5305  }
5306  close_db(newDb);
5307}
5308
5309/*
5310** Change the output file back to stdout.
5311**
5312** If the p->doXdgOpen flag is set, that means the output was being
5313** redirected to a temporary file named by p->zTempFile.  In that case,
5314** launch start/open/xdg-open on that temporary file.
5315*/
5316static void output_reset(ShellState *p){
5317  if( p->outfile[0]=='|' ){
5318#ifndef SQLITE_OMIT_POPEN
5319    pclose(p->out);
5320#endif
5321  }else{
5322    output_file_close(p->out);
5323#ifndef SQLITE_NOHAVE_SYSTEM
5324    if( p->doXdgOpen ){
5325      const char *zXdgOpenCmd =
5326#if defined(_WIN32)
5327      "start";
5328#elif defined(__APPLE__)
5329      "open";
5330#else
5331      "xdg-open";
5332#endif
5333      char *zCmd;
5334      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5335      if( system(zCmd) ){
5336        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5337      }else{
5338        /* Give the start/open/xdg-open command some time to get
5339        ** going before we continue, and potential delete the
5340        ** p->zTempFile data file out from under it */
5341        sqlite3_sleep(2000);
5342      }
5343      sqlite3_free(zCmd);
5344      outputModePop(p);
5345      p->doXdgOpen = 0;
5346    }
5347#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5348  }
5349  p->outfile[0] = 0;
5350  p->out = stdout;
5351}
5352
5353/*
5354** Run an SQL command and return the single integer result.
5355*/
5356static int db_int(ShellState *p, const char *zSql){
5357  sqlite3_stmt *pStmt;
5358  int res = 0;
5359  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5360  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5361    res = sqlite3_column_int(pStmt,0);
5362  }
5363  sqlite3_finalize(pStmt);
5364  return res;
5365}
5366
5367/*
5368** Convert a 2-byte or 4-byte big-endian integer into a native integer
5369*/
5370static unsigned int get2byteInt(unsigned char *a){
5371  return (a[0]<<8) + a[1];
5372}
5373static unsigned int get4byteInt(unsigned char *a){
5374  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5375}
5376
5377/*
5378** Implementation of the ".dbinfo" command.
5379**
5380** Return 1 on error, 2 to exit, and 0 otherwise.
5381*/
5382static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5383  static const struct { const char *zName; int ofst; } aField[] = {
5384     { "file change counter:",  24  },
5385     { "database page count:",  28  },
5386     { "freelist page count:",  36  },
5387     { "schema cookie:",        40  },
5388     { "schema format:",        44  },
5389     { "default cache size:",   48  },
5390     { "autovacuum top root:",  52  },
5391     { "incremental vacuum:",   64  },
5392     { "text encoding:",        56  },
5393     { "user version:",         60  },
5394     { "application id:",       68  },
5395     { "software version:",     96  },
5396  };
5397  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5398     { "number of tables:",
5399       "SELECT count(*) FROM %s WHERE type='table'" },
5400     { "number of indexes:",
5401       "SELECT count(*) FROM %s WHERE type='index'" },
5402     { "number of triggers:",
5403       "SELECT count(*) FROM %s WHERE type='trigger'" },
5404     { "number of views:",
5405       "SELECT count(*) FROM %s WHERE type='view'" },
5406     { "schema size:",
5407       "SELECT total(length(sql)) FROM %s" },
5408  };
5409  int i, rc;
5410  unsigned iDataVersion;
5411  char *zSchemaTab;
5412  char *zDb = nArg>=2 ? azArg[1] : "main";
5413  sqlite3_stmt *pStmt = 0;
5414  unsigned char aHdr[100];
5415  open_db(p, 0);
5416  if( p->db==0 ) return 1;
5417  rc = sqlite3_prepare_v2(p->db,
5418             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5419             -1, &pStmt, 0);
5420  if( rc ){
5421    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5422    sqlite3_finalize(pStmt);
5423    return 1;
5424  }
5425  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5426  if( sqlite3_step(pStmt)==SQLITE_ROW
5427   && sqlite3_column_bytes(pStmt,0)>100
5428  ){
5429    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5430    sqlite3_finalize(pStmt);
5431  }else{
5432    raw_printf(stderr, "unable to read database header\n");
5433    sqlite3_finalize(pStmt);
5434    return 1;
5435  }
5436  i = get2byteInt(aHdr+16);
5437  if( i==1 ) i = 65536;
5438  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5439  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5440  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5441  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5442  for(i=0; i<ArraySize(aField); i++){
5443    int ofst = aField[i].ofst;
5444    unsigned int val = get4byteInt(aHdr + ofst);
5445    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5446    switch( ofst ){
5447      case 56: {
5448        if( val==1 ) raw_printf(p->out, " (utf8)");
5449        if( val==2 ) raw_printf(p->out, " (utf16le)");
5450        if( val==3 ) raw_printf(p->out, " (utf16be)");
5451      }
5452    }
5453    raw_printf(p->out, "\n");
5454  }
5455  if( zDb==0 ){
5456    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5457  }else if( strcmp(zDb,"temp")==0 ){
5458    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5459  }else{
5460    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5461  }
5462  for(i=0; i<ArraySize(aQuery); i++){
5463    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5464    int val = db_int(p, zSql);
5465    sqlite3_free(zSql);
5466    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5467  }
5468  sqlite3_free(zSchemaTab);
5469  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5470  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5471  return 0;
5472}
5473
5474/*
5475** Print the current sqlite3_errmsg() value to stderr and return 1.
5476*/
5477static int shellDatabaseError(sqlite3 *db){
5478  const char *zErr = sqlite3_errmsg(db);
5479  utf8_printf(stderr, "Error: %s\n", zErr);
5480  return 1;
5481}
5482
5483/*
5484** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5485** if they match and FALSE (0) if they do not match.
5486**
5487** Globbing rules:
5488**
5489**      '*'       Matches any sequence of zero or more characters.
5490**
5491**      '?'       Matches exactly one character.
5492**
5493**     [...]      Matches one character from the enclosed list of
5494**                characters.
5495**
5496**     [^...]     Matches one character not in the enclosed list.
5497**
5498**      '#'       Matches any sequence of one or more digits with an
5499**                optional + or - sign in front
5500**
5501**      ' '       Any span of whitespace matches any other span of
5502**                whitespace.
5503**
5504** Extra whitespace at the end of z[] is ignored.
5505*/
5506static int testcase_glob(const char *zGlob, const char *z){
5507  int c, c2;
5508  int invert;
5509  int seen;
5510
5511  while( (c = (*(zGlob++)))!=0 ){
5512    if( IsSpace(c) ){
5513      if( !IsSpace(*z) ) return 0;
5514      while( IsSpace(*zGlob) ) zGlob++;
5515      while( IsSpace(*z) ) z++;
5516    }else if( c=='*' ){
5517      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5518        if( c=='?' && (*(z++))==0 ) return 0;
5519      }
5520      if( c==0 ){
5521        return 1;
5522      }else if( c=='[' ){
5523        while( *z && testcase_glob(zGlob-1,z)==0 ){
5524          z++;
5525        }
5526        return (*z)!=0;
5527      }
5528      while( (c2 = (*(z++)))!=0 ){
5529        while( c2!=c ){
5530          c2 = *(z++);
5531          if( c2==0 ) return 0;
5532        }
5533        if( testcase_glob(zGlob,z) ) return 1;
5534      }
5535      return 0;
5536    }else if( c=='?' ){
5537      if( (*(z++))==0 ) return 0;
5538    }else if( c=='[' ){
5539      int prior_c = 0;
5540      seen = 0;
5541      invert = 0;
5542      c = *(z++);
5543      if( c==0 ) return 0;
5544      c2 = *(zGlob++);
5545      if( c2=='^' ){
5546        invert = 1;
5547        c2 = *(zGlob++);
5548      }
5549      if( c2==']' ){
5550        if( c==']' ) seen = 1;
5551        c2 = *(zGlob++);
5552      }
5553      while( c2 && c2!=']' ){
5554        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5555          c2 = *(zGlob++);
5556          if( c>=prior_c && c<=c2 ) seen = 1;
5557          prior_c = 0;
5558        }else{
5559          if( c==c2 ){
5560            seen = 1;
5561          }
5562          prior_c = c2;
5563        }
5564        c2 = *(zGlob++);
5565      }
5566      if( c2==0 || (seen ^ invert)==0 ) return 0;
5567    }else if( c=='#' ){
5568      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5569      if( !IsDigit(z[0]) ) return 0;
5570      z++;
5571      while( IsDigit(z[0]) ){ z++; }
5572    }else{
5573      if( c!=(*(z++)) ) return 0;
5574    }
5575  }
5576  while( IsSpace(*z) ){ z++; }
5577  return *z==0;
5578}
5579
5580
5581/*
5582** Compare the string as a command-line option with either one or two
5583** initial "-" characters.
5584*/
5585static int optionMatch(const char *zStr, const char *zOpt){
5586  if( zStr[0]!='-' ) return 0;
5587  zStr++;
5588  if( zStr[0]=='-' ) zStr++;
5589  return strcmp(zStr, zOpt)==0;
5590}
5591
5592/*
5593** Delete a file.
5594*/
5595int shellDeleteFile(const char *zFilename){
5596  int rc;
5597#ifdef _WIN32
5598  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5599  rc = _wunlink(z);
5600  sqlite3_free(z);
5601#else
5602  rc = unlink(zFilename);
5603#endif
5604  return rc;
5605}
5606
5607/*
5608** Try to delete the temporary file (if there is one) and free the
5609** memory used to hold the name of the temp file.
5610*/
5611static void clearTempFile(ShellState *p){
5612  if( p->zTempFile==0 ) return;
5613  if( p->doXdgOpen ) return;
5614  if( shellDeleteFile(p->zTempFile) ) return;
5615  sqlite3_free(p->zTempFile);
5616  p->zTempFile = 0;
5617}
5618
5619/*
5620** Create a new temp file name with the given suffix.
5621*/
5622static void newTempFile(ShellState *p, const char *zSuffix){
5623  clearTempFile(p);
5624  sqlite3_free(p->zTempFile);
5625  p->zTempFile = 0;
5626  if( p->db ){
5627    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5628  }
5629  if( p->zTempFile==0 ){
5630    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5631    ** will not work and we will need to fallback to guessing */
5632    char *zTemp;
5633    sqlite3_uint64 r;
5634    sqlite3_randomness(sizeof(r), &r);
5635    zTemp = getenv("TEMP");
5636    if( zTemp==0 ) zTemp = getenv("TMP");
5637    if( zTemp==0 ){
5638#ifdef _WIN32
5639      zTemp = "\\tmp";
5640#else
5641      zTemp = "/tmp";
5642#endif
5643    }
5644    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5645  }else{
5646    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5647  }
5648  if( p->zTempFile==0 ){
5649    raw_printf(stderr, "out of memory\n");
5650    exit(1);
5651  }
5652}
5653
5654
5655/*
5656** The implementation of SQL scalar function fkey_collate_clause(), used
5657** by the ".lint fkey-indexes" command. This scalar function is always
5658** called with four arguments - the parent table name, the parent column name,
5659** the child table name and the child column name.
5660**
5661**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5662**
5663** If either of the named tables or columns do not exist, this function
5664** returns an empty string. An empty string is also returned if both tables
5665** and columns exist but have the same default collation sequence. Or,
5666** if both exist but the default collation sequences are different, this
5667** function returns the string " COLLATE <parent-collation>", where
5668** <parent-collation> is the default collation sequence of the parent column.
5669*/
5670static void shellFkeyCollateClause(
5671  sqlite3_context *pCtx,
5672  int nVal,
5673  sqlite3_value **apVal
5674){
5675  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5676  const char *zParent;
5677  const char *zParentCol;
5678  const char *zParentSeq;
5679  const char *zChild;
5680  const char *zChildCol;
5681  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5682  int rc;
5683
5684  assert( nVal==4 );
5685  zParent = (const char*)sqlite3_value_text(apVal[0]);
5686  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5687  zChild = (const char*)sqlite3_value_text(apVal[2]);
5688  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5689
5690  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5691  rc = sqlite3_table_column_metadata(
5692      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5693  );
5694  if( rc==SQLITE_OK ){
5695    rc = sqlite3_table_column_metadata(
5696        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5697    );
5698  }
5699
5700  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5701    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5702    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5703    sqlite3_free(z);
5704  }
5705}
5706
5707
5708/*
5709** The implementation of dot-command ".lint fkey-indexes".
5710*/
5711static int lintFkeyIndexes(
5712  ShellState *pState,             /* Current shell tool state */
5713  char **azArg,                   /* Array of arguments passed to dot command */
5714  int nArg                        /* Number of entries in azArg[] */
5715){
5716  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5717  FILE *out = pState->out;        /* Stream to write non-error output to */
5718  int bVerbose = 0;               /* If -verbose is present */
5719  int bGroupByParent = 0;         /* If -groupbyparent is present */
5720  int i;                          /* To iterate through azArg[] */
5721  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5722  int rc;                         /* Return code */
5723  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5724
5725  /*
5726  ** This SELECT statement returns one row for each foreign key constraint
5727  ** in the schema of the main database. The column values are:
5728  **
5729  ** 0. The text of an SQL statement similar to:
5730  **
5731  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5732  **
5733  **    This SELECT is similar to the one that the foreign keys implementation
5734  **    needs to run internally on child tables. If there is an index that can
5735  **    be used to optimize this query, then it can also be used by the FK
5736  **    implementation to optimize DELETE or UPDATE statements on the parent
5737  **    table.
5738  **
5739  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5740  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5741  **    contains an index that can be used to optimize the query.
5742  **
5743  ** 2. Human readable text that describes the child table and columns. e.g.
5744  **
5745  **       "child_table(child_key1, child_key2)"
5746  **
5747  ** 3. Human readable text that describes the parent table and columns. e.g.
5748  **
5749  **       "parent_table(parent_key1, parent_key2)"
5750  **
5751  ** 4. A full CREATE INDEX statement for an index that could be used to
5752  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5753  **
5754  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5755  **
5756  ** 5. The name of the parent table.
5757  **
5758  ** These six values are used by the C logic below to generate the report.
5759  */
5760  const char *zSql =
5761  "SELECT "
5762    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5763    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5764    "  || fkey_collate_clause("
5765    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5766    ", "
5767    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5768    "  || group_concat('*=?', ' AND ') || ')'"
5769    ", "
5770    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5771    ", "
5772    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5773    ", "
5774    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5775    "  || ' ON ' || quote(s.name) || '('"
5776    "  || group_concat(quote(f.[from]) ||"
5777    "        fkey_collate_clause("
5778    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5779    "  || ');'"
5780    ", "
5781    "     f.[table] "
5782    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5783    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5784    "GROUP BY s.name, f.id "
5785    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5786  ;
5787  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5788
5789  for(i=2; i<nArg; i++){
5790    int n = strlen30(azArg[i]);
5791    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5792      bVerbose = 1;
5793    }
5794    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5795      bGroupByParent = 1;
5796      zIndent = "    ";
5797    }
5798    else{
5799      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5800          azArg[0], azArg[1]
5801      );
5802      return SQLITE_ERROR;
5803    }
5804  }
5805
5806  /* Register the fkey_collate_clause() SQL function */
5807  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5808      0, shellFkeyCollateClause, 0, 0
5809  );
5810
5811
5812  if( rc==SQLITE_OK ){
5813    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5814  }
5815  if( rc==SQLITE_OK ){
5816    sqlite3_bind_int(pSql, 1, bGroupByParent);
5817  }
5818
5819  if( rc==SQLITE_OK ){
5820    int rc2;
5821    char *zPrev = 0;
5822    while( SQLITE_ROW==sqlite3_step(pSql) ){
5823      int res = -1;
5824      sqlite3_stmt *pExplain = 0;
5825      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5826      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5827      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5828      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5829      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5830      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5831
5832      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5833      if( rc!=SQLITE_OK ) break;
5834      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5835        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5836        res = (
5837              0==sqlite3_strglob(zGlob, zPlan)
5838           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5839        );
5840      }
5841      rc = sqlite3_finalize(pExplain);
5842      if( rc!=SQLITE_OK ) break;
5843
5844      if( res<0 ){
5845        raw_printf(stderr, "Error: internal error");
5846        break;
5847      }else{
5848        if( bGroupByParent
5849        && (bVerbose || res==0)
5850        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5851        ){
5852          raw_printf(out, "-- Parent table %s\n", zParent);
5853          sqlite3_free(zPrev);
5854          zPrev = sqlite3_mprintf("%s", zParent);
5855        }
5856
5857        if( res==0 ){
5858          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5859        }else if( bVerbose ){
5860          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5861              zIndent, zFrom, zTarget
5862          );
5863        }
5864      }
5865    }
5866    sqlite3_free(zPrev);
5867
5868    if( rc!=SQLITE_OK ){
5869      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5870    }
5871
5872    rc2 = sqlite3_finalize(pSql);
5873    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5874      rc = rc2;
5875      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5876    }
5877  }else{
5878    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5879  }
5880
5881  return rc;
5882}
5883
5884/*
5885** Implementation of ".lint" dot command.
5886*/
5887static int lintDotCommand(
5888  ShellState *pState,             /* Current shell tool state */
5889  char **azArg,                   /* Array of arguments passed to dot command */
5890  int nArg                        /* Number of entries in azArg[] */
5891){
5892  int n;
5893  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5894  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5895  return lintFkeyIndexes(pState, azArg, nArg);
5896
5897 usage:
5898  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5899  raw_printf(stderr, "Where sub-commands are:\n");
5900  raw_printf(stderr, "    fkey-indexes\n");
5901  return SQLITE_ERROR;
5902}
5903
5904#if !defined SQLITE_OMIT_VIRTUALTABLE
5905static void shellPrepare(
5906  sqlite3 *db,
5907  int *pRc,
5908  const char *zSql,
5909  sqlite3_stmt **ppStmt
5910){
5911  *ppStmt = 0;
5912  if( *pRc==SQLITE_OK ){
5913    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5914    if( rc!=SQLITE_OK ){
5915      raw_printf(stderr, "sql error: %s (%d)\n",
5916          sqlite3_errmsg(db), sqlite3_errcode(db)
5917      );
5918      *pRc = rc;
5919    }
5920  }
5921}
5922
5923/*
5924** Create a prepared statement using printf-style arguments for the SQL.
5925**
5926** This routine is could be marked "static".  But it is not always used,
5927** depending on compile-time options.  By omitting the "static", we avoid
5928** nuisance compiler warnings about "defined but not used".
5929*/
5930void shellPreparePrintf(
5931  sqlite3 *db,
5932  int *pRc,
5933  sqlite3_stmt **ppStmt,
5934  const char *zFmt,
5935  ...
5936){
5937  *ppStmt = 0;
5938  if( *pRc==SQLITE_OK ){
5939    va_list ap;
5940    char *z;
5941    va_start(ap, zFmt);
5942    z = sqlite3_vmprintf(zFmt, ap);
5943    va_end(ap);
5944    if( z==0 ){
5945      *pRc = SQLITE_NOMEM;
5946    }else{
5947      shellPrepare(db, pRc, z, ppStmt);
5948      sqlite3_free(z);
5949    }
5950  }
5951}
5952
5953/* Finalize the prepared statement created using shellPreparePrintf().
5954**
5955** This routine is could be marked "static".  But it is not always used,
5956** depending on compile-time options.  By omitting the "static", we avoid
5957** nuisance compiler warnings about "defined but not used".
5958*/
5959void shellFinalize(
5960  int *pRc,
5961  sqlite3_stmt *pStmt
5962){
5963  if( pStmt ){
5964    sqlite3 *db = sqlite3_db_handle(pStmt);
5965    int rc = sqlite3_finalize(pStmt);
5966    if( *pRc==SQLITE_OK ){
5967      if( rc!=SQLITE_OK ){
5968        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5969      }
5970      *pRc = rc;
5971    }
5972  }
5973}
5974
5975/* Reset the prepared statement created using shellPreparePrintf().
5976**
5977** This routine is could be marked "static".  But it is not always used,
5978** depending on compile-time options.  By omitting the "static", we avoid
5979** nuisance compiler warnings about "defined but not used".
5980*/
5981void shellReset(
5982  int *pRc,
5983  sqlite3_stmt *pStmt
5984){
5985  int rc = sqlite3_reset(pStmt);
5986  if( *pRc==SQLITE_OK ){
5987    if( rc!=SQLITE_OK ){
5988      sqlite3 *db = sqlite3_db_handle(pStmt);
5989      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5990    }
5991    *pRc = rc;
5992  }
5993}
5994#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
5995
5996#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5997/******************************************************************************
5998** The ".archive" or ".ar" command.
5999*/
6000/*
6001** Structure representing a single ".ar" command.
6002*/
6003typedef struct ArCommand ArCommand;
6004struct ArCommand {
6005  u8 eCmd;                        /* An AR_CMD_* value */
6006  u8 bVerbose;                    /* True if --verbose */
6007  u8 bZip;                        /* True if the archive is a ZIP */
6008  u8 bDryRun;                     /* True if --dry-run */
6009  u8 bAppend;                     /* True if --append */
6010  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6011  int nArg;                       /* Number of command arguments */
6012  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6013  const char *zFile;              /* --file argument, or NULL */
6014  const char *zDir;               /* --directory argument, or NULL */
6015  char **azArg;                   /* Array of command arguments */
6016  ShellState *p;                  /* Shell state */
6017  sqlite3 *db;                    /* Database containing the archive */
6018};
6019
6020/*
6021** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6022*/
6023static int arUsage(FILE *f){
6024  showHelp(f,"archive");
6025  return SQLITE_ERROR;
6026}
6027
6028/*
6029** Print an error message for the .ar command to stderr and return
6030** SQLITE_ERROR.
6031*/
6032static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6033  va_list ap;
6034  char *z;
6035  va_start(ap, zFmt);
6036  z = sqlite3_vmprintf(zFmt, ap);
6037  va_end(ap);
6038  utf8_printf(stderr, "Error: %s\n", z);
6039  if( pAr->fromCmdLine ){
6040    utf8_printf(stderr, "Use \"-A\" for more help\n");
6041  }else{
6042    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6043  }
6044  sqlite3_free(z);
6045  return SQLITE_ERROR;
6046}
6047
6048/*
6049** Values for ArCommand.eCmd.
6050*/
6051#define AR_CMD_CREATE       1
6052#define AR_CMD_UPDATE       2
6053#define AR_CMD_INSERT       3
6054#define AR_CMD_EXTRACT      4
6055#define AR_CMD_LIST         5
6056#define AR_CMD_HELP         6
6057
6058/*
6059** Other (non-command) switches.
6060*/
6061#define AR_SWITCH_VERBOSE     7
6062#define AR_SWITCH_FILE        8
6063#define AR_SWITCH_DIRECTORY   9
6064#define AR_SWITCH_APPEND     10
6065#define AR_SWITCH_DRYRUN     11
6066
6067static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6068  switch( eSwitch ){
6069    case AR_CMD_CREATE:
6070    case AR_CMD_EXTRACT:
6071    case AR_CMD_LIST:
6072    case AR_CMD_UPDATE:
6073    case AR_CMD_INSERT:
6074    case AR_CMD_HELP:
6075      if( pAr->eCmd ){
6076        return arErrorMsg(pAr, "multiple command options");
6077      }
6078      pAr->eCmd = eSwitch;
6079      break;
6080
6081    case AR_SWITCH_DRYRUN:
6082      pAr->bDryRun = 1;
6083      break;
6084    case AR_SWITCH_VERBOSE:
6085      pAr->bVerbose = 1;
6086      break;
6087    case AR_SWITCH_APPEND:
6088      pAr->bAppend = 1;
6089      /* Fall thru into --file */
6090    case AR_SWITCH_FILE:
6091      pAr->zFile = zArg;
6092      break;
6093    case AR_SWITCH_DIRECTORY:
6094      pAr->zDir = zArg;
6095      break;
6096  }
6097
6098  return SQLITE_OK;
6099}
6100
6101/*
6102** Parse the command line for an ".ar" command. The results are written into
6103** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6104** successfully, otherwise an error message is written to stderr and
6105** SQLITE_ERROR returned.
6106*/
6107static int arParseCommand(
6108  char **azArg,                   /* Array of arguments passed to dot command */
6109  int nArg,                       /* Number of entries in azArg[] */
6110  ArCommand *pAr                  /* Populate this object */
6111){
6112  struct ArSwitch {
6113    const char *zLong;
6114    char cShort;
6115    u8 eSwitch;
6116    u8 bArg;
6117  } aSwitch[] = {
6118    { "create",    'c', AR_CMD_CREATE,       0 },
6119    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6120    { "insert",    'i', AR_CMD_INSERT,       0 },
6121    { "list",      't', AR_CMD_LIST,         0 },
6122    { "update",    'u', AR_CMD_UPDATE,       0 },
6123    { "help",      'h', AR_CMD_HELP,         0 },
6124    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6125    { "file",      'f', AR_SWITCH_FILE,      1 },
6126    { "append",    'a', AR_SWITCH_APPEND,    1 },
6127    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6128    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6129  };
6130  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6131  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6132
6133  if( nArg<=1 ){
6134    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6135    return arUsage(stderr);
6136  }else{
6137    char *z = azArg[1];
6138    if( z[0]!='-' ){
6139      /* Traditional style [tar] invocation */
6140      int i;
6141      int iArg = 2;
6142      for(i=0; z[i]; i++){
6143        const char *zArg = 0;
6144        struct ArSwitch *pOpt;
6145        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6146          if( z[i]==pOpt->cShort ) break;
6147        }
6148        if( pOpt==pEnd ){
6149          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6150        }
6151        if( pOpt->bArg ){
6152          if( iArg>=nArg ){
6153            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6154          }
6155          zArg = azArg[iArg++];
6156        }
6157        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6158      }
6159      pAr->nArg = nArg-iArg;
6160      if( pAr->nArg>0 ){
6161        pAr->azArg = &azArg[iArg];
6162      }
6163    }else{
6164      /* Non-traditional invocation */
6165      int iArg;
6166      for(iArg=1; iArg<nArg; iArg++){
6167        int n;
6168        z = azArg[iArg];
6169        if( z[0]!='-' ){
6170          /* All remaining command line words are command arguments. */
6171          pAr->azArg = &azArg[iArg];
6172          pAr->nArg = nArg-iArg;
6173          break;
6174        }
6175        n = strlen30(z);
6176
6177        if( z[1]!='-' ){
6178          int i;
6179          /* One or more short options */
6180          for(i=1; i<n; i++){
6181            const char *zArg = 0;
6182            struct ArSwitch *pOpt;
6183            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6184              if( z[i]==pOpt->cShort ) break;
6185            }
6186            if( pOpt==pEnd ){
6187              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6188            }
6189            if( pOpt->bArg ){
6190              if( i<(n-1) ){
6191                zArg = &z[i+1];
6192                i = n;
6193              }else{
6194                if( iArg>=(nArg-1) ){
6195                  return arErrorMsg(pAr, "option requires an argument: %c",
6196                                    z[i]);
6197                }
6198                zArg = azArg[++iArg];
6199              }
6200            }
6201            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6202          }
6203        }else if( z[2]=='\0' ){
6204          /* A -- option, indicating that all remaining command line words
6205          ** are command arguments.  */
6206          pAr->azArg = &azArg[iArg+1];
6207          pAr->nArg = nArg-iArg-1;
6208          break;
6209        }else{
6210          /* A long option */
6211          const char *zArg = 0;             /* Argument for option, if any */
6212          struct ArSwitch *pMatch = 0;      /* Matching option */
6213          struct ArSwitch *pOpt;            /* Iterator */
6214          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6215            const char *zLong = pOpt->zLong;
6216            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6217              if( pMatch ){
6218                return arErrorMsg(pAr, "ambiguous option: %s",z);
6219              }else{
6220                pMatch = pOpt;
6221              }
6222            }
6223          }
6224
6225          if( pMatch==0 ){
6226            return arErrorMsg(pAr, "unrecognized option: %s", z);
6227          }
6228          if( pMatch->bArg ){
6229            if( iArg>=(nArg-1) ){
6230              return arErrorMsg(pAr, "option requires an argument: %s", z);
6231            }
6232            zArg = azArg[++iArg];
6233          }
6234          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6235        }
6236      }
6237    }
6238  }
6239
6240  return SQLITE_OK;
6241}
6242
6243/*
6244** This function assumes that all arguments within the ArCommand.azArg[]
6245** array refer to archive members, as for the --extract or --list commands.
6246** It checks that each of them are present. If any specified file is not
6247** present in the archive, an error is printed to stderr and an error
6248** code returned. Otherwise, if all specified arguments are present in
6249** the archive, SQLITE_OK is returned.
6250**
6251** This function strips any trailing '/' characters from each argument.
6252** This is consistent with the way the [tar] command seems to work on
6253** Linux.
6254*/
6255static int arCheckEntries(ArCommand *pAr){
6256  int rc = SQLITE_OK;
6257  if( pAr->nArg ){
6258    int i, j;
6259    sqlite3_stmt *pTest = 0;
6260
6261    shellPreparePrintf(pAr->db, &rc, &pTest,
6262        "SELECT name FROM %s WHERE name=$name",
6263        pAr->zSrcTable
6264    );
6265    j = sqlite3_bind_parameter_index(pTest, "$name");
6266    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6267      char *z = pAr->azArg[i];
6268      int n = strlen30(z);
6269      int bOk = 0;
6270      while( n>0 && z[n-1]=='/' ) n--;
6271      z[n] = '\0';
6272      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6273      if( SQLITE_ROW==sqlite3_step(pTest) ){
6274        bOk = 1;
6275      }
6276      shellReset(&rc, pTest);
6277      if( rc==SQLITE_OK && bOk==0 ){
6278        utf8_printf(stderr, "not found in archive: %s\n", z);
6279        rc = SQLITE_ERROR;
6280      }
6281    }
6282    shellFinalize(&rc, pTest);
6283  }
6284  return rc;
6285}
6286
6287/*
6288** Format a WHERE clause that can be used against the "sqlar" table to
6289** identify all archive members that match the command arguments held
6290** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6291** The caller is responsible for eventually calling sqlite3_free() on
6292** any non-NULL (*pzWhere) value.
6293*/
6294static void arWhereClause(
6295  int *pRc,
6296  ArCommand *pAr,
6297  char **pzWhere                  /* OUT: New WHERE clause */
6298){
6299  char *zWhere = 0;
6300  if( *pRc==SQLITE_OK ){
6301    if( pAr->nArg==0 ){
6302      zWhere = sqlite3_mprintf("1");
6303    }else{
6304      int i;
6305      const char *zSep = "";
6306      for(i=0; i<pAr->nArg; i++){
6307        const char *z = pAr->azArg[i];
6308        zWhere = sqlite3_mprintf(
6309          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6310          zWhere, zSep, z, strlen30(z)+1, z
6311        );
6312        if( zWhere==0 ){
6313          *pRc = SQLITE_NOMEM;
6314          break;
6315        }
6316        zSep = " OR ";
6317      }
6318    }
6319  }
6320  *pzWhere = zWhere;
6321}
6322
6323/*
6324** Implementation of .ar "lisT" command.
6325*/
6326static int arListCommand(ArCommand *pAr){
6327  const char *zSql = "SELECT %s FROM %s WHERE %s";
6328  const char *azCols[] = {
6329    "name",
6330    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6331  };
6332
6333  char *zWhere = 0;
6334  sqlite3_stmt *pSql = 0;
6335  int rc;
6336
6337  rc = arCheckEntries(pAr);
6338  arWhereClause(&rc, pAr, &zWhere);
6339
6340  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6341                     pAr->zSrcTable, zWhere);
6342  if( pAr->bDryRun ){
6343    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6344  }else{
6345    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6346      if( pAr->bVerbose ){
6347        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6348            sqlite3_column_text(pSql, 0),
6349            sqlite3_column_int(pSql, 1),
6350            sqlite3_column_text(pSql, 2),
6351            sqlite3_column_text(pSql, 3)
6352        );
6353      }else{
6354        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6355      }
6356    }
6357  }
6358  shellFinalize(&rc, pSql);
6359  sqlite3_free(zWhere);
6360  return rc;
6361}
6362
6363
6364/*
6365** Implementation of .ar "eXtract" command.
6366*/
6367static int arExtractCommand(ArCommand *pAr){
6368  const char *zSql1 =
6369    "SELECT "
6370    " ($dir || name),"
6371    " writefile(($dir || name), %s, mode, mtime) "
6372    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6373    " AND name NOT GLOB '*..[/\\]*'";
6374
6375  const char *azExtraArg[] = {
6376    "sqlar_uncompress(data, sz)",
6377    "data"
6378  };
6379
6380  sqlite3_stmt *pSql = 0;
6381  int rc = SQLITE_OK;
6382  char *zDir = 0;
6383  char *zWhere = 0;
6384  int i, j;
6385
6386  /* If arguments are specified, check that they actually exist within
6387  ** the archive before proceeding. And formulate a WHERE clause to
6388  ** match them.  */
6389  rc = arCheckEntries(pAr);
6390  arWhereClause(&rc, pAr, &zWhere);
6391
6392  if( rc==SQLITE_OK ){
6393    if( pAr->zDir ){
6394      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6395    }else{
6396      zDir = sqlite3_mprintf("");
6397    }
6398    if( zDir==0 ) rc = SQLITE_NOMEM;
6399  }
6400
6401  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6402      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6403  );
6404
6405  if( rc==SQLITE_OK ){
6406    j = sqlite3_bind_parameter_index(pSql, "$dir");
6407    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6408
6409    /* Run the SELECT statement twice. The first time, writefile() is called
6410    ** for all archive members that should be extracted. The second time,
6411    ** only for the directories. This is because the timestamps for
6412    ** extracted directories must be reset after they are populated (as
6413    ** populating them changes the timestamp).  */
6414    for(i=0; i<2; i++){
6415      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6416      sqlite3_bind_int(pSql, j, i);
6417      if( pAr->bDryRun ){
6418        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6419      }else{
6420        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6421          if( i==0 && pAr->bVerbose ){
6422            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6423          }
6424        }
6425      }
6426      shellReset(&rc, pSql);
6427    }
6428    shellFinalize(&rc, pSql);
6429  }
6430
6431  sqlite3_free(zDir);
6432  sqlite3_free(zWhere);
6433  return rc;
6434}
6435
6436/*
6437** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6438*/
6439static int arExecSql(ArCommand *pAr, const char *zSql){
6440  int rc;
6441  if( pAr->bDryRun ){
6442    utf8_printf(pAr->p->out, "%s\n", zSql);
6443    rc = SQLITE_OK;
6444  }else{
6445    char *zErr = 0;
6446    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6447    if( zErr ){
6448      utf8_printf(stdout, "ERROR: %s\n", zErr);
6449      sqlite3_free(zErr);
6450    }
6451  }
6452  return rc;
6453}
6454
6455
6456/*
6457** Implementation of .ar "create", "insert", and "update" commands.
6458**
6459**     create    ->     Create a new SQL archive
6460**     insert    ->     Insert or reinsert all files listed
6461**     update    ->     Insert files that have changed or that were not
6462**                      previously in the archive
6463**
6464** Create the "sqlar" table in the database if it does not already exist.
6465** Then add each file in the azFile[] array to the archive. Directories
6466** are added recursively. If argument bVerbose is non-zero, a message is
6467** printed on stdout for each file archived.
6468**
6469** The create command is the same as update, except that it drops
6470** any existing "sqlar" table before beginning.  The "insert" command
6471** always overwrites every file named on the command-line, where as
6472** "update" only overwrites if the size or mtime or mode has changed.
6473*/
6474static int arCreateOrUpdateCommand(
6475  ArCommand *pAr,                 /* Command arguments and options */
6476  int bUpdate,                    /* true for a --create. */
6477  int bOnlyIfChanged              /* Only update if file has changed */
6478){
6479  const char *zCreate =
6480      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6481      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6482      "  mode INT,               -- access permissions\n"
6483      "  mtime INT,              -- last modification time\n"
6484      "  sz INT,                 -- original file size\n"
6485      "  data BLOB               -- compressed content\n"
6486      ")";
6487  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6488  const char *zInsertFmt[2] = {
6489     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6490     "  SELECT\n"
6491     "    %s,\n"
6492     "    mode,\n"
6493     "    mtime,\n"
6494     "    CASE substr(lsmode(mode),1,1)\n"
6495     "      WHEN '-' THEN length(data)\n"
6496     "      WHEN 'd' THEN 0\n"
6497     "      ELSE -1 END,\n"
6498     "    sqlar_compress(data)\n"
6499     "  FROM fsdir(%Q,%Q) AS disk\n"
6500     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6501     ,
6502     "REPLACE INTO %s(name,mode,mtime,data)\n"
6503     "  SELECT\n"
6504     "    %s,\n"
6505     "    mode,\n"
6506     "    mtime,\n"
6507     "    data\n"
6508     "  FROM fsdir(%Q,%Q) AS disk\n"
6509     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6510  };
6511  int i;                          /* For iterating through azFile[] */
6512  int rc;                         /* Return code */
6513  const char *zTab = 0;           /* SQL table into which to insert */
6514  char *zSql;
6515  char zTemp[50];
6516  char *zExists = 0;
6517
6518  arExecSql(pAr, "PRAGMA page_size=512");
6519  rc = arExecSql(pAr, "SAVEPOINT ar;");
6520  if( rc!=SQLITE_OK ) return rc;
6521  zTemp[0] = 0;
6522  if( pAr->bZip ){
6523    /* Initialize the zipfile virtual table, if necessary */
6524    if( pAr->zFile ){
6525      sqlite3_uint64 r;
6526      sqlite3_randomness(sizeof(r),&r);
6527      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6528      zTab = zTemp;
6529      zSql = sqlite3_mprintf(
6530         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6531         zTab, pAr->zFile
6532      );
6533      rc = arExecSql(pAr, zSql);
6534      sqlite3_free(zSql);
6535    }else{
6536      zTab = "zip";
6537    }
6538  }else{
6539    /* Initialize the table for an SQLAR */
6540    zTab = "sqlar";
6541    if( bUpdate==0 ){
6542      rc = arExecSql(pAr, zDrop);
6543      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6544    }
6545    rc = arExecSql(pAr, zCreate);
6546  }
6547  if( bOnlyIfChanged ){
6548    zExists = sqlite3_mprintf(
6549      " AND NOT EXISTS("
6550          "SELECT 1 FROM %s AS mem"
6551          " WHERE mem.name=disk.name"
6552          " AND mem.mtime=disk.mtime"
6553          " AND mem.mode=disk.mode)", zTab);
6554  }else{
6555    zExists = sqlite3_mprintf("");
6556  }
6557  if( zExists==0 ) rc = SQLITE_NOMEM;
6558  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6559    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6560        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6561        pAr->azArg[i], pAr->zDir, zExists);
6562    rc = arExecSql(pAr, zSql2);
6563    sqlite3_free(zSql2);
6564  }
6565end_ar_transaction:
6566  if( rc!=SQLITE_OK ){
6567    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6568  }else{
6569    rc = arExecSql(pAr, "RELEASE ar;");
6570    if( pAr->bZip && pAr->zFile ){
6571      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6572      arExecSql(pAr, zSql);
6573      sqlite3_free(zSql);
6574    }
6575  }
6576  sqlite3_free(zExists);
6577  return rc;
6578}
6579
6580/*
6581** Implementation of ".ar" dot command.
6582*/
6583static int arDotCommand(
6584  ShellState *pState,          /* Current shell tool state */
6585  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6586  char **azArg,                /* Array of arguments passed to dot command */
6587  int nArg                     /* Number of entries in azArg[] */
6588){
6589  ArCommand cmd;
6590  int rc;
6591  memset(&cmd, 0, sizeof(cmd));
6592  cmd.fromCmdLine = fromCmdLine;
6593  rc = arParseCommand(azArg, nArg, &cmd);
6594  if( rc==SQLITE_OK ){
6595    int eDbType = SHELL_OPEN_UNSPEC;
6596    cmd.p = pState;
6597    cmd.db = pState->db;
6598    if( cmd.zFile ){
6599      eDbType = deduceDatabaseType(cmd.zFile, 1);
6600    }else{
6601      eDbType = pState->openMode;
6602    }
6603    if( eDbType==SHELL_OPEN_ZIPFILE ){
6604      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6605        if( cmd.zFile==0 ){
6606          cmd.zSrcTable = sqlite3_mprintf("zip");
6607        }else{
6608          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6609        }
6610      }
6611      cmd.bZip = 1;
6612    }else if( cmd.zFile ){
6613      int flags;
6614      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6615      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6616           || cmd.eCmd==AR_CMD_UPDATE ){
6617        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6618      }else{
6619        flags = SQLITE_OPEN_READONLY;
6620      }
6621      cmd.db = 0;
6622      if( cmd.bDryRun ){
6623        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6624             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6625      }
6626      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6627             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6628      if( rc!=SQLITE_OK ){
6629        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6630            cmd.zFile, sqlite3_errmsg(cmd.db)
6631        );
6632        goto end_ar_command;
6633      }
6634      sqlite3_fileio_init(cmd.db, 0, 0);
6635      sqlite3_sqlar_init(cmd.db, 0, 0);
6636      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6637                              shellPutsFunc, 0, 0);
6638
6639    }
6640    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6641      if( cmd.eCmd!=AR_CMD_CREATE
6642       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6643      ){
6644        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6645        rc = SQLITE_ERROR;
6646        goto end_ar_command;
6647      }
6648      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6649    }
6650
6651    switch( cmd.eCmd ){
6652      case AR_CMD_CREATE:
6653        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6654        break;
6655
6656      case AR_CMD_EXTRACT:
6657        rc = arExtractCommand(&cmd);
6658        break;
6659
6660      case AR_CMD_LIST:
6661        rc = arListCommand(&cmd);
6662        break;
6663
6664      case AR_CMD_HELP:
6665        arUsage(pState->out);
6666        break;
6667
6668      case AR_CMD_INSERT:
6669        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6670        break;
6671
6672      default:
6673        assert( cmd.eCmd==AR_CMD_UPDATE );
6674        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6675        break;
6676    }
6677  }
6678end_ar_command:
6679  if( cmd.db!=pState->db ){
6680    close_db(cmd.db);
6681  }
6682  sqlite3_free(cmd.zSrcTable);
6683
6684  return rc;
6685}
6686/* End of the ".archive" or ".ar" command logic
6687*******************************************************************************/
6688#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6689
6690#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6691/*
6692** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6693** Otherwise, the SQL statement or statements in zSql are executed using
6694** database connection db and the error code written to *pRc before
6695** this function returns.
6696*/
6697static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6698  int rc = *pRc;
6699  if( rc==SQLITE_OK ){
6700    char *zErr = 0;
6701    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6702    if( rc!=SQLITE_OK ){
6703      raw_printf(stderr, "SQL error: %s\n", zErr);
6704    }
6705    *pRc = rc;
6706  }
6707}
6708
6709/*
6710** Like shellExec(), except that zFmt is a printf() style format string.
6711*/
6712static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6713  char *z = 0;
6714  if( *pRc==SQLITE_OK ){
6715    va_list ap;
6716    va_start(ap, zFmt);
6717    z = sqlite3_vmprintf(zFmt, ap);
6718    va_end(ap);
6719    if( z==0 ){
6720      *pRc = SQLITE_NOMEM;
6721    }else{
6722      shellExec(db, pRc, z);
6723    }
6724    sqlite3_free(z);
6725  }
6726}
6727
6728/*
6729** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6730** Otherwise, an attempt is made to allocate, zero and return a pointer
6731** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6732** to SQLITE_NOMEM and NULL returned.
6733*/
6734static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6735  void *pRet = 0;
6736  if( *pRc==SQLITE_OK ){
6737    pRet = sqlite3_malloc64(nByte);
6738    if( pRet==0 ){
6739      *pRc = SQLITE_NOMEM;
6740    }else{
6741      memset(pRet, 0, nByte);
6742    }
6743  }
6744  return pRet;
6745}
6746
6747/*
6748** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6749** Otherwise, zFmt is treated as a printf() style string. The result of
6750** formatting it along with any trailing arguments is written into a
6751** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6752** It is the responsibility of the caller to eventually free this buffer
6753** using a call to sqlite3_free().
6754**
6755** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6756** pointer returned.
6757*/
6758static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6759  char *z = 0;
6760  if( *pRc==SQLITE_OK ){
6761    va_list ap;
6762    va_start(ap, zFmt);
6763    z = sqlite3_vmprintf(zFmt, ap);
6764    va_end(ap);
6765    if( z==0 ){
6766      *pRc = SQLITE_NOMEM;
6767    }
6768  }
6769  return z;
6770}
6771
6772/*
6773** When running the ".recover" command, each output table, and the special
6774** orphaned row table if it is required, is represented by an instance
6775** of the following struct.
6776*/
6777typedef struct RecoverTable RecoverTable;
6778struct RecoverTable {
6779  char *zQuoted;                  /* Quoted version of table name */
6780  int nCol;                       /* Number of columns in table */
6781  char **azlCol;                  /* Array of column lists */
6782  int iPk;                        /* Index of IPK column */
6783};
6784
6785/*
6786** Free a RecoverTable object allocated by recoverFindTable() or
6787** recoverOrphanTable().
6788*/
6789static void recoverFreeTable(RecoverTable *pTab){
6790  if( pTab ){
6791    sqlite3_free(pTab->zQuoted);
6792    if( pTab->azlCol ){
6793      int i;
6794      for(i=0; i<=pTab->nCol; i++){
6795        sqlite3_free(pTab->azlCol[i]);
6796      }
6797      sqlite3_free(pTab->azlCol);
6798    }
6799    sqlite3_free(pTab);
6800  }
6801}
6802
6803/*
6804** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6805** Otherwise, it allocates and returns a RecoverTable object based on the
6806** final four arguments passed to this function. It is the responsibility
6807** of the caller to eventually free the returned object using
6808** recoverFreeTable().
6809*/
6810static RecoverTable *recoverNewTable(
6811  int *pRc,                       /* IN/OUT: Error code */
6812  const char *zName,              /* Name of table */
6813  const char *zSql,               /* CREATE TABLE statement */
6814  int bIntkey,
6815  int nCol
6816){
6817  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6818  int rc = *pRc;
6819  RecoverTable *pTab = 0;
6820
6821  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6822  if( rc==SQLITE_OK ){
6823    int nSqlCol = 0;
6824    int bSqlIntkey = 0;
6825    sqlite3_stmt *pStmt = 0;
6826
6827    rc = sqlite3_open("", &dbtmp);
6828    if( rc==SQLITE_OK ){
6829      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6830                              shellIdQuote, 0, 0);
6831    }
6832    if( rc==SQLITE_OK ){
6833      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6834    }
6835    if( rc==SQLITE_OK ){
6836      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6837      if( rc==SQLITE_ERROR ){
6838        rc = SQLITE_OK;
6839        goto finished;
6840      }
6841    }
6842    shellPreparePrintf(dbtmp, &rc, &pStmt,
6843        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6844    );
6845    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6846      nSqlCol = sqlite3_column_int(pStmt, 0);
6847    }
6848    shellFinalize(&rc, pStmt);
6849
6850    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6851      goto finished;
6852    }
6853
6854    shellPreparePrintf(dbtmp, &rc, &pStmt,
6855      "SELECT ("
6856      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6857      ") FROM sqlite_schema WHERE name = %Q", zName
6858    );
6859    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6860      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6861    }
6862    shellFinalize(&rc, pStmt);
6863
6864    if( bIntkey==bSqlIntkey ){
6865      int i;
6866      const char *zPk = "_rowid_";
6867      sqlite3_stmt *pPkFinder = 0;
6868
6869      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6870      ** set zPk to the name of the PK column, and pTab->iPk to the index
6871      ** of the column, where columns are 0-numbered from left to right.
6872      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6873      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6874      pTab->iPk = -2;
6875      if( bIntkey ){
6876        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6877          "SELECT cid, name FROM pragma_table_info(%Q) "
6878          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6879          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6880          , zName, zName
6881        );
6882        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6883          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6884          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6885        }
6886      }
6887
6888      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6889      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6890      pTab->nCol = nSqlCol;
6891
6892      if( bIntkey ){
6893        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6894      }else{
6895        pTab->azlCol[0] = shellMPrintf(&rc, "");
6896      }
6897      i = 1;
6898      shellPreparePrintf(dbtmp, &rc, &pStmt,
6899          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6900          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6901          "FROM pragma_table_info(%Q)",
6902          bIntkey ? ", " : "", pTab->iPk,
6903          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6904          zName
6905      );
6906      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6907        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6908        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6909        i++;
6910      }
6911      shellFinalize(&rc, pStmt);
6912
6913      shellFinalize(&rc, pPkFinder);
6914    }
6915  }
6916
6917 finished:
6918  sqlite3_close(dbtmp);
6919  *pRc = rc;
6920  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6921    recoverFreeTable(pTab);
6922    pTab = 0;
6923  }
6924  return pTab;
6925}
6926
6927/*
6928** This function is called to search the schema recovered from the
6929** sqlite_schema table of the (possibly) corrupt database as part
6930** of a ".recover" command. Specifically, for a table with root page
6931** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6932** table must be a WITHOUT ROWID table, or if non-zero, not one of
6933** those.
6934**
6935** If a table is found, a (RecoverTable*) object is returned. Or, if
6936** no such table is found, but bIntkey is false and iRoot is the
6937** root page of an index in the recovered schema, then (*pbNoop) is
6938** set to true and NULL returned. Or, if there is no such table or
6939** index, NULL is returned and (*pbNoop) set to 0, indicating that
6940** the caller should write data to the orphans table.
6941*/
6942static RecoverTable *recoverFindTable(
6943  ShellState *pState,             /* Shell state object */
6944  int *pRc,                       /* IN/OUT: Error code */
6945  int iRoot,                      /* Root page of table */
6946  int bIntkey,                    /* True for an intkey table */
6947  int nCol,                       /* Number of columns in table */
6948  int *pbNoop                     /* OUT: True if iRoot is root of index */
6949){
6950  sqlite3_stmt *pStmt = 0;
6951  RecoverTable *pRet = 0;
6952  int bNoop = 0;
6953  const char *zSql = 0;
6954  const char *zName = 0;
6955
6956  /* Search the recovered schema for an object with root page iRoot. */
6957  shellPreparePrintf(pState->db, pRc, &pStmt,
6958      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6959  );
6960  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6961    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6962    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6963      bNoop = 1;
6964      break;
6965    }
6966    if( sqlite3_stricmp(zType, "table")==0 ){
6967      zName = (const char*)sqlite3_column_text(pStmt, 1);
6968      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6969      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6970      break;
6971    }
6972  }
6973
6974  shellFinalize(pRc, pStmt);
6975  *pbNoop = bNoop;
6976  return pRet;
6977}
6978
6979/*
6980** Return a RecoverTable object representing the orphans table.
6981*/
6982static RecoverTable *recoverOrphanTable(
6983  ShellState *pState,             /* Shell state object */
6984  int *pRc,                       /* IN/OUT: Error code */
6985  const char *zLostAndFound,      /* Base name for orphans table */
6986  int nCol                        /* Number of user data columns */
6987){
6988  RecoverTable *pTab = 0;
6989  if( nCol>=0 && *pRc==SQLITE_OK ){
6990    int i;
6991
6992    /* This block determines the name of the orphan table. The prefered
6993    ** name is zLostAndFound. But if that clashes with another name
6994    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
6995    ** and so on until a non-clashing name is found.  */
6996    int iTab = 0;
6997    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
6998    sqlite3_stmt *pTest = 0;
6999    shellPrepare(pState->db, pRc,
7000        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7001    );
7002    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7003    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7004      shellReset(pRc, pTest);
7005      sqlite3_free(zTab);
7006      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7007      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7008    }
7009    shellFinalize(pRc, pTest);
7010
7011    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7012    if( pTab ){
7013      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7014      pTab->nCol = nCol;
7015      pTab->iPk = -2;
7016      if( nCol>0 ){
7017        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7018        if( pTab->azlCol ){
7019          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7020          for(i=nCol-1; i>=0; i--){
7021            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7022          }
7023        }
7024      }
7025
7026      if( *pRc!=SQLITE_OK ){
7027        recoverFreeTable(pTab);
7028        pTab = 0;
7029      }else{
7030        raw_printf(pState->out,
7031            "CREATE TABLE %s(rootpgno INTEGER, "
7032            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7033        );
7034        for(i=0; i<nCol; i++){
7035          raw_printf(pState->out, ", c%d", i);
7036        }
7037        raw_printf(pState->out, ");\n");
7038      }
7039    }
7040    sqlite3_free(zTab);
7041  }
7042  return pTab;
7043}
7044
7045/*
7046** This function is called to recover data from the database. A script
7047** to construct a new database containing all recovered data is output
7048** on stream pState->out.
7049*/
7050static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7051  int rc = SQLITE_OK;
7052  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7053  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7054  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7055  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7056  const char *zLostAndFound = "lost_and_found";
7057  int i;
7058  int nOrphan = -1;
7059  RecoverTable *pOrphan = 0;
7060
7061  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7062  int bRowids = 1;                /* 0 if --no-rowids */
7063  for(i=1; i<nArg; i++){
7064    char *z = azArg[i];
7065    int n;
7066    if( z[0]=='-' && z[1]=='-' ) z++;
7067    n = strlen30(z);
7068    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7069      bFreelist = 0;
7070    }else
7071    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7072      i++;
7073      zRecoveryDb = azArg[i];
7074    }else
7075    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7076      i++;
7077      zLostAndFound = azArg[i];
7078    }else
7079    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7080      bRowids = 0;
7081    }
7082    else{
7083      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7084      showHelp(pState->out, azArg[0]);
7085      return 1;
7086    }
7087  }
7088
7089  shellExecPrintf(pState->db, &rc,
7090    /* Attach an in-memory database named 'recovery'. Create an indexed
7091    ** cache of the sqlite_dbptr virtual table. */
7092    "PRAGMA writable_schema = on;"
7093    "ATTACH %Q AS recovery;"
7094    "DROP TABLE IF EXISTS recovery.dbptr;"
7095    "DROP TABLE IF EXISTS recovery.freelist;"
7096    "DROP TABLE IF EXISTS recovery.map;"
7097    "DROP TABLE IF EXISTS recovery.schema;"
7098    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7099  );
7100
7101  if( bFreelist ){
7102    shellExec(pState->db, &rc,
7103      "WITH trunk(pgno) AS ("
7104      "  SELECT shell_int32("
7105      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7106      "      WHERE x>0"
7107      "    UNION"
7108      "  SELECT shell_int32("
7109      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7110      "      FROM trunk WHERE x>0"
7111      "),"
7112      "freelist(data, n, freepgno) AS ("
7113      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7114      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7115      "    UNION ALL"
7116      "  SELECT data, n-1, shell_int32(data, 2+n) "
7117      "      FROM freelist WHERE n>=0"
7118      ")"
7119      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7120    );
7121  }
7122
7123  /* If this is an auto-vacuum database, add all pointer-map pages to
7124  ** the freelist table. Do this regardless of whether or not
7125  ** --freelist-corrupt was specified.  */
7126  shellExec(pState->db, &rc,
7127    "WITH ptrmap(pgno) AS ("
7128    "  SELECT 2 WHERE shell_int32("
7129    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7130    "  )"
7131    "    UNION ALL "
7132    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7133    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7134    ")"
7135    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7136  );
7137
7138  shellExec(pState->db, &rc,
7139    "CREATE TABLE recovery.dbptr("
7140    "      pgno, child, PRIMARY KEY(child, pgno)"
7141    ") WITHOUT ROWID;"
7142    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7143    "    SELECT * FROM sqlite_dbptr"
7144    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7145
7146    /* Delete any pointer to page 1. This ensures that page 1 is considered
7147    ** a root page, regardless of how corrupt the db is. */
7148    "DELETE FROM recovery.dbptr WHERE child = 1;"
7149
7150    /* Delete all pointers to any pages that have more than one pointer
7151    ** to them. Such pages will be treated as root pages when recovering
7152    ** data.  */
7153    "DELETE FROM recovery.dbptr WHERE child IN ("
7154    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7155    ");"
7156
7157    /* Create the "map" table that will (eventually) contain instructions
7158    ** for dealing with each page in the db that contains one or more
7159    ** records. */
7160    "CREATE TABLE recovery.map("
7161      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7162    ");"
7163
7164    /* Populate table [map]. If there are circular loops of pages in the
7165    ** database, the following adds all pages in such a loop to the map
7166    ** as individual root pages. This could be handled better.  */
7167    "WITH pages(i, maxlen) AS ("
7168    "  SELECT page_count, ("
7169    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7170    "  ) FROM pragma_page_count WHERE page_count>0"
7171    "    UNION ALL"
7172    "  SELECT i-1, ("
7173    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7174    "  ) FROM pages WHERE i>=2"
7175    ")"
7176    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7177    "  SELECT i, maxlen, NULL, ("
7178    "    WITH p(orig, pgno, parent) AS ("
7179    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7180    "        UNION "
7181    "      SELECT i, p.parent, "
7182    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7183    "    )"
7184    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7185    ") "
7186    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7187    "UPDATE recovery.map AS o SET intkey = ("
7188    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7189    ");"
7190
7191    /* Extract data from page 1 and any linked pages into table
7192    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7193    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7194    "INSERT INTO recovery.schema SELECT "
7195    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7196    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7197    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7198    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7199    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7200    "FROM sqlite_dbdata WHERE pgno IN ("
7201    "  SELECT pgno FROM recovery.map WHERE root=1"
7202    ")"
7203    "GROUP BY pgno, cell;"
7204    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7205  );
7206
7207  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7208  ** CREATE TABLE statements that extracted from the existing schema.  */
7209  if( rc==SQLITE_OK ){
7210    sqlite3_stmt *pStmt = 0;
7211    /* ".recover" might output content in an order which causes immediate
7212    ** foreign key constraints to be violated. So disable foreign-key
7213    ** constraint enforcement to prevent problems when running the output
7214    ** script. */
7215    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7216    raw_printf(pState->out, "BEGIN;\n");
7217    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7218    shellPrepare(pState->db, &rc,
7219        "SELECT sql FROM recovery.schema "
7220        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7221    );
7222    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7223      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7224      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7225          &zCreateTable[12]
7226      );
7227    }
7228    shellFinalize(&rc, pStmt);
7229  }
7230
7231  /* Figure out if an orphan table will be required. And if so, how many
7232  ** user columns it should contain */
7233  shellPrepare(pState->db, &rc,
7234      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7235      , &pLoop
7236  );
7237  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7238    nOrphan = sqlite3_column_int(pLoop, 0);
7239  }
7240  shellFinalize(&rc, pLoop);
7241  pLoop = 0;
7242
7243  shellPrepare(pState->db, &rc,
7244      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7245  );
7246
7247  shellPrepare(pState->db, &rc,
7248      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7249      "(case when (? AND field<0) then NULL else value end)"
7250      "), ', ')"
7251      ", min(field) "
7252      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7253      "GROUP BY cell", &pCells
7254  );
7255
7256  /* Loop through each root page. */
7257  shellPrepare(pState->db, &rc,
7258      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7259      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7260      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7261      ")", &pLoop
7262  );
7263  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7264    int iRoot = sqlite3_column_int(pLoop, 0);
7265    int bIntkey = sqlite3_column_int(pLoop, 1);
7266    int nCol = sqlite3_column_int(pLoop, 2);
7267    int bNoop = 0;
7268    RecoverTable *pTab;
7269
7270    assert( bIntkey==0 || bIntkey==1 );
7271    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7272    if( bNoop || rc ) continue;
7273    if( pTab==0 ){
7274      if( pOrphan==0 ){
7275        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7276      }
7277      pTab = pOrphan;
7278      if( pTab==0 ) break;
7279    }
7280
7281    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7282      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7283    }
7284    sqlite3_bind_int(pPages, 1, iRoot);
7285    if( bRowids==0 && pTab->iPk<0 ){
7286      sqlite3_bind_int(pCells, 1, 1);
7287    }else{
7288      sqlite3_bind_int(pCells, 1, 0);
7289    }
7290    sqlite3_bind_int(pCells, 3, pTab->iPk);
7291
7292    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7293      int iPgno = sqlite3_column_int(pPages, 0);
7294      sqlite3_bind_int(pCells, 2, iPgno);
7295      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7296        int nField = sqlite3_column_int(pCells, 0);
7297        int iMin = sqlite3_column_int(pCells, 2);
7298        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7299
7300        RecoverTable *pTab2 = pTab;
7301        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7302          if( pOrphan==0 ){
7303            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7304          }
7305          pTab2 = pOrphan;
7306          if( pTab2==0 ) break;
7307        }
7308
7309        nField = nField+1;
7310        if( pTab2==pOrphan ){
7311          raw_printf(pState->out,
7312              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7313              pTab2->zQuoted, iRoot, iPgno, nField,
7314              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7315          );
7316        }else{
7317          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7318              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7319          );
7320        }
7321      }
7322      shellReset(&rc, pCells);
7323    }
7324    shellReset(&rc, pPages);
7325    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7326  }
7327  shellFinalize(&rc, pLoop);
7328  shellFinalize(&rc, pPages);
7329  shellFinalize(&rc, pCells);
7330  recoverFreeTable(pOrphan);
7331
7332  /* The rest of the schema */
7333  if( rc==SQLITE_OK ){
7334    sqlite3_stmt *pStmt = 0;
7335    shellPrepare(pState->db, &rc,
7336        "SELECT sql, name FROM recovery.schema "
7337        "WHERE sql NOT LIKE 'create table%'", &pStmt
7338    );
7339    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7340      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7341      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7342        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7343        char *zPrint = shellMPrintf(&rc,
7344          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7345          zName, zName, zSql
7346        );
7347        raw_printf(pState->out, "%s;\n", zPrint);
7348        sqlite3_free(zPrint);
7349      }else{
7350        raw_printf(pState->out, "%s;\n", zSql);
7351      }
7352    }
7353    shellFinalize(&rc, pStmt);
7354  }
7355
7356  if( rc==SQLITE_OK ){
7357    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7358    raw_printf(pState->out, "COMMIT;\n");
7359  }
7360  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7361  return rc;
7362}
7363#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7364
7365
7366/*
7367** If an input line begins with "." then invoke this routine to
7368** process that line.
7369**
7370** Return 1 on error, 2 to exit, and 0 otherwise.
7371*/
7372static int do_meta_command(char *zLine, ShellState *p){
7373  int h = 1;
7374  int nArg = 0;
7375  int n, c;
7376  int rc = 0;
7377  char *azArg[52];
7378
7379#ifndef SQLITE_OMIT_VIRTUALTABLE
7380  if( p->expert.pExpert ){
7381    expertFinish(p, 1, 0);
7382  }
7383#endif
7384
7385  /* Parse the input line into tokens.
7386  */
7387  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7388    while( IsSpace(zLine[h]) ){ h++; }
7389    if( zLine[h]==0 ) break;
7390    if( zLine[h]=='\'' || zLine[h]=='"' ){
7391      int delim = zLine[h++];
7392      azArg[nArg++] = &zLine[h];
7393      while( zLine[h] && zLine[h]!=delim ){
7394        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7395        h++;
7396      }
7397      if( zLine[h]==delim ){
7398        zLine[h++] = 0;
7399      }
7400      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7401    }else{
7402      azArg[nArg++] = &zLine[h];
7403      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7404      if( zLine[h] ) zLine[h++] = 0;
7405      resolve_backslashes(azArg[nArg-1]);
7406    }
7407  }
7408  azArg[nArg] = 0;
7409
7410  /* Process the input line.
7411  */
7412  if( nArg==0 ) return 0; /* no tokens, no error */
7413  n = strlen30(azArg[0]);
7414  c = azArg[0][0];
7415  clearTempFile(p);
7416
7417#ifndef SQLITE_OMIT_AUTHORIZATION
7418  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7419    if( nArg!=2 ){
7420      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7421      rc = 1;
7422      goto meta_command_exit;
7423    }
7424    open_db(p, 0);
7425    if( booleanValue(azArg[1]) ){
7426      sqlite3_set_authorizer(p->db, shellAuth, p);
7427    }else{
7428      sqlite3_set_authorizer(p->db, 0, 0);
7429    }
7430  }else
7431#endif
7432
7433#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7434  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7435    open_db(p, 0);
7436    rc = arDotCommand(p, 0, azArg, nArg);
7437  }else
7438#endif
7439
7440  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7441   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7442  ){
7443    const char *zDestFile = 0;
7444    const char *zDb = 0;
7445    sqlite3 *pDest;
7446    sqlite3_backup *pBackup;
7447    int j;
7448    int bAsync = 0;
7449    const char *zVfs = 0;
7450    for(j=1; j<nArg; j++){
7451      const char *z = azArg[j];
7452      if( z[0]=='-' ){
7453        if( z[1]=='-' ) z++;
7454        if( strcmp(z, "-append")==0 ){
7455          zVfs = "apndvfs";
7456        }else
7457        if( strcmp(z, "-async")==0 ){
7458          bAsync = 1;
7459        }else
7460        {
7461          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7462          return 1;
7463        }
7464      }else if( zDestFile==0 ){
7465        zDestFile = azArg[j];
7466      }else if( zDb==0 ){
7467        zDb = zDestFile;
7468        zDestFile = azArg[j];
7469      }else{
7470        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7471        return 1;
7472      }
7473    }
7474    if( zDestFile==0 ){
7475      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7476      return 1;
7477    }
7478    if( zDb==0 ) zDb = "main";
7479    rc = sqlite3_open_v2(zDestFile, &pDest,
7480                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7481    if( rc!=SQLITE_OK ){
7482      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7483      close_db(pDest);
7484      return 1;
7485    }
7486    if( bAsync ){
7487      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7488                   0, 0, 0);
7489    }
7490    open_db(p, 0);
7491    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7492    if( pBackup==0 ){
7493      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7494      close_db(pDest);
7495      return 1;
7496    }
7497    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7498    sqlite3_backup_finish(pBackup);
7499    if( rc==SQLITE_DONE ){
7500      rc = 0;
7501    }else{
7502      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7503      rc = 1;
7504    }
7505    close_db(pDest);
7506  }else
7507
7508  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7509    if( nArg==2 ){
7510      bail_on_error = booleanValue(azArg[1]);
7511    }else{
7512      raw_printf(stderr, "Usage: .bail on|off\n");
7513      rc = 1;
7514    }
7515  }else
7516
7517  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7518    if( nArg==2 ){
7519      if( booleanValue(azArg[1]) ){
7520        setBinaryMode(p->out, 1);
7521      }else{
7522        setTextMode(p->out, 1);
7523      }
7524    }else{
7525      raw_printf(stderr, "Usage: .binary on|off\n");
7526      rc = 1;
7527    }
7528  }else
7529
7530  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7531    if( nArg==2 ){
7532#if defined(_WIN32) || defined(WIN32)
7533      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7534      rc = !SetCurrentDirectoryW(z);
7535      sqlite3_free(z);
7536#else
7537      rc = chdir(azArg[1]);
7538#endif
7539      if( rc ){
7540        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7541        rc = 1;
7542      }
7543    }else{
7544      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7545      rc = 1;
7546    }
7547  }else
7548
7549  /* The undocumented ".breakpoint" command causes a call to the no-op
7550  ** routine named test_breakpoint().
7551  */
7552  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7553    test_breakpoint();
7554  }else
7555
7556  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7557    if( nArg==2 ){
7558      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7559    }else{
7560      raw_printf(stderr, "Usage: .changes on|off\n");
7561      rc = 1;
7562    }
7563  }else
7564
7565  /* Cancel output redirection, if it is currently set (by .testcase)
7566  ** Then read the content of the testcase-out.txt file and compare against
7567  ** azArg[1].  If there are differences, report an error and exit.
7568  */
7569  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7570    char *zRes = 0;
7571    output_reset(p);
7572    if( nArg!=2 ){
7573      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7574      rc = 2;
7575    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7576      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7577      rc = 2;
7578    }else if( testcase_glob(azArg[1],zRes)==0 ){
7579      utf8_printf(stderr,
7580                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7581                 p->zTestcase, azArg[1], zRes);
7582      rc = 1;
7583    }else{
7584      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7585      p->nCheck++;
7586    }
7587    sqlite3_free(zRes);
7588  }else
7589
7590  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7591    if( nArg==2 ){
7592      tryToClone(p, azArg[1]);
7593    }else{
7594      raw_printf(stderr, "Usage: .clone FILENAME\n");
7595      rc = 1;
7596    }
7597  }else
7598
7599  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7600    char **azName = 0;
7601    int nName = 0;
7602    sqlite3_stmt *pStmt;
7603    int i;
7604    open_db(p, 0);
7605    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7606    if( rc ){
7607      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7608      rc = 1;
7609    }else{
7610      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7611        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7612        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7613        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7614        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7615        azName[nName*2] = strdup(zSchema);
7616        azName[nName*2+1] = strdup(zFile);
7617        nName++;
7618      }
7619    }
7620    sqlite3_finalize(pStmt);
7621    for(i=0; i<nName; i++){
7622      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7623      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7624      const char *z = azName[i*2+1];
7625      utf8_printf(p->out, "%s: %s %s%s\n",
7626         azName[i*2],
7627         z && z[0] ? z : "\"\"",
7628         bRdonly ? "r/o" : "r/w",
7629         eTxn==SQLITE_TXN_NONE ? "" :
7630            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7631      free(azName[i*2]);
7632      free(azName[i*2+1]);
7633    }
7634    sqlite3_free(azName);
7635  }else
7636
7637  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7638    static const struct DbConfigChoices {
7639      const char *zName;
7640      int op;
7641    } aDbConfig[] = {
7642        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7643        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7644        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7645        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7646        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7647        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7648        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7649        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7650        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7651        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7652        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7653        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7654        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7655        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7656        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7657        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7658    };
7659    int ii, v;
7660    open_db(p, 0);
7661    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7662      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7663      if( nArg>=3 ){
7664        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7665      }
7666      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7667      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7668      if( nArg>1 ) break;
7669    }
7670    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7671      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7672      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7673    }
7674  }else
7675
7676  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7677    rc = shell_dbinfo_command(p, nArg, azArg);
7678  }else
7679
7680#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7681  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7682    open_db(p, 0);
7683    rc = recoverDatabaseCmd(p, nArg, azArg);
7684  }else
7685#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7686
7687  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7688    char *zLike = 0;
7689    char *zSql;
7690    int i;
7691    int savedShowHeader = p->showHeader;
7692    int savedShellFlags = p->shellFlgs;
7693    ShellClearFlag(p,
7694       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7695       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7696    for(i=1; i<nArg; i++){
7697      if( azArg[i][0]=='-' ){
7698        const char *z = azArg[i]+1;
7699        if( z[0]=='-' ) z++;
7700        if( strcmp(z,"preserve-rowids")==0 ){
7701#ifdef SQLITE_OMIT_VIRTUALTABLE
7702          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7703                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7704          rc = 1;
7705          sqlite3_free(zLike);
7706          goto meta_command_exit;
7707#else
7708          ShellSetFlag(p, SHFLG_PreserveRowid);
7709#endif
7710        }else
7711        if( strcmp(z,"newlines")==0 ){
7712          ShellSetFlag(p, SHFLG_Newlines);
7713        }else
7714        if( strcmp(z,"data-only")==0 ){
7715          ShellSetFlag(p, SHFLG_DumpDataOnly);
7716        }else
7717        if( strcmp(z,"nosys")==0 ){
7718          ShellSetFlag(p, SHFLG_DumpNoSys);
7719        }else
7720        {
7721          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7722          rc = 1;
7723          sqlite3_free(zLike);
7724          goto meta_command_exit;
7725        }
7726      }else if( zLike ){
7727        zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
7728                zLike, azArg[i]);
7729      }else{
7730        zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
7731      }
7732    }
7733
7734    open_db(p, 0);
7735
7736    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7737      /* When playing back a "dump", the content might appear in an order
7738      ** which causes immediate foreign key constraints to be violated.
7739      ** So disable foreign-key constraint enforcement to prevent problems. */
7740      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7741      raw_printf(p->out, "BEGIN TRANSACTION;\n");
7742    }
7743    p->writableSchema = 0;
7744    p->showHeader = 0;
7745    /* Set writable_schema=ON since doing so forces SQLite to initialize
7746    ** as much of the schema as it can even if the sqlite_schema table is
7747    ** corrupt. */
7748    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7749    p->nErr = 0;
7750    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7751    zSql = sqlite3_mprintf(
7752      "SELECT name, type, sql FROM sqlite_schema "
7753      "WHERE (%s) AND type=='table'"
7754      "  AND sql NOT NULL"
7755      " ORDER BY tbl_name='sqlite_sequence', rowid",
7756      zLike
7757    );
7758    run_schema_dump_query(p,zSql);
7759    sqlite3_free(zSql);
7760    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7761      zSql = sqlite3_mprintf(
7762        "SELECT sql FROM sqlite_schema "
7763        "WHERE (%s) AND sql NOT NULL"
7764        "  AND type IN ('index','trigger','view')",
7765        zLike
7766      );
7767      run_table_dump_query(p, zSql);
7768      sqlite3_free(zSql);
7769    }
7770    sqlite3_free(zLike);
7771    if( p->writableSchema ){
7772      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7773      p->writableSchema = 0;
7774    }
7775    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7776    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7777    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7778      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7779    }
7780    p->showHeader = savedShowHeader;
7781    p->shellFlgs = savedShellFlags;
7782  }else
7783
7784  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7785    if( nArg==2 ){
7786      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7787    }else{
7788      raw_printf(stderr, "Usage: .echo on|off\n");
7789      rc = 1;
7790    }
7791  }else
7792
7793  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7794    if( nArg==2 ){
7795      p->autoEQPtest = 0;
7796      if( p->autoEQPtrace ){
7797        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7798        p->autoEQPtrace = 0;
7799      }
7800      if( strcmp(azArg[1],"full")==0 ){
7801        p->autoEQP = AUTOEQP_full;
7802      }else if( strcmp(azArg[1],"trigger")==0 ){
7803        p->autoEQP = AUTOEQP_trigger;
7804#ifdef SQLITE_DEBUG
7805      }else if( strcmp(azArg[1],"test")==0 ){
7806        p->autoEQP = AUTOEQP_on;
7807        p->autoEQPtest = 1;
7808      }else if( strcmp(azArg[1],"trace")==0 ){
7809        p->autoEQP = AUTOEQP_full;
7810        p->autoEQPtrace = 1;
7811        open_db(p, 0);
7812        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
7813        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7814#endif
7815      }else{
7816        p->autoEQP = (u8)booleanValue(azArg[1]);
7817      }
7818    }else{
7819      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7820      rc = 1;
7821    }
7822  }else
7823
7824  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7825    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7826    rc = 2;
7827  }else
7828
7829  /* The ".explain" command is automatic now.  It is largely pointless.  It
7830  ** retained purely for backwards compatibility */
7831  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7832    int val = 1;
7833    if( nArg>=2 ){
7834      if( strcmp(azArg[1],"auto")==0 ){
7835        val = 99;
7836      }else{
7837        val =  booleanValue(azArg[1]);
7838      }
7839    }
7840    if( val==1 && p->mode!=MODE_Explain ){
7841      p->normalMode = p->mode;
7842      p->mode = MODE_Explain;
7843      p->autoExplain = 0;
7844    }else if( val==0 ){
7845      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7846      p->autoExplain = 0;
7847    }else if( val==99 ){
7848      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7849      p->autoExplain = 1;
7850    }
7851  }else
7852
7853#ifndef SQLITE_OMIT_VIRTUALTABLE
7854  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7855    open_db(p, 0);
7856    expertDotCommand(p, azArg, nArg);
7857  }else
7858#endif
7859
7860  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7861    static const struct {
7862       const char *zCtrlName;   /* Name of a test-control option */
7863       int ctrlCode;            /* Integer code for that option */
7864       const char *zUsage;      /* Usage notes */
7865    } aCtrl[] = {
7866      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7867      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7868   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7869      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7870      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7871   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7872      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7873      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7874      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7875      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
7876    };
7877    int filectrl = -1;
7878    int iCtrl = -1;
7879    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7880    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7881    int n2, i;
7882    const char *zCmd = 0;
7883    const char *zSchema = 0;
7884
7885    open_db(p, 0);
7886    zCmd = nArg>=2 ? azArg[1] : "help";
7887
7888    if( zCmd[0]=='-'
7889     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7890     && nArg>=4
7891    ){
7892      zSchema = azArg[2];
7893      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7894      nArg -= 2;
7895      zCmd = azArg[1];
7896    }
7897
7898    /* The argument can optionally begin with "-" or "--" */
7899    if( zCmd[0]=='-' && zCmd[1] ){
7900      zCmd++;
7901      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7902    }
7903
7904    /* --help lists all file-controls */
7905    if( strcmp(zCmd,"help")==0 ){
7906      utf8_printf(p->out, "Available file-controls:\n");
7907      for(i=0; i<ArraySize(aCtrl); i++){
7908        utf8_printf(p->out, "  .filectrl %s %s\n",
7909                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7910      }
7911      rc = 1;
7912      goto meta_command_exit;
7913    }
7914
7915    /* convert filectrl text option to value. allow any unique prefix
7916    ** of the option name, or a numerical value. */
7917    n2 = strlen30(zCmd);
7918    for(i=0; i<ArraySize(aCtrl); i++){
7919      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7920        if( filectrl<0 ){
7921          filectrl = aCtrl[i].ctrlCode;
7922          iCtrl = i;
7923        }else{
7924          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7925                              "Use \".filectrl --help\" for help\n", zCmd);
7926          rc = 1;
7927          goto meta_command_exit;
7928        }
7929      }
7930    }
7931    if( filectrl<0 ){
7932      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7933                         "Use \".filectrl --help\" for help\n", zCmd);
7934    }else{
7935      switch(filectrl){
7936        case SQLITE_FCNTL_SIZE_LIMIT: {
7937          if( nArg!=2 && nArg!=3 ) break;
7938          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7939          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7940          isOk = 1;
7941          break;
7942        }
7943        case SQLITE_FCNTL_LOCK_TIMEOUT:
7944        case SQLITE_FCNTL_CHUNK_SIZE: {
7945          int x;
7946          if( nArg!=3 ) break;
7947          x = (int)integerValue(azArg[2]);
7948          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7949          isOk = 2;
7950          break;
7951        }
7952        case SQLITE_FCNTL_PERSIST_WAL:
7953        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7954          int x;
7955          if( nArg!=2 && nArg!=3 ) break;
7956          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7957          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7958          iRes = x;
7959          isOk = 1;
7960          break;
7961        }
7962        case SQLITE_FCNTL_HAS_MOVED: {
7963          int x;
7964          if( nArg!=2 ) break;
7965          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7966          iRes = x;
7967          isOk = 1;
7968          break;
7969        }
7970        case SQLITE_FCNTL_TEMPFILENAME: {
7971          char *z = 0;
7972          if( nArg!=2 ) break;
7973          sqlite3_file_control(p->db, zSchema, filectrl, &z);
7974          if( z ){
7975            utf8_printf(p->out, "%s\n", z);
7976            sqlite3_free(z);
7977          }
7978          isOk = 2;
7979          break;
7980        }
7981        case SQLITE_FCNTL_RESERVE_BYTES: {
7982          int x;
7983          if( nArg>=3 ){
7984            x = atoi(azArg[2]);
7985            sqlite3_file_control(p->db, zSchema, filectrl, &x);
7986          }
7987          x = -1;
7988          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7989          utf8_printf(p->out,"%d\n", x);
7990          isOk = 2;
7991          break;
7992        }
7993      }
7994    }
7995    if( isOk==0 && iCtrl>=0 ){
7996      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
7997      rc = 1;
7998    }else if( isOk==1 ){
7999      char zBuf[100];
8000      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8001      raw_printf(p->out, "%s\n", zBuf);
8002    }
8003  }else
8004
8005  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8006    ShellState data;
8007    char *zErrMsg = 0;
8008    int doStats = 0;
8009    memcpy(&data, p, sizeof(data));
8010    data.showHeader = 0;
8011    data.cMode = data.mode = MODE_Semi;
8012    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8013      data.cMode = data.mode = MODE_Pretty;
8014      nArg = 1;
8015    }
8016    if( nArg!=1 ){
8017      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8018      rc = 1;
8019      goto meta_command_exit;
8020    }
8021    open_db(p, 0);
8022    rc = sqlite3_exec(p->db,
8023       "SELECT sql FROM"
8024       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8025       "     FROM sqlite_schema UNION ALL"
8026       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8027       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8028       "ORDER BY rowid",
8029       callback, &data, &zErrMsg
8030    );
8031    if( rc==SQLITE_OK ){
8032      sqlite3_stmt *pStmt;
8033      rc = sqlite3_prepare_v2(p->db,
8034               "SELECT rowid FROM sqlite_schema"
8035               " WHERE name GLOB 'sqlite_stat[134]'",
8036               -1, &pStmt, 0);
8037      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8038      sqlite3_finalize(pStmt);
8039    }
8040    if( doStats==0 ){
8041      raw_printf(p->out, "/* No STAT tables available */\n");
8042    }else{
8043      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8044      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
8045                   callback, &data, &zErrMsg);
8046      data.cMode = data.mode = MODE_Insert;
8047      data.zDestTable = "sqlite_stat1";
8048      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
8049      data.zDestTable = "sqlite_stat4";
8050      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
8051      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8052    }
8053  }else
8054
8055  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8056    if( nArg==2 ){
8057      p->showHeader = booleanValue(azArg[1]);
8058      p->shellFlgs |= SHFLG_HeaderSet;
8059    }else{
8060      raw_printf(stderr, "Usage: .headers on|off\n");
8061      rc = 1;
8062    }
8063  }else
8064
8065  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8066    if( nArg>=2 ){
8067      n = showHelp(p->out, azArg[1]);
8068      if( n==0 ){
8069        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8070      }
8071    }else{
8072      showHelp(p->out, 0);
8073    }
8074  }else
8075
8076  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8077    char *zTable = 0;           /* Insert data into this table */
8078    char *zFile = 0;            /* Name of file to extra content from */
8079    sqlite3_stmt *pStmt = NULL; /* A statement */
8080    int nCol;                   /* Number of columns in the table */
8081    int nByte;                  /* Number of bytes in an SQL string */
8082    int i, j;                   /* Loop counters */
8083    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8084    int nSep;                   /* Number of bytes in p->colSeparator[] */
8085    char *zSql;                 /* An SQL statement */
8086    ImportCtx sCtx;             /* Reader context */
8087    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8088    int eVerbose = 0;           /* Larger for more console output */
8089    int nSkip = 0;              /* Initial lines to skip */
8090    int useOutputMode = 1;      /* Use output mode to determine separators */
8091
8092    memset(&sCtx, 0, sizeof(sCtx));
8093    if( p->mode==MODE_Ascii ){
8094      xRead = ascii_read_one_field;
8095    }else{
8096      xRead = csv_read_one_field;
8097    }
8098    for(i=1; i<nArg; i++){
8099      char *z = azArg[i];
8100      if( z[0]=='-' && z[1]=='-' ) z++;
8101      if( z[0]!='-' ){
8102        if( zFile==0 ){
8103          zFile = z;
8104        }else if( zTable==0 ){
8105          zTable = z;
8106        }else{
8107          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8108          showHelp(p->out, "import");
8109          rc = 1;
8110          goto meta_command_exit;
8111        }
8112      }else if( strcmp(z,"-v")==0 ){
8113        eVerbose++;
8114      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8115        nSkip = integerValue(azArg[++i]);
8116      }else if( strcmp(z,"-ascii")==0 ){
8117        sCtx.cColSep = SEP_Unit[0];
8118        sCtx.cRowSep = SEP_Record[0];
8119        xRead = ascii_read_one_field;
8120        useOutputMode = 0;
8121      }else if( strcmp(z,"-csv")==0 ){
8122        sCtx.cColSep = ',';
8123        sCtx.cRowSep = '\n';
8124        xRead = csv_read_one_field;
8125        useOutputMode = 0;
8126      }else{
8127        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8128        showHelp(p->out, "import");
8129        rc = 1;
8130        goto meta_command_exit;
8131      }
8132    }
8133    if( zTable==0 ){
8134      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8135                  zFile==0 ? "FILE" : "TABLE");
8136      showHelp(p->out, "import");
8137      rc = 1;
8138      goto meta_command_exit;
8139    }
8140    seenInterrupt = 0;
8141    open_db(p, 0);
8142    if( useOutputMode ){
8143      /* If neither the --csv or --ascii options are specified, then set
8144      ** the column and row separator characters from the output mode. */
8145      nSep = strlen30(p->colSeparator);
8146      if( nSep==0 ){
8147        raw_printf(stderr,
8148                   "Error: non-null column separator required for import\n");
8149        rc = 1;
8150        goto meta_command_exit;
8151      }
8152      if( nSep>1 ){
8153        raw_printf(stderr,
8154              "Error: multi-character column separators not allowed"
8155              " for import\n");
8156        rc = 1;
8157        goto meta_command_exit;
8158      }
8159      nSep = strlen30(p->rowSeparator);
8160      if( nSep==0 ){
8161        raw_printf(stderr,
8162            "Error: non-null row separator required for import\n");
8163        rc = 1;
8164        goto meta_command_exit;
8165      }
8166      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8167        /* When importing CSV (only), if the row separator is set to the
8168        ** default output row separator, change it to the default input
8169        ** row separator.  This avoids having to maintain different input
8170        ** and output row separators. */
8171        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8172        nSep = strlen30(p->rowSeparator);
8173      }
8174      if( nSep>1 ){
8175        raw_printf(stderr, "Error: multi-character row separators not allowed"
8176                           " for import\n");
8177        rc = 1;
8178        goto meta_command_exit;
8179      }
8180      sCtx.cColSep = p->colSeparator[0];
8181      sCtx.cRowSep = p->rowSeparator[0];
8182    }
8183    sCtx.zFile = zFile;
8184    sCtx.nLine = 1;
8185    if( sCtx.zFile[0]=='|' ){
8186#ifdef SQLITE_OMIT_POPEN
8187      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8188      rc = 1;
8189      goto meta_command_exit;
8190#else
8191      sCtx.in = popen(sCtx.zFile+1, "r");
8192      sCtx.zFile = "<pipe>";
8193      sCtx.xCloser = pclose;
8194#endif
8195    }else{
8196      sCtx.in = fopen(sCtx.zFile, "rb");
8197      sCtx.xCloser = fclose;
8198    }
8199    if( sCtx.in==0 ){
8200      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8201      rc = 1;
8202      goto meta_command_exit;
8203    }
8204    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8205      char zSep[2];
8206      zSep[1] = 0;
8207      zSep[0] = sCtx.cColSep;
8208      utf8_printf(p->out, "Column separator ");
8209      output_c_string(p->out, zSep);
8210      utf8_printf(p->out, ", row separator ");
8211      zSep[0] = sCtx.cRowSep;
8212      output_c_string(p->out, zSep);
8213      utf8_printf(p->out, "\n");
8214    }
8215    while( (nSkip--)>0 ){
8216      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8217    }
8218    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
8219    if( zSql==0 ){
8220      import_cleanup(&sCtx);
8221      shell_out_of_memory();
8222    }
8223    nByte = strlen30(zSql);
8224    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8225    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8226    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8227      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
8228      char cSep = '(';
8229      while( xRead(&sCtx) ){
8230        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8231        cSep = ',';
8232        if( sCtx.cTerm!=sCtx.cColSep ) break;
8233      }
8234      if( cSep=='(' ){
8235        sqlite3_free(zCreate);
8236        import_cleanup(&sCtx);
8237        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8238        rc = 1;
8239        goto meta_command_exit;
8240      }
8241      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8242      if( eVerbose>=1 ){
8243        utf8_printf(p->out, "%s\n", zCreate);
8244      }
8245      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8246      sqlite3_free(zCreate);
8247      if( rc ){
8248        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
8249                sqlite3_errmsg(p->db));
8250        import_cleanup(&sCtx);
8251        rc = 1;
8252        goto meta_command_exit;
8253      }
8254      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8255    }
8256    sqlite3_free(zSql);
8257    if( rc ){
8258      if (pStmt) sqlite3_finalize(pStmt);
8259      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8260      import_cleanup(&sCtx);
8261      rc = 1;
8262      goto meta_command_exit;
8263    }
8264    nCol = sqlite3_column_count(pStmt);
8265    sqlite3_finalize(pStmt);
8266    pStmt = 0;
8267    if( nCol==0 ) return 0; /* no columns, no error */
8268    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8269    if( zSql==0 ){
8270      import_cleanup(&sCtx);
8271      shell_out_of_memory();
8272    }
8273    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8274    j = strlen30(zSql);
8275    for(i=1; i<nCol; i++){
8276      zSql[j++] = ',';
8277      zSql[j++] = '?';
8278    }
8279    zSql[j++] = ')';
8280    zSql[j] = 0;
8281    if( eVerbose>=2 ){
8282      utf8_printf(p->out, "Insert using: %s\n", zSql);
8283    }
8284    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8285    sqlite3_free(zSql);
8286    if( rc ){
8287      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8288      if (pStmt) sqlite3_finalize(pStmt);
8289      import_cleanup(&sCtx);
8290      rc = 1;
8291      goto meta_command_exit;
8292    }
8293    needCommit = sqlite3_get_autocommit(p->db);
8294    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8295    do{
8296      int startLine = sCtx.nLine;
8297      for(i=0; i<nCol; i++){
8298        char *z = xRead(&sCtx);
8299        /*
8300        ** Did we reach end-of-file before finding any columns?
8301        ** If so, stop instead of NULL filling the remaining columns.
8302        */
8303        if( z==0 && i==0 ) break;
8304        /*
8305        ** Did we reach end-of-file OR end-of-line before finding any
8306        ** columns in ASCII mode?  If so, stop instead of NULL filling
8307        ** the remaining columns.
8308        */
8309        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8310        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8311        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8312          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8313                          "filling the rest with NULL\n",
8314                          sCtx.zFile, startLine, nCol, i+1);
8315          i += 2;
8316          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8317        }
8318      }
8319      if( sCtx.cTerm==sCtx.cColSep ){
8320        do{
8321          xRead(&sCtx);
8322          i++;
8323        }while( sCtx.cTerm==sCtx.cColSep );
8324        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8325                        "extras ignored\n",
8326                        sCtx.zFile, startLine, nCol, i);
8327      }
8328      if( i>=nCol ){
8329        sqlite3_step(pStmt);
8330        rc = sqlite3_reset(pStmt);
8331        if( rc!=SQLITE_OK ){
8332          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8333                      startLine, sqlite3_errmsg(p->db));
8334          sCtx.nErr++;
8335        }else{
8336          sCtx.nRow++;
8337        }
8338      }
8339    }while( sCtx.cTerm!=EOF );
8340
8341    import_cleanup(&sCtx);
8342    sqlite3_finalize(pStmt);
8343    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8344    if( eVerbose>0 ){
8345      utf8_printf(p->out,
8346          "Added %d rows with %d errors using %d lines of input\n",
8347          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8348    }
8349  }else
8350
8351#ifndef SQLITE_UNTESTABLE
8352  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8353    char *zSql;
8354    char *zCollist = 0;
8355    sqlite3_stmt *pStmt;
8356    int tnum = 0;
8357    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8358    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8359    int i;
8360    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8361      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8362                          "       .imposter off\n");
8363      /* Also allowed, but not documented:
8364      **
8365      **    .imposter TABLE IMPOSTER
8366      **
8367      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8368      ** imposter is another WITHOUT ROWID table with the columns in
8369      ** storage order. */
8370      rc = 1;
8371      goto meta_command_exit;
8372    }
8373    open_db(p, 0);
8374    if( nArg==2 ){
8375      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8376      goto meta_command_exit;
8377    }
8378    zSql = sqlite3_mprintf(
8379      "SELECT rootpage, 0 FROM sqlite_schema"
8380      " WHERE name='%q' AND type='index'"
8381      "UNION ALL "
8382      "SELECT rootpage, 1 FROM sqlite_schema"
8383      " WHERE name='%q' AND type='table'"
8384      "   AND sql LIKE '%%without%%rowid%%'",
8385      azArg[1], azArg[1]
8386    );
8387    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8388    sqlite3_free(zSql);
8389    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8390      tnum = sqlite3_column_int(pStmt, 0);
8391      isWO = sqlite3_column_int(pStmt, 1);
8392    }
8393    sqlite3_finalize(pStmt);
8394    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8395    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8396    sqlite3_free(zSql);
8397    i = 0;
8398    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8399      char zLabel[20];
8400      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8401      i++;
8402      if( zCol==0 ){
8403        if( sqlite3_column_int(pStmt,1)==-1 ){
8404          zCol = "_ROWID_";
8405        }else{
8406          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8407          zCol = zLabel;
8408        }
8409      }
8410      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8411        lenPK = (int)strlen(zCollist);
8412      }
8413      if( zCollist==0 ){
8414        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8415      }else{
8416        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8417      }
8418    }
8419    sqlite3_finalize(pStmt);
8420    if( i==0 || tnum==0 ){
8421      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8422      rc = 1;
8423      sqlite3_free(zCollist);
8424      goto meta_command_exit;
8425    }
8426    if( lenPK==0 ) lenPK = 100000;
8427    zSql = sqlite3_mprintf(
8428          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8429          azArg[2], zCollist, lenPK, zCollist);
8430    sqlite3_free(zCollist);
8431    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8432    if( rc==SQLITE_OK ){
8433      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8434      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8435      if( rc ){
8436        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8437      }else{
8438        utf8_printf(stdout, "%s;\n", zSql);
8439        raw_printf(stdout,
8440          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8441          azArg[1], isWO ? "table" : "index"
8442        );
8443      }
8444    }else{
8445      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8446      rc = 1;
8447    }
8448    sqlite3_free(zSql);
8449  }else
8450#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8451
8452#ifdef SQLITE_ENABLE_IOTRACE
8453  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8454    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8455    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8456    iotrace = 0;
8457    if( nArg<2 ){
8458      sqlite3IoTrace = 0;
8459    }else if( strcmp(azArg[1], "-")==0 ){
8460      sqlite3IoTrace = iotracePrintf;
8461      iotrace = stdout;
8462    }else{
8463      iotrace = fopen(azArg[1], "w");
8464      if( iotrace==0 ){
8465        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8466        sqlite3IoTrace = 0;
8467        rc = 1;
8468      }else{
8469        sqlite3IoTrace = iotracePrintf;
8470      }
8471    }
8472  }else
8473#endif
8474
8475  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8476    static const struct {
8477       const char *zLimitName;   /* Name of a limit */
8478       int limitCode;            /* Integer code for that limit */
8479    } aLimit[] = {
8480      { "length",                SQLITE_LIMIT_LENGTH                    },
8481      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8482      { "column",                SQLITE_LIMIT_COLUMN                    },
8483      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8484      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8485      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8486      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8487      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8488      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8489      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8490      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8491      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8492    };
8493    int i, n2;
8494    open_db(p, 0);
8495    if( nArg==1 ){
8496      for(i=0; i<ArraySize(aLimit); i++){
8497        printf("%20s %d\n", aLimit[i].zLimitName,
8498               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8499      }
8500    }else if( nArg>3 ){
8501      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8502      rc = 1;
8503      goto meta_command_exit;
8504    }else{
8505      int iLimit = -1;
8506      n2 = strlen30(azArg[1]);
8507      for(i=0; i<ArraySize(aLimit); i++){
8508        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8509          if( iLimit<0 ){
8510            iLimit = i;
8511          }else{
8512            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8513            rc = 1;
8514            goto meta_command_exit;
8515          }
8516        }
8517      }
8518      if( iLimit<0 ){
8519        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8520                        "enter \".limits\" with no arguments for a list.\n",
8521                         azArg[1]);
8522        rc = 1;
8523        goto meta_command_exit;
8524      }
8525      if( nArg==3 ){
8526        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8527                      (int)integerValue(azArg[2]));
8528      }
8529      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8530             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8531    }
8532  }else
8533
8534  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8535    open_db(p, 0);
8536    lintDotCommand(p, azArg, nArg);
8537  }else
8538
8539#ifndef SQLITE_OMIT_LOAD_EXTENSION
8540  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8541    const char *zFile, *zProc;
8542    char *zErrMsg = 0;
8543    if( nArg<2 ){
8544      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8545      rc = 1;
8546      goto meta_command_exit;
8547    }
8548    zFile = azArg[1];
8549    zProc = nArg>=3 ? azArg[2] : 0;
8550    open_db(p, 0);
8551    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8552    if( rc!=SQLITE_OK ){
8553      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8554      sqlite3_free(zErrMsg);
8555      rc = 1;
8556    }
8557  }else
8558#endif
8559
8560  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8561    if( nArg!=2 ){
8562      raw_printf(stderr, "Usage: .log FILENAME\n");
8563      rc = 1;
8564    }else{
8565      const char *zFile = azArg[1];
8566      output_file_close(p->pLog);
8567      p->pLog = output_file_open(zFile, 0);
8568    }
8569  }else
8570
8571  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8572    const char *zMode = nArg>=2 ? azArg[1] : "";
8573    int n2 = strlen30(zMode);
8574    int c2 = zMode[0];
8575    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8576      p->mode = MODE_Line;
8577      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8578    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8579      p->mode = MODE_Column;
8580      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8581        p->showHeader = 1;
8582      }
8583      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8584    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8585      p->mode = MODE_List;
8586      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8587      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8588    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8589      p->mode = MODE_Html;
8590    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8591      p->mode = MODE_Tcl;
8592      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8593      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8594    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8595      p->mode = MODE_Csv;
8596      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8597      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8598    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8599      p->mode = MODE_List;
8600      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8601    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8602      p->mode = MODE_Insert;
8603      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8604    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8605      p->mode = MODE_Quote;
8606      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8607      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8608    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8609      p->mode = MODE_Ascii;
8610      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8611      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8612    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8613      p->mode = MODE_Markdown;
8614    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8615      p->mode = MODE_Table;
8616    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8617      p->mode = MODE_Box;
8618    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8619      p->mode = MODE_Json;
8620    }else if( nArg==1 ){
8621      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8622    }else{
8623      raw_printf(stderr, "Error: mode should be one of: "
8624         "ascii box column csv html insert json line list markdown "
8625         "quote table tabs tcl\n");
8626      rc = 1;
8627    }
8628    p->cMode = p->mode;
8629  }else
8630
8631  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8632    if( nArg==2 ){
8633      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8634                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8635    }else{
8636      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8637      rc = 1;
8638    }
8639  }else
8640
8641#ifdef SQLITE_DEBUG
8642  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8643    int i;
8644    for(i=1; i<nArg; i++){
8645      const char *z = azArg[i];
8646      if( z[0]=='-' && z[1]=='-' ) z++;
8647      if( strcmp(z,"-repeat")==0 ){
8648        if( i==nArg-1 ){
8649          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8650          rc = 1;
8651        }else{
8652          oomRepeat = (int)integerValue(azArg[++i]);
8653        }
8654      }else if( IsDigit(z[0]) ){
8655        oomCounter = (int)integerValue(azArg[i]);
8656      }else{
8657        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8658        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8659        rc = 1;
8660      }
8661    }
8662    if( rc==0 ){
8663      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8664      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8665    }
8666  }else
8667#endif /* SQLITE_DEBUG */
8668
8669  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8670    char *zNewFilename = 0;  /* Name of the database file to open */
8671    int iName = 1;           /* Index in azArg[] of the filename */
8672    int newFlag = 0;         /* True to delete file before opening */
8673    /* Close the existing database */
8674    session_close_all(p);
8675    close_db(p->db);
8676    p->db = 0;
8677    p->zDbFilename = 0;
8678    sqlite3_free(p->zFreeOnClose);
8679    p->zFreeOnClose = 0;
8680    p->openMode = SHELL_OPEN_UNSPEC;
8681    p->openFlags = 0;
8682    p->szMax = 0;
8683    /* Check for command-line arguments */
8684    for(iName=1; iName<nArg; iName++){
8685      const char *z = azArg[iName];
8686      if( optionMatch(z,"new") ){
8687        newFlag = 1;
8688#ifdef SQLITE_HAVE_ZLIB
8689      }else if( optionMatch(z, "zip") ){
8690        p->openMode = SHELL_OPEN_ZIPFILE;
8691#endif
8692      }else if( optionMatch(z, "append") ){
8693        p->openMode = SHELL_OPEN_APPENDVFS;
8694      }else if( optionMatch(z, "readonly") ){
8695        p->openMode = SHELL_OPEN_READONLY;
8696      }else if( optionMatch(z, "nofollow") ){
8697        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8698#ifdef SQLITE_ENABLE_DESERIALIZE
8699      }else if( optionMatch(z, "deserialize") ){
8700        p->openMode = SHELL_OPEN_DESERIALIZE;
8701      }else if( optionMatch(z, "hexdb") ){
8702        p->openMode = SHELL_OPEN_HEXDB;
8703      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8704        p->szMax = integerValue(azArg[++iName]);
8705#endif /* SQLITE_ENABLE_DESERIALIZE */
8706      }else if( z[0]=='-' ){
8707        utf8_printf(stderr, "unknown option: %s\n", z);
8708        rc = 1;
8709        goto meta_command_exit;
8710      }else if( zNewFilename ){
8711        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
8712        rc = 1;
8713        goto meta_command_exit;
8714      }else{
8715        zNewFilename = sqlite3_mprintf("%s", z);
8716      }
8717    }
8718    /* If a filename is specified, try to open it first */
8719    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8720      if( newFlag ) shellDeleteFile(zNewFilename);
8721      p->zDbFilename = zNewFilename;
8722      open_db(p, OPEN_DB_KEEPALIVE);
8723      if( p->db==0 ){
8724        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8725        sqlite3_free(zNewFilename);
8726      }else{
8727        p->zFreeOnClose = zNewFilename;
8728      }
8729    }
8730    if( p->db==0 ){
8731      /* As a fall-back open a TEMP database */
8732      p->zDbFilename = 0;
8733      open_db(p, 0);
8734    }
8735  }else
8736
8737  if( (c=='o'
8738        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8739   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8740  ){
8741    const char *zFile = 0;
8742    int bTxtMode = 0;
8743    int i;
8744    int eMode = 0;
8745    int bBOM = 0;
8746    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8747
8748    if( c=='e' ){
8749      eMode = 'x';
8750      bOnce = 2;
8751    }else if( strncmp(azArg[0],"once",n)==0 ){
8752      bOnce = 1;
8753    }
8754    for(i=1; i<nArg; i++){
8755      char *z = azArg[i];
8756      if( z[0]=='-' ){
8757        if( z[1]=='-' ) z++;
8758        if( strcmp(z,"-bom")==0 ){
8759          bBOM = 1;
8760        }else if( c!='e' && strcmp(z,"-x")==0 ){
8761          eMode = 'x';  /* spreadsheet */
8762        }else if( c!='e' && strcmp(z,"-e")==0 ){
8763          eMode = 'e';  /* text editor */
8764        }else{
8765          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8766                      azArg[i]);
8767          showHelp(p->out, azArg[0]);
8768          rc = 1;
8769          goto meta_command_exit;
8770        }
8771      }else if( zFile==0 ){
8772        zFile = z;
8773      }else{
8774        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8775                    azArg[i]);
8776        showHelp(p->out, azArg[0]);
8777        rc = 1;
8778        goto meta_command_exit;
8779      }
8780    }
8781    if( zFile==0 ) zFile = "stdout";
8782    if( bOnce ){
8783      p->outCount = 2;
8784    }else{
8785      p->outCount = 0;
8786    }
8787    output_reset(p);
8788#ifndef SQLITE_NOHAVE_SYSTEM
8789    if( eMode=='e' || eMode=='x' ){
8790      p->doXdgOpen = 1;
8791      outputModePush(p);
8792      if( eMode=='x' ){
8793        /* spreadsheet mode.  Output as CSV. */
8794        newTempFile(p, "csv");
8795        ShellClearFlag(p, SHFLG_Echo);
8796        p->mode = MODE_Csv;
8797        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8798        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8799      }else{
8800        /* text editor mode */
8801        newTempFile(p, "txt");
8802        bTxtMode = 1;
8803      }
8804      zFile = p->zTempFile;
8805    }
8806#endif /* SQLITE_NOHAVE_SYSTEM */
8807    if( zFile[0]=='|' ){
8808#ifdef SQLITE_OMIT_POPEN
8809      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8810      rc = 1;
8811      p->out = stdout;
8812#else
8813      p->out = popen(zFile + 1, "w");
8814      if( p->out==0 ){
8815        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8816        p->out = stdout;
8817        rc = 1;
8818      }else{
8819        if( bBOM ) fprintf(p->out,"\357\273\277");
8820        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8821      }
8822#endif
8823    }else{
8824      p->out = output_file_open(zFile, bTxtMode);
8825      if( p->out==0 ){
8826        if( strcmp(zFile,"off")!=0 ){
8827          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8828        }
8829        p->out = stdout;
8830        rc = 1;
8831      } else {
8832        if( bBOM ) fprintf(p->out,"\357\273\277");
8833        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8834      }
8835    }
8836  }else
8837
8838  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8839    open_db(p,0);
8840    if( nArg<=1 ) goto parameter_syntax_error;
8841
8842    /* .parameter clear
8843    ** Clear all bind parameters by dropping the TEMP table that holds them.
8844    */
8845    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8846      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8847                   0, 0, 0);
8848    }else
8849
8850    /* .parameter list
8851    ** List all bind parameters.
8852    */
8853    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8854      sqlite3_stmt *pStmt = 0;
8855      int rx;
8856      int len = 0;
8857      rx = sqlite3_prepare_v2(p->db,
8858             "SELECT max(length(key)) "
8859             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8860      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8861        len = sqlite3_column_int(pStmt, 0);
8862        if( len>40 ) len = 40;
8863      }
8864      sqlite3_finalize(pStmt);
8865      pStmt = 0;
8866      if( len ){
8867        rx = sqlite3_prepare_v2(p->db,
8868             "SELECT key, quote(value) "
8869             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8870        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8871          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8872                      sqlite3_column_text(pStmt,1));
8873        }
8874        sqlite3_finalize(pStmt);
8875      }
8876    }else
8877
8878    /* .parameter init
8879    ** Make sure the TEMP table used to hold bind parameters exists.
8880    ** Create it if necessary.
8881    */
8882    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8883      bind_table_init(p);
8884    }else
8885
8886    /* .parameter set NAME VALUE
8887    ** Set or reset a bind parameter.  NAME should be the full parameter
8888    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8889    ** VALUE can be in either SQL literal notation, or if not it will be
8890    ** understood to be a text string.
8891    */
8892    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8893      int rx;
8894      char *zSql;
8895      sqlite3_stmt *pStmt;
8896      const char *zKey = azArg[2];
8897      const char *zValue = azArg[3];
8898      bind_table_init(p);
8899      zSql = sqlite3_mprintf(
8900                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8901                  "VALUES(%Q,%s);", zKey, zValue);
8902      if( zSql==0 ) shell_out_of_memory();
8903      pStmt = 0;
8904      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8905      sqlite3_free(zSql);
8906      if( rx!=SQLITE_OK ){
8907        sqlite3_finalize(pStmt);
8908        pStmt = 0;
8909        zSql = sqlite3_mprintf(
8910                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8911                   "VALUES(%Q,%Q);", zKey, zValue);
8912        if( zSql==0 ) shell_out_of_memory();
8913        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8914        sqlite3_free(zSql);
8915        if( rx!=SQLITE_OK ){
8916          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8917          sqlite3_finalize(pStmt);
8918          pStmt = 0;
8919          rc = 1;
8920        }
8921      }
8922      sqlite3_step(pStmt);
8923      sqlite3_finalize(pStmt);
8924    }else
8925
8926    /* .parameter unset NAME
8927    ** Remove the NAME binding from the parameter binding table, if it
8928    ** exists.
8929    */
8930    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8931      char *zSql = sqlite3_mprintf(
8932          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8933      if( zSql==0 ) shell_out_of_memory();
8934      sqlite3_exec(p->db, zSql, 0, 0, 0);
8935      sqlite3_free(zSql);
8936    }else
8937    /* If no command name matches, show a syntax error */
8938    parameter_syntax_error:
8939    showHelp(p->out, "parameter");
8940  }else
8941
8942  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8943    int i;
8944    for(i=1; i<nArg; i++){
8945      if( i>1 ) raw_printf(p->out, " ");
8946      utf8_printf(p->out, "%s", azArg[i]);
8947    }
8948    raw_printf(p->out, "\n");
8949  }else
8950
8951#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8952  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8953    int i;
8954    int nn = 0;
8955    p->flgProgress = 0;
8956    p->mxProgress = 0;
8957    p->nProgress = 0;
8958    for(i=1; i<nArg; i++){
8959      const char *z = azArg[i];
8960      if( z[0]=='-' ){
8961        z++;
8962        if( z[0]=='-' ) z++;
8963        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
8964          p->flgProgress |= SHELL_PROGRESS_QUIET;
8965          continue;
8966        }
8967        if( strcmp(z,"reset")==0 ){
8968          p->flgProgress |= SHELL_PROGRESS_RESET;
8969          continue;
8970        }
8971        if( strcmp(z,"once")==0 ){
8972          p->flgProgress |= SHELL_PROGRESS_ONCE;
8973          continue;
8974        }
8975        if( strcmp(z,"limit")==0 ){
8976          if( i+1>=nArg ){
8977            utf8_printf(stderr, "Error: missing argument on --limit\n");
8978            rc = 1;
8979            goto meta_command_exit;
8980          }else{
8981            p->mxProgress = (int)integerValue(azArg[++i]);
8982          }
8983          continue;
8984        }
8985        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
8986        rc = 1;
8987        goto meta_command_exit;
8988      }else{
8989        nn = (int)integerValue(z);
8990      }
8991    }
8992    open_db(p, 0);
8993    sqlite3_progress_handler(p->db, nn, progress_handler, p);
8994  }else
8995#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
8996
8997  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
8998    if( nArg >= 2) {
8999      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9000    }
9001    if( nArg >= 3) {
9002      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9003    }
9004  }else
9005
9006  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
9007    rc = 2;
9008  }else
9009
9010  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
9011    FILE *inSaved = p->in;
9012    int savedLineno = p->lineno;
9013    if( nArg!=2 ){
9014      raw_printf(stderr, "Usage: .read FILE\n");
9015      rc = 1;
9016      goto meta_command_exit;
9017    }
9018    if( azArg[1][0]=='|' ){
9019      p->in = popen(azArg[1]+1, "r");
9020      if( p->in==0 ){
9021        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9022        rc = 1;
9023      }else{
9024        rc = process_input(p);
9025        pclose(p->in);
9026      }
9027    }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
9028      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9029      rc = 1;
9030    }else{
9031      rc = process_input(p);
9032      fclose(p->in);
9033    }
9034    p->in = inSaved;
9035    p->lineno = savedLineno;
9036  }else
9037
9038  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9039    const char *zSrcFile;
9040    const char *zDb;
9041    sqlite3 *pSrc;
9042    sqlite3_backup *pBackup;
9043    int nTimeout = 0;
9044
9045    if( nArg==2 ){
9046      zSrcFile = azArg[1];
9047      zDb = "main";
9048    }else if( nArg==3 ){
9049      zSrcFile = azArg[2];
9050      zDb = azArg[1];
9051    }else{
9052      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9053      rc = 1;
9054      goto meta_command_exit;
9055    }
9056    rc = sqlite3_open(zSrcFile, &pSrc);
9057    if( rc!=SQLITE_OK ){
9058      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9059      close_db(pSrc);
9060      return 1;
9061    }
9062    open_db(p, 0);
9063    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9064    if( pBackup==0 ){
9065      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9066      close_db(pSrc);
9067      return 1;
9068    }
9069    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9070          || rc==SQLITE_BUSY  ){
9071      if( rc==SQLITE_BUSY ){
9072        if( nTimeout++ >= 3 ) break;
9073        sqlite3_sleep(100);
9074      }
9075    }
9076    sqlite3_backup_finish(pBackup);
9077    if( rc==SQLITE_DONE ){
9078      rc = 0;
9079    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9080      raw_printf(stderr, "Error: source database is busy\n");
9081      rc = 1;
9082    }else{
9083      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9084      rc = 1;
9085    }
9086    close_db(pSrc);
9087  }else
9088
9089  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9090    if( nArg==2 ){
9091      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9092#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9093      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9094#endif
9095    }else{
9096      raw_printf(stderr, "Usage: .scanstats on|off\n");
9097      rc = 1;
9098    }
9099  }else
9100
9101  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9102    ShellText sSelect;
9103    ShellState data;
9104    char *zErrMsg = 0;
9105    const char *zDiv = "(";
9106    const char *zName = 0;
9107    int iSchema = 0;
9108    int bDebug = 0;
9109    int bNoSystemTabs = 0;
9110    int ii;
9111
9112    open_db(p, 0);
9113    memcpy(&data, p, sizeof(data));
9114    data.showHeader = 0;
9115    data.cMode = data.mode = MODE_Semi;
9116    initText(&sSelect);
9117    for(ii=1; ii<nArg; ii++){
9118      if( optionMatch(azArg[ii],"indent") ){
9119        data.cMode = data.mode = MODE_Pretty;
9120      }else if( optionMatch(azArg[ii],"debug") ){
9121        bDebug = 1;
9122      }else if( optionMatch(azArg[ii],"nosys") ){
9123        bNoSystemTabs = 1;
9124      }else if( azArg[ii][0]=='-' ){
9125        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9126        rc = 1;
9127        goto meta_command_exit;
9128      }else if( zName==0 ){
9129        zName = azArg[ii];
9130      }else{
9131        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9132        rc = 1;
9133        goto meta_command_exit;
9134      }
9135    }
9136    if( zName!=0 ){
9137      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9138                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9139                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9140                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9141      if( isSchema ){
9142        char *new_argv[2], *new_colv[2];
9143        new_argv[0] = sqlite3_mprintf(
9144                      "CREATE TABLE %s (\n"
9145                      "  type text,\n"
9146                      "  name text,\n"
9147                      "  tbl_name text,\n"
9148                      "  rootpage integer,\n"
9149                      "  sql text\n"
9150                      ")", zName);
9151        new_argv[1] = 0;
9152        new_colv[0] = "sql";
9153        new_colv[1] = 0;
9154        callback(&data, 1, new_argv, new_colv);
9155        sqlite3_free(new_argv[0]);
9156      }
9157    }
9158    if( zDiv ){
9159      sqlite3_stmt *pStmt = 0;
9160      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9161                              -1, &pStmt, 0);
9162      if( rc ){
9163        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9164        sqlite3_finalize(pStmt);
9165        rc = 1;
9166        goto meta_command_exit;
9167      }
9168      appendText(&sSelect, "SELECT sql FROM", 0);
9169      iSchema = 0;
9170      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9171        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9172        char zScNum[30];
9173        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9174        appendText(&sSelect, zDiv, 0);
9175        zDiv = " UNION ALL ";
9176        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9177        if( sqlite3_stricmp(zDb, "main")!=0 ){
9178          appendText(&sSelect, zDb, '\'');
9179        }else{
9180          appendText(&sSelect, "NULL", 0);
9181        }
9182        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9183        appendText(&sSelect, zScNum, 0);
9184        appendText(&sSelect, " AS snum, ", 0);
9185        appendText(&sSelect, zDb, '\'');
9186        appendText(&sSelect, " AS sname FROM ", 0);
9187        appendText(&sSelect, zDb, quoteChar(zDb));
9188        appendText(&sSelect, ".sqlite_schema", 0);
9189      }
9190      sqlite3_finalize(pStmt);
9191#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9192      if( zName ){
9193        appendText(&sSelect,
9194           " UNION ALL SELECT shell_module_schema(name),"
9195           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9196        0);
9197      }
9198#endif
9199      appendText(&sSelect, ") WHERE ", 0);
9200      if( zName ){
9201        char *zQarg = sqlite3_mprintf("%Q", zName);
9202        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9203                    strchr(zName, '[') != 0;
9204        if( strchr(zName, '.') ){
9205          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9206        }else{
9207          appendText(&sSelect, "lower(tbl_name)", 0);
9208        }
9209        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9210        appendText(&sSelect, zQarg, 0);
9211        if( !bGlob ){
9212          appendText(&sSelect, " ESCAPE '\\' ", 0);
9213        }
9214        appendText(&sSelect, " AND ", 0);
9215        sqlite3_free(zQarg);
9216      }
9217      if( bNoSystemTabs ){
9218        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9219      }
9220      appendText(&sSelect, "sql IS NOT NULL"
9221                           " ORDER BY snum, rowid", 0);
9222      if( bDebug ){
9223        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9224      }else{
9225        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9226      }
9227      freeText(&sSelect);
9228    }
9229    if( zErrMsg ){
9230      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9231      sqlite3_free(zErrMsg);
9232      rc = 1;
9233    }else if( rc != SQLITE_OK ){
9234      raw_printf(stderr,"Error: querying schema information\n");
9235      rc = 1;
9236    }else{
9237      rc = 0;
9238    }
9239  }else
9240
9241  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9242    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
9243    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
9244  }else
9245
9246#if defined(SQLITE_ENABLE_SESSION)
9247  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9248    OpenSession *pSession = &p->aSession[0];
9249    char **azCmd = &azArg[1];
9250    int iSes = 0;
9251    int nCmd = nArg - 1;
9252    int i;
9253    if( nArg<=1 ) goto session_syntax_error;
9254    open_db(p, 0);
9255    if( nArg>=3 ){
9256      for(iSes=0; iSes<p->nSession; iSes++){
9257        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
9258      }
9259      if( iSes<p->nSession ){
9260        pSession = &p->aSession[iSes];
9261        azCmd++;
9262        nCmd--;
9263      }else{
9264        pSession = &p->aSession[0];
9265        iSes = 0;
9266      }
9267    }
9268
9269    /* .session attach TABLE
9270    ** Invoke the sqlite3session_attach() interface to attach a particular
9271    ** table so that it is never filtered.
9272    */
9273    if( strcmp(azCmd[0],"attach")==0 ){
9274      if( nCmd!=2 ) goto session_syntax_error;
9275      if( pSession->p==0 ){
9276        session_not_open:
9277        raw_printf(stderr, "ERROR: No sessions are open\n");
9278      }else{
9279        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9280        if( rc ){
9281          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9282          rc = 0;
9283        }
9284      }
9285    }else
9286
9287    /* .session changeset FILE
9288    ** .session patchset FILE
9289    ** Write a changeset or patchset into a file.  The file is overwritten.
9290    */
9291    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9292      FILE *out = 0;
9293      if( nCmd!=2 ) goto session_syntax_error;
9294      if( pSession->p==0 ) goto session_not_open;
9295      out = fopen(azCmd[1], "wb");
9296      if( out==0 ){
9297        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9298                    azCmd[1]);
9299      }else{
9300        int szChng;
9301        void *pChng;
9302        if( azCmd[0][0]=='c' ){
9303          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9304        }else{
9305          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9306        }
9307        if( rc ){
9308          printf("Error: error code %d\n", rc);
9309          rc = 0;
9310        }
9311        if( pChng
9312          && fwrite(pChng, szChng, 1, out)!=1 ){
9313          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9314                  szChng);
9315        }
9316        sqlite3_free(pChng);
9317        fclose(out);
9318      }
9319    }else
9320
9321    /* .session close
9322    ** Close the identified session
9323    */
9324    if( strcmp(azCmd[0], "close")==0 ){
9325      if( nCmd!=1 ) goto session_syntax_error;
9326      if( p->nSession ){
9327        session_close(pSession);
9328        p->aSession[iSes] = p->aSession[--p->nSession];
9329      }
9330    }else
9331
9332    /* .session enable ?BOOLEAN?
9333    ** Query or set the enable flag
9334    */
9335    if( strcmp(azCmd[0], "enable")==0 ){
9336      int ii;
9337      if( nCmd>2 ) goto session_syntax_error;
9338      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9339      if( p->nSession ){
9340        ii = sqlite3session_enable(pSession->p, ii);
9341        utf8_printf(p->out, "session %s enable flag = %d\n",
9342                    pSession->zName, ii);
9343      }
9344    }else
9345
9346    /* .session filter GLOB ....
9347    ** Set a list of GLOB patterns of table names to be excluded.
9348    */
9349    if( strcmp(azCmd[0], "filter")==0 ){
9350      int ii, nByte;
9351      if( nCmd<2 ) goto session_syntax_error;
9352      if( p->nSession ){
9353        for(ii=0; ii<pSession->nFilter; ii++){
9354          sqlite3_free(pSession->azFilter[ii]);
9355        }
9356        sqlite3_free(pSession->azFilter);
9357        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9358        pSession->azFilter = sqlite3_malloc( nByte );
9359        if( pSession->azFilter==0 ){
9360          raw_printf(stderr, "Error: out or memory\n");
9361          exit(1);
9362        }
9363        for(ii=1; ii<nCmd; ii++){
9364          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9365        }
9366        pSession->nFilter = ii-1;
9367      }
9368    }else
9369
9370    /* .session indirect ?BOOLEAN?
9371    ** Query or set the indirect flag
9372    */
9373    if( strcmp(azCmd[0], "indirect")==0 ){
9374      int ii;
9375      if( nCmd>2 ) goto session_syntax_error;
9376      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9377      if( p->nSession ){
9378        ii = sqlite3session_indirect(pSession->p, ii);
9379        utf8_printf(p->out, "session %s indirect flag = %d\n",
9380                    pSession->zName, ii);
9381      }
9382    }else
9383
9384    /* .session isempty
9385    ** Determine if the session is empty
9386    */
9387    if( strcmp(azCmd[0], "isempty")==0 ){
9388      int ii;
9389      if( nCmd!=1 ) goto session_syntax_error;
9390      if( p->nSession ){
9391        ii = sqlite3session_isempty(pSession->p);
9392        utf8_printf(p->out, "session %s isempty flag = %d\n",
9393                    pSession->zName, ii);
9394      }
9395    }else
9396
9397    /* .session list
9398    ** List all currently open sessions
9399    */
9400    if( strcmp(azCmd[0],"list")==0 ){
9401      for(i=0; i<p->nSession; i++){
9402        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
9403      }
9404    }else
9405
9406    /* .session open DB NAME
9407    ** Open a new session called NAME on the attached database DB.
9408    ** DB is normally "main".
9409    */
9410    if( strcmp(azCmd[0],"open")==0 ){
9411      char *zName;
9412      if( nCmd!=3 ) goto session_syntax_error;
9413      zName = azCmd[2];
9414      if( zName[0]==0 ) goto session_syntax_error;
9415      for(i=0; i<p->nSession; i++){
9416        if( strcmp(p->aSession[i].zName,zName)==0 ){
9417          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9418          goto meta_command_exit;
9419        }
9420      }
9421      if( p->nSession>=ArraySize(p->aSession) ){
9422        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9423        goto meta_command_exit;
9424      }
9425      pSession = &p->aSession[p->nSession];
9426      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9427      if( rc ){
9428        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9429        rc = 0;
9430        goto meta_command_exit;
9431      }
9432      pSession->nFilter = 0;
9433      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9434      p->nSession++;
9435      pSession->zName = sqlite3_mprintf("%s", zName);
9436    }else
9437    /* If no command name matches, show a syntax error */
9438    session_syntax_error:
9439    showHelp(p->out, "session");
9440  }else
9441#endif
9442
9443#ifdef SQLITE_DEBUG
9444  /* Undocumented commands for internal testing.  Subject to change
9445  ** without notice. */
9446  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9447    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9448      int i, v;
9449      for(i=1; i<nArg; i++){
9450        v = booleanValue(azArg[i]);
9451        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9452      }
9453    }
9454    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9455      int i; sqlite3_int64 v;
9456      for(i=1; i<nArg; i++){
9457        char zBuf[200];
9458        v = integerValue(azArg[i]);
9459        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9460        utf8_printf(p->out, "%s", zBuf);
9461      }
9462    }
9463  }else
9464#endif
9465
9466  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9467    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9468    int bVerbose = 0;        /* Verbose output */
9469    int bSelftestExists;     /* True if SELFTEST already exists */
9470    int i, k;                /* Loop counters */
9471    int nTest = 0;           /* Number of tests runs */
9472    int nErr = 0;            /* Number of errors seen */
9473    ShellText str;           /* Answer for a query */
9474    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9475
9476    open_db(p,0);
9477    for(i=1; i<nArg; i++){
9478      const char *z = azArg[i];
9479      if( z[0]=='-' && z[1]=='-' ) z++;
9480      if( strcmp(z,"-init")==0 ){
9481        bIsInit = 1;
9482      }else
9483      if( strcmp(z,"-v")==0 ){
9484        bVerbose++;
9485      }else
9486      {
9487        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9488                    azArg[i], azArg[0]);
9489        raw_printf(stderr, "Should be one of: --init -v\n");
9490        rc = 1;
9491        goto meta_command_exit;
9492      }
9493    }
9494    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9495           != SQLITE_OK ){
9496      bSelftestExists = 0;
9497    }else{
9498      bSelftestExists = 1;
9499    }
9500    if( bIsInit ){
9501      createSelftestTable(p);
9502      bSelftestExists = 1;
9503    }
9504    initText(&str);
9505    appendText(&str, "x", 0);
9506    for(k=bSelftestExists; k>=0; k--){
9507      if( k==1 ){
9508        rc = sqlite3_prepare_v2(p->db,
9509            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9510            -1, &pStmt, 0);
9511      }else{
9512        rc = sqlite3_prepare_v2(p->db,
9513          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9514          "      (1,'run','PRAGMA integrity_check','ok')",
9515          -1, &pStmt, 0);
9516      }
9517      if( rc ){
9518        raw_printf(stderr, "Error querying the selftest table\n");
9519        rc = 1;
9520        sqlite3_finalize(pStmt);
9521        goto meta_command_exit;
9522      }
9523      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9524        int tno = sqlite3_column_int(pStmt, 0);
9525        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9526        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9527        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9528
9529        k = 0;
9530        if( bVerbose>0 ){
9531          char *zQuote = sqlite3_mprintf("%q", zSql);
9532          printf("%d: %s %s\n", tno, zOp, zSql);
9533          sqlite3_free(zQuote);
9534        }
9535        if( strcmp(zOp,"memo")==0 ){
9536          utf8_printf(p->out, "%s\n", zSql);
9537        }else
9538        if( strcmp(zOp,"run")==0 ){
9539          char *zErrMsg = 0;
9540          str.n = 0;
9541          str.z[0] = 0;
9542          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9543          nTest++;
9544          if( bVerbose ){
9545            utf8_printf(p->out, "Result: %s\n", str.z);
9546          }
9547          if( rc || zErrMsg ){
9548            nErr++;
9549            rc = 1;
9550            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9551            sqlite3_free(zErrMsg);
9552          }else if( strcmp(zAns,str.z)!=0 ){
9553            nErr++;
9554            rc = 1;
9555            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9556            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9557          }
9558        }else
9559        {
9560          utf8_printf(stderr,
9561            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9562          rc = 1;
9563          break;
9564        }
9565      } /* End loop over rows of content from SELFTEST */
9566      sqlite3_finalize(pStmt);
9567    } /* End loop over k */
9568    freeText(&str);
9569    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9570  }else
9571
9572  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9573    if( nArg<2 || nArg>3 ){
9574      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9575      rc = 1;
9576    }
9577    if( nArg>=2 ){
9578      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9579                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9580    }
9581    if( nArg>=3 ){
9582      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9583                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9584    }
9585  }else
9586
9587  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9588    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9589    int i;                   /* Loop counter */
9590    int bSchema = 0;         /* Also hash the schema */
9591    int bSeparate = 0;       /* Hash each table separately */
9592    int iSize = 224;         /* Hash algorithm to use */
9593    int bDebug = 0;          /* Only show the query that would have run */
9594    sqlite3_stmt *pStmt;     /* For querying tables names */
9595    char *zSql;              /* SQL to be run */
9596    char *zSep;              /* Separator */
9597    ShellText sSql;          /* Complete SQL for the query to run the hash */
9598    ShellText sQuery;        /* Set of queries used to read all content */
9599    open_db(p, 0);
9600    for(i=1; i<nArg; i++){
9601      const char *z = azArg[i];
9602      if( z[0]=='-' ){
9603        z++;
9604        if( z[0]=='-' ) z++;
9605        if( strcmp(z,"schema")==0 ){
9606          bSchema = 1;
9607        }else
9608        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9609         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9610        ){
9611          iSize = atoi(&z[5]);
9612        }else
9613        if( strcmp(z,"debug")==0 ){
9614          bDebug = 1;
9615        }else
9616        {
9617          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9618                      azArg[i], azArg[0]);
9619          showHelp(p->out, azArg[0]);
9620          rc = 1;
9621          goto meta_command_exit;
9622        }
9623      }else if( zLike ){
9624        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9625        rc = 1;
9626        goto meta_command_exit;
9627      }else{
9628        zLike = z;
9629        bSeparate = 1;
9630        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9631      }
9632    }
9633    if( bSchema ){
9634      zSql = "SELECT lower(name) FROM sqlite_schema"
9635             " WHERE type='table' AND coalesce(rootpage,0)>1"
9636             " UNION ALL SELECT 'sqlite_schema'"
9637             " ORDER BY 1 collate nocase";
9638    }else{
9639      zSql = "SELECT lower(name) FROM sqlite_schema"
9640             " WHERE type='table' AND coalesce(rootpage,0)>1"
9641             " AND name NOT LIKE 'sqlite_%'"
9642             " ORDER BY 1 collate nocase";
9643    }
9644    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9645    initText(&sQuery);
9646    initText(&sSql);
9647    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9648    zSep = "VALUES(";
9649    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9650      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9651      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9652      if( strncmp(zTab, "sqlite_",7)!=0 ){
9653        appendText(&sQuery,"SELECT * FROM ", 0);
9654        appendText(&sQuery,zTab,'"');
9655        appendText(&sQuery," NOT INDEXED;", 0);
9656      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9657        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9658                           " ORDER BY name;", 0);
9659      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9660        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9661                           " ORDER BY name;", 0);
9662      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9663        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9664                           " ORDER BY tbl,idx;", 0);
9665      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9666        appendText(&sQuery, "SELECT * FROM ", 0);
9667        appendText(&sQuery, zTab, 0);
9668        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9669      }
9670      appendText(&sSql, zSep, 0);
9671      appendText(&sSql, sQuery.z, '\'');
9672      sQuery.n = 0;
9673      appendText(&sSql, ",", 0);
9674      appendText(&sSql, zTab, '\'');
9675      zSep = "),(";
9676    }
9677    sqlite3_finalize(pStmt);
9678    if( bSeparate ){
9679      zSql = sqlite3_mprintf(
9680          "%s))"
9681          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9682          "   FROM [sha3sum$query]",
9683          sSql.z, iSize);
9684    }else{
9685      zSql = sqlite3_mprintf(
9686          "%s))"
9687          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9688          "   FROM [sha3sum$query]",
9689          sSql.z, iSize);
9690    }
9691    freeText(&sQuery);
9692    freeText(&sSql);
9693    if( bDebug ){
9694      utf8_printf(p->out, "%s\n", zSql);
9695    }else{
9696      shell_exec(p, zSql, 0);
9697    }
9698    sqlite3_free(zSql);
9699  }else
9700
9701#ifndef SQLITE_NOHAVE_SYSTEM
9702  if( c=='s'
9703   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9704  ){
9705    char *zCmd;
9706    int i, x;
9707    if( nArg<2 ){
9708      raw_printf(stderr, "Usage: .system COMMAND\n");
9709      rc = 1;
9710      goto meta_command_exit;
9711    }
9712    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9713    for(i=2; i<nArg; i++){
9714      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9715                             zCmd, azArg[i]);
9716    }
9717    x = system(zCmd);
9718    sqlite3_free(zCmd);
9719    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9720  }else
9721#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9722
9723  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9724    static const char *azBool[] = { "off", "on", "trigger", "full"};
9725    int i;
9726    if( nArg!=1 ){
9727      raw_printf(stderr, "Usage: .show\n");
9728      rc = 1;
9729      goto meta_command_exit;
9730    }
9731    utf8_printf(p->out, "%12.12s: %s\n","echo",
9732                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9733    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9734    utf8_printf(p->out, "%12.12s: %s\n","explain",
9735         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9736    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9737    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9738    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9739      output_c_string(p->out, p->nullValue);
9740      raw_printf(p->out, "\n");
9741    utf8_printf(p->out,"%12.12s: %s\n","output",
9742            strlen30(p->outfile) ? p->outfile : "stdout");
9743    utf8_printf(p->out,"%12.12s: ", "colseparator");
9744      output_c_string(p->out, p->colSeparator);
9745      raw_printf(p->out, "\n");
9746    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9747      output_c_string(p->out, p->rowSeparator);
9748      raw_printf(p->out, "\n");
9749    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
9750    utf8_printf(p->out, "%12.12s: ", "width");
9751    for (i=0;i<p->nWidth;i++) {
9752      raw_printf(p->out, "%d ", p->colWidth[i]);
9753    }
9754    raw_printf(p->out, "\n");
9755    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9756                p->zDbFilename ? p->zDbFilename : "");
9757  }else
9758
9759  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9760    if( nArg==2 ){
9761      p->statsOn = (u8)booleanValue(azArg[1]);
9762    }else if( nArg==1 ){
9763      display_stats(p->db, p, 0);
9764    }else{
9765      raw_printf(stderr, "Usage: .stats ?on|off?\n");
9766      rc = 1;
9767    }
9768  }else
9769
9770  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9771   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9772                 || strncmp(azArg[0], "indexes", n)==0) )
9773  ){
9774    sqlite3_stmt *pStmt;
9775    char **azResult;
9776    int nRow, nAlloc;
9777    int ii;
9778    ShellText s;
9779    initText(&s);
9780    open_db(p, 0);
9781    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9782    if( rc ){
9783      sqlite3_finalize(pStmt);
9784      return shellDatabaseError(p->db);
9785    }
9786
9787    if( nArg>2 && c=='i' ){
9788      /* It is an historical accident that the .indexes command shows an error
9789      ** when called with the wrong number of arguments whereas the .tables
9790      ** command does not. */
9791      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9792      rc = 1;
9793      sqlite3_finalize(pStmt);
9794      goto meta_command_exit;
9795    }
9796    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9797      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9798      if( zDbName==0 ) continue;
9799      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9800      if( sqlite3_stricmp(zDbName, "main")==0 ){
9801        appendText(&s, "SELECT name FROM ", 0);
9802      }else{
9803        appendText(&s, "SELECT ", 0);
9804        appendText(&s, zDbName, '\'');
9805        appendText(&s, "||'.'||name FROM ", 0);
9806      }
9807      appendText(&s, zDbName, '"');
9808      appendText(&s, ".sqlite_schema ", 0);
9809      if( c=='t' ){
9810        appendText(&s," WHERE type IN ('table','view')"
9811                      "   AND name NOT LIKE 'sqlite_%'"
9812                      "   AND name LIKE ?1", 0);
9813      }else{
9814        appendText(&s," WHERE type='index'"
9815                      "   AND tbl_name LIKE ?1", 0);
9816      }
9817    }
9818    rc = sqlite3_finalize(pStmt);
9819    appendText(&s, " ORDER BY 1", 0);
9820    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9821    freeText(&s);
9822    if( rc ) return shellDatabaseError(p->db);
9823
9824    /* Run the SQL statement prepared by the above block. Store the results
9825    ** as an array of nul-terminated strings in azResult[].  */
9826    nRow = nAlloc = 0;
9827    azResult = 0;
9828    if( nArg>1 ){
9829      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9830    }else{
9831      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9832    }
9833    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9834      if( nRow>=nAlloc ){
9835        char **azNew;
9836        int n2 = nAlloc*2 + 10;
9837        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9838        if( azNew==0 ) shell_out_of_memory();
9839        nAlloc = n2;
9840        azResult = azNew;
9841      }
9842      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9843      if( 0==azResult[nRow] ) shell_out_of_memory();
9844      nRow++;
9845    }
9846    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9847      rc = shellDatabaseError(p->db);
9848    }
9849
9850    /* Pretty-print the contents of array azResult[] to the output */
9851    if( rc==0 && nRow>0 ){
9852      int len, maxlen = 0;
9853      int i, j;
9854      int nPrintCol, nPrintRow;
9855      for(i=0; i<nRow; i++){
9856        len = strlen30(azResult[i]);
9857        if( len>maxlen ) maxlen = len;
9858      }
9859      nPrintCol = 80/(maxlen+2);
9860      if( nPrintCol<1 ) nPrintCol = 1;
9861      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9862      for(i=0; i<nPrintRow; i++){
9863        for(j=i; j<nRow; j+=nPrintRow){
9864          char *zSp = j<nPrintRow ? "" : "  ";
9865          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9866                      azResult[j] ? azResult[j]:"");
9867        }
9868        raw_printf(p->out, "\n");
9869      }
9870    }
9871
9872    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9873    sqlite3_free(azResult);
9874  }else
9875
9876  /* Begin redirecting output to the file "testcase-out.txt" */
9877  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9878    output_reset(p);
9879    p->out = output_file_open("testcase-out.txt", 0);
9880    if( p->out==0 ){
9881      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9882    }
9883    if( nArg>=2 ){
9884      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9885    }else{
9886      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9887    }
9888  }else
9889
9890#ifndef SQLITE_UNTESTABLE
9891  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9892    static const struct {
9893       const char *zCtrlName;   /* Name of a test-control option */
9894       int ctrlCode;            /* Integer code for that option */
9895       const char *zUsage;      /* Usage notes */
9896    } aCtrl[] = {
9897      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9898      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9899    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9900    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9901      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9902      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9903    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9904      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9905      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9906      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9907      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9908      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9909#ifdef YYCOVERAGE
9910      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9911#endif
9912      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9913      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9914      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9915      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9916      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
9917    };
9918    int testctrl = -1;
9919    int iCtrl = -1;
9920    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9921    int isOk = 0;
9922    int i, n2;
9923    const char *zCmd = 0;
9924
9925    open_db(p, 0);
9926    zCmd = nArg>=2 ? azArg[1] : "help";
9927
9928    /* The argument can optionally begin with "-" or "--" */
9929    if( zCmd[0]=='-' && zCmd[1] ){
9930      zCmd++;
9931      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9932    }
9933
9934    /* --help lists all test-controls */
9935    if( strcmp(zCmd,"help")==0 ){
9936      utf8_printf(p->out, "Available test-controls:\n");
9937      for(i=0; i<ArraySize(aCtrl); i++){
9938        utf8_printf(p->out, "  .testctrl %s %s\n",
9939                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
9940      }
9941      rc = 1;
9942      goto meta_command_exit;
9943    }
9944
9945    /* convert testctrl text option to value. allow any unique prefix
9946    ** of the option name, or a numerical value. */
9947    n2 = strlen30(zCmd);
9948    for(i=0; i<ArraySize(aCtrl); i++){
9949      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
9950        if( testctrl<0 ){
9951          testctrl = aCtrl[i].ctrlCode;
9952          iCtrl = i;
9953        }else{
9954          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
9955                              "Use \".testctrl --help\" for help\n", zCmd);
9956          rc = 1;
9957          goto meta_command_exit;
9958        }
9959      }
9960    }
9961    if( testctrl<0 ){
9962      utf8_printf(stderr,"Error: unknown test-control: %s\n"
9963                         "Use \".testctrl --help\" for help\n", zCmd);
9964    }else{
9965      switch(testctrl){
9966
9967        /* sqlite3_test_control(int, db, int) */
9968        case SQLITE_TESTCTRL_OPTIMIZATIONS:
9969          if( nArg==3 ){
9970            int opt = (int)strtol(azArg[2], 0, 0);
9971            rc2 = sqlite3_test_control(testctrl, p->db, opt);
9972            isOk = 3;
9973          }
9974          break;
9975
9976        /* sqlite3_test_control(int) */
9977        case SQLITE_TESTCTRL_PRNG_SAVE:
9978        case SQLITE_TESTCTRL_PRNG_RESTORE:
9979        case SQLITE_TESTCTRL_BYTEORDER:
9980          if( nArg==2 ){
9981            rc2 = sqlite3_test_control(testctrl);
9982            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
9983          }
9984          break;
9985
9986        /* sqlite3_test_control(int, uint) */
9987        case SQLITE_TESTCTRL_PENDING_BYTE:
9988          if( nArg==3 ){
9989            unsigned int opt = (unsigned int)integerValue(azArg[2]);
9990            rc2 = sqlite3_test_control(testctrl, opt);
9991            isOk = 3;
9992          }
9993          break;
9994
9995        /* sqlite3_test_control(int, int, sqlite3*) */
9996        case SQLITE_TESTCTRL_PRNG_SEED:
9997          if( nArg==3 || nArg==4 ){
9998            int ii = (int)integerValue(azArg[2]);
9999            sqlite3 *db;
10000            if( ii==0 && strcmp(azArg[2],"random")==0 ){
10001              sqlite3_randomness(sizeof(ii),&ii);
10002              printf("-- random seed: %d\n", ii);
10003            }
10004            if( nArg==3 ){
10005              db = 0;
10006            }else{
10007              db = p->db;
10008              /* Make sure the schema has been loaded */
10009              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10010            }
10011            rc2 = sqlite3_test_control(testctrl, ii, db);
10012            isOk = 3;
10013          }
10014          break;
10015
10016        /* sqlite3_test_control(int, int) */
10017        case SQLITE_TESTCTRL_ASSERT:
10018        case SQLITE_TESTCTRL_ALWAYS:
10019          if( nArg==3 ){
10020            int opt = booleanValue(azArg[2]);
10021            rc2 = sqlite3_test_control(testctrl, opt);
10022            isOk = 1;
10023          }
10024          break;
10025
10026        /* sqlite3_test_control(int, int) */
10027        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10028        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10029          if( nArg==3 ){
10030            int opt = booleanValue(azArg[2]);
10031            rc2 = sqlite3_test_control(testctrl, opt);
10032            isOk = 3;
10033          }
10034          break;
10035
10036        /* sqlite3_test_control(sqlite3*) */
10037        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10038          rc2 = sqlite3_test_control(testctrl, p->db);
10039          isOk = 3;
10040          break;
10041
10042        case SQLITE_TESTCTRL_IMPOSTER:
10043          if( nArg==5 ){
10044            rc2 = sqlite3_test_control(testctrl, p->db,
10045                          azArg[2],
10046                          integerValue(azArg[3]),
10047                          integerValue(azArg[4]));
10048            isOk = 3;
10049          }
10050          break;
10051
10052        case SQLITE_TESTCTRL_SEEK_COUNT: {
10053          u64 x = 0;
10054          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10055          utf8_printf(p->out, "%llu\n", x);
10056          isOk = 3;
10057          break;
10058        }
10059
10060#ifdef YYCOVERAGE
10061        case SQLITE_TESTCTRL_PARSER_COVERAGE:
10062          if( nArg==2 ){
10063            sqlite3_test_control(testctrl, p->out);
10064            isOk = 3;
10065          }
10066#endif
10067      }
10068    }
10069    if( isOk==0 && iCtrl>=0 ){
10070      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10071      rc = 1;
10072    }else if( isOk==1 ){
10073      raw_printf(p->out, "%d\n", rc2);
10074    }else if( isOk==2 ){
10075      raw_printf(p->out, "0x%08x\n", rc2);
10076    }
10077  }else
10078#endif /* !defined(SQLITE_UNTESTABLE) */
10079
10080  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10081    open_db(p, 0);
10082    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10083  }else
10084
10085  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10086    if( nArg==2 ){
10087      enableTimer = booleanValue(azArg[1]);
10088      if( enableTimer && !HAS_TIMER ){
10089        raw_printf(stderr, "Error: timer not available on this system.\n");
10090        enableTimer = 0;
10091      }
10092    }else{
10093      raw_printf(stderr, "Usage: .timer on|off\n");
10094      rc = 1;
10095    }
10096  }else
10097
10098#ifndef SQLITE_OMIT_TRACE
10099  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10100    int mType = 0;
10101    int jj;
10102    open_db(p, 0);
10103    for(jj=1; jj<nArg; jj++){
10104      const char *z = azArg[jj];
10105      if( z[0]=='-' ){
10106        if( optionMatch(z, "expanded") ){
10107          p->eTraceType = SHELL_TRACE_EXPANDED;
10108        }
10109#ifdef SQLITE_ENABLE_NORMALIZE
10110        else if( optionMatch(z, "normalized") ){
10111          p->eTraceType = SHELL_TRACE_NORMALIZED;
10112        }
10113#endif
10114        else if( optionMatch(z, "plain") ){
10115          p->eTraceType = SHELL_TRACE_PLAIN;
10116        }
10117        else if( optionMatch(z, "profile") ){
10118          mType |= SQLITE_TRACE_PROFILE;
10119        }
10120        else if( optionMatch(z, "row") ){
10121          mType |= SQLITE_TRACE_ROW;
10122        }
10123        else if( optionMatch(z, "stmt") ){
10124          mType |= SQLITE_TRACE_STMT;
10125        }
10126        else if( optionMatch(z, "close") ){
10127          mType |= SQLITE_TRACE_CLOSE;
10128        }
10129        else {
10130          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10131          rc = 1;
10132          goto meta_command_exit;
10133        }
10134      }else{
10135        output_file_close(p->traceOut);
10136        p->traceOut = output_file_open(azArg[1], 0);
10137      }
10138    }
10139    if( p->traceOut==0 ){
10140      sqlite3_trace_v2(p->db, 0, 0, 0);
10141    }else{
10142      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10143      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10144    }
10145  }else
10146#endif /* !defined(SQLITE_OMIT_TRACE) */
10147
10148#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10149  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10150    int ii;
10151    int lenOpt;
10152    char *zOpt;
10153    if( nArg<2 ){
10154      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10155      rc = 1;
10156      goto meta_command_exit;
10157    }
10158    open_db(p, 0);
10159    zOpt = azArg[1];
10160    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10161    lenOpt = (int)strlen(zOpt);
10162    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10163      assert( azArg[nArg]==0 );
10164      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10165    }else{
10166      for(ii=1; ii<nArg; ii++){
10167        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10168      }
10169    }
10170  }else
10171#endif
10172
10173#if SQLITE_USER_AUTHENTICATION
10174  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10175    if( nArg<2 ){
10176      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10177      rc = 1;
10178      goto meta_command_exit;
10179    }
10180    open_db(p, 0);
10181    if( strcmp(azArg[1],"login")==0 ){
10182      if( nArg!=4 ){
10183        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10184        rc = 1;
10185        goto meta_command_exit;
10186      }
10187      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10188                                     strlen30(azArg[3]));
10189      if( rc ){
10190        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10191        rc = 1;
10192      }
10193    }else if( strcmp(azArg[1],"add")==0 ){
10194      if( nArg!=5 ){
10195        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10196        rc = 1;
10197        goto meta_command_exit;
10198      }
10199      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10200                            booleanValue(azArg[4]));
10201      if( rc ){
10202        raw_printf(stderr, "User-Add failed: %d\n", rc);
10203        rc = 1;
10204      }
10205    }else if( strcmp(azArg[1],"edit")==0 ){
10206      if( nArg!=5 ){
10207        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10208        rc = 1;
10209        goto meta_command_exit;
10210      }
10211      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10212                              booleanValue(azArg[4]));
10213      if( rc ){
10214        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10215        rc = 1;
10216      }
10217    }else if( strcmp(azArg[1],"delete")==0 ){
10218      if( nArg!=3 ){
10219        raw_printf(stderr, "Usage: .user delete USER\n");
10220        rc = 1;
10221        goto meta_command_exit;
10222      }
10223      rc = sqlite3_user_delete(p->db, azArg[2]);
10224      if( rc ){
10225        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10226        rc = 1;
10227      }
10228    }else{
10229      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10230      rc = 1;
10231      goto meta_command_exit;
10232    }
10233  }else
10234#endif /* SQLITE_USER_AUTHENTICATION */
10235
10236  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10237    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10238        sqlite3_libversion(), sqlite3_sourceid());
10239#if SQLITE_HAVE_ZLIB
10240    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10241#endif
10242#define CTIMEOPT_VAL_(opt) #opt
10243#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10244#if defined(__clang__) && defined(__clang_major__)
10245    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10246                    CTIMEOPT_VAL(__clang_minor__) "."
10247                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10248#elif defined(_MSC_VER)
10249    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10250#elif defined(__GNUC__) && defined(__VERSION__)
10251    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10252#endif
10253  }else
10254
10255  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10256    const char *zDbName = nArg==2 ? azArg[1] : "main";
10257    sqlite3_vfs *pVfs = 0;
10258    if( p->db ){
10259      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10260      if( pVfs ){
10261        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10262        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10263        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10264        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10265      }
10266    }
10267  }else
10268
10269  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10270    sqlite3_vfs *pVfs;
10271    sqlite3_vfs *pCurrent = 0;
10272    if( p->db ){
10273      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10274    }
10275    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10276      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10277           pVfs==pCurrent ? "  <--- CURRENT" : "");
10278      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10279      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10280      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10281      if( pVfs->pNext ){
10282        raw_printf(p->out, "-----------------------------------\n");
10283      }
10284    }
10285  }else
10286
10287  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10288    const char *zDbName = nArg==2 ? azArg[1] : "main";
10289    char *zVfsName = 0;
10290    if( p->db ){
10291      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10292      if( zVfsName ){
10293        utf8_printf(p->out, "%s\n", zVfsName);
10294        sqlite3_free(zVfsName);
10295      }
10296    }
10297  }else
10298
10299  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10300    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10301    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
10302  }else
10303
10304  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10305    int j;
10306    assert( nArg<=ArraySize(azArg) );
10307    p->nWidth = nArg-1;
10308    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10309    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10310    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10311    for(j=1; j<nArg; j++){
10312      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10313    }
10314  }else
10315
10316  {
10317    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10318      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10319    rc = 1;
10320  }
10321
10322meta_command_exit:
10323  if( p->outCount ){
10324    p->outCount--;
10325    if( p->outCount==0 ) output_reset(p);
10326  }
10327  return rc;
10328}
10329
10330/*
10331** Return TRUE if a semicolon occurs anywhere in the first N characters
10332** of string z[].
10333*/
10334static int line_contains_semicolon(const char *z, int N){
10335  int i;
10336  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
10337  return 0;
10338}
10339
10340/*
10341** Test to see if a line consists entirely of whitespace.
10342*/
10343static int _all_whitespace(const char *z){
10344  for(; *z; z++){
10345    if( IsSpace(z[0]) ) continue;
10346    if( *z=='/' && z[1]=='*' ){
10347      z += 2;
10348      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
10349      if( *z==0 ) return 0;
10350      z++;
10351      continue;
10352    }
10353    if( *z=='-' && z[1]=='-' ){
10354      z += 2;
10355      while( *z && *z!='\n' ){ z++; }
10356      if( *z==0 ) return 1;
10357      continue;
10358    }
10359    return 0;
10360  }
10361  return 1;
10362}
10363
10364/*
10365** Return TRUE if the line typed in is an SQL command terminator other
10366** than a semi-colon.  The SQL Server style "go" command is understood
10367** as is the Oracle "/".
10368*/
10369static int line_is_command_terminator(const char *zLine){
10370  while( IsSpace(zLine[0]) ){ zLine++; };
10371  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
10372    return 1;  /* Oracle */
10373  }
10374  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
10375         && _all_whitespace(&zLine[2]) ){
10376    return 1;  /* SQL Server */
10377  }
10378  return 0;
10379}
10380
10381/*
10382** We need a default sqlite3_complete() implementation to use in case
10383** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10384** any arbitrary text is a complete SQL statement.  This is not very
10385** user-friendly, but it does seem to work.
10386*/
10387#ifdef SQLITE_OMIT_COMPLETE
10388#define sqlite3_complete(x) 1
10389#endif
10390
10391/*
10392** Return true if zSql is a complete SQL statement.  Return false if it
10393** ends in the middle of a string literal or C-style comment.
10394*/
10395static int line_is_complete(char *zSql, int nSql){
10396  int rc;
10397  if( zSql==0 ) return 1;
10398  zSql[nSql] = ';';
10399  zSql[nSql+1] = 0;
10400  rc = sqlite3_complete(zSql);
10401  zSql[nSql] = 0;
10402  return rc;
10403}
10404
10405/*
10406** Run a single line of SQL.  Return the number of errors.
10407*/
10408static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10409  int rc;
10410  char *zErrMsg = 0;
10411
10412  open_db(p, 0);
10413  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10414  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10415  BEGIN_TIMER;
10416  rc = shell_exec(p, zSql, &zErrMsg);
10417  END_TIMER;
10418  if( rc || zErrMsg ){
10419    char zPrefix[100];
10420    if( in!=0 || !stdin_is_interactive ){
10421      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10422                       "Error: near line %d:", startline);
10423    }else{
10424      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10425    }
10426    if( zErrMsg!=0 ){
10427      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10428      sqlite3_free(zErrMsg);
10429      zErrMsg = 0;
10430    }else{
10431      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10432    }
10433    return 1;
10434  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10435    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
10436            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
10437  }
10438  return 0;
10439}
10440
10441
10442/*
10443** Read input from *in and process it.  If *in==0 then input
10444** is interactive - the user is typing it it.  Otherwise, input
10445** is coming from a file or device.  A prompt is issued and history
10446** is saved only if input is interactive.  An interrupt signal will
10447** cause this routine to exit immediately, unless input is interactive.
10448**
10449** Return the number of errors.
10450*/
10451static int process_input(ShellState *p){
10452  char *zLine = 0;          /* A single input line */
10453  char *zSql = 0;           /* Accumulated SQL text */
10454  int nLine;                /* Length of current line */
10455  int nSql = 0;             /* Bytes of zSql[] used */
10456  int nAlloc = 0;           /* Allocated zSql[] space */
10457  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10458  int rc;                   /* Error code */
10459  int errCnt = 0;           /* Number of errors seen */
10460  int startline = 0;        /* Line number for start of current input */
10461
10462  p->lineno = 0;
10463  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10464    fflush(p->out);
10465    zLine = one_input_line(p->in, zLine, nSql>0);
10466    if( zLine==0 ){
10467      /* End of input */
10468      if( p->in==0 && stdin_is_interactive ) printf("\n");
10469      break;
10470    }
10471    if( seenInterrupt ){
10472      if( p->in!=0 ) break;
10473      seenInterrupt = 0;
10474    }
10475    p->lineno++;
10476    if( nSql==0 && _all_whitespace(zLine) ){
10477      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10478      continue;
10479    }
10480    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10481      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10482      if( zLine[0]=='.' ){
10483        rc = do_meta_command(zLine, p);
10484        if( rc==2 ){ /* exit requested */
10485          break;
10486        }else if( rc ){
10487          errCnt++;
10488        }
10489      }
10490      continue;
10491    }
10492    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10493      memcpy(zLine,";",2);
10494    }
10495    nLine = strlen30(zLine);
10496    if( nSql+nLine+2>=nAlloc ){
10497      nAlloc = nSql+nLine+100;
10498      zSql = realloc(zSql, nAlloc);
10499      if( zSql==0 ) shell_out_of_memory();
10500    }
10501    nSqlPrior = nSql;
10502    if( nSql==0 ){
10503      int i;
10504      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10505      assert( nAlloc>0 && zSql!=0 );
10506      memcpy(zSql, zLine+i, nLine+1-i);
10507      startline = p->lineno;
10508      nSql = nLine-i;
10509    }else{
10510      zSql[nSql++] = '\n';
10511      memcpy(zSql+nSql, zLine, nLine+1);
10512      nSql += nLine;
10513    }
10514    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10515                && sqlite3_complete(zSql) ){
10516      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10517      nSql = 0;
10518      if( p->outCount ){
10519        output_reset(p);
10520        p->outCount = 0;
10521      }else{
10522        clearTempFile(p);
10523      }
10524    }else if( nSql && _all_whitespace(zSql) ){
10525      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10526      nSql = 0;
10527    }
10528  }
10529  if( nSql && !_all_whitespace(zSql) ){
10530    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10531  }
10532  free(zSql);
10533  free(zLine);
10534  return errCnt>0;
10535}
10536
10537/*
10538** Return a pathname which is the user's home directory.  A
10539** 0 return indicates an error of some kind.
10540*/
10541static char *find_home_dir(int clearFlag){
10542  static char *home_dir = NULL;
10543  if( clearFlag ){
10544    free(home_dir);
10545    home_dir = 0;
10546    return 0;
10547  }
10548  if( home_dir ) return home_dir;
10549
10550#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10551     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10552  {
10553    struct passwd *pwent;
10554    uid_t uid = getuid();
10555    if( (pwent=getpwuid(uid)) != NULL) {
10556      home_dir = pwent->pw_dir;
10557    }
10558  }
10559#endif
10560
10561#if defined(_WIN32_WCE)
10562  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10563   */
10564  home_dir = "/";
10565#else
10566
10567#if defined(_WIN32) || defined(WIN32)
10568  if (!home_dir) {
10569    home_dir = getenv("USERPROFILE");
10570  }
10571#endif
10572
10573  if (!home_dir) {
10574    home_dir = getenv("HOME");
10575  }
10576
10577#if defined(_WIN32) || defined(WIN32)
10578  if (!home_dir) {
10579    char *zDrive, *zPath;
10580    int n;
10581    zDrive = getenv("HOMEDRIVE");
10582    zPath = getenv("HOMEPATH");
10583    if( zDrive && zPath ){
10584      n = strlen30(zDrive) + strlen30(zPath) + 1;
10585      home_dir = malloc( n );
10586      if( home_dir==0 ) return 0;
10587      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10588      return home_dir;
10589    }
10590    home_dir = "c:\\";
10591  }
10592#endif
10593
10594#endif /* !_WIN32_WCE */
10595
10596  if( home_dir ){
10597    int n = strlen30(home_dir) + 1;
10598    char *z = malloc( n );
10599    if( z ) memcpy(z, home_dir, n);
10600    home_dir = z;
10601  }
10602
10603  return home_dir;
10604}
10605
10606/*
10607** Read input from the file given by sqliterc_override.  Or if that
10608** parameter is NULL, take input from ~/.sqliterc
10609**
10610** Returns the number of errors.
10611*/
10612static void process_sqliterc(
10613  ShellState *p,                  /* Configuration data */
10614  const char *sqliterc_override   /* Name of config file. NULL to use default */
10615){
10616  char *home_dir = NULL;
10617  const char *sqliterc = sqliterc_override;
10618  char *zBuf = 0;
10619  FILE *inSaved = p->in;
10620  int savedLineno = p->lineno;
10621
10622  if (sqliterc == NULL) {
10623    home_dir = find_home_dir(0);
10624    if( home_dir==0 ){
10625      raw_printf(stderr, "-- warning: cannot find home directory;"
10626                      " cannot read ~/.sqliterc\n");
10627      return;
10628    }
10629    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10630    sqliterc = zBuf;
10631  }
10632  p->in = fopen(sqliterc,"rb");
10633  if( p->in ){
10634    if( stdin_is_interactive ){
10635      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10636    }
10637    if( process_input(p) && bail_on_error ) exit(1);
10638    fclose(p->in);
10639  }else if( sqliterc_override!=0 ){
10640    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
10641    if( bail_on_error ) exit(1);
10642  }
10643  p->in = inSaved;
10644  p->lineno = savedLineno;
10645  sqlite3_free(zBuf);
10646}
10647
10648/*
10649** Show available command line options
10650*/
10651static const char zOptions[] =
10652#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10653  "   -A ARGS...           run \".archive ARGS\" and exit\n"
10654#endif
10655  "   -append              append the database to the end of the file\n"
10656  "   -ascii               set output mode to 'ascii'\n"
10657  "   -bail                stop after hitting an error\n"
10658  "   -batch               force batch I/O\n"
10659  "   -box                 set output mode to 'box'\n"
10660  "   -column              set output mode to 'column'\n"
10661  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
10662  "   -csv                 set output mode to 'csv'\n"
10663#if defined(SQLITE_ENABLE_DESERIALIZE)
10664  "   -deserialize         open the database using sqlite3_deserialize()\n"
10665#endif
10666  "   -echo                print commands before execution\n"
10667  "   -init FILENAME       read/process named file\n"
10668  "   -[no]header          turn headers on or off\n"
10669#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10670  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
10671#endif
10672  "   -help                show this message\n"
10673  "   -html                set output mode to HTML\n"
10674  "   -interactive         force interactive I/O\n"
10675  "   -json                set output mode to 'json'\n"
10676  "   -line                set output mode to 'line'\n"
10677  "   -list                set output mode to 'list'\n"
10678  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
10679  "   -markdown            set output mode to 'markdown'\n"
10680#if defined(SQLITE_ENABLE_DESERIALIZE)
10681  "   -maxsize N           maximum size for a --deserialize database\n"
10682#endif
10683  "   -memtrace            trace all memory allocations and deallocations\n"
10684  "   -mmap N              default mmap size set to N\n"
10685#ifdef SQLITE_ENABLE_MULTIPLEX
10686  "   -multiplex           enable the multiplexor VFS\n"
10687#endif
10688  "   -newline SEP         set output row separator. Default: '\\n'\n"
10689  "   -nofollow            refuse to open symbolic links to database files\n"
10690  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
10691  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
10692  "   -quote               set output mode to 'quote'\n"
10693  "   -readonly            open the database read-only\n"
10694  "   -separator SEP       set output column separator. Default: '|'\n"
10695#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10696  "   -sorterref SIZE      sorter references threshold size\n"
10697#endif
10698  "   -stats               print memory stats before each finalize\n"
10699  "   -table               set output mode to 'table'\n"
10700  "   -tabs                set output mode to 'tabs'\n"
10701  "   -version             show SQLite version\n"
10702  "   -vfs NAME            use NAME as the default VFS\n"
10703#ifdef SQLITE_ENABLE_VFSTRACE
10704  "   -vfstrace            enable tracing of all VFS calls\n"
10705#endif
10706#ifdef SQLITE_HAVE_ZLIB
10707  "   -zip                 open the file as a ZIP Archive\n"
10708#endif
10709;
10710static void usage(int showDetail){
10711  utf8_printf(stderr,
10712      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10713      "FILENAME is the name of an SQLite database. A new database is created\n"
10714      "if the file does not previously exist.\n", Argv0);
10715  if( showDetail ){
10716    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10717  }else{
10718    raw_printf(stderr, "Use the -help option for additional information\n");
10719  }
10720  exit(1);
10721}
10722
10723/*
10724** Internal check:  Verify that the SQLite is uninitialized.  Print a
10725** error message if it is initialized.
10726*/
10727static void verify_uninitialized(void){
10728  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10729    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10730                        " initialization.\n");
10731  }
10732}
10733
10734/*
10735** Initialize the state information in data
10736*/
10737static void main_init(ShellState *data) {
10738  memset(data, 0, sizeof(*data));
10739  data->normalMode = data->cMode = data->mode = MODE_List;
10740  data->autoExplain = 1;
10741  memcpy(data->colSeparator,SEP_Column, 2);
10742  memcpy(data->rowSeparator,SEP_Row, 2);
10743  data->showHeader = 0;
10744  data->shellFlgs = SHFLG_Lookaside;
10745  verify_uninitialized();
10746  sqlite3_config(SQLITE_CONFIG_URI, 1);
10747  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10748  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10749  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10750  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10751}
10752
10753/*
10754** Output text to the console in a font that attracts extra attention.
10755*/
10756#ifdef _WIN32
10757static void printBold(const char *zText){
10758#if !SQLITE_OS_WINRT
10759  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10760  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10761  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10762  SetConsoleTextAttribute(out,
10763         FOREGROUND_RED|FOREGROUND_INTENSITY
10764  );
10765#endif
10766  printf("%s", zText);
10767#if !SQLITE_OS_WINRT
10768  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10769#endif
10770}
10771#else
10772static void printBold(const char *zText){
10773  printf("\033[1m%s\033[0m", zText);
10774}
10775#endif
10776
10777/*
10778** Get the argument to an --option.  Throw an error and die if no argument
10779** is available.
10780*/
10781static char *cmdline_option_value(int argc, char **argv, int i){
10782  if( i==argc ){
10783    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10784            argv[0], argv[argc-1]);
10785    exit(1);
10786  }
10787  return argv[i];
10788}
10789
10790#ifndef SQLITE_SHELL_IS_UTF8
10791#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
10792#    define SQLITE_SHELL_IS_UTF8          (0)
10793#  else
10794#    define SQLITE_SHELL_IS_UTF8          (1)
10795#  endif
10796#endif
10797
10798#if SQLITE_SHELL_IS_UTF8
10799int SQLITE_CDECL main(int argc, char **argv){
10800#else
10801int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10802  char **argv;
10803#endif
10804  char *zErrMsg = 0;
10805  ShellState data;
10806  const char *zInitFile = 0;
10807  int i;
10808  int rc = 0;
10809  int warnInmemoryDb = 0;
10810  int readStdin = 1;
10811  int nCmd = 0;
10812  char **azCmd = 0;
10813  const char *zVfs = 0;           /* Value of -vfs command-line option */
10814#if !SQLITE_SHELL_IS_UTF8
10815  char **argvToFree = 0;
10816  int argcToFree = 0;
10817#endif
10818
10819  setBinaryMode(stdin, 0);
10820  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10821  stdin_is_interactive = isatty(0);
10822  stdout_is_console = isatty(1);
10823
10824#ifdef SQLITE_DEBUG
10825  registerOomSimulator();
10826#endif
10827
10828#if !defined(_WIN32_WCE)
10829  if( getenv("SQLITE_DEBUG_BREAK") ){
10830    if( isatty(0) && isatty(2) ){
10831      fprintf(stderr,
10832          "attach debugger to process %d and press any key to continue.\n",
10833          GETPID());
10834      fgetc(stdin);
10835    }else{
10836#if defined(_WIN32) || defined(WIN32)
10837#if SQLITE_OS_WINRT
10838      __debugbreak();
10839#else
10840      DebugBreak();
10841#endif
10842#elif defined(SIGTRAP)
10843      raise(SIGTRAP);
10844#endif
10845    }
10846  }
10847#endif
10848
10849#if USE_SYSTEM_SQLITE+0!=1
10850  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10851    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10852            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10853    exit(1);
10854  }
10855#endif
10856  main_init(&data);
10857
10858  /* On Windows, we must translate command-line arguments into UTF-8.
10859  ** The SQLite memory allocator subsystem has to be enabled in order to
10860  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10861  ** subsequent sqlite3_config() calls will work.  So copy all results into
10862  ** memory that does not come from the SQLite memory allocator.
10863  */
10864#if !SQLITE_SHELL_IS_UTF8
10865  sqlite3_initialize();
10866  argvToFree = malloc(sizeof(argv[0])*argc*2);
10867  argcToFree = argc;
10868  argv = argvToFree + argc;
10869  if( argv==0 ) shell_out_of_memory();
10870  for(i=0; i<argc; i++){
10871    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10872    int n;
10873    if( z==0 ) shell_out_of_memory();
10874    n = (int)strlen(z);
10875    argv[i] = malloc( n+1 );
10876    if( argv[i]==0 ) shell_out_of_memory();
10877    memcpy(argv[i], z, n+1);
10878    argvToFree[i] = argv[i];
10879    sqlite3_free(z);
10880  }
10881  sqlite3_shutdown();
10882#endif
10883
10884  assert( argc>=1 && argv && argv[0] );
10885  Argv0 = argv[0];
10886
10887  /* Make sure we have a valid signal handler early, before anything
10888  ** else is done.
10889  */
10890#ifdef SIGINT
10891  signal(SIGINT, interrupt_handler);
10892#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10893  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10894#endif
10895
10896#ifdef SQLITE_SHELL_DBNAME_PROC
10897  {
10898    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10899    ** of a C-function that will provide the name of the database file.  Use
10900    ** this compile-time option to embed this shell program in larger
10901    ** applications. */
10902    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10903    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10904    warnInmemoryDb = 0;
10905  }
10906#endif
10907
10908  /* Do an initial pass through the command-line argument to locate
10909  ** the name of the database file, the name of the initialization file,
10910  ** the size of the alternative malloc heap,
10911  ** and the first command to execute.
10912  */
10913  verify_uninitialized();
10914  for(i=1; i<argc; i++){
10915    char *z;
10916    z = argv[i];
10917    if( z[0]!='-' ){
10918      if( data.zDbFilename==0 ){
10919        data.zDbFilename = z;
10920      }else{
10921        /* Excesss arguments are interpreted as SQL (or dot-commands) and
10922        ** mean that nothing is read from stdin */
10923        readStdin = 0;
10924        nCmd++;
10925        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
10926        if( azCmd==0 ) shell_out_of_memory();
10927        azCmd[nCmd-1] = z;
10928      }
10929    }
10930    if( z[1]=='-' ) z++;
10931    if( strcmp(z,"-separator")==0
10932     || strcmp(z,"-nullvalue")==0
10933     || strcmp(z,"-newline")==0
10934     || strcmp(z,"-cmd")==0
10935    ){
10936      (void)cmdline_option_value(argc, argv, ++i);
10937    }else if( strcmp(z,"-init")==0 ){
10938      zInitFile = cmdline_option_value(argc, argv, ++i);
10939    }else if( strcmp(z,"-batch")==0 ){
10940      /* Need to check for batch mode here to so we can avoid printing
10941      ** informational messages (like from process_sqliterc) before
10942      ** we do the actual processing of arguments later in a second pass.
10943      */
10944      stdin_is_interactive = 0;
10945    }else if( strcmp(z,"-heap")==0 ){
10946#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10947      const char *zSize;
10948      sqlite3_int64 szHeap;
10949
10950      zSize = cmdline_option_value(argc, argv, ++i);
10951      szHeap = integerValue(zSize);
10952      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
10953      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
10954#else
10955      (void)cmdline_option_value(argc, argv, ++i);
10956#endif
10957    }else if( strcmp(z,"-pagecache")==0 ){
10958      sqlite3_int64 n, sz;
10959      sz = integerValue(cmdline_option_value(argc,argv,++i));
10960      if( sz>70000 ) sz = 70000;
10961      if( sz<0 ) sz = 0;
10962      n = integerValue(cmdline_option_value(argc,argv,++i));
10963      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
10964        n = 0xffffffffffffLL/sz;
10965      }
10966      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
10967                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
10968      data.shellFlgs |= SHFLG_Pagecache;
10969    }else if( strcmp(z,"-lookaside")==0 ){
10970      int n, sz;
10971      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10972      if( sz<0 ) sz = 0;
10973      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10974      if( n<0 ) n = 0;
10975      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
10976      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
10977#ifdef SQLITE_ENABLE_VFSTRACE
10978    }else if( strcmp(z,"-vfstrace")==0 ){
10979      extern int vfstrace_register(
10980         const char *zTraceName,
10981         const char *zOldVfsName,
10982         int (*xOut)(const char*,void*),
10983         void *pOutArg,
10984         int makeDefault
10985      );
10986      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
10987#endif
10988#ifdef SQLITE_ENABLE_MULTIPLEX
10989    }else if( strcmp(z,"-multiplex")==0 ){
10990      extern int sqlite3_multiple_initialize(const char*,int);
10991      sqlite3_multiplex_initialize(0, 1);
10992#endif
10993    }else if( strcmp(z,"-mmap")==0 ){
10994      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10995      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
10996#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10997    }else if( strcmp(z,"-sorterref")==0 ){
10998      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10999      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11000#endif
11001    }else if( strcmp(z,"-vfs")==0 ){
11002      zVfs = cmdline_option_value(argc, argv, ++i);
11003#ifdef SQLITE_HAVE_ZLIB
11004    }else if( strcmp(z,"-zip")==0 ){
11005      data.openMode = SHELL_OPEN_ZIPFILE;
11006#endif
11007    }else if( strcmp(z,"-append")==0 ){
11008      data.openMode = SHELL_OPEN_APPENDVFS;
11009#ifdef SQLITE_ENABLE_DESERIALIZE
11010    }else if( strcmp(z,"-deserialize")==0 ){
11011      data.openMode = SHELL_OPEN_DESERIALIZE;
11012    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11013      data.szMax = integerValue(argv[++i]);
11014#endif
11015    }else if( strcmp(z,"-readonly")==0 ){
11016      data.openMode = SHELL_OPEN_READONLY;
11017    }else if( strcmp(z,"-nofollow")==0 ){
11018      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11019#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11020    }else if( strncmp(z, "-A",2)==0 ){
11021      /* All remaining command-line arguments are passed to the ".archive"
11022      ** command, so ignore them */
11023      break;
11024#endif
11025    }else if( strcmp(z, "-memtrace")==0 ){
11026      sqlite3MemTraceActivate(stderr);
11027    }else if( strcmp(z,"-bail")==0 ){
11028      bail_on_error = 1;
11029    }
11030  }
11031  verify_uninitialized();
11032
11033
11034#ifdef SQLITE_SHELL_INIT_PROC
11035  {
11036    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11037    ** of a C-function that will perform initialization actions on SQLite that
11038    ** occur just before or after sqlite3_initialize(). Use this compile-time
11039    ** option to embed this shell program in larger applications. */
11040    extern void SQLITE_SHELL_INIT_PROC(void);
11041    SQLITE_SHELL_INIT_PROC();
11042  }
11043#else
11044  /* All the sqlite3_config() calls have now been made. So it is safe
11045  ** to call sqlite3_initialize() and process any command line -vfs option. */
11046  sqlite3_initialize();
11047#endif
11048
11049  if( zVfs ){
11050    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11051    if( pVfs ){
11052      sqlite3_vfs_register(pVfs, 1);
11053    }else{
11054      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11055      exit(1);
11056    }
11057  }
11058
11059  if( data.zDbFilename==0 ){
11060#ifndef SQLITE_OMIT_MEMORYDB
11061    data.zDbFilename = ":memory:";
11062    warnInmemoryDb = argc==1;
11063#else
11064    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11065    return 1;
11066#endif
11067  }
11068  data.out = stdout;
11069  sqlite3_appendvfs_init(0,0,0);
11070
11071  /* Go ahead and open the database file if it already exists.  If the
11072  ** file does not exist, delay opening it.  This prevents empty database
11073  ** files from being created if a user mistypes the database name argument
11074  ** to the sqlite command-line tool.
11075  */
11076  if( access(data.zDbFilename, 0)==0 ){
11077    open_db(&data, 0);
11078  }
11079
11080  /* Process the initialization file if there is one.  If no -init option
11081  ** is given on the command line, look for a file named ~/.sqliterc and
11082  ** try to process it.
11083  */
11084  process_sqliterc(&data,zInitFile);
11085
11086  /* Make a second pass through the command-line argument and set
11087  ** options.  This second pass is delayed until after the initialization
11088  ** file is processed so that the command-line arguments will override
11089  ** settings in the initialization file.
11090  */
11091  for(i=1; i<argc; i++){
11092    char *z = argv[i];
11093    if( z[0]!='-' ) continue;
11094    if( z[1]=='-' ){ z++; }
11095    if( strcmp(z,"-init")==0 ){
11096      i++;
11097    }else if( strcmp(z,"-html")==0 ){
11098      data.mode = MODE_Html;
11099    }else if( strcmp(z,"-list")==0 ){
11100      data.mode = MODE_List;
11101    }else if( strcmp(z,"-quote")==0 ){
11102      data.mode = MODE_Quote;
11103      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11104      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11105    }else if( strcmp(z,"-line")==0 ){
11106      data.mode = MODE_Line;
11107    }else if( strcmp(z,"-column")==0 ){
11108      data.mode = MODE_Column;
11109    }else if( strcmp(z,"-json")==0 ){
11110      data.mode = MODE_Json;
11111    }else if( strcmp(z,"-markdown")==0 ){
11112      data.mode = MODE_Markdown;
11113    }else if( strcmp(z,"-table")==0 ){
11114      data.mode = MODE_Table;
11115    }else if( strcmp(z,"-box")==0 ){
11116      data.mode = MODE_Box;
11117    }else if( strcmp(z,"-csv")==0 ){
11118      data.mode = MODE_Csv;
11119      memcpy(data.colSeparator,",",2);
11120#ifdef SQLITE_HAVE_ZLIB
11121    }else if( strcmp(z,"-zip")==0 ){
11122      data.openMode = SHELL_OPEN_ZIPFILE;
11123#endif
11124    }else if( strcmp(z,"-append")==0 ){
11125      data.openMode = SHELL_OPEN_APPENDVFS;
11126#ifdef SQLITE_ENABLE_DESERIALIZE
11127    }else if( strcmp(z,"-deserialize")==0 ){
11128      data.openMode = SHELL_OPEN_DESERIALIZE;
11129    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11130      data.szMax = integerValue(argv[++i]);
11131#endif
11132    }else if( strcmp(z,"-readonly")==0 ){
11133      data.openMode = SHELL_OPEN_READONLY;
11134    }else if( strcmp(z,"-nofollow")==0 ){
11135      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11136    }else if( strcmp(z,"-ascii")==0 ){
11137      data.mode = MODE_Ascii;
11138      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
11139      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
11140    }else if( strcmp(z,"-tabs")==0 ){
11141      data.mode = MODE_List;
11142      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
11143      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11144    }else if( strcmp(z,"-separator")==0 ){
11145      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11146                       "%s",cmdline_option_value(argc,argv,++i));
11147    }else if( strcmp(z,"-newline")==0 ){
11148      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11149                       "%s",cmdline_option_value(argc,argv,++i));
11150    }else if( strcmp(z,"-nullvalue")==0 ){
11151      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11152                       "%s",cmdline_option_value(argc,argv,++i));
11153    }else if( strcmp(z,"-header")==0 ){
11154      data.showHeader = 1;
11155    }else if( strcmp(z,"-noheader")==0 ){
11156      data.showHeader = 0;
11157    }else if( strcmp(z,"-echo")==0 ){
11158      ShellSetFlag(&data, SHFLG_Echo);
11159    }else if( strcmp(z,"-eqp")==0 ){
11160      data.autoEQP = AUTOEQP_on;
11161    }else if( strcmp(z,"-eqpfull")==0 ){
11162      data.autoEQP = AUTOEQP_full;
11163    }else if( strcmp(z,"-stats")==0 ){
11164      data.statsOn = 1;
11165    }else if( strcmp(z,"-scanstats")==0 ){
11166      data.scanstatsOn = 1;
11167    }else if( strcmp(z,"-backslash")==0 ){
11168      /* Undocumented command-line option: -backslash
11169      ** Causes C-style backslash escapes to be evaluated in SQL statements
11170      ** prior to sending the SQL into SQLite.  Useful for injecting
11171      ** crazy bytes in the middle of SQL statements for testing and debugging.
11172      */
11173      ShellSetFlag(&data, SHFLG_Backslash);
11174    }else if( strcmp(z,"-bail")==0 ){
11175      /* No-op.  The bail_on_error flag should already be set. */
11176    }else if( strcmp(z,"-version")==0 ){
11177      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11178      return 0;
11179    }else if( strcmp(z,"-interactive")==0 ){
11180      stdin_is_interactive = 1;
11181    }else if( strcmp(z,"-batch")==0 ){
11182      stdin_is_interactive = 0;
11183    }else if( strcmp(z,"-heap")==0 ){
11184      i++;
11185    }else if( strcmp(z,"-pagecache")==0 ){
11186      i+=2;
11187    }else if( strcmp(z,"-lookaside")==0 ){
11188      i+=2;
11189    }else if( strcmp(z,"-mmap")==0 ){
11190      i++;
11191    }else if( strcmp(z,"-memtrace")==0 ){
11192      i++;
11193#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11194    }else if( strcmp(z,"-sorterref")==0 ){
11195      i++;
11196#endif
11197    }else if( strcmp(z,"-vfs")==0 ){
11198      i++;
11199#ifdef SQLITE_ENABLE_VFSTRACE
11200    }else if( strcmp(z,"-vfstrace")==0 ){
11201      i++;
11202#endif
11203#ifdef SQLITE_ENABLE_MULTIPLEX
11204    }else if( strcmp(z,"-multiplex")==0 ){
11205      i++;
11206#endif
11207    }else if( strcmp(z,"-help")==0 ){
11208      usage(1);
11209    }else if( strcmp(z,"-cmd")==0 ){
11210      /* Run commands that follow -cmd first and separately from commands
11211      ** that simply appear on the command-line.  This seems goofy.  It would
11212      ** be better if all commands ran in the order that they appear.  But
11213      ** we retain the goofy behavior for historical compatibility. */
11214      if( i==argc-1 ) break;
11215      z = cmdline_option_value(argc,argv,++i);
11216      if( z[0]=='.' ){
11217        rc = do_meta_command(z, &data);
11218        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11219      }else{
11220        open_db(&data, 0);
11221        rc = shell_exec(&data, z, &zErrMsg);
11222        if( zErrMsg!=0 ){
11223          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11224          if( bail_on_error ) return rc!=0 ? rc : 1;
11225        }else if( rc!=0 ){
11226          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11227          if( bail_on_error ) return rc;
11228        }
11229      }
11230#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11231    }else if( strncmp(z, "-A", 2)==0 ){
11232      if( nCmd>0 ){
11233        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11234                            " with \"%s\"\n", z);
11235        return 1;
11236      }
11237      open_db(&data, OPEN_DB_ZIPFILE);
11238      if( z[2] ){
11239        argv[i] = &z[2];
11240        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11241      }else{
11242        arDotCommand(&data, 1, argv+i, argc-i);
11243      }
11244      readStdin = 0;
11245      break;
11246#endif
11247    }else{
11248      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11249      raw_printf(stderr,"Use -help for a list of options.\n");
11250      return 1;
11251    }
11252    data.cMode = data.mode;
11253  }
11254
11255  if( !readStdin ){
11256    /* Run all arguments that do not begin with '-' as if they were separate
11257    ** command-line inputs, except for the argToSkip argument which contains
11258    ** the database filename.
11259    */
11260    for(i=0; i<nCmd; i++){
11261      if( azCmd[i][0]=='.' ){
11262        rc = do_meta_command(azCmd[i], &data);
11263        if( rc ){
11264          free(azCmd);
11265          return rc==2 ? 0 : rc;
11266        }
11267      }else{
11268        open_db(&data, 0);
11269        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11270        if( zErrMsg || rc ){
11271          if( zErrMsg!=0 ){
11272            utf8_printf(stderr,"Error: %s\n", zErrMsg);
11273          }else{
11274            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11275          }
11276          sqlite3_free(zErrMsg);
11277          free(azCmd);
11278          return rc!=0 ? rc : 1;
11279        }
11280      }
11281    }
11282  }else{
11283    /* Run commands received from standard input
11284    */
11285    if( stdin_is_interactive ){
11286      char *zHome;
11287      char *zHistory;
11288      int nHistory;
11289      printf(
11290        "SQLite version %s %.19s\n" /*extra-version-info*/
11291        "Enter \".help\" for usage hints.\n",
11292        sqlite3_libversion(), sqlite3_sourceid()
11293      );
11294      if( warnInmemoryDb ){
11295        printf("Connected to a ");
11296        printBold("transient in-memory database");
11297        printf(".\nUse \".open FILENAME\" to reopen on a "
11298               "persistent database.\n");
11299      }
11300      zHistory = getenv("SQLITE_HISTORY");
11301      if( zHistory ){
11302        zHistory = strdup(zHistory);
11303      }else if( (zHome = find_home_dir(0))!=0 ){
11304        nHistory = strlen30(zHome) + 20;
11305        if( (zHistory = malloc(nHistory))!=0 ){
11306          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11307        }
11308      }
11309      if( zHistory ){ shell_read_history(zHistory); }
11310#if HAVE_READLINE || HAVE_EDITLINE
11311      rl_attempted_completion_function = readline_completion;
11312#elif HAVE_LINENOISE
11313      linenoiseSetCompletionCallback(linenoise_completion);
11314#endif
11315      data.in = 0;
11316      rc = process_input(&data);
11317      if( zHistory ){
11318        shell_stifle_history(2000);
11319        shell_write_history(zHistory);
11320        free(zHistory);
11321      }
11322    }else{
11323      data.in = stdin;
11324      rc = process_input(&data);
11325    }
11326  }
11327  free(azCmd);
11328  set_table_name(&data, 0);
11329  if( data.db ){
11330    session_close_all(&data);
11331    close_db(data.db);
11332  }
11333  sqlite3_free(data.zFreeOnClose);
11334  find_home_dir(1);
11335  output_reset(&data);
11336  data.doXdgOpen = 0;
11337  clearTempFile(&data);
11338#if !SQLITE_SHELL_IS_UTF8
11339  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11340  free(argvToFree);
11341#endif
11342  free(data.colWidth);
11343  /* Clear the global data structure so that valgrind will detect memory
11344  ** leaks */
11345  memset(&data, 0, sizeof(data));
11346  return rc;
11347}
11348