xref: /sqlite-3.40.0/src/shell.c.in (revision 73983658)
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code.  In place of
5** a legal notice, here is a blessing:
6**
7**    May you do good and not evil.
8**    May you find forgiveness for yourself and forgive others.
9**    May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14*/
15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16/* This needs to come before any includes for MSVC compiler */
17#define _CRT_SECURE_NO_WARNINGS
18#endif
19
20/*
21** Optionally #include a user-defined header, whereby compilation options
22** may be set prior to where they take effect, but after platform setup.
23** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
24** file. Note that this macro has a like effect on sqlite3.c compilation.
25*/
26#ifdef SQLITE_CUSTOM_INCLUDE
27# define INC_STRINGIFY_(f) #f
28# define INC_STRINGIFY(f) INC_STRINGIFY_(f)
29# include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
30#endif
31
32/*
33** Determine if we are dealing with WinRT, which provides only a subset of
34** the full Win32 API.
35*/
36#if !defined(SQLITE_OS_WINRT)
37# define SQLITE_OS_WINRT 0
38#endif
39
40/*
41** Warning pragmas copied from msvc.h in the core.
42*/
43#if defined(_MSC_VER)
44#pragma warning(disable : 4054)
45#pragma warning(disable : 4055)
46#pragma warning(disable : 4100)
47#pragma warning(disable : 4127)
48#pragma warning(disable : 4130)
49#pragma warning(disable : 4152)
50#pragma warning(disable : 4189)
51#pragma warning(disable : 4206)
52#pragma warning(disable : 4210)
53#pragma warning(disable : 4232)
54#pragma warning(disable : 4244)
55#pragma warning(disable : 4305)
56#pragma warning(disable : 4306)
57#pragma warning(disable : 4702)
58#pragma warning(disable : 4706)
59#endif /* defined(_MSC_VER) */
60
61/*
62** No support for loadable extensions in VxWorks.
63*/
64#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
65# define SQLITE_OMIT_LOAD_EXTENSION 1
66#endif
67
68/*
69** Enable large-file support for fopen() and friends on unix.
70*/
71#ifndef SQLITE_DISABLE_LFS
72# define _LARGE_FILE       1
73# ifndef _FILE_OFFSET_BITS
74#   define _FILE_OFFSET_BITS 64
75# endif
76# define _LARGEFILE_SOURCE 1
77#endif
78
79#include <stdlib.h>
80#include <string.h>
81#include <stdio.h>
82#include <assert.h>
83#include "sqlite3.h"
84typedef sqlite3_int64 i64;
85typedef sqlite3_uint64 u64;
86typedef unsigned char u8;
87#if SQLITE_USER_AUTHENTICATION
88# include "sqlite3userauth.h"
89#endif
90#include <ctype.h>
91#include <stdarg.h>
92
93#if !defined(_WIN32) && !defined(WIN32)
94# include <signal.h>
95# if !defined(__RTP__) && !defined(_WRS_KERNEL)
96#  include <pwd.h>
97# endif
98#endif
99#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
100# include <unistd.h>
101# include <dirent.h>
102# define GETPID getpid
103# if defined(__MINGW32__)
104#  define DIRENT dirent
105#  ifndef S_ISLNK
106#   define S_ISLNK(mode) (0)
107#  endif
108# endif
109#else
110# define GETPID (int)GetCurrentProcessId
111#endif
112#include <sys/types.h>
113#include <sys/stat.h>
114
115#if HAVE_READLINE
116# include <readline/readline.h>
117# include <readline/history.h>
118#endif
119
120#if HAVE_EDITLINE
121# include <editline/readline.h>
122#endif
123
124#if HAVE_EDITLINE || HAVE_READLINE
125
126# define shell_add_history(X) add_history(X)
127# define shell_read_history(X) read_history(X)
128# define shell_write_history(X) write_history(X)
129# define shell_stifle_history(X) stifle_history(X)
130# define shell_readline(X) readline(X)
131
132#elif HAVE_LINENOISE
133
134# include "linenoise.h"
135# define shell_add_history(X) linenoiseHistoryAdd(X)
136# define shell_read_history(X) linenoiseHistoryLoad(X)
137# define shell_write_history(X) linenoiseHistorySave(X)
138# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
139# define shell_readline(X) linenoise(X)
140
141#else
142
143# define shell_read_history(X)
144# define shell_write_history(X)
145# define shell_stifle_history(X)
146
147# define SHELL_USE_LOCAL_GETLINE 1
148#endif
149
150
151#if defined(_WIN32) || defined(WIN32)
152# if SQLITE_OS_WINRT
153#  define SQLITE_OMIT_POPEN 1
154# else
155#  include <io.h>
156#  include <fcntl.h>
157#  define isatty(h) _isatty(h)
158#  ifndef access
159#   define access(f,m) _access((f),(m))
160#  endif
161#  ifndef unlink
162#   define unlink _unlink
163#  endif
164#  ifndef strdup
165#   define strdup _strdup
166#  endif
167#  undef popen
168#  define popen _popen
169#  undef pclose
170#  define pclose _pclose
171# endif
172#else
173 /* Make sure isatty() has a prototype. */
174 extern int isatty(int);
175
176# if !defined(__RTP__) && !defined(_WRS_KERNEL)
177  /* popen and pclose are not C89 functions and so are
178  ** sometimes omitted from the <stdio.h> header */
179   extern FILE *popen(const char*,const char*);
180   extern int pclose(FILE*);
181# else
182#  define SQLITE_OMIT_POPEN 1
183# endif
184#endif
185
186#if defined(_WIN32_WCE)
187/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
188 * thus we always assume that we have a console. That can be
189 * overridden with the -batch command line option.
190 */
191#define isatty(x) 1
192#endif
193
194/* ctype macros that work with signed characters */
195#define IsSpace(X)  isspace((unsigned char)X)
196#define IsDigit(X)  isdigit((unsigned char)X)
197#define ToLower(X)  (char)tolower((unsigned char)X)
198
199#if defined(_WIN32) || defined(WIN32)
200#if SQLITE_OS_WINRT
201#include <intrin.h>
202#endif
203#include <windows.h>
204
205/* string conversion routines only needed on Win32 */
206extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
207extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
208extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
209extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
210#endif
211
212/* On Windows, we normally run with output mode of TEXT so that \n characters
213** are automatically translated into \r\n.  However, this behavior needs
214** to be disabled in some cases (ex: when generating CSV output and when
215** rendering quoted strings that contain \n characters).  The following
216** routines take care of that.
217*/
218#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
219static void setBinaryMode(FILE *file, int isOutput){
220  if( isOutput ) fflush(file);
221  _setmode(_fileno(file), _O_BINARY);
222}
223static void setTextMode(FILE *file, int isOutput){
224  if( isOutput ) fflush(file);
225  _setmode(_fileno(file), _O_TEXT);
226}
227#else
228# define setBinaryMode(X,Y)
229# define setTextMode(X,Y)
230#endif
231
232
233/* True if the timer is enabled */
234static int enableTimer = 0;
235
236/* Return the current wall-clock time */
237static sqlite3_int64 timeOfDay(void){
238  static sqlite3_vfs *clockVfs = 0;
239  sqlite3_int64 t;
240  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
241  if( clockVfs==0 ) return 0;  /* Never actually happens */
242  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
243    clockVfs->xCurrentTimeInt64(clockVfs, &t);
244  }else{
245    double r;
246    clockVfs->xCurrentTime(clockVfs, &r);
247    t = (sqlite3_int64)(r*86400000.0);
248  }
249  return t;
250}
251
252#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
253#include <sys/time.h>
254#include <sys/resource.h>
255
256/* VxWorks does not support getrusage() as far as we can determine */
257#if defined(_WRS_KERNEL) || defined(__RTP__)
258struct rusage {
259  struct timeval ru_utime; /* user CPU time used */
260  struct timeval ru_stime; /* system CPU time used */
261};
262#define getrusage(A,B) memset(B,0,sizeof(*B))
263#endif
264
265/* Saved resource information for the beginning of an operation */
266static struct rusage sBegin;  /* CPU time at start */
267static sqlite3_int64 iBegin;  /* Wall-clock time at start */
268
269/*
270** Begin timing an operation
271*/
272static void beginTimer(void){
273  if( enableTimer ){
274    getrusage(RUSAGE_SELF, &sBegin);
275    iBegin = timeOfDay();
276  }
277}
278
279/* Return the difference of two time_structs in seconds */
280static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
281  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
282         (double)(pEnd->tv_sec - pStart->tv_sec);
283}
284
285/*
286** Print the timing results.
287*/
288static void endTimer(void){
289  if( enableTimer ){
290    sqlite3_int64 iEnd = timeOfDay();
291    struct rusage sEnd;
292    getrusage(RUSAGE_SELF, &sEnd);
293    printf("Run Time: real %.3f user %f sys %f\n",
294       (iEnd - iBegin)*0.001,
295       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
296       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
297  }
298}
299
300#define BEGIN_TIMER beginTimer()
301#define END_TIMER endTimer()
302#define HAS_TIMER 1
303
304#elif (defined(_WIN32) || defined(WIN32))
305
306/* Saved resource information for the beginning of an operation */
307static HANDLE hProcess;
308static FILETIME ftKernelBegin;
309static FILETIME ftUserBegin;
310static sqlite3_int64 ftWallBegin;
311typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
312                                    LPFILETIME, LPFILETIME);
313static GETPROCTIMES getProcessTimesAddr = NULL;
314
315/*
316** Check to see if we have timer support.  Return 1 if necessary
317** support found (or found previously).
318*/
319static int hasTimer(void){
320  if( getProcessTimesAddr ){
321    return 1;
322  } else {
323#if !SQLITE_OS_WINRT
324    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
325    ** versions. See if the version we are running on has it, and if it
326    ** does, save off a pointer to it and the current process handle.
327    */
328    hProcess = GetCurrentProcess();
329    if( hProcess ){
330      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
331      if( NULL != hinstLib ){
332        getProcessTimesAddr =
333            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
334        if( NULL != getProcessTimesAddr ){
335          return 1;
336        }
337        FreeLibrary(hinstLib);
338      }
339    }
340#endif
341  }
342  return 0;
343}
344
345/*
346** Begin timing an operation
347*/
348static void beginTimer(void){
349  if( enableTimer && getProcessTimesAddr ){
350    FILETIME ftCreation, ftExit;
351    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
352                        &ftKernelBegin,&ftUserBegin);
353    ftWallBegin = timeOfDay();
354  }
355}
356
357/* Return the difference of two FILETIME structs in seconds */
358static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
359  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
360  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
361  return (double) ((i64End - i64Start) / 10000000.0);
362}
363
364/*
365** Print the timing results.
366*/
367static void endTimer(void){
368  if( enableTimer && getProcessTimesAddr){
369    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
370    sqlite3_int64 ftWallEnd = timeOfDay();
371    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
372    printf("Run Time: real %.3f user %f sys %f\n",
373       (ftWallEnd - ftWallBegin)*0.001,
374       timeDiff(&ftUserBegin, &ftUserEnd),
375       timeDiff(&ftKernelBegin, &ftKernelEnd));
376  }
377}
378
379#define BEGIN_TIMER beginTimer()
380#define END_TIMER endTimer()
381#define HAS_TIMER hasTimer()
382
383#else
384#define BEGIN_TIMER
385#define END_TIMER
386#define HAS_TIMER 0
387#endif
388
389/*
390** Used to prevent warnings about unused parameters
391*/
392#define UNUSED_PARAMETER(x) (void)(x)
393
394/*
395** Number of elements in an array
396*/
397#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
398
399/*
400** If the following flag is set, then command execution stops
401** at an error if we are not interactive.
402*/
403static int bail_on_error = 0;
404
405/*
406** Threat stdin as an interactive input if the following variable
407** is true.  Otherwise, assume stdin is connected to a file or pipe.
408*/
409static int stdin_is_interactive = 1;
410
411/*
412** On Windows systems we have to know if standard output is a console
413** in order to translate UTF-8 into MBCS.  The following variable is
414** true if translation is required.
415*/
416static int stdout_is_console = 1;
417
418/*
419** The following is the open SQLite database.  We make a pointer
420** to this database a static variable so that it can be accessed
421** by the SIGINT handler to interrupt database processing.
422*/
423static sqlite3 *globalDb = 0;
424
425/*
426** True if an interrupt (Control-C) has been received.
427*/
428static volatile int seenInterrupt = 0;
429
430#ifdef SQLITE_DEBUG
431/*
432** Out-of-memory simulator variables
433*/
434static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
435static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
436static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
437#endif /* SQLITE_DEBUG */
438
439/*
440** This is the name of our program. It is set in main(), used
441** in a number of other places, mostly for error messages.
442*/
443static char *Argv0;
444
445/*
446** Prompt strings. Initialized in main. Settable with
447**   .prompt main continue
448*/
449static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
450static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
451
452/*
453** Render output like fprintf().  Except, if the output is going to the
454** console and if this is running on a Windows machine, translate the
455** output from UTF-8 into MBCS.
456*/
457#if defined(_WIN32) || defined(WIN32)
458void utf8_printf(FILE *out, const char *zFormat, ...){
459  va_list ap;
460  va_start(ap, zFormat);
461  if( stdout_is_console && (out==stdout || out==stderr) ){
462    char *z1 = sqlite3_vmprintf(zFormat, ap);
463    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
464    sqlite3_free(z1);
465    fputs(z2, out);
466    sqlite3_free(z2);
467  }else{
468    vfprintf(out, zFormat, ap);
469  }
470  va_end(ap);
471}
472#elif !defined(utf8_printf)
473# define utf8_printf fprintf
474#endif
475
476/*
477** Render output like fprintf().  This should not be used on anything that
478** includes string formatting (e.g. "%s").
479*/
480#if !defined(raw_printf)
481# define raw_printf fprintf
482#endif
483
484/* Indicate out-of-memory and exit. */
485static void shell_out_of_memory(void){
486  raw_printf(stderr,"Error: out of memory\n");
487  exit(1);
488}
489
490#ifdef SQLITE_DEBUG
491/* This routine is called when a simulated OOM occurs.  It is broken
492** out as a separate routine to make it easy to set a breakpoint on
493** the OOM
494*/
495void shellOomFault(void){
496  if( oomRepeat>0 ){
497    oomRepeat--;
498  }else{
499    oomCounter--;
500  }
501}
502#endif /* SQLITE_DEBUG */
503
504#ifdef SQLITE_DEBUG
505/* This routine is a replacement malloc() that is used to simulate
506** Out-Of-Memory (OOM) errors for testing purposes.
507*/
508static void *oomMalloc(int nByte){
509  if( oomCounter ){
510    if( oomCounter==1 ){
511      shellOomFault();
512      return 0;
513    }else{
514      oomCounter--;
515    }
516  }
517  return defaultMalloc(nByte);
518}
519#endif /* SQLITE_DEBUG */
520
521#ifdef SQLITE_DEBUG
522/* Register the OOM simulator.  This must occur before any memory
523** allocations */
524static void registerOomSimulator(void){
525  sqlite3_mem_methods mem;
526  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
527  defaultMalloc = mem.xMalloc;
528  mem.xMalloc = oomMalloc;
529  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
530}
531#endif
532
533/*
534** Write I/O traces to the following stream.
535*/
536#ifdef SQLITE_ENABLE_IOTRACE
537static FILE *iotrace = 0;
538#endif
539
540/*
541** This routine works like printf in that its first argument is a
542** format string and subsequent arguments are values to be substituted
543** in place of % fields.  The result of formatting this string
544** is written to iotrace.
545*/
546#ifdef SQLITE_ENABLE_IOTRACE
547static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
548  va_list ap;
549  char *z;
550  if( iotrace==0 ) return;
551  va_start(ap, zFormat);
552  z = sqlite3_vmprintf(zFormat, ap);
553  va_end(ap);
554  utf8_printf(iotrace, "%s", z);
555  sqlite3_free(z);
556}
557#endif
558
559/*
560** Output string zUtf to stream pOut as w characters.  If w is negative,
561** then right-justify the text.  W is the width in UTF-8 characters, not
562** in bytes.  This is different from the %*.*s specification in printf
563** since with %*.*s the width is measured in bytes, not characters.
564*/
565static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
566  int i;
567  int n;
568  int aw = w<0 ? -w : w;
569  for(i=n=0; zUtf[i]; i++){
570    if( (zUtf[i]&0xc0)!=0x80 ){
571      n++;
572      if( n==aw ){
573        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
574        break;
575      }
576    }
577  }
578  if( n>=aw ){
579    utf8_printf(pOut, "%.*s", i, zUtf);
580  }else if( w<0 ){
581    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
582  }else{
583    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
584  }
585}
586
587
588/*
589** Determines if a string is a number of not.
590*/
591static int isNumber(const char *z, int *realnum){
592  if( *z=='-' || *z=='+' ) z++;
593  if( !IsDigit(*z) ){
594    return 0;
595  }
596  z++;
597  if( realnum ) *realnum = 0;
598  while( IsDigit(*z) ){ z++; }
599  if( *z=='.' ){
600    z++;
601    if( !IsDigit(*z) ) return 0;
602    while( IsDigit(*z) ){ z++; }
603    if( realnum ) *realnum = 1;
604  }
605  if( *z=='e' || *z=='E' ){
606    z++;
607    if( *z=='+' || *z=='-' ) z++;
608    if( !IsDigit(*z) ) return 0;
609    while( IsDigit(*z) ){ z++; }
610    if( realnum ) *realnum = 1;
611  }
612  return *z==0;
613}
614
615/*
616** Compute a string length that is limited to what can be stored in
617** lower 30 bits of a 32-bit signed integer.
618*/
619static int strlen30(const char *z){
620  const char *z2 = z;
621  while( *z2 ){ z2++; }
622  return 0x3fffffff & (int)(z2 - z);
623}
624
625/*
626** Return the length of a string in characters.  Multibyte UTF8 characters
627** count as a single character.
628*/
629static int strlenChar(const char *z){
630  int n = 0;
631  while( *z ){
632    if( (0xc0&*(z++))!=0x80 ) n++;
633  }
634  return n;
635}
636
637/*
638** Return true if zFile does not exist or if it is not an ordinary file.
639*/
640#ifdef _WIN32
641# define notNormalFile(X) 0
642#else
643static int notNormalFile(const char *zFile){
644  struct stat x;
645  int rc;
646  memset(&x, 0, sizeof(x));
647  rc = stat(zFile, &x);
648  return rc || !S_ISREG(x.st_mode);
649}
650#endif
651
652/*
653** This routine reads a line of text from FILE in, stores
654** the text in memory obtained from malloc() and returns a pointer
655** to the text.  NULL is returned at end of file, or if malloc()
656** fails.
657**
658** If zLine is not NULL then it is a malloced buffer returned from
659** a previous call to this routine that may be reused.
660*/
661static char *local_getline(char *zLine, FILE *in){
662  int nLine = zLine==0 ? 0 : 100;
663  int n = 0;
664
665  while( 1 ){
666    if( n+100>nLine ){
667      nLine = nLine*2 + 100;
668      zLine = realloc(zLine, nLine);
669      if( zLine==0 ) shell_out_of_memory();
670    }
671    if( fgets(&zLine[n], nLine - n, in)==0 ){
672      if( n==0 ){
673        free(zLine);
674        return 0;
675      }
676      zLine[n] = 0;
677      break;
678    }
679    while( zLine[n] ) n++;
680    if( n>0 && zLine[n-1]=='\n' ){
681      n--;
682      if( n>0 && zLine[n-1]=='\r' ) n--;
683      zLine[n] = 0;
684      break;
685    }
686  }
687#if defined(_WIN32) || defined(WIN32)
688  /* For interactive input on Windows systems, translate the
689  ** multi-byte characterset characters into UTF-8. */
690  if( stdin_is_interactive && in==stdin ){
691    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
692    if( zTrans ){
693      int nTrans = strlen30(zTrans)+1;
694      if( nTrans>nLine ){
695        zLine = realloc(zLine, nTrans);
696        if( zLine==0 ) shell_out_of_memory();
697      }
698      memcpy(zLine, zTrans, nTrans);
699      sqlite3_free(zTrans);
700    }
701  }
702#endif /* defined(_WIN32) || defined(WIN32) */
703  return zLine;
704}
705
706/*
707** Retrieve a single line of input text.
708**
709** If in==0 then read from standard input and prompt before each line.
710** If isContinuation is true, then a continuation prompt is appropriate.
711** If isContinuation is zero, then the main prompt should be used.
712**
713** If zPrior is not NULL then it is a buffer from a prior call to this
714** routine that can be reused.
715**
716** The result is stored in space obtained from malloc() and must either
717** be freed by the caller or else passed back into this routine via the
718** zPrior argument for reuse.
719*/
720static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
721  char *zPrompt;
722  char *zResult;
723  if( in!=0 ){
724    zResult = local_getline(zPrior, in);
725  }else{
726    zPrompt = isContinuation ? continuePrompt : mainPrompt;
727#if SHELL_USE_LOCAL_GETLINE
728    printf("%s", zPrompt);
729    fflush(stdout);
730    zResult = local_getline(zPrior, stdin);
731#else
732    free(zPrior);
733    zResult = shell_readline(zPrompt);
734    if( zResult && *zResult ) shell_add_history(zResult);
735#endif
736  }
737  return zResult;
738}
739
740
741/*
742** Return the value of a hexadecimal digit.  Return -1 if the input
743** is not a hex digit.
744*/
745static int hexDigitValue(char c){
746  if( c>='0' && c<='9' ) return c - '0';
747  if( c>='a' && c<='f' ) return c - 'a' + 10;
748  if( c>='A' && c<='F' ) return c - 'A' + 10;
749  return -1;
750}
751
752/*
753** Interpret zArg as an integer value, possibly with suffixes.
754*/
755static sqlite3_int64 integerValue(const char *zArg){
756  sqlite3_int64 v = 0;
757  static const struct { char *zSuffix; int iMult; } aMult[] = {
758    { "KiB", 1024 },
759    { "MiB", 1024*1024 },
760    { "GiB", 1024*1024*1024 },
761    { "KB",  1000 },
762    { "MB",  1000000 },
763    { "GB",  1000000000 },
764    { "K",   1000 },
765    { "M",   1000000 },
766    { "G",   1000000000 },
767  };
768  int i;
769  int isNeg = 0;
770  if( zArg[0]=='-' ){
771    isNeg = 1;
772    zArg++;
773  }else if( zArg[0]=='+' ){
774    zArg++;
775  }
776  if( zArg[0]=='0' && zArg[1]=='x' ){
777    int x;
778    zArg += 2;
779    while( (x = hexDigitValue(zArg[0]))>=0 ){
780      v = (v<<4) + x;
781      zArg++;
782    }
783  }else{
784    while( IsDigit(zArg[0]) ){
785      v = v*10 + zArg[0] - '0';
786      zArg++;
787    }
788  }
789  for(i=0; i<ArraySize(aMult); i++){
790    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
791      v *= aMult[i].iMult;
792      break;
793    }
794  }
795  return isNeg? -v : v;
796}
797
798/*
799** A variable length string to which one can append text.
800*/
801typedef struct ShellText ShellText;
802struct ShellText {
803  char *z;
804  int n;
805  int nAlloc;
806};
807
808/*
809** Initialize and destroy a ShellText object
810*/
811static void initText(ShellText *p){
812  memset(p, 0, sizeof(*p));
813}
814static void freeText(ShellText *p){
815  free(p->z);
816  initText(p);
817}
818
819/* zIn is either a pointer to a NULL-terminated string in memory obtained
820** from malloc(), or a NULL pointer. The string pointed to by zAppend is
821** added to zIn, and the result returned in memory obtained from malloc().
822** zIn, if it was not NULL, is freed.
823**
824** If the third argument, quote, is not '\0', then it is used as a
825** quote character for zAppend.
826*/
827static void appendText(ShellText *p, char const *zAppend, char quote){
828  int len;
829  int i;
830  int nAppend = strlen30(zAppend);
831
832  len = nAppend+p->n+1;
833  if( quote ){
834    len += 2;
835    for(i=0; i<nAppend; i++){
836      if( zAppend[i]==quote ) len++;
837    }
838  }
839
840  if( p->n+len>=p->nAlloc ){
841    p->nAlloc = p->nAlloc*2 + len + 20;
842    p->z = realloc(p->z, p->nAlloc);
843    if( p->z==0 ) shell_out_of_memory();
844  }
845
846  if( quote ){
847    char *zCsr = p->z+p->n;
848    *zCsr++ = quote;
849    for(i=0; i<nAppend; i++){
850      *zCsr++ = zAppend[i];
851      if( zAppend[i]==quote ) *zCsr++ = quote;
852    }
853    *zCsr++ = quote;
854    p->n = (int)(zCsr - p->z);
855    *zCsr = '\0';
856  }else{
857    memcpy(p->z+p->n, zAppend, nAppend);
858    p->n += nAppend;
859    p->z[p->n] = '\0';
860  }
861}
862
863/*
864** Attempt to determine if identifier zName needs to be quoted, either
865** because it contains non-alphanumeric characters, or because it is an
866** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
867** that quoting is required.
868**
869** Return '"' if quoting is required.  Return 0 if no quoting is required.
870*/
871static char quoteChar(const char *zName){
872  int i;
873  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
874  for(i=0; zName[i]; i++){
875    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
876  }
877  return sqlite3_keyword_check(zName, i) ? '"' : 0;
878}
879
880/*
881** Construct a fake object name and column list to describe the structure
882** of the view, virtual table, or table valued function zSchema.zName.
883*/
884static char *shellFakeSchema(
885  sqlite3 *db,            /* The database connection containing the vtab */
886  const char *zSchema,    /* Schema of the database holding the vtab */
887  const char *zName       /* The name of the virtual table */
888){
889  sqlite3_stmt *pStmt = 0;
890  char *zSql;
891  ShellText s;
892  char cQuote;
893  char *zDiv = "(";
894  int nRow = 0;
895
896  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
897                         zSchema ? zSchema : "main", zName);
898  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
899  sqlite3_free(zSql);
900  initText(&s);
901  if( zSchema ){
902    cQuote = quoteChar(zSchema);
903    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
904    appendText(&s, zSchema, cQuote);
905    appendText(&s, ".", 0);
906  }
907  cQuote = quoteChar(zName);
908  appendText(&s, zName, cQuote);
909  while( sqlite3_step(pStmt)==SQLITE_ROW ){
910    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
911    nRow++;
912    appendText(&s, zDiv, 0);
913    zDiv = ",";
914    cQuote = quoteChar(zCol);
915    appendText(&s, zCol, cQuote);
916  }
917  appendText(&s, ")", 0);
918  sqlite3_finalize(pStmt);
919  if( nRow==0 ){
920    freeText(&s);
921    s.z = 0;
922  }
923  return s.z;
924}
925
926/*
927** SQL function:  shell_module_schema(X)
928**
929** Return a fake schema for the table-valued function or eponymous virtual
930** table X.
931*/
932static void shellModuleSchema(
933  sqlite3_context *pCtx,
934  int nVal,
935  sqlite3_value **apVal
936){
937  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
938  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
939  UNUSED_PARAMETER(nVal);
940  if( zFake ){
941    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
942                        -1, sqlite3_free);
943    free(zFake);
944  }
945}
946
947/*
948** SQL function:  shell_add_schema(S,X)
949**
950** Add the schema name X to the CREATE statement in S and return the result.
951** Examples:
952**
953**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
954**
955** Also works on
956**
957**    CREATE INDEX
958**    CREATE UNIQUE INDEX
959**    CREATE VIEW
960**    CREATE TRIGGER
961**    CREATE VIRTUAL TABLE
962**
963** This UDF is used by the .schema command to insert the schema name of
964** attached databases into the middle of the sqlite_schema.sql field.
965*/
966static void shellAddSchemaName(
967  sqlite3_context *pCtx,
968  int nVal,
969  sqlite3_value **apVal
970){
971  static const char *aPrefix[] = {
972     "TABLE",
973     "INDEX",
974     "UNIQUE INDEX",
975     "VIEW",
976     "TRIGGER",
977     "VIRTUAL TABLE"
978  };
979  int i = 0;
980  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
981  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
982  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
983  sqlite3 *db = sqlite3_context_db_handle(pCtx);
984  UNUSED_PARAMETER(nVal);
985  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
986    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
987      int n = strlen30(aPrefix[i]);
988      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
989        char *z = 0;
990        char *zFake = 0;
991        if( zSchema ){
992          char cQuote = quoteChar(zSchema);
993          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
994            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
995          }else{
996            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
997          }
998        }
999        if( zName
1000         && aPrefix[i][0]=='V'
1001         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1002        ){
1003          if( z==0 ){
1004            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1005          }else{
1006            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1007          }
1008          free(zFake);
1009        }
1010        if( z ){
1011          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1012          return;
1013        }
1014      }
1015    }
1016  }
1017  sqlite3_result_value(pCtx, apVal[0]);
1018}
1019
1020/*
1021** The source code for several run-time loadable extensions is inserted
1022** below by the ../tool/mkshellc.tcl script.  Before processing that included
1023** code, we need to override some macros to make the included program code
1024** work here in the middle of this regular program.
1025*/
1026#define SQLITE_EXTENSION_INIT1
1027#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1028
1029#if defined(_WIN32) && defined(_MSC_VER)
1030INCLUDE test_windirent.h
1031INCLUDE test_windirent.c
1032#define dirent DIRENT
1033#endif
1034INCLUDE ../ext/misc/shathree.c
1035INCLUDE ../ext/misc/fileio.c
1036INCLUDE ../ext/misc/completion.c
1037INCLUDE ../ext/misc/appendvfs.c
1038INCLUDE ../ext/misc/memtrace.c
1039INCLUDE ../ext/misc/uint.c
1040INCLUDE ../ext/misc/decimal.c
1041INCLUDE ../ext/misc/ieee754.c
1042INCLUDE ../ext/misc/series.c
1043INCLUDE ../ext/misc/regexp.c
1044#ifdef SQLITE_HAVE_ZLIB
1045INCLUDE ../ext/misc/zipfile.c
1046INCLUDE ../ext/misc/sqlar.c
1047#endif
1048INCLUDE ../ext/expert/sqlite3expert.h
1049INCLUDE ../ext/expert/sqlite3expert.c
1050
1051#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1052INCLUDE ../ext/misc/dbdata.c
1053#endif
1054
1055#if defined(SQLITE_ENABLE_SESSION)
1056/*
1057** State information for a single open session
1058*/
1059typedef struct OpenSession OpenSession;
1060struct OpenSession {
1061  char *zName;             /* Symbolic name for this session */
1062  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1063  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1064  sqlite3_session *p;      /* The open session */
1065};
1066#endif
1067
1068typedef struct ExpertInfo ExpertInfo;
1069struct ExpertInfo {
1070  sqlite3expert *pExpert;
1071  int bVerbose;
1072};
1073
1074/* A single line in the EQP output */
1075typedef struct EQPGraphRow EQPGraphRow;
1076struct EQPGraphRow {
1077  int iEqpId;           /* ID for this row */
1078  int iParentId;        /* ID of the parent row */
1079  EQPGraphRow *pNext;   /* Next row in sequence */
1080  char zText[1];        /* Text to display for this row */
1081};
1082
1083/* All EQP output is collected into an instance of the following */
1084typedef struct EQPGraph EQPGraph;
1085struct EQPGraph {
1086  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1087  EQPGraphRow *pLast;   /* Last element of the pRow list */
1088  char zPrefix[100];    /* Graph prefix */
1089};
1090
1091/*
1092** State information about the database connection is contained in an
1093** instance of the following structure.
1094*/
1095typedef struct ShellState ShellState;
1096struct ShellState {
1097  sqlite3 *db;           /* The database */
1098  u8 autoExplain;        /* Automatically turn on .explain mode */
1099  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1100  u8 autoEQPtest;        /* autoEQP is in test mode */
1101  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1102  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1103  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1104  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1105  u8 nEqpLevel;          /* Depth of the EQP output graph */
1106  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1107  unsigned statsOn;      /* True to display memory stats before each finalize */
1108  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1109  int outCount;          /* Revert to stdout when reaching zero */
1110  int cnt;               /* Number of records displayed so far */
1111  int lineno;            /* Line number of last line read from in */
1112  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1113  FILE *in;              /* Read commands from this stream */
1114  FILE *out;             /* Write results here */
1115  FILE *traceOut;        /* Output for sqlite3_trace() */
1116  int nErr;              /* Number of errors seen */
1117  int mode;              /* An output mode setting */
1118  int modePrior;         /* Saved mode */
1119  int cMode;             /* temporary output mode for the current query */
1120  int normalMode;        /* Output mode before ".explain on" */
1121  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1122  int showHeader;        /* True to show column names in List or Column mode */
1123  int nCheck;            /* Number of ".check" commands run */
1124  unsigned nProgress;    /* Number of progress callbacks encountered */
1125  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1126  unsigned flgProgress;  /* Flags for the progress callback */
1127  unsigned shellFlgs;    /* Various flags */
1128  unsigned priorShFlgs;  /* Saved copy of flags */
1129  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1130  char *zDestTable;      /* Name of destination table when MODE_Insert */
1131  char *zTempFile;       /* Temporary file that might need deleting */
1132  char zTestcase[30];    /* Name of current test case */
1133  char colSeparator[20]; /* Column separator character for several modes */
1134  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1135  char colSepPrior[20];  /* Saved column separator */
1136  char rowSepPrior[20];  /* Saved row separator */
1137  int *colWidth;         /* Requested width of each column in columnar modes */
1138  int *actualWidth;      /* Actual width of each column */
1139  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1140  char nullValue[20];    /* The text to print when a NULL comes back from
1141                         ** the database */
1142  char outfile[FILENAME_MAX]; /* Filename for *out */
1143  const char *zDbFilename;    /* name of the database file */
1144  char *zFreeOnClose;         /* Filename to free when closing */
1145  const char *zVfs;           /* Name of VFS to use */
1146  sqlite3_stmt *pStmt;   /* Current statement if any. */
1147  FILE *pLog;            /* Write log output here */
1148  int *aiIndent;         /* Array of indents used in MODE_Explain */
1149  int nIndent;           /* Size of array aiIndent[] */
1150  int iIndent;           /* Index of current op in aiIndent[] */
1151  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1152#if defined(SQLITE_ENABLE_SESSION)
1153  int nSession;             /* Number of active sessions */
1154  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1155#endif
1156  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1157};
1158
1159
1160/* Allowed values for ShellState.autoEQP
1161*/
1162#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1163#define AUTOEQP_on       1           /* Automatic EQP is on */
1164#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1165#define AUTOEQP_full     3           /* Show full EXPLAIN */
1166
1167/* Allowed values for ShellState.openMode
1168*/
1169#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1170#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1171#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1172#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1173#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1174#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1175#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1176
1177/* Allowed values for ShellState.eTraceType
1178*/
1179#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1180#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1181#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1182
1183/* Bits in the ShellState.flgProgress variable */
1184#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1185#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1186                                   ** callback limit is reached, and for each
1187                                   ** top-level SQL statement */
1188#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1189
1190/*
1191** These are the allowed shellFlgs values
1192*/
1193#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1194#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1195#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1196#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1197#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1198#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1199#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1200#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
1201#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
1202#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
1203
1204/*
1205** Macros for testing and setting shellFlgs
1206*/
1207#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1208#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1209#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1210
1211/*
1212** These are the allowed modes.
1213*/
1214#define MODE_Line     0  /* One column per line.  Blank line between records */
1215#define MODE_Column   1  /* One record per line in neat columns */
1216#define MODE_List     2  /* One record per line with a separator */
1217#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1218#define MODE_Html     4  /* Generate an XHTML table */
1219#define MODE_Insert   5  /* Generate SQL "insert" statements */
1220#define MODE_Quote    6  /* Quote values as for SQL */
1221#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1222#define MODE_Csv      8  /* Quote strings, numbers are plain */
1223#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1224#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1225#define MODE_Pretty  11  /* Pretty-print schemas */
1226#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1227#define MODE_Json    13  /* Output JSON */
1228#define MODE_Markdown 14 /* Markdown formatting */
1229#define MODE_Table   15  /* MySQL-style table formatting */
1230#define MODE_Box     16  /* Unicode box-drawing characters */
1231
1232static const char *modeDescr[] = {
1233  "line",
1234  "column",
1235  "list",
1236  "semi",
1237  "html",
1238  "insert",
1239  "quote",
1240  "tcl",
1241  "csv",
1242  "explain",
1243  "ascii",
1244  "prettyprint",
1245  "eqp",
1246  "json",
1247  "markdown",
1248  "table",
1249  "box"
1250};
1251
1252/*
1253** These are the column/row/line separators used by the various
1254** import/export modes.
1255*/
1256#define SEP_Column    "|"
1257#define SEP_Row       "\n"
1258#define SEP_Tab       "\t"
1259#define SEP_Space     " "
1260#define SEP_Comma     ","
1261#define SEP_CrLf      "\r\n"
1262#define SEP_Unit      "\x1F"
1263#define SEP_Record    "\x1E"
1264
1265/*
1266** A callback for the sqlite3_log() interface.
1267*/
1268static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1269  ShellState *p = (ShellState*)pArg;
1270  if( p->pLog==0 ) return;
1271  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1272  fflush(p->pLog);
1273}
1274
1275/*
1276** SQL function:  shell_putsnl(X)
1277**
1278** Write the text X to the screen (or whatever output is being directed)
1279** adding a newline at the end, and then return X.
1280*/
1281static void shellPutsFunc(
1282  sqlite3_context *pCtx,
1283  int nVal,
1284  sqlite3_value **apVal
1285){
1286  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1287  (void)nVal;
1288  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1289  sqlite3_result_value(pCtx, apVal[0]);
1290}
1291
1292/*
1293** SQL function:   edit(VALUE)
1294**                 edit(VALUE,EDITOR)
1295**
1296** These steps:
1297**
1298**     (1) Write VALUE into a temporary file.
1299**     (2) Run program EDITOR on that temporary file.
1300**     (3) Read the temporary file back and return its content as the result.
1301**     (4) Delete the temporary file
1302**
1303** If the EDITOR argument is omitted, use the value in the VISUAL
1304** environment variable.  If still there is no EDITOR, through an error.
1305**
1306** Also throw an error if the EDITOR program returns a non-zero exit code.
1307*/
1308#ifndef SQLITE_NOHAVE_SYSTEM
1309static void editFunc(
1310  sqlite3_context *context,
1311  int argc,
1312  sqlite3_value **argv
1313){
1314  const char *zEditor;
1315  char *zTempFile = 0;
1316  sqlite3 *db;
1317  char *zCmd = 0;
1318  int bBin;
1319  int rc;
1320  int hasCRNL = 0;
1321  FILE *f = 0;
1322  sqlite3_int64 sz;
1323  sqlite3_int64 x;
1324  unsigned char *p = 0;
1325
1326  if( argc==2 ){
1327    zEditor = (const char*)sqlite3_value_text(argv[1]);
1328  }else{
1329    zEditor = getenv("VISUAL");
1330  }
1331  if( zEditor==0 ){
1332    sqlite3_result_error(context, "no editor for edit()", -1);
1333    return;
1334  }
1335  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1336    sqlite3_result_error(context, "NULL input to edit()", -1);
1337    return;
1338  }
1339  db = sqlite3_context_db_handle(context);
1340  zTempFile = 0;
1341  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1342  if( zTempFile==0 ){
1343    sqlite3_uint64 r = 0;
1344    sqlite3_randomness(sizeof(r), &r);
1345    zTempFile = sqlite3_mprintf("temp%llx", r);
1346    if( zTempFile==0 ){
1347      sqlite3_result_error_nomem(context);
1348      return;
1349    }
1350  }
1351  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1352  /* When writing the file to be edited, do \n to \r\n conversions on systems
1353  ** that want \r\n line endings */
1354  f = fopen(zTempFile, bBin ? "wb" : "w");
1355  if( f==0 ){
1356    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1357    goto edit_func_end;
1358  }
1359  sz = sqlite3_value_bytes(argv[0]);
1360  if( bBin ){
1361    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1362  }else{
1363    const char *z = (const char*)sqlite3_value_text(argv[0]);
1364    /* Remember whether or not the value originally contained \r\n */
1365    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1366    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1367  }
1368  fclose(f);
1369  f = 0;
1370  if( x!=sz ){
1371    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1372    goto edit_func_end;
1373  }
1374  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1375  if( zCmd==0 ){
1376    sqlite3_result_error_nomem(context);
1377    goto edit_func_end;
1378  }
1379  rc = system(zCmd);
1380  sqlite3_free(zCmd);
1381  if( rc ){
1382    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1383    goto edit_func_end;
1384  }
1385  f = fopen(zTempFile, "rb");
1386  if( f==0 ){
1387    sqlite3_result_error(context,
1388      "edit() cannot reopen temp file after edit", -1);
1389    goto edit_func_end;
1390  }
1391  fseek(f, 0, SEEK_END);
1392  sz = ftell(f);
1393  rewind(f);
1394  p = sqlite3_malloc64( sz+1 );
1395  if( p==0 ){
1396    sqlite3_result_error_nomem(context);
1397    goto edit_func_end;
1398  }
1399  x = fread(p, 1, (size_t)sz, f);
1400  fclose(f);
1401  f = 0;
1402  if( x!=sz ){
1403    sqlite3_result_error(context, "could not read back the whole file", -1);
1404    goto edit_func_end;
1405  }
1406  if( bBin ){
1407    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1408  }else{
1409    sqlite3_int64 i, j;
1410    if( hasCRNL ){
1411      /* If the original contains \r\n then do no conversions back to \n */
1412      j = sz;
1413    }else{
1414      /* If the file did not originally contain \r\n then convert any new
1415      ** \r\n back into \n */
1416      for(i=j=0; i<sz; i++){
1417        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1418        p[j++] = p[i];
1419      }
1420      sz = j;
1421      p[sz] = 0;
1422    }
1423    sqlite3_result_text64(context, (const char*)p, sz,
1424                          sqlite3_free, SQLITE_UTF8);
1425  }
1426  p = 0;
1427
1428edit_func_end:
1429  if( f ) fclose(f);
1430  unlink(zTempFile);
1431  sqlite3_free(zTempFile);
1432  sqlite3_free(p);
1433}
1434#endif /* SQLITE_NOHAVE_SYSTEM */
1435
1436/*
1437** Save or restore the current output mode
1438*/
1439static void outputModePush(ShellState *p){
1440  p->modePrior = p->mode;
1441  p->priorShFlgs = p->shellFlgs;
1442  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1443  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1444}
1445static void outputModePop(ShellState *p){
1446  p->mode = p->modePrior;
1447  p->shellFlgs = p->priorShFlgs;
1448  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1449  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1450}
1451
1452/*
1453** Output the given string as a hex-encoded blob (eg. X'1234' )
1454*/
1455static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1456  int i;
1457  char *zBlob = (char *)pBlob;
1458  raw_printf(out,"X'");
1459  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1460  raw_printf(out,"'");
1461}
1462
1463/*
1464** Find a string that is not found anywhere in z[].  Return a pointer
1465** to that string.
1466**
1467** Try to use zA and zB first.  If both of those are already found in z[]
1468** then make up some string and store it in the buffer zBuf.
1469*/
1470static const char *unused_string(
1471  const char *z,                    /* Result must not appear anywhere in z */
1472  const char *zA, const char *zB,   /* Try these first */
1473  char *zBuf                        /* Space to store a generated string */
1474){
1475  unsigned i = 0;
1476  if( strstr(z, zA)==0 ) return zA;
1477  if( strstr(z, zB)==0 ) return zB;
1478  do{
1479    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1480  }while( strstr(z,zBuf)!=0 );
1481  return zBuf;
1482}
1483
1484/*
1485** Output the given string as a quoted string using SQL quoting conventions.
1486**
1487** See also: output_quoted_escaped_string()
1488*/
1489static void output_quoted_string(FILE *out, const char *z){
1490  int i;
1491  char c;
1492  setBinaryMode(out, 1);
1493  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1494  if( c==0 ){
1495    utf8_printf(out,"'%s'",z);
1496  }else{
1497    raw_printf(out, "'");
1498    while( *z ){
1499      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1500      if( c=='\'' ) i++;
1501      if( i ){
1502        utf8_printf(out, "%.*s", i, z);
1503        z += i;
1504      }
1505      if( c=='\'' ){
1506        raw_printf(out, "'");
1507        continue;
1508      }
1509      if( c==0 ){
1510        break;
1511      }
1512      z++;
1513    }
1514    raw_printf(out, "'");
1515  }
1516  setTextMode(out, 1);
1517}
1518
1519/*
1520** Output the given string as a quoted string using SQL quoting conventions.
1521** Additionallly , escape the "\n" and "\r" characters so that they do not
1522** get corrupted by end-of-line translation facilities in some operating
1523** systems.
1524**
1525** This is like output_quoted_string() but with the addition of the \r\n
1526** escape mechanism.
1527*/
1528static void output_quoted_escaped_string(FILE *out, const char *z){
1529  int i;
1530  char c;
1531  setBinaryMode(out, 1);
1532  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1533  if( c==0 ){
1534    utf8_printf(out,"'%s'",z);
1535  }else{
1536    const char *zNL = 0;
1537    const char *zCR = 0;
1538    int nNL = 0;
1539    int nCR = 0;
1540    char zBuf1[20], zBuf2[20];
1541    for(i=0; z[i]; i++){
1542      if( z[i]=='\n' ) nNL++;
1543      if( z[i]=='\r' ) nCR++;
1544    }
1545    if( nNL ){
1546      raw_printf(out, "replace(");
1547      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1548    }
1549    if( nCR ){
1550      raw_printf(out, "replace(");
1551      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1552    }
1553    raw_printf(out, "'");
1554    while( *z ){
1555      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1556      if( c=='\'' ) i++;
1557      if( i ){
1558        utf8_printf(out, "%.*s", i, z);
1559        z += i;
1560      }
1561      if( c=='\'' ){
1562        raw_printf(out, "'");
1563        continue;
1564      }
1565      if( c==0 ){
1566        break;
1567      }
1568      z++;
1569      if( c=='\n' ){
1570        raw_printf(out, "%s", zNL);
1571        continue;
1572      }
1573      raw_printf(out, "%s", zCR);
1574    }
1575    raw_printf(out, "'");
1576    if( nCR ){
1577      raw_printf(out, ",'%s',char(13))", zCR);
1578    }
1579    if( nNL ){
1580      raw_printf(out, ",'%s',char(10))", zNL);
1581    }
1582  }
1583  setTextMode(out, 1);
1584}
1585
1586/*
1587** Output the given string as a quoted according to C or TCL quoting rules.
1588*/
1589static void output_c_string(FILE *out, const char *z){
1590  unsigned int c;
1591  fputc('"', out);
1592  while( (c = *(z++))!=0 ){
1593    if( c=='\\' ){
1594      fputc(c, out);
1595      fputc(c, out);
1596    }else if( c=='"' ){
1597      fputc('\\', out);
1598      fputc('"', out);
1599    }else if( c=='\t' ){
1600      fputc('\\', out);
1601      fputc('t', out);
1602    }else if( c=='\n' ){
1603      fputc('\\', out);
1604      fputc('n', out);
1605    }else if( c=='\r' ){
1606      fputc('\\', out);
1607      fputc('r', out);
1608    }else if( !isprint(c&0xff) ){
1609      raw_printf(out, "\\%03o", c&0xff);
1610    }else{
1611      fputc(c, out);
1612    }
1613  }
1614  fputc('"', out);
1615}
1616
1617/*
1618** Output the given string as a quoted according to JSON quoting rules.
1619*/
1620static void output_json_string(FILE *out, const char *z, int n){
1621  unsigned int c;
1622  if( n<0 ) n = (int)strlen(z);
1623  fputc('"', out);
1624  while( n-- ){
1625    c = *(z++);
1626    if( c=='\\' || c=='"' ){
1627      fputc('\\', out);
1628      fputc(c, out);
1629    }else if( c<=0x1f ){
1630      fputc('\\', out);
1631      if( c=='\b' ){
1632        fputc('b', out);
1633      }else if( c=='\f' ){
1634        fputc('f', out);
1635      }else if( c=='\n' ){
1636        fputc('n', out);
1637      }else if( c=='\r' ){
1638        fputc('r', out);
1639      }else if( c=='\t' ){
1640        fputc('t', out);
1641      }else{
1642         raw_printf(out, "u%04x",c);
1643      }
1644    }else{
1645      fputc(c, out);
1646    }
1647  }
1648  fputc('"', out);
1649}
1650
1651/*
1652** Output the given string with characters that are special to
1653** HTML escaped.
1654*/
1655static void output_html_string(FILE *out, const char *z){
1656  int i;
1657  if( z==0 ) z = "";
1658  while( *z ){
1659    for(i=0;   z[i]
1660            && z[i]!='<'
1661            && z[i]!='&'
1662            && z[i]!='>'
1663            && z[i]!='\"'
1664            && z[i]!='\'';
1665        i++){}
1666    if( i>0 ){
1667      utf8_printf(out,"%.*s",i,z);
1668    }
1669    if( z[i]=='<' ){
1670      raw_printf(out,"&lt;");
1671    }else if( z[i]=='&' ){
1672      raw_printf(out,"&amp;");
1673    }else if( z[i]=='>' ){
1674      raw_printf(out,"&gt;");
1675    }else if( z[i]=='\"' ){
1676      raw_printf(out,"&quot;");
1677    }else if( z[i]=='\'' ){
1678      raw_printf(out,"&#39;");
1679    }else{
1680      break;
1681    }
1682    z += i + 1;
1683  }
1684}
1685
1686/*
1687** If a field contains any character identified by a 1 in the following
1688** array, then the string must be quoted for CSV.
1689*/
1690static const char needCsvQuote[] = {
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  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1694  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1695  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1696  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1697  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1698  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1699  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1700  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1701  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1702  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1703  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1704  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1705  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1706  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1707};
1708
1709/*
1710** Output a single term of CSV.  Actually, p->colSeparator is used for
1711** the separator, which may or may not be a comma.  p->nullValue is
1712** the null value.  Strings are quoted if necessary.  The separator
1713** is only issued if bSep is true.
1714*/
1715static void output_csv(ShellState *p, const char *z, int bSep){
1716  FILE *out = p->out;
1717  if( z==0 ){
1718    utf8_printf(out,"%s",p->nullValue);
1719  }else{
1720    int i;
1721    int nSep = strlen30(p->colSeparator);
1722    for(i=0; z[i]; i++){
1723      if( needCsvQuote[((unsigned char*)z)[i]]
1724         || (z[i]==p->colSeparator[0] &&
1725             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1726        i = 0;
1727        break;
1728      }
1729    }
1730    if( i==0 ){
1731      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1732      utf8_printf(out, "%s", zQuoted);
1733      sqlite3_free(zQuoted);
1734    }else{
1735      utf8_printf(out, "%s", z);
1736    }
1737  }
1738  if( bSep ){
1739    utf8_printf(p->out, "%s", p->colSeparator);
1740  }
1741}
1742
1743/*
1744** This routine runs when the user presses Ctrl-C
1745*/
1746static void interrupt_handler(int NotUsed){
1747  UNUSED_PARAMETER(NotUsed);
1748  seenInterrupt++;
1749  if( seenInterrupt>2 ) exit(1);
1750  if( globalDb ) sqlite3_interrupt(globalDb);
1751}
1752
1753#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1754/*
1755** This routine runs for console events (e.g. Ctrl-C) on Win32
1756*/
1757static BOOL WINAPI ConsoleCtrlHandler(
1758  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1759){
1760  if( dwCtrlType==CTRL_C_EVENT ){
1761    interrupt_handler(0);
1762    return TRUE;
1763  }
1764  return FALSE;
1765}
1766#endif
1767
1768#ifndef SQLITE_OMIT_AUTHORIZATION
1769/*
1770** When the ".auth ON" is set, the following authorizer callback is
1771** invoked.  It always returns SQLITE_OK.
1772*/
1773static int shellAuth(
1774  void *pClientData,
1775  int op,
1776  const char *zA1,
1777  const char *zA2,
1778  const char *zA3,
1779  const char *zA4
1780){
1781  ShellState *p = (ShellState*)pClientData;
1782  static const char *azAction[] = { 0,
1783     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1784     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1785     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1786     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1787     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1788     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1789     "PRAGMA",               "READ",                 "SELECT",
1790     "TRANSACTION",          "UPDATE",               "ATTACH",
1791     "DETACH",               "ALTER_TABLE",          "REINDEX",
1792     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1793     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1794  };
1795  int i;
1796  const char *az[4];
1797  az[0] = zA1;
1798  az[1] = zA2;
1799  az[2] = zA3;
1800  az[3] = zA4;
1801  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1802  for(i=0; i<4; i++){
1803    raw_printf(p->out, " ");
1804    if( az[i] ){
1805      output_c_string(p->out, az[i]);
1806    }else{
1807      raw_printf(p->out, "NULL");
1808    }
1809  }
1810  raw_printf(p->out, "\n");
1811  return SQLITE_OK;
1812}
1813#endif
1814
1815/*
1816** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1817**
1818** This routine converts some CREATE TABLE statements for shadow tables
1819** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1820*/
1821static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1822  if( z==0 ) return;
1823  if( zTail==0 ) return;
1824  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1825    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1826  }else{
1827    utf8_printf(out, "%s%s", z, zTail);
1828  }
1829}
1830static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1831  char c = z[n];
1832  z[n] = 0;
1833  printSchemaLine(out, z, zTail);
1834  z[n] = c;
1835}
1836
1837/*
1838** Return true if string z[] has nothing but whitespace and comments to the
1839** end of the first line.
1840*/
1841static int wsToEol(const char *z){
1842  int i;
1843  for(i=0; z[i]; i++){
1844    if( z[i]=='\n' ) return 1;
1845    if( IsSpace(z[i]) ) continue;
1846    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1847    return 0;
1848  }
1849  return 1;
1850}
1851
1852/*
1853** Add a new entry to the EXPLAIN QUERY PLAN data
1854*/
1855static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1856  EQPGraphRow *pNew;
1857  int nText = strlen30(zText);
1858  if( p->autoEQPtest ){
1859    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1860  }
1861  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1862  if( pNew==0 ) shell_out_of_memory();
1863  pNew->iEqpId = iEqpId;
1864  pNew->iParentId = p2;
1865  memcpy(pNew->zText, zText, nText+1);
1866  pNew->pNext = 0;
1867  if( p->sGraph.pLast ){
1868    p->sGraph.pLast->pNext = pNew;
1869  }else{
1870    p->sGraph.pRow = pNew;
1871  }
1872  p->sGraph.pLast = pNew;
1873}
1874
1875/*
1876** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1877** in p->sGraph.
1878*/
1879static void eqp_reset(ShellState *p){
1880  EQPGraphRow *pRow, *pNext;
1881  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1882    pNext = pRow->pNext;
1883    sqlite3_free(pRow);
1884  }
1885  memset(&p->sGraph, 0, sizeof(p->sGraph));
1886}
1887
1888/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1889** pOld, or return the first such line if pOld is NULL
1890*/
1891static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1892  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1893  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1894  return pRow;
1895}
1896
1897/* Render a single level of the graph that has iEqpId as its parent.  Called
1898** recursively to render sublevels.
1899*/
1900static void eqp_render_level(ShellState *p, int iEqpId){
1901  EQPGraphRow *pRow, *pNext;
1902  int n = strlen30(p->sGraph.zPrefix);
1903  char *z;
1904  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1905    pNext = eqp_next_row(p, iEqpId, pRow);
1906    z = pRow->zText;
1907    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1908                pNext ? "|--" : "`--", z);
1909    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1910      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1911      eqp_render_level(p, pRow->iEqpId);
1912      p->sGraph.zPrefix[n] = 0;
1913    }
1914  }
1915}
1916
1917/*
1918** Display and reset the EXPLAIN QUERY PLAN data
1919*/
1920static void eqp_render(ShellState *p){
1921  EQPGraphRow *pRow = p->sGraph.pRow;
1922  if( pRow ){
1923    if( pRow->zText[0]=='-' ){
1924      if( pRow->pNext==0 ){
1925        eqp_reset(p);
1926        return;
1927      }
1928      utf8_printf(p->out, "%s\n", pRow->zText+3);
1929      p->sGraph.pRow = pRow->pNext;
1930      sqlite3_free(pRow);
1931    }else{
1932      utf8_printf(p->out, "QUERY PLAN\n");
1933    }
1934    p->sGraph.zPrefix[0] = 0;
1935    eqp_render_level(p, 0);
1936    eqp_reset(p);
1937  }
1938}
1939
1940#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1941/*
1942** Progress handler callback.
1943*/
1944static int progress_handler(void *pClientData) {
1945  ShellState *p = (ShellState*)pClientData;
1946  p->nProgress++;
1947  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1948    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1949    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1950    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1951    return 1;
1952  }
1953  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1954    raw_printf(p->out, "Progress %u\n", p->nProgress);
1955  }
1956  return 0;
1957}
1958#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1959
1960/*
1961** Print N dashes
1962*/
1963static void print_dashes(FILE *out, int N){
1964  const char zDash[] = "--------------------------------------------------";
1965  const int nDash = sizeof(zDash) - 1;
1966  while( N>nDash ){
1967    fputs(zDash, out);
1968    N -= nDash;
1969  }
1970  raw_printf(out, "%.*s", N, zDash);
1971}
1972
1973/*
1974** Print a markdown or table-style row separator using ascii-art
1975*/
1976static void print_row_separator(
1977  ShellState *p,
1978  int nArg,
1979  const char *zSep
1980){
1981  int i;
1982  if( nArg>0 ){
1983    fputs(zSep, p->out);
1984    print_dashes(p->out, p->actualWidth[0]+2);
1985    for(i=1; i<nArg; i++){
1986      fputs(zSep, p->out);
1987      print_dashes(p->out, p->actualWidth[i]+2);
1988    }
1989    fputs(zSep, p->out);
1990  }
1991  fputs("\n", p->out);
1992}
1993
1994/*
1995** This is the callback routine that the shell
1996** invokes for each row of a query result.
1997*/
1998static int shell_callback(
1999  void *pArg,
2000  int nArg,        /* Number of result columns */
2001  char **azArg,    /* Text of each result column */
2002  char **azCol,    /* Column names */
2003  int *aiType      /* Column types.  Might be NULL */
2004){
2005  int i;
2006  ShellState *p = (ShellState*)pArg;
2007
2008  if( azArg==0 ) return 0;
2009  switch( p->cMode ){
2010    case MODE_Line: {
2011      int w = 5;
2012      if( azArg==0 ) break;
2013      for(i=0; i<nArg; i++){
2014        int len = strlen30(azCol[i] ? azCol[i] : "");
2015        if( len>w ) w = len;
2016      }
2017      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2018      for(i=0; i<nArg; i++){
2019        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2020                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2021      }
2022      break;
2023    }
2024    case MODE_Explain: {
2025      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2026      if( nArg>ArraySize(aExplainWidth) ){
2027        nArg = ArraySize(aExplainWidth);
2028      }
2029      if( p->cnt++==0 ){
2030        for(i=0; i<nArg; i++){
2031          int w = aExplainWidth[i];
2032          utf8_width_print(p->out, w, azCol[i]);
2033          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2034        }
2035        for(i=0; i<nArg; i++){
2036          int w = aExplainWidth[i];
2037          print_dashes(p->out, w);
2038          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2039        }
2040      }
2041      if( azArg==0 ) break;
2042      for(i=0; i<nArg; i++){
2043        int w = aExplainWidth[i];
2044        if( i==nArg-1 ) w = 0;
2045        if( azArg[i] && strlenChar(azArg[i])>w ){
2046          w = strlenChar(azArg[i]);
2047        }
2048        if( i==1 && p->aiIndent && p->pStmt ){
2049          if( p->iIndent<p->nIndent ){
2050            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2051          }
2052          p->iIndent++;
2053        }
2054        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2055        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2056      }
2057      break;
2058    }
2059    case MODE_Semi: {   /* .schema and .fullschema output */
2060      printSchemaLine(p->out, azArg[0], ";\n");
2061      break;
2062    }
2063    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2064      char *z;
2065      int j;
2066      int nParen = 0;
2067      char cEnd = 0;
2068      char c;
2069      int nLine = 0;
2070      assert( nArg==1 );
2071      if( azArg[0]==0 ) break;
2072      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2073       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2074      ){
2075        utf8_printf(p->out, "%s;\n", azArg[0]);
2076        break;
2077      }
2078      z = sqlite3_mprintf("%s", azArg[0]);
2079      j = 0;
2080      for(i=0; IsSpace(z[i]); i++){}
2081      for(; (c = z[i])!=0; i++){
2082        if( IsSpace(c) ){
2083          if( z[j-1]=='\r' ) z[j-1] = '\n';
2084          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2085        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2086          j--;
2087        }
2088        z[j++] = c;
2089      }
2090      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2091      z[j] = 0;
2092      if( strlen30(z)>=79 ){
2093        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2094          if( c==cEnd ){
2095            cEnd = 0;
2096          }else if( c=='"' || c=='\'' || c=='`' ){
2097            cEnd = c;
2098          }else if( c=='[' ){
2099            cEnd = ']';
2100          }else if( c=='-' && z[i+1]=='-' ){
2101            cEnd = '\n';
2102          }else if( c=='(' ){
2103            nParen++;
2104          }else if( c==')' ){
2105            nParen--;
2106            if( nLine>0 && nParen==0 && j>0 ){
2107              printSchemaLineN(p->out, z, j, "\n");
2108              j = 0;
2109            }
2110          }
2111          z[j++] = c;
2112          if( nParen==1 && cEnd==0
2113           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2114          ){
2115            if( c=='\n' ) j--;
2116            printSchemaLineN(p->out, z, j, "\n  ");
2117            j = 0;
2118            nLine++;
2119            while( IsSpace(z[i+1]) ){ i++; }
2120          }
2121        }
2122        z[j] = 0;
2123      }
2124      printSchemaLine(p->out, z, ";\n");
2125      sqlite3_free(z);
2126      break;
2127    }
2128    case MODE_List: {
2129      if( p->cnt++==0 && p->showHeader ){
2130        for(i=0; i<nArg; i++){
2131          utf8_printf(p->out,"%s%s",azCol[i],
2132                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2133        }
2134      }
2135      if( azArg==0 ) break;
2136      for(i=0; i<nArg; i++){
2137        char *z = azArg[i];
2138        if( z==0 ) z = p->nullValue;
2139        utf8_printf(p->out, "%s", z);
2140        if( i<nArg-1 ){
2141          utf8_printf(p->out, "%s", p->colSeparator);
2142        }else{
2143          utf8_printf(p->out, "%s", p->rowSeparator);
2144        }
2145      }
2146      break;
2147    }
2148    case MODE_Html: {
2149      if( p->cnt++==0 && p->showHeader ){
2150        raw_printf(p->out,"<TR>");
2151        for(i=0; i<nArg; i++){
2152          raw_printf(p->out,"<TH>");
2153          output_html_string(p->out, azCol[i]);
2154          raw_printf(p->out,"</TH>\n");
2155        }
2156        raw_printf(p->out,"</TR>\n");
2157      }
2158      if( azArg==0 ) break;
2159      raw_printf(p->out,"<TR>");
2160      for(i=0; i<nArg; i++){
2161        raw_printf(p->out,"<TD>");
2162        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2163        raw_printf(p->out,"</TD>\n");
2164      }
2165      raw_printf(p->out,"</TR>\n");
2166      break;
2167    }
2168    case MODE_Tcl: {
2169      if( p->cnt++==0 && p->showHeader ){
2170        for(i=0; i<nArg; i++){
2171          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2172          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2173        }
2174        utf8_printf(p->out, "%s", p->rowSeparator);
2175      }
2176      if( azArg==0 ) break;
2177      for(i=0; i<nArg; i++){
2178        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2179        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2180      }
2181      utf8_printf(p->out, "%s", p->rowSeparator);
2182      break;
2183    }
2184    case MODE_Csv: {
2185      setBinaryMode(p->out, 1);
2186      if( p->cnt++==0 && p->showHeader ){
2187        for(i=0; i<nArg; i++){
2188          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2189        }
2190        utf8_printf(p->out, "%s", p->rowSeparator);
2191      }
2192      if( nArg>0 ){
2193        for(i=0; i<nArg; i++){
2194          output_csv(p, azArg[i], i<nArg-1);
2195        }
2196        utf8_printf(p->out, "%s", p->rowSeparator);
2197      }
2198      setTextMode(p->out, 1);
2199      break;
2200    }
2201    case MODE_Insert: {
2202      if( azArg==0 ) break;
2203      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2204      if( p->showHeader ){
2205        raw_printf(p->out,"(");
2206        for(i=0; i<nArg; i++){
2207          if( i>0 ) raw_printf(p->out, ",");
2208          if( quoteChar(azCol[i]) ){
2209            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2210            utf8_printf(p->out, "%s", z);
2211            sqlite3_free(z);
2212          }else{
2213            raw_printf(p->out, "%s", azCol[i]);
2214          }
2215        }
2216        raw_printf(p->out,")");
2217      }
2218      p->cnt++;
2219      for(i=0; i<nArg; i++){
2220        raw_printf(p->out, i>0 ? "," : " VALUES(");
2221        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2222          utf8_printf(p->out,"NULL");
2223        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2224          if( ShellHasFlag(p, SHFLG_Newlines) ){
2225            output_quoted_string(p->out, azArg[i]);
2226          }else{
2227            output_quoted_escaped_string(p->out, azArg[i]);
2228          }
2229        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2230          utf8_printf(p->out,"%s", azArg[i]);
2231        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2232          char z[50];
2233          double r = sqlite3_column_double(p->pStmt, i);
2234          sqlite3_uint64 ur;
2235          memcpy(&ur,&r,sizeof(r));
2236          if( ur==0x7ff0000000000000LL ){
2237            raw_printf(p->out, "1e999");
2238          }else if( ur==0xfff0000000000000LL ){
2239            raw_printf(p->out, "-1e999");
2240          }else{
2241            sqlite3_snprintf(50,z,"%!.20g", r);
2242            raw_printf(p->out, "%s", z);
2243          }
2244        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2245          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2246          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2247          output_hex_blob(p->out, pBlob, nBlob);
2248        }else if( isNumber(azArg[i], 0) ){
2249          utf8_printf(p->out,"%s", azArg[i]);
2250        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2251          output_quoted_string(p->out, azArg[i]);
2252        }else{
2253          output_quoted_escaped_string(p->out, azArg[i]);
2254        }
2255      }
2256      raw_printf(p->out,");\n");
2257      break;
2258    }
2259    case MODE_Json: {
2260      if( azArg==0 ) break;
2261      if( p->cnt==0 ){
2262        fputs("[{", p->out);
2263      }else{
2264        fputs(",\n{", p->out);
2265      }
2266      p->cnt++;
2267      for(i=0; i<nArg; i++){
2268        output_json_string(p->out, azCol[i], -1);
2269        putc(':', p->out);
2270        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2271          fputs("null",p->out);
2272        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2273          char z[50];
2274          double r = sqlite3_column_double(p->pStmt, i);
2275          sqlite3_uint64 ur;
2276          memcpy(&ur,&r,sizeof(r));
2277          if( ur==0x7ff0000000000000LL ){
2278            raw_printf(p->out, "1e999");
2279          }else if( ur==0xfff0000000000000LL ){
2280            raw_printf(p->out, "-1e999");
2281          }else{
2282            sqlite3_snprintf(50,z,"%!.20g", r);
2283            raw_printf(p->out, "%s", z);
2284          }
2285        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2286          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2287          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2288          output_json_string(p->out, pBlob, nBlob);
2289        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2290          output_json_string(p->out, azArg[i], -1);
2291        }else{
2292          utf8_printf(p->out,"%s", azArg[i]);
2293        }
2294        if( i<nArg-1 ){
2295          putc(',', p->out);
2296        }
2297      }
2298      putc('}', p->out);
2299      break;
2300    }
2301    case MODE_Quote: {
2302      if( azArg==0 ) break;
2303      if( p->cnt==0 && p->showHeader ){
2304        for(i=0; i<nArg; i++){
2305          if( i>0 ) fputs(p->colSeparator, p->out);
2306          output_quoted_string(p->out, azCol[i]);
2307        }
2308        fputs(p->rowSeparator, p->out);
2309      }
2310      p->cnt++;
2311      for(i=0; i<nArg; i++){
2312        if( i>0 ) fputs(p->colSeparator, p->out);
2313        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2314          utf8_printf(p->out,"NULL");
2315        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2316          output_quoted_string(p->out, azArg[i]);
2317        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2318          utf8_printf(p->out,"%s", azArg[i]);
2319        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2320          char z[50];
2321          double r = sqlite3_column_double(p->pStmt, i);
2322          sqlite3_snprintf(50,z,"%!.20g", r);
2323          raw_printf(p->out, "%s", z);
2324        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2325          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2326          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2327          output_hex_blob(p->out, pBlob, nBlob);
2328        }else if( isNumber(azArg[i], 0) ){
2329          utf8_printf(p->out,"%s", azArg[i]);
2330        }else{
2331          output_quoted_string(p->out, azArg[i]);
2332        }
2333      }
2334      fputs(p->rowSeparator, p->out);
2335      break;
2336    }
2337    case MODE_Ascii: {
2338      if( p->cnt++==0 && p->showHeader ){
2339        for(i=0; i<nArg; i++){
2340          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2341          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2342        }
2343        utf8_printf(p->out, "%s", p->rowSeparator);
2344      }
2345      if( azArg==0 ) break;
2346      for(i=0; i<nArg; i++){
2347        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2348        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2349      }
2350      utf8_printf(p->out, "%s", p->rowSeparator);
2351      break;
2352    }
2353    case MODE_EQP: {
2354      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2355      break;
2356    }
2357  }
2358  return 0;
2359}
2360
2361/*
2362** This is the callback routine that the SQLite library
2363** invokes for each row of a query result.
2364*/
2365static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2366  /* since we don't have type info, call the shell_callback with a NULL value */
2367  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2368}
2369
2370/*
2371** This is the callback routine from sqlite3_exec() that appends all
2372** output onto the end of a ShellText object.
2373*/
2374static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2375  ShellText *p = (ShellText*)pArg;
2376  int i;
2377  UNUSED_PARAMETER(az);
2378  if( azArg==0 ) return 0;
2379  if( p->n ) appendText(p, "|", 0);
2380  for(i=0; i<nArg; i++){
2381    if( i ) appendText(p, ",", 0);
2382    if( azArg[i] ) appendText(p, azArg[i], 0);
2383  }
2384  return 0;
2385}
2386
2387/*
2388** Generate an appropriate SELFTEST table in the main database.
2389*/
2390static void createSelftestTable(ShellState *p){
2391  char *zErrMsg = 0;
2392  sqlite3_exec(p->db,
2393    "SAVEPOINT selftest_init;\n"
2394    "CREATE TABLE IF NOT EXISTS selftest(\n"
2395    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2396    "  op TEXT,\n"                   /* Operator:  memo run */
2397    "  cmd TEXT,\n"                  /* Command text */
2398    "  ans TEXT\n"                   /* Desired answer */
2399    ");"
2400    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2401    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2402    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2403    "         'memo','Tests generated by --init');\n"
2404    "INSERT INTO [_shell$self]\n"
2405    "  SELECT 'run',\n"
2406    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2407                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2408    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2409                          "FROM sqlite_schema ORDER BY 2',224));\n"
2410    "INSERT INTO [_shell$self]\n"
2411    "  SELECT 'run',"
2412    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2413    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2414    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2415    "  FROM (\n"
2416    "    SELECT name FROM sqlite_schema\n"
2417    "     WHERE type='table'\n"
2418    "       AND name<>'selftest'\n"
2419    "       AND coalesce(rootpage,0)>0\n"
2420    "  )\n"
2421    " ORDER BY name;\n"
2422    "INSERT INTO [_shell$self]\n"
2423    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2424    "INSERT INTO selftest(tno,op,cmd,ans)"
2425    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2426    "DROP TABLE [_shell$self];"
2427    ,0,0,&zErrMsg);
2428  if( zErrMsg ){
2429    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2430    sqlite3_free(zErrMsg);
2431  }
2432  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2433}
2434
2435
2436/*
2437** Set the destination table field of the ShellState structure to
2438** the name of the table given.  Escape any quote characters in the
2439** table name.
2440*/
2441static void set_table_name(ShellState *p, const char *zName){
2442  int i, n;
2443  char cQuote;
2444  char *z;
2445
2446  if( p->zDestTable ){
2447    free(p->zDestTable);
2448    p->zDestTable = 0;
2449  }
2450  if( zName==0 ) return;
2451  cQuote = quoteChar(zName);
2452  n = strlen30(zName);
2453  if( cQuote ) n += n+2;
2454  z = p->zDestTable = malloc( n+1 );
2455  if( z==0 ) shell_out_of_memory();
2456  n = 0;
2457  if( cQuote ) z[n++] = cQuote;
2458  for(i=0; zName[i]; i++){
2459    z[n++] = zName[i];
2460    if( zName[i]==cQuote ) z[n++] = cQuote;
2461  }
2462  if( cQuote ) z[n++] = cQuote;
2463  z[n] = 0;
2464}
2465
2466
2467/*
2468** Execute a query statement that will generate SQL output.  Print
2469** the result columns, comma-separated, on a line and then add a
2470** semicolon terminator to the end of that line.
2471**
2472** If the number of columns is 1 and that column contains text "--"
2473** then write the semicolon on a separate line.  That way, if a
2474** "--" comment occurs at the end of the statement, the comment
2475** won't consume the semicolon terminator.
2476*/
2477static int run_table_dump_query(
2478  ShellState *p,           /* Query context */
2479  const char *zSelect      /* SELECT statement to extract content */
2480){
2481  sqlite3_stmt *pSelect;
2482  int rc;
2483  int nResult;
2484  int i;
2485  const char *z;
2486  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2487  if( rc!=SQLITE_OK || !pSelect ){
2488    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2489                sqlite3_errmsg(p->db));
2490    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2491    return rc;
2492  }
2493  rc = sqlite3_step(pSelect);
2494  nResult = sqlite3_column_count(pSelect);
2495  while( rc==SQLITE_ROW ){
2496    z = (const char*)sqlite3_column_text(pSelect, 0);
2497    utf8_printf(p->out, "%s", z);
2498    for(i=1; i<nResult; i++){
2499      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2500    }
2501    if( z==0 ) z = "";
2502    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2503    if( z[0] ){
2504      raw_printf(p->out, "\n;\n");
2505    }else{
2506      raw_printf(p->out, ";\n");
2507    }
2508    rc = sqlite3_step(pSelect);
2509  }
2510  rc = sqlite3_finalize(pSelect);
2511  if( rc!=SQLITE_OK ){
2512    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2513                sqlite3_errmsg(p->db));
2514    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2515  }
2516  return rc;
2517}
2518
2519/*
2520** Allocate space and save off current error string.
2521*/
2522static char *save_err_msg(
2523  sqlite3 *db            /* Database to query */
2524){
2525  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2526  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2527  if( zErrMsg ){
2528    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2529  }
2530  return zErrMsg;
2531}
2532
2533#ifdef __linux__
2534/*
2535** Attempt to display I/O stats on Linux using /proc/PID/io
2536*/
2537static void displayLinuxIoStats(FILE *out){
2538  FILE *in;
2539  char z[200];
2540  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2541  in = fopen(z, "rb");
2542  if( in==0 ) return;
2543  while( fgets(z, sizeof(z), in)!=0 ){
2544    static const struct {
2545      const char *zPattern;
2546      const char *zDesc;
2547    } aTrans[] = {
2548      { "rchar: ",                  "Bytes received by read():" },
2549      { "wchar: ",                  "Bytes sent to write():"    },
2550      { "syscr: ",                  "Read() system calls:"      },
2551      { "syscw: ",                  "Write() system calls:"     },
2552      { "read_bytes: ",             "Bytes read from storage:"  },
2553      { "write_bytes: ",            "Bytes written to storage:" },
2554      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2555    };
2556    int i;
2557    for(i=0; i<ArraySize(aTrans); i++){
2558      int n = strlen30(aTrans[i].zPattern);
2559      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2560        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2561        break;
2562      }
2563    }
2564  }
2565  fclose(in);
2566}
2567#endif
2568
2569/*
2570** Display a single line of status using 64-bit values.
2571*/
2572static void displayStatLine(
2573  ShellState *p,            /* The shell context */
2574  char *zLabel,             /* Label for this one line */
2575  char *zFormat,            /* Format for the result */
2576  int iStatusCtrl,          /* Which status to display */
2577  int bReset                /* True to reset the stats */
2578){
2579  sqlite3_int64 iCur = -1;
2580  sqlite3_int64 iHiwtr = -1;
2581  int i, nPercent;
2582  char zLine[200];
2583  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2584  for(i=0, nPercent=0; zFormat[i]; i++){
2585    if( zFormat[i]=='%' ) nPercent++;
2586  }
2587  if( nPercent>1 ){
2588    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2589  }else{
2590    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2591  }
2592  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2593}
2594
2595/*
2596** Display memory stats.
2597*/
2598static int display_stats(
2599  sqlite3 *db,                /* Database to query */
2600  ShellState *pArg,           /* Pointer to ShellState */
2601  int bReset                  /* True to reset the stats */
2602){
2603  int iCur;
2604  int iHiwtr;
2605  FILE *out;
2606  if( pArg==0 || pArg->out==0 ) return 0;
2607  out = pArg->out;
2608
2609  if( pArg->pStmt && pArg->statsOn==2 ){
2610    int nCol, i, x;
2611    sqlite3_stmt *pStmt = pArg->pStmt;
2612    char z[100];
2613    nCol = sqlite3_column_count(pStmt);
2614    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2615    for(i=0; i<nCol; i++){
2616      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2617      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2618#ifndef SQLITE_OMIT_DECLTYPE
2619      sqlite3_snprintf(30, z+x, "declared type:");
2620      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2621#endif
2622#ifdef SQLITE_ENABLE_COLUMN_METADATA
2623      sqlite3_snprintf(30, z+x, "database name:");
2624      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2625      sqlite3_snprintf(30, z+x, "table name:");
2626      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2627      sqlite3_snprintf(30, z+x, "origin name:");
2628      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2629#endif
2630    }
2631  }
2632
2633  if( pArg->statsOn==3 ){
2634    if( pArg->pStmt ){
2635      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2636      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
2637    }
2638    return 0;
2639  }
2640
2641  displayStatLine(pArg, "Memory Used:",
2642     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2643  displayStatLine(pArg, "Number of Outstanding Allocations:",
2644     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2645  if( pArg->shellFlgs & SHFLG_Pagecache ){
2646    displayStatLine(pArg, "Number of Pcache Pages Used:",
2647       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2648  }
2649  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2650     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2651  displayStatLine(pArg, "Largest Allocation:",
2652     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2653  displayStatLine(pArg, "Largest Pcache Allocation:",
2654     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2655#ifdef YYTRACKMAXSTACKDEPTH
2656  displayStatLine(pArg, "Deepest Parser Stack:",
2657     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2658#endif
2659
2660  if( db ){
2661    if( pArg->shellFlgs & SHFLG_Lookaside ){
2662      iHiwtr = iCur = -1;
2663      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2664                        &iCur, &iHiwtr, bReset);
2665      raw_printf(pArg->out,
2666              "Lookaside Slots Used:                %d (max %d)\n",
2667              iCur, iHiwtr);
2668      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2669                        &iCur, &iHiwtr, bReset);
2670      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2671              iHiwtr);
2672      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2673                        &iCur, &iHiwtr, bReset);
2674      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2675              iHiwtr);
2676      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2677                        &iCur, &iHiwtr, bReset);
2678      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2679              iHiwtr);
2680    }
2681    iHiwtr = iCur = -1;
2682    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2683    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2684            iCur);
2685    iHiwtr = iCur = -1;
2686    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2687    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2688    iHiwtr = iCur = -1;
2689    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2690    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2691    iHiwtr = iCur = -1;
2692    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2693    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2694    iHiwtr = iCur = -1;
2695    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2696    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2697    iHiwtr = iCur = -1;
2698    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2699    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2700            iCur);
2701    iHiwtr = iCur = -1;
2702    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2703    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2704            iCur);
2705  }
2706
2707  if( pArg->pStmt ){
2708    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2709                               bReset);
2710    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2711    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2712    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2713    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2714    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2715    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2716    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2717    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2718    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2719    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2720    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2721    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2722    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2723  }
2724
2725#ifdef __linux__
2726  displayLinuxIoStats(pArg->out);
2727#endif
2728
2729  /* Do not remove this machine readable comment: extra-stats-output-here */
2730
2731  return 0;
2732}
2733
2734/*
2735** Display scan stats.
2736*/
2737static void display_scanstats(
2738  sqlite3 *db,                    /* Database to query */
2739  ShellState *pArg                /* Pointer to ShellState */
2740){
2741#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2742  UNUSED_PARAMETER(db);
2743  UNUSED_PARAMETER(pArg);
2744#else
2745  int i, k, n, mx;
2746  raw_printf(pArg->out, "-------- scanstats --------\n");
2747  mx = 0;
2748  for(k=0; k<=mx; k++){
2749    double rEstLoop = 1.0;
2750    for(i=n=0; 1; i++){
2751      sqlite3_stmt *p = pArg->pStmt;
2752      sqlite3_int64 nLoop, nVisit;
2753      double rEst;
2754      int iSid;
2755      const char *zExplain;
2756      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2757        break;
2758      }
2759      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2760      if( iSid>mx ) mx = iSid;
2761      if( iSid!=k ) continue;
2762      if( n==0 ){
2763        rEstLoop = (double)nLoop;
2764        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2765      }
2766      n++;
2767      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2768      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2769      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2770      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2771      rEstLoop *= rEst;
2772      raw_printf(pArg->out,
2773          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2774          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2775      );
2776    }
2777  }
2778  raw_printf(pArg->out, "---------------------------\n");
2779#endif
2780}
2781
2782/*
2783** Parameter azArray points to a zero-terminated array of strings. zStr
2784** points to a single nul-terminated string. Return non-zero if zStr
2785** is equal, according to strcmp(), to any of the strings in the array.
2786** Otherwise, return zero.
2787*/
2788static int str_in_array(const char *zStr, const char **azArray){
2789  int i;
2790  for(i=0; azArray[i]; i++){
2791    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2792  }
2793  return 0;
2794}
2795
2796/*
2797** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2798** and populate the ShellState.aiIndent[] array with the number of
2799** spaces each opcode should be indented before it is output.
2800**
2801** The indenting rules are:
2802**
2803**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2804**       all opcodes that occur between the p2 jump destination and the opcode
2805**       itself by 2 spaces.
2806**
2807**     * For each "Goto", if the jump destination is earlier in the program
2808**       and ends on one of:
2809**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2810**       or if the P1 parameter is one instead of zero,
2811**       then indent all opcodes between the earlier instruction
2812**       and "Goto" by 2 spaces.
2813*/
2814static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2815  const char *zSql;               /* The text of the SQL statement */
2816  const char *z;                  /* Used to check if this is an EXPLAIN */
2817  int *abYield = 0;               /* True if op is an OP_Yield */
2818  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2819  int iOp;                        /* Index of operation in p->aiIndent[] */
2820
2821  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2822  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2823                            "Rewind", 0 };
2824  const char *azGoto[] = { "Goto", 0 };
2825
2826  /* Try to figure out if this is really an EXPLAIN statement. If this
2827  ** cannot be verified, return early.  */
2828  if( sqlite3_column_count(pSql)!=8 ){
2829    p->cMode = p->mode;
2830    return;
2831  }
2832  zSql = sqlite3_sql(pSql);
2833  if( zSql==0 ) return;
2834  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2835  if( sqlite3_strnicmp(z, "explain", 7) ){
2836    p->cMode = p->mode;
2837    return;
2838  }
2839
2840  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2841    int i;
2842    int iAddr = sqlite3_column_int(pSql, 0);
2843    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2844
2845    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2846    ** p2 is an instruction address, set variable p2op to the index of that
2847    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2848    ** the current instruction is part of a sub-program generated by an
2849    ** SQL trigger or foreign key.  */
2850    int p2 = sqlite3_column_int(pSql, 3);
2851    int p2op = (p2 + (iOp-iAddr));
2852
2853    /* Grow the p->aiIndent array as required */
2854    if( iOp>=nAlloc ){
2855      if( iOp==0 ){
2856        /* Do further verfication that this is explain output.  Abort if
2857        ** it is not */
2858        static const char *explainCols[] = {
2859           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2860        int jj;
2861        for(jj=0; jj<ArraySize(explainCols); jj++){
2862          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2863            p->cMode = p->mode;
2864            sqlite3_reset(pSql);
2865            return;
2866          }
2867        }
2868      }
2869      nAlloc += 100;
2870      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2871      if( p->aiIndent==0 ) shell_out_of_memory();
2872      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2873      if( abYield==0 ) shell_out_of_memory();
2874    }
2875    abYield[iOp] = str_in_array(zOp, azYield);
2876    p->aiIndent[iOp] = 0;
2877    p->nIndent = iOp+1;
2878
2879    if( str_in_array(zOp, azNext) ){
2880      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2881    }
2882    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2883     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2884    ){
2885      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2886    }
2887  }
2888
2889  p->iIndent = 0;
2890  sqlite3_free(abYield);
2891  sqlite3_reset(pSql);
2892}
2893
2894/*
2895** Free the array allocated by explain_data_prepare().
2896*/
2897static void explain_data_delete(ShellState *p){
2898  sqlite3_free(p->aiIndent);
2899  p->aiIndent = 0;
2900  p->nIndent = 0;
2901  p->iIndent = 0;
2902}
2903
2904/*
2905** Disable and restore .wheretrace and .selecttrace settings.
2906*/
2907static unsigned int savedSelectTrace;
2908static unsigned int savedWhereTrace;
2909static void disable_debug_trace_modes(void){
2910  unsigned int zero = 0;
2911  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
2912  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
2913  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
2914  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
2915}
2916static void restore_debug_trace_modes(void){
2917  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
2918  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
2919}
2920
2921/* Create the TEMP table used to store parameter bindings */
2922static void bind_table_init(ShellState *p){
2923  int wrSchema = 0;
2924  int defensiveMode = 0;
2925  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2926  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2927  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2928  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2929  sqlite3_exec(p->db,
2930    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2931    "  key TEXT PRIMARY KEY,\n"
2932    "  value\n"
2933    ") WITHOUT ROWID;",
2934    0, 0, 0);
2935  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2936  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2937}
2938
2939/*
2940** Bind parameters on a prepared statement.
2941**
2942** Parameter bindings are taken from a TEMP table of the form:
2943**
2944**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2945**    WITHOUT ROWID;
2946**
2947** No bindings occur if this table does not exist.  The name of the table
2948** begins with "sqlite_" so that it will not collide with ordinary application
2949** tables.  The table must be in the TEMP schema.
2950*/
2951static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2952  int nVar;
2953  int i;
2954  int rc;
2955  sqlite3_stmt *pQ = 0;
2956
2957  nVar = sqlite3_bind_parameter_count(pStmt);
2958  if( nVar==0 ) return;  /* Nothing to do */
2959  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2960                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2961    return; /* Parameter table does not exist */
2962  }
2963  rc = sqlite3_prepare_v2(pArg->db,
2964          "SELECT value FROM temp.sqlite_parameters"
2965          " WHERE key=?1", -1, &pQ, 0);
2966  if( rc || pQ==0 ) return;
2967  for(i=1; i<=nVar; i++){
2968    char zNum[30];
2969    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2970    if( zVar==0 ){
2971      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2972      zVar = zNum;
2973    }
2974    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2975    if( sqlite3_step(pQ)==SQLITE_ROW ){
2976      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2977    }else{
2978      sqlite3_bind_null(pStmt, i);
2979    }
2980    sqlite3_reset(pQ);
2981  }
2982  sqlite3_finalize(pQ);
2983}
2984
2985/*
2986** UTF8 box-drawing characters.  Imagine box lines like this:
2987**
2988**           1
2989**           |
2990**       4 --+-- 2
2991**           |
2992**           3
2993**
2994** Each box characters has between 2 and 4 of the lines leading from
2995** the center.  The characters are here identified by the numbers of
2996** their corresponding lines.
2997*/
2998#define BOX_24   "\342\224\200"  /* U+2500 --- */
2999#define BOX_13   "\342\224\202"  /* U+2502  |  */
3000#define BOX_23   "\342\224\214"  /* U+250c  ,- */
3001#define BOX_34   "\342\224\220"  /* U+2510 -,  */
3002#define BOX_12   "\342\224\224"  /* U+2514  '- */
3003#define BOX_14   "\342\224\230"  /* U+2518 -'  */
3004#define BOX_123  "\342\224\234"  /* U+251c  |- */
3005#define BOX_134  "\342\224\244"  /* U+2524 -|  */
3006#define BOX_234  "\342\224\254"  /* U+252c -,- */
3007#define BOX_124  "\342\224\264"  /* U+2534 -'- */
3008#define BOX_1234 "\342\224\274"  /* U+253c -|- */
3009
3010/* Draw horizontal line N characters long using unicode box
3011** characters
3012*/
3013static void print_box_line(FILE *out, int N){
3014  const char zDash[] =
3015      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3016      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3017  const int nDash = sizeof(zDash) - 1;
3018  N *= 3;
3019  while( N>nDash ){
3020    utf8_printf(out, zDash);
3021    N -= nDash;
3022  }
3023  utf8_printf(out, "%.*s", N, zDash);
3024}
3025
3026/*
3027** Draw a horizontal separator for a MODE_Box table.
3028*/
3029static void print_box_row_separator(
3030  ShellState *p,
3031  int nArg,
3032  const char *zSep1,
3033  const char *zSep2,
3034  const char *zSep3
3035){
3036  int i;
3037  if( nArg>0 ){
3038    utf8_printf(p->out, "%s", zSep1);
3039    print_box_line(p->out, p->actualWidth[0]+2);
3040    for(i=1; i<nArg; i++){
3041      utf8_printf(p->out, "%s", zSep2);
3042      print_box_line(p->out, p->actualWidth[i]+2);
3043    }
3044    utf8_printf(p->out, "%s", zSep3);
3045  }
3046  fputs("\n", p->out);
3047}
3048
3049
3050
3051/*
3052** Run a prepared statement and output the result in one of the
3053** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3054** or MODE_Box.
3055**
3056** This is different from ordinary exec_prepared_stmt() in that
3057** it has to run the entire query and gather the results into memory
3058** first, in order to determine column widths, before providing
3059** any output.
3060*/
3061static void exec_prepared_stmt_columnar(
3062  ShellState *p,                        /* Pointer to ShellState */
3063  sqlite3_stmt *pStmt                   /* Statment to run */
3064){
3065  sqlite3_int64 nRow = 0;
3066  int nColumn = 0;
3067  char **azData = 0;
3068  sqlite3_int64 nAlloc = 0;
3069  const char *z;
3070  int rc;
3071  sqlite3_int64 i, nData;
3072  int j, nTotal, w, n;
3073  const char *colSep = 0;
3074  const char *rowSep = 0;
3075
3076  rc = sqlite3_step(pStmt);
3077  if( rc!=SQLITE_ROW ) return;
3078  nColumn = sqlite3_column_count(pStmt);
3079  nAlloc = nColumn*4;
3080  if( nAlloc<=0 ) nAlloc = 1;
3081  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3082  if( azData==0 ) shell_out_of_memory();
3083  for(i=0; i<nColumn; i++){
3084    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3085  }
3086  do{
3087    if( (nRow+2)*nColumn >= nAlloc ){
3088      nAlloc *= 2;
3089      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3090      if( azData==0 ) shell_out_of_memory();
3091    }
3092    nRow++;
3093    for(i=0; i<nColumn; i++){
3094      z = (const char*)sqlite3_column_text(pStmt,i);
3095      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3096    }
3097  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
3098  if( nColumn>p->nWidth ){
3099    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3100    if( p->colWidth==0 ) shell_out_of_memory();
3101    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3102    p->nWidth = nColumn;
3103    p->actualWidth = &p->colWidth[nColumn];
3104  }
3105  memset(p->actualWidth, 0, nColumn*sizeof(int));
3106  for(i=0; i<nColumn; i++){
3107    w = p->colWidth[i];
3108    if( w<0 ) w = -w;
3109    p->actualWidth[i] = w;
3110  }
3111  nTotal = nColumn*(nRow+1);
3112  for(i=0; i<nTotal; i++){
3113    z = azData[i];
3114    if( z==0 ) z = p->nullValue;
3115    n = strlenChar(z);
3116    j = i%nColumn;
3117    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3118  }
3119  if( seenInterrupt ) goto columnar_end;
3120  if( nColumn==0 ) goto columnar_end;
3121  switch( p->cMode ){
3122    case MODE_Column: {
3123      colSep = "  ";
3124      rowSep = "\n";
3125      if( p->showHeader ){
3126        for(i=0; i<nColumn; i++){
3127          w = p->actualWidth[i];
3128          if( p->colWidth[i]<0 ) w = -w;
3129          utf8_width_print(p->out, w, azData[i]);
3130          fputs(i==nColumn-1?"\n":"  ", p->out);
3131        }
3132        for(i=0; i<nColumn; i++){
3133          print_dashes(p->out, p->actualWidth[i]);
3134          fputs(i==nColumn-1?"\n":"  ", p->out);
3135        }
3136      }
3137      break;
3138    }
3139    case MODE_Table: {
3140      colSep = " | ";
3141      rowSep = " |\n";
3142      print_row_separator(p, nColumn, "+");
3143      fputs("| ", p->out);
3144      for(i=0; i<nColumn; i++){
3145        w = p->actualWidth[i];
3146        n = strlenChar(azData[i]);
3147        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3148        fputs(i==nColumn-1?" |\n":" | ", p->out);
3149      }
3150      print_row_separator(p, nColumn, "+");
3151      break;
3152    }
3153    case MODE_Markdown: {
3154      colSep = " | ";
3155      rowSep = " |\n";
3156      fputs("| ", p->out);
3157      for(i=0; i<nColumn; i++){
3158        w = p->actualWidth[i];
3159        n = strlenChar(azData[i]);
3160        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3161        fputs(i==nColumn-1?" |\n":" | ", p->out);
3162      }
3163      print_row_separator(p, nColumn, "|");
3164      break;
3165    }
3166    case MODE_Box: {
3167      colSep = " " BOX_13 " ";
3168      rowSep = " " BOX_13 "\n";
3169      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3170      utf8_printf(p->out, BOX_13 " ");
3171      for(i=0; i<nColumn; i++){
3172        w = p->actualWidth[i];
3173        n = strlenChar(azData[i]);
3174        utf8_printf(p->out, "%*s%s%*s%s",
3175            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3176            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3177      }
3178      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3179      break;
3180    }
3181  }
3182  for(i=nColumn, j=0; i<nTotal; i++, j++){
3183    if( j==0 && p->cMode!=MODE_Column ){
3184      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3185    }
3186    z = azData[i];
3187    if( z==0 ) z = p->nullValue;
3188    w = p->actualWidth[j];
3189    if( p->colWidth[j]<0 ) w = -w;
3190    utf8_width_print(p->out, w, z);
3191    if( j==nColumn-1 ){
3192      utf8_printf(p->out, "%s", rowSep);
3193      j = -1;
3194      if( seenInterrupt ) goto columnar_end;
3195    }else{
3196      utf8_printf(p->out, "%s", colSep);
3197    }
3198  }
3199  if( p->cMode==MODE_Table ){
3200    print_row_separator(p, nColumn, "+");
3201  }else if( p->cMode==MODE_Box ){
3202    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3203  }
3204columnar_end:
3205  if( seenInterrupt ){
3206    utf8_printf(p->out, "Interrupt\n");
3207  }
3208  nData = (nRow+1)*nColumn;
3209  for(i=0; i<nData; i++) free(azData[i]);
3210  sqlite3_free(azData);
3211}
3212
3213/*
3214** Run a prepared statement
3215*/
3216static void exec_prepared_stmt(
3217  ShellState *pArg,                                /* Pointer to ShellState */
3218  sqlite3_stmt *pStmt                              /* Statment to run */
3219){
3220  int rc;
3221
3222  if( pArg->cMode==MODE_Column
3223   || pArg->cMode==MODE_Table
3224   || pArg->cMode==MODE_Box
3225   || pArg->cMode==MODE_Markdown
3226  ){
3227    exec_prepared_stmt_columnar(pArg, pStmt);
3228    return;
3229  }
3230
3231  /* perform the first step.  this will tell us if we
3232  ** have a result set or not and how wide it is.
3233  */
3234  rc = sqlite3_step(pStmt);
3235  /* if we have a result set... */
3236  if( SQLITE_ROW == rc ){
3237    /* allocate space for col name ptr, value ptr, and type */
3238    int nCol = sqlite3_column_count(pStmt);
3239    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3240    if( !pData ){
3241      rc = SQLITE_NOMEM;
3242    }else{
3243      char **azCols = (char **)pData;      /* Names of result columns */
3244      char **azVals = &azCols[nCol];       /* Results */
3245      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3246      int i, x;
3247      assert(sizeof(int) <= sizeof(char *));
3248      /* save off ptrs to column names */
3249      for(i=0; i<nCol; i++){
3250        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3251      }
3252      do{
3253        /* extract the data and data types */
3254        for(i=0; i<nCol; i++){
3255          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3256          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3257            azVals[i] = "";
3258          }else{
3259            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3260          }
3261          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3262            rc = SQLITE_NOMEM;
3263            break; /* from for */
3264          }
3265        } /* end for */
3266
3267        /* if data and types extracted successfully... */
3268        if( SQLITE_ROW == rc ){
3269          /* call the supplied callback with the result row data */
3270          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3271            rc = SQLITE_ABORT;
3272          }else{
3273            rc = sqlite3_step(pStmt);
3274          }
3275        }
3276      } while( SQLITE_ROW == rc );
3277      sqlite3_free(pData);
3278      if( pArg->cMode==MODE_Json ){
3279        fputs("]\n", pArg->out);
3280      }
3281    }
3282  }
3283}
3284
3285#ifndef SQLITE_OMIT_VIRTUALTABLE
3286/*
3287** This function is called to process SQL if the previous shell command
3288** was ".expert". It passes the SQL in the second argument directly to
3289** the sqlite3expert object.
3290**
3291** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3292** code. In this case, (*pzErr) may be set to point to a buffer containing
3293** an English language error message. It is the responsibility of the
3294** caller to eventually free this buffer using sqlite3_free().
3295*/
3296static int expertHandleSQL(
3297  ShellState *pState,
3298  const char *zSql,
3299  char **pzErr
3300){
3301  assert( pState->expert.pExpert );
3302  assert( pzErr==0 || *pzErr==0 );
3303  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3304}
3305
3306/*
3307** This function is called either to silently clean up the object
3308** created by the ".expert" command (if bCancel==1), or to generate a
3309** report from it and then clean it up (if bCancel==0).
3310**
3311** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3312** code. In this case, (*pzErr) may be set to point to a buffer containing
3313** an English language error message. It is the responsibility of the
3314** caller to eventually free this buffer using sqlite3_free().
3315*/
3316static int expertFinish(
3317  ShellState *pState,
3318  int bCancel,
3319  char **pzErr
3320){
3321  int rc = SQLITE_OK;
3322  sqlite3expert *p = pState->expert.pExpert;
3323  assert( p );
3324  assert( bCancel || pzErr==0 || *pzErr==0 );
3325  if( bCancel==0 ){
3326    FILE *out = pState->out;
3327    int bVerbose = pState->expert.bVerbose;
3328
3329    rc = sqlite3_expert_analyze(p, pzErr);
3330    if( rc==SQLITE_OK ){
3331      int nQuery = sqlite3_expert_count(p);
3332      int i;
3333
3334      if( bVerbose ){
3335        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3336        raw_printf(out, "-- Candidates -----------------------------\n");
3337        raw_printf(out, "%s\n", zCand);
3338      }
3339      for(i=0; i<nQuery; i++){
3340        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3341        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3342        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3343        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3344        if( bVerbose ){
3345          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3346          raw_printf(out, "%s\n\n", zSql);
3347        }
3348        raw_printf(out, "%s\n", zIdx);
3349        raw_printf(out, "%s\n", zEQP);
3350      }
3351    }
3352  }
3353  sqlite3_expert_destroy(p);
3354  pState->expert.pExpert = 0;
3355  return rc;
3356}
3357
3358/*
3359** Implementation of ".expert" dot command.
3360*/
3361static int expertDotCommand(
3362  ShellState *pState,             /* Current shell tool state */
3363  char **azArg,                   /* Array of arguments passed to dot command */
3364  int nArg                        /* Number of entries in azArg[] */
3365){
3366  int rc = SQLITE_OK;
3367  char *zErr = 0;
3368  int i;
3369  int iSample = 0;
3370
3371  assert( pState->expert.pExpert==0 );
3372  memset(&pState->expert, 0, sizeof(ExpertInfo));
3373
3374  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3375    char *z = azArg[i];
3376    int n;
3377    if( z[0]=='-' && z[1]=='-' ) z++;
3378    n = strlen30(z);
3379    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3380      pState->expert.bVerbose = 1;
3381    }
3382    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3383      if( i==(nArg-1) ){
3384        raw_printf(stderr, "option requires an argument: %s\n", z);
3385        rc = SQLITE_ERROR;
3386      }else{
3387        iSample = (int)integerValue(azArg[++i]);
3388        if( iSample<0 || iSample>100 ){
3389          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3390          rc = SQLITE_ERROR;
3391        }
3392      }
3393    }
3394    else{
3395      raw_printf(stderr, "unknown option: %s\n", z);
3396      rc = SQLITE_ERROR;
3397    }
3398  }
3399
3400  if( rc==SQLITE_OK ){
3401    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3402    if( pState->expert.pExpert==0 ){
3403      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3404      rc = SQLITE_ERROR;
3405    }else{
3406      sqlite3_expert_config(
3407          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3408      );
3409    }
3410  }
3411
3412  return rc;
3413}
3414#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3415
3416/*
3417** Execute a statement or set of statements.  Print
3418** any result rows/columns depending on the current mode
3419** set via the supplied callback.
3420**
3421** This is very similar to SQLite's built-in sqlite3_exec()
3422** function except it takes a slightly different callback
3423** and callback data argument.
3424*/
3425static int shell_exec(
3426  ShellState *pArg,                         /* Pointer to ShellState */
3427  const char *zSql,                         /* SQL to be evaluated */
3428  char **pzErrMsg                           /* Error msg written here */
3429){
3430  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3431  int rc = SQLITE_OK;             /* Return Code */
3432  int rc2;
3433  const char *zLeftover;          /* Tail of unprocessed SQL */
3434  sqlite3 *db = pArg->db;
3435
3436  if( pzErrMsg ){
3437    *pzErrMsg = NULL;
3438  }
3439
3440#ifndef SQLITE_OMIT_VIRTUALTABLE
3441  if( pArg->expert.pExpert ){
3442    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3443    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3444  }
3445#endif
3446
3447  while( zSql[0] && (SQLITE_OK == rc) ){
3448    static const char *zStmtSql;
3449    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3450    if( SQLITE_OK != rc ){
3451      if( pzErrMsg ){
3452        *pzErrMsg = save_err_msg(db);
3453      }
3454    }else{
3455      if( !pStmt ){
3456        /* this happens for a comment or white-space */
3457        zSql = zLeftover;
3458        while( IsSpace(zSql[0]) ) zSql++;
3459        continue;
3460      }
3461      zStmtSql = sqlite3_sql(pStmt);
3462      if( zStmtSql==0 ) zStmtSql = "";
3463      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3464
3465      /* save off the prepared statment handle and reset row count */
3466      if( pArg ){
3467        pArg->pStmt = pStmt;
3468        pArg->cnt = 0;
3469      }
3470
3471      /* echo the sql statement if echo on */
3472      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3473        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3474      }
3475
3476      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3477      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3478        sqlite3_stmt *pExplain;
3479        char *zEQP;
3480        int triggerEQP = 0;
3481        disable_debug_trace_modes();
3482        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3483        if( pArg->autoEQP>=AUTOEQP_trigger ){
3484          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3485        }
3486        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3487        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3488        if( rc==SQLITE_OK ){
3489          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3490            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3491            int iEqpId = sqlite3_column_int(pExplain, 0);
3492            int iParentId = sqlite3_column_int(pExplain, 1);
3493            if( zEQPLine==0 ) zEQPLine = "";
3494            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3495            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3496          }
3497          eqp_render(pArg);
3498        }
3499        sqlite3_finalize(pExplain);
3500        sqlite3_free(zEQP);
3501        if( pArg->autoEQP>=AUTOEQP_full ){
3502          /* Also do an EXPLAIN for ".eqp full" mode */
3503          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3504          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3505          if( rc==SQLITE_OK ){
3506            pArg->cMode = MODE_Explain;
3507            explain_data_prepare(pArg, pExplain);
3508            exec_prepared_stmt(pArg, pExplain);
3509            explain_data_delete(pArg);
3510          }
3511          sqlite3_finalize(pExplain);
3512          sqlite3_free(zEQP);
3513        }
3514        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3515          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3516          /* Reprepare pStmt before reactiving trace modes */
3517          sqlite3_finalize(pStmt);
3518          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3519          if( pArg ) pArg->pStmt = pStmt;
3520        }
3521        restore_debug_trace_modes();
3522      }
3523
3524      if( pArg ){
3525        pArg->cMode = pArg->mode;
3526        if( pArg->autoExplain ){
3527          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3528            pArg->cMode = MODE_Explain;
3529          }
3530          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3531            pArg->cMode = MODE_EQP;
3532          }
3533        }
3534
3535        /* If the shell is currently in ".explain" mode, gather the extra
3536        ** data required to add indents to the output.*/
3537        if( pArg->cMode==MODE_Explain ){
3538          explain_data_prepare(pArg, pStmt);
3539        }
3540      }
3541
3542      bind_prepared_stmt(pArg, pStmt);
3543      exec_prepared_stmt(pArg, pStmt);
3544      explain_data_delete(pArg);
3545      eqp_render(pArg);
3546
3547      /* print usage stats if stats on */
3548      if( pArg && pArg->statsOn ){
3549        display_stats(db, pArg, 0);
3550      }
3551
3552      /* print loop-counters if required */
3553      if( pArg && pArg->scanstatsOn ){
3554        display_scanstats(db, pArg);
3555      }
3556
3557      /* Finalize the statement just executed. If this fails, save a
3558      ** copy of the error message. Otherwise, set zSql to point to the
3559      ** next statement to execute. */
3560      rc2 = sqlite3_finalize(pStmt);
3561      if( rc!=SQLITE_NOMEM ) rc = rc2;
3562      if( rc==SQLITE_OK ){
3563        zSql = zLeftover;
3564        while( IsSpace(zSql[0]) ) zSql++;
3565      }else if( pzErrMsg ){
3566        *pzErrMsg = save_err_msg(db);
3567      }
3568
3569      /* clear saved stmt handle */
3570      if( pArg ){
3571        pArg->pStmt = NULL;
3572      }
3573    }
3574  } /* end while */
3575
3576  return rc;
3577}
3578
3579/*
3580** Release memory previously allocated by tableColumnList().
3581*/
3582static void freeColumnList(char **azCol){
3583  int i;
3584  for(i=1; azCol[i]; i++){
3585    sqlite3_free(azCol[i]);
3586  }
3587  /* azCol[0] is a static string */
3588  sqlite3_free(azCol);
3589}
3590
3591/*
3592** Return a list of pointers to strings which are the names of all
3593** columns in table zTab.   The memory to hold the names is dynamically
3594** allocated and must be released by the caller using a subsequent call
3595** to freeColumnList().
3596**
3597** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3598** value that needs to be preserved, then azCol[0] is filled in with the
3599** name of the rowid column.
3600**
3601** The first regular column in the table is azCol[1].  The list is terminated
3602** by an entry with azCol[i]==0.
3603*/
3604static char **tableColumnList(ShellState *p, const char *zTab){
3605  char **azCol = 0;
3606  sqlite3_stmt *pStmt;
3607  char *zSql;
3608  int nCol = 0;
3609  int nAlloc = 0;
3610  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3611  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3612  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3613  int rc;
3614
3615  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3616  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3617  sqlite3_free(zSql);
3618  if( rc ) return 0;
3619  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3620    if( nCol>=nAlloc-2 ){
3621      nAlloc = nAlloc*2 + nCol + 10;
3622      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3623      if( azCol==0 ) shell_out_of_memory();
3624    }
3625    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3626    if( sqlite3_column_int(pStmt, 5) ){
3627      nPK++;
3628      if( nPK==1
3629       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3630                          "INTEGER")==0
3631      ){
3632        isIPK = 1;
3633      }else{
3634        isIPK = 0;
3635      }
3636    }
3637  }
3638  sqlite3_finalize(pStmt);
3639  if( azCol==0 ) return 0;
3640  azCol[0] = 0;
3641  azCol[nCol+1] = 0;
3642
3643  /* The decision of whether or not a rowid really needs to be preserved
3644  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3645  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3646  ** rowids on tables where the rowid is inaccessible because there are other
3647  ** columns in the table named "rowid", "_rowid_", and "oid".
3648  */
3649  if( preserveRowid && isIPK ){
3650    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3651    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3652    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3653    ** ROWID aliases.  To distinguish these cases, check to see if
3654    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3655    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3656    */
3657    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3658                           " WHERE origin='pk'", zTab);
3659    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3660    sqlite3_free(zSql);
3661    if( rc ){
3662      freeColumnList(azCol);
3663      return 0;
3664    }
3665    rc = sqlite3_step(pStmt);
3666    sqlite3_finalize(pStmt);
3667    preserveRowid = rc==SQLITE_ROW;
3668  }
3669  if( preserveRowid ){
3670    /* Only preserve the rowid if we can find a name to use for the
3671    ** rowid */
3672    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3673    int i, j;
3674    for(j=0; j<3; j++){
3675      for(i=1; i<=nCol; i++){
3676        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3677      }
3678      if( i>nCol ){
3679        /* At this point, we know that azRowid[j] is not the name of any
3680        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3681        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3682        ** tables will fail this last check */
3683        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3684        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3685        break;
3686      }
3687    }
3688  }
3689  return azCol;
3690}
3691
3692/*
3693** Toggle the reverse_unordered_selects setting.
3694*/
3695static void toggleSelectOrder(sqlite3 *db){
3696  sqlite3_stmt *pStmt = 0;
3697  int iSetting = 0;
3698  char zStmt[100];
3699  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3700  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3701    iSetting = sqlite3_column_int(pStmt, 0);
3702  }
3703  sqlite3_finalize(pStmt);
3704  sqlite3_snprintf(sizeof(zStmt), zStmt,
3705       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3706  sqlite3_exec(db, zStmt, 0, 0, 0);
3707}
3708
3709/*
3710** This is a different callback routine used for dumping the database.
3711** Each row received by this callback consists of a table name,
3712** the table type ("index" or "table") and SQL to create the table.
3713** This routine should print text sufficient to recreate the table.
3714*/
3715static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3716  int rc;
3717  const char *zTable;
3718  const char *zType;
3719  const char *zSql;
3720  ShellState *p = (ShellState *)pArg;
3721  int dataOnly;
3722  int noSys;
3723
3724  UNUSED_PARAMETER(azNotUsed);
3725  if( nArg!=3 || azArg==0 ) return 0;
3726  zTable = azArg[0];
3727  zType = azArg[1];
3728  zSql = azArg[2];
3729  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
3730  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
3731
3732  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
3733    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3734  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
3735    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3736  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3737    return 0;
3738  }else if( dataOnly ){
3739    /* no-op */
3740  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3741    char *zIns;
3742    if( !p->writableSchema ){
3743      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3744      p->writableSchema = 1;
3745    }
3746    zIns = sqlite3_mprintf(
3747       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3748       "VALUES('table','%q','%q',0,'%q');",
3749       zTable, zTable, zSql);
3750    utf8_printf(p->out, "%s\n", zIns);
3751    sqlite3_free(zIns);
3752    return 0;
3753  }else{
3754    printSchemaLine(p->out, zSql, ";\n");
3755  }
3756
3757  if( strcmp(zType, "table")==0 ){
3758    ShellText sSelect;
3759    ShellText sTable;
3760    char **azCol;
3761    int i;
3762    char *savedDestTable;
3763    int savedMode;
3764
3765    azCol = tableColumnList(p, zTable);
3766    if( azCol==0 ){
3767      p->nErr++;
3768      return 0;
3769    }
3770
3771    /* Always quote the table name, even if it appears to be pure ascii,
3772    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3773    initText(&sTable);
3774    appendText(&sTable, zTable, quoteChar(zTable));
3775    /* If preserving the rowid, add a column list after the table name.
3776    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3777    ** instead of the usual "INSERT INTO tab VALUES(...)".
3778    */
3779    if( azCol[0] ){
3780      appendText(&sTable, "(", 0);
3781      appendText(&sTable, azCol[0], 0);
3782      for(i=1; azCol[i]; i++){
3783        appendText(&sTable, ",", 0);
3784        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3785      }
3786      appendText(&sTable, ")", 0);
3787    }
3788
3789    /* Build an appropriate SELECT statement */
3790    initText(&sSelect);
3791    appendText(&sSelect, "SELECT ", 0);
3792    if( azCol[0] ){
3793      appendText(&sSelect, azCol[0], 0);
3794      appendText(&sSelect, ",", 0);
3795    }
3796    for(i=1; azCol[i]; i++){
3797      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3798      if( azCol[i+1] ){
3799        appendText(&sSelect, ",", 0);
3800      }
3801    }
3802    freeColumnList(azCol);
3803    appendText(&sSelect, " FROM ", 0);
3804    appendText(&sSelect, zTable, quoteChar(zTable));
3805
3806    savedDestTable = p->zDestTable;
3807    savedMode = p->mode;
3808    p->zDestTable = sTable.z;
3809    p->mode = p->cMode = MODE_Insert;
3810    rc = shell_exec(p, sSelect.z, 0);
3811    if( (rc&0xff)==SQLITE_CORRUPT ){
3812      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3813      toggleSelectOrder(p->db);
3814      shell_exec(p, sSelect.z, 0);
3815      toggleSelectOrder(p->db);
3816    }
3817    p->zDestTable = savedDestTable;
3818    p->mode = savedMode;
3819    freeText(&sTable);
3820    freeText(&sSelect);
3821    if( rc ) p->nErr++;
3822  }
3823  return 0;
3824}
3825
3826/*
3827** Run zQuery.  Use dump_callback() as the callback routine so that
3828** the contents of the query are output as SQL statements.
3829**
3830** If we get a SQLITE_CORRUPT error, rerun the query after appending
3831** "ORDER BY rowid DESC" to the end.
3832*/
3833static int run_schema_dump_query(
3834  ShellState *p,
3835  const char *zQuery
3836){
3837  int rc;
3838  char *zErr = 0;
3839  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3840  if( rc==SQLITE_CORRUPT ){
3841    char *zQ2;
3842    int len = strlen30(zQuery);
3843    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3844    if( zErr ){
3845      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3846      sqlite3_free(zErr);
3847      zErr = 0;
3848    }
3849    zQ2 = malloc( len+100 );
3850    if( zQ2==0 ) return rc;
3851    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3852    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3853    if( rc ){
3854      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3855    }else{
3856      rc = SQLITE_CORRUPT;
3857    }
3858    sqlite3_free(zErr);
3859    free(zQ2);
3860  }
3861  return rc;
3862}
3863
3864/*
3865** Text of help messages.
3866**
3867** The help text for each individual command begins with a line that starts
3868** with ".".  Subsequent lines are supplimental information.
3869**
3870** There must be two or more spaces between the end of the command and the
3871** start of the description of what that command does.
3872*/
3873static const char *(azHelp[]) = {
3874#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3875  ".archive ...             Manage SQL archives",
3876  "   Each command must have exactly one of the following options:",
3877  "     -c, --create               Create a new archive",
3878  "     -u, --update               Add or update files with changed mtime",
3879  "     -i, --insert               Like -u but always add even if unchanged",
3880  "     -t, --list                 List contents of archive",
3881  "     -x, --extract              Extract files from archive",
3882  "   Optional arguments:",
3883  "     -v, --verbose              Print each filename as it is processed",
3884  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3885  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3886  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3887  "     -n, --dryrun               Show the SQL that would have occurred",
3888  "   Examples:",
3889  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3890  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3891  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3892  "   See also:",
3893  "      http://sqlite.org/cli.html#sqlite_archive_support",
3894#endif
3895#ifndef SQLITE_OMIT_AUTHORIZATION
3896  ".auth ON|OFF             Show authorizer callbacks",
3897#endif
3898  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3899  "       --append            Use the appendvfs",
3900  "       --async             Write to FILE without journal and fsync()",
3901  ".bail on|off             Stop after hitting an error.  Default OFF",
3902  ".binary on|off           Turn binary output on or off.  Default OFF",
3903  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3904  ".changes on|off          Show number of rows changed by SQL",
3905  ".check GLOB              Fail if output since .testcase does not match",
3906  ".clone NEWDB             Clone data into NEWDB from the existing database",
3907  ".databases               List names and files of attached databases",
3908  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3909  ".dbinfo ?DB?             Show status information about the database",
3910  ".dump ?OBJECTS?          Render database content as SQL",
3911  "   Options:",
3912  "     --data-only            Output only INSERT statements",
3913  "     --newlines             Allow unescaped newline characters in output",
3914  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
3915  "     --preserve-rowids      Include ROWID values in the output",
3916  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
3917  "   Additional LIKE patterns can be given in subsequent arguments",
3918  ".echo on|off             Turn command echo on or off",
3919  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3920  "   Other Modes:",
3921#ifdef SQLITE_DEBUG
3922  "      test                  Show raw EXPLAIN QUERY PLAN output",
3923  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3924#endif
3925  "      trigger               Like \"full\" but also show trigger bytecode",
3926  ".excel                   Display the output of next command in spreadsheet",
3927  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
3928  ".exit ?CODE?             Exit this program with return-code CODE",
3929  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3930  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3931  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3932  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
3933  "   --help                  Show CMD details",
3934  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3935  ".headers on|off          Turn display of headers on or off",
3936  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3937  ".import FILE TABLE       Import data from FILE into TABLE",
3938  "   Options:",
3939  "     --ascii               Use \\037 and \\036 as column and row separators",
3940  "     --csv                 Use , and \\n as column and row separators",
3941  "     --skip N              Skip the first N rows of input",
3942  "     -v                    \"Verbose\" - increase auxiliary output",
3943  "   Notes:",
3944  "     *  If TABLE does not exist, it is created.  The first row of input",
3945  "        determines the column names.",
3946  "     *  If neither --csv or --ascii are used, the input mode is derived",
3947  "        from the \".mode\" output mode",
3948  "     *  If FILE begins with \"|\" then it is a command that generates the",
3949  "        input text.",
3950#ifndef SQLITE_OMIT_TEST_CONTROL
3951  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3952#endif
3953  ".indexes ?TABLE?         Show names of indexes",
3954  "                           If TABLE is specified, only show indexes for",
3955  "                           tables matching TABLE using the LIKE operator.",
3956#ifdef SQLITE_ENABLE_IOTRACE
3957  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3958#endif
3959  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3960  ".lint OPTIONS            Report potential schema issues.",
3961  "     Options:",
3962  "        fkey-indexes     Find missing foreign key indexes",
3963#ifndef SQLITE_OMIT_LOAD_EXTENSION
3964  ".load FILE ?ENTRY?       Load an extension library",
3965#endif
3966  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3967  ".mode MODE ?TABLE?       Set output mode",
3968  "   MODE is one of:",
3969  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
3970  "     box       Tables using unicode box-drawing characters",
3971  "     csv       Comma-separated values",
3972  "     column    Output in columns.  (See .width)",
3973  "     html      HTML <table> code",
3974  "     insert    SQL insert statements for TABLE",
3975  "     json      Results in a JSON array",
3976  "     line      One value per line",
3977  "     list      Values delimited by \"|\"",
3978  "     markdown  Markdown table format",
3979  "     quote     Escape answers as for SQL",
3980  "     table     ASCII-art table",
3981  "     tabs      Tab-separated values",
3982  "     tcl       TCL list elements",
3983  ".nullvalue STRING        Use STRING in place of NULL values",
3984  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
3985  "     If FILE begins with '|' then open as a pipe",
3986  "       --bom  Put a UTF8 byte-order mark at the beginning",
3987  "       -e     Send output to the system text editor",
3988  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
3989#ifdef SQLITE_DEBUG
3990  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
3991#endif
3992  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3993  "     Options:",
3994  "        --append        Use appendvfs to append database to the end of FILE",
3995#ifndef SQLITE_OMIT_DESERIALIZE
3996  "        --deserialize   Load into memory using sqlite3_deserialize()",
3997  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3998  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3999#endif
4000  "        --new           Initialize FILE to an empty database",
4001  "        --nofollow      Do not follow symbolic links",
4002  "        --readonly      Open FILE readonly",
4003  "        --zip           FILE is a ZIP archive",
4004  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
4005  "   If FILE begins with '|' then open it as a pipe.",
4006  "   Options:",
4007  "     --bom                 Prefix output with a UTF8 byte-order mark",
4008  "     -e                    Send output to the system text editor",
4009  "     -x                    Send output as CSV to a spreadsheet",
4010  ".parameter CMD ...       Manage SQL parameter bindings",
4011  "   clear                   Erase all bindings",
4012  "   init                    Initialize the TEMP table that holds bindings",
4013  "   list                    List the current parameter bindings",
4014  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
4015  "                           PARAMETER should start with one of: $ : @ ?",
4016  "   unset PARAMETER         Remove PARAMETER from the binding table",
4017  ".print STRING...         Print literal STRING",
4018#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4019  ".progress N              Invoke progress handler after every N opcodes",
4020  "   --limit N                 Interrupt after N progress callbacks",
4021  "   --once                    Do no more than one progress interrupt",
4022  "   --quiet|-q                No output except at interrupts",
4023  "   --reset                   Reset the count for each input and interrupt",
4024#endif
4025  ".prompt MAIN CONTINUE    Replace the standard prompts",
4026  ".quit                    Exit this program",
4027  ".read FILE               Read input from FILE",
4028#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4029  ".recover                 Recover as much data as possible from corrupt db.",
4030  "   --freelist-corrupt       Assume the freelist is corrupt",
4031  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4032  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4033  "   --no-rowids              Do not attempt to recover rowid values",
4034  "                            that are not also INTEGER PRIMARY KEYs",
4035#endif
4036  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4037  ".save FILE               Write in-memory database into FILE",
4038  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4039  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4040  "   Options:",
4041  "      --indent             Try to pretty-print the schema",
4042  "      --nosys              Omit objects whose names start with \"sqlite_\"",
4043  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4044  "    Options:",
4045  "       --init               Create a new SELFTEST table",
4046  "       -v                   Verbose output",
4047  ".separator COL ?ROW?     Change the column and row separators",
4048#if defined(SQLITE_ENABLE_SESSION)
4049  ".session ?NAME? CMD ...  Create or control sessions",
4050  "   Subcommands:",
4051  "     attach TABLE             Attach TABLE",
4052  "     changeset FILE           Write a changeset into FILE",
4053  "     close                    Close one session",
4054  "     enable ?BOOLEAN?         Set or query the enable bit",
4055  "     filter GLOB...           Reject tables matching GLOBs",
4056  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4057  "     isempty                  Query whether the session is empty",
4058  "     list                     List currently open session names",
4059  "     open DB NAME             Open a new session on DB",
4060  "     patchset FILE            Write a patchset into FILE",
4061  "   If ?NAME? is omitted, the first defined session is used.",
4062#endif
4063  ".sha3sum ...             Compute a SHA3 hash of database content",
4064  "    Options:",
4065  "      --schema              Also hash the sqlite_schema table",
4066  "      --sha3-224            Use the sha3-224 algorithm",
4067  "      --sha3-256            Use the sha3-256 algorithm (default)",
4068  "      --sha3-384            Use the sha3-384 algorithm",
4069  "      --sha3-512            Use the sha3-512 algorithm",
4070  "    Any other argument is a LIKE pattern for tables to hash",
4071#ifndef SQLITE_NOHAVE_SYSTEM
4072  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4073#endif
4074  ".show                    Show the current values for various settings",
4075  ".stats ?ARG?             Show stats or turn stats on or off",
4076  "   off                      Turn off automatic stat display",
4077  "   on                       Turn on automatic stat display",
4078  "   stmt                     Show statement stats",
4079  "   vmstep                   Show the virtual machine step count only",
4080#ifndef SQLITE_NOHAVE_SYSTEM
4081  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4082#endif
4083  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4084  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4085  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4086  "                           Run \".testctrl\" with no arguments for details",
4087  ".timeout MS              Try opening locked tables for MS milliseconds",
4088  ".timer on|off            Turn SQL timer on or off",
4089#ifndef SQLITE_OMIT_TRACE
4090  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4091  "    FILE                    Send output to FILE",
4092  "    stdout                  Send output to stdout",
4093  "    stderr                  Send output to stderr",
4094  "    off                     Disable tracing",
4095  "    --expanded              Expand query parameters",
4096#ifdef SQLITE_ENABLE_NORMALIZE
4097  "    --normalized            Normal the SQL statements",
4098#endif
4099  "    --plain                 Show SQL as it is input",
4100  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4101  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4102  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4103  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4104#endif /* SQLITE_OMIT_TRACE */
4105#ifdef SQLITE_DEBUG
4106  ".unmodule NAME ...       Unregister virtual table modules",
4107  "    --allexcept             Unregister everything except those named",
4108#endif
4109  ".vfsinfo ?AUX?           Information about the top-level VFS",
4110  ".vfslist                 List all available VFSes",
4111  ".vfsname ?AUX?           Print the name of the VFS stack",
4112  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4113  "     Negative values right-justify",
4114};
4115
4116/*
4117** Output help text.
4118**
4119** zPattern describes the set of commands for which help text is provided.
4120** If zPattern is NULL, then show all commands, but only give a one-line
4121** description of each.
4122**
4123** Return the number of matches.
4124*/
4125static int showHelp(FILE *out, const char *zPattern){
4126  int i = 0;
4127  int j = 0;
4128  int n = 0;
4129  char *zPat;
4130  if( zPattern==0
4131   || zPattern[0]=='0'
4132   || strcmp(zPattern,"-a")==0
4133   || strcmp(zPattern,"-all")==0
4134   || strcmp(zPattern,"--all")==0
4135  ){
4136    /* Show all commands, but only one line per command */
4137    if( zPattern==0 ) zPattern = "";
4138    for(i=0; i<ArraySize(azHelp); i++){
4139      if( azHelp[i][0]=='.' || zPattern[0] ){
4140        utf8_printf(out, "%s\n", azHelp[i]);
4141        n++;
4142      }
4143    }
4144  }else{
4145    /* Look for commands that for which zPattern is an exact prefix */
4146    zPat = sqlite3_mprintf(".%s*", zPattern);
4147    for(i=0; i<ArraySize(azHelp); i++){
4148      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4149        utf8_printf(out, "%s\n", azHelp[i]);
4150        j = i+1;
4151        n++;
4152      }
4153    }
4154    sqlite3_free(zPat);
4155    if( n ){
4156      if( n==1 ){
4157        /* when zPattern is a prefix of exactly one command, then include the
4158        ** details of that command, which should begin at offset j */
4159        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4160          utf8_printf(out, "%s\n", azHelp[j]);
4161          j++;
4162        }
4163      }
4164      return n;
4165    }
4166    /* Look for commands that contain zPattern anywhere.  Show the complete
4167    ** text of all commands that match. */
4168    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4169    for(i=0; i<ArraySize(azHelp); i++){
4170      if( azHelp[i][0]=='.' ) j = i;
4171      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4172        utf8_printf(out, "%s\n", azHelp[j]);
4173        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4174          j++;
4175          utf8_printf(out, "%s\n", azHelp[j]);
4176        }
4177        i = j;
4178        n++;
4179      }
4180    }
4181    sqlite3_free(zPat);
4182  }
4183  return n;
4184}
4185
4186/* Forward reference */
4187static int process_input(ShellState *p);
4188
4189/*
4190** Read the content of file zName into memory obtained from sqlite3_malloc64()
4191** and return a pointer to the buffer. The caller is responsible for freeing
4192** the memory.
4193**
4194** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4195** read.
4196**
4197** For convenience, a nul-terminator byte is always appended to the data read
4198** from the file before the buffer is returned. This byte is not included in
4199** the final value of (*pnByte), if applicable.
4200**
4201** NULL is returned if any error is encountered. The final value of *pnByte
4202** is undefined in this case.
4203*/
4204static char *readFile(const char *zName, int *pnByte){
4205  FILE *in = fopen(zName, "rb");
4206  long nIn;
4207  size_t nRead;
4208  char *pBuf;
4209  if( in==0 ) return 0;
4210  fseek(in, 0, SEEK_END);
4211  nIn = ftell(in);
4212  rewind(in);
4213  pBuf = sqlite3_malloc64( nIn+1 );
4214  if( pBuf==0 ){ fclose(in); return 0; }
4215  nRead = fread(pBuf, nIn, 1, in);
4216  fclose(in);
4217  if( nRead!=1 ){
4218    sqlite3_free(pBuf);
4219    return 0;
4220  }
4221  pBuf[nIn] = 0;
4222  if( pnByte ) *pnByte = nIn;
4223  return pBuf;
4224}
4225
4226#if defined(SQLITE_ENABLE_SESSION)
4227/*
4228** Close a single OpenSession object and release all of its associated
4229** resources.
4230*/
4231static void session_close(OpenSession *pSession){
4232  int i;
4233  sqlite3session_delete(pSession->p);
4234  sqlite3_free(pSession->zName);
4235  for(i=0; i<pSession->nFilter; i++){
4236    sqlite3_free(pSession->azFilter[i]);
4237  }
4238  sqlite3_free(pSession->azFilter);
4239  memset(pSession, 0, sizeof(OpenSession));
4240}
4241#endif
4242
4243/*
4244** Close all OpenSession objects and release all associated resources.
4245*/
4246#if defined(SQLITE_ENABLE_SESSION)
4247static void session_close_all(ShellState *p){
4248  int i;
4249  for(i=0; i<p->nSession; i++){
4250    session_close(&p->aSession[i]);
4251  }
4252  p->nSession = 0;
4253}
4254#else
4255# define session_close_all(X)
4256#endif
4257
4258/*
4259** Implementation of the xFilter function for an open session.  Omit
4260** any tables named by ".session filter" but let all other table through.
4261*/
4262#if defined(SQLITE_ENABLE_SESSION)
4263static int session_filter(void *pCtx, const char *zTab){
4264  OpenSession *pSession = (OpenSession*)pCtx;
4265  int i;
4266  for(i=0; i<pSession->nFilter; i++){
4267    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4268  }
4269  return 1;
4270}
4271#endif
4272
4273/*
4274** Try to deduce the type of file for zName based on its content.  Return
4275** one of the SHELL_OPEN_* constants.
4276**
4277** If the file does not exist or is empty but its name looks like a ZIP
4278** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4279** Otherwise, assume an ordinary database regardless of the filename if
4280** the type cannot be determined from content.
4281*/
4282int deduceDatabaseType(const char *zName, int dfltZip){
4283  FILE *f = fopen(zName, "rb");
4284  size_t n;
4285  int rc = SHELL_OPEN_UNSPEC;
4286  char zBuf[100];
4287  if( f==0 ){
4288    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4289       return SHELL_OPEN_ZIPFILE;
4290    }else{
4291       return SHELL_OPEN_NORMAL;
4292    }
4293  }
4294  n = fread(zBuf, 16, 1, f);
4295  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4296    fclose(f);
4297    return SHELL_OPEN_NORMAL;
4298  }
4299  fseek(f, -25, SEEK_END);
4300  n = fread(zBuf, 25, 1, f);
4301  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4302    rc = SHELL_OPEN_APPENDVFS;
4303  }else{
4304    fseek(f, -22, SEEK_END);
4305    n = fread(zBuf, 22, 1, f);
4306    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4307       && zBuf[3]==0x06 ){
4308      rc = SHELL_OPEN_ZIPFILE;
4309    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4310      rc = SHELL_OPEN_ZIPFILE;
4311    }
4312  }
4313  fclose(f);
4314  return rc;
4315}
4316
4317#ifndef SQLITE_OMIT_DESERIALIZE
4318/*
4319** Reconstruct an in-memory database using the output from the "dbtotxt"
4320** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
4321** is 0, then read from standard input.
4322*/
4323static unsigned char *readHexDb(ShellState *p, int *pnData){
4324  unsigned char *a = 0;
4325  int nLine;
4326  int n = 0;
4327  int pgsz = 0;
4328  int iOffset = 0;
4329  int j, k;
4330  int rc;
4331  FILE *in;
4332  unsigned int x[16];
4333  char zLine[1000];
4334  if( p->zDbFilename ){
4335    in = fopen(p->zDbFilename, "r");
4336    if( in==0 ){
4337      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
4338      return 0;
4339    }
4340    nLine = 0;
4341  }else{
4342    in = p->in;
4343    nLine = p->lineno;
4344    if( in==0 ) in = stdin;
4345  }
4346  *pnData = 0;
4347  nLine++;
4348  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4349  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4350  if( rc!=2 ) goto readHexDb_error;
4351  if( n<0 ) goto readHexDb_error;
4352  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4353  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4354  a = sqlite3_malloc( n ? n : 1 );
4355  if( a==0 ){
4356    utf8_printf(stderr, "Out of memory!\n");
4357    goto readHexDb_error;
4358  }
4359  memset(a, 0, n);
4360  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4361    utf8_printf(stderr, "invalid pagesize\n");
4362    goto readHexDb_error;
4363  }
4364  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4365    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4366    if( rc==2 ){
4367      iOffset = k;
4368      continue;
4369    }
4370    if( strncmp(zLine, "| end ", 6)==0 ){
4371      break;
4372    }
4373    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4374                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4375                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4376    if( rc==17 ){
4377      k = iOffset+j;
4378      if( k+16<=n ){
4379        int ii;
4380        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4381      }
4382    }
4383  }
4384  *pnData = n;
4385  if( in!=p->in ){
4386    fclose(in);
4387  }else{
4388    p->lineno = nLine;
4389  }
4390  return a;
4391
4392readHexDb_error:
4393  if( in!=p->in ){
4394    fclose(in);
4395  }else{
4396    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4397      nLine++;
4398      if(strncmp(zLine, "| end ", 6)==0 ) break;
4399    }
4400    p->lineno = nLine;
4401  }
4402  sqlite3_free(a);
4403  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4404  return 0;
4405}
4406#endif /* SQLITE_OMIT_DESERIALIZE */
4407
4408/*
4409** Scalar function "shell_int32". The first argument to this function
4410** must be a blob. The second a non-negative integer. This function
4411** reads and returns a 32-bit big-endian integer from byte
4412** offset (4*<arg2>) of the blob.
4413*/
4414static void shellInt32(
4415  sqlite3_context *context,
4416  int argc,
4417  sqlite3_value **argv
4418){
4419  const unsigned char *pBlob;
4420  int nBlob;
4421  int iInt;
4422
4423  UNUSED_PARAMETER(argc);
4424  nBlob = sqlite3_value_bytes(argv[0]);
4425  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4426  iInt = sqlite3_value_int(argv[1]);
4427
4428  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4429    const unsigned char *a = &pBlob[iInt*4];
4430    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4431                       + ((sqlite3_int64)a[1]<<16)
4432                       + ((sqlite3_int64)a[2]<< 8)
4433                       + ((sqlite3_int64)a[3]<< 0);
4434    sqlite3_result_int64(context, iVal);
4435  }
4436}
4437
4438/*
4439** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4440** using "..." with internal double-quote characters doubled.
4441*/
4442static void shellIdQuote(
4443  sqlite3_context *context,
4444  int argc,
4445  sqlite3_value **argv
4446){
4447  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4448  UNUSED_PARAMETER(argc);
4449  if( zName ){
4450    char *z = sqlite3_mprintf("\"%w\"", zName);
4451    sqlite3_result_text(context, z, -1, sqlite3_free);
4452  }
4453}
4454
4455/*
4456** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
4457*/
4458static void shellUSleepFunc(
4459  sqlite3_context *context,
4460  int argcUnused,
4461  sqlite3_value **argv
4462){
4463  int sleep = sqlite3_value_int(argv[0]);
4464  (void)argcUnused;
4465  sqlite3_sleep(sleep/1000);
4466  sqlite3_result_int(context, sleep);
4467}
4468
4469/*
4470** Scalar function "shell_escape_crnl" used by the .recover command.
4471** The argument passed to this function is the output of built-in
4472** function quote(). If the first character of the input is "'",
4473** indicating that the value passed to quote() was a text value,
4474** then this function searches the input for "\n" and "\r" characters
4475** and adds a wrapper similar to the following:
4476**
4477**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4478**
4479** Or, if the first character of the input is not "'", then a copy
4480** of the input is returned.
4481*/
4482static void shellEscapeCrnl(
4483  sqlite3_context *context,
4484  int argc,
4485  sqlite3_value **argv
4486){
4487  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4488  UNUSED_PARAMETER(argc);
4489  if( zText[0]=='\'' ){
4490    int nText = sqlite3_value_bytes(argv[0]);
4491    int i;
4492    char zBuf1[20];
4493    char zBuf2[20];
4494    const char *zNL = 0;
4495    const char *zCR = 0;
4496    int nCR = 0;
4497    int nNL = 0;
4498
4499    for(i=0; zText[i]; i++){
4500      if( zNL==0 && zText[i]=='\n' ){
4501        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4502        nNL = (int)strlen(zNL);
4503      }
4504      if( zCR==0 && zText[i]=='\r' ){
4505        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4506        nCR = (int)strlen(zCR);
4507      }
4508    }
4509
4510    if( zNL || zCR ){
4511      int iOut = 0;
4512      i64 nMax = (nNL > nCR) ? nNL : nCR;
4513      i64 nAlloc = nMax * nText + (nMax+64)*2;
4514      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4515      if( zOut==0 ){
4516        sqlite3_result_error_nomem(context);
4517        return;
4518      }
4519
4520      if( zNL && zCR ){
4521        memcpy(&zOut[iOut], "replace(replace(", 16);
4522        iOut += 16;
4523      }else{
4524        memcpy(&zOut[iOut], "replace(", 8);
4525        iOut += 8;
4526      }
4527      for(i=0; zText[i]; i++){
4528        if( zText[i]=='\n' ){
4529          memcpy(&zOut[iOut], zNL, nNL);
4530          iOut += nNL;
4531        }else if( zText[i]=='\r' ){
4532          memcpy(&zOut[iOut], zCR, nCR);
4533          iOut += nCR;
4534        }else{
4535          zOut[iOut] = zText[i];
4536          iOut++;
4537        }
4538      }
4539
4540      if( zNL ){
4541        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4542        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4543        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4544      }
4545      if( zCR ){
4546        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4547        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4548        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4549      }
4550
4551      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4552      sqlite3_free(zOut);
4553      return;
4554    }
4555  }
4556
4557  sqlite3_result_value(context, argv[0]);
4558}
4559
4560/* Flags for open_db().
4561**
4562** The default behavior of open_db() is to exit(1) if the database fails to
4563** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4564** but still returns without calling exit.
4565**
4566** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4567** ZIP archive if the file does not exist or is empty and its name matches
4568** the *.zip pattern.
4569*/
4570#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4571#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4572
4573/*
4574** Make sure the database is open.  If it is not, then open it.  If
4575** the database fails to open, print an error message and exit.
4576*/
4577static void open_db(ShellState *p, int openFlags){
4578  if( p->db==0 ){
4579    if( p->openMode==SHELL_OPEN_UNSPEC ){
4580      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4581        p->openMode = SHELL_OPEN_NORMAL;
4582      }else{
4583        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4584                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4585      }
4586    }
4587    switch( p->openMode ){
4588      case SHELL_OPEN_APPENDVFS: {
4589        sqlite3_open_v2(p->zDbFilename, &p->db,
4590           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4591        break;
4592      }
4593      case SHELL_OPEN_HEXDB:
4594      case SHELL_OPEN_DESERIALIZE: {
4595        sqlite3_open(0, &p->db);
4596        break;
4597      }
4598      case SHELL_OPEN_ZIPFILE: {
4599        sqlite3_open(":memory:", &p->db);
4600        break;
4601      }
4602      case SHELL_OPEN_READONLY: {
4603        sqlite3_open_v2(p->zDbFilename, &p->db,
4604            SQLITE_OPEN_READONLY|p->openFlags, 0);
4605        break;
4606      }
4607      case SHELL_OPEN_UNSPEC:
4608      case SHELL_OPEN_NORMAL: {
4609        sqlite3_open_v2(p->zDbFilename, &p->db,
4610           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4611        break;
4612      }
4613    }
4614    globalDb = p->db;
4615    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4616      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4617          p->zDbFilename, sqlite3_errmsg(p->db));
4618      if( openFlags & OPEN_DB_KEEPALIVE ){
4619        sqlite3_open(":memory:", &p->db);
4620        return;
4621      }
4622      exit(1);
4623    }
4624#ifndef SQLITE_OMIT_LOAD_EXTENSION
4625    sqlite3_enable_load_extension(p->db, 1);
4626#endif
4627    sqlite3_fileio_init(p->db, 0, 0);
4628    sqlite3_shathree_init(p->db, 0, 0);
4629    sqlite3_completion_init(p->db, 0, 0);
4630    sqlite3_uint_init(p->db, 0, 0);
4631    sqlite3_decimal_init(p->db, 0, 0);
4632    sqlite3_regexp_init(p->db, 0, 0);
4633    sqlite3_ieee_init(p->db, 0, 0);
4634    sqlite3_series_init(p->db, 0, 0);
4635#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4636    sqlite3_dbdata_init(p->db, 0, 0);
4637#endif
4638#ifdef SQLITE_HAVE_ZLIB
4639    sqlite3_zipfile_init(p->db, 0, 0);
4640    sqlite3_sqlar_init(p->db, 0, 0);
4641#endif
4642    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4643                            shellAddSchemaName, 0, 0);
4644    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4645                            shellModuleSchema, 0, 0);
4646    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4647                            shellPutsFunc, 0, 0);
4648    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4649                            shellEscapeCrnl, 0, 0);
4650    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4651                            shellInt32, 0, 0);
4652    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4653                            shellIdQuote, 0, 0);
4654    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
4655                            shellUSleepFunc, 0, 0);
4656#ifndef SQLITE_NOHAVE_SYSTEM
4657    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4658                            editFunc, 0, 0);
4659    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4660                            editFunc, 0, 0);
4661#endif
4662    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4663      char *zSql = sqlite3_mprintf(
4664         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4665      sqlite3_exec(p->db, zSql, 0, 0, 0);
4666      sqlite3_free(zSql);
4667    }
4668#ifndef SQLITE_OMIT_DESERIALIZE
4669    else
4670    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4671      int rc;
4672      int nData = 0;
4673      unsigned char *aData;
4674      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4675        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4676      }else{
4677        aData = readHexDb(p, &nData);
4678        if( aData==0 ){
4679          return;
4680        }
4681      }
4682      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4683                   SQLITE_DESERIALIZE_RESIZEABLE |
4684                   SQLITE_DESERIALIZE_FREEONCLOSE);
4685      if( rc ){
4686        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4687      }
4688      if( p->szMax>0 ){
4689        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4690      }
4691    }
4692#endif
4693  }
4694}
4695
4696/*
4697** Attempt to close the databaes connection.  Report errors.
4698*/
4699void close_db(sqlite3 *db){
4700  int rc = sqlite3_close(db);
4701  if( rc ){
4702    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4703        rc, sqlite3_errmsg(db));
4704  }
4705}
4706
4707#if HAVE_READLINE || HAVE_EDITLINE
4708/*
4709** Readline completion callbacks
4710*/
4711static char *readline_completion_generator(const char *text, int state){
4712  static sqlite3_stmt *pStmt = 0;
4713  char *zRet;
4714  if( state==0 ){
4715    char *zSql;
4716    sqlite3_finalize(pStmt);
4717    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4718                           "  FROM completion(%Q) ORDER BY 1", text);
4719    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4720    sqlite3_free(zSql);
4721  }
4722  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4723    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4724  }else{
4725    sqlite3_finalize(pStmt);
4726    pStmt = 0;
4727    zRet = 0;
4728  }
4729  return zRet;
4730}
4731static char **readline_completion(const char *zText, int iStart, int iEnd){
4732  rl_attempted_completion_over = 1;
4733  return rl_completion_matches(zText, readline_completion_generator);
4734}
4735
4736#elif HAVE_LINENOISE
4737/*
4738** Linenoise completion callback
4739*/
4740static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4741  int nLine = strlen30(zLine);
4742  int i, iStart;
4743  sqlite3_stmt *pStmt = 0;
4744  char *zSql;
4745  char zBuf[1000];
4746
4747  if( nLine>sizeof(zBuf)-30 ) return;
4748  if( zLine[0]=='.' || zLine[0]=='#') return;
4749  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4750  if( i==nLine-1 ) return;
4751  iStart = i+1;
4752  memcpy(zBuf, zLine, iStart);
4753  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4754                         "  FROM completion(%Q,%Q) ORDER BY 1",
4755                         &zLine[iStart], zLine);
4756  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4757  sqlite3_free(zSql);
4758  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4759  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4760    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4761    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4762    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4763      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4764      linenoiseAddCompletion(lc, zBuf);
4765    }
4766  }
4767  sqlite3_finalize(pStmt);
4768}
4769#endif
4770
4771/*
4772** Do C-language style dequoting.
4773**
4774**    \a    -> alarm
4775**    \b    -> backspace
4776**    \t    -> tab
4777**    \n    -> newline
4778**    \v    -> vertical tab
4779**    \f    -> form feed
4780**    \r    -> carriage return
4781**    \s    -> space
4782**    \"    -> "
4783**    \'    -> '
4784**    \\    -> backslash
4785**    \NNN  -> ascii character NNN in octal
4786*/
4787static void resolve_backslashes(char *z){
4788  int i, j;
4789  char c;
4790  while( *z && *z!='\\' ) z++;
4791  for(i=j=0; (c = z[i])!=0; i++, j++){
4792    if( c=='\\' && z[i+1]!=0 ){
4793      c = z[++i];
4794      if( c=='a' ){
4795        c = '\a';
4796      }else if( c=='b' ){
4797        c = '\b';
4798      }else if( c=='t' ){
4799        c = '\t';
4800      }else if( c=='n' ){
4801        c = '\n';
4802      }else if( c=='v' ){
4803        c = '\v';
4804      }else if( c=='f' ){
4805        c = '\f';
4806      }else if( c=='r' ){
4807        c = '\r';
4808      }else if( c=='"' ){
4809        c = '"';
4810      }else if( c=='\'' ){
4811        c = '\'';
4812      }else if( c=='\\' ){
4813        c = '\\';
4814      }else if( c>='0' && c<='7' ){
4815        c -= '0';
4816        if( z[i+1]>='0' && z[i+1]<='7' ){
4817          i++;
4818          c = (c<<3) + z[i] - '0';
4819          if( z[i+1]>='0' && z[i+1]<='7' ){
4820            i++;
4821            c = (c<<3) + z[i] - '0';
4822          }
4823        }
4824      }
4825    }
4826    z[j] = c;
4827  }
4828  if( j<i ) z[j] = 0;
4829}
4830
4831/*
4832** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4833** for TRUE and FALSE.  Return the integer value if appropriate.
4834*/
4835static int booleanValue(const char *zArg){
4836  int i;
4837  if( zArg[0]=='0' && zArg[1]=='x' ){
4838    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4839  }else{
4840    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4841  }
4842  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4843  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4844    return 1;
4845  }
4846  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4847    return 0;
4848  }
4849  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4850          zArg);
4851  return 0;
4852}
4853
4854/*
4855** Set or clear a shell flag according to a boolean value.
4856*/
4857static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4858  if( booleanValue(zArg) ){
4859    ShellSetFlag(p, mFlag);
4860  }else{
4861    ShellClearFlag(p, mFlag);
4862  }
4863}
4864
4865/*
4866** Close an output file, assuming it is not stderr or stdout
4867*/
4868static void output_file_close(FILE *f){
4869  if( f && f!=stdout && f!=stderr ) fclose(f);
4870}
4871
4872/*
4873** Try to open an output file.   The names "stdout" and "stderr" are
4874** recognized and do the right thing.  NULL is returned if the output
4875** filename is "off".
4876*/
4877static FILE *output_file_open(const char *zFile, int bTextMode){
4878  FILE *f;
4879  if( strcmp(zFile,"stdout")==0 ){
4880    f = stdout;
4881  }else if( strcmp(zFile, "stderr")==0 ){
4882    f = stderr;
4883  }else if( strcmp(zFile, "off")==0 ){
4884    f = 0;
4885  }else{
4886    f = fopen(zFile, bTextMode ? "w" : "wb");
4887    if( f==0 ){
4888      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4889    }
4890  }
4891  return f;
4892}
4893
4894#ifndef SQLITE_OMIT_TRACE
4895/*
4896** A routine for handling output from sqlite3_trace().
4897*/
4898static int sql_trace_callback(
4899  unsigned mType,         /* The trace type */
4900  void *pArg,             /* The ShellState pointer */
4901  void *pP,               /* Usually a pointer to sqlite_stmt */
4902  void *pX                /* Auxiliary output */
4903){
4904  ShellState *p = (ShellState*)pArg;
4905  sqlite3_stmt *pStmt;
4906  const char *zSql;
4907  int nSql;
4908  if( p->traceOut==0 ) return 0;
4909  if( mType==SQLITE_TRACE_CLOSE ){
4910    utf8_printf(p->traceOut, "-- closing database connection\n");
4911    return 0;
4912  }
4913  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4914    zSql = (const char*)pX;
4915  }else{
4916    pStmt = (sqlite3_stmt*)pP;
4917    switch( p->eTraceType ){
4918      case SHELL_TRACE_EXPANDED: {
4919        zSql = sqlite3_expanded_sql(pStmt);
4920        break;
4921      }
4922#ifdef SQLITE_ENABLE_NORMALIZE
4923      case SHELL_TRACE_NORMALIZED: {
4924        zSql = sqlite3_normalized_sql(pStmt);
4925        break;
4926      }
4927#endif
4928      default: {
4929        zSql = sqlite3_sql(pStmt);
4930        break;
4931      }
4932    }
4933  }
4934  if( zSql==0 ) return 0;
4935  nSql = strlen30(zSql);
4936  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4937  switch( mType ){
4938    case SQLITE_TRACE_ROW:
4939    case SQLITE_TRACE_STMT: {
4940      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4941      break;
4942    }
4943    case SQLITE_TRACE_PROFILE: {
4944      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4945      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4946      break;
4947    }
4948  }
4949  return 0;
4950}
4951#endif
4952
4953/*
4954** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4955** a useful spot to set a debugger breakpoint.
4956*/
4957static void test_breakpoint(void){
4958  static int nCall = 0;
4959  nCall++;
4960}
4961
4962/*
4963** An object used to read a CSV and other files for import.
4964*/
4965typedef struct ImportCtx ImportCtx;
4966struct ImportCtx {
4967  const char *zFile;  /* Name of the input file */
4968  FILE *in;           /* Read the CSV text from this input stream */
4969  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
4970  char *z;            /* Accumulated text for a field */
4971  int n;              /* Number of bytes in z */
4972  int nAlloc;         /* Space allocated for z[] */
4973  int nLine;          /* Current line number */
4974  int nRow;           /* Number of rows imported */
4975  int nErr;           /* Number of errors encountered */
4976  int bNotFirst;      /* True if one or more bytes already read */
4977  int cTerm;          /* Character that terminated the most recent field */
4978  int cColSep;        /* The column separator character.  (Usually ",") */
4979  int cRowSep;        /* The row separator character.  (Usually "\n") */
4980};
4981
4982/* Clean up resourced used by an ImportCtx */
4983static void import_cleanup(ImportCtx *p){
4984  if( p->in!=0 && p->xCloser!=0 ){
4985    p->xCloser(p->in);
4986    p->in = 0;
4987  }
4988  sqlite3_free(p->z);
4989  p->z = 0;
4990}
4991
4992/* Append a single byte to z[] */
4993static void import_append_char(ImportCtx *p, int c){
4994  if( p->n+1>=p->nAlloc ){
4995    p->nAlloc += p->nAlloc + 100;
4996    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4997    if( p->z==0 ) shell_out_of_memory();
4998  }
4999  p->z[p->n++] = (char)c;
5000}
5001
5002/* Read a single field of CSV text.  Compatible with rfc4180 and extended
5003** with the option of having a separator other than ",".
5004**
5005**   +  Input comes from p->in.
5006**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5007**      from sqlite3_malloc64().
5008**   +  Use p->cSep as the column separator.  The default is ",".
5009**   +  Use p->rSep as the row separator.  The default is "\n".
5010**   +  Keep track of the line number in p->nLine.
5011**   +  Store the character that terminates the field in p->cTerm.  Store
5012**      EOF on end-of-file.
5013**   +  Report syntax errors on stderr
5014*/
5015static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5016  int c;
5017  int cSep = p->cColSep;
5018  int rSep = p->cRowSep;
5019  p->n = 0;
5020  c = fgetc(p->in);
5021  if( c==EOF || seenInterrupt ){
5022    p->cTerm = EOF;
5023    return 0;
5024  }
5025  if( c=='"' ){
5026    int pc, ppc;
5027    int startLine = p->nLine;
5028    int cQuote = c;
5029    pc = ppc = 0;
5030    while( 1 ){
5031      c = fgetc(p->in);
5032      if( c==rSep ) p->nLine++;
5033      if( c==cQuote ){
5034        if( pc==cQuote ){
5035          pc = 0;
5036          continue;
5037        }
5038      }
5039      if( (c==cSep && pc==cQuote)
5040       || (c==rSep && pc==cQuote)
5041       || (c==rSep && pc=='\r' && ppc==cQuote)
5042       || (c==EOF && pc==cQuote)
5043      ){
5044        do{ p->n--; }while( p->z[p->n]!=cQuote );
5045        p->cTerm = c;
5046        break;
5047      }
5048      if( pc==cQuote && c!='\r' ){
5049        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5050                p->zFile, p->nLine, cQuote);
5051      }
5052      if( c==EOF ){
5053        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5054                p->zFile, startLine, cQuote);
5055        p->cTerm = c;
5056        break;
5057      }
5058      import_append_char(p, c);
5059      ppc = pc;
5060      pc = c;
5061    }
5062  }else{
5063    /* If this is the first field being parsed and it begins with the
5064    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5065    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5066      import_append_char(p, c);
5067      c = fgetc(p->in);
5068      if( (c&0xff)==0xbb ){
5069        import_append_char(p, c);
5070        c = fgetc(p->in);
5071        if( (c&0xff)==0xbf ){
5072          p->bNotFirst = 1;
5073          p->n = 0;
5074          return csv_read_one_field(p);
5075        }
5076      }
5077    }
5078    while( c!=EOF && c!=cSep && c!=rSep ){
5079      import_append_char(p, c);
5080      c = fgetc(p->in);
5081    }
5082    if( c==rSep ){
5083      p->nLine++;
5084      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5085    }
5086    p->cTerm = c;
5087  }
5088  if( p->z ) p->z[p->n] = 0;
5089  p->bNotFirst = 1;
5090  return p->z;
5091}
5092
5093/* Read a single field of ASCII delimited text.
5094**
5095**   +  Input comes from p->in.
5096**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5097**      from sqlite3_malloc64().
5098**   +  Use p->cSep as the column separator.  The default is "\x1F".
5099**   +  Use p->rSep as the row separator.  The default is "\x1E".
5100**   +  Keep track of the row number in p->nLine.
5101**   +  Store the character that terminates the field in p->cTerm.  Store
5102**      EOF on end-of-file.
5103**   +  Report syntax errors on stderr
5104*/
5105static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5106  int c;
5107  int cSep = p->cColSep;
5108  int rSep = p->cRowSep;
5109  p->n = 0;
5110  c = fgetc(p->in);
5111  if( c==EOF || seenInterrupt ){
5112    p->cTerm = EOF;
5113    return 0;
5114  }
5115  while( c!=EOF && c!=cSep && c!=rSep ){
5116    import_append_char(p, c);
5117    c = fgetc(p->in);
5118  }
5119  if( c==rSep ){
5120    p->nLine++;
5121  }
5122  p->cTerm = c;
5123  if( p->z ) p->z[p->n] = 0;
5124  return p->z;
5125}
5126
5127/*
5128** Try to transfer data for table zTable.  If an error is seen while
5129** moving forward, try to go backwards.  The backwards movement won't
5130** work for WITHOUT ROWID tables.
5131*/
5132static void tryToCloneData(
5133  ShellState *p,
5134  sqlite3 *newDb,
5135  const char *zTable
5136){
5137  sqlite3_stmt *pQuery = 0;
5138  sqlite3_stmt *pInsert = 0;
5139  char *zQuery = 0;
5140  char *zInsert = 0;
5141  int rc;
5142  int i, j, n;
5143  int nTable = strlen30(zTable);
5144  int k = 0;
5145  int cnt = 0;
5146  const int spinRate = 10000;
5147
5148  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5149  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5150  if( rc ){
5151    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5152            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5153            zQuery);
5154    goto end_data_xfer;
5155  }
5156  n = sqlite3_column_count(pQuery);
5157  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5158  if( zInsert==0 ) shell_out_of_memory();
5159  sqlite3_snprintf(200+nTable,zInsert,
5160                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5161  i = strlen30(zInsert);
5162  for(j=1; j<n; j++){
5163    memcpy(zInsert+i, ",?", 2);
5164    i += 2;
5165  }
5166  memcpy(zInsert+i, ");", 3);
5167  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5168  if( rc ){
5169    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5170            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5171            zQuery);
5172    goto end_data_xfer;
5173  }
5174  for(k=0; k<2; k++){
5175    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5176      for(i=0; i<n; i++){
5177        switch( sqlite3_column_type(pQuery, i) ){
5178          case SQLITE_NULL: {
5179            sqlite3_bind_null(pInsert, i+1);
5180            break;
5181          }
5182          case SQLITE_INTEGER: {
5183            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5184            break;
5185          }
5186          case SQLITE_FLOAT: {
5187            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5188            break;
5189          }
5190          case SQLITE_TEXT: {
5191            sqlite3_bind_text(pInsert, i+1,
5192                             (const char*)sqlite3_column_text(pQuery,i),
5193                             -1, SQLITE_STATIC);
5194            break;
5195          }
5196          case SQLITE_BLOB: {
5197            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5198                                            sqlite3_column_bytes(pQuery,i),
5199                                            SQLITE_STATIC);
5200            break;
5201          }
5202        }
5203      } /* End for */
5204      rc = sqlite3_step(pInsert);
5205      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5206        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5207                        sqlite3_errmsg(newDb));
5208      }
5209      sqlite3_reset(pInsert);
5210      cnt++;
5211      if( (cnt%spinRate)==0 ){
5212        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5213        fflush(stdout);
5214      }
5215    } /* End while */
5216    if( rc==SQLITE_DONE ) break;
5217    sqlite3_finalize(pQuery);
5218    sqlite3_free(zQuery);
5219    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5220                             zTable);
5221    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5222    if( rc ){
5223      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5224      break;
5225    }
5226  } /* End for(k=0...) */
5227
5228end_data_xfer:
5229  sqlite3_finalize(pQuery);
5230  sqlite3_finalize(pInsert);
5231  sqlite3_free(zQuery);
5232  sqlite3_free(zInsert);
5233}
5234
5235
5236/*
5237** Try to transfer all rows of the schema that match zWhere.  For
5238** each row, invoke xForEach() on the object defined by that row.
5239** If an error is encountered while moving forward through the
5240** sqlite_schema table, try again moving backwards.
5241*/
5242static void tryToCloneSchema(
5243  ShellState *p,
5244  sqlite3 *newDb,
5245  const char *zWhere,
5246  void (*xForEach)(ShellState*,sqlite3*,const char*)
5247){
5248  sqlite3_stmt *pQuery = 0;
5249  char *zQuery = 0;
5250  int rc;
5251  const unsigned char *zName;
5252  const unsigned char *zSql;
5253  char *zErrMsg = 0;
5254
5255  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5256                           " WHERE %s", zWhere);
5257  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5258  if( rc ){
5259    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5260                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5261                    zQuery);
5262    goto end_schema_xfer;
5263  }
5264  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5265    zName = sqlite3_column_text(pQuery, 0);
5266    zSql = sqlite3_column_text(pQuery, 1);
5267    printf("%s... ", zName); fflush(stdout);
5268    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5269    if( zErrMsg ){
5270      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5271      sqlite3_free(zErrMsg);
5272      zErrMsg = 0;
5273    }
5274    if( xForEach ){
5275      xForEach(p, newDb, (const char*)zName);
5276    }
5277    printf("done\n");
5278  }
5279  if( rc!=SQLITE_DONE ){
5280    sqlite3_finalize(pQuery);
5281    sqlite3_free(zQuery);
5282    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5283                             " WHERE %s ORDER BY rowid DESC", zWhere);
5284    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5285    if( rc ){
5286      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5287                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5288                      zQuery);
5289      goto end_schema_xfer;
5290    }
5291    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5292      zName = sqlite3_column_text(pQuery, 0);
5293      zSql = sqlite3_column_text(pQuery, 1);
5294      printf("%s... ", zName); fflush(stdout);
5295      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5296      if( zErrMsg ){
5297        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5298        sqlite3_free(zErrMsg);
5299        zErrMsg = 0;
5300      }
5301      if( xForEach ){
5302        xForEach(p, newDb, (const char*)zName);
5303      }
5304      printf("done\n");
5305    }
5306  }
5307end_schema_xfer:
5308  sqlite3_finalize(pQuery);
5309  sqlite3_free(zQuery);
5310}
5311
5312/*
5313** Open a new database file named "zNewDb".  Try to recover as much information
5314** as possible out of the main database (which might be corrupt) and write it
5315** into zNewDb.
5316*/
5317static void tryToClone(ShellState *p, const char *zNewDb){
5318  int rc;
5319  sqlite3 *newDb = 0;
5320  if( access(zNewDb,0)==0 ){
5321    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5322    return;
5323  }
5324  rc = sqlite3_open(zNewDb, &newDb);
5325  if( rc ){
5326    utf8_printf(stderr, "Cannot create output database: %s\n",
5327            sqlite3_errmsg(newDb));
5328  }else{
5329    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5330    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5331    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5332    tryToCloneSchema(p, newDb, "type!='table'", 0);
5333    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5334    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5335  }
5336  close_db(newDb);
5337}
5338
5339/*
5340** Change the output file back to stdout.
5341**
5342** If the p->doXdgOpen flag is set, that means the output was being
5343** redirected to a temporary file named by p->zTempFile.  In that case,
5344** launch start/open/xdg-open on that temporary file.
5345*/
5346static void output_reset(ShellState *p){
5347  if( p->outfile[0]=='|' ){
5348#ifndef SQLITE_OMIT_POPEN
5349    pclose(p->out);
5350#endif
5351  }else{
5352    output_file_close(p->out);
5353#ifndef SQLITE_NOHAVE_SYSTEM
5354    if( p->doXdgOpen ){
5355      const char *zXdgOpenCmd =
5356#if defined(_WIN32)
5357      "start";
5358#elif defined(__APPLE__)
5359      "open";
5360#else
5361      "xdg-open";
5362#endif
5363      char *zCmd;
5364      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5365      if( system(zCmd) ){
5366        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5367      }else{
5368        /* Give the start/open/xdg-open command some time to get
5369        ** going before we continue, and potential delete the
5370        ** p->zTempFile data file out from under it */
5371        sqlite3_sleep(2000);
5372      }
5373      sqlite3_free(zCmd);
5374      outputModePop(p);
5375      p->doXdgOpen = 0;
5376    }
5377#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5378  }
5379  p->outfile[0] = 0;
5380  p->out = stdout;
5381}
5382
5383/*
5384** Run an SQL command and return the single integer result.
5385*/
5386static int db_int(ShellState *p, const char *zSql){
5387  sqlite3_stmt *pStmt;
5388  int res = 0;
5389  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5390  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5391    res = sqlite3_column_int(pStmt,0);
5392  }
5393  sqlite3_finalize(pStmt);
5394  return res;
5395}
5396
5397/*
5398** Convert a 2-byte or 4-byte big-endian integer into a native integer
5399*/
5400static unsigned int get2byteInt(unsigned char *a){
5401  return (a[0]<<8) + a[1];
5402}
5403static unsigned int get4byteInt(unsigned char *a){
5404  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5405}
5406
5407/*
5408** Implementation of the ".dbinfo" command.
5409**
5410** Return 1 on error, 2 to exit, and 0 otherwise.
5411*/
5412static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5413  static const struct { const char *zName; int ofst; } aField[] = {
5414     { "file change counter:",  24  },
5415     { "database page count:",  28  },
5416     { "freelist page count:",  36  },
5417     { "schema cookie:",        40  },
5418     { "schema format:",        44  },
5419     { "default cache size:",   48  },
5420     { "autovacuum top root:",  52  },
5421     { "incremental vacuum:",   64  },
5422     { "text encoding:",        56  },
5423     { "user version:",         60  },
5424     { "application id:",       68  },
5425     { "software version:",     96  },
5426  };
5427  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5428     { "number of tables:",
5429       "SELECT count(*) FROM %s WHERE type='table'" },
5430     { "number of indexes:",
5431       "SELECT count(*) FROM %s WHERE type='index'" },
5432     { "number of triggers:",
5433       "SELECT count(*) FROM %s WHERE type='trigger'" },
5434     { "number of views:",
5435       "SELECT count(*) FROM %s WHERE type='view'" },
5436     { "schema size:",
5437       "SELECT total(length(sql)) FROM %s" },
5438  };
5439  int i, rc;
5440  unsigned iDataVersion;
5441  char *zSchemaTab;
5442  char *zDb = nArg>=2 ? azArg[1] : "main";
5443  sqlite3_stmt *pStmt = 0;
5444  unsigned char aHdr[100];
5445  open_db(p, 0);
5446  if( p->db==0 ) return 1;
5447  rc = sqlite3_prepare_v2(p->db,
5448             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5449             -1, &pStmt, 0);
5450  if( rc ){
5451    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5452    sqlite3_finalize(pStmt);
5453    return 1;
5454  }
5455  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5456  if( sqlite3_step(pStmt)==SQLITE_ROW
5457   && sqlite3_column_bytes(pStmt,0)>100
5458  ){
5459    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5460    sqlite3_finalize(pStmt);
5461  }else{
5462    raw_printf(stderr, "unable to read database header\n");
5463    sqlite3_finalize(pStmt);
5464    return 1;
5465  }
5466  i = get2byteInt(aHdr+16);
5467  if( i==1 ) i = 65536;
5468  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5469  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5470  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5471  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5472  for(i=0; i<ArraySize(aField); i++){
5473    int ofst = aField[i].ofst;
5474    unsigned int val = get4byteInt(aHdr + ofst);
5475    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5476    switch( ofst ){
5477      case 56: {
5478        if( val==1 ) raw_printf(p->out, " (utf8)");
5479        if( val==2 ) raw_printf(p->out, " (utf16le)");
5480        if( val==3 ) raw_printf(p->out, " (utf16be)");
5481      }
5482    }
5483    raw_printf(p->out, "\n");
5484  }
5485  if( zDb==0 ){
5486    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5487  }else if( strcmp(zDb,"temp")==0 ){
5488    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5489  }else{
5490    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5491  }
5492  for(i=0; i<ArraySize(aQuery); i++){
5493    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5494    int val = db_int(p, zSql);
5495    sqlite3_free(zSql);
5496    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5497  }
5498  sqlite3_free(zSchemaTab);
5499  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5500  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5501  return 0;
5502}
5503
5504/*
5505** Print the current sqlite3_errmsg() value to stderr and return 1.
5506*/
5507static int shellDatabaseError(sqlite3 *db){
5508  const char *zErr = sqlite3_errmsg(db);
5509  utf8_printf(stderr, "Error: %s\n", zErr);
5510  return 1;
5511}
5512
5513/*
5514** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5515** if they match and FALSE (0) if they do not match.
5516**
5517** Globbing rules:
5518**
5519**      '*'       Matches any sequence of zero or more characters.
5520**
5521**      '?'       Matches exactly one character.
5522**
5523**     [...]      Matches one character from the enclosed list of
5524**                characters.
5525**
5526**     [^...]     Matches one character not in the enclosed list.
5527**
5528**      '#'       Matches any sequence of one or more digits with an
5529**                optional + or - sign in front
5530**
5531**      ' '       Any span of whitespace matches any other span of
5532**                whitespace.
5533**
5534** Extra whitespace at the end of z[] is ignored.
5535*/
5536static int testcase_glob(const char *zGlob, const char *z){
5537  int c, c2;
5538  int invert;
5539  int seen;
5540
5541  while( (c = (*(zGlob++)))!=0 ){
5542    if( IsSpace(c) ){
5543      if( !IsSpace(*z) ) return 0;
5544      while( IsSpace(*zGlob) ) zGlob++;
5545      while( IsSpace(*z) ) z++;
5546    }else if( c=='*' ){
5547      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5548        if( c=='?' && (*(z++))==0 ) return 0;
5549      }
5550      if( c==0 ){
5551        return 1;
5552      }else if( c=='[' ){
5553        while( *z && testcase_glob(zGlob-1,z)==0 ){
5554          z++;
5555        }
5556        return (*z)!=0;
5557      }
5558      while( (c2 = (*(z++)))!=0 ){
5559        while( c2!=c ){
5560          c2 = *(z++);
5561          if( c2==0 ) return 0;
5562        }
5563        if( testcase_glob(zGlob,z) ) return 1;
5564      }
5565      return 0;
5566    }else if( c=='?' ){
5567      if( (*(z++))==0 ) return 0;
5568    }else if( c=='[' ){
5569      int prior_c = 0;
5570      seen = 0;
5571      invert = 0;
5572      c = *(z++);
5573      if( c==0 ) return 0;
5574      c2 = *(zGlob++);
5575      if( c2=='^' ){
5576        invert = 1;
5577        c2 = *(zGlob++);
5578      }
5579      if( c2==']' ){
5580        if( c==']' ) seen = 1;
5581        c2 = *(zGlob++);
5582      }
5583      while( c2 && c2!=']' ){
5584        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5585          c2 = *(zGlob++);
5586          if( c>=prior_c && c<=c2 ) seen = 1;
5587          prior_c = 0;
5588        }else{
5589          if( c==c2 ){
5590            seen = 1;
5591          }
5592          prior_c = c2;
5593        }
5594        c2 = *(zGlob++);
5595      }
5596      if( c2==0 || (seen ^ invert)==0 ) return 0;
5597    }else if( c=='#' ){
5598      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5599      if( !IsDigit(z[0]) ) return 0;
5600      z++;
5601      while( IsDigit(z[0]) ){ z++; }
5602    }else{
5603      if( c!=(*(z++)) ) return 0;
5604    }
5605  }
5606  while( IsSpace(*z) ){ z++; }
5607  return *z==0;
5608}
5609
5610
5611/*
5612** Compare the string as a command-line option with either one or two
5613** initial "-" characters.
5614*/
5615static int optionMatch(const char *zStr, const char *zOpt){
5616  if( zStr[0]!='-' ) return 0;
5617  zStr++;
5618  if( zStr[0]=='-' ) zStr++;
5619  return strcmp(zStr, zOpt)==0;
5620}
5621
5622/*
5623** Delete a file.
5624*/
5625int shellDeleteFile(const char *zFilename){
5626  int rc;
5627#ifdef _WIN32
5628  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5629  rc = _wunlink(z);
5630  sqlite3_free(z);
5631#else
5632  rc = unlink(zFilename);
5633#endif
5634  return rc;
5635}
5636
5637/*
5638** Try to delete the temporary file (if there is one) and free the
5639** memory used to hold the name of the temp file.
5640*/
5641static void clearTempFile(ShellState *p){
5642  if( p->zTempFile==0 ) return;
5643  if( p->doXdgOpen ) return;
5644  if( shellDeleteFile(p->zTempFile) ) return;
5645  sqlite3_free(p->zTempFile);
5646  p->zTempFile = 0;
5647}
5648
5649/*
5650** Create a new temp file name with the given suffix.
5651*/
5652static void newTempFile(ShellState *p, const char *zSuffix){
5653  clearTempFile(p);
5654  sqlite3_free(p->zTempFile);
5655  p->zTempFile = 0;
5656  if( p->db ){
5657    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5658  }
5659  if( p->zTempFile==0 ){
5660    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5661    ** will not work and we will need to fallback to guessing */
5662    char *zTemp;
5663    sqlite3_uint64 r;
5664    sqlite3_randomness(sizeof(r), &r);
5665    zTemp = getenv("TEMP");
5666    if( zTemp==0 ) zTemp = getenv("TMP");
5667    if( zTemp==0 ){
5668#ifdef _WIN32
5669      zTemp = "\\tmp";
5670#else
5671      zTemp = "/tmp";
5672#endif
5673    }
5674    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5675  }else{
5676    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5677  }
5678  if( p->zTempFile==0 ){
5679    raw_printf(stderr, "out of memory\n");
5680    exit(1);
5681  }
5682}
5683
5684
5685/*
5686** The implementation of SQL scalar function fkey_collate_clause(), used
5687** by the ".lint fkey-indexes" command. This scalar function is always
5688** called with four arguments - the parent table name, the parent column name,
5689** the child table name and the child column name.
5690**
5691**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5692**
5693** If either of the named tables or columns do not exist, this function
5694** returns an empty string. An empty string is also returned if both tables
5695** and columns exist but have the same default collation sequence. Or,
5696** if both exist but the default collation sequences are different, this
5697** function returns the string " COLLATE <parent-collation>", where
5698** <parent-collation> is the default collation sequence of the parent column.
5699*/
5700static void shellFkeyCollateClause(
5701  sqlite3_context *pCtx,
5702  int nVal,
5703  sqlite3_value **apVal
5704){
5705  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5706  const char *zParent;
5707  const char *zParentCol;
5708  const char *zParentSeq;
5709  const char *zChild;
5710  const char *zChildCol;
5711  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5712  int rc;
5713
5714  assert( nVal==4 );
5715  zParent = (const char*)sqlite3_value_text(apVal[0]);
5716  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5717  zChild = (const char*)sqlite3_value_text(apVal[2]);
5718  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5719
5720  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5721  rc = sqlite3_table_column_metadata(
5722      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5723  );
5724  if( rc==SQLITE_OK ){
5725    rc = sqlite3_table_column_metadata(
5726        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5727    );
5728  }
5729
5730  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5731    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5732    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5733    sqlite3_free(z);
5734  }
5735}
5736
5737
5738/*
5739** The implementation of dot-command ".lint fkey-indexes".
5740*/
5741static int lintFkeyIndexes(
5742  ShellState *pState,             /* Current shell tool state */
5743  char **azArg,                   /* Array of arguments passed to dot command */
5744  int nArg                        /* Number of entries in azArg[] */
5745){
5746  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5747  FILE *out = pState->out;        /* Stream to write non-error output to */
5748  int bVerbose = 0;               /* If -verbose is present */
5749  int bGroupByParent = 0;         /* If -groupbyparent is present */
5750  int i;                          /* To iterate through azArg[] */
5751  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5752  int rc;                         /* Return code */
5753  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5754
5755  /*
5756  ** This SELECT statement returns one row for each foreign key constraint
5757  ** in the schema of the main database. The column values are:
5758  **
5759  ** 0. The text of an SQL statement similar to:
5760  **
5761  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5762  **
5763  **    This SELECT is similar to the one that the foreign keys implementation
5764  **    needs to run internally on child tables. If there is an index that can
5765  **    be used to optimize this query, then it can also be used by the FK
5766  **    implementation to optimize DELETE or UPDATE statements on the parent
5767  **    table.
5768  **
5769  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5770  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5771  **    contains an index that can be used to optimize the query.
5772  **
5773  ** 2. Human readable text that describes the child table and columns. e.g.
5774  **
5775  **       "child_table(child_key1, child_key2)"
5776  **
5777  ** 3. Human readable text that describes the parent table and columns. e.g.
5778  **
5779  **       "parent_table(parent_key1, parent_key2)"
5780  **
5781  ** 4. A full CREATE INDEX statement for an index that could be used to
5782  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5783  **
5784  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5785  **
5786  ** 5. The name of the parent table.
5787  **
5788  ** These six values are used by the C logic below to generate the report.
5789  */
5790  const char *zSql =
5791  "SELECT "
5792    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5793    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5794    "  || fkey_collate_clause("
5795    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5796    ", "
5797    "     'SEARCH ' || s.name || ' USING COVERING INDEX*('"
5798    "  || group_concat('*=?', ' AND ') || ')'"
5799    ", "
5800    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5801    ", "
5802    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5803    ", "
5804    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5805    "  || ' ON ' || quote(s.name) || '('"
5806    "  || group_concat(quote(f.[from]) ||"
5807    "        fkey_collate_clause("
5808    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5809    "  || ');'"
5810    ", "
5811    "     f.[table] "
5812    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5813    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5814    "GROUP BY s.name, f.id "
5815    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5816  ;
5817  const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
5818
5819  for(i=2; i<nArg; i++){
5820    int n = strlen30(azArg[i]);
5821    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5822      bVerbose = 1;
5823    }
5824    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5825      bGroupByParent = 1;
5826      zIndent = "    ";
5827    }
5828    else{
5829      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5830          azArg[0], azArg[1]
5831      );
5832      return SQLITE_ERROR;
5833    }
5834  }
5835
5836  /* Register the fkey_collate_clause() SQL function */
5837  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5838      0, shellFkeyCollateClause, 0, 0
5839  );
5840
5841
5842  if( rc==SQLITE_OK ){
5843    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5844  }
5845  if( rc==SQLITE_OK ){
5846    sqlite3_bind_int(pSql, 1, bGroupByParent);
5847  }
5848
5849  if( rc==SQLITE_OK ){
5850    int rc2;
5851    char *zPrev = 0;
5852    while( SQLITE_ROW==sqlite3_step(pSql) ){
5853      int res = -1;
5854      sqlite3_stmt *pExplain = 0;
5855      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5856      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5857      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5858      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5859      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5860      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5861
5862      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5863      if( rc!=SQLITE_OK ) break;
5864      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5865        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5866        res = (
5867              0==sqlite3_strglob(zGlob, zPlan)
5868           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5869        );
5870      }
5871      rc = sqlite3_finalize(pExplain);
5872      if( rc!=SQLITE_OK ) break;
5873
5874      if( res<0 ){
5875        raw_printf(stderr, "Error: internal error");
5876        break;
5877      }else{
5878        if( bGroupByParent
5879        && (bVerbose || res==0)
5880        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5881        ){
5882          raw_printf(out, "-- Parent table %s\n", zParent);
5883          sqlite3_free(zPrev);
5884          zPrev = sqlite3_mprintf("%s", zParent);
5885        }
5886
5887        if( res==0 ){
5888          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5889        }else if( bVerbose ){
5890          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5891              zIndent, zFrom, zTarget
5892          );
5893        }
5894      }
5895    }
5896    sqlite3_free(zPrev);
5897
5898    if( rc!=SQLITE_OK ){
5899      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5900    }
5901
5902    rc2 = sqlite3_finalize(pSql);
5903    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5904      rc = rc2;
5905      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5906    }
5907  }else{
5908    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5909  }
5910
5911  return rc;
5912}
5913
5914/*
5915** Implementation of ".lint" dot command.
5916*/
5917static int lintDotCommand(
5918  ShellState *pState,             /* Current shell tool state */
5919  char **azArg,                   /* Array of arguments passed to dot command */
5920  int nArg                        /* Number of entries in azArg[] */
5921){
5922  int n;
5923  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5924  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5925  return lintFkeyIndexes(pState, azArg, nArg);
5926
5927 usage:
5928  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5929  raw_printf(stderr, "Where sub-commands are:\n");
5930  raw_printf(stderr, "    fkey-indexes\n");
5931  return SQLITE_ERROR;
5932}
5933
5934#if !defined SQLITE_OMIT_VIRTUALTABLE
5935static void shellPrepare(
5936  sqlite3 *db,
5937  int *pRc,
5938  const char *zSql,
5939  sqlite3_stmt **ppStmt
5940){
5941  *ppStmt = 0;
5942  if( *pRc==SQLITE_OK ){
5943    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5944    if( rc!=SQLITE_OK ){
5945      raw_printf(stderr, "sql error: %s (%d)\n",
5946          sqlite3_errmsg(db), sqlite3_errcode(db)
5947      );
5948      *pRc = rc;
5949    }
5950  }
5951}
5952
5953/*
5954** Create a prepared statement using printf-style arguments for the SQL.
5955**
5956** This routine is could be marked "static".  But it is not always used,
5957** depending on compile-time options.  By omitting the "static", we avoid
5958** nuisance compiler warnings about "defined but not used".
5959*/
5960void shellPreparePrintf(
5961  sqlite3 *db,
5962  int *pRc,
5963  sqlite3_stmt **ppStmt,
5964  const char *zFmt,
5965  ...
5966){
5967  *ppStmt = 0;
5968  if( *pRc==SQLITE_OK ){
5969    va_list ap;
5970    char *z;
5971    va_start(ap, zFmt);
5972    z = sqlite3_vmprintf(zFmt, ap);
5973    va_end(ap);
5974    if( z==0 ){
5975      *pRc = SQLITE_NOMEM;
5976    }else{
5977      shellPrepare(db, pRc, z, ppStmt);
5978      sqlite3_free(z);
5979    }
5980  }
5981}
5982
5983/* Finalize the prepared statement created using shellPreparePrintf().
5984**
5985** This routine is could be marked "static".  But it is not always used,
5986** depending on compile-time options.  By omitting the "static", we avoid
5987** nuisance compiler warnings about "defined but not used".
5988*/
5989void shellFinalize(
5990  int *pRc,
5991  sqlite3_stmt *pStmt
5992){
5993  if( pStmt ){
5994    sqlite3 *db = sqlite3_db_handle(pStmt);
5995    int rc = sqlite3_finalize(pStmt);
5996    if( *pRc==SQLITE_OK ){
5997      if( rc!=SQLITE_OK ){
5998        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5999      }
6000      *pRc = rc;
6001    }
6002  }
6003}
6004
6005/* Reset the prepared statement created using shellPreparePrintf().
6006**
6007** This routine is could be marked "static".  But it is not always used,
6008** depending on compile-time options.  By omitting the "static", we avoid
6009** nuisance compiler warnings about "defined but not used".
6010*/
6011void shellReset(
6012  int *pRc,
6013  sqlite3_stmt *pStmt
6014){
6015  int rc = sqlite3_reset(pStmt);
6016  if( *pRc==SQLITE_OK ){
6017    if( rc!=SQLITE_OK ){
6018      sqlite3 *db = sqlite3_db_handle(pStmt);
6019      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6020    }
6021    *pRc = rc;
6022  }
6023}
6024#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6025
6026#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6027/******************************************************************************
6028** The ".archive" or ".ar" command.
6029*/
6030/*
6031** Structure representing a single ".ar" command.
6032*/
6033typedef struct ArCommand ArCommand;
6034struct ArCommand {
6035  u8 eCmd;                        /* An AR_CMD_* value */
6036  u8 bVerbose;                    /* True if --verbose */
6037  u8 bZip;                        /* True if the archive is a ZIP */
6038  u8 bDryRun;                     /* True if --dry-run */
6039  u8 bAppend;                     /* True if --append */
6040  u8 fromCmdLine;                 /* Run from -A instead of .archive */
6041  int nArg;                       /* Number of command arguments */
6042  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6043  const char *zFile;              /* --file argument, or NULL */
6044  const char *zDir;               /* --directory argument, or NULL */
6045  char **azArg;                   /* Array of command arguments */
6046  ShellState *p;                  /* Shell state */
6047  sqlite3 *db;                    /* Database containing the archive */
6048};
6049
6050/*
6051** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6052*/
6053static int arUsage(FILE *f){
6054  showHelp(f,"archive");
6055  return SQLITE_ERROR;
6056}
6057
6058/*
6059** Print an error message for the .ar command to stderr and return
6060** SQLITE_ERROR.
6061*/
6062static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6063  va_list ap;
6064  char *z;
6065  va_start(ap, zFmt);
6066  z = sqlite3_vmprintf(zFmt, ap);
6067  va_end(ap);
6068  utf8_printf(stderr, "Error: %s\n", z);
6069  if( pAr->fromCmdLine ){
6070    utf8_printf(stderr, "Use \"-A\" for more help\n");
6071  }else{
6072    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6073  }
6074  sqlite3_free(z);
6075  return SQLITE_ERROR;
6076}
6077
6078/*
6079** Values for ArCommand.eCmd.
6080*/
6081#define AR_CMD_CREATE       1
6082#define AR_CMD_UPDATE       2
6083#define AR_CMD_INSERT       3
6084#define AR_CMD_EXTRACT      4
6085#define AR_CMD_LIST         5
6086#define AR_CMD_HELP         6
6087
6088/*
6089** Other (non-command) switches.
6090*/
6091#define AR_SWITCH_VERBOSE     7
6092#define AR_SWITCH_FILE        8
6093#define AR_SWITCH_DIRECTORY   9
6094#define AR_SWITCH_APPEND     10
6095#define AR_SWITCH_DRYRUN     11
6096
6097static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6098  switch( eSwitch ){
6099    case AR_CMD_CREATE:
6100    case AR_CMD_EXTRACT:
6101    case AR_CMD_LIST:
6102    case AR_CMD_UPDATE:
6103    case AR_CMD_INSERT:
6104    case AR_CMD_HELP:
6105      if( pAr->eCmd ){
6106        return arErrorMsg(pAr, "multiple command options");
6107      }
6108      pAr->eCmd = eSwitch;
6109      break;
6110
6111    case AR_SWITCH_DRYRUN:
6112      pAr->bDryRun = 1;
6113      break;
6114    case AR_SWITCH_VERBOSE:
6115      pAr->bVerbose = 1;
6116      break;
6117    case AR_SWITCH_APPEND:
6118      pAr->bAppend = 1;
6119      /* Fall thru into --file */
6120    case AR_SWITCH_FILE:
6121      pAr->zFile = zArg;
6122      break;
6123    case AR_SWITCH_DIRECTORY:
6124      pAr->zDir = zArg;
6125      break;
6126  }
6127
6128  return SQLITE_OK;
6129}
6130
6131/*
6132** Parse the command line for an ".ar" command. The results are written into
6133** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6134** successfully, otherwise an error message is written to stderr and
6135** SQLITE_ERROR returned.
6136*/
6137static int arParseCommand(
6138  char **azArg,                   /* Array of arguments passed to dot command */
6139  int nArg,                       /* Number of entries in azArg[] */
6140  ArCommand *pAr                  /* Populate this object */
6141){
6142  struct ArSwitch {
6143    const char *zLong;
6144    char cShort;
6145    u8 eSwitch;
6146    u8 bArg;
6147  } aSwitch[] = {
6148    { "create",    'c', AR_CMD_CREATE,       0 },
6149    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6150    { "insert",    'i', AR_CMD_INSERT,       0 },
6151    { "list",      't', AR_CMD_LIST,         0 },
6152    { "update",    'u', AR_CMD_UPDATE,       0 },
6153    { "help",      'h', AR_CMD_HELP,         0 },
6154    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6155    { "file",      'f', AR_SWITCH_FILE,      1 },
6156    { "append",    'a', AR_SWITCH_APPEND,    1 },
6157    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6158    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6159  };
6160  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6161  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6162
6163  if( nArg<=1 ){
6164    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6165    return arUsage(stderr);
6166  }else{
6167    char *z = azArg[1];
6168    if( z[0]!='-' ){
6169      /* Traditional style [tar] invocation */
6170      int i;
6171      int iArg = 2;
6172      for(i=0; z[i]; i++){
6173        const char *zArg = 0;
6174        struct ArSwitch *pOpt;
6175        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6176          if( z[i]==pOpt->cShort ) break;
6177        }
6178        if( pOpt==pEnd ){
6179          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6180        }
6181        if( pOpt->bArg ){
6182          if( iArg>=nArg ){
6183            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6184          }
6185          zArg = azArg[iArg++];
6186        }
6187        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6188      }
6189      pAr->nArg = nArg-iArg;
6190      if( pAr->nArg>0 ){
6191        pAr->azArg = &azArg[iArg];
6192      }
6193    }else{
6194      /* Non-traditional invocation */
6195      int iArg;
6196      for(iArg=1; iArg<nArg; iArg++){
6197        int n;
6198        z = azArg[iArg];
6199        if( z[0]!='-' ){
6200          /* All remaining command line words are command arguments. */
6201          pAr->azArg = &azArg[iArg];
6202          pAr->nArg = nArg-iArg;
6203          break;
6204        }
6205        n = strlen30(z);
6206
6207        if( z[1]!='-' ){
6208          int i;
6209          /* One or more short options */
6210          for(i=1; i<n; i++){
6211            const char *zArg = 0;
6212            struct ArSwitch *pOpt;
6213            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6214              if( z[i]==pOpt->cShort ) break;
6215            }
6216            if( pOpt==pEnd ){
6217              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6218            }
6219            if( pOpt->bArg ){
6220              if( i<(n-1) ){
6221                zArg = &z[i+1];
6222                i = n;
6223              }else{
6224                if( iArg>=(nArg-1) ){
6225                  return arErrorMsg(pAr, "option requires an argument: %c",
6226                                    z[i]);
6227                }
6228                zArg = azArg[++iArg];
6229              }
6230            }
6231            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6232          }
6233        }else if( z[2]=='\0' ){
6234          /* A -- option, indicating that all remaining command line words
6235          ** are command arguments.  */
6236          pAr->azArg = &azArg[iArg+1];
6237          pAr->nArg = nArg-iArg-1;
6238          break;
6239        }else{
6240          /* A long option */
6241          const char *zArg = 0;             /* Argument for option, if any */
6242          struct ArSwitch *pMatch = 0;      /* Matching option */
6243          struct ArSwitch *pOpt;            /* Iterator */
6244          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6245            const char *zLong = pOpt->zLong;
6246            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6247              if( pMatch ){
6248                return arErrorMsg(pAr, "ambiguous option: %s",z);
6249              }else{
6250                pMatch = pOpt;
6251              }
6252            }
6253          }
6254
6255          if( pMatch==0 ){
6256            return arErrorMsg(pAr, "unrecognized option: %s", z);
6257          }
6258          if( pMatch->bArg ){
6259            if( iArg>=(nArg-1) ){
6260              return arErrorMsg(pAr, "option requires an argument: %s", z);
6261            }
6262            zArg = azArg[++iArg];
6263          }
6264          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6265        }
6266      }
6267    }
6268  }
6269
6270  return SQLITE_OK;
6271}
6272
6273/*
6274** This function assumes that all arguments within the ArCommand.azArg[]
6275** array refer to archive members, as for the --extract or --list commands.
6276** It checks that each of them are present. If any specified file is not
6277** present in the archive, an error is printed to stderr and an error
6278** code returned. Otherwise, if all specified arguments are present in
6279** the archive, SQLITE_OK is returned.
6280**
6281** This function strips any trailing '/' characters from each argument.
6282** This is consistent with the way the [tar] command seems to work on
6283** Linux.
6284*/
6285static int arCheckEntries(ArCommand *pAr){
6286  int rc = SQLITE_OK;
6287  if( pAr->nArg ){
6288    int i, j;
6289    sqlite3_stmt *pTest = 0;
6290
6291    shellPreparePrintf(pAr->db, &rc, &pTest,
6292        "SELECT name FROM %s WHERE name=$name",
6293        pAr->zSrcTable
6294    );
6295    j = sqlite3_bind_parameter_index(pTest, "$name");
6296    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6297      char *z = pAr->azArg[i];
6298      int n = strlen30(z);
6299      int bOk = 0;
6300      while( n>0 && z[n-1]=='/' ) n--;
6301      z[n] = '\0';
6302      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6303      if( SQLITE_ROW==sqlite3_step(pTest) ){
6304        bOk = 1;
6305      }
6306      shellReset(&rc, pTest);
6307      if( rc==SQLITE_OK && bOk==0 ){
6308        utf8_printf(stderr, "not found in archive: %s\n", z);
6309        rc = SQLITE_ERROR;
6310      }
6311    }
6312    shellFinalize(&rc, pTest);
6313  }
6314  return rc;
6315}
6316
6317/*
6318** Format a WHERE clause that can be used against the "sqlar" table to
6319** identify all archive members that match the command arguments held
6320** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6321** The caller is responsible for eventually calling sqlite3_free() on
6322** any non-NULL (*pzWhere) value.
6323*/
6324static void arWhereClause(
6325  int *pRc,
6326  ArCommand *pAr,
6327  char **pzWhere                  /* OUT: New WHERE clause */
6328){
6329  char *zWhere = 0;
6330  if( *pRc==SQLITE_OK ){
6331    if( pAr->nArg==0 ){
6332      zWhere = sqlite3_mprintf("1");
6333    }else{
6334      int i;
6335      const char *zSep = "";
6336      for(i=0; i<pAr->nArg; i++){
6337        const char *z = pAr->azArg[i];
6338        zWhere = sqlite3_mprintf(
6339          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6340          zWhere, zSep, z, strlen30(z)+1, z
6341        );
6342        if( zWhere==0 ){
6343          *pRc = SQLITE_NOMEM;
6344          break;
6345        }
6346        zSep = " OR ";
6347      }
6348    }
6349  }
6350  *pzWhere = zWhere;
6351}
6352
6353/*
6354** Implementation of .ar "lisT" command.
6355*/
6356static int arListCommand(ArCommand *pAr){
6357  const char *zSql = "SELECT %s FROM %s WHERE %s";
6358  const char *azCols[] = {
6359    "name",
6360    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6361  };
6362
6363  char *zWhere = 0;
6364  sqlite3_stmt *pSql = 0;
6365  int rc;
6366
6367  rc = arCheckEntries(pAr);
6368  arWhereClause(&rc, pAr, &zWhere);
6369
6370  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6371                     pAr->zSrcTable, zWhere);
6372  if( pAr->bDryRun ){
6373    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6374  }else{
6375    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6376      if( pAr->bVerbose ){
6377        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6378            sqlite3_column_text(pSql, 0),
6379            sqlite3_column_int(pSql, 1),
6380            sqlite3_column_text(pSql, 2),
6381            sqlite3_column_text(pSql, 3)
6382        );
6383      }else{
6384        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6385      }
6386    }
6387  }
6388  shellFinalize(&rc, pSql);
6389  sqlite3_free(zWhere);
6390  return rc;
6391}
6392
6393
6394/*
6395** Implementation of .ar "eXtract" command.
6396*/
6397static int arExtractCommand(ArCommand *pAr){
6398  const char *zSql1 =
6399    "SELECT "
6400    " ($dir || name),"
6401    " writefile(($dir || name), %s, mode, mtime) "
6402    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6403    " AND name NOT GLOB '*..[/\\]*'";
6404
6405  const char *azExtraArg[] = {
6406    "sqlar_uncompress(data, sz)",
6407    "data"
6408  };
6409
6410  sqlite3_stmt *pSql = 0;
6411  int rc = SQLITE_OK;
6412  char *zDir = 0;
6413  char *zWhere = 0;
6414  int i, j;
6415
6416  /* If arguments are specified, check that they actually exist within
6417  ** the archive before proceeding. And formulate a WHERE clause to
6418  ** match them.  */
6419  rc = arCheckEntries(pAr);
6420  arWhereClause(&rc, pAr, &zWhere);
6421
6422  if( rc==SQLITE_OK ){
6423    if( pAr->zDir ){
6424      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6425    }else{
6426      zDir = sqlite3_mprintf("");
6427    }
6428    if( zDir==0 ) rc = SQLITE_NOMEM;
6429  }
6430
6431  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6432      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6433  );
6434
6435  if( rc==SQLITE_OK ){
6436    j = sqlite3_bind_parameter_index(pSql, "$dir");
6437    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6438
6439    /* Run the SELECT statement twice. The first time, writefile() is called
6440    ** for all archive members that should be extracted. The second time,
6441    ** only for the directories. This is because the timestamps for
6442    ** extracted directories must be reset after they are populated (as
6443    ** populating them changes the timestamp).  */
6444    for(i=0; i<2; i++){
6445      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6446      sqlite3_bind_int(pSql, j, i);
6447      if( pAr->bDryRun ){
6448        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6449      }else{
6450        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6451          if( i==0 && pAr->bVerbose ){
6452            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6453          }
6454        }
6455      }
6456      shellReset(&rc, pSql);
6457    }
6458    shellFinalize(&rc, pSql);
6459  }
6460
6461  sqlite3_free(zDir);
6462  sqlite3_free(zWhere);
6463  return rc;
6464}
6465
6466/*
6467** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6468*/
6469static int arExecSql(ArCommand *pAr, const char *zSql){
6470  int rc;
6471  if( pAr->bDryRun ){
6472    utf8_printf(pAr->p->out, "%s\n", zSql);
6473    rc = SQLITE_OK;
6474  }else{
6475    char *zErr = 0;
6476    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6477    if( zErr ){
6478      utf8_printf(stdout, "ERROR: %s\n", zErr);
6479      sqlite3_free(zErr);
6480    }
6481  }
6482  return rc;
6483}
6484
6485
6486/*
6487** Implementation of .ar "create", "insert", and "update" commands.
6488**
6489**     create    ->     Create a new SQL archive
6490**     insert    ->     Insert or reinsert all files listed
6491**     update    ->     Insert files that have changed or that were not
6492**                      previously in the archive
6493**
6494** Create the "sqlar" table in the database if it does not already exist.
6495** Then add each file in the azFile[] array to the archive. Directories
6496** are added recursively. If argument bVerbose is non-zero, a message is
6497** printed on stdout for each file archived.
6498**
6499** The create command is the same as update, except that it drops
6500** any existing "sqlar" table before beginning.  The "insert" command
6501** always overwrites every file named on the command-line, where as
6502** "update" only overwrites if the size or mtime or mode has changed.
6503*/
6504static int arCreateOrUpdateCommand(
6505  ArCommand *pAr,                 /* Command arguments and options */
6506  int bUpdate,                    /* true for a --create. */
6507  int bOnlyIfChanged              /* Only update if file has changed */
6508){
6509  const char *zCreate =
6510      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6511      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6512      "  mode INT,               -- access permissions\n"
6513      "  mtime INT,              -- last modification time\n"
6514      "  sz INT,                 -- original file size\n"
6515      "  data BLOB               -- compressed content\n"
6516      ")";
6517  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6518  const char *zInsertFmt[2] = {
6519     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6520     "  SELECT\n"
6521     "    %s,\n"
6522     "    mode,\n"
6523     "    mtime,\n"
6524     "    CASE substr(lsmode(mode),1,1)\n"
6525     "      WHEN '-' THEN length(data)\n"
6526     "      WHEN 'd' THEN 0\n"
6527     "      ELSE -1 END,\n"
6528     "    sqlar_compress(data)\n"
6529     "  FROM fsdir(%Q,%Q) AS disk\n"
6530     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6531     ,
6532     "REPLACE INTO %s(name,mode,mtime,data)\n"
6533     "  SELECT\n"
6534     "    %s,\n"
6535     "    mode,\n"
6536     "    mtime,\n"
6537     "    data\n"
6538     "  FROM fsdir(%Q,%Q) AS disk\n"
6539     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6540  };
6541  int i;                          /* For iterating through azFile[] */
6542  int rc;                         /* Return code */
6543  const char *zTab = 0;           /* SQL table into which to insert */
6544  char *zSql;
6545  char zTemp[50];
6546  char *zExists = 0;
6547
6548  arExecSql(pAr, "PRAGMA page_size=512");
6549  rc = arExecSql(pAr, "SAVEPOINT ar;");
6550  if( rc!=SQLITE_OK ) return rc;
6551  zTemp[0] = 0;
6552  if( pAr->bZip ){
6553    /* Initialize the zipfile virtual table, if necessary */
6554    if( pAr->zFile ){
6555      sqlite3_uint64 r;
6556      sqlite3_randomness(sizeof(r),&r);
6557      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6558      zTab = zTemp;
6559      zSql = sqlite3_mprintf(
6560         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6561         zTab, pAr->zFile
6562      );
6563      rc = arExecSql(pAr, zSql);
6564      sqlite3_free(zSql);
6565    }else{
6566      zTab = "zip";
6567    }
6568  }else{
6569    /* Initialize the table for an SQLAR */
6570    zTab = "sqlar";
6571    if( bUpdate==0 ){
6572      rc = arExecSql(pAr, zDrop);
6573      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6574    }
6575    rc = arExecSql(pAr, zCreate);
6576  }
6577  if( bOnlyIfChanged ){
6578    zExists = sqlite3_mprintf(
6579      " AND NOT EXISTS("
6580          "SELECT 1 FROM %s AS mem"
6581          " WHERE mem.name=disk.name"
6582          " AND mem.mtime=disk.mtime"
6583          " AND mem.mode=disk.mode)", zTab);
6584  }else{
6585    zExists = sqlite3_mprintf("");
6586  }
6587  if( zExists==0 ) rc = SQLITE_NOMEM;
6588  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6589    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6590        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6591        pAr->azArg[i], pAr->zDir, zExists);
6592    rc = arExecSql(pAr, zSql2);
6593    sqlite3_free(zSql2);
6594  }
6595end_ar_transaction:
6596  if( rc!=SQLITE_OK ){
6597    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6598  }else{
6599    rc = arExecSql(pAr, "RELEASE ar;");
6600    if( pAr->bZip && pAr->zFile ){
6601      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6602      arExecSql(pAr, zSql);
6603      sqlite3_free(zSql);
6604    }
6605  }
6606  sqlite3_free(zExists);
6607  return rc;
6608}
6609
6610/*
6611** Implementation of ".ar" dot command.
6612*/
6613static int arDotCommand(
6614  ShellState *pState,          /* Current shell tool state */
6615  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6616  char **azArg,                /* Array of arguments passed to dot command */
6617  int nArg                     /* Number of entries in azArg[] */
6618){
6619  ArCommand cmd;
6620  int rc;
6621  memset(&cmd, 0, sizeof(cmd));
6622  cmd.fromCmdLine = fromCmdLine;
6623  rc = arParseCommand(azArg, nArg, &cmd);
6624  if( rc==SQLITE_OK ){
6625    int eDbType = SHELL_OPEN_UNSPEC;
6626    cmd.p = pState;
6627    cmd.db = pState->db;
6628    if( cmd.zFile ){
6629      eDbType = deduceDatabaseType(cmd.zFile, 1);
6630    }else{
6631      eDbType = pState->openMode;
6632    }
6633    if( eDbType==SHELL_OPEN_ZIPFILE ){
6634      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6635        if( cmd.zFile==0 ){
6636          cmd.zSrcTable = sqlite3_mprintf("zip");
6637        }else{
6638          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6639        }
6640      }
6641      cmd.bZip = 1;
6642    }else if( cmd.zFile ){
6643      int flags;
6644      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6645      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6646           || cmd.eCmd==AR_CMD_UPDATE ){
6647        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6648      }else{
6649        flags = SQLITE_OPEN_READONLY;
6650      }
6651      cmd.db = 0;
6652      if( cmd.bDryRun ){
6653        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6654             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6655      }
6656      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6657             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6658      if( rc!=SQLITE_OK ){
6659        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6660            cmd.zFile, sqlite3_errmsg(cmd.db)
6661        );
6662        goto end_ar_command;
6663      }
6664      sqlite3_fileio_init(cmd.db, 0, 0);
6665      sqlite3_sqlar_init(cmd.db, 0, 0);
6666      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6667                              shellPutsFunc, 0, 0);
6668
6669    }
6670    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6671      if( cmd.eCmd!=AR_CMD_CREATE
6672       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6673      ){
6674        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6675        rc = SQLITE_ERROR;
6676        goto end_ar_command;
6677      }
6678      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6679    }
6680
6681    switch( cmd.eCmd ){
6682      case AR_CMD_CREATE:
6683        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6684        break;
6685
6686      case AR_CMD_EXTRACT:
6687        rc = arExtractCommand(&cmd);
6688        break;
6689
6690      case AR_CMD_LIST:
6691        rc = arListCommand(&cmd);
6692        break;
6693
6694      case AR_CMD_HELP:
6695        arUsage(pState->out);
6696        break;
6697
6698      case AR_CMD_INSERT:
6699        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6700        break;
6701
6702      default:
6703        assert( cmd.eCmd==AR_CMD_UPDATE );
6704        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6705        break;
6706    }
6707  }
6708end_ar_command:
6709  if( cmd.db!=pState->db ){
6710    close_db(cmd.db);
6711  }
6712  sqlite3_free(cmd.zSrcTable);
6713
6714  return rc;
6715}
6716/* End of the ".archive" or ".ar" command logic
6717*******************************************************************************/
6718#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6719
6720#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6721/*
6722** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6723** Otherwise, the SQL statement or statements in zSql are executed using
6724** database connection db and the error code written to *pRc before
6725** this function returns.
6726*/
6727static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6728  int rc = *pRc;
6729  if( rc==SQLITE_OK ){
6730    char *zErr = 0;
6731    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6732    if( rc!=SQLITE_OK ){
6733      raw_printf(stderr, "SQL error: %s\n", zErr);
6734    }
6735    sqlite3_free(zErr);
6736    *pRc = rc;
6737  }
6738}
6739
6740/*
6741** Like shellExec(), except that zFmt is a printf() style format string.
6742*/
6743static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6744  char *z = 0;
6745  if( *pRc==SQLITE_OK ){
6746    va_list ap;
6747    va_start(ap, zFmt);
6748    z = sqlite3_vmprintf(zFmt, ap);
6749    va_end(ap);
6750    if( z==0 ){
6751      *pRc = SQLITE_NOMEM;
6752    }else{
6753      shellExec(db, pRc, z);
6754    }
6755    sqlite3_free(z);
6756  }
6757}
6758
6759/*
6760** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6761** Otherwise, an attempt is made to allocate, zero and return a pointer
6762** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6763** to SQLITE_NOMEM and NULL returned.
6764*/
6765static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6766  void *pRet = 0;
6767  if( *pRc==SQLITE_OK ){
6768    pRet = sqlite3_malloc64(nByte);
6769    if( pRet==0 ){
6770      *pRc = SQLITE_NOMEM;
6771    }else{
6772      memset(pRet, 0, nByte);
6773    }
6774  }
6775  return pRet;
6776}
6777
6778/*
6779** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6780** Otherwise, zFmt is treated as a printf() style string. The result of
6781** formatting it along with any trailing arguments is written into a
6782** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6783** It is the responsibility of the caller to eventually free this buffer
6784** using a call to sqlite3_free().
6785**
6786** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6787** pointer returned.
6788*/
6789static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6790  char *z = 0;
6791  if( *pRc==SQLITE_OK ){
6792    va_list ap;
6793    va_start(ap, zFmt);
6794    z = sqlite3_vmprintf(zFmt, ap);
6795    va_end(ap);
6796    if( z==0 ){
6797      *pRc = SQLITE_NOMEM;
6798    }
6799  }
6800  return z;
6801}
6802
6803/*
6804** When running the ".recover" command, each output table, and the special
6805** orphaned row table if it is required, is represented by an instance
6806** of the following struct.
6807*/
6808typedef struct RecoverTable RecoverTable;
6809struct RecoverTable {
6810  char *zQuoted;                  /* Quoted version of table name */
6811  int nCol;                       /* Number of columns in table */
6812  char **azlCol;                  /* Array of column lists */
6813  int iPk;                        /* Index of IPK column */
6814};
6815
6816/*
6817** Free a RecoverTable object allocated by recoverFindTable() or
6818** recoverOrphanTable().
6819*/
6820static void recoverFreeTable(RecoverTable *pTab){
6821  if( pTab ){
6822    sqlite3_free(pTab->zQuoted);
6823    if( pTab->azlCol ){
6824      int i;
6825      for(i=0; i<=pTab->nCol; i++){
6826        sqlite3_free(pTab->azlCol[i]);
6827      }
6828      sqlite3_free(pTab->azlCol);
6829    }
6830    sqlite3_free(pTab);
6831  }
6832}
6833
6834/*
6835** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6836** Otherwise, it allocates and returns a RecoverTable object based on the
6837** final four arguments passed to this function. It is the responsibility
6838** of the caller to eventually free the returned object using
6839** recoverFreeTable().
6840*/
6841static RecoverTable *recoverNewTable(
6842  int *pRc,                       /* IN/OUT: Error code */
6843  const char *zName,              /* Name of table */
6844  const char *zSql,               /* CREATE TABLE statement */
6845  int bIntkey,
6846  int nCol
6847){
6848  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6849  int rc = *pRc;
6850  RecoverTable *pTab = 0;
6851
6852  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6853  if( rc==SQLITE_OK ){
6854    int nSqlCol = 0;
6855    int bSqlIntkey = 0;
6856    sqlite3_stmt *pStmt = 0;
6857
6858    rc = sqlite3_open("", &dbtmp);
6859    if( rc==SQLITE_OK ){
6860      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6861                              shellIdQuote, 0, 0);
6862    }
6863    if( rc==SQLITE_OK ){
6864      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6865    }
6866    if( rc==SQLITE_OK ){
6867      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6868      if( rc==SQLITE_ERROR ){
6869        rc = SQLITE_OK;
6870        goto finished;
6871      }
6872    }
6873    shellPreparePrintf(dbtmp, &rc, &pStmt,
6874        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6875    );
6876    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6877      nSqlCol = sqlite3_column_int(pStmt, 0);
6878    }
6879    shellFinalize(&rc, pStmt);
6880
6881    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6882      goto finished;
6883    }
6884
6885    shellPreparePrintf(dbtmp, &rc, &pStmt,
6886      "SELECT ("
6887      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6888      ") FROM sqlite_schema WHERE name = %Q", zName
6889    );
6890    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6891      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6892    }
6893    shellFinalize(&rc, pStmt);
6894
6895    if( bIntkey==bSqlIntkey ){
6896      int i;
6897      const char *zPk = "_rowid_";
6898      sqlite3_stmt *pPkFinder = 0;
6899
6900      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6901      ** set zPk to the name of the PK column, and pTab->iPk to the index
6902      ** of the column, where columns are 0-numbered from left to right.
6903      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6904      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6905      pTab->iPk = -2;
6906      if( bIntkey ){
6907        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6908          "SELECT cid, name FROM pragma_table_info(%Q) "
6909          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6910          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6911          , zName, zName
6912        );
6913        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6914          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6915          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6916        }
6917      }
6918
6919      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6920      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6921      pTab->nCol = nSqlCol;
6922
6923      if( bIntkey ){
6924        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6925      }else{
6926        pTab->azlCol[0] = shellMPrintf(&rc, "");
6927      }
6928      i = 1;
6929      shellPreparePrintf(dbtmp, &rc, &pStmt,
6930          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6931          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6932          "FROM pragma_table_info(%Q)",
6933          bIntkey ? ", " : "", pTab->iPk,
6934          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6935          zName
6936      );
6937      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6938        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6939        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6940        i++;
6941      }
6942      shellFinalize(&rc, pStmt);
6943
6944      shellFinalize(&rc, pPkFinder);
6945    }
6946  }
6947
6948 finished:
6949  sqlite3_close(dbtmp);
6950  *pRc = rc;
6951  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6952    recoverFreeTable(pTab);
6953    pTab = 0;
6954  }
6955  return pTab;
6956}
6957
6958/*
6959** This function is called to search the schema recovered from the
6960** sqlite_schema table of the (possibly) corrupt database as part
6961** of a ".recover" command. Specifically, for a table with root page
6962** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6963** table must be a WITHOUT ROWID table, or if non-zero, not one of
6964** those.
6965**
6966** If a table is found, a (RecoverTable*) object is returned. Or, if
6967** no such table is found, but bIntkey is false and iRoot is the
6968** root page of an index in the recovered schema, then (*pbNoop) is
6969** set to true and NULL returned. Or, if there is no such table or
6970** index, NULL is returned and (*pbNoop) set to 0, indicating that
6971** the caller should write data to the orphans table.
6972*/
6973static RecoverTable *recoverFindTable(
6974  ShellState *pState,             /* Shell state object */
6975  int *pRc,                       /* IN/OUT: Error code */
6976  int iRoot,                      /* Root page of table */
6977  int bIntkey,                    /* True for an intkey table */
6978  int nCol,                       /* Number of columns in table */
6979  int *pbNoop                     /* OUT: True if iRoot is root of index */
6980){
6981  sqlite3_stmt *pStmt = 0;
6982  RecoverTable *pRet = 0;
6983  int bNoop = 0;
6984  const char *zSql = 0;
6985  const char *zName = 0;
6986
6987  /* Search the recovered schema for an object with root page iRoot. */
6988  shellPreparePrintf(pState->db, pRc, &pStmt,
6989      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6990  );
6991  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6992    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6993    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6994      bNoop = 1;
6995      break;
6996    }
6997    if( sqlite3_stricmp(zType, "table")==0 ){
6998      zName = (const char*)sqlite3_column_text(pStmt, 1);
6999      zSql = (const char*)sqlite3_column_text(pStmt, 2);
7000      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
7001      break;
7002    }
7003  }
7004
7005  shellFinalize(pRc, pStmt);
7006  *pbNoop = bNoop;
7007  return pRet;
7008}
7009
7010/*
7011** Return a RecoverTable object representing the orphans table.
7012*/
7013static RecoverTable *recoverOrphanTable(
7014  ShellState *pState,             /* Shell state object */
7015  int *pRc,                       /* IN/OUT: Error code */
7016  const char *zLostAndFound,      /* Base name for orphans table */
7017  int nCol                        /* Number of user data columns */
7018){
7019  RecoverTable *pTab = 0;
7020  if( nCol>=0 && *pRc==SQLITE_OK ){
7021    int i;
7022
7023    /* This block determines the name of the orphan table. The prefered
7024    ** name is zLostAndFound. But if that clashes with another name
7025    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
7026    ** and so on until a non-clashing name is found.  */
7027    int iTab = 0;
7028    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
7029    sqlite3_stmt *pTest = 0;
7030    shellPrepare(pState->db, pRc,
7031        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
7032    );
7033    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7034    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
7035      shellReset(pRc, pTest);
7036      sqlite3_free(zTab);
7037      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
7038      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
7039    }
7040    shellFinalize(pRc, pTest);
7041
7042    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7043    if( pTab ){
7044      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7045      pTab->nCol = nCol;
7046      pTab->iPk = -2;
7047      if( nCol>0 ){
7048        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7049        if( pTab->azlCol ){
7050          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7051          for(i=nCol-1; i>=0; i--){
7052            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7053          }
7054        }
7055      }
7056
7057      if( *pRc!=SQLITE_OK ){
7058        recoverFreeTable(pTab);
7059        pTab = 0;
7060      }else{
7061        raw_printf(pState->out,
7062            "CREATE TABLE %s(rootpgno INTEGER, "
7063            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7064        );
7065        for(i=0; i<nCol; i++){
7066          raw_printf(pState->out, ", c%d", i);
7067        }
7068        raw_printf(pState->out, ");\n");
7069      }
7070    }
7071    sqlite3_free(zTab);
7072  }
7073  return pTab;
7074}
7075
7076/*
7077** This function is called to recover data from the database. A script
7078** to construct a new database containing all recovered data is output
7079** on stream pState->out.
7080*/
7081static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7082  int rc = SQLITE_OK;
7083  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7084  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7085  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7086  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7087  const char *zLostAndFound = "lost_and_found";
7088  int i;
7089  int nOrphan = -1;
7090  RecoverTable *pOrphan = 0;
7091
7092  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7093  int bRowids = 1;                /* 0 if --no-rowids */
7094  for(i=1; i<nArg; i++){
7095    char *z = azArg[i];
7096    int n;
7097    if( z[0]=='-' && z[1]=='-' ) z++;
7098    n = strlen30(z);
7099    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7100      bFreelist = 0;
7101    }else
7102    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7103      i++;
7104      zRecoveryDb = azArg[i];
7105    }else
7106    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7107      i++;
7108      zLostAndFound = azArg[i];
7109    }else
7110    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7111      bRowids = 0;
7112    }
7113    else{
7114      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7115      showHelp(pState->out, azArg[0]);
7116      return 1;
7117    }
7118  }
7119
7120  shellExecPrintf(pState->db, &rc,
7121    /* Attach an in-memory database named 'recovery'. Create an indexed
7122    ** cache of the sqlite_dbptr virtual table. */
7123    "PRAGMA writable_schema = on;"
7124    "ATTACH %Q AS recovery;"
7125    "DROP TABLE IF EXISTS recovery.dbptr;"
7126    "DROP TABLE IF EXISTS recovery.freelist;"
7127    "DROP TABLE IF EXISTS recovery.map;"
7128    "DROP TABLE IF EXISTS recovery.schema;"
7129    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7130  );
7131
7132  if( bFreelist ){
7133    shellExec(pState->db, &rc,
7134      "WITH trunk(pgno) AS ("
7135      "  SELECT shell_int32("
7136      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7137      "      WHERE x>0"
7138      "    UNION"
7139      "  SELECT shell_int32("
7140      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7141      "      FROM trunk WHERE x>0"
7142      "),"
7143      "freelist(data, n, freepgno) AS ("
7144      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7145      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7146      "    UNION ALL"
7147      "  SELECT data, n-1, shell_int32(data, 2+n) "
7148      "      FROM freelist WHERE n>=0"
7149      ")"
7150      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7151    );
7152  }
7153
7154  /* If this is an auto-vacuum database, add all pointer-map pages to
7155  ** the freelist table. Do this regardless of whether or not
7156  ** --freelist-corrupt was specified.  */
7157  shellExec(pState->db, &rc,
7158    "WITH ptrmap(pgno) AS ("
7159    "  SELECT 2 WHERE shell_int32("
7160    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7161    "  )"
7162    "    UNION ALL "
7163    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7164    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7165    ")"
7166    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7167  );
7168
7169  shellExec(pState->db, &rc,
7170    "CREATE TABLE recovery.dbptr("
7171    "      pgno, child, PRIMARY KEY(child, pgno)"
7172    ") WITHOUT ROWID;"
7173    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7174    "    SELECT * FROM sqlite_dbptr"
7175    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7176
7177    /* Delete any pointer to page 1. This ensures that page 1 is considered
7178    ** a root page, regardless of how corrupt the db is. */
7179    "DELETE FROM recovery.dbptr WHERE child = 1;"
7180
7181    /* Delete all pointers to any pages that have more than one pointer
7182    ** to them. Such pages will be treated as root pages when recovering
7183    ** data.  */
7184    "DELETE FROM recovery.dbptr WHERE child IN ("
7185    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7186    ");"
7187
7188    /* Create the "map" table that will (eventually) contain instructions
7189    ** for dealing with each page in the db that contains one or more
7190    ** records. */
7191    "CREATE TABLE recovery.map("
7192      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7193    ");"
7194
7195    /* Populate table [map]. If there are circular loops of pages in the
7196    ** database, the following adds all pages in such a loop to the map
7197    ** as individual root pages. This could be handled better.  */
7198    "WITH pages(i, maxlen) AS ("
7199    "  SELECT page_count, ("
7200    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7201    "  ) FROM pragma_page_count WHERE page_count>0"
7202    "    UNION ALL"
7203    "  SELECT i-1, ("
7204    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7205    "  ) FROM pages WHERE i>=2"
7206    ")"
7207    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7208    "  SELECT i, maxlen, NULL, ("
7209    "    WITH p(orig, pgno, parent) AS ("
7210    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7211    "        UNION "
7212    "      SELECT i, p.parent, "
7213    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7214    "    )"
7215    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7216    ") "
7217    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7218    "UPDATE recovery.map AS o SET intkey = ("
7219    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7220    ");"
7221
7222    /* Extract data from page 1 and any linked pages into table
7223    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7224    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7225    "INSERT INTO recovery.schema SELECT "
7226    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7227    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7228    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7229    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7230    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7231    "FROM sqlite_dbdata WHERE pgno IN ("
7232    "  SELECT pgno FROM recovery.map WHERE root=1"
7233    ")"
7234    "GROUP BY pgno, cell;"
7235    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7236  );
7237
7238  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7239  ** CREATE TABLE statements that extracted from the existing schema.  */
7240  if( rc==SQLITE_OK ){
7241    sqlite3_stmt *pStmt = 0;
7242    /* ".recover" might output content in an order which causes immediate
7243    ** foreign key constraints to be violated. So disable foreign-key
7244    ** constraint enforcement to prevent problems when running the output
7245    ** script. */
7246    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7247    raw_printf(pState->out, "BEGIN;\n");
7248    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7249    shellPrepare(pState->db, &rc,
7250        "SELECT sql FROM recovery.schema "
7251        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7252    );
7253    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7254      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7255      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7256          &zCreateTable[12]
7257      );
7258    }
7259    shellFinalize(&rc, pStmt);
7260  }
7261
7262  /* Figure out if an orphan table will be required. And if so, how many
7263  ** user columns it should contain */
7264  shellPrepare(pState->db, &rc,
7265      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7266      , &pLoop
7267  );
7268  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7269    nOrphan = sqlite3_column_int(pLoop, 0);
7270  }
7271  shellFinalize(&rc, pLoop);
7272  pLoop = 0;
7273
7274  shellPrepare(pState->db, &rc,
7275      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7276  );
7277
7278  shellPrepare(pState->db, &rc,
7279      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7280      "(case when (? AND field<0) then NULL else value end)"
7281      "), ', ')"
7282      ", min(field) "
7283      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7284      "GROUP BY cell", &pCells
7285  );
7286
7287  /* Loop through each root page. */
7288  shellPrepare(pState->db, &rc,
7289      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7290      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7291      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7292      ")", &pLoop
7293  );
7294  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7295    int iRoot = sqlite3_column_int(pLoop, 0);
7296    int bIntkey = sqlite3_column_int(pLoop, 1);
7297    int nCol = sqlite3_column_int(pLoop, 2);
7298    int bNoop = 0;
7299    RecoverTable *pTab;
7300
7301    assert( bIntkey==0 || bIntkey==1 );
7302    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7303    if( bNoop || rc ) continue;
7304    if( pTab==0 ){
7305      if( pOrphan==0 ){
7306        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7307      }
7308      pTab = pOrphan;
7309      if( pTab==0 ) break;
7310    }
7311
7312    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7313      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7314    }
7315    sqlite3_bind_int(pPages, 1, iRoot);
7316    if( bRowids==0 && pTab->iPk<0 ){
7317      sqlite3_bind_int(pCells, 1, 1);
7318    }else{
7319      sqlite3_bind_int(pCells, 1, 0);
7320    }
7321    sqlite3_bind_int(pCells, 3, pTab->iPk);
7322
7323    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7324      int iPgno = sqlite3_column_int(pPages, 0);
7325      sqlite3_bind_int(pCells, 2, iPgno);
7326      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7327        int nField = sqlite3_column_int(pCells, 0);
7328        int iMin = sqlite3_column_int(pCells, 2);
7329        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7330
7331        RecoverTable *pTab2 = pTab;
7332        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7333          if( pOrphan==0 ){
7334            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7335          }
7336          pTab2 = pOrphan;
7337          if( pTab2==0 ) break;
7338        }
7339
7340        nField = nField+1;
7341        if( pTab2==pOrphan ){
7342          raw_printf(pState->out,
7343              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7344              pTab2->zQuoted, iRoot, iPgno, nField,
7345              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7346          );
7347        }else{
7348          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7349              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7350          );
7351        }
7352      }
7353      shellReset(&rc, pCells);
7354    }
7355    shellReset(&rc, pPages);
7356    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7357  }
7358  shellFinalize(&rc, pLoop);
7359  shellFinalize(&rc, pPages);
7360  shellFinalize(&rc, pCells);
7361  recoverFreeTable(pOrphan);
7362
7363  /* The rest of the schema */
7364  if( rc==SQLITE_OK ){
7365    sqlite3_stmt *pStmt = 0;
7366    shellPrepare(pState->db, &rc,
7367        "SELECT sql, name FROM recovery.schema "
7368        "WHERE sql NOT LIKE 'create table%'", &pStmt
7369    );
7370    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7371      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7372      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7373        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7374        char *zPrint = shellMPrintf(&rc,
7375          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7376          zName, zName, zSql
7377        );
7378        raw_printf(pState->out, "%s;\n", zPrint);
7379        sqlite3_free(zPrint);
7380      }else{
7381        raw_printf(pState->out, "%s;\n", zSql);
7382      }
7383    }
7384    shellFinalize(&rc, pStmt);
7385  }
7386
7387  if( rc==SQLITE_OK ){
7388    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7389    raw_printf(pState->out, "COMMIT;\n");
7390  }
7391  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7392  return rc;
7393}
7394#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7395
7396
7397/*
7398** If an input line begins with "." then invoke this routine to
7399** process that line.
7400**
7401** Return 1 on error, 2 to exit, and 0 otherwise.
7402*/
7403static int do_meta_command(char *zLine, ShellState *p){
7404  int h = 1;
7405  int nArg = 0;
7406  int n, c;
7407  int rc = 0;
7408  char *azArg[52];
7409
7410#ifndef SQLITE_OMIT_VIRTUALTABLE
7411  if( p->expert.pExpert ){
7412    expertFinish(p, 1, 0);
7413  }
7414#endif
7415
7416  /* Parse the input line into tokens.
7417  */
7418  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7419    while( IsSpace(zLine[h]) ){ h++; }
7420    if( zLine[h]==0 ) break;
7421    if( zLine[h]=='\'' || zLine[h]=='"' ){
7422      int delim = zLine[h++];
7423      azArg[nArg++] = &zLine[h];
7424      while( zLine[h] && zLine[h]!=delim ){
7425        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7426        h++;
7427      }
7428      if( zLine[h]==delim ){
7429        zLine[h++] = 0;
7430      }
7431      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7432    }else{
7433      azArg[nArg++] = &zLine[h];
7434      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7435      if( zLine[h] ) zLine[h++] = 0;
7436      resolve_backslashes(azArg[nArg-1]);
7437    }
7438  }
7439  azArg[nArg] = 0;
7440
7441  /* Process the input line.
7442  */
7443  if( nArg==0 ) return 0; /* no tokens, no error */
7444  n = strlen30(azArg[0]);
7445  c = azArg[0][0];
7446  clearTempFile(p);
7447
7448#ifndef SQLITE_OMIT_AUTHORIZATION
7449  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7450    if( nArg!=2 ){
7451      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7452      rc = 1;
7453      goto meta_command_exit;
7454    }
7455    open_db(p, 0);
7456    if( booleanValue(azArg[1]) ){
7457      sqlite3_set_authorizer(p->db, shellAuth, p);
7458    }else{
7459      sqlite3_set_authorizer(p->db, 0, 0);
7460    }
7461  }else
7462#endif
7463
7464#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7465  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7466    open_db(p, 0);
7467    rc = arDotCommand(p, 0, azArg, nArg);
7468  }else
7469#endif
7470
7471  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7472   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7473  ){
7474    const char *zDestFile = 0;
7475    const char *zDb = 0;
7476    sqlite3 *pDest;
7477    sqlite3_backup *pBackup;
7478    int j;
7479    int bAsync = 0;
7480    const char *zVfs = 0;
7481    for(j=1; j<nArg; j++){
7482      const char *z = azArg[j];
7483      if( z[0]=='-' ){
7484        if( z[1]=='-' ) z++;
7485        if( strcmp(z, "-append")==0 ){
7486          zVfs = "apndvfs";
7487        }else
7488        if( strcmp(z, "-async")==0 ){
7489          bAsync = 1;
7490        }else
7491        {
7492          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7493          return 1;
7494        }
7495      }else if( zDestFile==0 ){
7496        zDestFile = azArg[j];
7497      }else if( zDb==0 ){
7498        zDb = zDestFile;
7499        zDestFile = azArg[j];
7500      }else{
7501        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7502        return 1;
7503      }
7504    }
7505    if( zDestFile==0 ){
7506      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7507      return 1;
7508    }
7509    if( zDb==0 ) zDb = "main";
7510    rc = sqlite3_open_v2(zDestFile, &pDest,
7511                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7512    if( rc!=SQLITE_OK ){
7513      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7514      close_db(pDest);
7515      return 1;
7516    }
7517    if( bAsync ){
7518      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7519                   0, 0, 0);
7520    }
7521    open_db(p, 0);
7522    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7523    if( pBackup==0 ){
7524      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7525      close_db(pDest);
7526      return 1;
7527    }
7528    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7529    sqlite3_backup_finish(pBackup);
7530    if( rc==SQLITE_DONE ){
7531      rc = 0;
7532    }else{
7533      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7534      rc = 1;
7535    }
7536    close_db(pDest);
7537  }else
7538
7539  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7540    if( nArg==2 ){
7541      bail_on_error = booleanValue(azArg[1]);
7542    }else{
7543      raw_printf(stderr, "Usage: .bail on|off\n");
7544      rc = 1;
7545    }
7546  }else
7547
7548  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7549    if( nArg==2 ){
7550      if( booleanValue(azArg[1]) ){
7551        setBinaryMode(p->out, 1);
7552      }else{
7553        setTextMode(p->out, 1);
7554      }
7555    }else{
7556      raw_printf(stderr, "Usage: .binary on|off\n");
7557      rc = 1;
7558    }
7559  }else
7560
7561  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7562    if( nArg==2 ){
7563#if defined(_WIN32) || defined(WIN32)
7564      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7565      rc = !SetCurrentDirectoryW(z);
7566      sqlite3_free(z);
7567#else
7568      rc = chdir(azArg[1]);
7569#endif
7570      if( rc ){
7571        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7572        rc = 1;
7573      }
7574    }else{
7575      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7576      rc = 1;
7577    }
7578  }else
7579
7580  /* The undocumented ".breakpoint" command causes a call to the no-op
7581  ** routine named test_breakpoint().
7582  */
7583  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7584    test_breakpoint();
7585  }else
7586
7587  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7588    if( nArg==2 ){
7589      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7590    }else{
7591      raw_printf(stderr, "Usage: .changes on|off\n");
7592      rc = 1;
7593    }
7594  }else
7595
7596  /* Cancel output redirection, if it is currently set (by .testcase)
7597  ** Then read the content of the testcase-out.txt file and compare against
7598  ** azArg[1].  If there are differences, report an error and exit.
7599  */
7600  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7601    char *zRes = 0;
7602    output_reset(p);
7603    if( nArg!=2 ){
7604      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7605      rc = 2;
7606    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7607      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7608      rc = 2;
7609    }else if( testcase_glob(azArg[1],zRes)==0 ){
7610      utf8_printf(stderr,
7611                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7612                 p->zTestcase, azArg[1], zRes);
7613      rc = 1;
7614    }else{
7615      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7616      p->nCheck++;
7617    }
7618    sqlite3_free(zRes);
7619  }else
7620
7621  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7622    if( nArg==2 ){
7623      tryToClone(p, azArg[1]);
7624    }else{
7625      raw_printf(stderr, "Usage: .clone FILENAME\n");
7626      rc = 1;
7627    }
7628  }else
7629
7630  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7631    char **azName = 0;
7632    int nName = 0;
7633    sqlite3_stmt *pStmt;
7634    int i;
7635    open_db(p, 0);
7636    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7637    if( rc ){
7638      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7639      rc = 1;
7640    }else{
7641      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7642        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7643        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7644        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7645        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7646        azName[nName*2] = strdup(zSchema);
7647        azName[nName*2+1] = strdup(zFile);
7648        nName++;
7649      }
7650    }
7651    sqlite3_finalize(pStmt);
7652    for(i=0; i<nName; i++){
7653      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7654      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7655      const char *z = azName[i*2+1];
7656      utf8_printf(p->out, "%s: %s %s%s\n",
7657         azName[i*2],
7658         z && z[0] ? z : "\"\"",
7659         bRdonly ? "r/o" : "r/w",
7660         eTxn==SQLITE_TXN_NONE ? "" :
7661            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7662      free(azName[i*2]);
7663      free(azName[i*2+1]);
7664    }
7665    sqlite3_free(azName);
7666  }else
7667
7668  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7669    static const struct DbConfigChoices {
7670      const char *zName;
7671      int op;
7672    } aDbConfig[] = {
7673        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7674        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7675        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7676        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7677        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7678        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7679        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7680        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7681        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7682        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7683        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7684        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7685        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7686        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7687        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7688        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7689    };
7690    int ii, v;
7691    open_db(p, 0);
7692    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7693      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7694      if( nArg>=3 ){
7695        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7696      }
7697      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7698      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7699      if( nArg>1 ) break;
7700    }
7701    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7702      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7703      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7704    }
7705  }else
7706
7707  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7708    rc = shell_dbinfo_command(p, nArg, azArg);
7709  }else
7710
7711#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7712  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7713    open_db(p, 0);
7714    rc = recoverDatabaseCmd(p, nArg, azArg);
7715  }else
7716#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7717
7718  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7719    char *zLike = 0;
7720    char *zSql;
7721    int i;
7722    int savedShowHeader = p->showHeader;
7723    int savedShellFlags = p->shellFlgs;
7724    ShellClearFlag(p,
7725       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
7726       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
7727    for(i=1; i<nArg; i++){
7728      if( azArg[i][0]=='-' ){
7729        const char *z = azArg[i]+1;
7730        if( z[0]=='-' ) z++;
7731        if( strcmp(z,"preserve-rowids")==0 ){
7732#ifdef SQLITE_OMIT_VIRTUALTABLE
7733          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7734                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7735          rc = 1;
7736          sqlite3_free(zLike);
7737          goto meta_command_exit;
7738#else
7739          ShellSetFlag(p, SHFLG_PreserveRowid);
7740#endif
7741        }else
7742        if( strcmp(z,"newlines")==0 ){
7743          ShellSetFlag(p, SHFLG_Newlines);
7744        }else
7745        if( strcmp(z,"data-only")==0 ){
7746          ShellSetFlag(p, SHFLG_DumpDataOnly);
7747        }else
7748        if( strcmp(z,"nosys")==0 ){
7749          ShellSetFlag(p, SHFLG_DumpNoSys);
7750        }else
7751        {
7752          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7753          rc = 1;
7754          sqlite3_free(zLike);
7755          goto meta_command_exit;
7756        }
7757      }else{
7758        /* azArg[i] contains a LIKE pattern. This ".dump" request should
7759        ** only dump data for tables for which either the table name matches
7760        ** the LIKE pattern, or the table appears to be a shadow table of
7761        ** a virtual table for which the name matches the LIKE pattern.
7762        */
7763        char *zExpr = sqlite3_mprintf(
7764            "name LIKE %Q ESCAPE '\\' OR EXISTS ("
7765            "  SELECT 1 FROM sqlite_schema WHERE "
7766            "    name LIKE %Q ESCAPE '\\' AND"
7767            "    sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
7768            "    substr(o.name, 1, length(name)+1) == (name||'_')"
7769            ")", azArg[i], azArg[i]
7770        );
7771
7772        if( zLike ){
7773          zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
7774        }else{
7775          zLike = zExpr;
7776        }
7777      }
7778    }
7779
7780    open_db(p, 0);
7781
7782    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7783      /* When playing back a "dump", the content might appear in an order
7784      ** which causes immediate foreign key constraints to be violated.
7785      ** So disable foreign-key constraint enforcement to prevent problems. */
7786      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7787      raw_printf(p->out, "BEGIN TRANSACTION;\n");
7788    }
7789    p->writableSchema = 0;
7790    p->showHeader = 0;
7791    /* Set writable_schema=ON since doing so forces SQLite to initialize
7792    ** as much of the schema as it can even if the sqlite_schema table is
7793    ** corrupt. */
7794    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7795    p->nErr = 0;
7796    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7797    zSql = sqlite3_mprintf(
7798      "SELECT name, type, sql FROM sqlite_schema AS o "
7799      "WHERE (%s) AND type=='table'"
7800      "  AND sql NOT NULL"
7801      " ORDER BY tbl_name='sqlite_sequence', rowid",
7802      zLike
7803    );
7804    run_schema_dump_query(p,zSql);
7805    sqlite3_free(zSql);
7806    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7807      zSql = sqlite3_mprintf(
7808        "SELECT sql FROM sqlite_schema AS o "
7809        "WHERE (%s) AND sql NOT NULL"
7810        "  AND type IN ('index','trigger','view')",
7811        zLike
7812      );
7813      run_table_dump_query(p, zSql);
7814      sqlite3_free(zSql);
7815    }
7816    sqlite3_free(zLike);
7817    if( p->writableSchema ){
7818      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7819      p->writableSchema = 0;
7820    }
7821    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7822    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7823    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
7824      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7825    }
7826    p->showHeader = savedShowHeader;
7827    p->shellFlgs = savedShellFlags;
7828  }else
7829
7830  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7831    if( nArg==2 ){
7832      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7833    }else{
7834      raw_printf(stderr, "Usage: .echo on|off\n");
7835      rc = 1;
7836    }
7837  }else
7838
7839  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7840    if( nArg==2 ){
7841      p->autoEQPtest = 0;
7842      if( p->autoEQPtrace ){
7843        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7844        p->autoEQPtrace = 0;
7845      }
7846      if( strcmp(azArg[1],"full")==0 ){
7847        p->autoEQP = AUTOEQP_full;
7848      }else if( strcmp(azArg[1],"trigger")==0 ){
7849        p->autoEQP = AUTOEQP_trigger;
7850#ifdef SQLITE_DEBUG
7851      }else if( strcmp(azArg[1],"test")==0 ){
7852        p->autoEQP = AUTOEQP_on;
7853        p->autoEQPtest = 1;
7854      }else if( strcmp(azArg[1],"trace")==0 ){
7855        p->autoEQP = AUTOEQP_full;
7856        p->autoEQPtrace = 1;
7857        open_db(p, 0);
7858        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
7859        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7860#endif
7861      }else{
7862        p->autoEQP = (u8)booleanValue(azArg[1]);
7863      }
7864    }else{
7865      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7866      rc = 1;
7867    }
7868  }else
7869
7870  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7871    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7872    rc = 2;
7873  }else
7874
7875  /* The ".explain" command is automatic now.  It is largely pointless.  It
7876  ** retained purely for backwards compatibility */
7877  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7878    int val = 1;
7879    if( nArg>=2 ){
7880      if( strcmp(azArg[1],"auto")==0 ){
7881        val = 99;
7882      }else{
7883        val =  booleanValue(azArg[1]);
7884      }
7885    }
7886    if( val==1 && p->mode!=MODE_Explain ){
7887      p->normalMode = p->mode;
7888      p->mode = MODE_Explain;
7889      p->autoExplain = 0;
7890    }else if( val==0 ){
7891      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7892      p->autoExplain = 0;
7893    }else if( val==99 ){
7894      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7895      p->autoExplain = 1;
7896    }
7897  }else
7898
7899#ifndef SQLITE_OMIT_VIRTUALTABLE
7900  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7901    open_db(p, 0);
7902    expertDotCommand(p, azArg, nArg);
7903  }else
7904#endif
7905
7906  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7907    static const struct {
7908       const char *zCtrlName;   /* Name of a test-control option */
7909       int ctrlCode;            /* Integer code for that option */
7910       const char *zUsage;      /* Usage notes */
7911    } aCtrl[] = {
7912      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7913      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
7914      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7915      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7916      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7917   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7918      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7919      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
7920      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7921      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7922   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7923    };
7924    int filectrl = -1;
7925    int iCtrl = -1;
7926    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7927    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7928    int n2, i;
7929    const char *zCmd = 0;
7930    const char *zSchema = 0;
7931
7932    open_db(p, 0);
7933    zCmd = nArg>=2 ? azArg[1] : "help";
7934
7935    if( zCmd[0]=='-'
7936     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7937     && nArg>=4
7938    ){
7939      zSchema = azArg[2];
7940      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7941      nArg -= 2;
7942      zCmd = azArg[1];
7943    }
7944
7945    /* The argument can optionally begin with "-" or "--" */
7946    if( zCmd[0]=='-' && zCmd[1] ){
7947      zCmd++;
7948      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7949    }
7950
7951    /* --help lists all file-controls */
7952    if( strcmp(zCmd,"help")==0 ){
7953      utf8_printf(p->out, "Available file-controls:\n");
7954      for(i=0; i<ArraySize(aCtrl); i++){
7955        utf8_printf(p->out, "  .filectrl %s %s\n",
7956                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7957      }
7958      rc = 1;
7959      goto meta_command_exit;
7960    }
7961
7962    /* convert filectrl text option to value. allow any unique prefix
7963    ** of the option name, or a numerical value. */
7964    n2 = strlen30(zCmd);
7965    for(i=0; i<ArraySize(aCtrl); i++){
7966      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7967        if( filectrl<0 ){
7968          filectrl = aCtrl[i].ctrlCode;
7969          iCtrl = i;
7970        }else{
7971          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7972                              "Use \".filectrl --help\" for help\n", zCmd);
7973          rc = 1;
7974          goto meta_command_exit;
7975        }
7976      }
7977    }
7978    if( filectrl<0 ){
7979      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7980                         "Use \".filectrl --help\" for help\n", zCmd);
7981    }else{
7982      switch(filectrl){
7983        case SQLITE_FCNTL_SIZE_LIMIT: {
7984          if( nArg!=2 && nArg!=3 ) break;
7985          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7986          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7987          isOk = 1;
7988          break;
7989        }
7990        case SQLITE_FCNTL_LOCK_TIMEOUT:
7991        case SQLITE_FCNTL_CHUNK_SIZE: {
7992          int x;
7993          if( nArg!=3 ) break;
7994          x = (int)integerValue(azArg[2]);
7995          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7996          isOk = 2;
7997          break;
7998        }
7999        case SQLITE_FCNTL_PERSIST_WAL:
8000        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8001          int x;
8002          if( nArg!=2 && nArg!=3 ) break;
8003          x = nArg==3 ? booleanValue(azArg[2]) : -1;
8004          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8005          iRes = x;
8006          isOk = 1;
8007          break;
8008        }
8009        case SQLITE_FCNTL_DATA_VERSION:
8010        case SQLITE_FCNTL_HAS_MOVED: {
8011          int x;
8012          if( nArg!=2 ) break;
8013          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8014          iRes = x;
8015          isOk = 1;
8016          break;
8017        }
8018        case SQLITE_FCNTL_TEMPFILENAME: {
8019          char *z = 0;
8020          if( nArg!=2 ) break;
8021          sqlite3_file_control(p->db, zSchema, filectrl, &z);
8022          if( z ){
8023            utf8_printf(p->out, "%s\n", z);
8024            sqlite3_free(z);
8025          }
8026          isOk = 2;
8027          break;
8028        }
8029        case SQLITE_FCNTL_RESERVE_BYTES: {
8030          int x;
8031          if( nArg>=3 ){
8032            x = atoi(azArg[2]);
8033            sqlite3_file_control(p->db, zSchema, filectrl, &x);
8034          }
8035          x = -1;
8036          sqlite3_file_control(p->db, zSchema, filectrl, &x);
8037          utf8_printf(p->out,"%d\n", x);
8038          isOk = 2;
8039          break;
8040        }
8041      }
8042    }
8043    if( isOk==0 && iCtrl>=0 ){
8044      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8045      rc = 1;
8046    }else if( isOk==1 ){
8047      char zBuf[100];
8048      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8049      raw_printf(p->out, "%s\n", zBuf);
8050    }
8051  }else
8052
8053  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
8054    ShellState data;
8055    int doStats = 0;
8056    memcpy(&data, p, sizeof(data));
8057    data.showHeader = 0;
8058    data.cMode = data.mode = MODE_Semi;
8059    if( nArg==2 && optionMatch(azArg[1], "indent") ){
8060      data.cMode = data.mode = MODE_Pretty;
8061      nArg = 1;
8062    }
8063    if( nArg!=1 ){
8064      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8065      rc = 1;
8066      goto meta_command_exit;
8067    }
8068    open_db(p, 0);
8069    rc = sqlite3_exec(p->db,
8070       "SELECT sql FROM"
8071       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8072       "     FROM sqlite_schema UNION ALL"
8073       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8074       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8075       "ORDER BY x",
8076       callback, &data, 0
8077    );
8078    if( rc==SQLITE_OK ){
8079      sqlite3_stmt *pStmt;
8080      rc = sqlite3_prepare_v2(p->db,
8081               "SELECT rowid FROM sqlite_schema"
8082               " WHERE name GLOB 'sqlite_stat[134]'",
8083               -1, &pStmt, 0);
8084      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8085      sqlite3_finalize(pStmt);
8086    }
8087    if( doStats==0 ){
8088      raw_printf(p->out, "/* No STAT tables available */\n");
8089    }else{
8090      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8091      data.cMode = data.mode = MODE_Insert;
8092      data.zDestTable = "sqlite_stat1";
8093      shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8094      data.zDestTable = "sqlite_stat4";
8095      shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8096      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8097    }
8098  }else
8099
8100  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8101    if( nArg==2 ){
8102      p->showHeader = booleanValue(azArg[1]);
8103      p->shellFlgs |= SHFLG_HeaderSet;
8104    }else{
8105      raw_printf(stderr, "Usage: .headers on|off\n");
8106      rc = 1;
8107    }
8108  }else
8109
8110  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8111    if( nArg>=2 ){
8112      n = showHelp(p->out, azArg[1]);
8113      if( n==0 ){
8114        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8115      }
8116    }else{
8117      showHelp(p->out, 0);
8118    }
8119  }else
8120
8121  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8122    char *zTable = 0;           /* Insert data into this table */
8123    char *zFile = 0;            /* Name of file to extra content from */
8124    sqlite3_stmt *pStmt = NULL; /* A statement */
8125    int nCol;                   /* Number of columns in the table */
8126    int nByte;                  /* Number of bytes in an SQL string */
8127    int i, j;                   /* Loop counters */
8128    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8129    int nSep;                   /* Number of bytes in p->colSeparator[] */
8130    char *zSql;                 /* An SQL statement */
8131    ImportCtx sCtx;             /* Reader context */
8132    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8133    int eVerbose = 0;           /* Larger for more console output */
8134    int nSkip = 0;              /* Initial lines to skip */
8135    int useOutputMode = 1;      /* Use output mode to determine separators */
8136
8137    memset(&sCtx, 0, sizeof(sCtx));
8138    if( p->mode==MODE_Ascii ){
8139      xRead = ascii_read_one_field;
8140    }else{
8141      xRead = csv_read_one_field;
8142    }
8143    for(i=1; i<nArg; i++){
8144      char *z = azArg[i];
8145      if( z[0]=='-' && z[1]=='-' ) z++;
8146      if( z[0]!='-' ){
8147        if( zFile==0 ){
8148          zFile = z;
8149        }else if( zTable==0 ){
8150          zTable = z;
8151        }else{
8152          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8153          showHelp(p->out, "import");
8154          rc = 1;
8155          goto meta_command_exit;
8156        }
8157      }else if( strcmp(z,"-v")==0 ){
8158        eVerbose++;
8159      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8160        nSkip = integerValue(azArg[++i]);
8161      }else if( strcmp(z,"-ascii")==0 ){
8162        sCtx.cColSep = SEP_Unit[0];
8163        sCtx.cRowSep = SEP_Record[0];
8164        xRead = ascii_read_one_field;
8165        useOutputMode = 0;
8166      }else if( strcmp(z,"-csv")==0 ){
8167        sCtx.cColSep = ',';
8168        sCtx.cRowSep = '\n';
8169        xRead = csv_read_one_field;
8170        useOutputMode = 0;
8171      }else{
8172        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8173        showHelp(p->out, "import");
8174        rc = 1;
8175        goto meta_command_exit;
8176      }
8177    }
8178    if( zTable==0 ){
8179      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8180                  zFile==0 ? "FILE" : "TABLE");
8181      showHelp(p->out, "import");
8182      rc = 1;
8183      goto meta_command_exit;
8184    }
8185    seenInterrupt = 0;
8186    open_db(p, 0);
8187    if( useOutputMode ){
8188      /* If neither the --csv or --ascii options are specified, then set
8189      ** the column and row separator characters from the output mode. */
8190      nSep = strlen30(p->colSeparator);
8191      if( nSep==0 ){
8192        raw_printf(stderr,
8193                   "Error: non-null column separator required for import\n");
8194        rc = 1;
8195        goto meta_command_exit;
8196      }
8197      if( nSep>1 ){
8198        raw_printf(stderr,
8199              "Error: multi-character column separators not allowed"
8200              " for import\n");
8201        rc = 1;
8202        goto meta_command_exit;
8203      }
8204      nSep = strlen30(p->rowSeparator);
8205      if( nSep==0 ){
8206        raw_printf(stderr,
8207            "Error: non-null row separator required for import\n");
8208        rc = 1;
8209        goto meta_command_exit;
8210      }
8211      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8212        /* When importing CSV (only), if the row separator is set to the
8213        ** default output row separator, change it to the default input
8214        ** row separator.  This avoids having to maintain different input
8215        ** and output row separators. */
8216        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8217        nSep = strlen30(p->rowSeparator);
8218      }
8219      if( nSep>1 ){
8220        raw_printf(stderr, "Error: multi-character row separators not allowed"
8221                           " for import\n");
8222        rc = 1;
8223        goto meta_command_exit;
8224      }
8225      sCtx.cColSep = p->colSeparator[0];
8226      sCtx.cRowSep = p->rowSeparator[0];
8227    }
8228    sCtx.zFile = zFile;
8229    sCtx.nLine = 1;
8230    if( sCtx.zFile[0]=='|' ){
8231#ifdef SQLITE_OMIT_POPEN
8232      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8233      rc = 1;
8234      goto meta_command_exit;
8235#else
8236      sCtx.in = popen(sCtx.zFile+1, "r");
8237      sCtx.zFile = "<pipe>";
8238      sCtx.xCloser = pclose;
8239#endif
8240    }else{
8241      sCtx.in = fopen(sCtx.zFile, "rb");
8242      sCtx.xCloser = fclose;
8243    }
8244    if( sCtx.in==0 ){
8245      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8246      rc = 1;
8247      goto meta_command_exit;
8248    }
8249    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8250      char zSep[2];
8251      zSep[1] = 0;
8252      zSep[0] = sCtx.cColSep;
8253      utf8_printf(p->out, "Column separator ");
8254      output_c_string(p->out, zSep);
8255      utf8_printf(p->out, ", row separator ");
8256      zSep[0] = sCtx.cRowSep;
8257      output_c_string(p->out, zSep);
8258      utf8_printf(p->out, "\n");
8259    }
8260    while( (nSkip--)>0 ){
8261      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8262    }
8263    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
8264    if( zSql==0 ){
8265      import_cleanup(&sCtx);
8266      shell_out_of_memory();
8267    }
8268    nByte = strlen30(zSql);
8269    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8270    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8271    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8272      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
8273      char cSep = '(';
8274      while( xRead(&sCtx) ){
8275        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8276        cSep = ',';
8277        if( sCtx.cTerm!=sCtx.cColSep ) break;
8278      }
8279      if( cSep=='(' ){
8280        sqlite3_free(zCreate);
8281        import_cleanup(&sCtx);
8282        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8283        rc = 1;
8284        goto meta_command_exit;
8285      }
8286      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8287      if( eVerbose>=1 ){
8288        utf8_printf(p->out, "%s\n", zCreate);
8289      }
8290      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8291      sqlite3_free(zCreate);
8292      if( rc ){
8293        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
8294                sqlite3_errmsg(p->db));
8295        import_cleanup(&sCtx);
8296        rc = 1;
8297        goto meta_command_exit;
8298      }
8299      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8300    }
8301    sqlite3_free(zSql);
8302    if( rc ){
8303      if (pStmt) sqlite3_finalize(pStmt);
8304      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8305      import_cleanup(&sCtx);
8306      rc = 1;
8307      goto meta_command_exit;
8308    }
8309    nCol = sqlite3_column_count(pStmt);
8310    sqlite3_finalize(pStmt);
8311    pStmt = 0;
8312    if( nCol==0 ) return 0; /* no columns, no error */
8313    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8314    if( zSql==0 ){
8315      import_cleanup(&sCtx);
8316      shell_out_of_memory();
8317    }
8318    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8319    j = strlen30(zSql);
8320    for(i=1; i<nCol; i++){
8321      zSql[j++] = ',';
8322      zSql[j++] = '?';
8323    }
8324    zSql[j++] = ')';
8325    zSql[j] = 0;
8326    if( eVerbose>=2 ){
8327      utf8_printf(p->out, "Insert using: %s\n", zSql);
8328    }
8329    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8330    sqlite3_free(zSql);
8331    if( rc ){
8332      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8333      if (pStmt) sqlite3_finalize(pStmt);
8334      import_cleanup(&sCtx);
8335      rc = 1;
8336      goto meta_command_exit;
8337    }
8338    needCommit = sqlite3_get_autocommit(p->db);
8339    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8340    do{
8341      int startLine = sCtx.nLine;
8342      for(i=0; i<nCol; i++){
8343        char *z = xRead(&sCtx);
8344        /*
8345        ** Did we reach end-of-file before finding any columns?
8346        ** If so, stop instead of NULL filling the remaining columns.
8347        */
8348        if( z==0 && i==0 ) break;
8349        /*
8350        ** Did we reach end-of-file OR end-of-line before finding any
8351        ** columns in ASCII mode?  If so, stop instead of NULL filling
8352        ** the remaining columns.
8353        */
8354        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8355        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8356        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8357          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8358                          "filling the rest with NULL\n",
8359                          sCtx.zFile, startLine, nCol, i+1);
8360          i += 2;
8361          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8362        }
8363      }
8364      if( sCtx.cTerm==sCtx.cColSep ){
8365        do{
8366          xRead(&sCtx);
8367          i++;
8368        }while( sCtx.cTerm==sCtx.cColSep );
8369        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8370                        "extras ignored\n",
8371                        sCtx.zFile, startLine, nCol, i);
8372      }
8373      if( i>=nCol ){
8374        sqlite3_step(pStmt);
8375        rc = sqlite3_reset(pStmt);
8376        if( rc!=SQLITE_OK ){
8377          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8378                      startLine, sqlite3_errmsg(p->db));
8379          sCtx.nErr++;
8380        }else{
8381          sCtx.nRow++;
8382        }
8383      }
8384    }while( sCtx.cTerm!=EOF );
8385
8386    import_cleanup(&sCtx);
8387    sqlite3_finalize(pStmt);
8388    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8389    if( eVerbose>0 ){
8390      utf8_printf(p->out,
8391          "Added %d rows with %d errors using %d lines of input\n",
8392          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8393    }
8394  }else
8395
8396#ifndef SQLITE_UNTESTABLE
8397  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8398    char *zSql;
8399    char *zCollist = 0;
8400    sqlite3_stmt *pStmt;
8401    int tnum = 0;
8402    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8403    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8404    int i;
8405    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8406      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8407                          "       .imposter off\n");
8408      /* Also allowed, but not documented:
8409      **
8410      **    .imposter TABLE IMPOSTER
8411      **
8412      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8413      ** imposter is another WITHOUT ROWID table with the columns in
8414      ** storage order. */
8415      rc = 1;
8416      goto meta_command_exit;
8417    }
8418    open_db(p, 0);
8419    if( nArg==2 ){
8420      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8421      goto meta_command_exit;
8422    }
8423    zSql = sqlite3_mprintf(
8424      "SELECT rootpage, 0 FROM sqlite_schema"
8425      " WHERE name='%q' AND type='index'"
8426      "UNION ALL "
8427      "SELECT rootpage, 1 FROM sqlite_schema"
8428      " WHERE name='%q' AND type='table'"
8429      "   AND sql LIKE '%%without%%rowid%%'",
8430      azArg[1], azArg[1]
8431    );
8432    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8433    sqlite3_free(zSql);
8434    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8435      tnum = sqlite3_column_int(pStmt, 0);
8436      isWO = sqlite3_column_int(pStmt, 1);
8437    }
8438    sqlite3_finalize(pStmt);
8439    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8440    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8441    sqlite3_free(zSql);
8442    i = 0;
8443    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8444      char zLabel[20];
8445      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8446      i++;
8447      if( zCol==0 ){
8448        if( sqlite3_column_int(pStmt,1)==-1 ){
8449          zCol = "_ROWID_";
8450        }else{
8451          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8452          zCol = zLabel;
8453        }
8454      }
8455      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8456        lenPK = (int)strlen(zCollist);
8457      }
8458      if( zCollist==0 ){
8459        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8460      }else{
8461        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8462      }
8463    }
8464    sqlite3_finalize(pStmt);
8465    if( i==0 || tnum==0 ){
8466      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8467      rc = 1;
8468      sqlite3_free(zCollist);
8469      goto meta_command_exit;
8470    }
8471    if( lenPK==0 ) lenPK = 100000;
8472    zSql = sqlite3_mprintf(
8473          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8474          azArg[2], zCollist, lenPK, zCollist);
8475    sqlite3_free(zCollist);
8476    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8477    if( rc==SQLITE_OK ){
8478      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8479      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8480      if( rc ){
8481        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8482      }else{
8483        utf8_printf(stdout, "%s;\n", zSql);
8484        raw_printf(stdout,
8485          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8486          azArg[1], isWO ? "table" : "index"
8487        );
8488      }
8489    }else{
8490      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8491      rc = 1;
8492    }
8493    sqlite3_free(zSql);
8494  }else
8495#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8496
8497#ifdef SQLITE_ENABLE_IOTRACE
8498  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8499    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8500    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8501    iotrace = 0;
8502    if( nArg<2 ){
8503      sqlite3IoTrace = 0;
8504    }else if( strcmp(azArg[1], "-")==0 ){
8505      sqlite3IoTrace = iotracePrintf;
8506      iotrace = stdout;
8507    }else{
8508      iotrace = fopen(azArg[1], "w");
8509      if( iotrace==0 ){
8510        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8511        sqlite3IoTrace = 0;
8512        rc = 1;
8513      }else{
8514        sqlite3IoTrace = iotracePrintf;
8515      }
8516    }
8517  }else
8518#endif
8519
8520  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8521    static const struct {
8522       const char *zLimitName;   /* Name of a limit */
8523       int limitCode;            /* Integer code for that limit */
8524    } aLimit[] = {
8525      { "length",                SQLITE_LIMIT_LENGTH                    },
8526      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8527      { "column",                SQLITE_LIMIT_COLUMN                    },
8528      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8529      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8530      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8531      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8532      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8533      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8534      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8535      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8536      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8537    };
8538    int i, n2;
8539    open_db(p, 0);
8540    if( nArg==1 ){
8541      for(i=0; i<ArraySize(aLimit); i++){
8542        printf("%20s %d\n", aLimit[i].zLimitName,
8543               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8544      }
8545    }else if( nArg>3 ){
8546      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8547      rc = 1;
8548      goto meta_command_exit;
8549    }else{
8550      int iLimit = -1;
8551      n2 = strlen30(azArg[1]);
8552      for(i=0; i<ArraySize(aLimit); i++){
8553        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8554          if( iLimit<0 ){
8555            iLimit = i;
8556          }else{
8557            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8558            rc = 1;
8559            goto meta_command_exit;
8560          }
8561        }
8562      }
8563      if( iLimit<0 ){
8564        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8565                        "enter \".limits\" with no arguments for a list.\n",
8566                         azArg[1]);
8567        rc = 1;
8568        goto meta_command_exit;
8569      }
8570      if( nArg==3 ){
8571        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8572                      (int)integerValue(azArg[2]));
8573      }
8574      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8575             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8576    }
8577  }else
8578
8579  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8580    open_db(p, 0);
8581    lintDotCommand(p, azArg, nArg);
8582  }else
8583
8584#ifndef SQLITE_OMIT_LOAD_EXTENSION
8585  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8586    const char *zFile, *zProc;
8587    char *zErrMsg = 0;
8588    if( nArg<2 ){
8589      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8590      rc = 1;
8591      goto meta_command_exit;
8592    }
8593    zFile = azArg[1];
8594    zProc = nArg>=3 ? azArg[2] : 0;
8595    open_db(p, 0);
8596    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8597    if( rc!=SQLITE_OK ){
8598      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8599      sqlite3_free(zErrMsg);
8600      rc = 1;
8601    }
8602  }else
8603#endif
8604
8605  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8606    if( nArg!=2 ){
8607      raw_printf(stderr, "Usage: .log FILENAME\n");
8608      rc = 1;
8609    }else{
8610      const char *zFile = azArg[1];
8611      output_file_close(p->pLog);
8612      p->pLog = output_file_open(zFile, 0);
8613    }
8614  }else
8615
8616  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8617    const char *zMode = nArg>=2 ? azArg[1] : "";
8618    int n2 = strlen30(zMode);
8619    int c2 = zMode[0];
8620    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8621      p->mode = MODE_Line;
8622      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8623    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8624      p->mode = MODE_Column;
8625      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8626        p->showHeader = 1;
8627      }
8628      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8629    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8630      p->mode = MODE_List;
8631      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8632      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8633    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8634      p->mode = MODE_Html;
8635    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8636      p->mode = MODE_Tcl;
8637      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8638      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8639    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8640      p->mode = MODE_Csv;
8641      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8642      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8643    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8644      p->mode = MODE_List;
8645      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8646    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8647      p->mode = MODE_Insert;
8648      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8649    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8650      p->mode = MODE_Quote;
8651      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8652      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8653    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8654      p->mode = MODE_Ascii;
8655      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8656      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8657    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8658      p->mode = MODE_Markdown;
8659    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8660      p->mode = MODE_Table;
8661    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8662      p->mode = MODE_Box;
8663    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8664      p->mode = MODE_Json;
8665    }else if( nArg==1 ){
8666      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8667    }else{
8668      raw_printf(stderr, "Error: mode should be one of: "
8669         "ascii box column csv html insert json line list markdown "
8670         "quote table tabs tcl\n");
8671      rc = 1;
8672    }
8673    p->cMode = p->mode;
8674  }else
8675
8676  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8677    if( nArg==2 ){
8678      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8679                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8680    }else{
8681      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8682      rc = 1;
8683    }
8684  }else
8685
8686#ifdef SQLITE_DEBUG
8687  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8688    int i;
8689    for(i=1; i<nArg; i++){
8690      const char *z = azArg[i];
8691      if( z[0]=='-' && z[1]=='-' ) z++;
8692      if( strcmp(z,"-repeat")==0 ){
8693        if( i==nArg-1 ){
8694          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8695          rc = 1;
8696        }else{
8697          oomRepeat = (int)integerValue(azArg[++i]);
8698        }
8699      }else if( IsDigit(z[0]) ){
8700        oomCounter = (int)integerValue(azArg[i]);
8701      }else{
8702        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8703        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8704        rc = 1;
8705      }
8706    }
8707    if( rc==0 ){
8708      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8709      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8710    }
8711  }else
8712#endif /* SQLITE_DEBUG */
8713
8714  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8715    char *zNewFilename = 0;  /* Name of the database file to open */
8716    int iName = 1;           /* Index in azArg[] of the filename */
8717    int newFlag = 0;         /* True to delete file before opening */
8718    /* Close the existing database */
8719    session_close_all(p);
8720    close_db(p->db);
8721    p->db = 0;
8722    p->zDbFilename = 0;
8723    sqlite3_free(p->zFreeOnClose);
8724    p->zFreeOnClose = 0;
8725    p->openMode = SHELL_OPEN_UNSPEC;
8726    p->openFlags = 0;
8727    p->szMax = 0;
8728    /* Check for command-line arguments */
8729    for(iName=1; iName<nArg; iName++){
8730      const char *z = azArg[iName];
8731      if( optionMatch(z,"new") ){
8732        newFlag = 1;
8733#ifdef SQLITE_HAVE_ZLIB
8734      }else if( optionMatch(z, "zip") ){
8735        p->openMode = SHELL_OPEN_ZIPFILE;
8736#endif
8737      }else if( optionMatch(z, "append") ){
8738        p->openMode = SHELL_OPEN_APPENDVFS;
8739      }else if( optionMatch(z, "readonly") ){
8740        p->openMode = SHELL_OPEN_READONLY;
8741      }else if( optionMatch(z, "nofollow") ){
8742        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8743      }else if( optionMatch(z, "excl") ){
8744        p->openFlags |= SQLITE_OPEN_EXCLUSIVE;
8745#ifndef SQLITE_OMIT_DESERIALIZE
8746      }else if( optionMatch(z, "deserialize") ){
8747        p->openMode = SHELL_OPEN_DESERIALIZE;
8748      }else if( optionMatch(z, "hexdb") ){
8749        p->openMode = SHELL_OPEN_HEXDB;
8750      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8751        p->szMax = integerValue(azArg[++iName]);
8752#endif /* SQLITE_OMIT_DESERIALIZE */
8753      }else if( z[0]=='-' ){
8754        utf8_printf(stderr, "unknown option: %s\n", z);
8755        rc = 1;
8756        goto meta_command_exit;
8757      }else if( zNewFilename ){
8758        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
8759        rc = 1;
8760        goto meta_command_exit;
8761      }else{
8762        zNewFilename = sqlite3_mprintf("%s", z);
8763      }
8764    }
8765    /* If a filename is specified, try to open it first */
8766    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8767      if( newFlag ) shellDeleteFile(zNewFilename);
8768      p->zDbFilename = zNewFilename;
8769      open_db(p, OPEN_DB_KEEPALIVE);
8770      if( p->db==0 ){
8771        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8772        sqlite3_free(zNewFilename);
8773      }else{
8774        p->zFreeOnClose = zNewFilename;
8775      }
8776    }
8777    if( p->db==0 ){
8778      /* As a fall-back open a TEMP database */
8779      p->zDbFilename = 0;
8780      open_db(p, 0);
8781    }
8782  }else
8783
8784  if( (c=='o'
8785        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8786   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8787  ){
8788    char *zFile = 0;
8789    int bTxtMode = 0;
8790    int i;
8791    int eMode = 0;
8792    int bBOM = 0;
8793    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8794
8795    if( c=='e' ){
8796      eMode = 'x';
8797      bOnce = 2;
8798    }else if( strncmp(azArg[0],"once",n)==0 ){
8799      bOnce = 1;
8800    }
8801    for(i=1; i<nArg; i++){
8802      char *z = azArg[i];
8803      if( z[0]=='-' ){
8804        if( z[1]=='-' ) z++;
8805        if( strcmp(z,"-bom")==0 ){
8806          bBOM = 1;
8807        }else if( c!='e' && strcmp(z,"-x")==0 ){
8808          eMode = 'x';  /* spreadsheet */
8809        }else if( c!='e' && strcmp(z,"-e")==0 ){
8810          eMode = 'e';  /* text editor */
8811        }else{
8812          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8813                      azArg[i]);
8814          showHelp(p->out, azArg[0]);
8815          rc = 1;
8816          goto meta_command_exit;
8817        }
8818      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
8819        zFile = sqlite3_mprintf("%s", z);
8820        if( zFile[0]=='|' ){
8821          while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
8822          break;
8823        }
8824      }else{
8825        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8826                    azArg[i]);
8827        showHelp(p->out, azArg[0]);
8828        rc = 1;
8829        sqlite3_free(zFile);
8830        goto meta_command_exit;
8831      }
8832    }
8833    if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
8834    if( bOnce ){
8835      p->outCount = 2;
8836    }else{
8837      p->outCount = 0;
8838    }
8839    output_reset(p);
8840#ifndef SQLITE_NOHAVE_SYSTEM
8841    if( eMode=='e' || eMode=='x' ){
8842      p->doXdgOpen = 1;
8843      outputModePush(p);
8844      if( eMode=='x' ){
8845        /* spreadsheet mode.  Output as CSV. */
8846        newTempFile(p, "csv");
8847        ShellClearFlag(p, SHFLG_Echo);
8848        p->mode = MODE_Csv;
8849        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8850        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8851      }else{
8852        /* text editor mode */
8853        newTempFile(p, "txt");
8854        bTxtMode = 1;
8855      }
8856      sqlite3_free(zFile);
8857      zFile = sqlite3_mprintf("%s", p->zTempFile);
8858    }
8859#endif /* SQLITE_NOHAVE_SYSTEM */
8860    if( zFile[0]=='|' ){
8861#ifdef SQLITE_OMIT_POPEN
8862      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8863      rc = 1;
8864      p->out = stdout;
8865#else
8866      p->out = popen(zFile + 1, "w");
8867      if( p->out==0 ){
8868        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8869        p->out = stdout;
8870        rc = 1;
8871      }else{
8872        if( bBOM ) fprintf(p->out,"\357\273\277");
8873        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8874      }
8875#endif
8876    }else{
8877      p->out = output_file_open(zFile, bTxtMode);
8878      if( p->out==0 ){
8879        if( strcmp(zFile,"off")!=0 ){
8880          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8881        }
8882        p->out = stdout;
8883        rc = 1;
8884      } else {
8885        if( bBOM ) fprintf(p->out,"\357\273\277");
8886        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8887      }
8888    }
8889    sqlite3_free(zFile);
8890  }else
8891
8892  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8893    open_db(p,0);
8894    if( nArg<=1 ) goto parameter_syntax_error;
8895
8896    /* .parameter clear
8897    ** Clear all bind parameters by dropping the TEMP table that holds them.
8898    */
8899    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8900      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8901                   0, 0, 0);
8902    }else
8903
8904    /* .parameter list
8905    ** List all bind parameters.
8906    */
8907    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8908      sqlite3_stmt *pStmt = 0;
8909      int rx;
8910      int len = 0;
8911      rx = sqlite3_prepare_v2(p->db,
8912             "SELECT max(length(key)) "
8913             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8914      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8915        len = sqlite3_column_int(pStmt, 0);
8916        if( len>40 ) len = 40;
8917      }
8918      sqlite3_finalize(pStmt);
8919      pStmt = 0;
8920      if( len ){
8921        rx = sqlite3_prepare_v2(p->db,
8922             "SELECT key, quote(value) "
8923             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8924        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8925          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8926                      sqlite3_column_text(pStmt,1));
8927        }
8928        sqlite3_finalize(pStmt);
8929      }
8930    }else
8931
8932    /* .parameter init
8933    ** Make sure the TEMP table used to hold bind parameters exists.
8934    ** Create it if necessary.
8935    */
8936    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8937      bind_table_init(p);
8938    }else
8939
8940    /* .parameter set NAME VALUE
8941    ** Set or reset a bind parameter.  NAME should be the full parameter
8942    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8943    ** VALUE can be in either SQL literal notation, or if not it will be
8944    ** understood to be a text string.
8945    */
8946    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8947      int rx;
8948      char *zSql;
8949      sqlite3_stmt *pStmt;
8950      const char *zKey = azArg[2];
8951      const char *zValue = azArg[3];
8952      bind_table_init(p);
8953      zSql = sqlite3_mprintf(
8954                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8955                  "VALUES(%Q,%s);", zKey, zValue);
8956      if( zSql==0 ) shell_out_of_memory();
8957      pStmt = 0;
8958      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8959      sqlite3_free(zSql);
8960      if( rx!=SQLITE_OK ){
8961        sqlite3_finalize(pStmt);
8962        pStmt = 0;
8963        zSql = sqlite3_mprintf(
8964                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8965                   "VALUES(%Q,%Q);", zKey, zValue);
8966        if( zSql==0 ) shell_out_of_memory();
8967        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8968        sqlite3_free(zSql);
8969        if( rx!=SQLITE_OK ){
8970          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8971          sqlite3_finalize(pStmt);
8972          pStmt = 0;
8973          rc = 1;
8974        }
8975      }
8976      sqlite3_step(pStmt);
8977      sqlite3_finalize(pStmt);
8978    }else
8979
8980    /* .parameter unset NAME
8981    ** Remove the NAME binding from the parameter binding table, if it
8982    ** exists.
8983    */
8984    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8985      char *zSql = sqlite3_mprintf(
8986          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8987      if( zSql==0 ) shell_out_of_memory();
8988      sqlite3_exec(p->db, zSql, 0, 0, 0);
8989      sqlite3_free(zSql);
8990    }else
8991    /* If no command name matches, show a syntax error */
8992    parameter_syntax_error:
8993    showHelp(p->out, "parameter");
8994  }else
8995
8996  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8997    int i;
8998    for(i=1; i<nArg; i++){
8999      if( i>1 ) raw_printf(p->out, " ");
9000      utf8_printf(p->out, "%s", azArg[i]);
9001    }
9002    raw_printf(p->out, "\n");
9003  }else
9004
9005#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9006  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
9007    int i;
9008    int nn = 0;
9009    p->flgProgress = 0;
9010    p->mxProgress = 0;
9011    p->nProgress = 0;
9012    for(i=1; i<nArg; i++){
9013      const char *z = azArg[i];
9014      if( z[0]=='-' ){
9015        z++;
9016        if( z[0]=='-' ) z++;
9017        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
9018          p->flgProgress |= SHELL_PROGRESS_QUIET;
9019          continue;
9020        }
9021        if( strcmp(z,"reset")==0 ){
9022          p->flgProgress |= SHELL_PROGRESS_RESET;
9023          continue;
9024        }
9025        if( strcmp(z,"once")==0 ){
9026          p->flgProgress |= SHELL_PROGRESS_ONCE;
9027          continue;
9028        }
9029        if( strcmp(z,"limit")==0 ){
9030          if( i+1>=nArg ){
9031            utf8_printf(stderr, "Error: missing argument on --limit\n");
9032            rc = 1;
9033            goto meta_command_exit;
9034          }else{
9035            p->mxProgress = (int)integerValue(azArg[++i]);
9036          }
9037          continue;
9038        }
9039        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9040        rc = 1;
9041        goto meta_command_exit;
9042      }else{
9043        nn = (int)integerValue(z);
9044      }
9045    }
9046    open_db(p, 0);
9047    sqlite3_progress_handler(p->db, nn, progress_handler, p);
9048  }else
9049#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9050
9051  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
9052    if( nArg >= 2) {
9053      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9054    }
9055    if( nArg >= 3) {
9056      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9057    }
9058  }else
9059
9060  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
9061    rc = 2;
9062  }else
9063
9064  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
9065    FILE *inSaved = p->in;
9066    int savedLineno = p->lineno;
9067    if( nArg!=2 ){
9068      raw_printf(stderr, "Usage: .read FILE\n");
9069      rc = 1;
9070      goto meta_command_exit;
9071    }
9072    if( azArg[1][0]=='|' ){
9073#ifdef SQLITE_OMIT_POPEN
9074      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9075      rc = 1;
9076      p->out = stdout;
9077#else
9078      p->in = popen(azArg[1]+1, "r");
9079      if( p->in==0 ){
9080        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9081        rc = 1;
9082      }else{
9083        rc = process_input(p);
9084        pclose(p->in);
9085      }
9086#endif
9087    }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
9088      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9089      rc = 1;
9090    }else{
9091      rc = process_input(p);
9092      fclose(p->in);
9093    }
9094    p->in = inSaved;
9095    p->lineno = savedLineno;
9096  }else
9097
9098  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9099    const char *zSrcFile;
9100    const char *zDb;
9101    sqlite3 *pSrc;
9102    sqlite3_backup *pBackup;
9103    int nTimeout = 0;
9104
9105    if( nArg==2 ){
9106      zSrcFile = azArg[1];
9107      zDb = "main";
9108    }else if( nArg==3 ){
9109      zSrcFile = azArg[2];
9110      zDb = azArg[1];
9111    }else{
9112      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9113      rc = 1;
9114      goto meta_command_exit;
9115    }
9116    rc = sqlite3_open(zSrcFile, &pSrc);
9117    if( rc!=SQLITE_OK ){
9118      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9119      close_db(pSrc);
9120      return 1;
9121    }
9122    open_db(p, 0);
9123    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9124    if( pBackup==0 ){
9125      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9126      close_db(pSrc);
9127      return 1;
9128    }
9129    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9130          || rc==SQLITE_BUSY  ){
9131      if( rc==SQLITE_BUSY ){
9132        if( nTimeout++ >= 3 ) break;
9133        sqlite3_sleep(100);
9134      }
9135    }
9136    sqlite3_backup_finish(pBackup);
9137    if( rc==SQLITE_DONE ){
9138      rc = 0;
9139    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9140      raw_printf(stderr, "Error: source database is busy\n");
9141      rc = 1;
9142    }else{
9143      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9144      rc = 1;
9145    }
9146    close_db(pSrc);
9147  }else
9148
9149  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9150    if( nArg==2 ){
9151      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9152#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9153      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9154#endif
9155    }else{
9156      raw_printf(stderr, "Usage: .scanstats on|off\n");
9157      rc = 1;
9158    }
9159  }else
9160
9161  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9162    ShellText sSelect;
9163    ShellState data;
9164    char *zErrMsg = 0;
9165    const char *zDiv = "(";
9166    const char *zName = 0;
9167    int iSchema = 0;
9168    int bDebug = 0;
9169    int bNoSystemTabs = 0;
9170    int ii;
9171
9172    open_db(p, 0);
9173    memcpy(&data, p, sizeof(data));
9174    data.showHeader = 0;
9175    data.cMode = data.mode = MODE_Semi;
9176    initText(&sSelect);
9177    for(ii=1; ii<nArg; ii++){
9178      if( optionMatch(azArg[ii],"indent") ){
9179        data.cMode = data.mode = MODE_Pretty;
9180      }else if( optionMatch(azArg[ii],"debug") ){
9181        bDebug = 1;
9182      }else if( optionMatch(azArg[ii],"nosys") ){
9183        bNoSystemTabs = 1;
9184      }else if( azArg[ii][0]=='-' ){
9185        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9186        rc = 1;
9187        goto meta_command_exit;
9188      }else if( zName==0 ){
9189        zName = azArg[ii];
9190      }else{
9191        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9192        rc = 1;
9193        goto meta_command_exit;
9194      }
9195    }
9196    if( zName!=0 ){
9197      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9198                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9199                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9200                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9201      if( isSchema ){
9202        char *new_argv[2], *new_colv[2];
9203        new_argv[0] = sqlite3_mprintf(
9204                      "CREATE TABLE %s (\n"
9205                      "  type text,\n"
9206                      "  name text,\n"
9207                      "  tbl_name text,\n"
9208                      "  rootpage integer,\n"
9209                      "  sql text\n"
9210                      ")", zName);
9211        new_argv[1] = 0;
9212        new_colv[0] = "sql";
9213        new_colv[1] = 0;
9214        callback(&data, 1, new_argv, new_colv);
9215        sqlite3_free(new_argv[0]);
9216      }
9217    }
9218    if( zDiv ){
9219      sqlite3_stmt *pStmt = 0;
9220      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9221                              -1, &pStmt, 0);
9222      if( rc ){
9223        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9224        sqlite3_finalize(pStmt);
9225        rc = 1;
9226        goto meta_command_exit;
9227      }
9228      appendText(&sSelect, "SELECT sql FROM", 0);
9229      iSchema = 0;
9230      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9231        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9232        char zScNum[30];
9233        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9234        appendText(&sSelect, zDiv, 0);
9235        zDiv = " UNION ALL ";
9236        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9237        if( sqlite3_stricmp(zDb, "main")!=0 ){
9238          appendText(&sSelect, zDb, '\'');
9239        }else{
9240          appendText(&sSelect, "NULL", 0);
9241        }
9242        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9243        appendText(&sSelect, zScNum, 0);
9244        appendText(&sSelect, " AS snum, ", 0);
9245        appendText(&sSelect, zDb, '\'');
9246        appendText(&sSelect, " AS sname FROM ", 0);
9247        appendText(&sSelect, zDb, quoteChar(zDb));
9248        appendText(&sSelect, ".sqlite_schema", 0);
9249      }
9250      sqlite3_finalize(pStmt);
9251#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9252      if( zName ){
9253        appendText(&sSelect,
9254           " UNION ALL SELECT shell_module_schema(name),"
9255           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9256        0);
9257      }
9258#endif
9259      appendText(&sSelect, ") WHERE ", 0);
9260      if( zName ){
9261        char *zQarg = sqlite3_mprintf("%Q", zName);
9262        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9263                    strchr(zName, '[') != 0;
9264        if( strchr(zName, '.') ){
9265          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9266        }else{
9267          appendText(&sSelect, "lower(tbl_name)", 0);
9268        }
9269        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9270        appendText(&sSelect, zQarg, 0);
9271        if( !bGlob ){
9272          appendText(&sSelect, " ESCAPE '\\' ", 0);
9273        }
9274        appendText(&sSelect, " AND ", 0);
9275        sqlite3_free(zQarg);
9276      }
9277      if( bNoSystemTabs ){
9278        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
9279      }
9280      appendText(&sSelect, "sql IS NOT NULL"
9281                           " ORDER BY snum, rowid", 0);
9282      if( bDebug ){
9283        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9284      }else{
9285        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9286      }
9287      freeText(&sSelect);
9288    }
9289    if( zErrMsg ){
9290      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9291      sqlite3_free(zErrMsg);
9292      rc = 1;
9293    }else if( rc != SQLITE_OK ){
9294      raw_printf(stderr,"Error: querying schema information\n");
9295      rc = 1;
9296    }else{
9297      rc = 0;
9298    }
9299  }else
9300
9301  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9302    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
9303    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
9304  }else
9305
9306#if defined(SQLITE_ENABLE_SESSION)
9307  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9308    OpenSession *pSession = &p->aSession[0];
9309    char **azCmd = &azArg[1];
9310    int iSes = 0;
9311    int nCmd = nArg - 1;
9312    int i;
9313    if( nArg<=1 ) goto session_syntax_error;
9314    open_db(p, 0);
9315    if( nArg>=3 ){
9316      for(iSes=0; iSes<p->nSession; iSes++){
9317        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
9318      }
9319      if( iSes<p->nSession ){
9320        pSession = &p->aSession[iSes];
9321        azCmd++;
9322        nCmd--;
9323      }else{
9324        pSession = &p->aSession[0];
9325        iSes = 0;
9326      }
9327    }
9328
9329    /* .session attach TABLE
9330    ** Invoke the sqlite3session_attach() interface to attach a particular
9331    ** table so that it is never filtered.
9332    */
9333    if( strcmp(azCmd[0],"attach")==0 ){
9334      if( nCmd!=2 ) goto session_syntax_error;
9335      if( pSession->p==0 ){
9336        session_not_open:
9337        raw_printf(stderr, "ERROR: No sessions are open\n");
9338      }else{
9339        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9340        if( rc ){
9341          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9342          rc = 0;
9343        }
9344      }
9345    }else
9346
9347    /* .session changeset FILE
9348    ** .session patchset FILE
9349    ** Write a changeset or patchset into a file.  The file is overwritten.
9350    */
9351    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9352      FILE *out = 0;
9353      if( nCmd!=2 ) goto session_syntax_error;
9354      if( pSession->p==0 ) goto session_not_open;
9355      out = fopen(azCmd[1], "wb");
9356      if( out==0 ){
9357        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9358                    azCmd[1]);
9359      }else{
9360        int szChng;
9361        void *pChng;
9362        if( azCmd[0][0]=='c' ){
9363          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9364        }else{
9365          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9366        }
9367        if( rc ){
9368          printf("Error: error code %d\n", rc);
9369          rc = 0;
9370        }
9371        if( pChng
9372          && fwrite(pChng, szChng, 1, out)!=1 ){
9373          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9374                  szChng);
9375        }
9376        sqlite3_free(pChng);
9377        fclose(out);
9378      }
9379    }else
9380
9381    /* .session close
9382    ** Close the identified session
9383    */
9384    if( strcmp(azCmd[0], "close")==0 ){
9385      if( nCmd!=1 ) goto session_syntax_error;
9386      if( p->nSession ){
9387        session_close(pSession);
9388        p->aSession[iSes] = p->aSession[--p->nSession];
9389      }
9390    }else
9391
9392    /* .session enable ?BOOLEAN?
9393    ** Query or set the enable flag
9394    */
9395    if( strcmp(azCmd[0], "enable")==0 ){
9396      int ii;
9397      if( nCmd>2 ) goto session_syntax_error;
9398      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9399      if( p->nSession ){
9400        ii = sqlite3session_enable(pSession->p, ii);
9401        utf8_printf(p->out, "session %s enable flag = %d\n",
9402                    pSession->zName, ii);
9403      }
9404    }else
9405
9406    /* .session filter GLOB ....
9407    ** Set a list of GLOB patterns of table names to be excluded.
9408    */
9409    if( strcmp(azCmd[0], "filter")==0 ){
9410      int ii, nByte;
9411      if( nCmd<2 ) goto session_syntax_error;
9412      if( p->nSession ){
9413        for(ii=0; ii<pSession->nFilter; ii++){
9414          sqlite3_free(pSession->azFilter[ii]);
9415        }
9416        sqlite3_free(pSession->azFilter);
9417        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9418        pSession->azFilter = sqlite3_malloc( nByte );
9419        if( pSession->azFilter==0 ){
9420          raw_printf(stderr, "Error: out or memory\n");
9421          exit(1);
9422        }
9423        for(ii=1; ii<nCmd; ii++){
9424          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9425        }
9426        pSession->nFilter = ii-1;
9427      }
9428    }else
9429
9430    /* .session indirect ?BOOLEAN?
9431    ** Query or set the indirect flag
9432    */
9433    if( strcmp(azCmd[0], "indirect")==0 ){
9434      int ii;
9435      if( nCmd>2 ) goto session_syntax_error;
9436      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9437      if( p->nSession ){
9438        ii = sqlite3session_indirect(pSession->p, ii);
9439        utf8_printf(p->out, "session %s indirect flag = %d\n",
9440                    pSession->zName, ii);
9441      }
9442    }else
9443
9444    /* .session isempty
9445    ** Determine if the session is empty
9446    */
9447    if( strcmp(azCmd[0], "isempty")==0 ){
9448      int ii;
9449      if( nCmd!=1 ) goto session_syntax_error;
9450      if( p->nSession ){
9451        ii = sqlite3session_isempty(pSession->p);
9452        utf8_printf(p->out, "session %s isempty flag = %d\n",
9453                    pSession->zName, ii);
9454      }
9455    }else
9456
9457    /* .session list
9458    ** List all currently open sessions
9459    */
9460    if( strcmp(azCmd[0],"list")==0 ){
9461      for(i=0; i<p->nSession; i++){
9462        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
9463      }
9464    }else
9465
9466    /* .session open DB NAME
9467    ** Open a new session called NAME on the attached database DB.
9468    ** DB is normally "main".
9469    */
9470    if( strcmp(azCmd[0],"open")==0 ){
9471      char *zName;
9472      if( nCmd!=3 ) goto session_syntax_error;
9473      zName = azCmd[2];
9474      if( zName[0]==0 ) goto session_syntax_error;
9475      for(i=0; i<p->nSession; i++){
9476        if( strcmp(p->aSession[i].zName,zName)==0 ){
9477          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9478          goto meta_command_exit;
9479        }
9480      }
9481      if( p->nSession>=ArraySize(p->aSession) ){
9482        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9483        goto meta_command_exit;
9484      }
9485      pSession = &p->aSession[p->nSession];
9486      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9487      if( rc ){
9488        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9489        rc = 0;
9490        goto meta_command_exit;
9491      }
9492      pSession->nFilter = 0;
9493      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9494      p->nSession++;
9495      pSession->zName = sqlite3_mprintf("%s", zName);
9496    }else
9497    /* If no command name matches, show a syntax error */
9498    session_syntax_error:
9499    showHelp(p->out, "session");
9500  }else
9501#endif
9502
9503#ifdef SQLITE_DEBUG
9504  /* Undocumented commands for internal testing.  Subject to change
9505  ** without notice. */
9506  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9507    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9508      int i, v;
9509      for(i=1; i<nArg; i++){
9510        v = booleanValue(azArg[i]);
9511        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9512      }
9513    }
9514    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9515      int i; sqlite3_int64 v;
9516      for(i=1; i<nArg; i++){
9517        char zBuf[200];
9518        v = integerValue(azArg[i]);
9519        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9520        utf8_printf(p->out, "%s", zBuf);
9521      }
9522    }
9523  }else
9524#endif
9525
9526  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9527    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9528    int bVerbose = 0;        /* Verbose output */
9529    int bSelftestExists;     /* True if SELFTEST already exists */
9530    int i, k;                /* Loop counters */
9531    int nTest = 0;           /* Number of tests runs */
9532    int nErr = 0;            /* Number of errors seen */
9533    ShellText str;           /* Answer for a query */
9534    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9535
9536    open_db(p,0);
9537    for(i=1; i<nArg; i++){
9538      const char *z = azArg[i];
9539      if( z[0]=='-' && z[1]=='-' ) z++;
9540      if( strcmp(z,"-init")==0 ){
9541        bIsInit = 1;
9542      }else
9543      if( strcmp(z,"-v")==0 ){
9544        bVerbose++;
9545      }else
9546      {
9547        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9548                    azArg[i], azArg[0]);
9549        raw_printf(stderr, "Should be one of: --init -v\n");
9550        rc = 1;
9551        goto meta_command_exit;
9552      }
9553    }
9554    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9555           != SQLITE_OK ){
9556      bSelftestExists = 0;
9557    }else{
9558      bSelftestExists = 1;
9559    }
9560    if( bIsInit ){
9561      createSelftestTable(p);
9562      bSelftestExists = 1;
9563    }
9564    initText(&str);
9565    appendText(&str, "x", 0);
9566    for(k=bSelftestExists; k>=0; k--){
9567      if( k==1 ){
9568        rc = sqlite3_prepare_v2(p->db,
9569            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9570            -1, &pStmt, 0);
9571      }else{
9572        rc = sqlite3_prepare_v2(p->db,
9573          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9574          "      (1,'run','PRAGMA integrity_check','ok')",
9575          -1, &pStmt, 0);
9576      }
9577      if( rc ){
9578        raw_printf(stderr, "Error querying the selftest table\n");
9579        rc = 1;
9580        sqlite3_finalize(pStmt);
9581        goto meta_command_exit;
9582      }
9583      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9584        int tno = sqlite3_column_int(pStmt, 0);
9585        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9586        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9587        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9588
9589        k = 0;
9590        if( bVerbose>0 ){
9591          char *zQuote = sqlite3_mprintf("%q", zSql);
9592          printf("%d: %s %s\n", tno, zOp, zSql);
9593          sqlite3_free(zQuote);
9594        }
9595        if( strcmp(zOp,"memo")==0 ){
9596          utf8_printf(p->out, "%s\n", zSql);
9597        }else
9598        if( strcmp(zOp,"run")==0 ){
9599          char *zErrMsg = 0;
9600          str.n = 0;
9601          str.z[0] = 0;
9602          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9603          nTest++;
9604          if( bVerbose ){
9605            utf8_printf(p->out, "Result: %s\n", str.z);
9606          }
9607          if( rc || zErrMsg ){
9608            nErr++;
9609            rc = 1;
9610            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9611            sqlite3_free(zErrMsg);
9612          }else if( strcmp(zAns,str.z)!=0 ){
9613            nErr++;
9614            rc = 1;
9615            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9616            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9617          }
9618        }else
9619        {
9620          utf8_printf(stderr,
9621            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9622          rc = 1;
9623          break;
9624        }
9625      } /* End loop over rows of content from SELFTEST */
9626      sqlite3_finalize(pStmt);
9627    } /* End loop over k */
9628    freeText(&str);
9629    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9630  }else
9631
9632  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9633    if( nArg<2 || nArg>3 ){
9634      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9635      rc = 1;
9636    }
9637    if( nArg>=2 ){
9638      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9639                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9640    }
9641    if( nArg>=3 ){
9642      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9643                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9644    }
9645  }else
9646
9647  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9648    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9649    int i;                   /* Loop counter */
9650    int bSchema = 0;         /* Also hash the schema */
9651    int bSeparate = 0;       /* Hash each table separately */
9652    int iSize = 224;         /* Hash algorithm to use */
9653    int bDebug = 0;          /* Only show the query that would have run */
9654    sqlite3_stmt *pStmt;     /* For querying tables names */
9655    char *zSql;              /* SQL to be run */
9656    char *zSep;              /* Separator */
9657    ShellText sSql;          /* Complete SQL for the query to run the hash */
9658    ShellText sQuery;        /* Set of queries used to read all content */
9659    open_db(p, 0);
9660    for(i=1; i<nArg; i++){
9661      const char *z = azArg[i];
9662      if( z[0]=='-' ){
9663        z++;
9664        if( z[0]=='-' ) z++;
9665        if( strcmp(z,"schema")==0 ){
9666          bSchema = 1;
9667        }else
9668        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9669         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9670        ){
9671          iSize = atoi(&z[5]);
9672        }else
9673        if( strcmp(z,"debug")==0 ){
9674          bDebug = 1;
9675        }else
9676        {
9677          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9678                      azArg[i], azArg[0]);
9679          showHelp(p->out, azArg[0]);
9680          rc = 1;
9681          goto meta_command_exit;
9682        }
9683      }else if( zLike ){
9684        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9685        rc = 1;
9686        goto meta_command_exit;
9687      }else{
9688        zLike = z;
9689        bSeparate = 1;
9690        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9691      }
9692    }
9693    if( bSchema ){
9694      zSql = "SELECT lower(name) FROM sqlite_schema"
9695             " WHERE type='table' AND coalesce(rootpage,0)>1"
9696             " UNION ALL SELECT 'sqlite_schema'"
9697             " ORDER BY 1 collate nocase";
9698    }else{
9699      zSql = "SELECT lower(name) FROM sqlite_schema"
9700             " WHERE type='table' AND coalesce(rootpage,0)>1"
9701             " AND name NOT LIKE 'sqlite_%'"
9702             " ORDER BY 1 collate nocase";
9703    }
9704    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9705    initText(&sQuery);
9706    initText(&sSql);
9707    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9708    zSep = "VALUES(";
9709    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9710      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9711      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9712      if( strncmp(zTab, "sqlite_",7)!=0 ){
9713        appendText(&sQuery,"SELECT * FROM ", 0);
9714        appendText(&sQuery,zTab,'"');
9715        appendText(&sQuery," NOT INDEXED;", 0);
9716      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9717        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9718                           " ORDER BY name;", 0);
9719      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9720        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9721                           " ORDER BY name;", 0);
9722      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9723        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9724                           " ORDER BY tbl,idx;", 0);
9725      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9726        appendText(&sQuery, "SELECT * FROM ", 0);
9727        appendText(&sQuery, zTab, 0);
9728        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9729      }
9730      appendText(&sSql, zSep, 0);
9731      appendText(&sSql, sQuery.z, '\'');
9732      sQuery.n = 0;
9733      appendText(&sSql, ",", 0);
9734      appendText(&sSql, zTab, '\'');
9735      zSep = "),(";
9736    }
9737    sqlite3_finalize(pStmt);
9738    if( bSeparate ){
9739      zSql = sqlite3_mprintf(
9740          "%s))"
9741          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9742          "   FROM [sha3sum$query]",
9743          sSql.z, iSize);
9744    }else{
9745      zSql = sqlite3_mprintf(
9746          "%s))"
9747          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9748          "   FROM [sha3sum$query]",
9749          sSql.z, iSize);
9750    }
9751    freeText(&sQuery);
9752    freeText(&sSql);
9753    if( bDebug ){
9754      utf8_printf(p->out, "%s\n", zSql);
9755    }else{
9756      shell_exec(p, zSql, 0);
9757    }
9758    sqlite3_free(zSql);
9759  }else
9760
9761#ifndef SQLITE_NOHAVE_SYSTEM
9762  if( c=='s'
9763   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9764  ){
9765    char *zCmd;
9766    int i, x;
9767    if( nArg<2 ){
9768      raw_printf(stderr, "Usage: .system COMMAND\n");
9769      rc = 1;
9770      goto meta_command_exit;
9771    }
9772    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9773    for(i=2; i<nArg; i++){
9774      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9775                             zCmd, azArg[i]);
9776    }
9777    x = system(zCmd);
9778    sqlite3_free(zCmd);
9779    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9780  }else
9781#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9782
9783  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9784    static const char *azBool[] = { "off", "on", "trigger", "full"};
9785    const char *zOut;
9786    int i;
9787    if( nArg!=1 ){
9788      raw_printf(stderr, "Usage: .show\n");
9789      rc = 1;
9790      goto meta_command_exit;
9791    }
9792    utf8_printf(p->out, "%12.12s: %s\n","echo",
9793                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9794    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9795    utf8_printf(p->out, "%12.12s: %s\n","explain",
9796         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9797    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9798    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9799    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9800      output_c_string(p->out, p->nullValue);
9801      raw_printf(p->out, "\n");
9802    utf8_printf(p->out,"%12.12s: %s\n","output",
9803            strlen30(p->outfile) ? p->outfile : "stdout");
9804    utf8_printf(p->out,"%12.12s: ", "colseparator");
9805      output_c_string(p->out, p->colSeparator);
9806      raw_printf(p->out, "\n");
9807    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9808      output_c_string(p->out, p->rowSeparator);
9809      raw_printf(p->out, "\n");
9810    switch( p->statsOn ){
9811      case 0:  zOut = "off";     break;
9812      default: zOut = "on";      break;
9813      case 2:  zOut = "stmt";    break;
9814      case 3:  zOut = "vmstep";  break;
9815    }
9816    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
9817    utf8_printf(p->out, "%12.12s: ", "width");
9818    for (i=0;i<p->nWidth;i++) {
9819      raw_printf(p->out, "%d ", p->colWidth[i]);
9820    }
9821    raw_printf(p->out, "\n");
9822    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9823                p->zDbFilename ? p->zDbFilename : "");
9824  }else
9825
9826  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9827    if( nArg==2 ){
9828      if( strcmp(azArg[1],"stmt")==0 ){
9829        p->statsOn = 2;
9830      }else if( strcmp(azArg[1],"vmstep")==0 ){
9831        p->statsOn = 3;
9832      }else{
9833        p->statsOn = (u8)booleanValue(azArg[1]);
9834      }
9835    }else if( nArg==1 ){
9836      display_stats(p->db, p, 0);
9837    }else{
9838      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
9839      rc = 1;
9840    }
9841  }else
9842
9843  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9844   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9845                 || strncmp(azArg[0], "indexes", n)==0) )
9846  ){
9847    sqlite3_stmt *pStmt;
9848    char **azResult;
9849    int nRow, nAlloc;
9850    int ii;
9851    ShellText s;
9852    initText(&s);
9853    open_db(p, 0);
9854    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9855    if( rc ){
9856      sqlite3_finalize(pStmt);
9857      return shellDatabaseError(p->db);
9858    }
9859
9860    if( nArg>2 && c=='i' ){
9861      /* It is an historical accident that the .indexes command shows an error
9862      ** when called with the wrong number of arguments whereas the .tables
9863      ** command does not. */
9864      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9865      rc = 1;
9866      sqlite3_finalize(pStmt);
9867      goto meta_command_exit;
9868    }
9869    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9870      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9871      if( zDbName==0 ) continue;
9872      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9873      if( sqlite3_stricmp(zDbName, "main")==0 ){
9874        appendText(&s, "SELECT name FROM ", 0);
9875      }else{
9876        appendText(&s, "SELECT ", 0);
9877        appendText(&s, zDbName, '\'');
9878        appendText(&s, "||'.'||name FROM ", 0);
9879      }
9880      appendText(&s, zDbName, '"');
9881      appendText(&s, ".sqlite_schema ", 0);
9882      if( c=='t' ){
9883        appendText(&s," WHERE type IN ('table','view')"
9884                      "   AND name NOT LIKE 'sqlite_%'"
9885                      "   AND name LIKE ?1", 0);
9886      }else{
9887        appendText(&s," WHERE type='index'"
9888                      "   AND tbl_name LIKE ?1", 0);
9889      }
9890    }
9891    rc = sqlite3_finalize(pStmt);
9892    appendText(&s, " ORDER BY 1", 0);
9893    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9894    freeText(&s);
9895    if( rc ) return shellDatabaseError(p->db);
9896
9897    /* Run the SQL statement prepared by the above block. Store the results
9898    ** as an array of nul-terminated strings in azResult[].  */
9899    nRow = nAlloc = 0;
9900    azResult = 0;
9901    if( nArg>1 ){
9902      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9903    }else{
9904      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9905    }
9906    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9907      if( nRow>=nAlloc ){
9908        char **azNew;
9909        int n2 = nAlloc*2 + 10;
9910        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9911        if( azNew==0 ) shell_out_of_memory();
9912        nAlloc = n2;
9913        azResult = azNew;
9914      }
9915      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9916      if( 0==azResult[nRow] ) shell_out_of_memory();
9917      nRow++;
9918    }
9919    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9920      rc = shellDatabaseError(p->db);
9921    }
9922
9923    /* Pretty-print the contents of array azResult[] to the output */
9924    if( rc==0 && nRow>0 ){
9925      int len, maxlen = 0;
9926      int i, j;
9927      int nPrintCol, nPrintRow;
9928      for(i=0; i<nRow; i++){
9929        len = strlen30(azResult[i]);
9930        if( len>maxlen ) maxlen = len;
9931      }
9932      nPrintCol = 80/(maxlen+2);
9933      if( nPrintCol<1 ) nPrintCol = 1;
9934      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9935      for(i=0; i<nPrintRow; i++){
9936        for(j=i; j<nRow; j+=nPrintRow){
9937          char *zSp = j<nPrintRow ? "" : "  ";
9938          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9939                      azResult[j] ? azResult[j]:"");
9940        }
9941        raw_printf(p->out, "\n");
9942      }
9943    }
9944
9945    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9946    sqlite3_free(azResult);
9947  }else
9948
9949  /* Begin redirecting output to the file "testcase-out.txt" */
9950  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9951    output_reset(p);
9952    p->out = output_file_open("testcase-out.txt", 0);
9953    if( p->out==0 ){
9954      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9955    }
9956    if( nArg>=2 ){
9957      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9958    }else{
9959      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9960    }
9961  }else
9962
9963#ifndef SQLITE_UNTESTABLE
9964  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9965    static const struct {
9966       const char *zCtrlName;   /* Name of a test-control option */
9967       int ctrlCode;            /* Integer code for that option */
9968       const char *zUsage;      /* Usage notes */
9969    } aCtrl[] = {
9970      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9971      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9972    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9973    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9974      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9975      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9976    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9977      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9978      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9979      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9980      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9981      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9982#ifdef YYCOVERAGE
9983      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9984#endif
9985      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9986      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9987      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9988      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9989      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
9990      { "tune",               SQLITE_TESTCTRL_TUNE,          "ID VALUE"       },
9991    };
9992    int testctrl = -1;
9993    int iCtrl = -1;
9994    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9995    int isOk = 0;
9996    int i, n2;
9997    const char *zCmd = 0;
9998
9999    open_db(p, 0);
10000    zCmd = nArg>=2 ? azArg[1] : "help";
10001
10002    /* The argument can optionally begin with "-" or "--" */
10003    if( zCmd[0]=='-' && zCmd[1] ){
10004      zCmd++;
10005      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10006    }
10007
10008    /* --help lists all test-controls */
10009    if( strcmp(zCmd,"help")==0 ){
10010      utf8_printf(p->out, "Available test-controls:\n");
10011      for(i=0; i<ArraySize(aCtrl); i++){
10012        utf8_printf(p->out, "  .testctrl %s %s\n",
10013                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10014      }
10015      rc = 1;
10016      goto meta_command_exit;
10017    }
10018
10019    /* convert testctrl text option to value. allow any unique prefix
10020    ** of the option name, or a numerical value. */
10021    n2 = strlen30(zCmd);
10022    for(i=0; i<ArraySize(aCtrl); i++){
10023      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10024        if( testctrl<0 ){
10025          testctrl = aCtrl[i].ctrlCode;
10026          iCtrl = i;
10027        }else{
10028          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
10029                              "Use \".testctrl --help\" for help\n", zCmd);
10030          rc = 1;
10031          goto meta_command_exit;
10032        }
10033      }
10034    }
10035    if( testctrl<0 ){
10036      utf8_printf(stderr,"Error: unknown test-control: %s\n"
10037                         "Use \".testctrl --help\" for help\n", zCmd);
10038    }else{
10039      switch(testctrl){
10040
10041        /* sqlite3_test_control(int, db, int) */
10042        case SQLITE_TESTCTRL_OPTIMIZATIONS:
10043          if( nArg==3 ){
10044            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
10045            rc2 = sqlite3_test_control(testctrl, p->db, opt);
10046            isOk = 3;
10047          }
10048          break;
10049
10050        /* sqlite3_test_control(int) */
10051        case SQLITE_TESTCTRL_PRNG_SAVE:
10052        case SQLITE_TESTCTRL_PRNG_RESTORE:
10053        case SQLITE_TESTCTRL_BYTEORDER:
10054          if( nArg==2 ){
10055            rc2 = sqlite3_test_control(testctrl);
10056            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
10057          }
10058          break;
10059
10060        /* sqlite3_test_control(int, uint) */
10061        case SQLITE_TESTCTRL_PENDING_BYTE:
10062          if( nArg==3 ){
10063            unsigned int opt = (unsigned int)integerValue(azArg[2]);
10064            rc2 = sqlite3_test_control(testctrl, opt);
10065            isOk = 3;
10066          }
10067          break;
10068
10069        /* sqlite3_test_control(int, int, sqlite3*) */
10070        case SQLITE_TESTCTRL_PRNG_SEED:
10071          if( nArg==3 || nArg==4 ){
10072            int ii = (int)integerValue(azArg[2]);
10073            sqlite3 *db;
10074            if( ii==0 && strcmp(azArg[2],"random")==0 ){
10075              sqlite3_randomness(sizeof(ii),&ii);
10076              printf("-- random seed: %d\n", ii);
10077            }
10078            if( nArg==3 ){
10079              db = 0;
10080            }else{
10081              db = p->db;
10082              /* Make sure the schema has been loaded */
10083              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10084            }
10085            rc2 = sqlite3_test_control(testctrl, ii, db);
10086            isOk = 3;
10087          }
10088          break;
10089
10090        /* sqlite3_test_control(int, int) */
10091        case SQLITE_TESTCTRL_ASSERT:
10092        case SQLITE_TESTCTRL_ALWAYS:
10093          if( nArg==3 ){
10094            int opt = booleanValue(azArg[2]);
10095            rc2 = sqlite3_test_control(testctrl, opt);
10096            isOk = 1;
10097          }
10098          break;
10099
10100        /* sqlite3_test_control(int, int) */
10101        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10102        case SQLITE_TESTCTRL_NEVER_CORRUPT:
10103          if( nArg==3 ){
10104            int opt = booleanValue(azArg[2]);
10105            rc2 = sqlite3_test_control(testctrl, opt);
10106            isOk = 3;
10107          }
10108          break;
10109
10110        /* sqlite3_test_control(sqlite3*) */
10111        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10112          rc2 = sqlite3_test_control(testctrl, p->db);
10113          isOk = 3;
10114          break;
10115
10116        case SQLITE_TESTCTRL_IMPOSTER:
10117          if( nArg==5 ){
10118            rc2 = sqlite3_test_control(testctrl, p->db,
10119                          azArg[2],
10120                          integerValue(azArg[3]),
10121                          integerValue(azArg[4]));
10122            isOk = 3;
10123          }
10124          break;
10125
10126        case SQLITE_TESTCTRL_SEEK_COUNT: {
10127          u64 x = 0;
10128          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10129          utf8_printf(p->out, "%llu\n", x);
10130          isOk = 3;
10131          break;
10132        }
10133
10134#ifdef YYCOVERAGE
10135        case SQLITE_TESTCTRL_PARSER_COVERAGE: {
10136          if( nArg==2 ){
10137            sqlite3_test_control(testctrl, p->out);
10138            isOk = 3;
10139          }
10140          break;
10141        }
10142#endif
10143#ifdef SQLITE_DEBUG
10144        case SQLITE_TESTCTRL_TUNE: {
10145          if( nArg==4 ){
10146            int id = (int)integerValue(azArg[2]);
10147            int val = (int)integerValue(azArg[3]);
10148            sqlite3_test_control(testctrl, id, &val);
10149            isOk = 3;
10150          }else if( nArg==3 ){
10151            int id = (int)integerValue(azArg[2]);
10152            sqlite3_test_control(testctrl, -id, &rc2);
10153            isOk = 1;
10154          }else if( nArg==2 ){
10155            int id = 1;
10156            while(1){
10157              int val = 0;
10158              rc2 = sqlite3_test_control(testctrl, -id, &val);
10159              if( rc2!=SQLITE_OK ) break;
10160              if( id>1 ) utf8_printf(p->out, "  ");
10161              utf8_printf(p->out, "%d: %d", id, val);
10162              id++;
10163            }
10164            if( id>1 ) utf8_printf(p->out, "\n");
10165            isOk = 3;
10166          }
10167          break;
10168        }
10169#endif
10170      }
10171    }
10172    if( isOk==0 && iCtrl>=0 ){
10173      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10174      rc = 1;
10175    }else if( isOk==1 ){
10176      raw_printf(p->out, "%d\n", rc2);
10177    }else if( isOk==2 ){
10178      raw_printf(p->out, "0x%08x\n", rc2);
10179    }
10180  }else
10181#endif /* !defined(SQLITE_UNTESTABLE) */
10182
10183  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10184    open_db(p, 0);
10185    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10186  }else
10187
10188  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10189    if( nArg==2 ){
10190      enableTimer = booleanValue(azArg[1]);
10191      if( enableTimer && !HAS_TIMER ){
10192        raw_printf(stderr, "Error: timer not available on this system.\n");
10193        enableTimer = 0;
10194      }
10195    }else{
10196      raw_printf(stderr, "Usage: .timer on|off\n");
10197      rc = 1;
10198    }
10199  }else
10200
10201#ifndef SQLITE_OMIT_TRACE
10202  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10203    int mType = 0;
10204    int jj;
10205    open_db(p, 0);
10206    for(jj=1; jj<nArg; jj++){
10207      const char *z = azArg[jj];
10208      if( z[0]=='-' ){
10209        if( optionMatch(z, "expanded") ){
10210          p->eTraceType = SHELL_TRACE_EXPANDED;
10211        }
10212#ifdef SQLITE_ENABLE_NORMALIZE
10213        else if( optionMatch(z, "normalized") ){
10214          p->eTraceType = SHELL_TRACE_NORMALIZED;
10215        }
10216#endif
10217        else if( optionMatch(z, "plain") ){
10218          p->eTraceType = SHELL_TRACE_PLAIN;
10219        }
10220        else if( optionMatch(z, "profile") ){
10221          mType |= SQLITE_TRACE_PROFILE;
10222        }
10223        else if( optionMatch(z, "row") ){
10224          mType |= SQLITE_TRACE_ROW;
10225        }
10226        else if( optionMatch(z, "stmt") ){
10227          mType |= SQLITE_TRACE_STMT;
10228        }
10229        else if( optionMatch(z, "close") ){
10230          mType |= SQLITE_TRACE_CLOSE;
10231        }
10232        else {
10233          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10234          rc = 1;
10235          goto meta_command_exit;
10236        }
10237      }else{
10238        output_file_close(p->traceOut);
10239        p->traceOut = output_file_open(azArg[1], 0);
10240      }
10241    }
10242    if( p->traceOut==0 ){
10243      sqlite3_trace_v2(p->db, 0, 0, 0);
10244    }else{
10245      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10246      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10247    }
10248  }else
10249#endif /* !defined(SQLITE_OMIT_TRACE) */
10250
10251#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10252  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10253    int ii;
10254    int lenOpt;
10255    char *zOpt;
10256    if( nArg<2 ){
10257      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10258      rc = 1;
10259      goto meta_command_exit;
10260    }
10261    open_db(p, 0);
10262    zOpt = azArg[1];
10263    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10264    lenOpt = (int)strlen(zOpt);
10265    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10266      assert( azArg[nArg]==0 );
10267      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10268    }else{
10269      for(ii=1; ii<nArg; ii++){
10270        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10271      }
10272    }
10273  }else
10274#endif
10275
10276#if SQLITE_USER_AUTHENTICATION
10277  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10278    if( nArg<2 ){
10279      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10280      rc = 1;
10281      goto meta_command_exit;
10282    }
10283    open_db(p, 0);
10284    if( strcmp(azArg[1],"login")==0 ){
10285      if( nArg!=4 ){
10286        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10287        rc = 1;
10288        goto meta_command_exit;
10289      }
10290      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10291                                     strlen30(azArg[3]));
10292      if( rc ){
10293        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10294        rc = 1;
10295      }
10296    }else if( strcmp(azArg[1],"add")==0 ){
10297      if( nArg!=5 ){
10298        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10299        rc = 1;
10300        goto meta_command_exit;
10301      }
10302      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10303                            booleanValue(azArg[4]));
10304      if( rc ){
10305        raw_printf(stderr, "User-Add failed: %d\n", rc);
10306        rc = 1;
10307      }
10308    }else if( strcmp(azArg[1],"edit")==0 ){
10309      if( nArg!=5 ){
10310        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10311        rc = 1;
10312        goto meta_command_exit;
10313      }
10314      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10315                              booleanValue(azArg[4]));
10316      if( rc ){
10317        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10318        rc = 1;
10319      }
10320    }else if( strcmp(azArg[1],"delete")==0 ){
10321      if( nArg!=3 ){
10322        raw_printf(stderr, "Usage: .user delete USER\n");
10323        rc = 1;
10324        goto meta_command_exit;
10325      }
10326      rc = sqlite3_user_delete(p->db, azArg[2]);
10327      if( rc ){
10328        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10329        rc = 1;
10330      }
10331    }else{
10332      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10333      rc = 1;
10334      goto meta_command_exit;
10335    }
10336  }else
10337#endif /* SQLITE_USER_AUTHENTICATION */
10338
10339  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10340    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10341        sqlite3_libversion(), sqlite3_sourceid());
10342#if SQLITE_HAVE_ZLIB
10343    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10344#endif
10345#define CTIMEOPT_VAL_(opt) #opt
10346#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10347#if defined(__clang__) && defined(__clang_major__)
10348    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10349                    CTIMEOPT_VAL(__clang_minor__) "."
10350                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10351#elif defined(_MSC_VER)
10352    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10353#elif defined(__GNUC__) && defined(__VERSION__)
10354    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10355#endif
10356  }else
10357
10358  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10359    const char *zDbName = nArg==2 ? azArg[1] : "main";
10360    sqlite3_vfs *pVfs = 0;
10361    if( p->db ){
10362      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10363      if( pVfs ){
10364        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10365        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10366        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10367        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10368      }
10369    }
10370  }else
10371
10372  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10373    sqlite3_vfs *pVfs;
10374    sqlite3_vfs *pCurrent = 0;
10375    if( p->db ){
10376      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10377    }
10378    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10379      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10380           pVfs==pCurrent ? "  <--- CURRENT" : "");
10381      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10382      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10383      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10384      if( pVfs->pNext ){
10385        raw_printf(p->out, "-----------------------------------\n");
10386      }
10387    }
10388  }else
10389
10390  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10391    const char *zDbName = nArg==2 ? azArg[1] : "main";
10392    char *zVfsName = 0;
10393    if( p->db ){
10394      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10395      if( zVfsName ){
10396        utf8_printf(p->out, "%s\n", zVfsName);
10397        sqlite3_free(zVfsName);
10398      }
10399    }
10400  }else
10401
10402  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10403    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10404    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
10405  }else
10406
10407  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10408    int j;
10409    assert( nArg<=ArraySize(azArg) );
10410    p->nWidth = nArg-1;
10411    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10412    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10413    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10414    for(j=1; j<nArg; j++){
10415      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10416    }
10417  }else
10418
10419  {
10420    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10421      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10422    rc = 1;
10423  }
10424
10425meta_command_exit:
10426  if( p->outCount ){
10427    p->outCount--;
10428    if( p->outCount==0 ) output_reset(p);
10429  }
10430  return rc;
10431}
10432
10433/*
10434** Return TRUE if a semicolon occurs anywhere in the first N characters
10435** of string z[].
10436*/
10437static int line_contains_semicolon(const char *z, int N){
10438  int i;
10439  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
10440  return 0;
10441}
10442
10443/*
10444** Test to see if a line consists entirely of whitespace.
10445*/
10446static int _all_whitespace(const char *z){
10447  for(; *z; z++){
10448    if( IsSpace(z[0]) ) continue;
10449    if( *z=='/' && z[1]=='*' ){
10450      z += 2;
10451      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
10452      if( *z==0 ) return 0;
10453      z++;
10454      continue;
10455    }
10456    if( *z=='-' && z[1]=='-' ){
10457      z += 2;
10458      while( *z && *z!='\n' ){ z++; }
10459      if( *z==0 ) return 1;
10460      continue;
10461    }
10462    return 0;
10463  }
10464  return 1;
10465}
10466
10467/*
10468** Return TRUE if the line typed in is an SQL command terminator other
10469** than a semi-colon.  The SQL Server style "go" command is understood
10470** as is the Oracle "/".
10471*/
10472static int line_is_command_terminator(const char *zLine){
10473  while( IsSpace(zLine[0]) ){ zLine++; };
10474  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
10475    return 1;  /* Oracle */
10476  }
10477  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
10478         && _all_whitespace(&zLine[2]) ){
10479    return 1;  /* SQL Server */
10480  }
10481  return 0;
10482}
10483
10484/*
10485** We need a default sqlite3_complete() implementation to use in case
10486** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10487** any arbitrary text is a complete SQL statement.  This is not very
10488** user-friendly, but it does seem to work.
10489*/
10490#ifdef SQLITE_OMIT_COMPLETE
10491#define sqlite3_complete(x) 1
10492#endif
10493
10494/*
10495** Return true if zSql is a complete SQL statement.  Return false if it
10496** ends in the middle of a string literal or C-style comment.
10497*/
10498static int line_is_complete(char *zSql, int nSql){
10499  int rc;
10500  if( zSql==0 ) return 1;
10501  zSql[nSql] = ';';
10502  zSql[nSql+1] = 0;
10503  rc = sqlite3_complete(zSql);
10504  zSql[nSql] = 0;
10505  return rc;
10506}
10507
10508/*
10509** Run a single line of SQL.  Return the number of errors.
10510*/
10511static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10512  int rc;
10513  char *zErrMsg = 0;
10514
10515  open_db(p, 0);
10516  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10517  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10518  BEGIN_TIMER;
10519  rc = shell_exec(p, zSql, &zErrMsg);
10520  END_TIMER;
10521  if( rc || zErrMsg ){
10522    char zPrefix[100];
10523    if( in!=0 || !stdin_is_interactive ){
10524      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10525                       "Error: near line %d:", startline);
10526    }else{
10527      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10528    }
10529    if( zErrMsg!=0 ){
10530      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10531      sqlite3_free(zErrMsg);
10532      zErrMsg = 0;
10533    }else{
10534      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10535    }
10536    return 1;
10537  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10538    raw_printf(p->out, "changes: %3lld   total_changes: %lld\n",
10539            sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
10540  }
10541  return 0;
10542}
10543
10544
10545/*
10546** Read input from *in and process it.  If *in==0 then input
10547** is interactive - the user is typing it it.  Otherwise, input
10548** is coming from a file or device.  A prompt is issued and history
10549** is saved only if input is interactive.  An interrupt signal will
10550** cause this routine to exit immediately, unless input is interactive.
10551**
10552** Return the number of errors.
10553*/
10554static int process_input(ShellState *p){
10555  char *zLine = 0;          /* A single input line */
10556  char *zSql = 0;           /* Accumulated SQL text */
10557  int nLine;                /* Length of current line */
10558  int nSql = 0;             /* Bytes of zSql[] used */
10559  int nAlloc = 0;           /* Allocated zSql[] space */
10560  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10561  int rc;                   /* Error code */
10562  int errCnt = 0;           /* Number of errors seen */
10563  int startline = 0;        /* Line number for start of current input */
10564
10565  p->lineno = 0;
10566  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10567    fflush(p->out);
10568    zLine = one_input_line(p->in, zLine, nSql>0);
10569    if( zLine==0 ){
10570      /* End of input */
10571      if( p->in==0 && stdin_is_interactive ) printf("\n");
10572      break;
10573    }
10574    if( seenInterrupt ){
10575      if( p->in!=0 ) break;
10576      seenInterrupt = 0;
10577    }
10578    p->lineno++;
10579    if( nSql==0 && _all_whitespace(zLine) ){
10580      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10581      continue;
10582    }
10583    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10584      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10585      if( zLine[0]=='.' ){
10586        rc = do_meta_command(zLine, p);
10587        if( rc==2 ){ /* exit requested */
10588          break;
10589        }else if( rc ){
10590          errCnt++;
10591        }
10592      }
10593      continue;
10594    }
10595    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10596      memcpy(zLine,";",2);
10597    }
10598    nLine = strlen30(zLine);
10599    if( nSql+nLine+2>=nAlloc ){
10600      nAlloc = nSql+nLine+100;
10601      zSql = realloc(zSql, nAlloc);
10602      if( zSql==0 ) shell_out_of_memory();
10603    }
10604    nSqlPrior = nSql;
10605    if( nSql==0 ){
10606      int i;
10607      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10608      assert( nAlloc>0 && zSql!=0 );
10609      memcpy(zSql, zLine+i, nLine+1-i);
10610      startline = p->lineno;
10611      nSql = nLine-i;
10612    }else{
10613      zSql[nSql++] = '\n';
10614      memcpy(zSql+nSql, zLine, nLine+1);
10615      nSql += nLine;
10616    }
10617    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10618                && sqlite3_complete(zSql) ){
10619      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10620      nSql = 0;
10621      if( p->outCount ){
10622        output_reset(p);
10623        p->outCount = 0;
10624      }else{
10625        clearTempFile(p);
10626      }
10627    }else if( nSql && _all_whitespace(zSql) ){
10628      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10629      nSql = 0;
10630    }
10631  }
10632  if( nSql && !_all_whitespace(zSql) ){
10633    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10634  }
10635  free(zSql);
10636  free(zLine);
10637  return errCnt>0;
10638}
10639
10640/*
10641** Return a pathname which is the user's home directory.  A
10642** 0 return indicates an error of some kind.
10643*/
10644static char *find_home_dir(int clearFlag){
10645  static char *home_dir = NULL;
10646  if( clearFlag ){
10647    free(home_dir);
10648    home_dir = 0;
10649    return 0;
10650  }
10651  if( home_dir ) return home_dir;
10652
10653#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10654     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10655  {
10656    struct passwd *pwent;
10657    uid_t uid = getuid();
10658    if( (pwent=getpwuid(uid)) != NULL) {
10659      home_dir = pwent->pw_dir;
10660    }
10661  }
10662#endif
10663
10664#if defined(_WIN32_WCE)
10665  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10666   */
10667  home_dir = "/";
10668#else
10669
10670#if defined(_WIN32) || defined(WIN32)
10671  if (!home_dir) {
10672    home_dir = getenv("USERPROFILE");
10673  }
10674#endif
10675
10676  if (!home_dir) {
10677    home_dir = getenv("HOME");
10678  }
10679
10680#if defined(_WIN32) || defined(WIN32)
10681  if (!home_dir) {
10682    char *zDrive, *zPath;
10683    int n;
10684    zDrive = getenv("HOMEDRIVE");
10685    zPath = getenv("HOMEPATH");
10686    if( zDrive && zPath ){
10687      n = strlen30(zDrive) + strlen30(zPath) + 1;
10688      home_dir = malloc( n );
10689      if( home_dir==0 ) return 0;
10690      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10691      return home_dir;
10692    }
10693    home_dir = "c:\\";
10694  }
10695#endif
10696
10697#endif /* !_WIN32_WCE */
10698
10699  if( home_dir ){
10700    int n = strlen30(home_dir) + 1;
10701    char *z = malloc( n );
10702    if( z ) memcpy(z, home_dir, n);
10703    home_dir = z;
10704  }
10705
10706  return home_dir;
10707}
10708
10709/*
10710** Read input from the file given by sqliterc_override.  Or if that
10711** parameter is NULL, take input from ~/.sqliterc
10712**
10713** Returns the number of errors.
10714*/
10715static void process_sqliterc(
10716  ShellState *p,                  /* Configuration data */
10717  const char *sqliterc_override   /* Name of config file. NULL to use default */
10718){
10719  char *home_dir = NULL;
10720  const char *sqliterc = sqliterc_override;
10721  char *zBuf = 0;
10722  FILE *inSaved = p->in;
10723  int savedLineno = p->lineno;
10724
10725  if (sqliterc == NULL) {
10726    home_dir = find_home_dir(0);
10727    if( home_dir==0 ){
10728      raw_printf(stderr, "-- warning: cannot find home directory;"
10729                      " cannot read ~/.sqliterc\n");
10730      return;
10731    }
10732    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10733    sqliterc = zBuf;
10734  }
10735  p->in = fopen(sqliterc,"rb");
10736  if( p->in ){
10737    if( stdin_is_interactive ){
10738      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10739    }
10740    if( process_input(p) && bail_on_error ) exit(1);
10741    fclose(p->in);
10742  }else if( sqliterc_override!=0 ){
10743    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
10744    if( bail_on_error ) exit(1);
10745  }
10746  p->in = inSaved;
10747  p->lineno = savedLineno;
10748  sqlite3_free(zBuf);
10749}
10750
10751/*
10752** Show available command line options
10753*/
10754static const char zOptions[] =
10755#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10756  "   -A ARGS...           run \".archive ARGS\" and exit\n"
10757#endif
10758  "   -append              append the database to the end of the file\n"
10759  "   -ascii               set output mode to 'ascii'\n"
10760  "   -bail                stop after hitting an error\n"
10761  "   -batch               force batch I/O\n"
10762  "   -box                 set output mode to 'box'\n"
10763  "   -column              set output mode to 'column'\n"
10764  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
10765  "   -csv                 set output mode to 'csv'\n"
10766#if !defined(SQLITE_OMIT_DESERIALIZE)
10767  "   -deserialize         open the database using sqlite3_deserialize()\n"
10768#endif
10769  "   -echo                print commands before execution\n"
10770  "   -init FILENAME       read/process named file\n"
10771  "   -[no]header          turn headers on or off\n"
10772#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10773  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
10774#endif
10775  "   -help                show this message\n"
10776  "   -html                set output mode to HTML\n"
10777  "   -interactive         force interactive I/O\n"
10778  "   -json                set output mode to 'json'\n"
10779  "   -line                set output mode to 'line'\n"
10780  "   -list                set output mode to 'list'\n"
10781  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
10782  "   -markdown            set output mode to 'markdown'\n"
10783#if !defined(SQLITE_OMIT_DESERIALIZE)
10784  "   -maxsize N           maximum size for a --deserialize database\n"
10785#endif
10786  "   -memtrace            trace all memory allocations and deallocations\n"
10787  "   -mmap N              default mmap size set to N\n"
10788#ifdef SQLITE_ENABLE_MULTIPLEX
10789  "   -multiplex           enable the multiplexor VFS\n"
10790#endif
10791  "   -newline SEP         set output row separator. Default: '\\n'\n"
10792  "   -nofollow            refuse to open symbolic links to database files\n"
10793  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
10794  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
10795  "   -quote               set output mode to 'quote'\n"
10796  "   -readonly            open the database read-only\n"
10797  "   -separator SEP       set output column separator. Default: '|'\n"
10798#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10799  "   -sorterref SIZE      sorter references threshold size\n"
10800#endif
10801  "   -stats               print memory stats before each finalize\n"
10802  "   -table               set output mode to 'table'\n"
10803  "   -tabs                set output mode to 'tabs'\n"
10804  "   -version             show SQLite version\n"
10805  "   -vfs NAME            use NAME as the default VFS\n"
10806#ifdef SQLITE_ENABLE_VFSTRACE
10807  "   -vfstrace            enable tracing of all VFS calls\n"
10808#endif
10809#ifdef SQLITE_HAVE_ZLIB
10810  "   -zip                 open the file as a ZIP Archive\n"
10811#endif
10812;
10813static void usage(int showDetail){
10814  utf8_printf(stderr,
10815      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10816      "FILENAME is the name of an SQLite database. A new database is created\n"
10817      "if the file does not previously exist.\n", Argv0);
10818  if( showDetail ){
10819    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10820  }else{
10821    raw_printf(stderr, "Use the -help option for additional information\n");
10822  }
10823  exit(1);
10824}
10825
10826/*
10827** Internal check:  Verify that the SQLite is uninitialized.  Print a
10828** error message if it is initialized.
10829*/
10830static void verify_uninitialized(void){
10831  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10832    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10833                        " initialization.\n");
10834  }
10835}
10836
10837/*
10838** Initialize the state information in data
10839*/
10840static void main_init(ShellState *data) {
10841  memset(data, 0, sizeof(*data));
10842  data->normalMode = data->cMode = data->mode = MODE_List;
10843  data->autoExplain = 1;
10844  memcpy(data->colSeparator,SEP_Column, 2);
10845  memcpy(data->rowSeparator,SEP_Row, 2);
10846  data->showHeader = 0;
10847  data->shellFlgs = SHFLG_Lookaside;
10848  verify_uninitialized();
10849  sqlite3_config(SQLITE_CONFIG_URI, 1);
10850  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10851  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10852  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10853  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10854}
10855
10856/*
10857** Output text to the console in a font that attracts extra attention.
10858*/
10859#ifdef _WIN32
10860static void printBold(const char *zText){
10861#if !SQLITE_OS_WINRT
10862  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10863  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10864  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10865  SetConsoleTextAttribute(out,
10866         FOREGROUND_RED|FOREGROUND_INTENSITY
10867  );
10868#endif
10869  printf("%s", zText);
10870#if !SQLITE_OS_WINRT
10871  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10872#endif
10873}
10874#else
10875static void printBold(const char *zText){
10876  printf("\033[1m%s\033[0m", zText);
10877}
10878#endif
10879
10880/*
10881** Get the argument to an --option.  Throw an error and die if no argument
10882** is available.
10883*/
10884static char *cmdline_option_value(int argc, char **argv, int i){
10885  if( i==argc ){
10886    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10887            argv[0], argv[argc-1]);
10888    exit(1);
10889  }
10890  return argv[i];
10891}
10892
10893#ifndef SQLITE_SHELL_IS_UTF8
10894#  if (defined(_WIN32) || defined(WIN32)) \
10895   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
10896#    define SQLITE_SHELL_IS_UTF8          (0)
10897#  else
10898#    define SQLITE_SHELL_IS_UTF8          (1)
10899#  endif
10900#endif
10901
10902#if SQLITE_SHELL_IS_UTF8
10903int SQLITE_CDECL main(int argc, char **argv){
10904#else
10905int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10906  char **argv;
10907#endif
10908  char *zErrMsg = 0;
10909  ShellState data;
10910  const char *zInitFile = 0;
10911  int i;
10912  int rc = 0;
10913  int warnInmemoryDb = 0;
10914  int readStdin = 1;
10915  int nCmd = 0;
10916  char **azCmd = 0;
10917  const char *zVfs = 0;           /* Value of -vfs command-line option */
10918#if !SQLITE_SHELL_IS_UTF8
10919  char **argvToFree = 0;
10920  int argcToFree = 0;
10921#endif
10922
10923  setBinaryMode(stdin, 0);
10924  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10925  stdin_is_interactive = isatty(0);
10926  stdout_is_console = isatty(1);
10927
10928#ifdef SQLITE_DEBUG
10929  registerOomSimulator();
10930#endif
10931
10932#if !defined(_WIN32_WCE)
10933  if( getenv("SQLITE_DEBUG_BREAK") ){
10934    if( isatty(0) && isatty(2) ){
10935      fprintf(stderr,
10936          "attach debugger to process %d and press any key to continue.\n",
10937          GETPID());
10938      fgetc(stdin);
10939    }else{
10940#if defined(_WIN32) || defined(WIN32)
10941#if SQLITE_OS_WINRT
10942      __debugbreak();
10943#else
10944      DebugBreak();
10945#endif
10946#elif defined(SIGTRAP)
10947      raise(SIGTRAP);
10948#endif
10949    }
10950  }
10951#endif
10952
10953#if USE_SYSTEM_SQLITE+0!=1
10954  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10955    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10956            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10957    exit(1);
10958  }
10959#endif
10960  main_init(&data);
10961
10962  /* On Windows, we must translate command-line arguments into UTF-8.
10963  ** The SQLite memory allocator subsystem has to be enabled in order to
10964  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10965  ** subsequent sqlite3_config() calls will work.  So copy all results into
10966  ** memory that does not come from the SQLite memory allocator.
10967  */
10968#if !SQLITE_SHELL_IS_UTF8
10969  sqlite3_initialize();
10970  argvToFree = malloc(sizeof(argv[0])*argc*2);
10971  argcToFree = argc;
10972  argv = argvToFree + argc;
10973  if( argv==0 ) shell_out_of_memory();
10974  for(i=0; i<argc; i++){
10975    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10976    int n;
10977    if( z==0 ) shell_out_of_memory();
10978    n = (int)strlen(z);
10979    argv[i] = malloc( n+1 );
10980    if( argv[i]==0 ) shell_out_of_memory();
10981    memcpy(argv[i], z, n+1);
10982    argvToFree[i] = argv[i];
10983    sqlite3_free(z);
10984  }
10985  sqlite3_shutdown();
10986#endif
10987
10988  assert( argc>=1 && argv && argv[0] );
10989  Argv0 = argv[0];
10990
10991  /* Make sure we have a valid signal handler early, before anything
10992  ** else is done.
10993  */
10994#ifdef SIGINT
10995  signal(SIGINT, interrupt_handler);
10996#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10997  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10998#endif
10999
11000#ifdef SQLITE_SHELL_DBNAME_PROC
11001  {
11002    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
11003    ** of a C-function that will provide the name of the database file.  Use
11004    ** this compile-time option to embed this shell program in larger
11005    ** applications. */
11006    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
11007    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
11008    warnInmemoryDb = 0;
11009  }
11010#endif
11011
11012  /* Do an initial pass through the command-line argument to locate
11013  ** the name of the database file, the name of the initialization file,
11014  ** the size of the alternative malloc heap,
11015  ** and the first command to execute.
11016  */
11017  verify_uninitialized();
11018  for(i=1; i<argc; i++){
11019    char *z;
11020    z = argv[i];
11021    if( z[0]!='-' ){
11022      if( data.zDbFilename==0 ){
11023        data.zDbFilename = z;
11024      }else{
11025        /* Excesss arguments are interpreted as SQL (or dot-commands) and
11026        ** mean that nothing is read from stdin */
11027        readStdin = 0;
11028        nCmd++;
11029        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
11030        if( azCmd==0 ) shell_out_of_memory();
11031        azCmd[nCmd-1] = z;
11032      }
11033    }
11034    if( z[1]=='-' ) z++;
11035    if( strcmp(z,"-separator")==0
11036     || strcmp(z,"-nullvalue")==0
11037     || strcmp(z,"-newline")==0
11038     || strcmp(z,"-cmd")==0
11039    ){
11040      (void)cmdline_option_value(argc, argv, ++i);
11041    }else if( strcmp(z,"-init")==0 ){
11042      zInitFile = cmdline_option_value(argc, argv, ++i);
11043    }else if( strcmp(z,"-batch")==0 ){
11044      /* Need to check for batch mode here to so we can avoid printing
11045      ** informational messages (like from process_sqliterc) before
11046      ** we do the actual processing of arguments later in a second pass.
11047      */
11048      stdin_is_interactive = 0;
11049    }else if( strcmp(z,"-heap")==0 ){
11050#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11051      const char *zSize;
11052      sqlite3_int64 szHeap;
11053
11054      zSize = cmdline_option_value(argc, argv, ++i);
11055      szHeap = integerValue(zSize);
11056      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
11057      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
11058#else
11059      (void)cmdline_option_value(argc, argv, ++i);
11060#endif
11061    }else if( strcmp(z,"-pagecache")==0 ){
11062      sqlite3_int64 n, sz;
11063      sz = integerValue(cmdline_option_value(argc,argv,++i));
11064      if( sz>70000 ) sz = 70000;
11065      if( sz<0 ) sz = 0;
11066      n = integerValue(cmdline_option_value(argc,argv,++i));
11067      if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
11068        n = 0xffffffffffffLL/sz;
11069      }
11070      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
11071                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
11072      data.shellFlgs |= SHFLG_Pagecache;
11073    }else if( strcmp(z,"-lookaside")==0 ){
11074      int n, sz;
11075      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
11076      if( sz<0 ) sz = 0;
11077      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
11078      if( n<0 ) n = 0;
11079      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
11080      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
11081#ifdef SQLITE_ENABLE_VFSTRACE
11082    }else if( strcmp(z,"-vfstrace")==0 ){
11083      extern int vfstrace_register(
11084         const char *zTraceName,
11085         const char *zOldVfsName,
11086         int (*xOut)(const char*,void*),
11087         void *pOutArg,
11088         int makeDefault
11089      );
11090      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
11091#endif
11092#ifdef SQLITE_ENABLE_MULTIPLEX
11093    }else if( strcmp(z,"-multiplex")==0 ){
11094      extern int sqlite3_multiple_initialize(const char*,int);
11095      sqlite3_multiplex_initialize(0, 1);
11096#endif
11097    }else if( strcmp(z,"-mmap")==0 ){
11098      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11099      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
11100#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11101    }else if( strcmp(z,"-sorterref")==0 ){
11102      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
11103      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
11104#endif
11105    }else if( strcmp(z,"-vfs")==0 ){
11106      zVfs = cmdline_option_value(argc, argv, ++i);
11107#ifdef SQLITE_HAVE_ZLIB
11108    }else if( strcmp(z,"-zip")==0 ){
11109      data.openMode = SHELL_OPEN_ZIPFILE;
11110#endif
11111    }else if( strcmp(z,"-append")==0 ){
11112      data.openMode = SHELL_OPEN_APPENDVFS;
11113#ifndef SQLITE_OMIT_DESERIALIZE
11114    }else if( strcmp(z,"-deserialize")==0 ){
11115      data.openMode = SHELL_OPEN_DESERIALIZE;
11116    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11117      data.szMax = integerValue(argv[++i]);
11118#endif
11119    }else if( strcmp(z,"-readonly")==0 ){
11120      data.openMode = SHELL_OPEN_READONLY;
11121    }else if( strcmp(z,"-nofollow")==0 ){
11122      data.openFlags = SQLITE_OPEN_NOFOLLOW;
11123#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11124    }else if( strncmp(z, "-A",2)==0 ){
11125      /* All remaining command-line arguments are passed to the ".archive"
11126      ** command, so ignore them */
11127      break;
11128#endif
11129    }else if( strcmp(z, "-memtrace")==0 ){
11130      sqlite3MemTraceActivate(stderr);
11131    }else if( strcmp(z,"-bail")==0 ){
11132      bail_on_error = 1;
11133    }
11134  }
11135  verify_uninitialized();
11136
11137
11138#ifdef SQLITE_SHELL_INIT_PROC
11139  {
11140    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
11141    ** of a C-function that will perform initialization actions on SQLite that
11142    ** occur just before or after sqlite3_initialize(). Use this compile-time
11143    ** option to embed this shell program in larger applications. */
11144    extern void SQLITE_SHELL_INIT_PROC(void);
11145    SQLITE_SHELL_INIT_PROC();
11146  }
11147#else
11148  /* All the sqlite3_config() calls have now been made. So it is safe
11149  ** to call sqlite3_initialize() and process any command line -vfs option. */
11150  sqlite3_initialize();
11151#endif
11152
11153  if( zVfs ){
11154    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11155    if( pVfs ){
11156      sqlite3_vfs_register(pVfs, 1);
11157    }else{
11158      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11159      exit(1);
11160    }
11161  }
11162
11163  if( data.zDbFilename==0 ){
11164#ifndef SQLITE_OMIT_MEMORYDB
11165    data.zDbFilename = ":memory:";
11166    warnInmemoryDb = argc==1;
11167#else
11168    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11169    return 1;
11170#endif
11171  }
11172  data.out = stdout;
11173  sqlite3_appendvfs_init(0,0,0);
11174
11175  /* Go ahead and open the database file if it already exists.  If the
11176  ** file does not exist, delay opening it.  This prevents empty database
11177  ** files from being created if a user mistypes the database name argument
11178  ** to the sqlite command-line tool.
11179  */
11180  if( access(data.zDbFilename, 0)==0 ){
11181    open_db(&data, 0);
11182  }
11183
11184  /* Process the initialization file if there is one.  If no -init option
11185  ** is given on the command line, look for a file named ~/.sqliterc and
11186  ** try to process it.
11187  */
11188  process_sqliterc(&data,zInitFile);
11189
11190  /* Make a second pass through the command-line argument and set
11191  ** options.  This second pass is delayed until after the initialization
11192  ** file is processed so that the command-line arguments will override
11193  ** settings in the initialization file.
11194  */
11195  for(i=1; i<argc; i++){
11196    char *z = argv[i];
11197    if( z[0]!='-' ) continue;
11198    if( z[1]=='-' ){ z++; }
11199    if( strcmp(z,"-init")==0 ){
11200      i++;
11201    }else if( strcmp(z,"-html")==0 ){
11202      data.mode = MODE_Html;
11203    }else if( strcmp(z,"-list")==0 ){
11204      data.mode = MODE_List;
11205    }else if( strcmp(z,"-quote")==0 ){
11206      data.mode = MODE_Quote;
11207      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11208      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11209    }else if( strcmp(z,"-line")==0 ){
11210      data.mode = MODE_Line;
11211    }else if( strcmp(z,"-column")==0 ){
11212      data.mode = MODE_Column;
11213    }else if( strcmp(z,"-json")==0 ){
11214      data.mode = MODE_Json;
11215    }else if( strcmp(z,"-markdown")==0 ){
11216      data.mode = MODE_Markdown;
11217    }else if( strcmp(z,"-table")==0 ){
11218      data.mode = MODE_Table;
11219    }else if( strcmp(z,"-box")==0 ){
11220      data.mode = MODE_Box;
11221    }else if( strcmp(z,"-csv")==0 ){
11222      data.mode = MODE_Csv;
11223      memcpy(data.colSeparator,",",2);
11224#ifdef SQLITE_HAVE_ZLIB
11225    }else if( strcmp(z,"-zip")==0 ){
11226      data.openMode = SHELL_OPEN_ZIPFILE;
11227#endif
11228    }else if( strcmp(z,"-append")==0 ){
11229      data.openMode = SHELL_OPEN_APPENDVFS;
11230#ifndef SQLITE_OMIT_DESERIALIZE
11231    }else if( strcmp(z,"-deserialize")==0 ){
11232      data.openMode = SHELL_OPEN_DESERIALIZE;
11233    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11234      data.szMax = integerValue(argv[++i]);
11235#endif
11236    }else if( strcmp(z,"-readonly")==0 ){
11237      data.openMode = SHELL_OPEN_READONLY;
11238    }else if( strcmp(z,"-nofollow")==0 ){
11239      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11240    }else if( strcmp(z,"-ascii")==0 ){
11241      data.mode = MODE_Ascii;
11242      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
11243      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
11244    }else if( strcmp(z,"-tabs")==0 ){
11245      data.mode = MODE_List;
11246      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
11247      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11248    }else if( strcmp(z,"-separator")==0 ){
11249      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11250                       "%s",cmdline_option_value(argc,argv,++i));
11251    }else if( strcmp(z,"-newline")==0 ){
11252      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11253                       "%s",cmdline_option_value(argc,argv,++i));
11254    }else if( strcmp(z,"-nullvalue")==0 ){
11255      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11256                       "%s",cmdline_option_value(argc,argv,++i));
11257    }else if( strcmp(z,"-header")==0 ){
11258      data.showHeader = 1;
11259    }else if( strcmp(z,"-noheader")==0 ){
11260      data.showHeader = 0;
11261    }else if( strcmp(z,"-echo")==0 ){
11262      ShellSetFlag(&data, SHFLG_Echo);
11263    }else if( strcmp(z,"-eqp")==0 ){
11264      data.autoEQP = AUTOEQP_on;
11265    }else if( strcmp(z,"-eqpfull")==0 ){
11266      data.autoEQP = AUTOEQP_full;
11267    }else if( strcmp(z,"-stats")==0 ){
11268      data.statsOn = 1;
11269    }else if( strcmp(z,"-scanstats")==0 ){
11270      data.scanstatsOn = 1;
11271    }else if( strcmp(z,"-backslash")==0 ){
11272      /* Undocumented command-line option: -backslash
11273      ** Causes C-style backslash escapes to be evaluated in SQL statements
11274      ** prior to sending the SQL into SQLite.  Useful for injecting
11275      ** crazy bytes in the middle of SQL statements for testing and debugging.
11276      */
11277      ShellSetFlag(&data, SHFLG_Backslash);
11278    }else if( strcmp(z,"-bail")==0 ){
11279      /* No-op.  The bail_on_error flag should already be set. */
11280    }else if( strcmp(z,"-version")==0 ){
11281      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11282      return 0;
11283    }else if( strcmp(z,"-interactive")==0 ){
11284      stdin_is_interactive = 1;
11285    }else if( strcmp(z,"-batch")==0 ){
11286      stdin_is_interactive = 0;
11287    }else if( strcmp(z,"-heap")==0 ){
11288      i++;
11289    }else if( strcmp(z,"-pagecache")==0 ){
11290      i+=2;
11291    }else if( strcmp(z,"-lookaside")==0 ){
11292      i+=2;
11293    }else if( strcmp(z,"-mmap")==0 ){
11294      i++;
11295    }else if( strcmp(z,"-memtrace")==0 ){
11296      i++;
11297#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11298    }else if( strcmp(z,"-sorterref")==0 ){
11299      i++;
11300#endif
11301    }else if( strcmp(z,"-vfs")==0 ){
11302      i++;
11303#ifdef SQLITE_ENABLE_VFSTRACE
11304    }else if( strcmp(z,"-vfstrace")==0 ){
11305      i++;
11306#endif
11307#ifdef SQLITE_ENABLE_MULTIPLEX
11308    }else if( strcmp(z,"-multiplex")==0 ){
11309      i++;
11310#endif
11311    }else if( strcmp(z,"-help")==0 ){
11312      usage(1);
11313    }else if( strcmp(z,"-cmd")==0 ){
11314      /* Run commands that follow -cmd first and separately from commands
11315      ** that simply appear on the command-line.  This seems goofy.  It would
11316      ** be better if all commands ran in the order that they appear.  But
11317      ** we retain the goofy behavior for historical compatibility. */
11318      if( i==argc-1 ) break;
11319      z = cmdline_option_value(argc,argv,++i);
11320      if( z[0]=='.' ){
11321        rc = do_meta_command(z, &data);
11322        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11323      }else{
11324        open_db(&data, 0);
11325        rc = shell_exec(&data, z, &zErrMsg);
11326        if( zErrMsg!=0 ){
11327          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11328          if( bail_on_error ) return rc!=0 ? rc : 1;
11329        }else if( rc!=0 ){
11330          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11331          if( bail_on_error ) return rc;
11332        }
11333      }
11334#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11335    }else if( strncmp(z, "-A", 2)==0 ){
11336      if( nCmd>0 ){
11337        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11338                            " with \"%s\"\n", z);
11339        return 1;
11340      }
11341      open_db(&data, OPEN_DB_ZIPFILE);
11342      if( z[2] ){
11343        argv[i] = &z[2];
11344        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11345      }else{
11346        arDotCommand(&data, 1, argv+i, argc-i);
11347      }
11348      readStdin = 0;
11349      break;
11350#endif
11351    }else{
11352      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11353      raw_printf(stderr,"Use -help for a list of options.\n");
11354      return 1;
11355    }
11356    data.cMode = data.mode;
11357  }
11358
11359  if( !readStdin ){
11360    /* Run all arguments that do not begin with '-' as if they were separate
11361    ** command-line inputs, except for the argToSkip argument which contains
11362    ** the database filename.
11363    */
11364    for(i=0; i<nCmd; i++){
11365      if( azCmd[i][0]=='.' ){
11366        rc = do_meta_command(azCmd[i], &data);
11367        if( rc ){
11368          free(azCmd);
11369          return rc==2 ? 0 : rc;
11370        }
11371      }else{
11372        open_db(&data, 0);
11373        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11374        if( zErrMsg || rc ){
11375          if( zErrMsg!=0 ){
11376            utf8_printf(stderr,"Error: %s\n", zErrMsg);
11377          }else{
11378            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11379          }
11380          sqlite3_free(zErrMsg);
11381          free(azCmd);
11382          return rc!=0 ? rc : 1;
11383        }
11384      }
11385    }
11386  }else{
11387    /* Run commands received from standard input
11388    */
11389    if( stdin_is_interactive ){
11390      char *zHome;
11391      char *zHistory;
11392      int nHistory;
11393      printf(
11394        "SQLite version %s %.19s\n" /*extra-version-info*/
11395        "Enter \".help\" for usage hints.\n",
11396        sqlite3_libversion(), sqlite3_sourceid()
11397      );
11398      if( warnInmemoryDb ){
11399        printf("Connected to a ");
11400        printBold("transient in-memory database");
11401        printf(".\nUse \".open FILENAME\" to reopen on a "
11402               "persistent database.\n");
11403      }
11404      zHistory = getenv("SQLITE_HISTORY");
11405      if( zHistory ){
11406        zHistory = strdup(zHistory);
11407      }else if( (zHome = find_home_dir(0))!=0 ){
11408        nHistory = strlen30(zHome) + 20;
11409        if( (zHistory = malloc(nHistory))!=0 ){
11410          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11411        }
11412      }
11413      if( zHistory ){ shell_read_history(zHistory); }
11414#if HAVE_READLINE || HAVE_EDITLINE
11415      rl_attempted_completion_function = readline_completion;
11416#elif HAVE_LINENOISE
11417      linenoiseSetCompletionCallback(linenoise_completion);
11418#endif
11419      data.in = 0;
11420      rc = process_input(&data);
11421      if( zHistory ){
11422        shell_stifle_history(2000);
11423        shell_write_history(zHistory);
11424        free(zHistory);
11425      }
11426    }else{
11427      data.in = stdin;
11428      rc = process_input(&data);
11429    }
11430  }
11431  free(azCmd);
11432  set_table_name(&data, 0);
11433  if( data.db ){
11434    session_close_all(&data);
11435    close_db(data.db);
11436  }
11437  sqlite3_free(data.zFreeOnClose);
11438  find_home_dir(1);
11439  output_reset(&data);
11440  data.doXdgOpen = 0;
11441  clearTempFile(&data);
11442#if !SQLITE_SHELL_IS_UTF8
11443  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11444  free(argvToFree);
11445#endif
11446  free(data.colWidth);
11447  /* Clear the global data structure so that valgrind will detect memory
11448  ** leaks */
11449  memset(&data, 0, sizeof(data));
11450  return rc;
11451}
11452