xref: /sqlite-3.40.0/src/shell.c.in (revision 7b852416)
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code.  In place of
5** a legal notice, here is a blessing:
6**
7**    May you do good and not evil.
8**    May you find forgiveness for yourself and forgive others.
9**    May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14*/
15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16/* This needs to come before any includes for MSVC compiler */
17#define _CRT_SECURE_NO_WARNINGS
18#endif
19
20/*
21** Determine if we are dealing with WinRT, which provides only a subset of
22** the full Win32 API.
23*/
24#if !defined(SQLITE_OS_WINRT)
25# define SQLITE_OS_WINRT 0
26#endif
27
28/*
29** Warning pragmas copied from msvc.h in the core.
30*/
31#if defined(_MSC_VER)
32#pragma warning(disable : 4054)
33#pragma warning(disable : 4055)
34#pragma warning(disable : 4100)
35#pragma warning(disable : 4127)
36#pragma warning(disable : 4130)
37#pragma warning(disable : 4152)
38#pragma warning(disable : 4189)
39#pragma warning(disable : 4206)
40#pragma warning(disable : 4210)
41#pragma warning(disable : 4232)
42#pragma warning(disable : 4244)
43#pragma warning(disable : 4305)
44#pragma warning(disable : 4306)
45#pragma warning(disable : 4702)
46#pragma warning(disable : 4706)
47#endif /* defined(_MSC_VER) */
48
49/*
50** No support for loadable extensions in VxWorks.
51*/
52#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
53# define SQLITE_OMIT_LOAD_EXTENSION 1
54#endif
55
56/*
57** Enable large-file support for fopen() and friends on unix.
58*/
59#ifndef SQLITE_DISABLE_LFS
60# define _LARGE_FILE       1
61# ifndef _FILE_OFFSET_BITS
62#   define _FILE_OFFSET_BITS 64
63# endif
64# define _LARGEFILE_SOURCE 1
65#endif
66
67#include <stdlib.h>
68#include <string.h>
69#include <stdio.h>
70#include <assert.h>
71#include "sqlite3.h"
72typedef sqlite3_int64 i64;
73typedef sqlite3_uint64 u64;
74typedef unsigned char u8;
75#if SQLITE_USER_AUTHENTICATION
76# include "sqlite3userauth.h"
77#endif
78#include <ctype.h>
79#include <stdarg.h>
80
81#if !defined(_WIN32) && !defined(WIN32)
82# include <signal.h>
83# if !defined(__RTP__) && !defined(_WRS_KERNEL)
84#  include <pwd.h>
85# endif
86#endif
87#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
88# include <unistd.h>
89# include <dirent.h>
90# define GETPID getpid
91# if defined(__MINGW32__)
92#  define DIRENT dirent
93#  ifndef S_ISLNK
94#   define S_ISLNK(mode) (0)
95#  endif
96# endif
97#else
98# define GETPID (int)GetCurrentProcessId
99#endif
100#include <sys/types.h>
101#include <sys/stat.h>
102
103#if HAVE_READLINE
104# include <readline/readline.h>
105# include <readline/history.h>
106#endif
107
108#if HAVE_EDITLINE
109# include <editline/readline.h>
110#endif
111
112#if HAVE_EDITLINE || HAVE_READLINE
113
114# define shell_add_history(X) add_history(X)
115# define shell_read_history(X) read_history(X)
116# define shell_write_history(X) write_history(X)
117# define shell_stifle_history(X) stifle_history(X)
118# define shell_readline(X) readline(X)
119
120#elif HAVE_LINENOISE
121
122# include "linenoise.h"
123# define shell_add_history(X) linenoiseHistoryAdd(X)
124# define shell_read_history(X) linenoiseHistoryLoad(X)
125# define shell_write_history(X) linenoiseHistorySave(X)
126# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
127# define shell_readline(X) linenoise(X)
128
129#else
130
131# define shell_read_history(X)
132# define shell_write_history(X)
133# define shell_stifle_history(X)
134
135# define SHELL_USE_LOCAL_GETLINE 1
136#endif
137
138
139#if defined(_WIN32) || defined(WIN32)
140# if SQLITE_OS_WINRT
141#  define SQLITE_OMIT_POPEN 1
142# else
143#  include <io.h>
144#  include <fcntl.h>
145#  define isatty(h) _isatty(h)
146#  ifndef access
147#   define access(f,m) _access((f),(m))
148#  endif
149#  ifndef unlink
150#   define unlink _unlink
151#  endif
152#  ifndef strdup
153#   define strdup _strdup
154#  endif
155#  undef popen
156#  define popen _popen
157#  undef pclose
158#  define pclose _pclose
159# endif
160#else
161 /* Make sure isatty() has a prototype. */
162 extern int isatty(int);
163
164# if !defined(__RTP__) && !defined(_WRS_KERNEL)
165  /* popen and pclose are not C89 functions and so are
166  ** sometimes omitted from the <stdio.h> header */
167   extern FILE *popen(const char*,const char*);
168   extern int pclose(FILE*);
169# else
170#  define SQLITE_OMIT_POPEN 1
171# endif
172#endif
173
174#if defined(_WIN32_WCE)
175/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
176 * thus we always assume that we have a console. That can be
177 * overridden with the -batch command line option.
178 */
179#define isatty(x) 1
180#endif
181
182/* ctype macros that work with signed characters */
183#define IsSpace(X)  isspace((unsigned char)X)
184#define IsDigit(X)  isdigit((unsigned char)X)
185#define ToLower(X)  (char)tolower((unsigned char)X)
186
187#if defined(_WIN32) || defined(WIN32)
188#if SQLITE_OS_WINRT
189#include <intrin.h>
190#endif
191#include <windows.h>
192
193/* string conversion routines only needed on Win32 */
194extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
195extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
196extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
197extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
198#endif
199
200/* On Windows, we normally run with output mode of TEXT so that \n characters
201** are automatically translated into \r\n.  However, this behavior needs
202** to be disabled in some cases (ex: when generating CSV output and when
203** rendering quoted strings that contain \n characters).  The following
204** routines take care of that.
205*/
206#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
207static void setBinaryMode(FILE *file, int isOutput){
208  if( isOutput ) fflush(file);
209  _setmode(_fileno(file), _O_BINARY);
210}
211static void setTextMode(FILE *file, int isOutput){
212  if( isOutput ) fflush(file);
213  _setmode(_fileno(file), _O_TEXT);
214}
215#else
216# define setBinaryMode(X,Y)
217# define setTextMode(X,Y)
218#endif
219
220
221/* True if the timer is enabled */
222static int enableTimer = 0;
223
224/* Return the current wall-clock time */
225static sqlite3_int64 timeOfDay(void){
226  static sqlite3_vfs *clockVfs = 0;
227  sqlite3_int64 t;
228  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
229  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
230    clockVfs->xCurrentTimeInt64(clockVfs, &t);
231  }else{
232    double r;
233    clockVfs->xCurrentTime(clockVfs, &r);
234    t = (sqlite3_int64)(r*86400000.0);
235  }
236  return t;
237}
238
239#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
240#include <sys/time.h>
241#include <sys/resource.h>
242
243/* VxWorks does not support getrusage() as far as we can determine */
244#if defined(_WRS_KERNEL) || defined(__RTP__)
245struct rusage {
246  struct timeval ru_utime; /* user CPU time used */
247  struct timeval ru_stime; /* system CPU time used */
248};
249#define getrusage(A,B) memset(B,0,sizeof(*B))
250#endif
251
252/* Saved resource information for the beginning of an operation */
253static struct rusage sBegin;  /* CPU time at start */
254static sqlite3_int64 iBegin;  /* Wall-clock time at start */
255
256/*
257** Begin timing an operation
258*/
259static void beginTimer(void){
260  if( enableTimer ){
261    getrusage(RUSAGE_SELF, &sBegin);
262    iBegin = timeOfDay();
263  }
264}
265
266/* Return the difference of two time_structs in seconds */
267static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
268  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
269         (double)(pEnd->tv_sec - pStart->tv_sec);
270}
271
272/*
273** Print the timing results.
274*/
275static void endTimer(void){
276  if( enableTimer ){
277    sqlite3_int64 iEnd = timeOfDay();
278    struct rusage sEnd;
279    getrusage(RUSAGE_SELF, &sEnd);
280    printf("Run Time: real %.3f user %f sys %f\n",
281       (iEnd - iBegin)*0.001,
282       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
283       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
284  }
285}
286
287#define BEGIN_TIMER beginTimer()
288#define END_TIMER endTimer()
289#define HAS_TIMER 1
290
291#elif (defined(_WIN32) || defined(WIN32))
292
293/* Saved resource information for the beginning of an operation */
294static HANDLE hProcess;
295static FILETIME ftKernelBegin;
296static FILETIME ftUserBegin;
297static sqlite3_int64 ftWallBegin;
298typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
299                                    LPFILETIME, LPFILETIME);
300static GETPROCTIMES getProcessTimesAddr = NULL;
301
302/*
303** Check to see if we have timer support.  Return 1 if necessary
304** support found (or found previously).
305*/
306static int hasTimer(void){
307  if( getProcessTimesAddr ){
308    return 1;
309  } else {
310#if !SQLITE_OS_WINRT
311    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
312    ** versions. See if the version we are running on has it, and if it
313    ** does, save off a pointer to it and the current process handle.
314    */
315    hProcess = GetCurrentProcess();
316    if( hProcess ){
317      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
318      if( NULL != hinstLib ){
319        getProcessTimesAddr =
320            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
321        if( NULL != getProcessTimesAddr ){
322          return 1;
323        }
324        FreeLibrary(hinstLib);
325      }
326    }
327#endif
328  }
329  return 0;
330}
331
332/*
333** Begin timing an operation
334*/
335static void beginTimer(void){
336  if( enableTimer && getProcessTimesAddr ){
337    FILETIME ftCreation, ftExit;
338    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
339                        &ftKernelBegin,&ftUserBegin);
340    ftWallBegin = timeOfDay();
341  }
342}
343
344/* Return the difference of two FILETIME structs in seconds */
345static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
346  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
347  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
348  return (double) ((i64End - i64Start) / 10000000.0);
349}
350
351/*
352** Print the timing results.
353*/
354static void endTimer(void){
355  if( enableTimer && getProcessTimesAddr){
356    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
357    sqlite3_int64 ftWallEnd = timeOfDay();
358    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
359    printf("Run Time: real %.3f user %f sys %f\n",
360       (ftWallEnd - ftWallBegin)*0.001,
361       timeDiff(&ftUserBegin, &ftUserEnd),
362       timeDiff(&ftKernelBegin, &ftKernelEnd));
363  }
364}
365
366#define BEGIN_TIMER beginTimer()
367#define END_TIMER endTimer()
368#define HAS_TIMER hasTimer()
369
370#else
371#define BEGIN_TIMER
372#define END_TIMER
373#define HAS_TIMER 0
374#endif
375
376/*
377** Used to prevent warnings about unused parameters
378*/
379#define UNUSED_PARAMETER(x) (void)(x)
380
381/*
382** Number of elements in an array
383*/
384#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
385
386/*
387** If the following flag is set, then command execution stops
388** at an error if we are not interactive.
389*/
390static int bail_on_error = 0;
391
392/*
393** Threat stdin as an interactive input if the following variable
394** is true.  Otherwise, assume stdin is connected to a file or pipe.
395*/
396static int stdin_is_interactive = 1;
397
398/*
399** On Windows systems we have to know if standard output is a console
400** in order to translate UTF-8 into MBCS.  The following variable is
401** true if translation is required.
402*/
403static int stdout_is_console = 1;
404
405/*
406** The following is the open SQLite database.  We make a pointer
407** to this database a static variable so that it can be accessed
408** by the SIGINT handler to interrupt database processing.
409*/
410static sqlite3 *globalDb = 0;
411
412/*
413** True if an interrupt (Control-C) has been received.
414*/
415static volatile int seenInterrupt = 0;
416
417#ifdef SQLITE_DEBUG
418/*
419** Out-of-memory simulator variables
420*/
421static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
422static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
423static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
424#endif /* SQLITE_DEBUG */
425
426/*
427** This is the name of our program. It is set in main(), used
428** in a number of other places, mostly for error messages.
429*/
430static char *Argv0;
431
432/*
433** Prompt strings. Initialized in main. Settable with
434**   .prompt main continue
435*/
436static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
437static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
438
439/*
440** Render output like fprintf().  Except, if the output is going to the
441** console and if this is running on a Windows machine, translate the
442** output from UTF-8 into MBCS.
443*/
444#if defined(_WIN32) || defined(WIN32)
445void utf8_printf(FILE *out, const char *zFormat, ...){
446  va_list ap;
447  va_start(ap, zFormat);
448  if( stdout_is_console && (out==stdout || out==stderr) ){
449    char *z1 = sqlite3_vmprintf(zFormat, ap);
450    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
451    sqlite3_free(z1);
452    fputs(z2, out);
453    sqlite3_free(z2);
454  }else{
455    vfprintf(out, zFormat, ap);
456  }
457  va_end(ap);
458}
459#elif !defined(utf8_printf)
460# define utf8_printf fprintf
461#endif
462
463/*
464** Render output like fprintf().  This should not be used on anything that
465** includes string formatting (e.g. "%s").
466*/
467#if !defined(raw_printf)
468# define raw_printf fprintf
469#endif
470
471/* Indicate out-of-memory and exit. */
472static void shell_out_of_memory(void){
473  raw_printf(stderr,"Error: out of memory\n");
474  exit(1);
475}
476
477#ifdef SQLITE_DEBUG
478/* This routine is called when a simulated OOM occurs.  It is broken
479** out as a separate routine to make it easy to set a breakpoint on
480** the OOM
481*/
482void shellOomFault(void){
483  if( oomRepeat>0 ){
484    oomRepeat--;
485  }else{
486    oomCounter--;
487  }
488}
489#endif /* SQLITE_DEBUG */
490
491#ifdef SQLITE_DEBUG
492/* This routine is a replacement malloc() that is used to simulate
493** Out-Of-Memory (OOM) errors for testing purposes.
494*/
495static void *oomMalloc(int nByte){
496  if( oomCounter ){
497    if( oomCounter==1 ){
498      shellOomFault();
499      return 0;
500    }else{
501      oomCounter--;
502    }
503  }
504  return defaultMalloc(nByte);
505}
506#endif /* SQLITE_DEBUG */
507
508#ifdef SQLITE_DEBUG
509/* Register the OOM simulator.  This must occur before any memory
510** allocations */
511static void registerOomSimulator(void){
512  sqlite3_mem_methods mem;
513  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
514  defaultMalloc = mem.xMalloc;
515  mem.xMalloc = oomMalloc;
516  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
517}
518#endif
519
520/*
521** Write I/O traces to the following stream.
522*/
523#ifdef SQLITE_ENABLE_IOTRACE
524static FILE *iotrace = 0;
525#endif
526
527/*
528** This routine works like printf in that its first argument is a
529** format string and subsequent arguments are values to be substituted
530** in place of % fields.  The result of formatting this string
531** is written to iotrace.
532*/
533#ifdef SQLITE_ENABLE_IOTRACE
534static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
535  va_list ap;
536  char *z;
537  if( iotrace==0 ) return;
538  va_start(ap, zFormat);
539  z = sqlite3_vmprintf(zFormat, ap);
540  va_end(ap);
541  utf8_printf(iotrace, "%s", z);
542  sqlite3_free(z);
543}
544#endif
545
546/*
547** Output string zUtf to stream pOut as w characters.  If w is negative,
548** then right-justify the text.  W is the width in UTF-8 characters, not
549** in bytes.  This is different from the %*.*s specification in printf
550** since with %*.*s the width is measured in bytes, not characters.
551*/
552static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
553  int i;
554  int n;
555  int aw = w<0 ? -w : w;
556  char zBuf[1000];
557  if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
558  for(i=n=0; zUtf[i]; i++){
559    if( (zUtf[i]&0xc0)!=0x80 ){
560      n++;
561      if( n==aw ){
562        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
563        break;
564      }
565    }
566  }
567  if( n>=aw ){
568    utf8_printf(pOut, "%.*s", i, zUtf);
569  }else if( w<0 ){
570    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
571  }else{
572    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
573  }
574}
575
576
577/*
578** Determines if a string is a number of not.
579*/
580static int isNumber(const char *z, int *realnum){
581  if( *z=='-' || *z=='+' ) z++;
582  if( !IsDigit(*z) ){
583    return 0;
584  }
585  z++;
586  if( realnum ) *realnum = 0;
587  while( IsDigit(*z) ){ z++; }
588  if( *z=='.' ){
589    z++;
590    if( !IsDigit(*z) ) return 0;
591    while( IsDigit(*z) ){ z++; }
592    if( realnum ) *realnum = 1;
593  }
594  if( *z=='e' || *z=='E' ){
595    z++;
596    if( *z=='+' || *z=='-' ) z++;
597    if( !IsDigit(*z) ) return 0;
598    while( IsDigit(*z) ){ z++; }
599    if( realnum ) *realnum = 1;
600  }
601  return *z==0;
602}
603
604/*
605** Compute a string length that is limited to what can be stored in
606** lower 30 bits of a 32-bit signed integer.
607*/
608static int strlen30(const char *z){
609  const char *z2 = z;
610  while( *z2 ){ z2++; }
611  return 0x3fffffff & (int)(z2 - z);
612}
613
614/*
615** Return the length of a string in characters.  Multibyte UTF8 characters
616** count as a single character.
617*/
618static int strlenChar(const char *z){
619  int n = 0;
620  while( *z ){
621    if( (0xc0&*(z++))!=0x80 ) n++;
622  }
623  return n;
624}
625
626/*
627** Return true if zFile does not exist or if it is not an ordinary file.
628*/
629#ifdef _WIN32
630# define notNormalFile(X) 0
631#else
632static int notNormalFile(const char *zFile){
633  struct stat x;
634  int rc;
635  memset(&x, 0, sizeof(x));
636  rc = stat(zFile, &x);
637  return rc || !S_ISREG(x.st_mode);
638}
639#endif
640
641/*
642** This routine reads a line of text from FILE in, stores
643** the text in memory obtained from malloc() and returns a pointer
644** to the text.  NULL is returned at end of file, or if malloc()
645** fails.
646**
647** If zLine is not NULL then it is a malloced buffer returned from
648** a previous call to this routine that may be reused.
649*/
650static char *local_getline(char *zLine, FILE *in){
651  int nLine = zLine==0 ? 0 : 100;
652  int n = 0;
653
654  while( 1 ){
655    if( n+100>nLine ){
656      nLine = nLine*2 + 100;
657      zLine = realloc(zLine, nLine);
658      if( zLine==0 ) shell_out_of_memory();
659    }
660    if( fgets(&zLine[n], nLine - n, in)==0 ){
661      if( n==0 ){
662        free(zLine);
663        return 0;
664      }
665      zLine[n] = 0;
666      break;
667    }
668    while( zLine[n] ) n++;
669    if( n>0 && zLine[n-1]=='\n' ){
670      n--;
671      if( n>0 && zLine[n-1]=='\r' ) n--;
672      zLine[n] = 0;
673      break;
674    }
675  }
676#if defined(_WIN32) || defined(WIN32)
677  /* For interactive input on Windows systems, translate the
678  ** multi-byte characterset characters into UTF-8. */
679  if( stdin_is_interactive && in==stdin ){
680    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
681    if( zTrans ){
682      int nTrans = strlen30(zTrans)+1;
683      if( nTrans>nLine ){
684        zLine = realloc(zLine, nTrans);
685        if( zLine==0 ) shell_out_of_memory();
686      }
687      memcpy(zLine, zTrans, nTrans);
688      sqlite3_free(zTrans);
689    }
690  }
691#endif /* defined(_WIN32) || defined(WIN32) */
692  return zLine;
693}
694
695/*
696** Retrieve a single line of input text.
697**
698** If in==0 then read from standard input and prompt before each line.
699** If isContinuation is true, then a continuation prompt is appropriate.
700** If isContinuation is zero, then the main prompt should be used.
701**
702** If zPrior is not NULL then it is a buffer from a prior call to this
703** routine that can be reused.
704**
705** The result is stored in space obtained from malloc() and must either
706** be freed by the caller or else passed back into this routine via the
707** zPrior argument for reuse.
708*/
709static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
710  char *zPrompt;
711  char *zResult;
712  if( in!=0 ){
713    zResult = local_getline(zPrior, in);
714  }else{
715    zPrompt = isContinuation ? continuePrompt : mainPrompt;
716#if SHELL_USE_LOCAL_GETLINE
717    printf("%s", zPrompt);
718    fflush(stdout);
719    zResult = local_getline(zPrior, stdin);
720#else
721    free(zPrior);
722    zResult = shell_readline(zPrompt);
723    if( zResult && *zResult ) shell_add_history(zResult);
724#endif
725  }
726  return zResult;
727}
728
729
730/*
731** Return the value of a hexadecimal digit.  Return -1 if the input
732** is not a hex digit.
733*/
734static int hexDigitValue(char c){
735  if( c>='0' && c<='9' ) return c - '0';
736  if( c>='a' && c<='f' ) return c - 'a' + 10;
737  if( c>='A' && c<='F' ) return c - 'A' + 10;
738  return -1;
739}
740
741/*
742** Interpret zArg as an integer value, possibly with suffixes.
743*/
744static sqlite3_int64 integerValue(const char *zArg){
745  sqlite3_int64 v = 0;
746  static const struct { char *zSuffix; int iMult; } aMult[] = {
747    { "KiB", 1024 },
748    { "MiB", 1024*1024 },
749    { "GiB", 1024*1024*1024 },
750    { "KB",  1000 },
751    { "MB",  1000000 },
752    { "GB",  1000000000 },
753    { "K",   1000 },
754    { "M",   1000000 },
755    { "G",   1000000000 },
756  };
757  int i;
758  int isNeg = 0;
759  if( zArg[0]=='-' ){
760    isNeg = 1;
761    zArg++;
762  }else if( zArg[0]=='+' ){
763    zArg++;
764  }
765  if( zArg[0]=='0' && zArg[1]=='x' ){
766    int x;
767    zArg += 2;
768    while( (x = hexDigitValue(zArg[0]))>=0 ){
769      v = (v<<4) + x;
770      zArg++;
771    }
772  }else{
773    while( IsDigit(zArg[0]) ){
774      v = v*10 + zArg[0] - '0';
775      zArg++;
776    }
777  }
778  for(i=0; i<ArraySize(aMult); i++){
779    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
780      v *= aMult[i].iMult;
781      break;
782    }
783  }
784  return isNeg? -v : v;
785}
786
787/*
788** A variable length string to which one can append text.
789*/
790typedef struct ShellText ShellText;
791struct ShellText {
792  char *z;
793  int n;
794  int nAlloc;
795};
796
797/*
798** Initialize and destroy a ShellText object
799*/
800static void initText(ShellText *p){
801  memset(p, 0, sizeof(*p));
802}
803static void freeText(ShellText *p){
804  free(p->z);
805  initText(p);
806}
807
808/* zIn is either a pointer to a NULL-terminated string in memory obtained
809** from malloc(), or a NULL pointer. The string pointed to by zAppend is
810** added to zIn, and the result returned in memory obtained from malloc().
811** zIn, if it was not NULL, is freed.
812**
813** If the third argument, quote, is not '\0', then it is used as a
814** quote character for zAppend.
815*/
816static void appendText(ShellText *p, char const *zAppend, char quote){
817  int len;
818  int i;
819  int nAppend = strlen30(zAppend);
820
821  len = nAppend+p->n+1;
822  if( quote ){
823    len += 2;
824    for(i=0; i<nAppend; i++){
825      if( zAppend[i]==quote ) len++;
826    }
827  }
828
829  if( p->n+len>=p->nAlloc ){
830    p->nAlloc = p->nAlloc*2 + len + 20;
831    p->z = realloc(p->z, p->nAlloc);
832    if( p->z==0 ) shell_out_of_memory();
833  }
834
835  if( quote ){
836    char *zCsr = p->z+p->n;
837    *zCsr++ = quote;
838    for(i=0; i<nAppend; i++){
839      *zCsr++ = zAppend[i];
840      if( zAppend[i]==quote ) *zCsr++ = quote;
841    }
842    *zCsr++ = quote;
843    p->n = (int)(zCsr - p->z);
844    *zCsr = '\0';
845  }else{
846    memcpy(p->z+p->n, zAppend, nAppend);
847    p->n += nAppend;
848    p->z[p->n] = '\0';
849  }
850}
851
852/*
853** Attempt to determine if identifier zName needs to be quoted, either
854** because it contains non-alphanumeric characters, or because it is an
855** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
856** that quoting is required.
857**
858** Return '"' if quoting is required.  Return 0 if no quoting is required.
859*/
860static char quoteChar(const char *zName){
861  int i;
862  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
863  for(i=0; zName[i]; i++){
864    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
865  }
866  return sqlite3_keyword_check(zName, i) ? '"' : 0;
867}
868
869/*
870** Construct a fake object name and column list to describe the structure
871** of the view, virtual table, or table valued function zSchema.zName.
872*/
873static char *shellFakeSchema(
874  sqlite3 *db,            /* The database connection containing the vtab */
875  const char *zSchema,    /* Schema of the database holding the vtab */
876  const char *zName       /* The name of the virtual table */
877){
878  sqlite3_stmt *pStmt = 0;
879  char *zSql;
880  ShellText s;
881  char cQuote;
882  char *zDiv = "(";
883  int nRow = 0;
884
885  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
886                         zSchema ? zSchema : "main", zName);
887  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
888  sqlite3_free(zSql);
889  initText(&s);
890  if( zSchema ){
891    cQuote = quoteChar(zSchema);
892    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
893    appendText(&s, zSchema, cQuote);
894    appendText(&s, ".", 0);
895  }
896  cQuote = quoteChar(zName);
897  appendText(&s, zName, cQuote);
898  while( sqlite3_step(pStmt)==SQLITE_ROW ){
899    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
900    nRow++;
901    appendText(&s, zDiv, 0);
902    zDiv = ",";
903    cQuote = quoteChar(zCol);
904    appendText(&s, zCol, cQuote);
905  }
906  appendText(&s, ")", 0);
907  sqlite3_finalize(pStmt);
908  if( nRow==0 ){
909    freeText(&s);
910    s.z = 0;
911  }
912  return s.z;
913}
914
915/*
916** SQL function:  shell_module_schema(X)
917**
918** Return a fake schema for the table-valued function or eponymous virtual
919** table X.
920*/
921static void shellModuleSchema(
922  sqlite3_context *pCtx,
923  int nVal,
924  sqlite3_value **apVal
925){
926  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
927  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
928  UNUSED_PARAMETER(nVal);
929  if( zFake ){
930    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
931                        -1, sqlite3_free);
932    free(zFake);
933  }
934}
935
936/*
937** SQL function:  shell_add_schema(S,X)
938**
939** Add the schema name X to the CREATE statement in S and return the result.
940** Examples:
941**
942**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
943**
944** Also works on
945**
946**    CREATE INDEX
947**    CREATE UNIQUE INDEX
948**    CREATE VIEW
949**    CREATE TRIGGER
950**    CREATE VIRTUAL TABLE
951**
952** This UDF is used by the .schema command to insert the schema name of
953** attached databases into the middle of the sqlite_schema.sql field.
954*/
955static void shellAddSchemaName(
956  sqlite3_context *pCtx,
957  int nVal,
958  sqlite3_value **apVal
959){
960  static const char *aPrefix[] = {
961     "TABLE",
962     "INDEX",
963     "UNIQUE INDEX",
964     "VIEW",
965     "TRIGGER",
966     "VIRTUAL TABLE"
967  };
968  int i = 0;
969  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
970  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
971  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
972  sqlite3 *db = sqlite3_context_db_handle(pCtx);
973  UNUSED_PARAMETER(nVal);
974  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
975    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
976      int n = strlen30(aPrefix[i]);
977      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
978        char *z = 0;
979        char *zFake = 0;
980        if( zSchema ){
981          char cQuote = quoteChar(zSchema);
982          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
983            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
984          }else{
985            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
986          }
987        }
988        if( zName
989         && aPrefix[i][0]=='V'
990         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
991        ){
992          if( z==0 ){
993            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
994          }else{
995            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
996          }
997          free(zFake);
998        }
999        if( z ){
1000          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1001          return;
1002        }
1003      }
1004    }
1005  }
1006  sqlite3_result_value(pCtx, apVal[0]);
1007}
1008
1009/*
1010** The source code for several run-time loadable extensions is inserted
1011** below by the ../tool/mkshellc.tcl script.  Before processing that included
1012** code, we need to override some macros to make the included program code
1013** work here in the middle of this regular program.
1014*/
1015#define SQLITE_EXTENSION_INIT1
1016#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1017
1018#if defined(_WIN32) && defined(_MSC_VER)
1019INCLUDE test_windirent.h
1020INCLUDE test_windirent.c
1021#define dirent DIRENT
1022#endif
1023INCLUDE ../ext/misc/shathree.c
1024INCLUDE ../ext/misc/fileio.c
1025INCLUDE ../ext/misc/completion.c
1026INCLUDE ../ext/misc/appendvfs.c
1027INCLUDE ../ext/misc/memtrace.c
1028INCLUDE ../ext/misc/uint.c
1029INCLUDE ../ext/misc/decimal.c
1030INCLUDE ../ext/misc/ieee754.c
1031#ifdef SQLITE_HAVE_ZLIB
1032INCLUDE ../ext/misc/zipfile.c
1033INCLUDE ../ext/misc/sqlar.c
1034#endif
1035INCLUDE ../ext/expert/sqlite3expert.h
1036INCLUDE ../ext/expert/sqlite3expert.c
1037
1038#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1039INCLUDE ../ext/misc/dbdata.c
1040#endif
1041
1042#if defined(SQLITE_ENABLE_SESSION)
1043/*
1044** State information for a single open session
1045*/
1046typedef struct OpenSession OpenSession;
1047struct OpenSession {
1048  char *zName;             /* Symbolic name for this session */
1049  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1050  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1051  sqlite3_session *p;      /* The open session */
1052};
1053#endif
1054
1055typedef struct ExpertInfo ExpertInfo;
1056struct ExpertInfo {
1057  sqlite3expert *pExpert;
1058  int bVerbose;
1059};
1060
1061/* A single line in the EQP output */
1062typedef struct EQPGraphRow EQPGraphRow;
1063struct EQPGraphRow {
1064  int iEqpId;           /* ID for this row */
1065  int iParentId;        /* ID of the parent row */
1066  EQPGraphRow *pNext;   /* Next row in sequence */
1067  char zText[1];        /* Text to display for this row */
1068};
1069
1070/* All EQP output is collected into an instance of the following */
1071typedef struct EQPGraph EQPGraph;
1072struct EQPGraph {
1073  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1074  EQPGraphRow *pLast;   /* Last element of the pRow list */
1075  char zPrefix[100];    /* Graph prefix */
1076};
1077
1078/*
1079** State information about the database connection is contained in an
1080** instance of the following structure.
1081*/
1082typedef struct ShellState ShellState;
1083struct ShellState {
1084  sqlite3 *db;           /* The database */
1085  u8 autoExplain;        /* Automatically turn on .explain mode */
1086  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1087  u8 autoEQPtest;        /* autoEQP is in test mode */
1088  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1089  u8 statsOn;            /* True to display memory stats before each finalize */
1090  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1091  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1092  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1093  u8 nEqpLevel;          /* Depth of the EQP output graph */
1094  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1095  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1096  int outCount;          /* Revert to stdout when reaching zero */
1097  int cnt;               /* Number of records displayed so far */
1098  int lineno;            /* Line number of last line read from in */
1099  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1100  FILE *in;              /* Read commands from this stream */
1101  FILE *out;             /* Write results here */
1102  FILE *traceOut;        /* Output for sqlite3_trace() */
1103  int nErr;              /* Number of errors seen */
1104  int mode;              /* An output mode setting */
1105  int modePrior;         /* Saved mode */
1106  int cMode;             /* temporary output mode for the current query */
1107  int normalMode;        /* Output mode before ".explain on" */
1108  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1109  int showHeader;        /* True to show column names in List or Column mode */
1110  int nCheck;            /* Number of ".check" commands run */
1111  unsigned nProgress;    /* Number of progress callbacks encountered */
1112  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1113  unsigned flgProgress;  /* Flags for the progress callback */
1114  unsigned shellFlgs;    /* Various flags */
1115  unsigned priorShFlgs;  /* Saved copy of flags */
1116  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1117  char *zDestTable;      /* Name of destination table when MODE_Insert */
1118  char *zTempFile;       /* Temporary file that might need deleting */
1119  char zTestcase[30];    /* Name of current test case */
1120  char colSeparator[20]; /* Column separator character for several modes */
1121  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1122  char colSepPrior[20];  /* Saved column separator */
1123  char rowSepPrior[20];  /* Saved row separator */
1124  int *colWidth;         /* Requested width of each column in columnar modes */
1125  int *actualWidth;      /* Actual width of each column */
1126  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1127  char nullValue[20];    /* The text to print when a NULL comes back from
1128                         ** the database */
1129  char outfile[FILENAME_MAX]; /* Filename for *out */
1130  const char *zDbFilename;    /* name of the database file */
1131  char *zFreeOnClose;         /* Filename to free when closing */
1132  const char *zVfs;           /* Name of VFS to use */
1133  sqlite3_stmt *pStmt;   /* Current statement if any. */
1134  FILE *pLog;            /* Write log output here */
1135  int *aiIndent;         /* Array of indents used in MODE_Explain */
1136  int nIndent;           /* Size of array aiIndent[] */
1137  int iIndent;           /* Index of current op in aiIndent[] */
1138  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1139#if defined(SQLITE_ENABLE_SESSION)
1140  int nSession;             /* Number of active sessions */
1141  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1142#endif
1143  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1144};
1145
1146
1147/* Allowed values for ShellState.autoEQP
1148*/
1149#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1150#define AUTOEQP_on       1           /* Automatic EQP is on */
1151#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1152#define AUTOEQP_full     3           /* Show full EXPLAIN */
1153
1154/* Allowed values for ShellState.openMode
1155*/
1156#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1157#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1158#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1159#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1160#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1161#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1162#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1163
1164/* Allowed values for ShellState.eTraceType
1165*/
1166#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1167#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1168#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1169
1170/* Bits in the ShellState.flgProgress variable */
1171#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1172#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1173                                   ** callback limit is reached, and for each
1174                                   ** top-level SQL statement */
1175#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1176
1177/*
1178** These are the allowed shellFlgs values
1179*/
1180#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1181#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1182#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1183#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1184#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1185#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1186#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1187#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
1188
1189/*
1190** Macros for testing and setting shellFlgs
1191*/
1192#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1193#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1194#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1195
1196/*
1197** These are the allowed modes.
1198*/
1199#define MODE_Line     0  /* One column per line.  Blank line between records */
1200#define MODE_Column   1  /* One record per line in neat columns */
1201#define MODE_List     2  /* One record per line with a separator */
1202#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1203#define MODE_Html     4  /* Generate an XHTML table */
1204#define MODE_Insert   5  /* Generate SQL "insert" statements */
1205#define MODE_Quote    6  /* Quote values as for SQL */
1206#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1207#define MODE_Csv      8  /* Quote strings, numbers are plain */
1208#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1209#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1210#define MODE_Pretty  11  /* Pretty-print schemas */
1211#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1212#define MODE_Json    13  /* Output JSON */
1213#define MODE_Markdown 14 /* Markdown formatting */
1214#define MODE_Table   15  /* MySQL-style table formatting */
1215#define MODE_Box     16  /* Unicode box-drawing characters */
1216
1217static const char *modeDescr[] = {
1218  "line",
1219  "column",
1220  "list",
1221  "semi",
1222  "html",
1223  "insert",
1224  "quote",
1225  "tcl",
1226  "csv",
1227  "explain",
1228  "ascii",
1229  "prettyprint",
1230  "eqp",
1231  "json",
1232  "markdown",
1233  "table",
1234  "box"
1235};
1236
1237/*
1238** These are the column/row/line separators used by the various
1239** import/export modes.
1240*/
1241#define SEP_Column    "|"
1242#define SEP_Row       "\n"
1243#define SEP_Tab       "\t"
1244#define SEP_Space     " "
1245#define SEP_Comma     ","
1246#define SEP_CrLf      "\r\n"
1247#define SEP_Unit      "\x1F"
1248#define SEP_Record    "\x1E"
1249
1250/*
1251** A callback for the sqlite3_log() interface.
1252*/
1253static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1254  ShellState *p = (ShellState*)pArg;
1255  if( p->pLog==0 ) return;
1256  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1257  fflush(p->pLog);
1258}
1259
1260/*
1261** SQL function:  shell_putsnl(X)
1262**
1263** Write the text X to the screen (or whatever output is being directed)
1264** adding a newline at the end, and then return X.
1265*/
1266static void shellPutsFunc(
1267  sqlite3_context *pCtx,
1268  int nVal,
1269  sqlite3_value **apVal
1270){
1271  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1272  (void)nVal;
1273  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1274  sqlite3_result_value(pCtx, apVal[0]);
1275}
1276
1277/*
1278** SQL function:   edit(VALUE)
1279**                 edit(VALUE,EDITOR)
1280**
1281** These steps:
1282**
1283**     (1) Write VALUE into a temporary file.
1284**     (2) Run program EDITOR on that temporary file.
1285**     (3) Read the temporary file back and return its content as the result.
1286**     (4) Delete the temporary file
1287**
1288** If the EDITOR argument is omitted, use the value in the VISUAL
1289** environment variable.  If still there is no EDITOR, through an error.
1290**
1291** Also throw an error if the EDITOR program returns a non-zero exit code.
1292*/
1293#ifndef SQLITE_NOHAVE_SYSTEM
1294static void editFunc(
1295  sqlite3_context *context,
1296  int argc,
1297  sqlite3_value **argv
1298){
1299  const char *zEditor;
1300  char *zTempFile = 0;
1301  sqlite3 *db;
1302  char *zCmd = 0;
1303  int bBin;
1304  int rc;
1305  int hasCRNL = 0;
1306  FILE *f = 0;
1307  sqlite3_int64 sz;
1308  sqlite3_int64 x;
1309  unsigned char *p = 0;
1310
1311  if( argc==2 ){
1312    zEditor = (const char*)sqlite3_value_text(argv[1]);
1313  }else{
1314    zEditor = getenv("VISUAL");
1315  }
1316  if( zEditor==0 ){
1317    sqlite3_result_error(context, "no editor for edit()", -1);
1318    return;
1319  }
1320  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1321    sqlite3_result_error(context, "NULL input to edit()", -1);
1322    return;
1323  }
1324  db = sqlite3_context_db_handle(context);
1325  zTempFile = 0;
1326  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1327  if( zTempFile==0 ){
1328    sqlite3_uint64 r = 0;
1329    sqlite3_randomness(sizeof(r), &r);
1330    zTempFile = sqlite3_mprintf("temp%llx", r);
1331    if( zTempFile==0 ){
1332      sqlite3_result_error_nomem(context);
1333      return;
1334    }
1335  }
1336  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1337  /* When writing the file to be edited, do \n to \r\n conversions on systems
1338  ** that want \r\n line endings */
1339  f = fopen(zTempFile, bBin ? "wb" : "w");
1340  if( f==0 ){
1341    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1342    goto edit_func_end;
1343  }
1344  sz = sqlite3_value_bytes(argv[0]);
1345  if( bBin ){
1346    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1347  }else{
1348    const char *z = (const char*)sqlite3_value_text(argv[0]);
1349    /* Remember whether or not the value originally contained \r\n */
1350    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1351    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1352  }
1353  fclose(f);
1354  f = 0;
1355  if( x!=sz ){
1356    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1357    goto edit_func_end;
1358  }
1359  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1360  if( zCmd==0 ){
1361    sqlite3_result_error_nomem(context);
1362    goto edit_func_end;
1363  }
1364  rc = system(zCmd);
1365  sqlite3_free(zCmd);
1366  if( rc ){
1367    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1368    goto edit_func_end;
1369  }
1370  f = fopen(zTempFile, "rb");
1371  if( f==0 ){
1372    sqlite3_result_error(context,
1373      "edit() cannot reopen temp file after edit", -1);
1374    goto edit_func_end;
1375  }
1376  fseek(f, 0, SEEK_END);
1377  sz = ftell(f);
1378  rewind(f);
1379  p = sqlite3_malloc64( sz+1 );
1380  if( p==0 ){
1381    sqlite3_result_error_nomem(context);
1382    goto edit_func_end;
1383  }
1384  x = fread(p, 1, (size_t)sz, f);
1385  fclose(f);
1386  f = 0;
1387  if( x!=sz ){
1388    sqlite3_result_error(context, "could not read back the whole file", -1);
1389    goto edit_func_end;
1390  }
1391  if( bBin ){
1392    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1393  }else{
1394    sqlite3_int64 i, j;
1395    if( hasCRNL ){
1396      /* If the original contains \r\n then do no conversions back to \n */
1397      j = sz;
1398    }else{
1399      /* If the file did not originally contain \r\n then convert any new
1400      ** \r\n back into \n */
1401      for(i=j=0; i<sz; i++){
1402        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1403        p[j++] = p[i];
1404      }
1405      sz = j;
1406      p[sz] = 0;
1407    }
1408    sqlite3_result_text64(context, (const char*)p, sz,
1409                          sqlite3_free, SQLITE_UTF8);
1410  }
1411  p = 0;
1412
1413edit_func_end:
1414  if( f ) fclose(f);
1415  unlink(zTempFile);
1416  sqlite3_free(zTempFile);
1417  sqlite3_free(p);
1418}
1419#endif /* SQLITE_NOHAVE_SYSTEM */
1420
1421/*
1422** Save or restore the current output mode
1423*/
1424static void outputModePush(ShellState *p){
1425  p->modePrior = p->mode;
1426  p->priorShFlgs = p->shellFlgs;
1427  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1428  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1429}
1430static void outputModePop(ShellState *p){
1431  p->mode = p->modePrior;
1432  p->shellFlgs = p->priorShFlgs;
1433  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1434  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1435}
1436
1437/*
1438** Output the given string as a hex-encoded blob (eg. X'1234' )
1439*/
1440static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1441  int i;
1442  char *zBlob = (char *)pBlob;
1443  raw_printf(out,"X'");
1444  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1445  raw_printf(out,"'");
1446}
1447
1448/*
1449** Find a string that is not found anywhere in z[].  Return a pointer
1450** to that string.
1451**
1452** Try to use zA and zB first.  If both of those are already found in z[]
1453** then make up some string and store it in the buffer zBuf.
1454*/
1455static const char *unused_string(
1456  const char *z,                    /* Result must not appear anywhere in z */
1457  const char *zA, const char *zB,   /* Try these first */
1458  char *zBuf                        /* Space to store a generated string */
1459){
1460  unsigned i = 0;
1461  if( strstr(z, zA)==0 ) return zA;
1462  if( strstr(z, zB)==0 ) return zB;
1463  do{
1464    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1465  }while( strstr(z,zBuf)!=0 );
1466  return zBuf;
1467}
1468
1469/*
1470** Output the given string as a quoted string using SQL quoting conventions.
1471**
1472** See also: output_quoted_escaped_string()
1473*/
1474static void output_quoted_string(FILE *out, const char *z){
1475  int i;
1476  char c;
1477  setBinaryMode(out, 1);
1478  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1479  if( c==0 ){
1480    utf8_printf(out,"'%s'",z);
1481  }else{
1482    raw_printf(out, "'");
1483    while( *z ){
1484      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1485      if( c=='\'' ) i++;
1486      if( i ){
1487        utf8_printf(out, "%.*s", i, z);
1488        z += i;
1489      }
1490      if( c=='\'' ){
1491        raw_printf(out, "'");
1492        continue;
1493      }
1494      if( c==0 ){
1495        break;
1496      }
1497      z++;
1498    }
1499    raw_printf(out, "'");
1500  }
1501  setTextMode(out, 1);
1502}
1503
1504/*
1505** Output the given string as a quoted string using SQL quoting conventions.
1506** Additionallly , escape the "\n" and "\r" characters so that they do not
1507** get corrupted by end-of-line translation facilities in some operating
1508** systems.
1509**
1510** This is like output_quoted_string() but with the addition of the \r\n
1511** escape mechanism.
1512*/
1513static void output_quoted_escaped_string(FILE *out, const char *z){
1514  int i;
1515  char c;
1516  setBinaryMode(out, 1);
1517  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1518  if( c==0 ){
1519    utf8_printf(out,"'%s'",z);
1520  }else{
1521    const char *zNL = 0;
1522    const char *zCR = 0;
1523    int nNL = 0;
1524    int nCR = 0;
1525    char zBuf1[20], zBuf2[20];
1526    for(i=0; z[i]; i++){
1527      if( z[i]=='\n' ) nNL++;
1528      if( z[i]=='\r' ) nCR++;
1529    }
1530    if( nNL ){
1531      raw_printf(out, "replace(");
1532      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1533    }
1534    if( nCR ){
1535      raw_printf(out, "replace(");
1536      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1537    }
1538    raw_printf(out, "'");
1539    while( *z ){
1540      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1541      if( c=='\'' ) i++;
1542      if( i ){
1543        utf8_printf(out, "%.*s", i, z);
1544        z += i;
1545      }
1546      if( c=='\'' ){
1547        raw_printf(out, "'");
1548        continue;
1549      }
1550      if( c==0 ){
1551        break;
1552      }
1553      z++;
1554      if( c=='\n' ){
1555        raw_printf(out, "%s", zNL);
1556        continue;
1557      }
1558      raw_printf(out, "%s", zCR);
1559    }
1560    raw_printf(out, "'");
1561    if( nCR ){
1562      raw_printf(out, ",'%s',char(13))", zCR);
1563    }
1564    if( nNL ){
1565      raw_printf(out, ",'%s',char(10))", zNL);
1566    }
1567  }
1568  setTextMode(out, 1);
1569}
1570
1571/*
1572** Output the given string as a quoted according to C or TCL quoting rules.
1573*/
1574static void output_c_string(FILE *out, const char *z){
1575  unsigned int c;
1576  fputc('"', out);
1577  while( (c = *(z++))!=0 ){
1578    if( c=='\\' ){
1579      fputc(c, out);
1580      fputc(c, out);
1581    }else if( c=='"' ){
1582      fputc('\\', out);
1583      fputc('"', out);
1584    }else if( c=='\t' ){
1585      fputc('\\', out);
1586      fputc('t', out);
1587    }else if( c=='\n' ){
1588      fputc('\\', out);
1589      fputc('n', out);
1590    }else if( c=='\r' ){
1591      fputc('\\', out);
1592      fputc('r', out);
1593    }else if( !isprint(c&0xff) ){
1594      raw_printf(out, "\\%03o", c&0xff);
1595    }else{
1596      fputc(c, out);
1597    }
1598  }
1599  fputc('"', out);
1600}
1601
1602/*
1603** Output the given string as a quoted according to JSON quoting rules.
1604*/
1605static void output_json_string(FILE *out, const char *z, int n){
1606  unsigned int c;
1607  if( n<0 ) n = (int)strlen(z);
1608  fputc('"', out);
1609  while( n-- ){
1610    c = *(z++);
1611    if( c=='\\' || c=='"' ){
1612      fputc('\\', out);
1613      fputc(c, out);
1614    }else if( c<=0x1f ){
1615      fputc('\\', out);
1616      if( c=='\b' ){
1617        fputc('b', out);
1618      }else if( c=='\f' ){
1619        fputc('f', out);
1620      }else if( c=='\n' ){
1621        fputc('n', out);
1622      }else if( c=='\r' ){
1623        fputc('r', out);
1624      }else if( c=='\t' ){
1625        fputc('t', out);
1626      }else{
1627         raw_printf(out, "u%04x",c);
1628      }
1629    }else{
1630      fputc(c, out);
1631    }
1632  }
1633  fputc('"', out);
1634}
1635
1636/*
1637** Output the given string with characters that are special to
1638** HTML escaped.
1639*/
1640static void output_html_string(FILE *out, const char *z){
1641  int i;
1642  if( z==0 ) z = "";
1643  while( *z ){
1644    for(i=0;   z[i]
1645            && z[i]!='<'
1646            && z[i]!='&'
1647            && z[i]!='>'
1648            && z[i]!='\"'
1649            && z[i]!='\'';
1650        i++){}
1651    if( i>0 ){
1652      utf8_printf(out,"%.*s",i,z);
1653    }
1654    if( z[i]=='<' ){
1655      raw_printf(out,"&lt;");
1656    }else if( z[i]=='&' ){
1657      raw_printf(out,"&amp;");
1658    }else if( z[i]=='>' ){
1659      raw_printf(out,"&gt;");
1660    }else if( z[i]=='\"' ){
1661      raw_printf(out,"&quot;");
1662    }else if( z[i]=='\'' ){
1663      raw_printf(out,"&#39;");
1664    }else{
1665      break;
1666    }
1667    z += i + 1;
1668  }
1669}
1670
1671/*
1672** If a field contains any character identified by a 1 in the following
1673** array, then the string must be quoted for CSV.
1674*/
1675static const char needCsvQuote[] = {
1676  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1677  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1678  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1679  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1680  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1681  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1682  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1683  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1684  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1685  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1686  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1687  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1688  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1689  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1690  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1691  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1692};
1693
1694/*
1695** Output a single term of CSV.  Actually, p->colSeparator is used for
1696** the separator, which may or may not be a comma.  p->nullValue is
1697** the null value.  Strings are quoted if necessary.  The separator
1698** is only issued if bSep is true.
1699*/
1700static void output_csv(ShellState *p, const char *z, int bSep){
1701  FILE *out = p->out;
1702  if( z==0 ){
1703    utf8_printf(out,"%s",p->nullValue);
1704  }else{
1705    int i;
1706    int nSep = strlen30(p->colSeparator);
1707    for(i=0; z[i]; i++){
1708      if( needCsvQuote[((unsigned char*)z)[i]]
1709         || (z[i]==p->colSeparator[0] &&
1710             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1711        i = 0;
1712        break;
1713      }
1714    }
1715    if( i==0 ){
1716      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1717      utf8_printf(out, "%s", zQuoted);
1718      sqlite3_free(zQuoted);
1719    }else{
1720      utf8_printf(out, "%s", z);
1721    }
1722  }
1723  if( bSep ){
1724    utf8_printf(p->out, "%s", p->colSeparator);
1725  }
1726}
1727
1728/*
1729** This routine runs when the user presses Ctrl-C
1730*/
1731static void interrupt_handler(int NotUsed){
1732  UNUSED_PARAMETER(NotUsed);
1733  seenInterrupt++;
1734  if( seenInterrupt>2 ) exit(1);
1735  if( globalDb ) sqlite3_interrupt(globalDb);
1736}
1737
1738#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1739/*
1740** This routine runs for console events (e.g. Ctrl-C) on Win32
1741*/
1742static BOOL WINAPI ConsoleCtrlHandler(
1743  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1744){
1745  if( dwCtrlType==CTRL_C_EVENT ){
1746    interrupt_handler(0);
1747    return TRUE;
1748  }
1749  return FALSE;
1750}
1751#endif
1752
1753#ifndef SQLITE_OMIT_AUTHORIZATION
1754/*
1755** When the ".auth ON" is set, the following authorizer callback is
1756** invoked.  It always returns SQLITE_OK.
1757*/
1758static int shellAuth(
1759  void *pClientData,
1760  int op,
1761  const char *zA1,
1762  const char *zA2,
1763  const char *zA3,
1764  const char *zA4
1765){
1766  ShellState *p = (ShellState*)pClientData;
1767  static const char *azAction[] = { 0,
1768     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1769     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1770     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1771     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1772     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1773     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1774     "PRAGMA",               "READ",                 "SELECT",
1775     "TRANSACTION",          "UPDATE",               "ATTACH",
1776     "DETACH",               "ALTER_TABLE",          "REINDEX",
1777     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1778     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1779  };
1780  int i;
1781  const char *az[4];
1782  az[0] = zA1;
1783  az[1] = zA2;
1784  az[2] = zA3;
1785  az[3] = zA4;
1786  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1787  for(i=0; i<4; i++){
1788    raw_printf(p->out, " ");
1789    if( az[i] ){
1790      output_c_string(p->out, az[i]);
1791    }else{
1792      raw_printf(p->out, "NULL");
1793    }
1794  }
1795  raw_printf(p->out, "\n");
1796  return SQLITE_OK;
1797}
1798#endif
1799
1800/*
1801** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1802**
1803** This routine converts some CREATE TABLE statements for shadow tables
1804** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1805*/
1806static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1807  if( z==0 ) return;
1808  if( zTail==0 ) return;
1809  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1810    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1811  }else{
1812    utf8_printf(out, "%s%s", z, zTail);
1813  }
1814}
1815static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1816  char c = z[n];
1817  z[n] = 0;
1818  printSchemaLine(out, z, zTail);
1819  z[n] = c;
1820}
1821
1822/*
1823** Return true if string z[] has nothing but whitespace and comments to the
1824** end of the first line.
1825*/
1826static int wsToEol(const char *z){
1827  int i;
1828  for(i=0; z[i]; i++){
1829    if( z[i]=='\n' ) return 1;
1830    if( IsSpace(z[i]) ) continue;
1831    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1832    return 0;
1833  }
1834  return 1;
1835}
1836
1837/*
1838** Add a new entry to the EXPLAIN QUERY PLAN data
1839*/
1840static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1841  EQPGraphRow *pNew;
1842  int nText = strlen30(zText);
1843  if( p->autoEQPtest ){
1844    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1845  }
1846  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1847  if( pNew==0 ) shell_out_of_memory();
1848  pNew->iEqpId = iEqpId;
1849  pNew->iParentId = p2;
1850  memcpy(pNew->zText, zText, nText+1);
1851  pNew->pNext = 0;
1852  if( p->sGraph.pLast ){
1853    p->sGraph.pLast->pNext = pNew;
1854  }else{
1855    p->sGraph.pRow = pNew;
1856  }
1857  p->sGraph.pLast = pNew;
1858}
1859
1860/*
1861** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1862** in p->sGraph.
1863*/
1864static void eqp_reset(ShellState *p){
1865  EQPGraphRow *pRow, *pNext;
1866  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1867    pNext = pRow->pNext;
1868    sqlite3_free(pRow);
1869  }
1870  memset(&p->sGraph, 0, sizeof(p->sGraph));
1871}
1872
1873/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1874** pOld, or return the first such line if pOld is NULL
1875*/
1876static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1877  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1878  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1879  return pRow;
1880}
1881
1882/* Render a single level of the graph that has iEqpId as its parent.  Called
1883** recursively to render sublevels.
1884*/
1885static void eqp_render_level(ShellState *p, int iEqpId){
1886  EQPGraphRow *pRow, *pNext;
1887  int n = strlen30(p->sGraph.zPrefix);
1888  char *z;
1889  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1890    pNext = eqp_next_row(p, iEqpId, pRow);
1891    z = pRow->zText;
1892    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1893                pNext ? "|--" : "`--", z);
1894    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1895      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1896      eqp_render_level(p, pRow->iEqpId);
1897      p->sGraph.zPrefix[n] = 0;
1898    }
1899  }
1900}
1901
1902/*
1903** Display and reset the EXPLAIN QUERY PLAN data
1904*/
1905static void eqp_render(ShellState *p){
1906  EQPGraphRow *pRow = p->sGraph.pRow;
1907  if( pRow ){
1908    if( pRow->zText[0]=='-' ){
1909      if( pRow->pNext==0 ){
1910        eqp_reset(p);
1911        return;
1912      }
1913      utf8_printf(p->out, "%s\n", pRow->zText+3);
1914      p->sGraph.pRow = pRow->pNext;
1915      sqlite3_free(pRow);
1916    }else{
1917      utf8_printf(p->out, "QUERY PLAN\n");
1918    }
1919    p->sGraph.zPrefix[0] = 0;
1920    eqp_render_level(p, 0);
1921    eqp_reset(p);
1922  }
1923}
1924
1925#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1926/*
1927** Progress handler callback.
1928*/
1929static int progress_handler(void *pClientData) {
1930  ShellState *p = (ShellState*)pClientData;
1931  p->nProgress++;
1932  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1933    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1934    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1935    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1936    return 1;
1937  }
1938  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1939    raw_printf(p->out, "Progress %u\n", p->nProgress);
1940  }
1941  return 0;
1942}
1943#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1944
1945/*
1946** Print N dashes
1947*/
1948static void print_dashes(FILE *out, int N){
1949  const char zDash[] = "--------------------------------------------------";
1950  const int nDash = sizeof(zDash) - 1;
1951  while( N>nDash ){
1952    fputs(zDash, out);
1953    N -= nDash;
1954  }
1955  raw_printf(out, "%.*s", N, zDash);
1956}
1957
1958/*
1959** Print a markdown or table-style row separator using ascii-art
1960*/
1961static void print_row_separator(
1962  ShellState *p,
1963  int nArg,
1964  const char *zSep
1965){
1966  int i;
1967  if( nArg>0 ){
1968    fputs(zSep, p->out);
1969    print_dashes(p->out, p->actualWidth[0]+2);
1970    for(i=1; i<nArg; i++){
1971      fputs(zSep, p->out);
1972      print_dashes(p->out, p->actualWidth[i]+2);
1973    }
1974    fputs(zSep, p->out);
1975  }
1976  fputs("\n", p->out);
1977}
1978
1979/*
1980** This is the callback routine that the shell
1981** invokes for each row of a query result.
1982*/
1983static int shell_callback(
1984  void *pArg,
1985  int nArg,        /* Number of result columns */
1986  char **azArg,    /* Text of each result column */
1987  char **azCol,    /* Column names */
1988  int *aiType      /* Column types.  Might be NULL */
1989){
1990  int i;
1991  ShellState *p = (ShellState*)pArg;
1992
1993  if( azArg==0 ) return 0;
1994  switch( p->cMode ){
1995    case MODE_Line: {
1996      int w = 5;
1997      if( azArg==0 ) break;
1998      for(i=0; i<nArg; i++){
1999        int len = strlen30(azCol[i] ? azCol[i] : "");
2000        if( len>w ) w = len;
2001      }
2002      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2003      for(i=0; i<nArg; i++){
2004        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2005                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2006      }
2007      break;
2008    }
2009    case MODE_Explain: {
2010      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2011      if( nArg>ArraySize(aExplainWidth) ){
2012        nArg = ArraySize(aExplainWidth);
2013      }
2014      if( p->cnt++==0 ){
2015        for(i=0; i<nArg; i++){
2016          int w = aExplainWidth[i];
2017          utf8_width_print(p->out, w, azCol[i]);
2018          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2019        }
2020        for(i=0; i<nArg; i++){
2021          int w = aExplainWidth[i];
2022          print_dashes(p->out, w);
2023          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2024        }
2025      }
2026      if( azArg==0 ) break;
2027      for(i=0; i<nArg; i++){
2028        int w = aExplainWidth[i];
2029        if( azArg[i] && strlenChar(azArg[i])>w ){
2030          w = strlenChar(azArg[i]);
2031        }
2032        if( i==1 && p->aiIndent && p->pStmt ){
2033          if( p->iIndent<p->nIndent ){
2034            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2035          }
2036          p->iIndent++;
2037        }
2038        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2039        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2040      }
2041      break;
2042    }
2043    case MODE_Semi: {   /* .schema and .fullschema output */
2044      printSchemaLine(p->out, azArg[0], ";\n");
2045      break;
2046    }
2047    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2048      char *z;
2049      int j;
2050      int nParen = 0;
2051      char cEnd = 0;
2052      char c;
2053      int nLine = 0;
2054      assert( nArg==1 );
2055      if( azArg[0]==0 ) break;
2056      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2057       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2058      ){
2059        utf8_printf(p->out, "%s;\n", azArg[0]);
2060        break;
2061      }
2062      z = sqlite3_mprintf("%s", azArg[0]);
2063      j = 0;
2064      for(i=0; IsSpace(z[i]); i++){}
2065      for(; (c = z[i])!=0; i++){
2066        if( IsSpace(c) ){
2067          if( z[j-1]=='\r' ) z[j-1] = '\n';
2068          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2069        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2070          j--;
2071        }
2072        z[j++] = c;
2073      }
2074      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2075      z[j] = 0;
2076      if( strlen30(z)>=79 ){
2077        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2078          if( c==cEnd ){
2079            cEnd = 0;
2080          }else if( c=='"' || c=='\'' || c=='`' ){
2081            cEnd = c;
2082          }else if( c=='[' ){
2083            cEnd = ']';
2084          }else if( c=='-' && z[i+1]=='-' ){
2085            cEnd = '\n';
2086          }else if( c=='(' ){
2087            nParen++;
2088          }else if( c==')' ){
2089            nParen--;
2090            if( nLine>0 && nParen==0 && j>0 ){
2091              printSchemaLineN(p->out, z, j, "\n");
2092              j = 0;
2093            }
2094          }
2095          z[j++] = c;
2096          if( nParen==1 && cEnd==0
2097           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2098          ){
2099            if( c=='\n' ) j--;
2100            printSchemaLineN(p->out, z, j, "\n  ");
2101            j = 0;
2102            nLine++;
2103            while( IsSpace(z[i+1]) ){ i++; }
2104          }
2105        }
2106        z[j] = 0;
2107      }
2108      printSchemaLine(p->out, z, ";\n");
2109      sqlite3_free(z);
2110      break;
2111    }
2112    case MODE_List: {
2113      if( p->cnt++==0 && p->showHeader ){
2114        for(i=0; i<nArg; i++){
2115          utf8_printf(p->out,"%s%s",azCol[i],
2116                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2117        }
2118      }
2119      if( azArg==0 ) break;
2120      for(i=0; i<nArg; i++){
2121        char *z = azArg[i];
2122        if( z==0 ) z = p->nullValue;
2123        utf8_printf(p->out, "%s", z);
2124        if( i<nArg-1 ){
2125          utf8_printf(p->out, "%s", p->colSeparator);
2126        }else{
2127          utf8_printf(p->out, "%s", p->rowSeparator);
2128        }
2129      }
2130      break;
2131    }
2132    case MODE_Html: {
2133      if( p->cnt++==0 && p->showHeader ){
2134        raw_printf(p->out,"<TR>");
2135        for(i=0; i<nArg; i++){
2136          raw_printf(p->out,"<TH>");
2137          output_html_string(p->out, azCol[i]);
2138          raw_printf(p->out,"</TH>\n");
2139        }
2140        raw_printf(p->out,"</TR>\n");
2141      }
2142      if( azArg==0 ) break;
2143      raw_printf(p->out,"<TR>");
2144      for(i=0; i<nArg; i++){
2145        raw_printf(p->out,"<TD>");
2146        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2147        raw_printf(p->out,"</TD>\n");
2148      }
2149      raw_printf(p->out,"</TR>\n");
2150      break;
2151    }
2152    case MODE_Tcl: {
2153      if( p->cnt++==0 && p->showHeader ){
2154        for(i=0; i<nArg; i++){
2155          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2156          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2157        }
2158        utf8_printf(p->out, "%s", p->rowSeparator);
2159      }
2160      if( azArg==0 ) break;
2161      for(i=0; i<nArg; i++){
2162        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2163        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2164      }
2165      utf8_printf(p->out, "%s", p->rowSeparator);
2166      break;
2167    }
2168    case MODE_Csv: {
2169      setBinaryMode(p->out, 1);
2170      if( p->cnt++==0 && p->showHeader ){
2171        for(i=0; i<nArg; i++){
2172          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2173        }
2174        utf8_printf(p->out, "%s", p->rowSeparator);
2175      }
2176      if( nArg>0 ){
2177        for(i=0; i<nArg; i++){
2178          output_csv(p, azArg[i], i<nArg-1);
2179        }
2180        utf8_printf(p->out, "%s", p->rowSeparator);
2181      }
2182      setTextMode(p->out, 1);
2183      break;
2184    }
2185    case MODE_Insert: {
2186      if( azArg==0 ) break;
2187      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2188      if( p->showHeader ){
2189        raw_printf(p->out,"(");
2190        for(i=0; i<nArg; i++){
2191          if( i>0 ) raw_printf(p->out, ",");
2192          if( quoteChar(azCol[i]) ){
2193            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2194            utf8_printf(p->out, "%s", z);
2195            sqlite3_free(z);
2196          }else{
2197            raw_printf(p->out, "%s", azCol[i]);
2198          }
2199        }
2200        raw_printf(p->out,")");
2201      }
2202      p->cnt++;
2203      for(i=0; i<nArg; i++){
2204        raw_printf(p->out, i>0 ? "," : " VALUES(");
2205        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2206          utf8_printf(p->out,"NULL");
2207        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2208          if( ShellHasFlag(p, SHFLG_Newlines) ){
2209            output_quoted_string(p->out, azArg[i]);
2210          }else{
2211            output_quoted_escaped_string(p->out, azArg[i]);
2212          }
2213        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2214          utf8_printf(p->out,"%s", azArg[i]);
2215        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2216          char z[50];
2217          double r = sqlite3_column_double(p->pStmt, i);
2218          sqlite3_uint64 ur;
2219          memcpy(&ur,&r,sizeof(r));
2220          if( ur==0x7ff0000000000000LL ){
2221            raw_printf(p->out, "1e999");
2222          }else if( ur==0xfff0000000000000LL ){
2223            raw_printf(p->out, "-1e999");
2224          }else{
2225            sqlite3_snprintf(50,z,"%!.20g", r);
2226            raw_printf(p->out, "%s", z);
2227          }
2228        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2229          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2230          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2231          output_hex_blob(p->out, pBlob, nBlob);
2232        }else if( isNumber(azArg[i], 0) ){
2233          utf8_printf(p->out,"%s", azArg[i]);
2234        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2235          output_quoted_string(p->out, azArg[i]);
2236        }else{
2237          output_quoted_escaped_string(p->out, azArg[i]);
2238        }
2239      }
2240      raw_printf(p->out,");\n");
2241      break;
2242    }
2243    case MODE_Json: {
2244      if( azArg==0 ) break;
2245      if( p->cnt==0 ){
2246        fputs("[{", p->out);
2247      }else{
2248        fputs(",\n{", p->out);
2249      }
2250      p->cnt++;
2251      for(i=0; i<nArg; i++){
2252        output_json_string(p->out, azCol[i], -1);
2253        putc(':', p->out);
2254        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2255          fputs("null",p->out);
2256        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2257          char z[50];
2258          double r = sqlite3_column_double(p->pStmt, i);
2259          sqlite3_uint64 ur;
2260          memcpy(&ur,&r,sizeof(r));
2261          if( ur==0x7ff0000000000000LL ){
2262            raw_printf(p->out, "1e999");
2263          }else if( ur==0xfff0000000000000LL ){
2264            raw_printf(p->out, "-1e999");
2265          }else{
2266            sqlite3_snprintf(50,z,"%!.20g", r);
2267            raw_printf(p->out, "%s", z);
2268          }
2269        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2270          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2271          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2272          output_json_string(p->out, pBlob, nBlob);
2273        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2274          output_json_string(p->out, azArg[i], -1);
2275        }else{
2276          utf8_printf(p->out,"%s", azArg[i]);
2277        }
2278        if( i<nArg-1 ){
2279          putc(',', p->out);
2280        }
2281      }
2282      putc('}', p->out);
2283      break;
2284    }
2285    case MODE_Quote: {
2286      if( azArg==0 ) break;
2287      if( p->cnt==0 && p->showHeader ){
2288        for(i=0; i<nArg; i++){
2289          if( i>0 ) fputs(p->colSeparator, p->out);
2290          output_quoted_string(p->out, azCol[i]);
2291        }
2292        fputs(p->rowSeparator, p->out);
2293      }
2294      p->cnt++;
2295      for(i=0; i<nArg; i++){
2296        if( i>0 ) fputs(p->colSeparator, p->out);
2297        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2298          utf8_printf(p->out,"NULL");
2299        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2300          output_quoted_string(p->out, azArg[i]);
2301        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2302          utf8_printf(p->out,"%s", azArg[i]);
2303        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2304          char z[50];
2305          double r = sqlite3_column_double(p->pStmt, i);
2306          sqlite3_snprintf(50,z,"%!.20g", r);
2307          raw_printf(p->out, "%s", z);
2308        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2309          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2310          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2311          output_hex_blob(p->out, pBlob, nBlob);
2312        }else if( isNumber(azArg[i], 0) ){
2313          utf8_printf(p->out,"%s", azArg[i]);
2314        }else{
2315          output_quoted_string(p->out, azArg[i]);
2316        }
2317      }
2318      fputs(p->rowSeparator, p->out);
2319      break;
2320    }
2321    case MODE_Ascii: {
2322      if( p->cnt++==0 && p->showHeader ){
2323        for(i=0; i<nArg; i++){
2324          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2325          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2326        }
2327        utf8_printf(p->out, "%s", p->rowSeparator);
2328      }
2329      if( azArg==0 ) break;
2330      for(i=0; i<nArg; i++){
2331        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2332        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2333      }
2334      utf8_printf(p->out, "%s", p->rowSeparator);
2335      break;
2336    }
2337    case MODE_EQP: {
2338      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2339      break;
2340    }
2341  }
2342  return 0;
2343}
2344
2345/*
2346** This is the callback routine that the SQLite library
2347** invokes for each row of a query result.
2348*/
2349static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2350  /* since we don't have type info, call the shell_callback with a NULL value */
2351  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2352}
2353
2354/*
2355** This is the callback routine from sqlite3_exec() that appends all
2356** output onto the end of a ShellText object.
2357*/
2358static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2359  ShellText *p = (ShellText*)pArg;
2360  int i;
2361  UNUSED_PARAMETER(az);
2362  if( azArg==0 ) return 0;
2363  if( p->n ) appendText(p, "|", 0);
2364  for(i=0; i<nArg; i++){
2365    if( i ) appendText(p, ",", 0);
2366    if( azArg[i] ) appendText(p, azArg[i], 0);
2367  }
2368  return 0;
2369}
2370
2371/*
2372** Generate an appropriate SELFTEST table in the main database.
2373*/
2374static void createSelftestTable(ShellState *p){
2375  char *zErrMsg = 0;
2376  sqlite3_exec(p->db,
2377    "SAVEPOINT selftest_init;\n"
2378    "CREATE TABLE IF NOT EXISTS selftest(\n"
2379    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2380    "  op TEXT,\n"                   /* Operator:  memo run */
2381    "  cmd TEXT,\n"                  /* Command text */
2382    "  ans TEXT\n"                   /* Desired answer */
2383    ");"
2384    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2385    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2386    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2387    "         'memo','Tests generated by --init');\n"
2388    "INSERT INTO [_shell$self]\n"
2389    "  SELECT 'run',\n"
2390    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2391                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2392    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2393                          "FROM sqlite_schema ORDER BY 2',224));\n"
2394    "INSERT INTO [_shell$self]\n"
2395    "  SELECT 'run',"
2396    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2397    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2398    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2399    "  FROM (\n"
2400    "    SELECT name FROM sqlite_schema\n"
2401    "     WHERE type='table'\n"
2402    "       AND name<>'selftest'\n"
2403    "       AND coalesce(rootpage,0)>0\n"
2404    "  )\n"
2405    " ORDER BY name;\n"
2406    "INSERT INTO [_shell$self]\n"
2407    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2408    "INSERT INTO selftest(tno,op,cmd,ans)"
2409    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2410    "DROP TABLE [_shell$self];"
2411    ,0,0,&zErrMsg);
2412  if( zErrMsg ){
2413    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2414    sqlite3_free(zErrMsg);
2415  }
2416  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2417}
2418
2419
2420/*
2421** Set the destination table field of the ShellState structure to
2422** the name of the table given.  Escape any quote characters in the
2423** table name.
2424*/
2425static void set_table_name(ShellState *p, const char *zName){
2426  int i, n;
2427  char cQuote;
2428  char *z;
2429
2430  if( p->zDestTable ){
2431    free(p->zDestTable);
2432    p->zDestTable = 0;
2433  }
2434  if( zName==0 ) return;
2435  cQuote = quoteChar(zName);
2436  n = strlen30(zName);
2437  if( cQuote ) n += n+2;
2438  z = p->zDestTable = malloc( n+1 );
2439  if( z==0 ) shell_out_of_memory();
2440  n = 0;
2441  if( cQuote ) z[n++] = cQuote;
2442  for(i=0; zName[i]; i++){
2443    z[n++] = zName[i];
2444    if( zName[i]==cQuote ) z[n++] = cQuote;
2445  }
2446  if( cQuote ) z[n++] = cQuote;
2447  z[n] = 0;
2448}
2449
2450
2451/*
2452** Execute a query statement that will generate SQL output.  Print
2453** the result columns, comma-separated, on a line and then add a
2454** semicolon terminator to the end of that line.
2455**
2456** If the number of columns is 1 and that column contains text "--"
2457** then write the semicolon on a separate line.  That way, if a
2458** "--" comment occurs at the end of the statement, the comment
2459** won't consume the semicolon terminator.
2460*/
2461static int run_table_dump_query(
2462  ShellState *p,           /* Query context */
2463  const char *zSelect      /* SELECT statement to extract content */
2464){
2465  sqlite3_stmt *pSelect;
2466  int rc;
2467  int nResult;
2468  int i;
2469  const char *z;
2470  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2471  if( rc!=SQLITE_OK || !pSelect ){
2472    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2473                sqlite3_errmsg(p->db));
2474    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2475    return rc;
2476  }
2477  rc = sqlite3_step(pSelect);
2478  nResult = sqlite3_column_count(pSelect);
2479  while( rc==SQLITE_ROW ){
2480    z = (const char*)sqlite3_column_text(pSelect, 0);
2481    utf8_printf(p->out, "%s", z);
2482    for(i=1; i<nResult; i++){
2483      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2484    }
2485    if( z==0 ) z = "";
2486    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2487    if( z[0] ){
2488      raw_printf(p->out, "\n;\n");
2489    }else{
2490      raw_printf(p->out, ";\n");
2491    }
2492    rc = sqlite3_step(pSelect);
2493  }
2494  rc = sqlite3_finalize(pSelect);
2495  if( rc!=SQLITE_OK ){
2496    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2497                sqlite3_errmsg(p->db));
2498    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2499  }
2500  return rc;
2501}
2502
2503/*
2504** Allocate space and save off current error string.
2505*/
2506static char *save_err_msg(
2507  sqlite3 *db            /* Database to query */
2508){
2509  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2510  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2511  if( zErrMsg ){
2512    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2513  }
2514  return zErrMsg;
2515}
2516
2517#ifdef __linux__
2518/*
2519** Attempt to display I/O stats on Linux using /proc/PID/io
2520*/
2521static void displayLinuxIoStats(FILE *out){
2522  FILE *in;
2523  char z[200];
2524  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2525  in = fopen(z, "rb");
2526  if( in==0 ) return;
2527  while( fgets(z, sizeof(z), in)!=0 ){
2528    static const struct {
2529      const char *zPattern;
2530      const char *zDesc;
2531    } aTrans[] = {
2532      { "rchar: ",                  "Bytes received by read():" },
2533      { "wchar: ",                  "Bytes sent to write():"    },
2534      { "syscr: ",                  "Read() system calls:"      },
2535      { "syscw: ",                  "Write() system calls:"     },
2536      { "read_bytes: ",             "Bytes read from storage:"  },
2537      { "write_bytes: ",            "Bytes written to storage:" },
2538      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2539    };
2540    int i;
2541    for(i=0; i<ArraySize(aTrans); i++){
2542      int n = strlen30(aTrans[i].zPattern);
2543      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2544        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2545        break;
2546      }
2547    }
2548  }
2549  fclose(in);
2550}
2551#endif
2552
2553/*
2554** Display a single line of status using 64-bit values.
2555*/
2556static void displayStatLine(
2557  ShellState *p,            /* The shell context */
2558  char *zLabel,             /* Label for this one line */
2559  char *zFormat,            /* Format for the result */
2560  int iStatusCtrl,          /* Which status to display */
2561  int bReset                /* True to reset the stats */
2562){
2563  sqlite3_int64 iCur = -1;
2564  sqlite3_int64 iHiwtr = -1;
2565  int i, nPercent;
2566  char zLine[200];
2567  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2568  for(i=0, nPercent=0; zFormat[i]; i++){
2569    if( zFormat[i]=='%' ) nPercent++;
2570  }
2571  if( nPercent>1 ){
2572    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2573  }else{
2574    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2575  }
2576  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2577}
2578
2579/*
2580** Display memory stats.
2581*/
2582static int display_stats(
2583  sqlite3 *db,                /* Database to query */
2584  ShellState *pArg,           /* Pointer to ShellState */
2585  int bReset                  /* True to reset the stats */
2586){
2587  int iCur;
2588  int iHiwtr;
2589  FILE *out;
2590  if( pArg==0 || pArg->out==0 ) return 0;
2591  out = pArg->out;
2592
2593  if( pArg->pStmt && (pArg->statsOn & 2) ){
2594    int nCol, i, x;
2595    sqlite3_stmt *pStmt = pArg->pStmt;
2596    char z[100];
2597    nCol = sqlite3_column_count(pStmt);
2598    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2599    for(i=0; i<nCol; i++){
2600      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2601      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2602#ifndef SQLITE_OMIT_DECLTYPE
2603      sqlite3_snprintf(30, z+x, "declared type:");
2604      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2605#endif
2606#ifdef SQLITE_ENABLE_COLUMN_METADATA
2607      sqlite3_snprintf(30, z+x, "database name:");
2608      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2609      sqlite3_snprintf(30, z+x, "table name:");
2610      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2611      sqlite3_snprintf(30, z+x, "origin name:");
2612      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2613#endif
2614    }
2615  }
2616
2617  displayStatLine(pArg, "Memory Used:",
2618     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2619  displayStatLine(pArg, "Number of Outstanding Allocations:",
2620     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2621  if( pArg->shellFlgs & SHFLG_Pagecache ){
2622    displayStatLine(pArg, "Number of Pcache Pages Used:",
2623       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2624  }
2625  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2626     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2627  displayStatLine(pArg, "Largest Allocation:",
2628     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2629  displayStatLine(pArg, "Largest Pcache Allocation:",
2630     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2631#ifdef YYTRACKMAXSTACKDEPTH
2632  displayStatLine(pArg, "Deepest Parser Stack:",
2633     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2634#endif
2635
2636  if( db ){
2637    if( pArg->shellFlgs & SHFLG_Lookaside ){
2638      iHiwtr = iCur = -1;
2639      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2640                        &iCur, &iHiwtr, bReset);
2641      raw_printf(pArg->out,
2642              "Lookaside Slots Used:                %d (max %d)\n",
2643              iCur, iHiwtr);
2644      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2645                        &iCur, &iHiwtr, bReset);
2646      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2647              iHiwtr);
2648      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2649                        &iCur, &iHiwtr, bReset);
2650      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2651              iHiwtr);
2652      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2653                        &iCur, &iHiwtr, bReset);
2654      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2655              iHiwtr);
2656    }
2657    iHiwtr = iCur = -1;
2658    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2659    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2660            iCur);
2661    iHiwtr = iCur = -1;
2662    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2663    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2664    iHiwtr = iCur = -1;
2665    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2666    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2667    iHiwtr = iCur = -1;
2668    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2669    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2670    iHiwtr = iCur = -1;
2671    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2672    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2673    iHiwtr = iCur = -1;
2674    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2675    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2676            iCur);
2677    iHiwtr = iCur = -1;
2678    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2679    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2680            iCur);
2681  }
2682
2683  if( pArg->pStmt ){
2684    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2685                               bReset);
2686    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2687    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2688    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2689    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2690    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2691    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2692    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2693    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2694    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2695    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2696    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2697    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2698    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2699  }
2700
2701#ifdef __linux__
2702  displayLinuxIoStats(pArg->out);
2703#endif
2704
2705  /* Do not remove this machine readable comment: extra-stats-output-here */
2706
2707  return 0;
2708}
2709
2710/*
2711** Display scan stats.
2712*/
2713static void display_scanstats(
2714  sqlite3 *db,                    /* Database to query */
2715  ShellState *pArg                /* Pointer to ShellState */
2716){
2717#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2718  UNUSED_PARAMETER(db);
2719  UNUSED_PARAMETER(pArg);
2720#else
2721  int i, k, n, mx;
2722  raw_printf(pArg->out, "-------- scanstats --------\n");
2723  mx = 0;
2724  for(k=0; k<=mx; k++){
2725    double rEstLoop = 1.0;
2726    for(i=n=0; 1; i++){
2727      sqlite3_stmt *p = pArg->pStmt;
2728      sqlite3_int64 nLoop, nVisit;
2729      double rEst;
2730      int iSid;
2731      const char *zExplain;
2732      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2733        break;
2734      }
2735      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2736      if( iSid>mx ) mx = iSid;
2737      if( iSid!=k ) continue;
2738      if( n==0 ){
2739        rEstLoop = (double)nLoop;
2740        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2741      }
2742      n++;
2743      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2744      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2745      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2746      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2747      rEstLoop *= rEst;
2748      raw_printf(pArg->out,
2749          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2750          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2751      );
2752    }
2753  }
2754  raw_printf(pArg->out, "---------------------------\n");
2755#endif
2756}
2757
2758/*
2759** Parameter azArray points to a zero-terminated array of strings. zStr
2760** points to a single nul-terminated string. Return non-zero if zStr
2761** is equal, according to strcmp(), to any of the strings in the array.
2762** Otherwise, return zero.
2763*/
2764static int str_in_array(const char *zStr, const char **azArray){
2765  int i;
2766  for(i=0; azArray[i]; i++){
2767    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2768  }
2769  return 0;
2770}
2771
2772/*
2773** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2774** and populate the ShellState.aiIndent[] array with the number of
2775** spaces each opcode should be indented before it is output.
2776**
2777** The indenting rules are:
2778**
2779**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2780**       all opcodes that occur between the p2 jump destination and the opcode
2781**       itself by 2 spaces.
2782**
2783**     * For each "Goto", if the jump destination is earlier in the program
2784**       and ends on one of:
2785**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2786**       or if the P1 parameter is one instead of zero,
2787**       then indent all opcodes between the earlier instruction
2788**       and "Goto" by 2 spaces.
2789*/
2790static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2791  const char *zSql;               /* The text of the SQL statement */
2792  const char *z;                  /* Used to check if this is an EXPLAIN */
2793  int *abYield = 0;               /* True if op is an OP_Yield */
2794  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2795  int iOp;                        /* Index of operation in p->aiIndent[] */
2796
2797  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2798  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2799                            "Rewind", 0 };
2800  const char *azGoto[] = { "Goto", 0 };
2801
2802  /* Try to figure out if this is really an EXPLAIN statement. If this
2803  ** cannot be verified, return early.  */
2804  if( sqlite3_column_count(pSql)!=8 ){
2805    p->cMode = p->mode;
2806    return;
2807  }
2808  zSql = sqlite3_sql(pSql);
2809  if( zSql==0 ) return;
2810  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2811  if( sqlite3_strnicmp(z, "explain", 7) ){
2812    p->cMode = p->mode;
2813    return;
2814  }
2815
2816  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2817    int i;
2818    int iAddr = sqlite3_column_int(pSql, 0);
2819    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2820
2821    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2822    ** p2 is an instruction address, set variable p2op to the index of that
2823    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2824    ** the current instruction is part of a sub-program generated by an
2825    ** SQL trigger or foreign key.  */
2826    int p2 = sqlite3_column_int(pSql, 3);
2827    int p2op = (p2 + (iOp-iAddr));
2828
2829    /* Grow the p->aiIndent array as required */
2830    if( iOp>=nAlloc ){
2831      if( iOp==0 ){
2832        /* Do further verfication that this is explain output.  Abort if
2833        ** it is not */
2834        static const char *explainCols[] = {
2835           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2836        int jj;
2837        for(jj=0; jj<ArraySize(explainCols); jj++){
2838          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2839            p->cMode = p->mode;
2840            sqlite3_reset(pSql);
2841            return;
2842          }
2843        }
2844      }
2845      nAlloc += 100;
2846      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2847      if( p->aiIndent==0 ) shell_out_of_memory();
2848      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2849      if( abYield==0 ) shell_out_of_memory();
2850    }
2851    abYield[iOp] = str_in_array(zOp, azYield);
2852    p->aiIndent[iOp] = 0;
2853    p->nIndent = iOp+1;
2854
2855    if( str_in_array(zOp, azNext) ){
2856      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2857    }
2858    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2859     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2860    ){
2861      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2862    }
2863  }
2864
2865  p->iIndent = 0;
2866  sqlite3_free(abYield);
2867  sqlite3_reset(pSql);
2868}
2869
2870/*
2871** Free the array allocated by explain_data_prepare().
2872*/
2873static void explain_data_delete(ShellState *p){
2874  sqlite3_free(p->aiIndent);
2875  p->aiIndent = 0;
2876  p->nIndent = 0;
2877  p->iIndent = 0;
2878}
2879
2880/*
2881** Disable and restore .wheretrace and .selecttrace settings.
2882*/
2883#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2884extern unsigned int sqlite3_unsupported_selecttrace;
2885static int savedSelectTrace;
2886#endif
2887#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2888extern int sqlite3WhereTrace;
2889static int savedWhereTrace;
2890#endif
2891static void disable_debug_trace_modes(void){
2892#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2893  savedSelectTrace = sqlite3_unsupported_selecttrace;
2894  sqlite3_unsupported_selecttrace = 0;
2895#endif
2896#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2897  savedWhereTrace = sqlite3WhereTrace;
2898  sqlite3WhereTrace = 0;
2899#endif
2900}
2901static void restore_debug_trace_modes(void){
2902#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2903  sqlite3_unsupported_selecttrace = savedSelectTrace;
2904#endif
2905#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2906  sqlite3WhereTrace = savedWhereTrace;
2907#endif
2908}
2909
2910/* Create the TEMP table used to store parameter bindings */
2911static void bind_table_init(ShellState *p){
2912  int wrSchema = 0;
2913  int defensiveMode = 0;
2914  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2915  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2916  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2917  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2918  sqlite3_exec(p->db,
2919    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2920    "  key TEXT PRIMARY KEY,\n"
2921    "  value ANY\n"
2922    ") WITHOUT ROWID;",
2923    0, 0, 0);
2924  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2925  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2926}
2927
2928/*
2929** Bind parameters on a prepared statement.
2930**
2931** Parameter bindings are taken from a TEMP table of the form:
2932**
2933**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2934**    WITHOUT ROWID;
2935**
2936** No bindings occur if this table does not exist.  The name of the table
2937** begins with "sqlite_" so that it will not collide with ordinary application
2938** tables.  The table must be in the TEMP schema.
2939*/
2940static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2941  int nVar;
2942  int i;
2943  int rc;
2944  sqlite3_stmt *pQ = 0;
2945
2946  nVar = sqlite3_bind_parameter_count(pStmt);
2947  if( nVar==0 ) return;  /* Nothing to do */
2948  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2949                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2950    return; /* Parameter table does not exist */
2951  }
2952  rc = sqlite3_prepare_v2(pArg->db,
2953          "SELECT value FROM temp.sqlite_parameters"
2954          " WHERE key=?1", -1, &pQ, 0);
2955  if( rc || pQ==0 ) return;
2956  for(i=1; i<=nVar; i++){
2957    char zNum[30];
2958    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2959    if( zVar==0 ){
2960      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2961      zVar = zNum;
2962    }
2963    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2964    if( sqlite3_step(pQ)==SQLITE_ROW ){
2965      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2966    }else{
2967      sqlite3_bind_null(pStmt, i);
2968    }
2969    sqlite3_reset(pQ);
2970  }
2971  sqlite3_finalize(pQ);
2972}
2973
2974/*
2975** UTF8 box-drawing characters.  Imagine box lines like this:
2976**
2977**           1
2978**           |
2979**       4 --+-- 2
2980**           |
2981**           3
2982**
2983** Each box characters has between 2 and 4 of the lines leading from
2984** the center.  The characters are here identified by the numbers of
2985** their corresponding lines.
2986*/
2987#define BOX_24   "\342\224\200"  /* U+2500 --- */
2988#define BOX_13   "\342\224\202"  /* U+2502  |  */
2989#define BOX_23   "\342\224\214"  /* U+250c  ,- */
2990#define BOX_34   "\342\224\220"  /* U+2510 -,  */
2991#define BOX_12   "\342\224\224"  /* U+2514  '- */
2992#define BOX_14   "\342\224\230"  /* U+2518 -'  */
2993#define BOX_123  "\342\224\234"  /* U+251c  |- */
2994#define BOX_134  "\342\224\244"  /* U+2524 -|  */
2995#define BOX_234  "\342\224\254"  /* U+252c -,- */
2996#define BOX_124  "\342\224\264"  /* U+2534 -'- */
2997#define BOX_1234 "\342\224\274"  /* U+253c -|- */
2998
2999/* Draw horizontal line N characters long using unicode box
3000** characters
3001*/
3002static void print_box_line(FILE *out, int N){
3003  const char zDash[] =
3004      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3005      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3006  const int nDash = sizeof(zDash) - 1;
3007  N *= 3;
3008  while( N>nDash ){
3009    utf8_printf(out, zDash);
3010    N -= nDash;
3011  }
3012  utf8_printf(out, "%.*s", N, zDash);
3013}
3014
3015/*
3016** Draw a horizontal separator for a MODE_Box table.
3017*/
3018static void print_box_row_separator(
3019  ShellState *p,
3020  int nArg,
3021  const char *zSep1,
3022  const char *zSep2,
3023  const char *zSep3
3024){
3025  int i;
3026  if( nArg>0 ){
3027    utf8_printf(p->out, "%s", zSep1);
3028    print_box_line(p->out, p->actualWidth[0]+2);
3029    for(i=1; i<nArg; i++){
3030      utf8_printf(p->out, "%s", zSep2);
3031      print_box_line(p->out, p->actualWidth[i]+2);
3032    }
3033    utf8_printf(p->out, "%s", zSep3);
3034  }
3035  fputs("\n", p->out);
3036}
3037
3038
3039
3040/*
3041** Run a prepared statement and output the result in one of the
3042** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3043** or MODE_Box.
3044**
3045** This is different from ordinary exec_prepared_stmt() in that
3046** it has to run the entire query and gather the results into memory
3047** first, in order to determine column widths, before providing
3048** any output.
3049*/
3050static void exec_prepared_stmt_columnar(
3051  ShellState *p,                        /* Pointer to ShellState */
3052  sqlite3_stmt *pStmt                   /* Statment to run */
3053){
3054  sqlite3_int64 nRow = 0;
3055  int nColumn = 0;
3056  char **azData = 0;
3057  sqlite3_int64 nAlloc = 0;
3058  const char *z;
3059  int rc;
3060  sqlite3_int64 i, nData;
3061  int j, nTotal, w, n;
3062  const char *colSep = 0;
3063  const char *rowSep = 0;
3064
3065  rc = sqlite3_step(pStmt);
3066  if( rc!=SQLITE_ROW ) return;
3067  nColumn = sqlite3_column_count(pStmt);
3068  nAlloc = nColumn*4;
3069  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3070  if( azData==0 ) shell_out_of_memory();
3071  for(i=0; i<nColumn; i++){
3072    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3073  }
3074  do{
3075    if( (nRow+2)*nColumn >= nAlloc ){
3076      nAlloc *= 2;
3077      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3078      if( azData==0 ) shell_out_of_memory();
3079    }
3080    nRow++;
3081    for(i=0; i<nColumn; i++){
3082      z = (const char*)sqlite3_column_text(pStmt,i);
3083      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3084    }
3085  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
3086  if( nColumn>p->nWidth ){
3087    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3088    if( p->colWidth==0 ) shell_out_of_memory();
3089    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3090    p->nWidth = nColumn;
3091    p->actualWidth = &p->colWidth[nColumn];
3092  }
3093  memset(p->actualWidth, 0, nColumn*sizeof(int));
3094  for(i=0; i<nColumn; i++){
3095    w = p->colWidth[i];
3096    if( w<0 ) w = -w;
3097    p->actualWidth[i] = w;
3098  }
3099  nTotal = nColumn*(nRow+1);
3100  for(i=0; i<nTotal; i++){
3101    z = azData[i];
3102    if( z==0 ) z = p->nullValue;
3103    n = strlenChar(z);
3104    j = i%nColumn;
3105    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3106  }
3107  if( seenInterrupt ) goto columnar_end;
3108  switch( p->cMode ){
3109    case MODE_Column: {
3110      colSep = "  ";
3111      rowSep = "\n";
3112      if( p->showHeader ){
3113        for(i=0; i<nColumn; i++){
3114          w = p->actualWidth[i];
3115          if( p->colWidth[i]<0 ) w = -w;
3116          utf8_width_print(p->out, w, azData[i]);
3117          fputs(i==nColumn-1?"\n":"  ", p->out);
3118        }
3119        for(i=0; i<nColumn; i++){
3120          print_dashes(p->out, p->actualWidth[i]);
3121          fputs(i==nColumn-1?"\n":"  ", p->out);
3122        }
3123      }
3124      break;
3125    }
3126    case MODE_Table: {
3127      colSep = " | ";
3128      rowSep = " |\n";
3129      print_row_separator(p, nColumn, "+");
3130      fputs("| ", p->out);
3131      for(i=0; i<nColumn; i++){
3132        w = p->actualWidth[i];
3133        n = strlenChar(azData[i]);
3134        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3135        fputs(i==nColumn-1?" |\n":" | ", p->out);
3136      }
3137      print_row_separator(p, nColumn, "+");
3138      break;
3139    }
3140    case MODE_Markdown: {
3141      colSep = " | ";
3142      rowSep = " |\n";
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_Box: {
3154      colSep = " " BOX_13 " ";
3155      rowSep = " " BOX_13 "\n";
3156      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3157      utf8_printf(p->out, BOX_13 " ");
3158      for(i=0; i<nColumn; i++){
3159        w = p->actualWidth[i];
3160        n = strlenChar(azData[i]);
3161        utf8_printf(p->out, "%*s%s%*s%s",
3162            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3163            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3164      }
3165      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3166      break;
3167    }
3168  }
3169  for(i=nColumn, j=0; i<nTotal; i++, j++){
3170    if( j==0 && p->cMode!=MODE_Column ){
3171      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3172    }
3173    z = azData[i];
3174    if( z==0 ) z = p->nullValue;
3175    w = p->actualWidth[j];
3176    if( p->colWidth[j]<0 ) w = -w;
3177    utf8_width_print(p->out, w, z);
3178    if( j==nColumn-1 ){
3179      utf8_printf(p->out, "%s", rowSep);
3180      j = -1;
3181      if( seenInterrupt ) goto columnar_end;
3182    }else{
3183      utf8_printf(p->out, "%s", colSep);
3184    }
3185  }
3186  if( p->cMode==MODE_Table ){
3187    print_row_separator(p, nColumn, "+");
3188  }else if( p->cMode==MODE_Box ){
3189    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3190  }
3191columnar_end:
3192  if( seenInterrupt ){
3193    utf8_printf(p->out, "Interrupt\n");
3194  }
3195  nData = (nRow+1)*nColumn;
3196  for(i=0; i<nData; i++) free(azData[i]);
3197  sqlite3_free(azData);
3198}
3199
3200/*
3201** Run a prepared statement
3202*/
3203static void exec_prepared_stmt(
3204  ShellState *pArg,                                /* Pointer to ShellState */
3205  sqlite3_stmt *pStmt                              /* Statment to run */
3206){
3207  int rc;
3208
3209  if( pArg->cMode==MODE_Column
3210   || pArg->cMode==MODE_Table
3211   || pArg->cMode==MODE_Box
3212   || pArg->cMode==MODE_Markdown
3213  ){
3214    exec_prepared_stmt_columnar(pArg, pStmt);
3215    return;
3216  }
3217
3218  /* perform the first step.  this will tell us if we
3219  ** have a result set or not and how wide it is.
3220  */
3221  rc = sqlite3_step(pStmt);
3222  /* if we have a result set... */
3223  if( SQLITE_ROW == rc ){
3224    /* allocate space for col name ptr, value ptr, and type */
3225    int nCol = sqlite3_column_count(pStmt);
3226    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3227    if( !pData ){
3228      rc = SQLITE_NOMEM;
3229    }else{
3230      char **azCols = (char **)pData;      /* Names of result columns */
3231      char **azVals = &azCols[nCol];       /* Results */
3232      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3233      int i, x;
3234      assert(sizeof(int) <= sizeof(char *));
3235      /* save off ptrs to column names */
3236      for(i=0; i<nCol; i++){
3237        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3238      }
3239      do{
3240        /* extract the data and data types */
3241        for(i=0; i<nCol; i++){
3242          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3243          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3244            azVals[i] = "";
3245          }else{
3246            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3247          }
3248          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3249            rc = SQLITE_NOMEM;
3250            break; /* from for */
3251          }
3252        } /* end for */
3253
3254        /* if data and types extracted successfully... */
3255        if( SQLITE_ROW == rc ){
3256          /* call the supplied callback with the result row data */
3257          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3258            rc = SQLITE_ABORT;
3259          }else{
3260            rc = sqlite3_step(pStmt);
3261          }
3262        }
3263      } while( SQLITE_ROW == rc );
3264      sqlite3_free(pData);
3265      if( pArg->cMode==MODE_Json ){
3266        fputs("]\n", pArg->out);
3267      }
3268    }
3269  }
3270}
3271
3272#ifndef SQLITE_OMIT_VIRTUALTABLE
3273/*
3274** This function is called to process SQL if the previous shell command
3275** was ".expert". It passes the SQL in the second argument directly to
3276** the sqlite3expert object.
3277**
3278** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3279** code. In this case, (*pzErr) may be set to point to a buffer containing
3280** an English language error message. It is the responsibility of the
3281** caller to eventually free this buffer using sqlite3_free().
3282*/
3283static int expertHandleSQL(
3284  ShellState *pState,
3285  const char *zSql,
3286  char **pzErr
3287){
3288  assert( pState->expert.pExpert );
3289  assert( pzErr==0 || *pzErr==0 );
3290  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3291}
3292
3293/*
3294** This function is called either to silently clean up the object
3295** created by the ".expert" command (if bCancel==1), or to generate a
3296** report from it and then clean it up (if bCancel==0).
3297**
3298** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3299** code. In this case, (*pzErr) may be set to point to a buffer containing
3300** an English language error message. It is the responsibility of the
3301** caller to eventually free this buffer using sqlite3_free().
3302*/
3303static int expertFinish(
3304  ShellState *pState,
3305  int bCancel,
3306  char **pzErr
3307){
3308  int rc = SQLITE_OK;
3309  sqlite3expert *p = pState->expert.pExpert;
3310  assert( p );
3311  assert( bCancel || pzErr==0 || *pzErr==0 );
3312  if( bCancel==0 ){
3313    FILE *out = pState->out;
3314    int bVerbose = pState->expert.bVerbose;
3315
3316    rc = sqlite3_expert_analyze(p, pzErr);
3317    if( rc==SQLITE_OK ){
3318      int nQuery = sqlite3_expert_count(p);
3319      int i;
3320
3321      if( bVerbose ){
3322        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3323        raw_printf(out, "-- Candidates -----------------------------\n");
3324        raw_printf(out, "%s\n", zCand);
3325      }
3326      for(i=0; i<nQuery; i++){
3327        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3328        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3329        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3330        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3331        if( bVerbose ){
3332          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3333          raw_printf(out, "%s\n\n", zSql);
3334        }
3335        raw_printf(out, "%s\n", zIdx);
3336        raw_printf(out, "%s\n", zEQP);
3337      }
3338    }
3339  }
3340  sqlite3_expert_destroy(p);
3341  pState->expert.pExpert = 0;
3342  return rc;
3343}
3344
3345/*
3346** Implementation of ".expert" dot command.
3347*/
3348static int expertDotCommand(
3349  ShellState *pState,             /* Current shell tool state */
3350  char **azArg,                   /* Array of arguments passed to dot command */
3351  int nArg                        /* Number of entries in azArg[] */
3352){
3353  int rc = SQLITE_OK;
3354  char *zErr = 0;
3355  int i;
3356  int iSample = 0;
3357
3358  assert( pState->expert.pExpert==0 );
3359  memset(&pState->expert, 0, sizeof(ExpertInfo));
3360
3361  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3362    char *z = azArg[i];
3363    int n;
3364    if( z[0]=='-' && z[1]=='-' ) z++;
3365    n = strlen30(z);
3366    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3367      pState->expert.bVerbose = 1;
3368    }
3369    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3370      if( i==(nArg-1) ){
3371        raw_printf(stderr, "option requires an argument: %s\n", z);
3372        rc = SQLITE_ERROR;
3373      }else{
3374        iSample = (int)integerValue(azArg[++i]);
3375        if( iSample<0 || iSample>100 ){
3376          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3377          rc = SQLITE_ERROR;
3378        }
3379      }
3380    }
3381    else{
3382      raw_printf(stderr, "unknown option: %s\n", z);
3383      rc = SQLITE_ERROR;
3384    }
3385  }
3386
3387  if( rc==SQLITE_OK ){
3388    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3389    if( pState->expert.pExpert==0 ){
3390      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3391      rc = SQLITE_ERROR;
3392    }else{
3393      sqlite3_expert_config(
3394          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3395      );
3396    }
3397  }
3398
3399  return rc;
3400}
3401#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3402
3403/*
3404** Execute a statement or set of statements.  Print
3405** any result rows/columns depending on the current mode
3406** set via the supplied callback.
3407**
3408** This is very similar to SQLite's built-in sqlite3_exec()
3409** function except it takes a slightly different callback
3410** and callback data argument.
3411*/
3412static int shell_exec(
3413  ShellState *pArg,                         /* Pointer to ShellState */
3414  const char *zSql,                         /* SQL to be evaluated */
3415  char **pzErrMsg                           /* Error msg written here */
3416){
3417  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3418  int rc = SQLITE_OK;             /* Return Code */
3419  int rc2;
3420  const char *zLeftover;          /* Tail of unprocessed SQL */
3421  sqlite3 *db = pArg->db;
3422
3423  if( pzErrMsg ){
3424    *pzErrMsg = NULL;
3425  }
3426
3427#ifndef SQLITE_OMIT_VIRTUALTABLE
3428  if( pArg->expert.pExpert ){
3429    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3430    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3431  }
3432#endif
3433
3434  while( zSql[0] && (SQLITE_OK == rc) ){
3435    static const char *zStmtSql;
3436    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3437    if( SQLITE_OK != rc ){
3438      if( pzErrMsg ){
3439        *pzErrMsg = save_err_msg(db);
3440      }
3441    }else{
3442      if( !pStmt ){
3443        /* this happens for a comment or white-space */
3444        zSql = zLeftover;
3445        while( IsSpace(zSql[0]) ) zSql++;
3446        continue;
3447      }
3448      zStmtSql = sqlite3_sql(pStmt);
3449      if( zStmtSql==0 ) zStmtSql = "";
3450      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3451
3452      /* save off the prepared statment handle and reset row count */
3453      if( pArg ){
3454        pArg->pStmt = pStmt;
3455        pArg->cnt = 0;
3456      }
3457
3458      /* echo the sql statement if echo on */
3459      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3460        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3461      }
3462
3463      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3464      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3465        sqlite3_stmt *pExplain;
3466        char *zEQP;
3467        int triggerEQP = 0;
3468        disable_debug_trace_modes();
3469        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3470        if( pArg->autoEQP>=AUTOEQP_trigger ){
3471          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3472        }
3473        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3474        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3475        if( rc==SQLITE_OK ){
3476          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3477            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3478            int iEqpId = sqlite3_column_int(pExplain, 0);
3479            int iParentId = sqlite3_column_int(pExplain, 1);
3480            if( zEQPLine==0 ) zEQPLine = "";
3481            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3482            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3483          }
3484          eqp_render(pArg);
3485        }
3486        sqlite3_finalize(pExplain);
3487        sqlite3_free(zEQP);
3488        if( pArg->autoEQP>=AUTOEQP_full ){
3489          /* Also do an EXPLAIN for ".eqp full" mode */
3490          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3491          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3492          if( rc==SQLITE_OK ){
3493            pArg->cMode = MODE_Explain;
3494            explain_data_prepare(pArg, pExplain);
3495            exec_prepared_stmt(pArg, pExplain);
3496            explain_data_delete(pArg);
3497          }
3498          sqlite3_finalize(pExplain);
3499          sqlite3_free(zEQP);
3500        }
3501        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3502          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3503          /* Reprepare pStmt before reactiving trace modes */
3504          sqlite3_finalize(pStmt);
3505          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3506          if( pArg ) pArg->pStmt = pStmt;
3507        }
3508        restore_debug_trace_modes();
3509      }
3510
3511      if( pArg ){
3512        pArg->cMode = pArg->mode;
3513        if( pArg->autoExplain ){
3514          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3515            pArg->cMode = MODE_Explain;
3516          }
3517          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3518            pArg->cMode = MODE_EQP;
3519          }
3520        }
3521
3522        /* If the shell is currently in ".explain" mode, gather the extra
3523        ** data required to add indents to the output.*/
3524        if( pArg->cMode==MODE_Explain ){
3525          explain_data_prepare(pArg, pStmt);
3526        }
3527      }
3528
3529      bind_prepared_stmt(pArg, pStmt);
3530      exec_prepared_stmt(pArg, pStmt);
3531      explain_data_delete(pArg);
3532      eqp_render(pArg);
3533
3534      /* print usage stats if stats on */
3535      if( pArg && pArg->statsOn ){
3536        display_stats(db, pArg, 0);
3537      }
3538
3539      /* print loop-counters if required */
3540      if( pArg && pArg->scanstatsOn ){
3541        display_scanstats(db, pArg);
3542      }
3543
3544      /* Finalize the statement just executed. If this fails, save a
3545      ** copy of the error message. Otherwise, set zSql to point to the
3546      ** next statement to execute. */
3547      rc2 = sqlite3_finalize(pStmt);
3548      if( rc!=SQLITE_NOMEM ) rc = rc2;
3549      if( rc==SQLITE_OK ){
3550        zSql = zLeftover;
3551        while( IsSpace(zSql[0]) ) zSql++;
3552      }else if( pzErrMsg ){
3553        *pzErrMsg = save_err_msg(db);
3554      }
3555
3556      /* clear saved stmt handle */
3557      if( pArg ){
3558        pArg->pStmt = NULL;
3559      }
3560    }
3561  } /* end while */
3562
3563  return rc;
3564}
3565
3566/*
3567** Release memory previously allocated by tableColumnList().
3568*/
3569static void freeColumnList(char **azCol){
3570  int i;
3571  for(i=1; azCol[i]; i++){
3572    sqlite3_free(azCol[i]);
3573  }
3574  /* azCol[0] is a static string */
3575  sqlite3_free(azCol);
3576}
3577
3578/*
3579** Return a list of pointers to strings which are the names of all
3580** columns in table zTab.   The memory to hold the names is dynamically
3581** allocated and must be released by the caller using a subsequent call
3582** to freeColumnList().
3583**
3584** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3585** value that needs to be preserved, then azCol[0] is filled in with the
3586** name of the rowid column.
3587**
3588** The first regular column in the table is azCol[1].  The list is terminated
3589** by an entry with azCol[i]==0.
3590*/
3591static char **tableColumnList(ShellState *p, const char *zTab){
3592  char **azCol = 0;
3593  sqlite3_stmt *pStmt;
3594  char *zSql;
3595  int nCol = 0;
3596  int nAlloc = 0;
3597  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3598  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3599  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3600  int rc;
3601
3602  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3603  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3604  sqlite3_free(zSql);
3605  if( rc ) return 0;
3606  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3607    if( nCol>=nAlloc-2 ){
3608      nAlloc = nAlloc*2 + nCol + 10;
3609      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3610      if( azCol==0 ) shell_out_of_memory();
3611    }
3612    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3613    if( sqlite3_column_int(pStmt, 5) ){
3614      nPK++;
3615      if( nPK==1
3616       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3617                          "INTEGER")==0
3618      ){
3619        isIPK = 1;
3620      }else{
3621        isIPK = 0;
3622      }
3623    }
3624  }
3625  sqlite3_finalize(pStmt);
3626  if( azCol==0 ) return 0;
3627  azCol[0] = 0;
3628  azCol[nCol+1] = 0;
3629
3630  /* The decision of whether or not a rowid really needs to be preserved
3631  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3632  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3633  ** rowids on tables where the rowid is inaccessible because there are other
3634  ** columns in the table named "rowid", "_rowid_", and "oid".
3635  */
3636  if( preserveRowid && isIPK ){
3637    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3638    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3639    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3640    ** ROWID aliases.  To distinguish these cases, check to see if
3641    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3642    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3643    */
3644    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3645                           " WHERE origin='pk'", zTab);
3646    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3647    sqlite3_free(zSql);
3648    if( rc ){
3649      freeColumnList(azCol);
3650      return 0;
3651    }
3652    rc = sqlite3_step(pStmt);
3653    sqlite3_finalize(pStmt);
3654    preserveRowid = rc==SQLITE_ROW;
3655  }
3656  if( preserveRowid ){
3657    /* Only preserve the rowid if we can find a name to use for the
3658    ** rowid */
3659    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3660    int i, j;
3661    for(j=0; j<3; j++){
3662      for(i=1; i<=nCol; i++){
3663        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3664      }
3665      if( i>nCol ){
3666        /* At this point, we know that azRowid[j] is not the name of any
3667        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3668        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3669        ** tables will fail this last check */
3670        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3671        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3672        break;
3673      }
3674    }
3675  }
3676  return azCol;
3677}
3678
3679/*
3680** Toggle the reverse_unordered_selects setting.
3681*/
3682static void toggleSelectOrder(sqlite3 *db){
3683  sqlite3_stmt *pStmt = 0;
3684  int iSetting = 0;
3685  char zStmt[100];
3686  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3687  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3688    iSetting = sqlite3_column_int(pStmt, 0);
3689  }
3690  sqlite3_finalize(pStmt);
3691  sqlite3_snprintf(sizeof(zStmt), zStmt,
3692       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3693  sqlite3_exec(db, zStmt, 0, 0, 0);
3694}
3695
3696/*
3697** This is a different callback routine used for dumping the database.
3698** Each row received by this callback consists of a table name,
3699** the table type ("index" or "table") and SQL to create the table.
3700** This routine should print text sufficient to recreate the table.
3701*/
3702static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3703  int rc;
3704  const char *zTable;
3705  const char *zType;
3706  const char *zSql;
3707  ShellState *p = (ShellState *)pArg;
3708
3709  UNUSED_PARAMETER(azNotUsed);
3710  if( nArg!=3 || azArg==0 ) return 0;
3711  zTable = azArg[0];
3712  zType = azArg[1];
3713  zSql = azArg[2];
3714
3715  if( strcmp(zTable, "sqlite_sequence")==0 ){
3716    raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3717  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
3718    raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3719  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3720    return 0;
3721  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3722    char *zIns;
3723    if( !p->writableSchema ){
3724      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3725      p->writableSchema = 1;
3726    }
3727    zIns = sqlite3_mprintf(
3728       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3729       "VALUES('table','%q','%q',0,'%q');",
3730       zTable, zTable, zSql);
3731    utf8_printf(p->out, "%s\n", zIns);
3732    sqlite3_free(zIns);
3733    return 0;
3734  }else{
3735    printSchemaLine(p->out, zSql, ";\n");
3736  }
3737
3738  if( strcmp(zType, "table")==0 ){
3739    ShellText sSelect;
3740    ShellText sTable;
3741    char **azCol;
3742    int i;
3743    char *savedDestTable;
3744    int savedMode;
3745
3746    azCol = tableColumnList(p, zTable);
3747    if( azCol==0 ){
3748      p->nErr++;
3749      return 0;
3750    }
3751
3752    /* Always quote the table name, even if it appears to be pure ascii,
3753    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3754    initText(&sTable);
3755    appendText(&sTable, zTable, quoteChar(zTable));
3756    /* If preserving the rowid, add a column list after the table name.
3757    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3758    ** instead of the usual "INSERT INTO tab VALUES(...)".
3759    */
3760    if( azCol[0] ){
3761      appendText(&sTable, "(", 0);
3762      appendText(&sTable, azCol[0], 0);
3763      for(i=1; azCol[i]; i++){
3764        appendText(&sTable, ",", 0);
3765        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3766      }
3767      appendText(&sTable, ")", 0);
3768    }
3769
3770    /* Build an appropriate SELECT statement */
3771    initText(&sSelect);
3772    appendText(&sSelect, "SELECT ", 0);
3773    if( azCol[0] ){
3774      appendText(&sSelect, azCol[0], 0);
3775      appendText(&sSelect, ",", 0);
3776    }
3777    for(i=1; azCol[i]; i++){
3778      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3779      if( azCol[i+1] ){
3780        appendText(&sSelect, ",", 0);
3781      }
3782    }
3783    freeColumnList(azCol);
3784    appendText(&sSelect, " FROM ", 0);
3785    appendText(&sSelect, zTable, quoteChar(zTable));
3786
3787    savedDestTable = p->zDestTable;
3788    savedMode = p->mode;
3789    p->zDestTable = sTable.z;
3790    p->mode = p->cMode = MODE_Insert;
3791    rc = shell_exec(p, sSelect.z, 0);
3792    if( (rc&0xff)==SQLITE_CORRUPT ){
3793      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3794      toggleSelectOrder(p->db);
3795      shell_exec(p, sSelect.z, 0);
3796      toggleSelectOrder(p->db);
3797    }
3798    p->zDestTable = savedDestTable;
3799    p->mode = savedMode;
3800    freeText(&sTable);
3801    freeText(&sSelect);
3802    if( rc ) p->nErr++;
3803  }
3804  return 0;
3805}
3806
3807/*
3808** Run zQuery.  Use dump_callback() as the callback routine so that
3809** the contents of the query are output as SQL statements.
3810**
3811** If we get a SQLITE_CORRUPT error, rerun the query after appending
3812** "ORDER BY rowid DESC" to the end.
3813*/
3814static int run_schema_dump_query(
3815  ShellState *p,
3816  const char *zQuery
3817){
3818  int rc;
3819  char *zErr = 0;
3820  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3821  if( rc==SQLITE_CORRUPT ){
3822    char *zQ2;
3823    int len = strlen30(zQuery);
3824    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3825    if( zErr ){
3826      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3827      sqlite3_free(zErr);
3828      zErr = 0;
3829    }
3830    zQ2 = malloc( len+100 );
3831    if( zQ2==0 ) return rc;
3832    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3833    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3834    if( rc ){
3835      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3836    }else{
3837      rc = SQLITE_CORRUPT;
3838    }
3839    sqlite3_free(zErr);
3840    free(zQ2);
3841  }
3842  return rc;
3843}
3844
3845/*
3846** Text of help messages.
3847**
3848** The help text for each individual command begins with a line that starts
3849** with ".".  Subsequent lines are supplimental information.
3850**
3851** There must be two or more spaces between the end of the command and the
3852** start of the description of what that command does.
3853*/
3854static const char *(azHelp[]) = {
3855#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3856  ".archive ...             Manage SQL archives",
3857  "   Each command must have exactly one of the following options:",
3858  "     -c, --create               Create a new archive",
3859  "     -u, --update               Add or update files with changed mtime",
3860  "     -i, --insert               Like -u but always add even if unchanged",
3861  "     -t, --list                 List contents of archive",
3862  "     -x, --extract              Extract files from archive",
3863  "   Optional arguments:",
3864  "     -v, --verbose              Print each filename as it is processed",
3865  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3866  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3867  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3868  "     -n, --dryrun               Show the SQL that would have occurred",
3869  "   Examples:",
3870  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3871  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3872  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3873  "   See also:",
3874  "      http://sqlite.org/cli.html#sqlar_archive_support",
3875#endif
3876#ifndef SQLITE_OMIT_AUTHORIZATION
3877  ".auth ON|OFF             Show authorizer callbacks",
3878#endif
3879  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3880  "       --append            Use the appendvfs",
3881  "       --async             Write to FILE without journal and fsync()",
3882  ".bail on|off             Stop after hitting an error.  Default OFF",
3883  ".binary on|off           Turn binary output on or off.  Default OFF",
3884  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3885  ".changes on|off          Show number of rows changed by SQL",
3886  ".check GLOB              Fail if output since .testcase does not match",
3887  ".clone NEWDB             Clone data into NEWDB from the existing database",
3888  ".databases               List names and files of attached databases",
3889  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3890  ".dbinfo ?DB?             Show status information about the database",
3891  ".dump ?TABLE?            Render database content as SQL",
3892  "   Options:",
3893  "     --preserve-rowids      Include ROWID values in the output",
3894  "     --newlines             Allow unescaped newline characters in output",
3895  "   TABLE is a LIKE pattern for the tables to dump",
3896  "   Additional LIKE patterns can be given in subsequent arguments",
3897  ".echo on|off             Turn command echo on or off",
3898  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3899  "   Other Modes:",
3900#ifdef SQLITE_DEBUG
3901  "      test                  Show raw EXPLAIN QUERY PLAN output",
3902  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3903#endif
3904  "      trigger               Like \"full\" but also show trigger bytecode",
3905  ".excel                   Display the output of next command in spreadsheet",
3906  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
3907  ".exit ?CODE?             Exit this program with return-code CODE",
3908  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3909  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3910  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3911  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
3912  "   --help                  Show CMD details",
3913  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3914  ".headers on|off          Turn display of headers on or off",
3915  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3916  ".import FILE TABLE       Import data from FILE into TABLE",
3917  "   Options:",
3918  "     --ascii               Use \\037 and \\036 as column and row separators",
3919  "     --csv                 Use , and \\n as column and row separators",
3920  "     --skip N              Skip the first N rows of input",
3921  "     -v                    \"Verbose\" - increase auxiliary output",
3922  "   Notes:",
3923  "     *  If TABLE does not exist, it is created.  The first row of input",
3924  "        determines the column names.",
3925  "     *  If neither --csv or --ascii are used, the input mode is derived",
3926  "        from the \".mode\" output mode",
3927  "     *  If FILE begins with \"|\" then it is a command that generates the",
3928  "        input text.",
3929#ifndef SQLITE_OMIT_TEST_CONTROL
3930  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3931#endif
3932  ".indexes ?TABLE?         Show names of indexes",
3933  "                           If TABLE is specified, only show indexes for",
3934  "                           tables matching TABLE using the LIKE operator.",
3935#ifdef SQLITE_ENABLE_IOTRACE
3936  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3937#endif
3938  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3939  ".lint OPTIONS            Report potential schema issues.",
3940  "     Options:",
3941  "        fkey-indexes     Find missing foreign key indexes",
3942#ifndef SQLITE_OMIT_LOAD_EXTENSION
3943  ".load FILE ?ENTRY?       Load an extension library",
3944#endif
3945  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3946  ".mode MODE ?TABLE?       Set output mode",
3947  "   MODE is one of:",
3948  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
3949  "     box       Tables using unicode box-drawing characters",
3950  "     csv       Comma-separated values",
3951  "     column    Output in columns.  (See .width)",
3952  "     html      HTML <table> code",
3953  "     insert    SQL insert statements for TABLE",
3954  "     json      Results in a JSON array",
3955  "     line      One value per line",
3956  "     list      Values delimited by \"|\"",
3957  "     markdown  Markdown table format",
3958  "     quote     Escape answers as for SQL",
3959  "     table     ASCII-art table",
3960  "     tabs      Tab-separated values",
3961  "     tcl       TCL list elements",
3962  ".nullvalue STRING        Use STRING in place of NULL values",
3963  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
3964  "     If FILE begins with '|' then open as a pipe",
3965  "       --bom  Put a UTF8 byte-order mark at the beginning",
3966  "       -e     Send output to the system text editor",
3967  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
3968#ifdef SQLITE_DEBUG
3969  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
3970#endif
3971  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3972  "     Options:",
3973  "        --append        Use appendvfs to append database to the end of FILE",
3974#ifdef SQLITE_ENABLE_DESERIALIZE
3975  "        --deserialize   Load into memory useing sqlite3_deserialize()",
3976  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3977  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3978#endif
3979  "        --new           Initialize FILE to an empty database",
3980  "        --nofollow      Do not follow symbolic links",
3981  "        --readonly      Open FILE readonly",
3982  "        --zip           FILE is a ZIP archive",
3983  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
3984  "   If FILE begins with '|' then open it as a pipe.",
3985  "   Options:",
3986  "     --bom                 Prefix output with a UTF8 byte-order mark",
3987  "     -e                    Send output to the system text editor",
3988  "     -x                    Send output as CSV to a spreadsheet",
3989  ".parameter CMD ...       Manage SQL parameter bindings",
3990  "   clear                   Erase all bindings",
3991  "   init                    Initialize the TEMP table that holds bindings",
3992  "   list                    List the current parameter bindings",
3993  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
3994  "                           PARAMETER should start with one of: $ : @ ?",
3995  "   unset PARAMETER         Remove PARAMETER from the binding table",
3996  ".print STRING...         Print literal STRING",
3997#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
3998  ".progress N              Invoke progress handler after every N opcodes",
3999  "   --limit N                 Interrupt after N progress callbacks",
4000  "   --once                    Do no more than one progress interrupt",
4001  "   --quiet|-q                No output except at interrupts",
4002  "   --reset                   Reset the count for each input and interrupt",
4003#endif
4004  ".prompt MAIN CONTINUE    Replace the standard prompts",
4005  ".quit                    Exit this program",
4006  ".read FILE               Read input from FILE",
4007#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4008  ".recover                 Recover as much data as possible from corrupt db.",
4009  "   --freelist-corrupt       Assume the freelist is corrupt",
4010  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4011  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4012  "   --no-rowids              Do not attempt to recover rowid values",
4013  "                            that are not also INTEGER PRIMARY KEYs",
4014#endif
4015  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4016  ".save FILE               Write in-memory database into FILE",
4017  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4018  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4019  "     Options:",
4020  "         --indent            Try to pretty-print the schema",
4021  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4022  "    Options:",
4023  "       --init               Create a new SELFTEST table",
4024  "       -v                   Verbose output",
4025  ".separator COL ?ROW?     Change the column and row separators",
4026#if defined(SQLITE_ENABLE_SESSION)
4027  ".session ?NAME? CMD ...  Create or control sessions",
4028  "   Subcommands:",
4029  "     attach TABLE             Attach TABLE",
4030  "     changeset FILE           Write a changeset into FILE",
4031  "     close                    Close one session",
4032  "     enable ?BOOLEAN?         Set or query the enable bit",
4033  "     filter GLOB...           Reject tables matching GLOBs",
4034  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4035  "     isempty                  Query whether the session is empty",
4036  "     list                     List currently open session names",
4037  "     open DB NAME             Open a new session on DB",
4038  "     patchset FILE            Write a patchset into FILE",
4039  "   If ?NAME? is omitted, the first defined session is used.",
4040#endif
4041  ".sha3sum ...             Compute a SHA3 hash of database content",
4042  "    Options:",
4043  "      --schema              Also hash the sqlite_schema table",
4044  "      --sha3-224            Use the sha3-224 algorithm",
4045  "      --sha3-256            Use the sha3-256 algorithm (default)",
4046  "      --sha3-384            Use the sha3-384 algorithm",
4047  "      --sha3-512            Use the sha3-512 algorithm",
4048  "    Any other argument is a LIKE pattern for tables to hash",
4049#ifndef SQLITE_NOHAVE_SYSTEM
4050  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4051#endif
4052  ".show                    Show the current values for various settings",
4053  ".stats ?on|off?          Show stats or turn stats on or off",
4054#ifndef SQLITE_NOHAVE_SYSTEM
4055  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4056#endif
4057  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4058  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4059  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4060  "                           Run \".testctrl\" with no arguments for details",
4061  ".timeout MS              Try opening locked tables for MS milliseconds",
4062  ".timer on|off            Turn SQL timer on or off",
4063#ifndef SQLITE_OMIT_TRACE
4064  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4065  "    FILE                    Send output to FILE",
4066  "    stdout                  Send output to stdout",
4067  "    stderr                  Send output to stderr",
4068  "    off                     Disable tracing",
4069  "    --expanded              Expand query parameters",
4070#ifdef SQLITE_ENABLE_NORMALIZE
4071  "    --normalized            Normal the SQL statements",
4072#endif
4073  "    --plain                 Show SQL as it is input",
4074  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4075  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4076  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4077  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4078#endif /* SQLITE_OMIT_TRACE */
4079#ifdef SQLITE_DEBUG
4080  ".unmodule NAME ...       Unregister virtual table modules",
4081  "    --allexcept             Unregister everything except those named",
4082#endif
4083  ".vfsinfo ?AUX?           Information about the top-level VFS",
4084  ".vfslist                 List all available VFSes",
4085  ".vfsname ?AUX?           Print the name of the VFS stack",
4086  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4087  "     Negative values right-justify",
4088};
4089
4090/*
4091** Output help text.
4092**
4093** zPattern describes the set of commands for which help text is provided.
4094** If zPattern is NULL, then show all commands, but only give a one-line
4095** description of each.
4096**
4097** Return the number of matches.
4098*/
4099static int showHelp(FILE *out, const char *zPattern){
4100  int i = 0;
4101  int j = 0;
4102  int n = 0;
4103  char *zPat;
4104  if( zPattern==0
4105   || zPattern[0]=='0'
4106   || strcmp(zPattern,"-a")==0
4107   || strcmp(zPattern,"-all")==0
4108   || strcmp(zPattern,"--all")==0
4109  ){
4110    /* Show all commands, but only one line per command */
4111    if( zPattern==0 ) zPattern = "";
4112    for(i=0; i<ArraySize(azHelp); i++){
4113      if( azHelp[i][0]=='.' || zPattern[0] ){
4114        utf8_printf(out, "%s\n", azHelp[i]);
4115        n++;
4116      }
4117    }
4118  }else{
4119    /* Look for commands that for which zPattern is an exact prefix */
4120    zPat = sqlite3_mprintf(".%s*", zPattern);
4121    for(i=0; i<ArraySize(azHelp); i++){
4122      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4123        utf8_printf(out, "%s\n", azHelp[i]);
4124        j = i+1;
4125        n++;
4126      }
4127    }
4128    sqlite3_free(zPat);
4129    if( n ){
4130      if( n==1 ){
4131        /* when zPattern is a prefix of exactly one command, then include the
4132        ** details of that command, which should begin at offset j */
4133        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4134          utf8_printf(out, "%s\n", azHelp[j]);
4135          j++;
4136        }
4137      }
4138      return n;
4139    }
4140    /* Look for commands that contain zPattern anywhere.  Show the complete
4141    ** text of all commands that match. */
4142    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4143    for(i=0; i<ArraySize(azHelp); i++){
4144      if( azHelp[i][0]=='.' ) j = i;
4145      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4146        utf8_printf(out, "%s\n", azHelp[j]);
4147        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4148          j++;
4149          utf8_printf(out, "%s\n", azHelp[j]);
4150        }
4151        i = j;
4152        n++;
4153      }
4154    }
4155    sqlite3_free(zPat);
4156  }
4157  return n;
4158}
4159
4160/* Forward reference */
4161static int process_input(ShellState *p);
4162
4163/*
4164** Read the content of file zName into memory obtained from sqlite3_malloc64()
4165** and return a pointer to the buffer. The caller is responsible for freeing
4166** the memory.
4167**
4168** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4169** read.
4170**
4171** For convenience, a nul-terminator byte is always appended to the data read
4172** from the file before the buffer is returned. This byte is not included in
4173** the final value of (*pnByte), if applicable.
4174**
4175** NULL is returned if any error is encountered. The final value of *pnByte
4176** is undefined in this case.
4177*/
4178static char *readFile(const char *zName, int *pnByte){
4179  FILE *in = fopen(zName, "rb");
4180  long nIn;
4181  size_t nRead;
4182  char *pBuf;
4183  if( in==0 ) return 0;
4184  fseek(in, 0, SEEK_END);
4185  nIn = ftell(in);
4186  rewind(in);
4187  pBuf = sqlite3_malloc64( nIn+1 );
4188  if( pBuf==0 ){ fclose(in); return 0; }
4189  nRead = fread(pBuf, nIn, 1, in);
4190  fclose(in);
4191  if( nRead!=1 ){
4192    sqlite3_free(pBuf);
4193    return 0;
4194  }
4195  pBuf[nIn] = 0;
4196  if( pnByte ) *pnByte = nIn;
4197  return pBuf;
4198}
4199
4200#if defined(SQLITE_ENABLE_SESSION)
4201/*
4202** Close a single OpenSession object and release all of its associated
4203** resources.
4204*/
4205static void session_close(OpenSession *pSession){
4206  int i;
4207  sqlite3session_delete(pSession->p);
4208  sqlite3_free(pSession->zName);
4209  for(i=0; i<pSession->nFilter; i++){
4210    sqlite3_free(pSession->azFilter[i]);
4211  }
4212  sqlite3_free(pSession->azFilter);
4213  memset(pSession, 0, sizeof(OpenSession));
4214}
4215#endif
4216
4217/*
4218** Close all OpenSession objects and release all associated resources.
4219*/
4220#if defined(SQLITE_ENABLE_SESSION)
4221static void session_close_all(ShellState *p){
4222  int i;
4223  for(i=0; i<p->nSession; i++){
4224    session_close(&p->aSession[i]);
4225  }
4226  p->nSession = 0;
4227}
4228#else
4229# define session_close_all(X)
4230#endif
4231
4232/*
4233** Implementation of the xFilter function for an open session.  Omit
4234** any tables named by ".session filter" but let all other table through.
4235*/
4236#if defined(SQLITE_ENABLE_SESSION)
4237static int session_filter(void *pCtx, const char *zTab){
4238  OpenSession *pSession = (OpenSession*)pCtx;
4239  int i;
4240  for(i=0; i<pSession->nFilter; i++){
4241    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4242  }
4243  return 1;
4244}
4245#endif
4246
4247/*
4248** Try to deduce the type of file for zName based on its content.  Return
4249** one of the SHELL_OPEN_* constants.
4250**
4251** If the file does not exist or is empty but its name looks like a ZIP
4252** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4253** Otherwise, assume an ordinary database regardless of the filename if
4254** the type cannot be determined from content.
4255*/
4256int deduceDatabaseType(const char *zName, int dfltZip){
4257  FILE *f = fopen(zName, "rb");
4258  size_t n;
4259  int rc = SHELL_OPEN_UNSPEC;
4260  char zBuf[100];
4261  if( f==0 ){
4262    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4263       return SHELL_OPEN_ZIPFILE;
4264    }else{
4265       return SHELL_OPEN_NORMAL;
4266    }
4267  }
4268  n = fread(zBuf, 16, 1, f);
4269  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4270    fclose(f);
4271    return SHELL_OPEN_NORMAL;
4272  }
4273  fseek(f, -25, SEEK_END);
4274  n = fread(zBuf, 25, 1, f);
4275  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4276    rc = SHELL_OPEN_APPENDVFS;
4277  }else{
4278    fseek(f, -22, SEEK_END);
4279    n = fread(zBuf, 22, 1, f);
4280    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4281       && zBuf[3]==0x06 ){
4282      rc = SHELL_OPEN_ZIPFILE;
4283    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4284      rc = SHELL_OPEN_ZIPFILE;
4285    }
4286  }
4287  fclose(f);
4288  return rc;
4289}
4290
4291#ifdef SQLITE_ENABLE_DESERIALIZE
4292/*
4293** Reconstruct an in-memory database using the output from the "dbtotxt"
4294** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
4295** is 0, then read from standard input.
4296*/
4297static unsigned char *readHexDb(ShellState *p, int *pnData){
4298  unsigned char *a = 0;
4299  int nLine;
4300  int n = 0;
4301  int pgsz = 0;
4302  int iOffset = 0;
4303  int j, k;
4304  int rc;
4305  FILE *in;
4306  unsigned int x[16];
4307  char zLine[1000];
4308  if( p->zDbFilename ){
4309    in = fopen(p->zDbFilename, "r");
4310    if( in==0 ){
4311      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
4312      return 0;
4313    }
4314    nLine = 0;
4315  }else{
4316    in = p->in;
4317    nLine = p->lineno;
4318    if( in==0 ) in = stdin;
4319  }
4320  *pnData = 0;
4321  nLine++;
4322  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4323  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4324  if( rc!=2 ) goto readHexDb_error;
4325  if( n<0 ) goto readHexDb_error;
4326  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4327  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4328  a = sqlite3_malloc( n ? n : 1 );
4329  if( a==0 ){
4330    utf8_printf(stderr, "Out of memory!\n");
4331    goto readHexDb_error;
4332  }
4333  memset(a, 0, n);
4334  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4335    utf8_printf(stderr, "invalid pagesize\n");
4336    goto readHexDb_error;
4337  }
4338  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4339    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4340    if( rc==2 ){
4341      iOffset = k;
4342      continue;
4343    }
4344    if( strncmp(zLine, "| end ", 6)==0 ){
4345      break;
4346    }
4347    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4348                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4349                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4350    if( rc==17 ){
4351      k = iOffset+j;
4352      if( k+16<=n ){
4353        int ii;
4354        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4355      }
4356    }
4357  }
4358  *pnData = n;
4359  if( in!=p->in ){
4360    fclose(in);
4361  }else{
4362    p->lineno = nLine;
4363  }
4364  return a;
4365
4366readHexDb_error:
4367  if( in!=p->in ){
4368    fclose(in);
4369  }else{
4370    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4371      nLine++;
4372      if(strncmp(zLine, "| end ", 6)==0 ) break;
4373    }
4374    p->lineno = nLine;
4375  }
4376  sqlite3_free(a);
4377  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4378  return 0;
4379}
4380#endif /* SQLITE_ENABLE_DESERIALIZE */
4381
4382/*
4383** Scalar function "shell_int32". The first argument to this function
4384** must be a blob. The second a non-negative integer. This function
4385** reads and returns a 32-bit big-endian integer from byte
4386** offset (4*<arg2>) of the blob.
4387*/
4388static void shellInt32(
4389  sqlite3_context *context,
4390  int argc,
4391  sqlite3_value **argv
4392){
4393  const unsigned char *pBlob;
4394  int nBlob;
4395  int iInt;
4396
4397  UNUSED_PARAMETER(argc);
4398  nBlob = sqlite3_value_bytes(argv[0]);
4399  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4400  iInt = sqlite3_value_int(argv[1]);
4401
4402  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4403    const unsigned char *a = &pBlob[iInt*4];
4404    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4405                       + ((sqlite3_int64)a[1]<<16)
4406                       + ((sqlite3_int64)a[2]<< 8)
4407                       + ((sqlite3_int64)a[3]<< 0);
4408    sqlite3_result_int64(context, iVal);
4409  }
4410}
4411
4412/*
4413** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4414** using "..." with internal double-quote characters doubled.
4415*/
4416static void shellIdQuote(
4417  sqlite3_context *context,
4418  int argc,
4419  sqlite3_value **argv
4420){
4421  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4422  UNUSED_PARAMETER(argc);
4423  if( zName ){
4424    char *z = sqlite3_mprintf("\"%w\"", zName);
4425    sqlite3_result_text(context, z, -1, sqlite3_free);
4426  }
4427}
4428
4429/*
4430** Scalar function "shell_escape_crnl" used by the .recover command.
4431** The argument passed to this function is the output of built-in
4432** function quote(). If the first character of the input is "'",
4433** indicating that the value passed to quote() was a text value,
4434** then this function searches the input for "\n" and "\r" characters
4435** and adds a wrapper similar to the following:
4436**
4437**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4438**
4439** Or, if the first character of the input is not "'", then a copy
4440** of the input is returned.
4441*/
4442static void shellEscapeCrnl(
4443  sqlite3_context *context,
4444  int argc,
4445  sqlite3_value **argv
4446){
4447  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4448  UNUSED_PARAMETER(argc);
4449  if( zText[0]=='\'' ){
4450    int nText = sqlite3_value_bytes(argv[0]);
4451    int i;
4452    char zBuf1[20];
4453    char zBuf2[20];
4454    const char *zNL = 0;
4455    const char *zCR = 0;
4456    int nCR = 0;
4457    int nNL = 0;
4458
4459    for(i=0; zText[i]; i++){
4460      if( zNL==0 && zText[i]=='\n' ){
4461        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4462        nNL = (int)strlen(zNL);
4463      }
4464      if( zCR==0 && zText[i]=='\r' ){
4465        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4466        nCR = (int)strlen(zCR);
4467      }
4468    }
4469
4470    if( zNL || zCR ){
4471      int iOut = 0;
4472      i64 nMax = (nNL > nCR) ? nNL : nCR;
4473      i64 nAlloc = nMax * nText + (nMax+64)*2;
4474      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4475      if( zOut==0 ){
4476        sqlite3_result_error_nomem(context);
4477        return;
4478      }
4479
4480      if( zNL && zCR ){
4481        memcpy(&zOut[iOut], "replace(replace(", 16);
4482        iOut += 16;
4483      }else{
4484        memcpy(&zOut[iOut], "replace(", 8);
4485        iOut += 8;
4486      }
4487      for(i=0; zText[i]; i++){
4488        if( zText[i]=='\n' ){
4489          memcpy(&zOut[iOut], zNL, nNL);
4490          iOut += nNL;
4491        }else if( zText[i]=='\r' ){
4492          memcpy(&zOut[iOut], zCR, nCR);
4493          iOut += nCR;
4494        }else{
4495          zOut[iOut] = zText[i];
4496          iOut++;
4497        }
4498      }
4499
4500      if( zNL ){
4501        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4502        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4503        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4504      }
4505      if( zCR ){
4506        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4507        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4508        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4509      }
4510
4511      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4512      sqlite3_free(zOut);
4513      return;
4514    }
4515  }
4516
4517  sqlite3_result_value(context, argv[0]);
4518}
4519
4520/* Flags for open_db().
4521**
4522** The default behavior of open_db() is to exit(1) if the database fails to
4523** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4524** but still returns without calling exit.
4525**
4526** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4527** ZIP archive if the file does not exist or is empty and its name matches
4528** the *.zip pattern.
4529*/
4530#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4531#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4532
4533/*
4534** Make sure the database is open.  If it is not, then open it.  If
4535** the database fails to open, print an error message and exit.
4536*/
4537static void open_db(ShellState *p, int openFlags){
4538  if( p->db==0 ){
4539    if( p->openMode==SHELL_OPEN_UNSPEC ){
4540      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4541        p->openMode = SHELL_OPEN_NORMAL;
4542      }else{
4543        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4544                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4545      }
4546    }
4547    switch( p->openMode ){
4548      case SHELL_OPEN_APPENDVFS: {
4549        sqlite3_open_v2(p->zDbFilename, &p->db,
4550           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4551        break;
4552      }
4553      case SHELL_OPEN_HEXDB:
4554      case SHELL_OPEN_DESERIALIZE: {
4555        sqlite3_open(0, &p->db);
4556        break;
4557      }
4558      case SHELL_OPEN_ZIPFILE: {
4559        sqlite3_open(":memory:", &p->db);
4560        break;
4561      }
4562      case SHELL_OPEN_READONLY: {
4563        sqlite3_open_v2(p->zDbFilename, &p->db,
4564            SQLITE_OPEN_READONLY|p->openFlags, 0);
4565        break;
4566      }
4567      case SHELL_OPEN_UNSPEC:
4568      case SHELL_OPEN_NORMAL: {
4569        sqlite3_open_v2(p->zDbFilename, &p->db,
4570           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4571        break;
4572      }
4573    }
4574    globalDb = p->db;
4575    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4576      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4577          p->zDbFilename, sqlite3_errmsg(p->db));
4578      if( openFlags & OPEN_DB_KEEPALIVE ){
4579        sqlite3_open(":memory:", &p->db);
4580        return;
4581      }
4582      exit(1);
4583    }
4584#ifndef SQLITE_OMIT_LOAD_EXTENSION
4585    sqlite3_enable_load_extension(p->db, 1);
4586#endif
4587    sqlite3_fileio_init(p->db, 0, 0);
4588    sqlite3_shathree_init(p->db, 0, 0);
4589    sqlite3_completion_init(p->db, 0, 0);
4590    sqlite3_uint_init(p->db, 0, 0);
4591    sqlite3_decimal_init(p->db, 0, 0);
4592    sqlite3_ieee_init(p->db, 0, 0);
4593#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4594    sqlite3_dbdata_init(p->db, 0, 0);
4595#endif
4596#ifdef SQLITE_HAVE_ZLIB
4597    sqlite3_zipfile_init(p->db, 0, 0);
4598    sqlite3_sqlar_init(p->db, 0, 0);
4599#endif
4600    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4601                            shellAddSchemaName, 0, 0);
4602    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4603                            shellModuleSchema, 0, 0);
4604    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4605                            shellPutsFunc, 0, 0);
4606    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4607                            shellEscapeCrnl, 0, 0);
4608    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4609                            shellInt32, 0, 0);
4610    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4611                            shellIdQuote, 0, 0);
4612#ifndef SQLITE_NOHAVE_SYSTEM
4613    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4614                            editFunc, 0, 0);
4615    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4616                            editFunc, 0, 0);
4617#endif
4618    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4619      char *zSql = sqlite3_mprintf(
4620         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4621      sqlite3_exec(p->db, zSql, 0, 0, 0);
4622      sqlite3_free(zSql);
4623    }
4624#ifdef SQLITE_ENABLE_DESERIALIZE
4625    else
4626    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4627      int rc;
4628      int nData = 0;
4629      unsigned char *aData;
4630      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4631        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4632      }else{
4633        aData = readHexDb(p, &nData);
4634        if( aData==0 ){
4635          return;
4636        }
4637      }
4638      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4639                   SQLITE_DESERIALIZE_RESIZEABLE |
4640                   SQLITE_DESERIALIZE_FREEONCLOSE);
4641      if( rc ){
4642        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4643      }
4644      if( p->szMax>0 ){
4645        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4646      }
4647    }
4648#endif
4649  }
4650}
4651
4652/*
4653** Attempt to close the databaes connection.  Report errors.
4654*/
4655void close_db(sqlite3 *db){
4656  int rc = sqlite3_close(db);
4657  if( rc ){
4658    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4659        rc, sqlite3_errmsg(db));
4660  }
4661}
4662
4663#if HAVE_READLINE || HAVE_EDITLINE
4664/*
4665** Readline completion callbacks
4666*/
4667static char *readline_completion_generator(const char *text, int state){
4668  static sqlite3_stmt *pStmt = 0;
4669  char *zRet;
4670  if( state==0 ){
4671    char *zSql;
4672    sqlite3_finalize(pStmt);
4673    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4674                           "  FROM completion(%Q) ORDER BY 1", text);
4675    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4676    sqlite3_free(zSql);
4677  }
4678  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4679    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4680  }else{
4681    sqlite3_finalize(pStmt);
4682    pStmt = 0;
4683    zRet = 0;
4684  }
4685  return zRet;
4686}
4687static char **readline_completion(const char *zText, int iStart, int iEnd){
4688  rl_attempted_completion_over = 1;
4689  return rl_completion_matches(zText, readline_completion_generator);
4690}
4691
4692#elif HAVE_LINENOISE
4693/*
4694** Linenoise completion callback
4695*/
4696static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4697  int nLine = strlen30(zLine);
4698  int i, iStart;
4699  sqlite3_stmt *pStmt = 0;
4700  char *zSql;
4701  char zBuf[1000];
4702
4703  if( nLine>sizeof(zBuf)-30 ) return;
4704  if( zLine[0]=='.' || zLine[0]=='#') return;
4705  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4706  if( i==nLine-1 ) return;
4707  iStart = i+1;
4708  memcpy(zBuf, zLine, iStart);
4709  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4710                         "  FROM completion(%Q,%Q) ORDER BY 1",
4711                         &zLine[iStart], zLine);
4712  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4713  sqlite3_free(zSql);
4714  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4715  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4716    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4717    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4718    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4719      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4720      linenoiseAddCompletion(lc, zBuf);
4721    }
4722  }
4723  sqlite3_finalize(pStmt);
4724}
4725#endif
4726
4727/*
4728** Do C-language style dequoting.
4729**
4730**    \a    -> alarm
4731**    \b    -> backspace
4732**    \t    -> tab
4733**    \n    -> newline
4734**    \v    -> vertical tab
4735**    \f    -> form feed
4736**    \r    -> carriage return
4737**    \s    -> space
4738**    \"    -> "
4739**    \'    -> '
4740**    \\    -> backslash
4741**    \NNN  -> ascii character NNN in octal
4742*/
4743static void resolve_backslashes(char *z){
4744  int i, j;
4745  char c;
4746  while( *z && *z!='\\' ) z++;
4747  for(i=j=0; (c = z[i])!=0; i++, j++){
4748    if( c=='\\' && z[i+1]!=0 ){
4749      c = z[++i];
4750      if( c=='a' ){
4751        c = '\a';
4752      }else if( c=='b' ){
4753        c = '\b';
4754      }else if( c=='t' ){
4755        c = '\t';
4756      }else if( c=='n' ){
4757        c = '\n';
4758      }else if( c=='v' ){
4759        c = '\v';
4760      }else if( c=='f' ){
4761        c = '\f';
4762      }else if( c=='r' ){
4763        c = '\r';
4764      }else if( c=='"' ){
4765        c = '"';
4766      }else if( c=='\'' ){
4767        c = '\'';
4768      }else if( c=='\\' ){
4769        c = '\\';
4770      }else if( c>='0' && c<='7' ){
4771        c -= '0';
4772        if( z[i+1]>='0' && z[i+1]<='7' ){
4773          i++;
4774          c = (c<<3) + z[i] - '0';
4775          if( z[i+1]>='0' && z[i+1]<='7' ){
4776            i++;
4777            c = (c<<3) + z[i] - '0';
4778          }
4779        }
4780      }
4781    }
4782    z[j] = c;
4783  }
4784  if( j<i ) z[j] = 0;
4785}
4786
4787/*
4788** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4789** for TRUE and FALSE.  Return the integer value if appropriate.
4790*/
4791static int booleanValue(const char *zArg){
4792  int i;
4793  if( zArg[0]=='0' && zArg[1]=='x' ){
4794    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4795  }else{
4796    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4797  }
4798  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4799  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4800    return 1;
4801  }
4802  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4803    return 0;
4804  }
4805  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4806          zArg);
4807  return 0;
4808}
4809
4810/*
4811** Set or clear a shell flag according to a boolean value.
4812*/
4813static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4814  if( booleanValue(zArg) ){
4815    ShellSetFlag(p, mFlag);
4816  }else{
4817    ShellClearFlag(p, mFlag);
4818  }
4819}
4820
4821/*
4822** Close an output file, assuming it is not stderr or stdout
4823*/
4824static void output_file_close(FILE *f){
4825  if( f && f!=stdout && f!=stderr ) fclose(f);
4826}
4827
4828/*
4829** Try to open an output file.   The names "stdout" and "stderr" are
4830** recognized and do the right thing.  NULL is returned if the output
4831** filename is "off".
4832*/
4833static FILE *output_file_open(const char *zFile, int bTextMode){
4834  FILE *f;
4835  if( strcmp(zFile,"stdout")==0 ){
4836    f = stdout;
4837  }else if( strcmp(zFile, "stderr")==0 ){
4838    f = stderr;
4839  }else if( strcmp(zFile, "off")==0 ){
4840    f = 0;
4841  }else{
4842    f = fopen(zFile, bTextMode ? "w" : "wb");
4843    if( f==0 ){
4844      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4845    }
4846  }
4847  return f;
4848}
4849
4850#ifndef SQLITE_OMIT_TRACE
4851/*
4852** A routine for handling output from sqlite3_trace().
4853*/
4854static int sql_trace_callback(
4855  unsigned mType,         /* The trace type */
4856  void *pArg,             /* The ShellState pointer */
4857  void *pP,               /* Usually a pointer to sqlite_stmt */
4858  void *pX                /* Auxiliary output */
4859){
4860  ShellState *p = (ShellState*)pArg;
4861  sqlite3_stmt *pStmt;
4862  const char *zSql;
4863  int nSql;
4864  if( p->traceOut==0 ) return 0;
4865  if( mType==SQLITE_TRACE_CLOSE ){
4866    utf8_printf(p->traceOut, "-- closing database connection\n");
4867    return 0;
4868  }
4869  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4870    zSql = (const char*)pX;
4871  }else{
4872    pStmt = (sqlite3_stmt*)pP;
4873    switch( p->eTraceType ){
4874      case SHELL_TRACE_EXPANDED: {
4875        zSql = sqlite3_expanded_sql(pStmt);
4876        break;
4877      }
4878#ifdef SQLITE_ENABLE_NORMALIZE
4879      case SHELL_TRACE_NORMALIZED: {
4880        zSql = sqlite3_normalized_sql(pStmt);
4881        break;
4882      }
4883#endif
4884      default: {
4885        zSql = sqlite3_sql(pStmt);
4886        break;
4887      }
4888    }
4889  }
4890  if( zSql==0 ) return 0;
4891  nSql = strlen30(zSql);
4892  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4893  switch( mType ){
4894    case SQLITE_TRACE_ROW:
4895    case SQLITE_TRACE_STMT: {
4896      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4897      break;
4898    }
4899    case SQLITE_TRACE_PROFILE: {
4900      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4901      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4902      break;
4903    }
4904  }
4905  return 0;
4906}
4907#endif
4908
4909/*
4910** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4911** a useful spot to set a debugger breakpoint.
4912*/
4913static void test_breakpoint(void){
4914  static int nCall = 0;
4915  nCall++;
4916}
4917
4918/*
4919** An object used to read a CSV and other files for import.
4920*/
4921typedef struct ImportCtx ImportCtx;
4922struct ImportCtx {
4923  const char *zFile;  /* Name of the input file */
4924  FILE *in;           /* Read the CSV text from this input stream */
4925  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
4926  char *z;            /* Accumulated text for a field */
4927  int n;              /* Number of bytes in z */
4928  int nAlloc;         /* Space allocated for z[] */
4929  int nLine;          /* Current line number */
4930  int nRow;           /* Number of rows imported */
4931  int nErr;           /* Number of errors encountered */
4932  int bNotFirst;      /* True if one or more bytes already read */
4933  int cTerm;          /* Character that terminated the most recent field */
4934  int cColSep;        /* The column separator character.  (Usually ",") */
4935  int cRowSep;        /* The row separator character.  (Usually "\n") */
4936};
4937
4938/* Clean up resourced used by an ImportCtx */
4939static void import_cleanup(ImportCtx *p){
4940  if( p->in!=0 && p->xCloser!=0 ){
4941    p->xCloser(p->in);
4942    p->in = 0;
4943  }
4944  sqlite3_free(p->z);
4945  p->z = 0;
4946}
4947
4948/* Append a single byte to z[] */
4949static void import_append_char(ImportCtx *p, int c){
4950  if( p->n+1>=p->nAlloc ){
4951    p->nAlloc += p->nAlloc + 100;
4952    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4953    if( p->z==0 ) shell_out_of_memory();
4954  }
4955  p->z[p->n++] = (char)c;
4956}
4957
4958/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4959** with the option of having a separator other than ",".
4960**
4961**   +  Input comes from p->in.
4962**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4963**      from sqlite3_malloc64().
4964**   +  Use p->cSep as the column separator.  The default is ",".
4965**   +  Use p->rSep as the row separator.  The default is "\n".
4966**   +  Keep track of the line number in p->nLine.
4967**   +  Store the character that terminates the field in p->cTerm.  Store
4968**      EOF on end-of-file.
4969**   +  Report syntax errors on stderr
4970*/
4971static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4972  int c;
4973  int cSep = p->cColSep;
4974  int rSep = p->cRowSep;
4975  p->n = 0;
4976  c = fgetc(p->in);
4977  if( c==EOF || seenInterrupt ){
4978    p->cTerm = EOF;
4979    return 0;
4980  }
4981  if( c=='"' ){
4982    int pc, ppc;
4983    int startLine = p->nLine;
4984    int cQuote = c;
4985    pc = ppc = 0;
4986    while( 1 ){
4987      c = fgetc(p->in);
4988      if( c==rSep ) p->nLine++;
4989      if( c==cQuote ){
4990        if( pc==cQuote ){
4991          pc = 0;
4992          continue;
4993        }
4994      }
4995      if( (c==cSep && pc==cQuote)
4996       || (c==rSep && pc==cQuote)
4997       || (c==rSep && pc=='\r' && ppc==cQuote)
4998       || (c==EOF && pc==cQuote)
4999      ){
5000        do{ p->n--; }while( p->z[p->n]!=cQuote );
5001        p->cTerm = c;
5002        break;
5003      }
5004      if( pc==cQuote && c!='\r' ){
5005        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5006                p->zFile, p->nLine, cQuote);
5007      }
5008      if( c==EOF ){
5009        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5010                p->zFile, startLine, cQuote);
5011        p->cTerm = c;
5012        break;
5013      }
5014      import_append_char(p, c);
5015      ppc = pc;
5016      pc = c;
5017    }
5018  }else{
5019    /* If this is the first field being parsed and it begins with the
5020    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5021    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5022      import_append_char(p, c);
5023      c = fgetc(p->in);
5024      if( (c&0xff)==0xbb ){
5025        import_append_char(p, c);
5026        c = fgetc(p->in);
5027        if( (c&0xff)==0xbf ){
5028          p->bNotFirst = 1;
5029          p->n = 0;
5030          return csv_read_one_field(p);
5031        }
5032      }
5033    }
5034    while( c!=EOF && c!=cSep && c!=rSep ){
5035      import_append_char(p, c);
5036      c = fgetc(p->in);
5037    }
5038    if( c==rSep ){
5039      p->nLine++;
5040      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5041    }
5042    p->cTerm = c;
5043  }
5044  if( p->z ) p->z[p->n] = 0;
5045  p->bNotFirst = 1;
5046  return p->z;
5047}
5048
5049/* Read a single field of ASCII delimited text.
5050**
5051**   +  Input comes from p->in.
5052**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5053**      from sqlite3_malloc64().
5054**   +  Use p->cSep as the column separator.  The default is "\x1F".
5055**   +  Use p->rSep as the row separator.  The default is "\x1E".
5056**   +  Keep track of the row number in p->nLine.
5057**   +  Store the character that terminates the field in p->cTerm.  Store
5058**      EOF on end-of-file.
5059**   +  Report syntax errors on stderr
5060*/
5061static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5062  int c;
5063  int cSep = p->cColSep;
5064  int rSep = p->cRowSep;
5065  p->n = 0;
5066  c = fgetc(p->in);
5067  if( c==EOF || seenInterrupt ){
5068    p->cTerm = EOF;
5069    return 0;
5070  }
5071  while( c!=EOF && c!=cSep && c!=rSep ){
5072    import_append_char(p, c);
5073    c = fgetc(p->in);
5074  }
5075  if( c==rSep ){
5076    p->nLine++;
5077  }
5078  p->cTerm = c;
5079  if( p->z ) p->z[p->n] = 0;
5080  return p->z;
5081}
5082
5083/*
5084** Try to transfer data for table zTable.  If an error is seen while
5085** moving forward, try to go backwards.  The backwards movement won't
5086** work for WITHOUT ROWID tables.
5087*/
5088static void tryToCloneData(
5089  ShellState *p,
5090  sqlite3 *newDb,
5091  const char *zTable
5092){
5093  sqlite3_stmt *pQuery = 0;
5094  sqlite3_stmt *pInsert = 0;
5095  char *zQuery = 0;
5096  char *zInsert = 0;
5097  int rc;
5098  int i, j, n;
5099  int nTable = strlen30(zTable);
5100  int k = 0;
5101  int cnt = 0;
5102  const int spinRate = 10000;
5103
5104  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5105  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5106  if( rc ){
5107    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5108            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5109            zQuery);
5110    goto end_data_xfer;
5111  }
5112  n = sqlite3_column_count(pQuery);
5113  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5114  if( zInsert==0 ) shell_out_of_memory();
5115  sqlite3_snprintf(200+nTable,zInsert,
5116                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5117  i = strlen30(zInsert);
5118  for(j=1; j<n; j++){
5119    memcpy(zInsert+i, ",?", 2);
5120    i += 2;
5121  }
5122  memcpy(zInsert+i, ");", 3);
5123  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5124  if( rc ){
5125    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5126            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5127            zQuery);
5128    goto end_data_xfer;
5129  }
5130  for(k=0; k<2; k++){
5131    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5132      for(i=0; i<n; i++){
5133        switch( sqlite3_column_type(pQuery, i) ){
5134          case SQLITE_NULL: {
5135            sqlite3_bind_null(pInsert, i+1);
5136            break;
5137          }
5138          case SQLITE_INTEGER: {
5139            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5140            break;
5141          }
5142          case SQLITE_FLOAT: {
5143            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5144            break;
5145          }
5146          case SQLITE_TEXT: {
5147            sqlite3_bind_text(pInsert, i+1,
5148                             (const char*)sqlite3_column_text(pQuery,i),
5149                             -1, SQLITE_STATIC);
5150            break;
5151          }
5152          case SQLITE_BLOB: {
5153            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5154                                            sqlite3_column_bytes(pQuery,i),
5155                                            SQLITE_STATIC);
5156            break;
5157          }
5158        }
5159      } /* End for */
5160      rc = sqlite3_step(pInsert);
5161      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5162        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5163                        sqlite3_errmsg(newDb));
5164      }
5165      sqlite3_reset(pInsert);
5166      cnt++;
5167      if( (cnt%spinRate)==0 ){
5168        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5169        fflush(stdout);
5170      }
5171    } /* End while */
5172    if( rc==SQLITE_DONE ) break;
5173    sqlite3_finalize(pQuery);
5174    sqlite3_free(zQuery);
5175    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5176                             zTable);
5177    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5178    if( rc ){
5179      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5180      break;
5181    }
5182  } /* End for(k=0...) */
5183
5184end_data_xfer:
5185  sqlite3_finalize(pQuery);
5186  sqlite3_finalize(pInsert);
5187  sqlite3_free(zQuery);
5188  sqlite3_free(zInsert);
5189}
5190
5191
5192/*
5193** Try to transfer all rows of the schema that match zWhere.  For
5194** each row, invoke xForEach() on the object defined by that row.
5195** If an error is encountered while moving forward through the
5196** sqlite_schema table, try again moving backwards.
5197*/
5198static void tryToCloneSchema(
5199  ShellState *p,
5200  sqlite3 *newDb,
5201  const char *zWhere,
5202  void (*xForEach)(ShellState*,sqlite3*,const char*)
5203){
5204  sqlite3_stmt *pQuery = 0;
5205  char *zQuery = 0;
5206  int rc;
5207  const unsigned char *zName;
5208  const unsigned char *zSql;
5209  char *zErrMsg = 0;
5210
5211  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5212                           " WHERE %s", zWhere);
5213  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5214  if( rc ){
5215    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5216                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5217                    zQuery);
5218    goto end_schema_xfer;
5219  }
5220  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5221    zName = sqlite3_column_text(pQuery, 0);
5222    zSql = sqlite3_column_text(pQuery, 1);
5223    printf("%s... ", zName); fflush(stdout);
5224    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5225    if( zErrMsg ){
5226      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5227      sqlite3_free(zErrMsg);
5228      zErrMsg = 0;
5229    }
5230    if( xForEach ){
5231      xForEach(p, newDb, (const char*)zName);
5232    }
5233    printf("done\n");
5234  }
5235  if( rc!=SQLITE_DONE ){
5236    sqlite3_finalize(pQuery);
5237    sqlite3_free(zQuery);
5238    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5239                             " WHERE %s ORDER BY rowid DESC", zWhere);
5240    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5241    if( rc ){
5242      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5243                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5244                      zQuery);
5245      goto end_schema_xfer;
5246    }
5247    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5248      zName = sqlite3_column_text(pQuery, 0);
5249      zSql = sqlite3_column_text(pQuery, 1);
5250      printf("%s... ", zName); fflush(stdout);
5251      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5252      if( zErrMsg ){
5253        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5254        sqlite3_free(zErrMsg);
5255        zErrMsg = 0;
5256      }
5257      if( xForEach ){
5258        xForEach(p, newDb, (const char*)zName);
5259      }
5260      printf("done\n");
5261    }
5262  }
5263end_schema_xfer:
5264  sqlite3_finalize(pQuery);
5265  sqlite3_free(zQuery);
5266}
5267
5268/*
5269** Open a new database file named "zNewDb".  Try to recover as much information
5270** as possible out of the main database (which might be corrupt) and write it
5271** into zNewDb.
5272*/
5273static void tryToClone(ShellState *p, const char *zNewDb){
5274  int rc;
5275  sqlite3 *newDb = 0;
5276  if( access(zNewDb,0)==0 ){
5277    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5278    return;
5279  }
5280  rc = sqlite3_open(zNewDb, &newDb);
5281  if( rc ){
5282    utf8_printf(stderr, "Cannot create output database: %s\n",
5283            sqlite3_errmsg(newDb));
5284  }else{
5285    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5286    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5287    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5288    tryToCloneSchema(p, newDb, "type!='table'", 0);
5289    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5290    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5291  }
5292  close_db(newDb);
5293}
5294
5295/*
5296** Change the output file back to stdout.
5297**
5298** If the p->doXdgOpen flag is set, that means the output was being
5299** redirected to a temporary file named by p->zTempFile.  In that case,
5300** launch start/open/xdg-open on that temporary file.
5301*/
5302static void output_reset(ShellState *p){
5303  if( p->outfile[0]=='|' ){
5304#ifndef SQLITE_OMIT_POPEN
5305    pclose(p->out);
5306#endif
5307  }else{
5308    output_file_close(p->out);
5309#ifndef SQLITE_NOHAVE_SYSTEM
5310    if( p->doXdgOpen ){
5311      const char *zXdgOpenCmd =
5312#if defined(_WIN32)
5313      "start";
5314#elif defined(__APPLE__)
5315      "open";
5316#else
5317      "xdg-open";
5318#endif
5319      char *zCmd;
5320      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5321      if( system(zCmd) ){
5322        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5323      }else{
5324        /* Give the start/open/xdg-open command some time to get
5325        ** going before we continue, and potential delete the
5326        ** p->zTempFile data file out from under it */
5327        sqlite3_sleep(2000);
5328      }
5329      sqlite3_free(zCmd);
5330      outputModePop(p);
5331      p->doXdgOpen = 0;
5332    }
5333#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5334  }
5335  p->outfile[0] = 0;
5336  p->out = stdout;
5337}
5338
5339/*
5340** Run an SQL command and return the single integer result.
5341*/
5342static int db_int(ShellState *p, const char *zSql){
5343  sqlite3_stmt *pStmt;
5344  int res = 0;
5345  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5346  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5347    res = sqlite3_column_int(pStmt,0);
5348  }
5349  sqlite3_finalize(pStmt);
5350  return res;
5351}
5352
5353/*
5354** Convert a 2-byte or 4-byte big-endian integer into a native integer
5355*/
5356static unsigned int get2byteInt(unsigned char *a){
5357  return (a[0]<<8) + a[1];
5358}
5359static unsigned int get4byteInt(unsigned char *a){
5360  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5361}
5362
5363/*
5364** Implementation of the ".dbinfo" command.
5365**
5366** Return 1 on error, 2 to exit, and 0 otherwise.
5367*/
5368static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5369  static const struct { const char *zName; int ofst; } aField[] = {
5370     { "file change counter:",  24  },
5371     { "database page count:",  28  },
5372     { "freelist page count:",  36  },
5373     { "schema cookie:",        40  },
5374     { "schema format:",        44  },
5375     { "default cache size:",   48  },
5376     { "autovacuum top root:",  52  },
5377     { "incremental vacuum:",   64  },
5378     { "text encoding:",        56  },
5379     { "user version:",         60  },
5380     { "application id:",       68  },
5381     { "software version:",     96  },
5382  };
5383  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5384     { "number of tables:",
5385       "SELECT count(*) FROM %s WHERE type='table'" },
5386     { "number of indexes:",
5387       "SELECT count(*) FROM %s WHERE type='index'" },
5388     { "number of triggers:",
5389       "SELECT count(*) FROM %s WHERE type='trigger'" },
5390     { "number of views:",
5391       "SELECT count(*) FROM %s WHERE type='view'" },
5392     { "schema size:",
5393       "SELECT total(length(sql)) FROM %s" },
5394  };
5395  int i, rc;
5396  unsigned iDataVersion;
5397  char *zSchemaTab;
5398  char *zDb = nArg>=2 ? azArg[1] : "main";
5399  sqlite3_stmt *pStmt = 0;
5400  unsigned char aHdr[100];
5401  open_db(p, 0);
5402  if( p->db==0 ) return 1;
5403  rc = sqlite3_prepare_v2(p->db,
5404             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5405             -1, &pStmt, 0);
5406  if( rc ){
5407    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5408    sqlite3_finalize(pStmt);
5409    return 1;
5410  }
5411  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5412  if( sqlite3_step(pStmt)==SQLITE_ROW
5413   && sqlite3_column_bytes(pStmt,0)>100
5414  ){
5415    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5416    sqlite3_finalize(pStmt);
5417  }else{
5418    raw_printf(stderr, "unable to read database header\n");
5419    sqlite3_finalize(pStmt);
5420    return 1;
5421  }
5422  i = get2byteInt(aHdr+16);
5423  if( i==1 ) i = 65536;
5424  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5425  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5426  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5427  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5428  for(i=0; i<ArraySize(aField); i++){
5429    int ofst = aField[i].ofst;
5430    unsigned int val = get4byteInt(aHdr + ofst);
5431    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5432    switch( ofst ){
5433      case 56: {
5434        if( val==1 ) raw_printf(p->out, " (utf8)");
5435        if( val==2 ) raw_printf(p->out, " (utf16le)");
5436        if( val==3 ) raw_printf(p->out, " (utf16be)");
5437      }
5438    }
5439    raw_printf(p->out, "\n");
5440  }
5441  if( zDb==0 ){
5442    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5443  }else if( strcmp(zDb,"temp")==0 ){
5444    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5445  }else{
5446    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5447  }
5448  for(i=0; i<ArraySize(aQuery); i++){
5449    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5450    int val = db_int(p, zSql);
5451    sqlite3_free(zSql);
5452    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5453  }
5454  sqlite3_free(zSchemaTab);
5455  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5456  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5457  return 0;
5458}
5459
5460/*
5461** Print the current sqlite3_errmsg() value to stderr and return 1.
5462*/
5463static int shellDatabaseError(sqlite3 *db){
5464  const char *zErr = sqlite3_errmsg(db);
5465  utf8_printf(stderr, "Error: %s\n", zErr);
5466  return 1;
5467}
5468
5469/*
5470** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5471** if they match and FALSE (0) if they do not match.
5472**
5473** Globbing rules:
5474**
5475**      '*'       Matches any sequence of zero or more characters.
5476**
5477**      '?'       Matches exactly one character.
5478**
5479**     [...]      Matches one character from the enclosed list of
5480**                characters.
5481**
5482**     [^...]     Matches one character not in the enclosed list.
5483**
5484**      '#'       Matches any sequence of one or more digits with an
5485**                optional + or - sign in front
5486**
5487**      ' '       Any span of whitespace matches any other span of
5488**                whitespace.
5489**
5490** Extra whitespace at the end of z[] is ignored.
5491*/
5492static int testcase_glob(const char *zGlob, const char *z){
5493  int c, c2;
5494  int invert;
5495  int seen;
5496
5497  while( (c = (*(zGlob++)))!=0 ){
5498    if( IsSpace(c) ){
5499      if( !IsSpace(*z) ) return 0;
5500      while( IsSpace(*zGlob) ) zGlob++;
5501      while( IsSpace(*z) ) z++;
5502    }else if( c=='*' ){
5503      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5504        if( c=='?' && (*(z++))==0 ) return 0;
5505      }
5506      if( c==0 ){
5507        return 1;
5508      }else if( c=='[' ){
5509        while( *z && testcase_glob(zGlob-1,z)==0 ){
5510          z++;
5511        }
5512        return (*z)!=0;
5513      }
5514      while( (c2 = (*(z++)))!=0 ){
5515        while( c2!=c ){
5516          c2 = *(z++);
5517          if( c2==0 ) return 0;
5518        }
5519        if( testcase_glob(zGlob,z) ) return 1;
5520      }
5521      return 0;
5522    }else if( c=='?' ){
5523      if( (*(z++))==0 ) return 0;
5524    }else if( c=='[' ){
5525      int prior_c = 0;
5526      seen = 0;
5527      invert = 0;
5528      c = *(z++);
5529      if( c==0 ) return 0;
5530      c2 = *(zGlob++);
5531      if( c2=='^' ){
5532        invert = 1;
5533        c2 = *(zGlob++);
5534      }
5535      if( c2==']' ){
5536        if( c==']' ) seen = 1;
5537        c2 = *(zGlob++);
5538      }
5539      while( c2 && c2!=']' ){
5540        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5541          c2 = *(zGlob++);
5542          if( c>=prior_c && c<=c2 ) seen = 1;
5543          prior_c = 0;
5544        }else{
5545          if( c==c2 ){
5546            seen = 1;
5547          }
5548          prior_c = c2;
5549        }
5550        c2 = *(zGlob++);
5551      }
5552      if( c2==0 || (seen ^ invert)==0 ) return 0;
5553    }else if( c=='#' ){
5554      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5555      if( !IsDigit(z[0]) ) return 0;
5556      z++;
5557      while( IsDigit(z[0]) ){ z++; }
5558    }else{
5559      if( c!=(*(z++)) ) return 0;
5560    }
5561  }
5562  while( IsSpace(*z) ){ z++; }
5563  return *z==0;
5564}
5565
5566
5567/*
5568** Compare the string as a command-line option with either one or two
5569** initial "-" characters.
5570*/
5571static int optionMatch(const char *zStr, const char *zOpt){
5572  if( zStr[0]!='-' ) return 0;
5573  zStr++;
5574  if( zStr[0]=='-' ) zStr++;
5575  return strcmp(zStr, zOpt)==0;
5576}
5577
5578/*
5579** Delete a file.
5580*/
5581int shellDeleteFile(const char *zFilename){
5582  int rc;
5583#ifdef _WIN32
5584  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5585  rc = _wunlink(z);
5586  sqlite3_free(z);
5587#else
5588  rc = unlink(zFilename);
5589#endif
5590  return rc;
5591}
5592
5593/*
5594** Try to delete the temporary file (if there is one) and free the
5595** memory used to hold the name of the temp file.
5596*/
5597static void clearTempFile(ShellState *p){
5598  if( p->zTempFile==0 ) return;
5599  if( p->doXdgOpen ) return;
5600  if( shellDeleteFile(p->zTempFile) ) return;
5601  sqlite3_free(p->zTempFile);
5602  p->zTempFile = 0;
5603}
5604
5605/*
5606** Create a new temp file name with the given suffix.
5607*/
5608static void newTempFile(ShellState *p, const char *zSuffix){
5609  clearTempFile(p);
5610  sqlite3_free(p->zTempFile);
5611  p->zTempFile = 0;
5612  if( p->db ){
5613    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5614  }
5615  if( p->zTempFile==0 ){
5616    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5617    ** will not work and we will need to fallback to guessing */
5618    char *zTemp;
5619    sqlite3_uint64 r;
5620    sqlite3_randomness(sizeof(r), &r);
5621    zTemp = getenv("TEMP");
5622    if( zTemp==0 ) zTemp = getenv("TMP");
5623    if( zTemp==0 ){
5624#ifdef _WIN32
5625      zTemp = "\\tmp";
5626#else
5627      zTemp = "/tmp";
5628#endif
5629    }
5630    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5631  }else{
5632    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5633  }
5634  if( p->zTempFile==0 ){
5635    raw_printf(stderr, "out of memory\n");
5636    exit(1);
5637  }
5638}
5639
5640
5641/*
5642** The implementation of SQL scalar function fkey_collate_clause(), used
5643** by the ".lint fkey-indexes" command. This scalar function is always
5644** called with four arguments - the parent table name, the parent column name,
5645** the child table name and the child column name.
5646**
5647**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5648**
5649** If either of the named tables or columns do not exist, this function
5650** returns an empty string. An empty string is also returned if both tables
5651** and columns exist but have the same default collation sequence. Or,
5652** if both exist but the default collation sequences are different, this
5653** function returns the string " COLLATE <parent-collation>", where
5654** <parent-collation> is the default collation sequence of the parent column.
5655*/
5656static void shellFkeyCollateClause(
5657  sqlite3_context *pCtx,
5658  int nVal,
5659  sqlite3_value **apVal
5660){
5661  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5662  const char *zParent;
5663  const char *zParentCol;
5664  const char *zParentSeq;
5665  const char *zChild;
5666  const char *zChildCol;
5667  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5668  int rc;
5669
5670  assert( nVal==4 );
5671  zParent = (const char*)sqlite3_value_text(apVal[0]);
5672  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5673  zChild = (const char*)sqlite3_value_text(apVal[2]);
5674  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5675
5676  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5677  rc = sqlite3_table_column_metadata(
5678      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5679  );
5680  if( rc==SQLITE_OK ){
5681    rc = sqlite3_table_column_metadata(
5682        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5683    );
5684  }
5685
5686  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5687    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5688    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5689    sqlite3_free(z);
5690  }
5691}
5692
5693
5694/*
5695** The implementation of dot-command ".lint fkey-indexes".
5696*/
5697static int lintFkeyIndexes(
5698  ShellState *pState,             /* Current shell tool state */
5699  char **azArg,                   /* Array of arguments passed to dot command */
5700  int nArg                        /* Number of entries in azArg[] */
5701){
5702  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5703  FILE *out = pState->out;        /* Stream to write non-error output to */
5704  int bVerbose = 0;               /* If -verbose is present */
5705  int bGroupByParent = 0;         /* If -groupbyparent is present */
5706  int i;                          /* To iterate through azArg[] */
5707  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5708  int rc;                         /* Return code */
5709  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5710
5711  /*
5712  ** This SELECT statement returns one row for each foreign key constraint
5713  ** in the schema of the main database. The column values are:
5714  **
5715  ** 0. The text of an SQL statement similar to:
5716  **
5717  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5718  **
5719  **    This SELECT is similar to the one that the foreign keys implementation
5720  **    needs to run internally on child tables. If there is an index that can
5721  **    be used to optimize this query, then it can also be used by the FK
5722  **    implementation to optimize DELETE or UPDATE statements on the parent
5723  **    table.
5724  **
5725  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5726  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5727  **    contains an index that can be used to optimize the query.
5728  **
5729  ** 2. Human readable text that describes the child table and columns. e.g.
5730  **
5731  **       "child_table(child_key1, child_key2)"
5732  **
5733  ** 3. Human readable text that describes the parent table and columns. e.g.
5734  **
5735  **       "parent_table(parent_key1, parent_key2)"
5736  **
5737  ** 4. A full CREATE INDEX statement for an index that could be used to
5738  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5739  **
5740  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5741  **
5742  ** 5. The name of the parent table.
5743  **
5744  ** These six values are used by the C logic below to generate the report.
5745  */
5746  const char *zSql =
5747  "SELECT "
5748    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5749    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5750    "  || fkey_collate_clause("
5751    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5752    ", "
5753    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5754    "  || group_concat('*=?', ' AND ') || ')'"
5755    ", "
5756    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5757    ", "
5758    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5759    ", "
5760    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5761    "  || ' ON ' || quote(s.name) || '('"
5762    "  || group_concat(quote(f.[from]) ||"
5763    "        fkey_collate_clause("
5764    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5765    "  || ');'"
5766    ", "
5767    "     f.[table] "
5768    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5769    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5770    "GROUP BY s.name, f.id "
5771    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5772  ;
5773  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5774
5775  for(i=2; i<nArg; i++){
5776    int n = strlen30(azArg[i]);
5777    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5778      bVerbose = 1;
5779    }
5780    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5781      bGroupByParent = 1;
5782      zIndent = "    ";
5783    }
5784    else{
5785      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5786          azArg[0], azArg[1]
5787      );
5788      return SQLITE_ERROR;
5789    }
5790  }
5791
5792  /* Register the fkey_collate_clause() SQL function */
5793  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5794      0, shellFkeyCollateClause, 0, 0
5795  );
5796
5797
5798  if( rc==SQLITE_OK ){
5799    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5800  }
5801  if( rc==SQLITE_OK ){
5802    sqlite3_bind_int(pSql, 1, bGroupByParent);
5803  }
5804
5805  if( rc==SQLITE_OK ){
5806    int rc2;
5807    char *zPrev = 0;
5808    while( SQLITE_ROW==sqlite3_step(pSql) ){
5809      int res = -1;
5810      sqlite3_stmt *pExplain = 0;
5811      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5812      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5813      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5814      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5815      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5816      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5817
5818      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5819      if( rc!=SQLITE_OK ) break;
5820      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5821        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5822        res = (
5823              0==sqlite3_strglob(zGlob, zPlan)
5824           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5825        );
5826      }
5827      rc = sqlite3_finalize(pExplain);
5828      if( rc!=SQLITE_OK ) break;
5829
5830      if( res<0 ){
5831        raw_printf(stderr, "Error: internal error");
5832        break;
5833      }else{
5834        if( bGroupByParent
5835        && (bVerbose || res==0)
5836        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5837        ){
5838          raw_printf(out, "-- Parent table %s\n", zParent);
5839          sqlite3_free(zPrev);
5840          zPrev = sqlite3_mprintf("%s", zParent);
5841        }
5842
5843        if( res==0 ){
5844          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5845        }else if( bVerbose ){
5846          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5847              zIndent, zFrom, zTarget
5848          );
5849        }
5850      }
5851    }
5852    sqlite3_free(zPrev);
5853
5854    if( rc!=SQLITE_OK ){
5855      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5856    }
5857
5858    rc2 = sqlite3_finalize(pSql);
5859    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5860      rc = rc2;
5861      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5862    }
5863  }else{
5864    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5865  }
5866
5867  return rc;
5868}
5869
5870/*
5871** Implementation of ".lint" dot command.
5872*/
5873static int lintDotCommand(
5874  ShellState *pState,             /* Current shell tool state */
5875  char **azArg,                   /* Array of arguments passed to dot command */
5876  int nArg                        /* Number of entries in azArg[] */
5877){
5878  int n;
5879  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5880  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5881  return lintFkeyIndexes(pState, azArg, nArg);
5882
5883 usage:
5884  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5885  raw_printf(stderr, "Where sub-commands are:\n");
5886  raw_printf(stderr, "    fkey-indexes\n");
5887  return SQLITE_ERROR;
5888}
5889
5890#if !defined SQLITE_OMIT_VIRTUALTABLE
5891static void shellPrepare(
5892  sqlite3 *db,
5893  int *pRc,
5894  const char *zSql,
5895  sqlite3_stmt **ppStmt
5896){
5897  *ppStmt = 0;
5898  if( *pRc==SQLITE_OK ){
5899    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5900    if( rc!=SQLITE_OK ){
5901      raw_printf(stderr, "sql error: %s (%d)\n",
5902          sqlite3_errmsg(db), sqlite3_errcode(db)
5903      );
5904      *pRc = rc;
5905    }
5906  }
5907}
5908
5909/*
5910** Create a prepared statement using printf-style arguments for the SQL.
5911**
5912** This routine is could be marked "static".  But it is not always used,
5913** depending on compile-time options.  By omitting the "static", we avoid
5914** nuisance compiler warnings about "defined but not used".
5915*/
5916void shellPreparePrintf(
5917  sqlite3 *db,
5918  int *pRc,
5919  sqlite3_stmt **ppStmt,
5920  const char *zFmt,
5921  ...
5922){
5923  *ppStmt = 0;
5924  if( *pRc==SQLITE_OK ){
5925    va_list ap;
5926    char *z;
5927    va_start(ap, zFmt);
5928    z = sqlite3_vmprintf(zFmt, ap);
5929    va_end(ap);
5930    if( z==0 ){
5931      *pRc = SQLITE_NOMEM;
5932    }else{
5933      shellPrepare(db, pRc, z, ppStmt);
5934      sqlite3_free(z);
5935    }
5936  }
5937}
5938
5939/* Finalize the prepared statement created using shellPreparePrintf().
5940**
5941** This routine is could be marked "static".  But it is not always used,
5942** depending on compile-time options.  By omitting the "static", we avoid
5943** nuisance compiler warnings about "defined but not used".
5944*/
5945void shellFinalize(
5946  int *pRc,
5947  sqlite3_stmt *pStmt
5948){
5949  if( pStmt ){
5950    sqlite3 *db = sqlite3_db_handle(pStmt);
5951    int rc = sqlite3_finalize(pStmt);
5952    if( *pRc==SQLITE_OK ){
5953      if( rc!=SQLITE_OK ){
5954        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5955      }
5956      *pRc = rc;
5957    }
5958  }
5959}
5960
5961/* Reset the prepared statement created using shellPreparePrintf().
5962**
5963** This routine is could be marked "static".  But it is not always used,
5964** depending on compile-time options.  By omitting the "static", we avoid
5965** nuisance compiler warnings about "defined but not used".
5966*/
5967void shellReset(
5968  int *pRc,
5969  sqlite3_stmt *pStmt
5970){
5971  int rc = sqlite3_reset(pStmt);
5972  if( *pRc==SQLITE_OK ){
5973    if( rc!=SQLITE_OK ){
5974      sqlite3 *db = sqlite3_db_handle(pStmt);
5975      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5976    }
5977    *pRc = rc;
5978  }
5979}
5980#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
5981
5982#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5983/******************************************************************************
5984** The ".archive" or ".ar" command.
5985*/
5986/*
5987** Structure representing a single ".ar" command.
5988*/
5989typedef struct ArCommand ArCommand;
5990struct ArCommand {
5991  u8 eCmd;                        /* An AR_CMD_* value */
5992  u8 bVerbose;                    /* True if --verbose */
5993  u8 bZip;                        /* True if the archive is a ZIP */
5994  u8 bDryRun;                     /* True if --dry-run */
5995  u8 bAppend;                     /* True if --append */
5996  u8 fromCmdLine;                 /* Run from -A instead of .archive */
5997  int nArg;                       /* Number of command arguments */
5998  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
5999  const char *zFile;              /* --file argument, or NULL */
6000  const char *zDir;               /* --directory argument, or NULL */
6001  char **azArg;                   /* Array of command arguments */
6002  ShellState *p;                  /* Shell state */
6003  sqlite3 *db;                    /* Database containing the archive */
6004};
6005
6006/*
6007** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6008*/
6009static int arUsage(FILE *f){
6010  showHelp(f,"archive");
6011  return SQLITE_ERROR;
6012}
6013
6014/*
6015** Print an error message for the .ar command to stderr and return
6016** SQLITE_ERROR.
6017*/
6018static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6019  va_list ap;
6020  char *z;
6021  va_start(ap, zFmt);
6022  z = sqlite3_vmprintf(zFmt, ap);
6023  va_end(ap);
6024  utf8_printf(stderr, "Error: %s\n", z);
6025  if( pAr->fromCmdLine ){
6026    utf8_printf(stderr, "Use \"-A\" for more help\n");
6027  }else{
6028    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6029  }
6030  sqlite3_free(z);
6031  return SQLITE_ERROR;
6032}
6033
6034/*
6035** Values for ArCommand.eCmd.
6036*/
6037#define AR_CMD_CREATE       1
6038#define AR_CMD_UPDATE       2
6039#define AR_CMD_INSERT       3
6040#define AR_CMD_EXTRACT      4
6041#define AR_CMD_LIST         5
6042#define AR_CMD_HELP         6
6043
6044/*
6045** Other (non-command) switches.
6046*/
6047#define AR_SWITCH_VERBOSE     7
6048#define AR_SWITCH_FILE        8
6049#define AR_SWITCH_DIRECTORY   9
6050#define AR_SWITCH_APPEND     10
6051#define AR_SWITCH_DRYRUN     11
6052
6053static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6054  switch( eSwitch ){
6055    case AR_CMD_CREATE:
6056    case AR_CMD_EXTRACT:
6057    case AR_CMD_LIST:
6058    case AR_CMD_UPDATE:
6059    case AR_CMD_INSERT:
6060    case AR_CMD_HELP:
6061      if( pAr->eCmd ){
6062        return arErrorMsg(pAr, "multiple command options");
6063      }
6064      pAr->eCmd = eSwitch;
6065      break;
6066
6067    case AR_SWITCH_DRYRUN:
6068      pAr->bDryRun = 1;
6069      break;
6070    case AR_SWITCH_VERBOSE:
6071      pAr->bVerbose = 1;
6072      break;
6073    case AR_SWITCH_APPEND:
6074      pAr->bAppend = 1;
6075      /* Fall thru into --file */
6076    case AR_SWITCH_FILE:
6077      pAr->zFile = zArg;
6078      break;
6079    case AR_SWITCH_DIRECTORY:
6080      pAr->zDir = zArg;
6081      break;
6082  }
6083
6084  return SQLITE_OK;
6085}
6086
6087/*
6088** Parse the command line for an ".ar" command. The results are written into
6089** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6090** successfully, otherwise an error message is written to stderr and
6091** SQLITE_ERROR returned.
6092*/
6093static int arParseCommand(
6094  char **azArg,                   /* Array of arguments passed to dot command */
6095  int nArg,                       /* Number of entries in azArg[] */
6096  ArCommand *pAr                  /* Populate this object */
6097){
6098  struct ArSwitch {
6099    const char *zLong;
6100    char cShort;
6101    u8 eSwitch;
6102    u8 bArg;
6103  } aSwitch[] = {
6104    { "create",    'c', AR_CMD_CREATE,       0 },
6105    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6106    { "insert",    'i', AR_CMD_INSERT,       0 },
6107    { "list",      't', AR_CMD_LIST,         0 },
6108    { "update",    'u', AR_CMD_UPDATE,       0 },
6109    { "help",      'h', AR_CMD_HELP,         0 },
6110    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6111    { "file",      'f', AR_SWITCH_FILE,      1 },
6112    { "append",    'a', AR_SWITCH_APPEND,    1 },
6113    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6114    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6115  };
6116  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6117  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6118
6119  if( nArg<=1 ){
6120    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6121    return arUsage(stderr);
6122  }else{
6123    char *z = azArg[1];
6124    if( z[0]!='-' ){
6125      /* Traditional style [tar] invocation */
6126      int i;
6127      int iArg = 2;
6128      for(i=0; z[i]; i++){
6129        const char *zArg = 0;
6130        struct ArSwitch *pOpt;
6131        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6132          if( z[i]==pOpt->cShort ) break;
6133        }
6134        if( pOpt==pEnd ){
6135          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6136        }
6137        if( pOpt->bArg ){
6138          if( iArg>=nArg ){
6139            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6140          }
6141          zArg = azArg[iArg++];
6142        }
6143        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6144      }
6145      pAr->nArg = nArg-iArg;
6146      if( pAr->nArg>0 ){
6147        pAr->azArg = &azArg[iArg];
6148      }
6149    }else{
6150      /* Non-traditional invocation */
6151      int iArg;
6152      for(iArg=1; iArg<nArg; iArg++){
6153        int n;
6154        z = azArg[iArg];
6155        if( z[0]!='-' ){
6156          /* All remaining command line words are command arguments. */
6157          pAr->azArg = &azArg[iArg];
6158          pAr->nArg = nArg-iArg;
6159          break;
6160        }
6161        n = strlen30(z);
6162
6163        if( z[1]!='-' ){
6164          int i;
6165          /* One or more short options */
6166          for(i=1; i<n; i++){
6167            const char *zArg = 0;
6168            struct ArSwitch *pOpt;
6169            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6170              if( z[i]==pOpt->cShort ) break;
6171            }
6172            if( pOpt==pEnd ){
6173              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6174            }
6175            if( pOpt->bArg ){
6176              if( i<(n-1) ){
6177                zArg = &z[i+1];
6178                i = n;
6179              }else{
6180                if( iArg>=(nArg-1) ){
6181                  return arErrorMsg(pAr, "option requires an argument: %c",
6182                                    z[i]);
6183                }
6184                zArg = azArg[++iArg];
6185              }
6186            }
6187            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6188          }
6189        }else if( z[2]=='\0' ){
6190          /* A -- option, indicating that all remaining command line words
6191          ** are command arguments.  */
6192          pAr->azArg = &azArg[iArg+1];
6193          pAr->nArg = nArg-iArg-1;
6194          break;
6195        }else{
6196          /* A long option */
6197          const char *zArg = 0;             /* Argument for option, if any */
6198          struct ArSwitch *pMatch = 0;      /* Matching option */
6199          struct ArSwitch *pOpt;            /* Iterator */
6200          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6201            const char *zLong = pOpt->zLong;
6202            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6203              if( pMatch ){
6204                return arErrorMsg(pAr, "ambiguous option: %s",z);
6205              }else{
6206                pMatch = pOpt;
6207              }
6208            }
6209          }
6210
6211          if( pMatch==0 ){
6212            return arErrorMsg(pAr, "unrecognized option: %s", z);
6213          }
6214          if( pMatch->bArg ){
6215            if( iArg>=(nArg-1) ){
6216              return arErrorMsg(pAr, "option requires an argument: %s", z);
6217            }
6218            zArg = azArg[++iArg];
6219          }
6220          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6221        }
6222      }
6223    }
6224  }
6225
6226  return SQLITE_OK;
6227}
6228
6229/*
6230** This function assumes that all arguments within the ArCommand.azArg[]
6231** array refer to archive members, as for the --extract or --list commands.
6232** It checks that each of them are present. If any specified file is not
6233** present in the archive, an error is printed to stderr and an error
6234** code returned. Otherwise, if all specified arguments are present in
6235** the archive, SQLITE_OK is returned.
6236**
6237** This function strips any trailing '/' characters from each argument.
6238** This is consistent with the way the [tar] command seems to work on
6239** Linux.
6240*/
6241static int arCheckEntries(ArCommand *pAr){
6242  int rc = SQLITE_OK;
6243  if( pAr->nArg ){
6244    int i, j;
6245    sqlite3_stmt *pTest = 0;
6246
6247    shellPreparePrintf(pAr->db, &rc, &pTest,
6248        "SELECT name FROM %s WHERE name=$name",
6249        pAr->zSrcTable
6250    );
6251    j = sqlite3_bind_parameter_index(pTest, "$name");
6252    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6253      char *z = pAr->azArg[i];
6254      int n = strlen30(z);
6255      int bOk = 0;
6256      while( n>0 && z[n-1]=='/' ) n--;
6257      z[n] = '\0';
6258      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6259      if( SQLITE_ROW==sqlite3_step(pTest) ){
6260        bOk = 1;
6261      }
6262      shellReset(&rc, pTest);
6263      if( rc==SQLITE_OK && bOk==0 ){
6264        utf8_printf(stderr, "not found in archive: %s\n", z);
6265        rc = SQLITE_ERROR;
6266      }
6267    }
6268    shellFinalize(&rc, pTest);
6269  }
6270  return rc;
6271}
6272
6273/*
6274** Format a WHERE clause that can be used against the "sqlar" table to
6275** identify all archive members that match the command arguments held
6276** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6277** The caller is responsible for eventually calling sqlite3_free() on
6278** any non-NULL (*pzWhere) value.
6279*/
6280static void arWhereClause(
6281  int *pRc,
6282  ArCommand *pAr,
6283  char **pzWhere                  /* OUT: New WHERE clause */
6284){
6285  char *zWhere = 0;
6286  if( *pRc==SQLITE_OK ){
6287    if( pAr->nArg==0 ){
6288      zWhere = sqlite3_mprintf("1");
6289    }else{
6290      int i;
6291      const char *zSep = "";
6292      for(i=0; i<pAr->nArg; i++){
6293        const char *z = pAr->azArg[i];
6294        zWhere = sqlite3_mprintf(
6295          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6296          zWhere, zSep, z, strlen30(z)+1, z
6297        );
6298        if( zWhere==0 ){
6299          *pRc = SQLITE_NOMEM;
6300          break;
6301        }
6302        zSep = " OR ";
6303      }
6304    }
6305  }
6306  *pzWhere = zWhere;
6307}
6308
6309/*
6310** Implementation of .ar "lisT" command.
6311*/
6312static int arListCommand(ArCommand *pAr){
6313  const char *zSql = "SELECT %s FROM %s WHERE %s";
6314  const char *azCols[] = {
6315    "name",
6316    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6317  };
6318
6319  char *zWhere = 0;
6320  sqlite3_stmt *pSql = 0;
6321  int rc;
6322
6323  rc = arCheckEntries(pAr);
6324  arWhereClause(&rc, pAr, &zWhere);
6325
6326  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6327                     pAr->zSrcTable, zWhere);
6328  if( pAr->bDryRun ){
6329    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6330  }else{
6331    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6332      if( pAr->bVerbose ){
6333        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6334            sqlite3_column_text(pSql, 0),
6335            sqlite3_column_int(pSql, 1),
6336            sqlite3_column_text(pSql, 2),
6337            sqlite3_column_text(pSql, 3)
6338        );
6339      }else{
6340        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6341      }
6342    }
6343  }
6344  shellFinalize(&rc, pSql);
6345  sqlite3_free(zWhere);
6346  return rc;
6347}
6348
6349
6350/*
6351** Implementation of .ar "eXtract" command.
6352*/
6353static int arExtractCommand(ArCommand *pAr){
6354  const char *zSql1 =
6355    "SELECT "
6356    " ($dir || name),"
6357    " writefile(($dir || name), %s, mode, mtime) "
6358    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6359    " AND name NOT GLOB '*..[/\\]*'";
6360
6361  const char *azExtraArg[] = {
6362    "sqlar_uncompress(data, sz)",
6363    "data"
6364  };
6365
6366  sqlite3_stmt *pSql = 0;
6367  int rc = SQLITE_OK;
6368  char *zDir = 0;
6369  char *zWhere = 0;
6370  int i, j;
6371
6372  /* If arguments are specified, check that they actually exist within
6373  ** the archive before proceeding. And formulate a WHERE clause to
6374  ** match them.  */
6375  rc = arCheckEntries(pAr);
6376  arWhereClause(&rc, pAr, &zWhere);
6377
6378  if( rc==SQLITE_OK ){
6379    if( pAr->zDir ){
6380      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6381    }else{
6382      zDir = sqlite3_mprintf("");
6383    }
6384    if( zDir==0 ) rc = SQLITE_NOMEM;
6385  }
6386
6387  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6388      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6389  );
6390
6391  if( rc==SQLITE_OK ){
6392    j = sqlite3_bind_parameter_index(pSql, "$dir");
6393    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6394
6395    /* Run the SELECT statement twice. The first time, writefile() is called
6396    ** for all archive members that should be extracted. The second time,
6397    ** only for the directories. This is because the timestamps for
6398    ** extracted directories must be reset after they are populated (as
6399    ** populating them changes the timestamp).  */
6400    for(i=0; i<2; i++){
6401      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6402      sqlite3_bind_int(pSql, j, i);
6403      if( pAr->bDryRun ){
6404        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6405      }else{
6406        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6407          if( i==0 && pAr->bVerbose ){
6408            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6409          }
6410        }
6411      }
6412      shellReset(&rc, pSql);
6413    }
6414    shellFinalize(&rc, pSql);
6415  }
6416
6417  sqlite3_free(zDir);
6418  sqlite3_free(zWhere);
6419  return rc;
6420}
6421
6422/*
6423** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6424*/
6425static int arExecSql(ArCommand *pAr, const char *zSql){
6426  int rc;
6427  if( pAr->bDryRun ){
6428    utf8_printf(pAr->p->out, "%s\n", zSql);
6429    rc = SQLITE_OK;
6430  }else{
6431    char *zErr = 0;
6432    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6433    if( zErr ){
6434      utf8_printf(stdout, "ERROR: %s\n", zErr);
6435      sqlite3_free(zErr);
6436    }
6437  }
6438  return rc;
6439}
6440
6441
6442/*
6443** Implementation of .ar "create", "insert", and "update" commands.
6444**
6445**     create    ->     Create a new SQL archive
6446**     insert    ->     Insert or reinsert all files listed
6447**     update    ->     Insert files that have changed or that were not
6448**                      previously in the archive
6449**
6450** Create the "sqlar" table in the database if it does not already exist.
6451** Then add each file in the azFile[] array to the archive. Directories
6452** are added recursively. If argument bVerbose is non-zero, a message is
6453** printed on stdout for each file archived.
6454**
6455** The create command is the same as update, except that it drops
6456** any existing "sqlar" table before beginning.  The "insert" command
6457** always overwrites every file named on the command-line, where as
6458** "update" only overwrites if the size or mtime or mode has changed.
6459*/
6460static int arCreateOrUpdateCommand(
6461  ArCommand *pAr,                 /* Command arguments and options */
6462  int bUpdate,                    /* true for a --create. */
6463  int bOnlyIfChanged              /* Only update if file has changed */
6464){
6465  const char *zCreate =
6466      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6467      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6468      "  mode INT,               -- access permissions\n"
6469      "  mtime INT,              -- last modification time\n"
6470      "  sz INT,                 -- original file size\n"
6471      "  data BLOB               -- compressed content\n"
6472      ")";
6473  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6474  const char *zInsertFmt[2] = {
6475     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6476     "  SELECT\n"
6477     "    %s,\n"
6478     "    mode,\n"
6479     "    mtime,\n"
6480     "    CASE substr(lsmode(mode),1,1)\n"
6481     "      WHEN '-' THEN length(data)\n"
6482     "      WHEN 'd' THEN 0\n"
6483     "      ELSE -1 END,\n"
6484     "    sqlar_compress(data)\n"
6485     "  FROM fsdir(%Q,%Q) AS disk\n"
6486     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6487     ,
6488     "REPLACE INTO %s(name,mode,mtime,data)\n"
6489     "  SELECT\n"
6490     "    %s,\n"
6491     "    mode,\n"
6492     "    mtime,\n"
6493     "    data\n"
6494     "  FROM fsdir(%Q,%Q) AS disk\n"
6495     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6496  };
6497  int i;                          /* For iterating through azFile[] */
6498  int rc;                         /* Return code */
6499  const char *zTab = 0;           /* SQL table into which to insert */
6500  char *zSql;
6501  char zTemp[50];
6502  char *zExists = 0;
6503
6504  arExecSql(pAr, "PRAGMA page_size=512");
6505  rc = arExecSql(pAr, "SAVEPOINT ar;");
6506  if( rc!=SQLITE_OK ) return rc;
6507  zTemp[0] = 0;
6508  if( pAr->bZip ){
6509    /* Initialize the zipfile virtual table, if necessary */
6510    if( pAr->zFile ){
6511      sqlite3_uint64 r;
6512      sqlite3_randomness(sizeof(r),&r);
6513      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6514      zTab = zTemp;
6515      zSql = sqlite3_mprintf(
6516         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6517         zTab, pAr->zFile
6518      );
6519      rc = arExecSql(pAr, zSql);
6520      sqlite3_free(zSql);
6521    }else{
6522      zTab = "zip";
6523    }
6524  }else{
6525    /* Initialize the table for an SQLAR */
6526    zTab = "sqlar";
6527    if( bUpdate==0 ){
6528      rc = arExecSql(pAr, zDrop);
6529      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6530    }
6531    rc = arExecSql(pAr, zCreate);
6532  }
6533  if( bOnlyIfChanged ){
6534    zExists = sqlite3_mprintf(
6535      " AND NOT EXISTS("
6536          "SELECT 1 FROM %s AS mem"
6537          " WHERE mem.name=disk.name"
6538          " AND mem.mtime=disk.mtime"
6539          " AND mem.mode=disk.mode)", zTab);
6540  }else{
6541    zExists = sqlite3_mprintf("");
6542  }
6543  if( zExists==0 ) rc = SQLITE_NOMEM;
6544  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6545    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6546        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6547        pAr->azArg[i], pAr->zDir, zExists);
6548    rc = arExecSql(pAr, zSql2);
6549    sqlite3_free(zSql2);
6550  }
6551end_ar_transaction:
6552  if( rc!=SQLITE_OK ){
6553    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6554  }else{
6555    rc = arExecSql(pAr, "RELEASE ar;");
6556    if( pAr->bZip && pAr->zFile ){
6557      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6558      arExecSql(pAr, zSql);
6559      sqlite3_free(zSql);
6560    }
6561  }
6562  sqlite3_free(zExists);
6563  return rc;
6564}
6565
6566/*
6567** Implementation of ".ar" dot command.
6568*/
6569static int arDotCommand(
6570  ShellState *pState,          /* Current shell tool state */
6571  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6572  char **azArg,                /* Array of arguments passed to dot command */
6573  int nArg                     /* Number of entries in azArg[] */
6574){
6575  ArCommand cmd;
6576  int rc;
6577  memset(&cmd, 0, sizeof(cmd));
6578  cmd.fromCmdLine = fromCmdLine;
6579  rc = arParseCommand(azArg, nArg, &cmd);
6580  if( rc==SQLITE_OK ){
6581    int eDbType = SHELL_OPEN_UNSPEC;
6582    cmd.p = pState;
6583    cmd.db = pState->db;
6584    if( cmd.zFile ){
6585      eDbType = deduceDatabaseType(cmd.zFile, 1);
6586    }else{
6587      eDbType = pState->openMode;
6588    }
6589    if( eDbType==SHELL_OPEN_ZIPFILE ){
6590      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6591        if( cmd.zFile==0 ){
6592          cmd.zSrcTable = sqlite3_mprintf("zip");
6593        }else{
6594          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6595        }
6596      }
6597      cmd.bZip = 1;
6598    }else if( cmd.zFile ){
6599      int flags;
6600      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6601      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6602           || cmd.eCmd==AR_CMD_UPDATE ){
6603        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6604      }else{
6605        flags = SQLITE_OPEN_READONLY;
6606      }
6607      cmd.db = 0;
6608      if( cmd.bDryRun ){
6609        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6610             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6611      }
6612      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6613             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6614      if( rc!=SQLITE_OK ){
6615        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6616            cmd.zFile, sqlite3_errmsg(cmd.db)
6617        );
6618        goto end_ar_command;
6619      }
6620      sqlite3_fileio_init(cmd.db, 0, 0);
6621      sqlite3_sqlar_init(cmd.db, 0, 0);
6622      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6623                              shellPutsFunc, 0, 0);
6624
6625    }
6626    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6627      if( cmd.eCmd!=AR_CMD_CREATE
6628       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6629      ){
6630        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6631        rc = SQLITE_ERROR;
6632        goto end_ar_command;
6633      }
6634      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6635    }
6636
6637    switch( cmd.eCmd ){
6638      case AR_CMD_CREATE:
6639        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6640        break;
6641
6642      case AR_CMD_EXTRACT:
6643        rc = arExtractCommand(&cmd);
6644        break;
6645
6646      case AR_CMD_LIST:
6647        rc = arListCommand(&cmd);
6648        break;
6649
6650      case AR_CMD_HELP:
6651        arUsage(pState->out);
6652        break;
6653
6654      case AR_CMD_INSERT:
6655        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6656        break;
6657
6658      default:
6659        assert( cmd.eCmd==AR_CMD_UPDATE );
6660        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6661        break;
6662    }
6663  }
6664end_ar_command:
6665  if( cmd.db!=pState->db ){
6666    close_db(cmd.db);
6667  }
6668  sqlite3_free(cmd.zSrcTable);
6669
6670  return rc;
6671}
6672/* End of the ".archive" or ".ar" command logic
6673*******************************************************************************/
6674#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6675
6676#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6677/*
6678** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6679** Otherwise, the SQL statement or statements in zSql are executed using
6680** database connection db and the error code written to *pRc before
6681** this function returns.
6682*/
6683static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6684  int rc = *pRc;
6685  if( rc==SQLITE_OK ){
6686    char *zErr = 0;
6687    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6688    if( rc!=SQLITE_OK ){
6689      raw_printf(stderr, "SQL error: %s\n", zErr);
6690    }
6691    *pRc = rc;
6692  }
6693}
6694
6695/*
6696** Like shellExec(), except that zFmt is a printf() style format string.
6697*/
6698static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6699  char *z = 0;
6700  if( *pRc==SQLITE_OK ){
6701    va_list ap;
6702    va_start(ap, zFmt);
6703    z = sqlite3_vmprintf(zFmt, ap);
6704    va_end(ap);
6705    if( z==0 ){
6706      *pRc = SQLITE_NOMEM;
6707    }else{
6708      shellExec(db, pRc, z);
6709    }
6710    sqlite3_free(z);
6711  }
6712}
6713
6714/*
6715** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6716** Otherwise, an attempt is made to allocate, zero and return a pointer
6717** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6718** to SQLITE_NOMEM and NULL returned.
6719*/
6720static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6721  void *pRet = 0;
6722  if( *pRc==SQLITE_OK ){
6723    pRet = sqlite3_malloc64(nByte);
6724    if( pRet==0 ){
6725      *pRc = SQLITE_NOMEM;
6726    }else{
6727      memset(pRet, 0, nByte);
6728    }
6729  }
6730  return pRet;
6731}
6732
6733/*
6734** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6735** Otherwise, zFmt is treated as a printf() style string. The result of
6736** formatting it along with any trailing arguments is written into a
6737** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6738** It is the responsibility of the caller to eventually free this buffer
6739** using a call to sqlite3_free().
6740**
6741** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6742** pointer returned.
6743*/
6744static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6745  char *z = 0;
6746  if( *pRc==SQLITE_OK ){
6747    va_list ap;
6748    va_start(ap, zFmt);
6749    z = sqlite3_vmprintf(zFmt, ap);
6750    va_end(ap);
6751    if( z==0 ){
6752      *pRc = SQLITE_NOMEM;
6753    }
6754  }
6755  return z;
6756}
6757
6758/*
6759** When running the ".recover" command, each output table, and the special
6760** orphaned row table if it is required, is represented by an instance
6761** of the following struct.
6762*/
6763typedef struct RecoverTable RecoverTable;
6764struct RecoverTable {
6765  char *zQuoted;                  /* Quoted version of table name */
6766  int nCol;                       /* Number of columns in table */
6767  char **azlCol;                  /* Array of column lists */
6768  int iPk;                        /* Index of IPK column */
6769};
6770
6771/*
6772** Free a RecoverTable object allocated by recoverFindTable() or
6773** recoverOrphanTable().
6774*/
6775static void recoverFreeTable(RecoverTable *pTab){
6776  if( pTab ){
6777    sqlite3_free(pTab->zQuoted);
6778    if( pTab->azlCol ){
6779      int i;
6780      for(i=0; i<=pTab->nCol; i++){
6781        sqlite3_free(pTab->azlCol[i]);
6782      }
6783      sqlite3_free(pTab->azlCol);
6784    }
6785    sqlite3_free(pTab);
6786  }
6787}
6788
6789/*
6790** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6791** Otherwise, it allocates and returns a RecoverTable object based on the
6792** final four arguments passed to this function. It is the responsibility
6793** of the caller to eventually free the returned object using
6794** recoverFreeTable().
6795*/
6796static RecoverTable *recoverNewTable(
6797  int *pRc,                       /* IN/OUT: Error code */
6798  const char *zName,              /* Name of table */
6799  const char *zSql,               /* CREATE TABLE statement */
6800  int bIntkey,
6801  int nCol
6802){
6803  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6804  int rc = *pRc;
6805  RecoverTable *pTab = 0;
6806
6807  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6808  if( rc==SQLITE_OK ){
6809    int nSqlCol = 0;
6810    int bSqlIntkey = 0;
6811    sqlite3_stmt *pStmt = 0;
6812
6813    rc = sqlite3_open("", &dbtmp);
6814    if( rc==SQLITE_OK ){
6815      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6816                              shellIdQuote, 0, 0);
6817    }
6818    if( rc==SQLITE_OK ){
6819      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6820    }
6821    if( rc==SQLITE_OK ){
6822      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6823      if( rc==SQLITE_ERROR ){
6824        rc = SQLITE_OK;
6825        goto finished;
6826      }
6827    }
6828    shellPreparePrintf(dbtmp, &rc, &pStmt,
6829        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6830    );
6831    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6832      nSqlCol = sqlite3_column_int(pStmt, 0);
6833    }
6834    shellFinalize(&rc, pStmt);
6835
6836    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6837      goto finished;
6838    }
6839
6840    shellPreparePrintf(dbtmp, &rc, &pStmt,
6841      "SELECT ("
6842      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6843      ") FROM sqlite_schema WHERE name = %Q", zName
6844    );
6845    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6846      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6847    }
6848    shellFinalize(&rc, pStmt);
6849
6850    if( bIntkey==bSqlIntkey ){
6851      int i;
6852      const char *zPk = "_rowid_";
6853      sqlite3_stmt *pPkFinder = 0;
6854
6855      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6856      ** set zPk to the name of the PK column, and pTab->iPk to the index
6857      ** of the column, where columns are 0-numbered from left to right.
6858      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6859      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6860      pTab->iPk = -2;
6861      if( bIntkey ){
6862        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6863          "SELECT cid, name FROM pragma_table_info(%Q) "
6864          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6865          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6866          , zName, zName
6867        );
6868        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6869          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6870          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6871        }
6872      }
6873
6874      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6875      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6876      pTab->nCol = nSqlCol;
6877
6878      if( bIntkey ){
6879        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6880      }else{
6881        pTab->azlCol[0] = shellMPrintf(&rc, "");
6882      }
6883      i = 1;
6884      shellPreparePrintf(dbtmp, &rc, &pStmt,
6885          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6886          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6887          "FROM pragma_table_info(%Q)",
6888          bIntkey ? ", " : "", pTab->iPk,
6889          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6890          zName
6891      );
6892      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6893        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6894        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6895        i++;
6896      }
6897      shellFinalize(&rc, pStmt);
6898
6899      shellFinalize(&rc, pPkFinder);
6900    }
6901  }
6902
6903 finished:
6904  sqlite3_close(dbtmp);
6905  *pRc = rc;
6906  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6907    recoverFreeTable(pTab);
6908    pTab = 0;
6909  }
6910  return pTab;
6911}
6912
6913/*
6914** This function is called to search the schema recovered from the
6915** sqlite_schema table of the (possibly) corrupt database as part
6916** of a ".recover" command. Specifically, for a table with root page
6917** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6918** table must be a WITHOUT ROWID table, or if non-zero, not one of
6919** those.
6920**
6921** If a table is found, a (RecoverTable*) object is returned. Or, if
6922** no such table is found, but bIntkey is false and iRoot is the
6923** root page of an index in the recovered schema, then (*pbNoop) is
6924** set to true and NULL returned. Or, if there is no such table or
6925** index, NULL is returned and (*pbNoop) set to 0, indicating that
6926** the caller should write data to the orphans table.
6927*/
6928static RecoverTable *recoverFindTable(
6929  ShellState *pState,             /* Shell state object */
6930  int *pRc,                       /* IN/OUT: Error code */
6931  int iRoot,                      /* Root page of table */
6932  int bIntkey,                    /* True for an intkey table */
6933  int nCol,                       /* Number of columns in table */
6934  int *pbNoop                     /* OUT: True if iRoot is root of index */
6935){
6936  sqlite3_stmt *pStmt = 0;
6937  RecoverTable *pRet = 0;
6938  int bNoop = 0;
6939  const char *zSql = 0;
6940  const char *zName = 0;
6941
6942  /* Search the recovered schema for an object with root page iRoot. */
6943  shellPreparePrintf(pState->db, pRc, &pStmt,
6944      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6945  );
6946  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6947    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6948    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6949      bNoop = 1;
6950      break;
6951    }
6952    if( sqlite3_stricmp(zType, "table")==0 ){
6953      zName = (const char*)sqlite3_column_text(pStmt, 1);
6954      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6955      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6956      break;
6957    }
6958  }
6959
6960  shellFinalize(pRc, pStmt);
6961  *pbNoop = bNoop;
6962  return pRet;
6963}
6964
6965/*
6966** Return a RecoverTable object representing the orphans table.
6967*/
6968static RecoverTable *recoverOrphanTable(
6969  ShellState *pState,             /* Shell state object */
6970  int *pRc,                       /* IN/OUT: Error code */
6971  const char *zLostAndFound,      /* Base name for orphans table */
6972  int nCol                        /* Number of user data columns */
6973){
6974  RecoverTable *pTab = 0;
6975  if( nCol>=0 && *pRc==SQLITE_OK ){
6976    int i;
6977
6978    /* This block determines the name of the orphan table. The prefered
6979    ** name is zLostAndFound. But if that clashes with another name
6980    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
6981    ** and so on until a non-clashing name is found.  */
6982    int iTab = 0;
6983    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
6984    sqlite3_stmt *pTest = 0;
6985    shellPrepare(pState->db, pRc,
6986        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
6987    );
6988    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6989    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
6990      shellReset(pRc, pTest);
6991      sqlite3_free(zTab);
6992      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
6993      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6994    }
6995    shellFinalize(pRc, pTest);
6996
6997    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
6998    if( pTab ){
6999      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7000      pTab->nCol = nCol;
7001      pTab->iPk = -2;
7002      if( nCol>0 ){
7003        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7004        if( pTab->azlCol ){
7005          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7006          for(i=nCol-1; i>=0; i--){
7007            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7008          }
7009        }
7010      }
7011
7012      if( *pRc!=SQLITE_OK ){
7013        recoverFreeTable(pTab);
7014        pTab = 0;
7015      }else{
7016        raw_printf(pState->out,
7017            "CREATE TABLE %s(rootpgno INTEGER, "
7018            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7019        );
7020        for(i=0; i<nCol; i++){
7021          raw_printf(pState->out, ", c%d", i);
7022        }
7023        raw_printf(pState->out, ");\n");
7024      }
7025    }
7026    sqlite3_free(zTab);
7027  }
7028  return pTab;
7029}
7030
7031/*
7032** This function is called to recover data from the database. A script
7033** to construct a new database containing all recovered data is output
7034** on stream pState->out.
7035*/
7036static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7037  int rc = SQLITE_OK;
7038  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7039  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7040  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7041  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7042  const char *zLostAndFound = "lost_and_found";
7043  int i;
7044  int nOrphan = -1;
7045  RecoverTable *pOrphan = 0;
7046
7047  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7048  int bRowids = 1;                /* 0 if --no-rowids */
7049  for(i=1; i<nArg; i++){
7050    char *z = azArg[i];
7051    int n;
7052    if( z[0]=='-' && z[1]=='-' ) z++;
7053    n = strlen30(z);
7054    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7055      bFreelist = 0;
7056    }else
7057    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7058      i++;
7059      zRecoveryDb = azArg[i];
7060    }else
7061    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7062      i++;
7063      zLostAndFound = azArg[i];
7064    }else
7065    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7066      bRowids = 0;
7067    }
7068    else{
7069      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7070      showHelp(pState->out, azArg[0]);
7071      return 1;
7072    }
7073  }
7074
7075  shellExecPrintf(pState->db, &rc,
7076    /* Attach an in-memory database named 'recovery'. Create an indexed
7077    ** cache of the sqlite_dbptr virtual table. */
7078    "PRAGMA writable_schema = on;"
7079    "ATTACH %Q AS recovery;"
7080    "DROP TABLE IF EXISTS recovery.dbptr;"
7081    "DROP TABLE IF EXISTS recovery.freelist;"
7082    "DROP TABLE IF EXISTS recovery.map;"
7083    "DROP TABLE IF EXISTS recovery.schema;"
7084    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7085  );
7086
7087  if( bFreelist ){
7088    shellExec(pState->db, &rc,
7089      "WITH trunk(pgno) AS ("
7090      "  SELECT shell_int32("
7091      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7092      "      WHERE x>0"
7093      "    UNION"
7094      "  SELECT shell_int32("
7095      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7096      "      FROM trunk WHERE x>0"
7097      "),"
7098      "freelist(data, n, freepgno) AS ("
7099      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7100      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7101      "    UNION ALL"
7102      "  SELECT data, n-1, shell_int32(data, 2+n) "
7103      "      FROM freelist WHERE n>=0"
7104      ")"
7105      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7106    );
7107  }
7108
7109  /* If this is an auto-vacuum database, add all pointer-map pages to
7110  ** the freelist table. Do this regardless of whether or not
7111  ** --freelist-corrupt was specified.  */
7112  shellExec(pState->db, &rc,
7113    "WITH ptrmap(pgno) AS ("
7114    "  SELECT 2 WHERE shell_int32("
7115    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7116    "  )"
7117    "    UNION ALL "
7118    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7119    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7120    ")"
7121    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7122  );
7123
7124  shellExec(pState->db, &rc,
7125    "CREATE TABLE recovery.dbptr("
7126    "      pgno, child, PRIMARY KEY(child, pgno)"
7127    ") WITHOUT ROWID;"
7128    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7129    "    SELECT * FROM sqlite_dbptr"
7130    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7131
7132    /* Delete any pointer to page 1. This ensures that page 1 is considered
7133    ** a root page, regardless of how corrupt the db is. */
7134    "DELETE FROM recovery.dbptr WHERE child = 1;"
7135
7136    /* Delete all pointers to any pages that have more than one pointer
7137    ** to them. Such pages will be treated as root pages when recovering
7138    ** data.  */
7139    "DELETE FROM recovery.dbptr WHERE child IN ("
7140    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7141    ");"
7142
7143    /* Create the "map" table that will (eventually) contain instructions
7144    ** for dealing with each page in the db that contains one or more
7145    ** records. */
7146    "CREATE TABLE recovery.map("
7147      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7148    ");"
7149
7150    /* Populate table [map]. If there are circular loops of pages in the
7151    ** database, the following adds all pages in such a loop to the map
7152    ** as individual root pages. This could be handled better.  */
7153    "WITH pages(i, maxlen) AS ("
7154    "  SELECT page_count, ("
7155    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7156    "  ) FROM pragma_page_count WHERE page_count>0"
7157    "    UNION ALL"
7158    "  SELECT i-1, ("
7159    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7160    "  ) FROM pages WHERE i>=2"
7161    ")"
7162    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7163    "  SELECT i, maxlen, NULL, ("
7164    "    WITH p(orig, pgno, parent) AS ("
7165    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7166    "        UNION "
7167    "      SELECT i, p.parent, "
7168    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7169    "    )"
7170    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7171    ") "
7172    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7173    "UPDATE recovery.map AS o SET intkey = ("
7174    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7175    ");"
7176
7177    /* Extract data from page 1 and any linked pages into table
7178    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7179    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7180    "INSERT INTO recovery.schema SELECT "
7181    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7182    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7183    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7184    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7185    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7186    "FROM sqlite_dbdata WHERE pgno IN ("
7187    "  SELECT pgno FROM recovery.map WHERE root=1"
7188    ")"
7189    "GROUP BY pgno, cell;"
7190    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7191  );
7192
7193  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7194  ** CREATE TABLE statements that extracted from the existing schema.  */
7195  if( rc==SQLITE_OK ){
7196    sqlite3_stmt *pStmt = 0;
7197    /* ".recover" might output content in an order which causes immediate
7198    ** foreign key constraints to be violated. So disable foreign-key
7199    ** constraint enforcement to prevent problems when running the output
7200    ** script. */
7201    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7202    raw_printf(pState->out, "BEGIN;\n");
7203    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7204    shellPrepare(pState->db, &rc,
7205        "SELECT sql FROM recovery.schema "
7206        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7207    );
7208    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7209      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7210      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7211          &zCreateTable[12]
7212      );
7213    }
7214    shellFinalize(&rc, pStmt);
7215  }
7216
7217  /* Figure out if an orphan table will be required. And if so, how many
7218  ** user columns it should contain */
7219  shellPrepare(pState->db, &rc,
7220      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7221      , &pLoop
7222  );
7223  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7224    nOrphan = sqlite3_column_int(pLoop, 0);
7225  }
7226  shellFinalize(&rc, pLoop);
7227  pLoop = 0;
7228
7229  shellPrepare(pState->db, &rc,
7230      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7231  );
7232
7233  shellPrepare(pState->db, &rc,
7234      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7235      "(case when (? AND field<0) then NULL else value end)"
7236      "), ', ')"
7237      ", min(field) "
7238      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7239      "GROUP BY cell", &pCells
7240  );
7241
7242  /* Loop through each root page. */
7243  shellPrepare(pState->db, &rc,
7244      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7245      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7246      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7247      ")", &pLoop
7248  );
7249  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7250    int iRoot = sqlite3_column_int(pLoop, 0);
7251    int bIntkey = sqlite3_column_int(pLoop, 1);
7252    int nCol = sqlite3_column_int(pLoop, 2);
7253    int bNoop = 0;
7254    RecoverTable *pTab;
7255
7256    assert( bIntkey==0 || bIntkey==1 );
7257    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7258    if( bNoop || rc ) continue;
7259    if( pTab==0 ){
7260      if( pOrphan==0 ){
7261        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7262      }
7263      pTab = pOrphan;
7264      if( pTab==0 ) break;
7265    }
7266
7267    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7268      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7269    }
7270    sqlite3_bind_int(pPages, 1, iRoot);
7271    if( bRowids==0 && pTab->iPk<0 ){
7272      sqlite3_bind_int(pCells, 1, 1);
7273    }else{
7274      sqlite3_bind_int(pCells, 1, 0);
7275    }
7276    sqlite3_bind_int(pCells, 3, pTab->iPk);
7277
7278    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7279      int iPgno = sqlite3_column_int(pPages, 0);
7280      sqlite3_bind_int(pCells, 2, iPgno);
7281      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7282        int nField = sqlite3_column_int(pCells, 0);
7283        int iMin = sqlite3_column_int(pCells, 2);
7284        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7285
7286        RecoverTable *pTab2 = pTab;
7287        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7288          if( pOrphan==0 ){
7289            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7290          }
7291          pTab2 = pOrphan;
7292          if( pTab2==0 ) break;
7293        }
7294
7295        nField = nField+1;
7296        if( pTab2==pOrphan ){
7297          raw_printf(pState->out,
7298              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7299              pTab2->zQuoted, iRoot, iPgno, nField,
7300              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7301          );
7302        }else{
7303          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7304              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7305          );
7306        }
7307      }
7308      shellReset(&rc, pCells);
7309    }
7310    shellReset(&rc, pPages);
7311    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7312  }
7313  shellFinalize(&rc, pLoop);
7314  shellFinalize(&rc, pPages);
7315  shellFinalize(&rc, pCells);
7316  recoverFreeTable(pOrphan);
7317
7318  /* The rest of the schema */
7319  if( rc==SQLITE_OK ){
7320    sqlite3_stmt *pStmt = 0;
7321    shellPrepare(pState->db, &rc,
7322        "SELECT sql, name FROM recovery.schema "
7323        "WHERE sql NOT LIKE 'create table%'", &pStmt
7324    );
7325    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7326      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7327      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7328        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7329        char *zPrint = shellMPrintf(&rc,
7330          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7331          zName, zName, zSql
7332        );
7333        raw_printf(pState->out, "%s;\n", zPrint);
7334        sqlite3_free(zPrint);
7335      }else{
7336        raw_printf(pState->out, "%s;\n", zSql);
7337      }
7338    }
7339    shellFinalize(&rc, pStmt);
7340  }
7341
7342  if( rc==SQLITE_OK ){
7343    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7344    raw_printf(pState->out, "COMMIT;\n");
7345  }
7346  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7347  return rc;
7348}
7349#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7350
7351
7352/*
7353** If an input line begins with "." then invoke this routine to
7354** process that line.
7355**
7356** Return 1 on error, 2 to exit, and 0 otherwise.
7357*/
7358static int do_meta_command(char *zLine, ShellState *p){
7359  int h = 1;
7360  int nArg = 0;
7361  int n, c;
7362  int rc = 0;
7363  char *azArg[52];
7364
7365#ifndef SQLITE_OMIT_VIRTUALTABLE
7366  if( p->expert.pExpert ){
7367    expertFinish(p, 1, 0);
7368  }
7369#endif
7370
7371  /* Parse the input line into tokens.
7372  */
7373  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7374    while( IsSpace(zLine[h]) ){ h++; }
7375    if( zLine[h]==0 ) break;
7376    if( zLine[h]=='\'' || zLine[h]=='"' ){
7377      int delim = zLine[h++];
7378      azArg[nArg++] = &zLine[h];
7379      while( zLine[h] && zLine[h]!=delim ){
7380        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7381        h++;
7382      }
7383      if( zLine[h]==delim ){
7384        zLine[h++] = 0;
7385      }
7386      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7387    }else{
7388      azArg[nArg++] = &zLine[h];
7389      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7390      if( zLine[h] ) zLine[h++] = 0;
7391      resolve_backslashes(azArg[nArg-1]);
7392    }
7393  }
7394  azArg[nArg] = 0;
7395
7396  /* Process the input line.
7397  */
7398  if( nArg==0 ) return 0; /* no tokens, no error */
7399  n = strlen30(azArg[0]);
7400  c = azArg[0][0];
7401  clearTempFile(p);
7402
7403#ifndef SQLITE_OMIT_AUTHORIZATION
7404  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7405    if( nArg!=2 ){
7406      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7407      rc = 1;
7408      goto meta_command_exit;
7409    }
7410    open_db(p, 0);
7411    if( booleanValue(azArg[1]) ){
7412      sqlite3_set_authorizer(p->db, shellAuth, p);
7413    }else{
7414      sqlite3_set_authorizer(p->db, 0, 0);
7415    }
7416  }else
7417#endif
7418
7419#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7420  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7421    open_db(p, 0);
7422    rc = arDotCommand(p, 0, azArg, nArg);
7423  }else
7424#endif
7425
7426  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7427   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7428  ){
7429    const char *zDestFile = 0;
7430    const char *zDb = 0;
7431    sqlite3 *pDest;
7432    sqlite3_backup *pBackup;
7433    int j;
7434    int bAsync = 0;
7435    const char *zVfs = 0;
7436    for(j=1; j<nArg; j++){
7437      const char *z = azArg[j];
7438      if( z[0]=='-' ){
7439        if( z[1]=='-' ) z++;
7440        if( strcmp(z, "-append")==0 ){
7441          zVfs = "apndvfs";
7442        }else
7443        if( strcmp(z, "-async")==0 ){
7444          bAsync = 1;
7445        }else
7446        {
7447          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7448          return 1;
7449        }
7450      }else if( zDestFile==0 ){
7451        zDestFile = azArg[j];
7452      }else if( zDb==0 ){
7453        zDb = zDestFile;
7454        zDestFile = azArg[j];
7455      }else{
7456        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7457        return 1;
7458      }
7459    }
7460    if( zDestFile==0 ){
7461      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7462      return 1;
7463    }
7464    if( zDb==0 ) zDb = "main";
7465    rc = sqlite3_open_v2(zDestFile, &pDest,
7466                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7467    if( rc!=SQLITE_OK ){
7468      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7469      close_db(pDest);
7470      return 1;
7471    }
7472    if( bAsync ){
7473      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7474                   0, 0, 0);
7475    }
7476    open_db(p, 0);
7477    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7478    if( pBackup==0 ){
7479      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7480      close_db(pDest);
7481      return 1;
7482    }
7483    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7484    sqlite3_backup_finish(pBackup);
7485    if( rc==SQLITE_DONE ){
7486      rc = 0;
7487    }else{
7488      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7489      rc = 1;
7490    }
7491    close_db(pDest);
7492  }else
7493
7494  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7495    if( nArg==2 ){
7496      bail_on_error = booleanValue(azArg[1]);
7497    }else{
7498      raw_printf(stderr, "Usage: .bail on|off\n");
7499      rc = 1;
7500    }
7501  }else
7502
7503  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7504    if( nArg==2 ){
7505      if( booleanValue(azArg[1]) ){
7506        setBinaryMode(p->out, 1);
7507      }else{
7508        setTextMode(p->out, 1);
7509      }
7510    }else{
7511      raw_printf(stderr, "Usage: .binary on|off\n");
7512      rc = 1;
7513    }
7514  }else
7515
7516  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7517    if( nArg==2 ){
7518#if defined(_WIN32) || defined(WIN32)
7519      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7520      rc = !SetCurrentDirectoryW(z);
7521      sqlite3_free(z);
7522#else
7523      rc = chdir(azArg[1]);
7524#endif
7525      if( rc ){
7526        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7527        rc = 1;
7528      }
7529    }else{
7530      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7531      rc = 1;
7532    }
7533  }else
7534
7535  /* The undocumented ".breakpoint" command causes a call to the no-op
7536  ** routine named test_breakpoint().
7537  */
7538  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7539    test_breakpoint();
7540  }else
7541
7542  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7543    if( nArg==2 ){
7544      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7545    }else{
7546      raw_printf(stderr, "Usage: .changes on|off\n");
7547      rc = 1;
7548    }
7549  }else
7550
7551  /* Cancel output redirection, if it is currently set (by .testcase)
7552  ** Then read the content of the testcase-out.txt file and compare against
7553  ** azArg[1].  If there are differences, report an error and exit.
7554  */
7555  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7556    char *zRes = 0;
7557    output_reset(p);
7558    if( nArg!=2 ){
7559      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7560      rc = 2;
7561    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7562      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7563      rc = 2;
7564    }else if( testcase_glob(azArg[1],zRes)==0 ){
7565      utf8_printf(stderr,
7566                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7567                 p->zTestcase, azArg[1], zRes);
7568      rc = 1;
7569    }else{
7570      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7571      p->nCheck++;
7572    }
7573    sqlite3_free(zRes);
7574  }else
7575
7576  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7577    if( nArg==2 ){
7578      tryToClone(p, azArg[1]);
7579    }else{
7580      raw_printf(stderr, "Usage: .clone FILENAME\n");
7581      rc = 1;
7582    }
7583  }else
7584
7585  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7586    char **azName = 0;
7587    int nName = 0;
7588    sqlite3_stmt *pStmt;
7589    int i;
7590    open_db(p, 0);
7591    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7592    if( rc ){
7593      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7594      rc = 1;
7595    }else{
7596      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7597        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7598        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7599        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7600        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7601        azName[nName*2] = strdup(zSchema);
7602        azName[nName*2+1] = strdup(zFile);
7603        nName++;
7604      }
7605    }
7606    sqlite3_finalize(pStmt);
7607    for(i=0; i<nName; i++){
7608      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7609      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7610      const char *z = azName[i*2+1];
7611      utf8_printf(p->out, "%s: %s %s%s\n",
7612         azName[i*2],
7613         z && z[0] ? z : "\"\"",
7614         bRdonly ? "r/o" : "r/w",
7615         eTxn==SQLITE_TXN_NONE ? "" :
7616            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7617      free(azName[i*2]);
7618      free(azName[i*2+1]);
7619    }
7620    sqlite3_free(azName);
7621  }else
7622
7623  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7624    static const struct DbConfigChoices {
7625      const char *zName;
7626      int op;
7627    } aDbConfig[] = {
7628        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7629        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7630        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7631        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7632        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7633        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7634        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7635        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7636        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7637        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7638        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7639        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7640        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7641        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7642        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7643        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7644    };
7645    int ii, v;
7646    open_db(p, 0);
7647    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7648      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7649      if( nArg>=3 ){
7650        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7651      }
7652      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7653      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7654      if( nArg>1 ) break;
7655    }
7656    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7657      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7658      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7659    }
7660  }else
7661
7662  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7663    rc = shell_dbinfo_command(p, nArg, azArg);
7664  }else
7665
7666#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7667  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7668    open_db(p, 0);
7669    rc = recoverDatabaseCmd(p, nArg, azArg);
7670  }else
7671#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7672
7673  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7674    char *zLike = 0;
7675    char *zSql;
7676    int i;
7677    int savedShowHeader = p->showHeader;
7678    int savedShellFlags = p->shellFlgs;
7679    ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
7680    for(i=1; i<nArg; i++){
7681      if( azArg[i][0]=='-' ){
7682        const char *z = azArg[i]+1;
7683        if( z[0]=='-' ) z++;
7684        if( strcmp(z,"preserve-rowids")==0 ){
7685#ifdef SQLITE_OMIT_VIRTUALTABLE
7686          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7687                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7688          rc = 1;
7689          sqlite3_free(zLike);
7690          goto meta_command_exit;
7691#else
7692          ShellSetFlag(p, SHFLG_PreserveRowid);
7693#endif
7694        }else
7695        if( strcmp(z,"newlines")==0 ){
7696          ShellSetFlag(p, SHFLG_Newlines);
7697        }else
7698        {
7699          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7700          rc = 1;
7701          sqlite3_free(zLike);
7702          goto meta_command_exit;
7703        }
7704      }else if( zLike ){
7705        zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
7706                zLike, azArg[i]);
7707      }else{
7708        zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
7709      }
7710    }
7711
7712    open_db(p, 0);
7713
7714    /* When playing back a "dump", the content might appear in an order
7715    ** which causes immediate foreign key constraints to be violated.
7716    ** So disable foreign-key constraint enforcement to prevent problems. */
7717    raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7718    raw_printf(p->out, "BEGIN TRANSACTION;\n");
7719    p->writableSchema = 0;
7720    p->showHeader = 0;
7721    /* Set writable_schema=ON since doing so forces SQLite to initialize
7722    ** as much of the schema as it can even if the sqlite_schema table is
7723    ** corrupt. */
7724    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7725    p->nErr = 0;
7726    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7727    zSql = sqlite3_mprintf(
7728      "SELECT name, type, sql FROM sqlite_schema "
7729      "WHERE (%s) AND type=='table'"
7730      "  AND sql NOT NULL"
7731      " ORDER BY tbl_name='sqlite_sequence', rowid",
7732      zLike
7733    );
7734    run_schema_dump_query(p,zSql);
7735    sqlite3_free(zSql);
7736    zSql = sqlite3_mprintf(
7737      "SELECT sql FROM sqlite_schema "
7738      "WHERE (%s) AND sql NOT NULL"
7739      "  AND type IN ('index','trigger','view')",
7740      zLike
7741    );
7742    run_table_dump_query(p, zSql);
7743    sqlite3_free(zSql);
7744    sqlite3_free(zLike);
7745    if( p->writableSchema ){
7746      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7747      p->writableSchema = 0;
7748    }
7749    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7750    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7751    raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7752    p->showHeader = savedShowHeader;
7753    p->shellFlgs = savedShellFlags;
7754  }else
7755
7756  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7757    if( nArg==2 ){
7758      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7759    }else{
7760      raw_printf(stderr, "Usage: .echo on|off\n");
7761      rc = 1;
7762    }
7763  }else
7764
7765  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7766    if( nArg==2 ){
7767      p->autoEQPtest = 0;
7768      if( p->autoEQPtrace ){
7769        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7770        p->autoEQPtrace = 0;
7771      }
7772      if( strcmp(azArg[1],"full")==0 ){
7773        p->autoEQP = AUTOEQP_full;
7774      }else if( strcmp(azArg[1],"trigger")==0 ){
7775        p->autoEQP = AUTOEQP_trigger;
7776#ifdef SQLITE_DEBUG
7777      }else if( strcmp(azArg[1],"test")==0 ){
7778        p->autoEQP = AUTOEQP_on;
7779        p->autoEQPtest = 1;
7780      }else if( strcmp(azArg[1],"trace")==0 ){
7781        p->autoEQP = AUTOEQP_full;
7782        p->autoEQPtrace = 1;
7783        open_db(p, 0);
7784        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
7785        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7786#endif
7787      }else{
7788        p->autoEQP = (u8)booleanValue(azArg[1]);
7789      }
7790    }else{
7791      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7792      rc = 1;
7793    }
7794  }else
7795
7796  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7797    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7798    rc = 2;
7799  }else
7800
7801  /* The ".explain" command is automatic now.  It is largely pointless.  It
7802  ** retained purely for backwards compatibility */
7803  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7804    int val = 1;
7805    if( nArg>=2 ){
7806      if( strcmp(azArg[1],"auto")==0 ){
7807        val = 99;
7808      }else{
7809        val =  booleanValue(azArg[1]);
7810      }
7811    }
7812    if( val==1 && p->mode!=MODE_Explain ){
7813      p->normalMode = p->mode;
7814      p->mode = MODE_Explain;
7815      p->autoExplain = 0;
7816    }else if( val==0 ){
7817      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7818      p->autoExplain = 0;
7819    }else if( val==99 ){
7820      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7821      p->autoExplain = 1;
7822    }
7823  }else
7824
7825#ifndef SQLITE_OMIT_VIRTUALTABLE
7826  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7827    open_db(p, 0);
7828    expertDotCommand(p, azArg, nArg);
7829  }else
7830#endif
7831
7832  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7833    static const struct {
7834       const char *zCtrlName;   /* Name of a test-control option */
7835       int ctrlCode;            /* Integer code for that option */
7836       const char *zUsage;      /* Usage notes */
7837    } aCtrl[] = {
7838      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7839      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7840   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7841      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7842      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7843   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7844      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7845      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7846      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7847      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
7848    };
7849    int filectrl = -1;
7850    int iCtrl = -1;
7851    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7852    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7853    int n2, i;
7854    const char *zCmd = 0;
7855    const char *zSchema = 0;
7856
7857    open_db(p, 0);
7858    zCmd = nArg>=2 ? azArg[1] : "help";
7859
7860    if( zCmd[0]=='-'
7861     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7862     && nArg>=4
7863    ){
7864      zSchema = azArg[2];
7865      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7866      nArg -= 2;
7867      zCmd = azArg[1];
7868    }
7869
7870    /* The argument can optionally begin with "-" or "--" */
7871    if( zCmd[0]=='-' && zCmd[1] ){
7872      zCmd++;
7873      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7874    }
7875
7876    /* --help lists all file-controls */
7877    if( strcmp(zCmd,"help")==0 ){
7878      utf8_printf(p->out, "Available file-controls:\n");
7879      for(i=0; i<ArraySize(aCtrl); i++){
7880        utf8_printf(p->out, "  .filectrl %s %s\n",
7881                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7882      }
7883      rc = 1;
7884      goto meta_command_exit;
7885    }
7886
7887    /* convert filectrl text option to value. allow any unique prefix
7888    ** of the option name, or a numerical value. */
7889    n2 = strlen30(zCmd);
7890    for(i=0; i<ArraySize(aCtrl); i++){
7891      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7892        if( filectrl<0 ){
7893          filectrl = aCtrl[i].ctrlCode;
7894          iCtrl = i;
7895        }else{
7896          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7897                              "Use \".filectrl --help\" for help\n", zCmd);
7898          rc = 1;
7899          goto meta_command_exit;
7900        }
7901      }
7902    }
7903    if( filectrl<0 ){
7904      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7905                         "Use \".filectrl --help\" for help\n", zCmd);
7906    }else{
7907      switch(filectrl){
7908        case SQLITE_FCNTL_SIZE_LIMIT: {
7909          if( nArg!=2 && nArg!=3 ) break;
7910          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7911          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7912          isOk = 1;
7913          break;
7914        }
7915        case SQLITE_FCNTL_LOCK_TIMEOUT:
7916        case SQLITE_FCNTL_CHUNK_SIZE: {
7917          int x;
7918          if( nArg!=3 ) break;
7919          x = (int)integerValue(azArg[2]);
7920          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7921          isOk = 2;
7922          break;
7923        }
7924        case SQLITE_FCNTL_PERSIST_WAL:
7925        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7926          int x;
7927          if( nArg!=2 && nArg!=3 ) break;
7928          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7929          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7930          iRes = x;
7931          isOk = 1;
7932          break;
7933        }
7934        case SQLITE_FCNTL_HAS_MOVED: {
7935          int x;
7936          if( nArg!=2 ) break;
7937          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7938          iRes = x;
7939          isOk = 1;
7940          break;
7941        }
7942        case SQLITE_FCNTL_TEMPFILENAME: {
7943          char *z = 0;
7944          if( nArg!=2 ) break;
7945          sqlite3_file_control(p->db, zSchema, filectrl, &z);
7946          if( z ){
7947            utf8_printf(p->out, "%s\n", z);
7948            sqlite3_free(z);
7949          }
7950          isOk = 2;
7951          break;
7952        }
7953        case SQLITE_FCNTL_RESERVE_BYTES: {
7954          int x;
7955          if( nArg>=3 ){
7956            x = atoi(azArg[2]);
7957            sqlite3_file_control(p->db, zSchema, filectrl, &x);
7958          }
7959          x = -1;
7960          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7961          utf8_printf(p->out,"%d\n", x);
7962          isOk = 2;
7963          break;
7964        }
7965      }
7966    }
7967    if( isOk==0 && iCtrl>=0 ){
7968      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
7969      rc = 1;
7970    }else if( isOk==1 ){
7971      char zBuf[100];
7972      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
7973      raw_printf(p->out, "%s\n", zBuf);
7974    }
7975  }else
7976
7977  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
7978    ShellState data;
7979    char *zErrMsg = 0;
7980    int doStats = 0;
7981    memcpy(&data, p, sizeof(data));
7982    data.showHeader = 0;
7983    data.cMode = data.mode = MODE_Semi;
7984    if( nArg==2 && optionMatch(azArg[1], "indent") ){
7985      data.cMode = data.mode = MODE_Pretty;
7986      nArg = 1;
7987    }
7988    if( nArg!=1 ){
7989      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
7990      rc = 1;
7991      goto meta_command_exit;
7992    }
7993    open_db(p, 0);
7994    rc = sqlite3_exec(p->db,
7995       "SELECT sql FROM"
7996       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
7997       "     FROM sqlite_schema UNION ALL"
7998       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
7999       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8000       "ORDER BY rowid",
8001       callback, &data, &zErrMsg
8002    );
8003    if( rc==SQLITE_OK ){
8004      sqlite3_stmt *pStmt;
8005      rc = sqlite3_prepare_v2(p->db,
8006               "SELECT rowid FROM sqlite_schema"
8007               " WHERE name GLOB 'sqlite_stat[134]'",
8008               -1, &pStmt, 0);
8009      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8010      sqlite3_finalize(pStmt);
8011    }
8012    if( doStats==0 ){
8013      raw_printf(p->out, "/* No STAT tables available */\n");
8014    }else{
8015      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8016      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
8017                   callback, &data, &zErrMsg);
8018      data.cMode = data.mode = MODE_Insert;
8019      data.zDestTable = "sqlite_stat1";
8020      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
8021      data.zDestTable = "sqlite_stat4";
8022      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
8023      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8024    }
8025  }else
8026
8027  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8028    if( nArg==2 ){
8029      p->showHeader = booleanValue(azArg[1]);
8030      p->shellFlgs |= SHFLG_HeaderSet;
8031    }else{
8032      raw_printf(stderr, "Usage: .headers on|off\n");
8033      rc = 1;
8034    }
8035  }else
8036
8037  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8038    if( nArg>=2 ){
8039      n = showHelp(p->out, azArg[1]);
8040      if( n==0 ){
8041        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8042      }
8043    }else{
8044      showHelp(p->out, 0);
8045    }
8046  }else
8047
8048  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8049    char *zTable = 0;           /* Insert data into this table */
8050    char *zFile = 0;            /* Name of file to extra content from */
8051    sqlite3_stmt *pStmt = NULL; /* A statement */
8052    int nCol;                   /* Number of columns in the table */
8053    int nByte;                  /* Number of bytes in an SQL string */
8054    int i, j;                   /* Loop counters */
8055    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8056    int nSep;                   /* Number of bytes in p->colSeparator[] */
8057    char *zSql;                 /* An SQL statement */
8058    ImportCtx sCtx;             /* Reader context */
8059    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8060    int eVerbose = 0;           /* Larger for more console output */
8061    int nSkip = 0;              /* Initial lines to skip */
8062    int useOutputMode = 1;      /* Use output mode to determine separators */
8063
8064    memset(&sCtx, 0, sizeof(sCtx));
8065    if( p->mode==MODE_Ascii ){
8066      xRead = ascii_read_one_field;
8067    }else{
8068      xRead = csv_read_one_field;
8069    }
8070    for(i=1; i<nArg; i++){
8071      char *z = azArg[i];
8072      if( z[0]=='-' && z[1]=='-' ) z++;
8073      if( z[0]!='-' ){
8074        if( zFile==0 ){
8075          zFile = z;
8076        }else if( zTable==0 ){
8077          zTable = z;
8078        }else{
8079          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8080          showHelp(p->out, "import");
8081          rc = 1;
8082          goto meta_command_exit;
8083        }
8084      }else if( strcmp(z,"-v")==0 ){
8085        eVerbose++;
8086      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8087        nSkip = integerValue(azArg[++i]);
8088      }else if( strcmp(z,"-ascii")==0 ){
8089        sCtx.cColSep = SEP_Unit[0];
8090        sCtx.cRowSep = SEP_Record[0];
8091        xRead = ascii_read_one_field;
8092        useOutputMode = 0;
8093      }else if( strcmp(z,"-csv")==0 ){
8094        sCtx.cColSep = ',';
8095        sCtx.cRowSep = '\n';
8096        xRead = csv_read_one_field;
8097        useOutputMode = 0;
8098      }else{
8099        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8100        showHelp(p->out, "import");
8101        rc = 1;
8102        goto meta_command_exit;
8103      }
8104    }
8105    if( zTable==0 ){
8106      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8107                  zFile==0 ? "FILE" : "TABLE");
8108      showHelp(p->out, "import");
8109      rc = 1;
8110      goto meta_command_exit;
8111    }
8112    seenInterrupt = 0;
8113    open_db(p, 0);
8114    if( useOutputMode ){
8115      /* If neither the --csv or --ascii options are specified, then set
8116      ** the column and row separator characters from the output mode. */
8117      nSep = strlen30(p->colSeparator);
8118      if( nSep==0 ){
8119        raw_printf(stderr,
8120                   "Error: non-null column separator required for import\n");
8121        rc = 1;
8122        goto meta_command_exit;
8123      }
8124      if( nSep>1 ){
8125        raw_printf(stderr,
8126              "Error: multi-character column separators not allowed"
8127              " for import\n");
8128        rc = 1;
8129        goto meta_command_exit;
8130      }
8131      nSep = strlen30(p->rowSeparator);
8132      if( nSep==0 ){
8133        raw_printf(stderr,
8134            "Error: non-null row separator required for import\n");
8135        rc = 1;
8136        goto meta_command_exit;
8137      }
8138      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8139        /* When importing CSV (only), if the row separator is set to the
8140        ** default output row separator, change it to the default input
8141        ** row separator.  This avoids having to maintain different input
8142        ** and output row separators. */
8143        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8144        nSep = strlen30(p->rowSeparator);
8145      }
8146      if( nSep>1 ){
8147        raw_printf(stderr, "Error: multi-character row separators not allowed"
8148                           " for import\n");
8149        rc = 1;
8150        goto meta_command_exit;
8151      }
8152      sCtx.cColSep = p->colSeparator[0];
8153      sCtx.cRowSep = p->rowSeparator[0];
8154    }
8155    sCtx.zFile = zFile;
8156    sCtx.nLine = 1;
8157    if( sCtx.zFile[0]=='|' ){
8158#ifdef SQLITE_OMIT_POPEN
8159      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8160      rc = 1;
8161      goto meta_command_exit;
8162#else
8163      sCtx.in = popen(sCtx.zFile+1, "r");
8164      sCtx.zFile = "<pipe>";
8165      sCtx.xCloser = pclose;
8166#endif
8167    }else{
8168      sCtx.in = fopen(sCtx.zFile, "rb");
8169      sCtx.xCloser = fclose;
8170    }
8171    if( sCtx.in==0 ){
8172      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8173      rc = 1;
8174      goto meta_command_exit;
8175    }
8176    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8177      char zSep[2];
8178      zSep[1] = 0;
8179      zSep[0] = sCtx.cColSep;
8180      utf8_printf(p->out, "Column separator ");
8181      output_c_string(p->out, zSep);
8182      utf8_printf(p->out, ", row separator ");
8183      zSep[0] = sCtx.cRowSep;
8184      output_c_string(p->out, zSep);
8185      utf8_printf(p->out, "\n");
8186    }
8187    while( (nSkip--)>0 ){
8188      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8189    }
8190    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
8191    if( zSql==0 ){
8192      import_cleanup(&sCtx);
8193      shell_out_of_memory();
8194    }
8195    nByte = strlen30(zSql);
8196    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8197    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8198    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8199      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
8200      char cSep = '(';
8201      while( xRead(&sCtx) ){
8202        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8203        cSep = ',';
8204        if( sCtx.cTerm!=sCtx.cColSep ) break;
8205      }
8206      if( cSep=='(' ){
8207        sqlite3_free(zCreate);
8208        import_cleanup(&sCtx);
8209        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8210        rc = 1;
8211        goto meta_command_exit;
8212      }
8213      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8214      if( eVerbose>=1 ){
8215        utf8_printf(p->out, "%s\n", zCreate);
8216      }
8217      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8218      sqlite3_free(zCreate);
8219      if( rc ){
8220        utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
8221                sqlite3_errmsg(p->db));
8222        import_cleanup(&sCtx);
8223        rc = 1;
8224        goto meta_command_exit;
8225      }
8226      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8227    }
8228    sqlite3_free(zSql);
8229    if( rc ){
8230      if (pStmt) sqlite3_finalize(pStmt);
8231      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8232      import_cleanup(&sCtx);
8233      rc = 1;
8234      goto meta_command_exit;
8235    }
8236    nCol = sqlite3_column_count(pStmt);
8237    sqlite3_finalize(pStmt);
8238    pStmt = 0;
8239    if( nCol==0 ) return 0; /* no columns, no error */
8240    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8241    if( zSql==0 ){
8242      import_cleanup(&sCtx);
8243      shell_out_of_memory();
8244    }
8245    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8246    j = strlen30(zSql);
8247    for(i=1; i<nCol; i++){
8248      zSql[j++] = ',';
8249      zSql[j++] = '?';
8250    }
8251    zSql[j++] = ')';
8252    zSql[j] = 0;
8253    if( eVerbose>=2 ){
8254      utf8_printf(p->out, "Insert using: %s\n", zSql);
8255    }
8256    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8257    sqlite3_free(zSql);
8258    if( rc ){
8259      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8260      if (pStmt) sqlite3_finalize(pStmt);
8261      import_cleanup(&sCtx);
8262      rc = 1;
8263      goto meta_command_exit;
8264    }
8265    needCommit = sqlite3_get_autocommit(p->db);
8266    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8267    do{
8268      int startLine = sCtx.nLine;
8269      for(i=0; i<nCol; i++){
8270        char *z = xRead(&sCtx);
8271        /*
8272        ** Did we reach end-of-file before finding any columns?
8273        ** If so, stop instead of NULL filling the remaining columns.
8274        */
8275        if( z==0 && i==0 ) break;
8276        /*
8277        ** Did we reach end-of-file OR end-of-line before finding any
8278        ** columns in ASCII mode?  If so, stop instead of NULL filling
8279        ** the remaining columns.
8280        */
8281        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8282        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8283        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8284          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8285                          "filling the rest with NULL\n",
8286                          sCtx.zFile, startLine, nCol, i+1);
8287          i += 2;
8288          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8289        }
8290      }
8291      if( sCtx.cTerm==sCtx.cColSep ){
8292        do{
8293          xRead(&sCtx);
8294          i++;
8295        }while( sCtx.cTerm==sCtx.cColSep );
8296        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8297                        "extras ignored\n",
8298                        sCtx.zFile, startLine, nCol, i);
8299      }
8300      if( i>=nCol ){
8301        sqlite3_step(pStmt);
8302        rc = sqlite3_reset(pStmt);
8303        if( rc!=SQLITE_OK ){
8304          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8305                      startLine, sqlite3_errmsg(p->db));
8306          sCtx.nErr++;
8307        }else{
8308          sCtx.nRow++;
8309        }
8310      }
8311    }while( sCtx.cTerm!=EOF );
8312
8313    import_cleanup(&sCtx);
8314    sqlite3_finalize(pStmt);
8315    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8316    if( eVerbose>0 ){
8317      utf8_printf(p->out,
8318          "Added %d rows with %d errors using %d lines of input\n",
8319          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8320    }
8321  }else
8322
8323#ifndef SQLITE_UNTESTABLE
8324  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8325    char *zSql;
8326    char *zCollist = 0;
8327    sqlite3_stmt *pStmt;
8328    int tnum = 0;
8329    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8330    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8331    int i;
8332    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8333      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8334                          "       .imposter off\n");
8335      /* Also allowed, but not documented:
8336      **
8337      **    .imposter TABLE IMPOSTER
8338      **
8339      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8340      ** imposter is another WITHOUT ROWID table with the columns in
8341      ** storage order. */
8342      rc = 1;
8343      goto meta_command_exit;
8344    }
8345    open_db(p, 0);
8346    if( nArg==2 ){
8347      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8348      goto meta_command_exit;
8349    }
8350    zSql = sqlite3_mprintf(
8351      "SELECT rootpage, 0 FROM sqlite_schema"
8352      " WHERE name='%q' AND type='index'"
8353      "UNION ALL "
8354      "SELECT rootpage, 1 FROM sqlite_schema"
8355      " WHERE name='%q' AND type='table'"
8356      "   AND sql LIKE '%%without%%rowid%%'",
8357      azArg[1], azArg[1]
8358    );
8359    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8360    sqlite3_free(zSql);
8361    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8362      tnum = sqlite3_column_int(pStmt, 0);
8363      isWO = sqlite3_column_int(pStmt, 1);
8364    }
8365    sqlite3_finalize(pStmt);
8366    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8367    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8368    sqlite3_free(zSql);
8369    i = 0;
8370    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8371      char zLabel[20];
8372      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8373      i++;
8374      if( zCol==0 ){
8375        if( sqlite3_column_int(pStmt,1)==-1 ){
8376          zCol = "_ROWID_";
8377        }else{
8378          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8379          zCol = zLabel;
8380        }
8381      }
8382      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8383        lenPK = (int)strlen(zCollist);
8384      }
8385      if( zCollist==0 ){
8386        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8387      }else{
8388        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8389      }
8390    }
8391    sqlite3_finalize(pStmt);
8392    if( i==0 || tnum==0 ){
8393      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8394      rc = 1;
8395      sqlite3_free(zCollist);
8396      goto meta_command_exit;
8397    }
8398    if( lenPK==0 ) lenPK = 100000;
8399    zSql = sqlite3_mprintf(
8400          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8401          azArg[2], zCollist, lenPK, zCollist);
8402    sqlite3_free(zCollist);
8403    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8404    if( rc==SQLITE_OK ){
8405      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8406      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8407      if( rc ){
8408        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8409      }else{
8410        utf8_printf(stdout, "%s;\n", zSql);
8411        raw_printf(stdout,
8412          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8413          azArg[1], isWO ? "table" : "index"
8414        );
8415      }
8416    }else{
8417      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8418      rc = 1;
8419    }
8420    sqlite3_free(zSql);
8421  }else
8422#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8423
8424#ifdef SQLITE_ENABLE_IOTRACE
8425  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8426    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8427    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8428    iotrace = 0;
8429    if( nArg<2 ){
8430      sqlite3IoTrace = 0;
8431    }else if( strcmp(azArg[1], "-")==0 ){
8432      sqlite3IoTrace = iotracePrintf;
8433      iotrace = stdout;
8434    }else{
8435      iotrace = fopen(azArg[1], "w");
8436      if( iotrace==0 ){
8437        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8438        sqlite3IoTrace = 0;
8439        rc = 1;
8440      }else{
8441        sqlite3IoTrace = iotracePrintf;
8442      }
8443    }
8444  }else
8445#endif
8446
8447  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8448    static const struct {
8449       const char *zLimitName;   /* Name of a limit */
8450       int limitCode;            /* Integer code for that limit */
8451    } aLimit[] = {
8452      { "length",                SQLITE_LIMIT_LENGTH                    },
8453      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8454      { "column",                SQLITE_LIMIT_COLUMN                    },
8455      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8456      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8457      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8458      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8459      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8460      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8461      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8462      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8463      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8464    };
8465    int i, n2;
8466    open_db(p, 0);
8467    if( nArg==1 ){
8468      for(i=0; i<ArraySize(aLimit); i++){
8469        printf("%20s %d\n", aLimit[i].zLimitName,
8470               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8471      }
8472    }else if( nArg>3 ){
8473      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8474      rc = 1;
8475      goto meta_command_exit;
8476    }else{
8477      int iLimit = -1;
8478      n2 = strlen30(azArg[1]);
8479      for(i=0; i<ArraySize(aLimit); i++){
8480        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8481          if( iLimit<0 ){
8482            iLimit = i;
8483          }else{
8484            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8485            rc = 1;
8486            goto meta_command_exit;
8487          }
8488        }
8489      }
8490      if( iLimit<0 ){
8491        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8492                        "enter \".limits\" with no arguments for a list.\n",
8493                         azArg[1]);
8494        rc = 1;
8495        goto meta_command_exit;
8496      }
8497      if( nArg==3 ){
8498        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8499                      (int)integerValue(azArg[2]));
8500      }
8501      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8502             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8503    }
8504  }else
8505
8506  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8507    open_db(p, 0);
8508    lintDotCommand(p, azArg, nArg);
8509  }else
8510
8511#ifndef SQLITE_OMIT_LOAD_EXTENSION
8512  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8513    const char *zFile, *zProc;
8514    char *zErrMsg = 0;
8515    if( nArg<2 ){
8516      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8517      rc = 1;
8518      goto meta_command_exit;
8519    }
8520    zFile = azArg[1];
8521    zProc = nArg>=3 ? azArg[2] : 0;
8522    open_db(p, 0);
8523    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8524    if( rc!=SQLITE_OK ){
8525      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8526      sqlite3_free(zErrMsg);
8527      rc = 1;
8528    }
8529  }else
8530#endif
8531
8532  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8533    if( nArg!=2 ){
8534      raw_printf(stderr, "Usage: .log FILENAME\n");
8535      rc = 1;
8536    }else{
8537      const char *zFile = azArg[1];
8538      output_file_close(p->pLog);
8539      p->pLog = output_file_open(zFile, 0);
8540    }
8541  }else
8542
8543  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8544    const char *zMode = nArg>=2 ? azArg[1] : "";
8545    int n2 = strlen30(zMode);
8546    int c2 = zMode[0];
8547    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8548      p->mode = MODE_Line;
8549      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8550    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8551      p->mode = MODE_Column;
8552      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8553        p->showHeader = 1;
8554      }
8555      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8556    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8557      p->mode = MODE_List;
8558      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8559      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8560    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8561      p->mode = MODE_Html;
8562    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8563      p->mode = MODE_Tcl;
8564      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8565      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8566    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8567      p->mode = MODE_Csv;
8568      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8569      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8570    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8571      p->mode = MODE_List;
8572      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8573    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8574      p->mode = MODE_Insert;
8575      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8576    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8577      p->mode = MODE_Quote;
8578      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8579      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8580    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8581      p->mode = MODE_Ascii;
8582      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8583      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8584    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8585      p->mode = MODE_Markdown;
8586    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8587      p->mode = MODE_Table;
8588    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8589      p->mode = MODE_Box;
8590    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8591      p->mode = MODE_Json;
8592    }else if( nArg==1 ){
8593      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8594    }else{
8595      raw_printf(stderr, "Error: mode should be one of: "
8596         "ascii box column csv html insert json line list markdown "
8597         "quote table tabs tcl\n");
8598      rc = 1;
8599    }
8600    p->cMode = p->mode;
8601  }else
8602
8603  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8604    if( nArg==2 ){
8605      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8606                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8607    }else{
8608      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8609      rc = 1;
8610    }
8611  }else
8612
8613#ifdef SQLITE_DEBUG
8614  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8615    int i;
8616    for(i=1; i<nArg; i++){
8617      const char *z = azArg[i];
8618      if( z[0]=='-' && z[1]=='-' ) z++;
8619      if( strcmp(z,"-repeat")==0 ){
8620        if( i==nArg-1 ){
8621          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8622          rc = 1;
8623        }else{
8624          oomRepeat = (int)integerValue(azArg[++i]);
8625        }
8626      }else if( IsDigit(z[0]) ){
8627        oomCounter = (int)integerValue(azArg[i]);
8628      }else{
8629        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8630        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8631        rc = 1;
8632      }
8633    }
8634    if( rc==0 ){
8635      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8636      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8637    }
8638  }else
8639#endif /* SQLITE_DEBUG */
8640
8641  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8642    char *zNewFilename;  /* Name of the database file to open */
8643    int iName = 1;       /* Index in azArg[] of the filename */
8644    int newFlag = 0;     /* True to delete file before opening */
8645    /* Close the existing database */
8646    session_close_all(p);
8647    close_db(p->db);
8648    p->db = 0;
8649    p->zDbFilename = 0;
8650    sqlite3_free(p->zFreeOnClose);
8651    p->zFreeOnClose = 0;
8652    p->openMode = SHELL_OPEN_UNSPEC;
8653    p->openFlags = 0;
8654    p->szMax = 0;
8655    /* Check for command-line arguments */
8656    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
8657      const char *z = azArg[iName];
8658      if( optionMatch(z,"new") ){
8659        newFlag = 1;
8660#ifdef SQLITE_HAVE_ZLIB
8661      }else if( optionMatch(z, "zip") ){
8662        p->openMode = SHELL_OPEN_ZIPFILE;
8663#endif
8664      }else if( optionMatch(z, "append") ){
8665        p->openMode = SHELL_OPEN_APPENDVFS;
8666      }else if( optionMatch(z, "readonly") ){
8667        p->openMode = SHELL_OPEN_READONLY;
8668      }else if( optionMatch(z, "nofollow") ){
8669        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8670#ifdef SQLITE_ENABLE_DESERIALIZE
8671      }else if( optionMatch(z, "deserialize") ){
8672        p->openMode = SHELL_OPEN_DESERIALIZE;
8673      }else if( optionMatch(z, "hexdb") ){
8674        p->openMode = SHELL_OPEN_HEXDB;
8675      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8676        p->szMax = integerValue(azArg[++iName]);
8677#endif /* SQLITE_ENABLE_DESERIALIZE */
8678      }else if( z[0]=='-' ){
8679        utf8_printf(stderr, "unknown option: %s\n", z);
8680        rc = 1;
8681        goto meta_command_exit;
8682      }
8683    }
8684    /* If a filename is specified, try to open it first */
8685    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
8686    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8687      if( newFlag ) shellDeleteFile(zNewFilename);
8688      p->zDbFilename = zNewFilename;
8689      open_db(p, OPEN_DB_KEEPALIVE);
8690      if( p->db==0 ){
8691        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8692        sqlite3_free(zNewFilename);
8693      }else{
8694        p->zFreeOnClose = zNewFilename;
8695      }
8696    }
8697    if( p->db==0 ){
8698      /* As a fall-back open a TEMP database */
8699      p->zDbFilename = 0;
8700      open_db(p, 0);
8701    }
8702  }else
8703
8704  if( (c=='o'
8705        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8706   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8707  ){
8708    const char *zFile = 0;
8709    int bTxtMode = 0;
8710    int i;
8711    int eMode = 0;
8712    int bBOM = 0;
8713    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8714
8715    if( c=='e' ){
8716      eMode = 'x';
8717      bOnce = 2;
8718    }else if( strncmp(azArg[0],"once",n)==0 ){
8719      bOnce = 1;
8720    }
8721    for(i=1; i<nArg; i++){
8722      char *z = azArg[i];
8723      if( z[0]=='-' ){
8724        if( z[1]=='-' ) z++;
8725        if( strcmp(z,"-bom")==0 ){
8726          bBOM = 1;
8727        }else if( c!='e' && strcmp(z,"-x")==0 ){
8728          eMode = 'x';  /* spreadsheet */
8729        }else if( c!='e' && strcmp(z,"-e")==0 ){
8730          eMode = 'e';  /* text editor */
8731        }else{
8732          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8733                      azArg[i]);
8734          showHelp(p->out, azArg[0]);
8735          rc = 1;
8736          goto meta_command_exit;
8737        }
8738      }else if( zFile==0 ){
8739        zFile = z;
8740      }else{
8741        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8742                    azArg[i]);
8743        showHelp(p->out, azArg[0]);
8744        rc = 1;
8745        goto meta_command_exit;
8746      }
8747    }
8748    if( zFile==0 ) zFile = "stdout";
8749    if( bOnce ){
8750      p->outCount = 2;
8751    }else{
8752      p->outCount = 0;
8753    }
8754    output_reset(p);
8755#ifndef SQLITE_NOHAVE_SYSTEM
8756    if( eMode=='e' || eMode=='x' ){
8757      p->doXdgOpen = 1;
8758      outputModePush(p);
8759      if( eMode=='x' ){
8760        /* spreadsheet mode.  Output as CSV. */
8761        newTempFile(p, "csv");
8762        ShellClearFlag(p, SHFLG_Echo);
8763        p->mode = MODE_Csv;
8764        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8765        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8766      }else{
8767        /* text editor mode */
8768        newTempFile(p, "txt");
8769        bTxtMode = 1;
8770      }
8771      zFile = p->zTempFile;
8772    }
8773#endif /* SQLITE_NOHAVE_SYSTEM */
8774    if( zFile[0]=='|' ){
8775#ifdef SQLITE_OMIT_POPEN
8776      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8777      rc = 1;
8778      p->out = stdout;
8779#else
8780      p->out = popen(zFile + 1, "w");
8781      if( p->out==0 ){
8782        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8783        p->out = stdout;
8784        rc = 1;
8785      }else{
8786        if( bBOM ) fprintf(p->out,"\357\273\277");
8787        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8788      }
8789#endif
8790    }else{
8791      p->out = output_file_open(zFile, bTxtMode);
8792      if( p->out==0 ){
8793        if( strcmp(zFile,"off")!=0 ){
8794          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8795        }
8796        p->out = stdout;
8797        rc = 1;
8798      } else {
8799        if( bBOM ) fprintf(p->out,"\357\273\277");
8800        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8801      }
8802    }
8803  }else
8804
8805  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8806    open_db(p,0);
8807    if( nArg<=1 ) goto parameter_syntax_error;
8808
8809    /* .parameter clear
8810    ** Clear all bind parameters by dropping the TEMP table that holds them.
8811    */
8812    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8813      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8814                   0, 0, 0);
8815    }else
8816
8817    /* .parameter list
8818    ** List all bind parameters.
8819    */
8820    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8821      sqlite3_stmt *pStmt = 0;
8822      int rx;
8823      int len = 0;
8824      rx = sqlite3_prepare_v2(p->db,
8825             "SELECT max(length(key)) "
8826             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8827      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8828        len = sqlite3_column_int(pStmt, 0);
8829        if( len>40 ) len = 40;
8830      }
8831      sqlite3_finalize(pStmt);
8832      pStmt = 0;
8833      if( len ){
8834        rx = sqlite3_prepare_v2(p->db,
8835             "SELECT key, quote(value) "
8836             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8837        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8838          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8839                      sqlite3_column_text(pStmt,1));
8840        }
8841        sqlite3_finalize(pStmt);
8842      }
8843    }else
8844
8845    /* .parameter init
8846    ** Make sure the TEMP table used to hold bind parameters exists.
8847    ** Create it if necessary.
8848    */
8849    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8850      bind_table_init(p);
8851    }else
8852
8853    /* .parameter set NAME VALUE
8854    ** Set or reset a bind parameter.  NAME should be the full parameter
8855    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8856    ** VALUE can be in either SQL literal notation, or if not it will be
8857    ** understood to be a text string.
8858    */
8859    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8860      int rx;
8861      char *zSql;
8862      sqlite3_stmt *pStmt;
8863      const char *zKey = azArg[2];
8864      const char *zValue = azArg[3];
8865      bind_table_init(p);
8866      zSql = sqlite3_mprintf(
8867                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8868                  "VALUES(%Q,%s);", zKey, zValue);
8869      if( zSql==0 ) shell_out_of_memory();
8870      pStmt = 0;
8871      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8872      sqlite3_free(zSql);
8873      if( rx!=SQLITE_OK ){
8874        sqlite3_finalize(pStmt);
8875        pStmt = 0;
8876        zSql = sqlite3_mprintf(
8877                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8878                   "VALUES(%Q,%Q);", zKey, zValue);
8879        if( zSql==0 ) shell_out_of_memory();
8880        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8881        sqlite3_free(zSql);
8882        if( rx!=SQLITE_OK ){
8883          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8884          sqlite3_finalize(pStmt);
8885          pStmt = 0;
8886          rc = 1;
8887        }
8888      }
8889      sqlite3_step(pStmt);
8890      sqlite3_finalize(pStmt);
8891    }else
8892
8893    /* .parameter unset NAME
8894    ** Remove the NAME binding from the parameter binding table, if it
8895    ** exists.
8896    */
8897    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8898      char *zSql = sqlite3_mprintf(
8899          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8900      if( zSql==0 ) shell_out_of_memory();
8901      sqlite3_exec(p->db, zSql, 0, 0, 0);
8902      sqlite3_free(zSql);
8903    }else
8904    /* If no command name matches, show a syntax error */
8905    parameter_syntax_error:
8906    showHelp(p->out, "parameter");
8907  }else
8908
8909  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8910    int i;
8911    for(i=1; i<nArg; i++){
8912      if( i>1 ) raw_printf(p->out, " ");
8913      utf8_printf(p->out, "%s", azArg[i]);
8914    }
8915    raw_printf(p->out, "\n");
8916  }else
8917
8918#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8919  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8920    int i;
8921    int nn = 0;
8922    p->flgProgress = 0;
8923    p->mxProgress = 0;
8924    p->nProgress = 0;
8925    for(i=1; i<nArg; i++){
8926      const char *z = azArg[i];
8927      if( z[0]=='-' ){
8928        z++;
8929        if( z[0]=='-' ) z++;
8930        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
8931          p->flgProgress |= SHELL_PROGRESS_QUIET;
8932          continue;
8933        }
8934        if( strcmp(z,"reset")==0 ){
8935          p->flgProgress |= SHELL_PROGRESS_RESET;
8936          continue;
8937        }
8938        if( strcmp(z,"once")==0 ){
8939          p->flgProgress |= SHELL_PROGRESS_ONCE;
8940          continue;
8941        }
8942        if( strcmp(z,"limit")==0 ){
8943          if( i+1>=nArg ){
8944            utf8_printf(stderr, "Error: missing argument on --limit\n");
8945            rc = 1;
8946            goto meta_command_exit;
8947          }else{
8948            p->mxProgress = (int)integerValue(azArg[++i]);
8949          }
8950          continue;
8951        }
8952        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
8953        rc = 1;
8954        goto meta_command_exit;
8955      }else{
8956        nn = (int)integerValue(z);
8957      }
8958    }
8959    open_db(p, 0);
8960    sqlite3_progress_handler(p->db, nn, progress_handler, p);
8961  }else
8962#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
8963
8964  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
8965    if( nArg >= 2) {
8966      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
8967    }
8968    if( nArg >= 3) {
8969      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
8970    }
8971  }else
8972
8973  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
8974    rc = 2;
8975  }else
8976
8977  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
8978    FILE *inSaved = p->in;
8979    int savedLineno = p->lineno;
8980    if( nArg!=2 ){
8981      raw_printf(stderr, "Usage: .read FILE\n");
8982      rc = 1;
8983      goto meta_command_exit;
8984    }
8985    if( azArg[1][0]=='|' ){
8986      p->in = popen(azArg[1]+1, "r");
8987      if( p->in==0 ){
8988        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8989        rc = 1;
8990      }else{
8991        rc = process_input(p);
8992        pclose(p->in);
8993      }
8994    }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
8995      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
8996      rc = 1;
8997    }else{
8998      rc = process_input(p);
8999      fclose(p->in);
9000    }
9001    p->in = inSaved;
9002    p->lineno = savedLineno;
9003  }else
9004
9005  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9006    const char *zSrcFile;
9007    const char *zDb;
9008    sqlite3 *pSrc;
9009    sqlite3_backup *pBackup;
9010    int nTimeout = 0;
9011
9012    if( nArg==2 ){
9013      zSrcFile = azArg[1];
9014      zDb = "main";
9015    }else if( nArg==3 ){
9016      zSrcFile = azArg[2];
9017      zDb = azArg[1];
9018    }else{
9019      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9020      rc = 1;
9021      goto meta_command_exit;
9022    }
9023    rc = sqlite3_open(zSrcFile, &pSrc);
9024    if( rc!=SQLITE_OK ){
9025      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9026      close_db(pSrc);
9027      return 1;
9028    }
9029    open_db(p, 0);
9030    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9031    if( pBackup==0 ){
9032      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9033      close_db(pSrc);
9034      return 1;
9035    }
9036    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9037          || rc==SQLITE_BUSY  ){
9038      if( rc==SQLITE_BUSY ){
9039        if( nTimeout++ >= 3 ) break;
9040        sqlite3_sleep(100);
9041      }
9042    }
9043    sqlite3_backup_finish(pBackup);
9044    if( rc==SQLITE_DONE ){
9045      rc = 0;
9046    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9047      raw_printf(stderr, "Error: source database is busy\n");
9048      rc = 1;
9049    }else{
9050      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9051      rc = 1;
9052    }
9053    close_db(pSrc);
9054  }else
9055
9056  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9057    if( nArg==2 ){
9058      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9059#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9060      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9061#endif
9062    }else{
9063      raw_printf(stderr, "Usage: .scanstats on|off\n");
9064      rc = 1;
9065    }
9066  }else
9067
9068  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9069    ShellText sSelect;
9070    ShellState data;
9071    char *zErrMsg = 0;
9072    const char *zDiv = "(";
9073    const char *zName = 0;
9074    int iSchema = 0;
9075    int bDebug = 0;
9076    int ii;
9077
9078    open_db(p, 0);
9079    memcpy(&data, p, sizeof(data));
9080    data.showHeader = 0;
9081    data.cMode = data.mode = MODE_Semi;
9082    initText(&sSelect);
9083    for(ii=1; ii<nArg; ii++){
9084      if( optionMatch(azArg[ii],"indent") ){
9085        data.cMode = data.mode = MODE_Pretty;
9086      }else if( optionMatch(azArg[ii],"debug") ){
9087        bDebug = 1;
9088      }else if( zName==0 ){
9089        zName = azArg[ii];
9090      }else{
9091        raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
9092        rc = 1;
9093        goto meta_command_exit;
9094      }
9095    }
9096    if( zName!=0 ){
9097      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9098                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9099                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9100                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9101      if( isSchema ){
9102        char *new_argv[2], *new_colv[2];
9103        new_argv[0] = sqlite3_mprintf(
9104                      "CREATE TABLE %s (\n"
9105                      "  type text,\n"
9106                      "  name text,\n"
9107                      "  tbl_name text,\n"
9108                      "  rootpage integer,\n"
9109                      "  sql text\n"
9110                      ")", zName);
9111        new_argv[1] = 0;
9112        new_colv[0] = "sql";
9113        new_colv[1] = 0;
9114        callback(&data, 1, new_argv, new_colv);
9115        sqlite3_free(new_argv[0]);
9116      }
9117    }
9118    if( zDiv ){
9119      sqlite3_stmt *pStmt = 0;
9120      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9121                              -1, &pStmt, 0);
9122      if( rc ){
9123        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9124        sqlite3_finalize(pStmt);
9125        rc = 1;
9126        goto meta_command_exit;
9127      }
9128      appendText(&sSelect, "SELECT sql FROM", 0);
9129      iSchema = 0;
9130      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9131        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9132        char zScNum[30];
9133        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9134        appendText(&sSelect, zDiv, 0);
9135        zDiv = " UNION ALL ";
9136        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9137        if( sqlite3_stricmp(zDb, "main")!=0 ){
9138          appendText(&sSelect, zDb, '\'');
9139        }else{
9140          appendText(&sSelect, "NULL", 0);
9141        }
9142        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9143        appendText(&sSelect, zScNum, 0);
9144        appendText(&sSelect, " AS snum, ", 0);
9145        appendText(&sSelect, zDb, '\'');
9146        appendText(&sSelect, " AS sname FROM ", 0);
9147        appendText(&sSelect, zDb, quoteChar(zDb));
9148        appendText(&sSelect, ".sqlite_schema", 0);
9149      }
9150      sqlite3_finalize(pStmt);
9151#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9152      if( zName ){
9153        appendText(&sSelect,
9154           " UNION ALL SELECT shell_module_schema(name),"
9155           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9156        0);
9157      }
9158#endif
9159      appendText(&sSelect, ") WHERE ", 0);
9160      if( zName ){
9161        char *zQarg = sqlite3_mprintf("%Q", zName);
9162        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9163                    strchr(zName, '[') != 0;
9164        if( strchr(zName, '.') ){
9165          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9166        }else{
9167          appendText(&sSelect, "lower(tbl_name)", 0);
9168        }
9169        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9170        appendText(&sSelect, zQarg, 0);
9171        if( !bGlob ){
9172          appendText(&sSelect, " ESCAPE '\\' ", 0);
9173        }
9174        appendText(&sSelect, " AND ", 0);
9175        sqlite3_free(zQarg);
9176      }
9177      appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
9178                           " ORDER BY snum, rowid", 0);
9179      if( bDebug ){
9180        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9181      }else{
9182        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9183      }
9184      freeText(&sSelect);
9185    }
9186    if( zErrMsg ){
9187      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9188      sqlite3_free(zErrMsg);
9189      rc = 1;
9190    }else if( rc != SQLITE_OK ){
9191      raw_printf(stderr,"Error: querying schema information\n");
9192      rc = 1;
9193    }else{
9194      rc = 0;
9195    }
9196  }else
9197
9198#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9199  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9200    sqlite3_unsupported_selecttrace = nArg>=2 ? (int)integerValue(azArg[1]) : 0xffff;
9201  }else
9202#endif
9203
9204#if defined(SQLITE_ENABLE_SESSION)
9205  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9206    OpenSession *pSession = &p->aSession[0];
9207    char **azCmd = &azArg[1];
9208    int iSes = 0;
9209    int nCmd = nArg - 1;
9210    int i;
9211    if( nArg<=1 ) goto session_syntax_error;
9212    open_db(p, 0);
9213    if( nArg>=3 ){
9214      for(iSes=0; iSes<p->nSession; iSes++){
9215        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
9216      }
9217      if( iSes<p->nSession ){
9218        pSession = &p->aSession[iSes];
9219        azCmd++;
9220        nCmd--;
9221      }else{
9222        pSession = &p->aSession[0];
9223        iSes = 0;
9224      }
9225    }
9226
9227    /* .session attach TABLE
9228    ** Invoke the sqlite3session_attach() interface to attach a particular
9229    ** table so that it is never filtered.
9230    */
9231    if( strcmp(azCmd[0],"attach")==0 ){
9232      if( nCmd!=2 ) goto session_syntax_error;
9233      if( pSession->p==0 ){
9234        session_not_open:
9235        raw_printf(stderr, "ERROR: No sessions are open\n");
9236      }else{
9237        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9238        if( rc ){
9239          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9240          rc = 0;
9241        }
9242      }
9243    }else
9244
9245    /* .session changeset FILE
9246    ** .session patchset FILE
9247    ** Write a changeset or patchset into a file.  The file is overwritten.
9248    */
9249    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9250      FILE *out = 0;
9251      if( nCmd!=2 ) goto session_syntax_error;
9252      if( pSession->p==0 ) goto session_not_open;
9253      out = fopen(azCmd[1], "wb");
9254      if( out==0 ){
9255        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9256                    azCmd[1]);
9257      }else{
9258        int szChng;
9259        void *pChng;
9260        if( azCmd[0][0]=='c' ){
9261          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9262        }else{
9263          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9264        }
9265        if( rc ){
9266          printf("Error: error code %d\n", rc);
9267          rc = 0;
9268        }
9269        if( pChng
9270          && fwrite(pChng, szChng, 1, out)!=1 ){
9271          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9272                  szChng);
9273        }
9274        sqlite3_free(pChng);
9275        fclose(out);
9276      }
9277    }else
9278
9279    /* .session close
9280    ** Close the identified session
9281    */
9282    if( strcmp(azCmd[0], "close")==0 ){
9283      if( nCmd!=1 ) goto session_syntax_error;
9284      if( p->nSession ){
9285        session_close(pSession);
9286        p->aSession[iSes] = p->aSession[--p->nSession];
9287      }
9288    }else
9289
9290    /* .session enable ?BOOLEAN?
9291    ** Query or set the enable flag
9292    */
9293    if( strcmp(azCmd[0], "enable")==0 ){
9294      int ii;
9295      if( nCmd>2 ) goto session_syntax_error;
9296      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9297      if( p->nSession ){
9298        ii = sqlite3session_enable(pSession->p, ii);
9299        utf8_printf(p->out, "session %s enable flag = %d\n",
9300                    pSession->zName, ii);
9301      }
9302    }else
9303
9304    /* .session filter GLOB ....
9305    ** Set a list of GLOB patterns of table names to be excluded.
9306    */
9307    if( strcmp(azCmd[0], "filter")==0 ){
9308      int ii, nByte;
9309      if( nCmd<2 ) goto session_syntax_error;
9310      if( p->nSession ){
9311        for(ii=0; ii<pSession->nFilter; ii++){
9312          sqlite3_free(pSession->azFilter[ii]);
9313        }
9314        sqlite3_free(pSession->azFilter);
9315        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9316        pSession->azFilter = sqlite3_malloc( nByte );
9317        if( pSession->azFilter==0 ){
9318          raw_printf(stderr, "Error: out or memory\n");
9319          exit(1);
9320        }
9321        for(ii=1; ii<nCmd; ii++){
9322          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9323        }
9324        pSession->nFilter = ii-1;
9325      }
9326    }else
9327
9328    /* .session indirect ?BOOLEAN?
9329    ** Query or set the indirect flag
9330    */
9331    if( strcmp(azCmd[0], "indirect")==0 ){
9332      int ii;
9333      if( nCmd>2 ) goto session_syntax_error;
9334      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9335      if( p->nSession ){
9336        ii = sqlite3session_indirect(pSession->p, ii);
9337        utf8_printf(p->out, "session %s indirect flag = %d\n",
9338                    pSession->zName, ii);
9339      }
9340    }else
9341
9342    /* .session isempty
9343    ** Determine if the session is empty
9344    */
9345    if( strcmp(azCmd[0], "isempty")==0 ){
9346      int ii;
9347      if( nCmd!=1 ) goto session_syntax_error;
9348      if( p->nSession ){
9349        ii = sqlite3session_isempty(pSession->p);
9350        utf8_printf(p->out, "session %s isempty flag = %d\n",
9351                    pSession->zName, ii);
9352      }
9353    }else
9354
9355    /* .session list
9356    ** List all currently open sessions
9357    */
9358    if( strcmp(azCmd[0],"list")==0 ){
9359      for(i=0; i<p->nSession; i++){
9360        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
9361      }
9362    }else
9363
9364    /* .session open DB NAME
9365    ** Open a new session called NAME on the attached database DB.
9366    ** DB is normally "main".
9367    */
9368    if( strcmp(azCmd[0],"open")==0 ){
9369      char *zName;
9370      if( nCmd!=3 ) goto session_syntax_error;
9371      zName = azCmd[2];
9372      if( zName[0]==0 ) goto session_syntax_error;
9373      for(i=0; i<p->nSession; i++){
9374        if( strcmp(p->aSession[i].zName,zName)==0 ){
9375          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9376          goto meta_command_exit;
9377        }
9378      }
9379      if( p->nSession>=ArraySize(p->aSession) ){
9380        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9381        goto meta_command_exit;
9382      }
9383      pSession = &p->aSession[p->nSession];
9384      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9385      if( rc ){
9386        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9387        rc = 0;
9388        goto meta_command_exit;
9389      }
9390      pSession->nFilter = 0;
9391      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9392      p->nSession++;
9393      pSession->zName = sqlite3_mprintf("%s", zName);
9394    }else
9395    /* If no command name matches, show a syntax error */
9396    session_syntax_error:
9397    showHelp(p->out, "session");
9398  }else
9399#endif
9400
9401#ifdef SQLITE_DEBUG
9402  /* Undocumented commands for internal testing.  Subject to change
9403  ** without notice. */
9404  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9405    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9406      int i, v;
9407      for(i=1; i<nArg; i++){
9408        v = booleanValue(azArg[i]);
9409        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9410      }
9411    }
9412    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9413      int i; sqlite3_int64 v;
9414      for(i=1; i<nArg; i++){
9415        char zBuf[200];
9416        v = integerValue(azArg[i]);
9417        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9418        utf8_printf(p->out, "%s", zBuf);
9419      }
9420    }
9421  }else
9422#endif
9423
9424  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9425    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9426    int bVerbose = 0;        /* Verbose output */
9427    int bSelftestExists;     /* True if SELFTEST already exists */
9428    int i, k;                /* Loop counters */
9429    int nTest = 0;           /* Number of tests runs */
9430    int nErr = 0;            /* Number of errors seen */
9431    ShellText str;           /* Answer for a query */
9432    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9433
9434    open_db(p,0);
9435    for(i=1; i<nArg; i++){
9436      const char *z = azArg[i];
9437      if( z[0]=='-' && z[1]=='-' ) z++;
9438      if( strcmp(z,"-init")==0 ){
9439        bIsInit = 1;
9440      }else
9441      if( strcmp(z,"-v")==0 ){
9442        bVerbose++;
9443      }else
9444      {
9445        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9446                    azArg[i], azArg[0]);
9447        raw_printf(stderr, "Should be one of: --init -v\n");
9448        rc = 1;
9449        goto meta_command_exit;
9450      }
9451    }
9452    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9453           != SQLITE_OK ){
9454      bSelftestExists = 0;
9455    }else{
9456      bSelftestExists = 1;
9457    }
9458    if( bIsInit ){
9459      createSelftestTable(p);
9460      bSelftestExists = 1;
9461    }
9462    initText(&str);
9463    appendText(&str, "x", 0);
9464    for(k=bSelftestExists; k>=0; k--){
9465      if( k==1 ){
9466        rc = sqlite3_prepare_v2(p->db,
9467            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9468            -1, &pStmt, 0);
9469      }else{
9470        rc = sqlite3_prepare_v2(p->db,
9471          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9472          "      (1,'run','PRAGMA integrity_check','ok')",
9473          -1, &pStmt, 0);
9474      }
9475      if( rc ){
9476        raw_printf(stderr, "Error querying the selftest table\n");
9477        rc = 1;
9478        sqlite3_finalize(pStmt);
9479        goto meta_command_exit;
9480      }
9481      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9482        int tno = sqlite3_column_int(pStmt, 0);
9483        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9484        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9485        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9486
9487        k = 0;
9488        if( bVerbose>0 ){
9489          char *zQuote = sqlite3_mprintf("%q", zSql);
9490          printf("%d: %s %s\n", tno, zOp, zSql);
9491          sqlite3_free(zQuote);
9492        }
9493        if( strcmp(zOp,"memo")==0 ){
9494          utf8_printf(p->out, "%s\n", zSql);
9495        }else
9496        if( strcmp(zOp,"run")==0 ){
9497          char *zErrMsg = 0;
9498          str.n = 0;
9499          str.z[0] = 0;
9500          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9501          nTest++;
9502          if( bVerbose ){
9503            utf8_printf(p->out, "Result: %s\n", str.z);
9504          }
9505          if( rc || zErrMsg ){
9506            nErr++;
9507            rc = 1;
9508            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9509            sqlite3_free(zErrMsg);
9510          }else if( strcmp(zAns,str.z)!=0 ){
9511            nErr++;
9512            rc = 1;
9513            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9514            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9515          }
9516        }else
9517        {
9518          utf8_printf(stderr,
9519            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9520          rc = 1;
9521          break;
9522        }
9523      } /* End loop over rows of content from SELFTEST */
9524      sqlite3_finalize(pStmt);
9525    } /* End loop over k */
9526    freeText(&str);
9527    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9528  }else
9529
9530  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9531    if( nArg<2 || nArg>3 ){
9532      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9533      rc = 1;
9534    }
9535    if( nArg>=2 ){
9536      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9537                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9538    }
9539    if( nArg>=3 ){
9540      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9541                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9542    }
9543  }else
9544
9545  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9546    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9547    int i;                   /* Loop counter */
9548    int bSchema = 0;         /* Also hash the schema */
9549    int bSeparate = 0;       /* Hash each table separately */
9550    int iSize = 224;         /* Hash algorithm to use */
9551    int bDebug = 0;          /* Only show the query that would have run */
9552    sqlite3_stmt *pStmt;     /* For querying tables names */
9553    char *zSql;              /* SQL to be run */
9554    char *zSep;              /* Separator */
9555    ShellText sSql;          /* Complete SQL for the query to run the hash */
9556    ShellText sQuery;        /* Set of queries used to read all content */
9557    open_db(p, 0);
9558    for(i=1; i<nArg; i++){
9559      const char *z = azArg[i];
9560      if( z[0]=='-' ){
9561        z++;
9562        if( z[0]=='-' ) z++;
9563        if( strcmp(z,"schema")==0 ){
9564          bSchema = 1;
9565        }else
9566        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9567         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9568        ){
9569          iSize = atoi(&z[5]);
9570        }else
9571        if( strcmp(z,"debug")==0 ){
9572          bDebug = 1;
9573        }else
9574        {
9575          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9576                      azArg[i], azArg[0]);
9577          showHelp(p->out, azArg[0]);
9578          rc = 1;
9579          goto meta_command_exit;
9580        }
9581      }else if( zLike ){
9582        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9583        rc = 1;
9584        goto meta_command_exit;
9585      }else{
9586        zLike = z;
9587        bSeparate = 1;
9588        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9589      }
9590    }
9591    if( bSchema ){
9592      zSql = "SELECT lower(name) FROM sqlite_schema"
9593             " WHERE type='table' AND coalesce(rootpage,0)>1"
9594             " UNION ALL SELECT 'sqlite_schema'"
9595             " ORDER BY 1 collate nocase";
9596    }else{
9597      zSql = "SELECT lower(name) FROM sqlite_schema"
9598             " WHERE type='table' AND coalesce(rootpage,0)>1"
9599             " AND name NOT LIKE 'sqlite_%'"
9600             " ORDER BY 1 collate nocase";
9601    }
9602    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9603    initText(&sQuery);
9604    initText(&sSql);
9605    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9606    zSep = "VALUES(";
9607    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9608      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9609      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9610      if( strncmp(zTab, "sqlite_",7)!=0 ){
9611        appendText(&sQuery,"SELECT * FROM ", 0);
9612        appendText(&sQuery,zTab,'"');
9613        appendText(&sQuery," NOT INDEXED;", 0);
9614      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9615        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9616                           " ORDER BY name;", 0);
9617      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9618        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9619                           " ORDER BY name;", 0);
9620      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9621        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9622                           " ORDER BY tbl,idx;", 0);
9623      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9624        appendText(&sQuery, "SELECT * FROM ", 0);
9625        appendText(&sQuery, zTab, 0);
9626        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9627      }
9628      appendText(&sSql, zSep, 0);
9629      appendText(&sSql, sQuery.z, '\'');
9630      sQuery.n = 0;
9631      appendText(&sSql, ",", 0);
9632      appendText(&sSql, zTab, '\'');
9633      zSep = "),(";
9634    }
9635    sqlite3_finalize(pStmt);
9636    if( bSeparate ){
9637      zSql = sqlite3_mprintf(
9638          "%s))"
9639          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9640          "   FROM [sha3sum$query]",
9641          sSql.z, iSize);
9642    }else{
9643      zSql = sqlite3_mprintf(
9644          "%s))"
9645          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9646          "   FROM [sha3sum$query]",
9647          sSql.z, iSize);
9648    }
9649    freeText(&sQuery);
9650    freeText(&sSql);
9651    if( bDebug ){
9652      utf8_printf(p->out, "%s\n", zSql);
9653    }else{
9654      shell_exec(p, zSql, 0);
9655    }
9656    sqlite3_free(zSql);
9657  }else
9658
9659#ifndef SQLITE_NOHAVE_SYSTEM
9660  if( c=='s'
9661   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9662  ){
9663    char *zCmd;
9664    int i, x;
9665    if( nArg<2 ){
9666      raw_printf(stderr, "Usage: .system COMMAND\n");
9667      rc = 1;
9668      goto meta_command_exit;
9669    }
9670    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9671    for(i=2; i<nArg; i++){
9672      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9673                             zCmd, azArg[i]);
9674    }
9675    x = system(zCmd);
9676    sqlite3_free(zCmd);
9677    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9678  }else
9679#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9680
9681  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9682    static const char *azBool[] = { "off", "on", "trigger", "full"};
9683    int i;
9684    if( nArg!=1 ){
9685      raw_printf(stderr, "Usage: .show\n");
9686      rc = 1;
9687      goto meta_command_exit;
9688    }
9689    utf8_printf(p->out, "%12.12s: %s\n","echo",
9690                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9691    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9692    utf8_printf(p->out, "%12.12s: %s\n","explain",
9693         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9694    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9695    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9696    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9697      output_c_string(p->out, p->nullValue);
9698      raw_printf(p->out, "\n");
9699    utf8_printf(p->out,"%12.12s: %s\n","output",
9700            strlen30(p->outfile) ? p->outfile : "stdout");
9701    utf8_printf(p->out,"%12.12s: ", "colseparator");
9702      output_c_string(p->out, p->colSeparator);
9703      raw_printf(p->out, "\n");
9704    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9705      output_c_string(p->out, p->rowSeparator);
9706      raw_printf(p->out, "\n");
9707    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
9708    utf8_printf(p->out, "%12.12s: ", "width");
9709    for (i=0;i<p->nWidth;i++) {
9710      raw_printf(p->out, "%d ", p->colWidth[i]);
9711    }
9712    raw_printf(p->out, "\n");
9713    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9714                p->zDbFilename ? p->zDbFilename : "");
9715  }else
9716
9717  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9718    if( nArg==2 ){
9719      p->statsOn = (u8)booleanValue(azArg[1]);
9720    }else if( nArg==1 ){
9721      display_stats(p->db, p, 0);
9722    }else{
9723      raw_printf(stderr, "Usage: .stats ?on|off?\n");
9724      rc = 1;
9725    }
9726  }else
9727
9728  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9729   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9730                 || strncmp(azArg[0], "indexes", n)==0) )
9731  ){
9732    sqlite3_stmt *pStmt;
9733    char **azResult;
9734    int nRow, nAlloc;
9735    int ii;
9736    ShellText s;
9737    initText(&s);
9738    open_db(p, 0);
9739    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9740    if( rc ){
9741      sqlite3_finalize(pStmt);
9742      return shellDatabaseError(p->db);
9743    }
9744
9745    if( nArg>2 && c=='i' ){
9746      /* It is an historical accident that the .indexes command shows an error
9747      ** when called with the wrong number of arguments whereas the .tables
9748      ** command does not. */
9749      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9750      rc = 1;
9751      sqlite3_finalize(pStmt);
9752      goto meta_command_exit;
9753    }
9754    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9755      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9756      if( zDbName==0 ) continue;
9757      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9758      if( sqlite3_stricmp(zDbName, "main")==0 ){
9759        appendText(&s, "SELECT name FROM ", 0);
9760      }else{
9761        appendText(&s, "SELECT ", 0);
9762        appendText(&s, zDbName, '\'');
9763        appendText(&s, "||'.'||name FROM ", 0);
9764      }
9765      appendText(&s, zDbName, '"');
9766      appendText(&s, ".sqlite_schema ", 0);
9767      if( c=='t' ){
9768        appendText(&s," WHERE type IN ('table','view')"
9769                      "   AND name NOT LIKE 'sqlite_%'"
9770                      "   AND name LIKE ?1", 0);
9771      }else{
9772        appendText(&s," WHERE type='index'"
9773                      "   AND tbl_name LIKE ?1", 0);
9774      }
9775    }
9776    rc = sqlite3_finalize(pStmt);
9777    appendText(&s, " ORDER BY 1", 0);
9778    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9779    freeText(&s);
9780    if( rc ) return shellDatabaseError(p->db);
9781
9782    /* Run the SQL statement prepared by the above block. Store the results
9783    ** as an array of nul-terminated strings in azResult[].  */
9784    nRow = nAlloc = 0;
9785    azResult = 0;
9786    if( nArg>1 ){
9787      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9788    }else{
9789      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9790    }
9791    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9792      if( nRow>=nAlloc ){
9793        char **azNew;
9794        int n2 = nAlloc*2 + 10;
9795        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9796        if( azNew==0 ) shell_out_of_memory();
9797        nAlloc = n2;
9798        azResult = azNew;
9799      }
9800      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9801      if( 0==azResult[nRow] ) shell_out_of_memory();
9802      nRow++;
9803    }
9804    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9805      rc = shellDatabaseError(p->db);
9806    }
9807
9808    /* Pretty-print the contents of array azResult[] to the output */
9809    if( rc==0 && nRow>0 ){
9810      int len, maxlen = 0;
9811      int i, j;
9812      int nPrintCol, nPrintRow;
9813      for(i=0; i<nRow; i++){
9814        len = strlen30(azResult[i]);
9815        if( len>maxlen ) maxlen = len;
9816      }
9817      nPrintCol = 80/(maxlen+2);
9818      if( nPrintCol<1 ) nPrintCol = 1;
9819      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9820      for(i=0; i<nPrintRow; i++){
9821        for(j=i; j<nRow; j+=nPrintRow){
9822          char *zSp = j<nPrintRow ? "" : "  ";
9823          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9824                      azResult[j] ? azResult[j]:"");
9825        }
9826        raw_printf(p->out, "\n");
9827      }
9828    }
9829
9830    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9831    sqlite3_free(azResult);
9832  }else
9833
9834  /* Begin redirecting output to the file "testcase-out.txt" */
9835  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9836    output_reset(p);
9837    p->out = output_file_open("testcase-out.txt", 0);
9838    if( p->out==0 ){
9839      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9840    }
9841    if( nArg>=2 ){
9842      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9843    }else{
9844      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9845    }
9846  }else
9847
9848#ifndef SQLITE_UNTESTABLE
9849  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9850    static const struct {
9851       const char *zCtrlName;   /* Name of a test-control option */
9852       int ctrlCode;            /* Integer code for that option */
9853       const char *zUsage;      /* Usage notes */
9854    } aCtrl[] = {
9855      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9856      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9857    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9858    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9859      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9860      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9861    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9862      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9863      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9864      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9865      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9866      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9867#ifdef YYCOVERAGE
9868      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9869#endif
9870      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9871      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9872      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9873      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9874    };
9875    int testctrl = -1;
9876    int iCtrl = -1;
9877    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9878    int isOk = 0;
9879    int i, n2;
9880    const char *zCmd = 0;
9881
9882    open_db(p, 0);
9883    zCmd = nArg>=2 ? azArg[1] : "help";
9884
9885    /* The argument can optionally begin with "-" or "--" */
9886    if( zCmd[0]=='-' && zCmd[1] ){
9887      zCmd++;
9888      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9889    }
9890
9891    /* --help lists all test-controls */
9892    if( strcmp(zCmd,"help")==0 ){
9893      utf8_printf(p->out, "Available test-controls:\n");
9894      for(i=0; i<ArraySize(aCtrl); i++){
9895        utf8_printf(p->out, "  .testctrl %s %s\n",
9896                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
9897      }
9898      rc = 1;
9899      goto meta_command_exit;
9900    }
9901
9902    /* convert testctrl text option to value. allow any unique prefix
9903    ** of the option name, or a numerical value. */
9904    n2 = strlen30(zCmd);
9905    for(i=0; i<ArraySize(aCtrl); i++){
9906      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
9907        if( testctrl<0 ){
9908          testctrl = aCtrl[i].ctrlCode;
9909          iCtrl = i;
9910        }else{
9911          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
9912                              "Use \".testctrl --help\" for help\n", zCmd);
9913          rc = 1;
9914          goto meta_command_exit;
9915        }
9916      }
9917    }
9918    if( testctrl<0 ){
9919      utf8_printf(stderr,"Error: unknown test-control: %s\n"
9920                         "Use \".testctrl --help\" for help\n", zCmd);
9921    }else{
9922      switch(testctrl){
9923
9924        /* sqlite3_test_control(int, db, int) */
9925        case SQLITE_TESTCTRL_OPTIMIZATIONS:
9926          if( nArg==3 ){
9927            int opt = (int)strtol(azArg[2], 0, 0);
9928            rc2 = sqlite3_test_control(testctrl, p->db, opt);
9929            isOk = 3;
9930          }
9931          break;
9932
9933        /* sqlite3_test_control(int) */
9934        case SQLITE_TESTCTRL_PRNG_SAVE:
9935        case SQLITE_TESTCTRL_PRNG_RESTORE:
9936        case SQLITE_TESTCTRL_PRNG_RESET:
9937        case SQLITE_TESTCTRL_BYTEORDER:
9938          if( nArg==2 ){
9939            rc2 = sqlite3_test_control(testctrl);
9940            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
9941          }
9942          break;
9943
9944        /* sqlite3_test_control(int, uint) */
9945        case SQLITE_TESTCTRL_PENDING_BYTE:
9946          if( nArg==3 ){
9947            unsigned int opt = (unsigned int)integerValue(azArg[2]);
9948            rc2 = sqlite3_test_control(testctrl, opt);
9949            isOk = 3;
9950          }
9951          break;
9952
9953        /* sqlite3_test_control(int, int, sqlite3*) */
9954        case SQLITE_TESTCTRL_PRNG_SEED:
9955          if( nArg==3 || nArg==4 ){
9956            int ii = (int)integerValue(azArg[2]);
9957            sqlite3 *db;
9958            if( ii==0 && strcmp(azArg[2],"random")==0 ){
9959              sqlite3_randomness(sizeof(ii),&ii);
9960              printf("-- random seed: %d\n", ii);
9961            }
9962            if( nArg==3 ){
9963              db = 0;
9964            }else{
9965              db = p->db;
9966              /* Make sure the schema has been loaded */
9967              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
9968            }
9969            rc2 = sqlite3_test_control(testctrl, ii, db);
9970            isOk = 3;
9971          }
9972          break;
9973
9974        /* sqlite3_test_control(int, int) */
9975        case SQLITE_TESTCTRL_ASSERT:
9976        case SQLITE_TESTCTRL_ALWAYS:
9977          if( nArg==3 ){
9978            int opt = booleanValue(azArg[2]);
9979            rc2 = sqlite3_test_control(testctrl, opt);
9980            isOk = 1;
9981          }
9982          break;
9983
9984        /* sqlite3_test_control(int, int) */
9985        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
9986        case SQLITE_TESTCTRL_NEVER_CORRUPT:
9987          if( nArg==3 ){
9988            int opt = booleanValue(azArg[2]);
9989            rc2 = sqlite3_test_control(testctrl, opt);
9990            isOk = 3;
9991          }
9992          break;
9993
9994        /* sqlite3_test_control(sqlite3*) */
9995        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
9996          rc2 = sqlite3_test_control(testctrl, p->db);
9997          isOk = 3;
9998          break;
9999
10000        case SQLITE_TESTCTRL_IMPOSTER:
10001          if( nArg==5 ){
10002            rc2 = sqlite3_test_control(testctrl, p->db,
10003                          azArg[2],
10004                          integerValue(azArg[3]),
10005                          integerValue(azArg[4]));
10006            isOk = 3;
10007          }
10008          break;
10009
10010#ifdef YYCOVERAGE
10011        case SQLITE_TESTCTRL_PARSER_COVERAGE:
10012          if( nArg==2 ){
10013            sqlite3_test_control(testctrl, p->out);
10014            isOk = 3;
10015          }
10016#endif
10017      }
10018    }
10019    if( isOk==0 && iCtrl>=0 ){
10020      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10021      rc = 1;
10022    }else if( isOk==1 ){
10023      raw_printf(p->out, "%d\n", rc2);
10024    }else if( isOk==2 ){
10025      raw_printf(p->out, "0x%08x\n", rc2);
10026    }
10027  }else
10028#endif /* !defined(SQLITE_UNTESTABLE) */
10029
10030  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10031    open_db(p, 0);
10032    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10033  }else
10034
10035  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10036    if( nArg==2 ){
10037      enableTimer = booleanValue(azArg[1]);
10038      if( enableTimer && !HAS_TIMER ){
10039        raw_printf(stderr, "Error: timer not available on this system.\n");
10040        enableTimer = 0;
10041      }
10042    }else{
10043      raw_printf(stderr, "Usage: .timer on|off\n");
10044      rc = 1;
10045    }
10046  }else
10047
10048#ifndef SQLITE_OMIT_TRACE
10049  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10050    int mType = 0;
10051    int jj;
10052    open_db(p, 0);
10053    for(jj=1; jj<nArg; jj++){
10054      const char *z = azArg[jj];
10055      if( z[0]=='-' ){
10056        if( optionMatch(z, "expanded") ){
10057          p->eTraceType = SHELL_TRACE_EXPANDED;
10058        }
10059#ifdef SQLITE_ENABLE_NORMALIZE
10060        else if( optionMatch(z, "normalized") ){
10061          p->eTraceType = SHELL_TRACE_NORMALIZED;
10062        }
10063#endif
10064        else if( optionMatch(z, "plain") ){
10065          p->eTraceType = SHELL_TRACE_PLAIN;
10066        }
10067        else if( optionMatch(z, "profile") ){
10068          mType |= SQLITE_TRACE_PROFILE;
10069        }
10070        else if( optionMatch(z, "row") ){
10071          mType |= SQLITE_TRACE_ROW;
10072        }
10073        else if( optionMatch(z, "stmt") ){
10074          mType |= SQLITE_TRACE_STMT;
10075        }
10076        else if( optionMatch(z, "close") ){
10077          mType |= SQLITE_TRACE_CLOSE;
10078        }
10079        else {
10080          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10081          rc = 1;
10082          goto meta_command_exit;
10083        }
10084      }else{
10085        output_file_close(p->traceOut);
10086        p->traceOut = output_file_open(azArg[1], 0);
10087      }
10088    }
10089    if( p->traceOut==0 ){
10090      sqlite3_trace_v2(p->db, 0, 0, 0);
10091    }else{
10092      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10093      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10094    }
10095  }else
10096#endif /* !defined(SQLITE_OMIT_TRACE) */
10097
10098#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10099  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10100    int ii;
10101    int lenOpt;
10102    char *zOpt;
10103    if( nArg<2 ){
10104      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10105      rc = 1;
10106      goto meta_command_exit;
10107    }
10108    open_db(p, 0);
10109    zOpt = azArg[1];
10110    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10111    lenOpt = (int)strlen(zOpt);
10112    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10113      assert( azArg[nArg]==0 );
10114      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10115    }else{
10116      for(ii=1; ii<nArg; ii++){
10117        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10118      }
10119    }
10120  }else
10121#endif
10122
10123#if SQLITE_USER_AUTHENTICATION
10124  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10125    if( nArg<2 ){
10126      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10127      rc = 1;
10128      goto meta_command_exit;
10129    }
10130    open_db(p, 0);
10131    if( strcmp(azArg[1],"login")==0 ){
10132      if( nArg!=4 ){
10133        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10134        rc = 1;
10135        goto meta_command_exit;
10136      }
10137      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10138                                     strlen30(azArg[3]));
10139      if( rc ){
10140        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10141        rc = 1;
10142      }
10143    }else if( strcmp(azArg[1],"add")==0 ){
10144      if( nArg!=5 ){
10145        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10146        rc = 1;
10147        goto meta_command_exit;
10148      }
10149      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10150                            booleanValue(azArg[4]));
10151      if( rc ){
10152        raw_printf(stderr, "User-Add failed: %d\n", rc);
10153        rc = 1;
10154      }
10155    }else if( strcmp(azArg[1],"edit")==0 ){
10156      if( nArg!=5 ){
10157        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10158        rc = 1;
10159        goto meta_command_exit;
10160      }
10161      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10162                              booleanValue(azArg[4]));
10163      if( rc ){
10164        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10165        rc = 1;
10166      }
10167    }else if( strcmp(azArg[1],"delete")==0 ){
10168      if( nArg!=3 ){
10169        raw_printf(stderr, "Usage: .user delete USER\n");
10170        rc = 1;
10171        goto meta_command_exit;
10172      }
10173      rc = sqlite3_user_delete(p->db, azArg[2]);
10174      if( rc ){
10175        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10176        rc = 1;
10177      }
10178    }else{
10179      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10180      rc = 1;
10181      goto meta_command_exit;
10182    }
10183  }else
10184#endif /* SQLITE_USER_AUTHENTICATION */
10185
10186  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10187    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10188        sqlite3_libversion(), sqlite3_sourceid());
10189#if SQLITE_HAVE_ZLIB
10190    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10191#endif
10192#define CTIMEOPT_VAL_(opt) #opt
10193#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10194#if defined(__clang__) && defined(__clang_major__)
10195    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10196                    CTIMEOPT_VAL(__clang_minor__) "."
10197                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10198#elif defined(_MSC_VER)
10199    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10200#elif defined(__GNUC__) && defined(__VERSION__)
10201    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10202#endif
10203  }else
10204
10205  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10206    const char *zDbName = nArg==2 ? azArg[1] : "main";
10207    sqlite3_vfs *pVfs = 0;
10208    if( p->db ){
10209      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10210      if( pVfs ){
10211        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10212        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10213        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10214        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10215      }
10216    }
10217  }else
10218
10219  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10220    sqlite3_vfs *pVfs;
10221    sqlite3_vfs *pCurrent = 0;
10222    if( p->db ){
10223      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10224    }
10225    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10226      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10227           pVfs==pCurrent ? "  <--- CURRENT" : "");
10228      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10229      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10230      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10231      if( pVfs->pNext ){
10232        raw_printf(p->out, "-----------------------------------\n");
10233      }
10234    }
10235  }else
10236
10237  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10238    const char *zDbName = nArg==2 ? azArg[1] : "main";
10239    char *zVfsName = 0;
10240    if( p->db ){
10241      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10242      if( zVfsName ){
10243        utf8_printf(p->out, "%s\n", zVfsName);
10244        sqlite3_free(zVfsName);
10245      }
10246    }
10247  }else
10248
10249#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10250  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10251    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
10252  }else
10253#endif
10254
10255  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10256    int j;
10257    assert( nArg<=ArraySize(azArg) );
10258    p->nWidth = nArg-1;
10259    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10260    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10261    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10262    for(j=1; j<nArg; j++){
10263      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10264    }
10265  }else
10266
10267  {
10268    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10269      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10270    rc = 1;
10271  }
10272
10273meta_command_exit:
10274  if( p->outCount ){
10275    p->outCount--;
10276    if( p->outCount==0 ) output_reset(p);
10277  }
10278  return rc;
10279}
10280
10281/*
10282** Return TRUE if a semicolon occurs anywhere in the first N characters
10283** of string z[].
10284*/
10285static int line_contains_semicolon(const char *z, int N){
10286  int i;
10287  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
10288  return 0;
10289}
10290
10291/*
10292** Test to see if a line consists entirely of whitespace.
10293*/
10294static int _all_whitespace(const char *z){
10295  for(; *z; z++){
10296    if( IsSpace(z[0]) ) continue;
10297    if( *z=='/' && z[1]=='*' ){
10298      z += 2;
10299      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
10300      if( *z==0 ) return 0;
10301      z++;
10302      continue;
10303    }
10304    if( *z=='-' && z[1]=='-' ){
10305      z += 2;
10306      while( *z && *z!='\n' ){ z++; }
10307      if( *z==0 ) return 1;
10308      continue;
10309    }
10310    return 0;
10311  }
10312  return 1;
10313}
10314
10315/*
10316** Return TRUE if the line typed in is an SQL command terminator other
10317** than a semi-colon.  The SQL Server style "go" command is understood
10318** as is the Oracle "/".
10319*/
10320static int line_is_command_terminator(const char *zLine){
10321  while( IsSpace(zLine[0]) ){ zLine++; };
10322  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
10323    return 1;  /* Oracle */
10324  }
10325  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
10326         && _all_whitespace(&zLine[2]) ){
10327    return 1;  /* SQL Server */
10328  }
10329  return 0;
10330}
10331
10332/*
10333** We need a default sqlite3_complete() implementation to use in case
10334** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10335** any arbitrary text is a complete SQL statement.  This is not very
10336** user-friendly, but it does seem to work.
10337*/
10338#ifdef SQLITE_OMIT_COMPLETE
10339#define sqlite3_complete(x) 1
10340#endif
10341
10342/*
10343** Return true if zSql is a complete SQL statement.  Return false if it
10344** ends in the middle of a string literal or C-style comment.
10345*/
10346static int line_is_complete(char *zSql, int nSql){
10347  int rc;
10348  if( zSql==0 ) return 1;
10349  zSql[nSql] = ';';
10350  zSql[nSql+1] = 0;
10351  rc = sqlite3_complete(zSql);
10352  zSql[nSql] = 0;
10353  return rc;
10354}
10355
10356/*
10357** Run a single line of SQL.  Return the number of errors.
10358*/
10359static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10360  int rc;
10361  char *zErrMsg = 0;
10362
10363  open_db(p, 0);
10364  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10365  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10366  BEGIN_TIMER;
10367  rc = shell_exec(p, zSql, &zErrMsg);
10368  END_TIMER;
10369  if( rc || zErrMsg ){
10370    char zPrefix[100];
10371    if( in!=0 || !stdin_is_interactive ){
10372      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10373                       "Error: near line %d:", startline);
10374    }else{
10375      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10376    }
10377    if( zErrMsg!=0 ){
10378      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10379      sqlite3_free(zErrMsg);
10380      zErrMsg = 0;
10381    }else{
10382      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10383    }
10384    return 1;
10385  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10386    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
10387            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
10388  }
10389  return 0;
10390}
10391
10392
10393/*
10394** Read input from *in and process it.  If *in==0 then input
10395** is interactive - the user is typing it it.  Otherwise, input
10396** is coming from a file or device.  A prompt is issued and history
10397** is saved only if input is interactive.  An interrupt signal will
10398** cause this routine to exit immediately, unless input is interactive.
10399**
10400** Return the number of errors.
10401*/
10402static int process_input(ShellState *p){
10403  char *zLine = 0;          /* A single input line */
10404  char *zSql = 0;           /* Accumulated SQL text */
10405  int nLine;                /* Length of current line */
10406  int nSql = 0;             /* Bytes of zSql[] used */
10407  int nAlloc = 0;           /* Allocated zSql[] space */
10408  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10409  int rc;                   /* Error code */
10410  int errCnt = 0;           /* Number of errors seen */
10411  int startline = 0;        /* Line number for start of current input */
10412
10413  p->lineno = 0;
10414  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10415    fflush(p->out);
10416    zLine = one_input_line(p->in, zLine, nSql>0);
10417    if( zLine==0 ){
10418      /* End of input */
10419      if( p->in==0 && stdin_is_interactive ) printf("\n");
10420      break;
10421    }
10422    if( seenInterrupt ){
10423      if( p->in!=0 ) break;
10424      seenInterrupt = 0;
10425    }
10426    p->lineno++;
10427    if( nSql==0 && _all_whitespace(zLine) ){
10428      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10429      continue;
10430    }
10431    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10432      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10433      if( zLine[0]=='.' ){
10434        rc = do_meta_command(zLine, p);
10435        if( rc==2 ){ /* exit requested */
10436          break;
10437        }else if( rc ){
10438          errCnt++;
10439        }
10440      }
10441      continue;
10442    }
10443    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10444      memcpy(zLine,";",2);
10445    }
10446    nLine = strlen30(zLine);
10447    if( nSql+nLine+2>=nAlloc ){
10448      nAlloc = nSql+nLine+100;
10449      zSql = realloc(zSql, nAlloc);
10450      if( zSql==0 ) shell_out_of_memory();
10451    }
10452    nSqlPrior = nSql;
10453    if( nSql==0 ){
10454      int i;
10455      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10456      assert( nAlloc>0 && zSql!=0 );
10457      memcpy(zSql, zLine+i, nLine+1-i);
10458      startline = p->lineno;
10459      nSql = nLine-i;
10460    }else{
10461      zSql[nSql++] = '\n';
10462      memcpy(zSql+nSql, zLine, nLine+1);
10463      nSql += nLine;
10464    }
10465    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10466                && sqlite3_complete(zSql) ){
10467      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10468      nSql = 0;
10469      if( p->outCount ){
10470        output_reset(p);
10471        p->outCount = 0;
10472      }else{
10473        clearTempFile(p);
10474      }
10475    }else if( nSql && _all_whitespace(zSql) ){
10476      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10477      nSql = 0;
10478    }
10479  }
10480  if( nSql && !_all_whitespace(zSql) ){
10481    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10482  }
10483  free(zSql);
10484  free(zLine);
10485  return errCnt>0;
10486}
10487
10488/*
10489** Return a pathname which is the user's home directory.  A
10490** 0 return indicates an error of some kind.
10491*/
10492static char *find_home_dir(int clearFlag){
10493  static char *home_dir = NULL;
10494  if( clearFlag ){
10495    free(home_dir);
10496    home_dir = 0;
10497    return 0;
10498  }
10499  if( home_dir ) return home_dir;
10500
10501#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10502     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10503  {
10504    struct passwd *pwent;
10505    uid_t uid = getuid();
10506    if( (pwent=getpwuid(uid)) != NULL) {
10507      home_dir = pwent->pw_dir;
10508    }
10509  }
10510#endif
10511
10512#if defined(_WIN32_WCE)
10513  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10514   */
10515  home_dir = "/";
10516#else
10517
10518#if defined(_WIN32) || defined(WIN32)
10519  if (!home_dir) {
10520    home_dir = getenv("USERPROFILE");
10521  }
10522#endif
10523
10524  if (!home_dir) {
10525    home_dir = getenv("HOME");
10526  }
10527
10528#if defined(_WIN32) || defined(WIN32)
10529  if (!home_dir) {
10530    char *zDrive, *zPath;
10531    int n;
10532    zDrive = getenv("HOMEDRIVE");
10533    zPath = getenv("HOMEPATH");
10534    if( zDrive && zPath ){
10535      n = strlen30(zDrive) + strlen30(zPath) + 1;
10536      home_dir = malloc( n );
10537      if( home_dir==0 ) return 0;
10538      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10539      return home_dir;
10540    }
10541    home_dir = "c:\\";
10542  }
10543#endif
10544
10545#endif /* !_WIN32_WCE */
10546
10547  if( home_dir ){
10548    int n = strlen30(home_dir) + 1;
10549    char *z = malloc( n );
10550    if( z ) memcpy(z, home_dir, n);
10551    home_dir = z;
10552  }
10553
10554  return home_dir;
10555}
10556
10557/*
10558** Read input from the file given by sqliterc_override.  Or if that
10559** parameter is NULL, take input from ~/.sqliterc
10560**
10561** Returns the number of errors.
10562*/
10563static void process_sqliterc(
10564  ShellState *p,                  /* Configuration data */
10565  const char *sqliterc_override   /* Name of config file. NULL to use default */
10566){
10567  char *home_dir = NULL;
10568  const char *sqliterc = sqliterc_override;
10569  char *zBuf = 0;
10570  FILE *inSaved = p->in;
10571  int savedLineno = p->lineno;
10572
10573  if (sqliterc == NULL) {
10574    home_dir = find_home_dir(0);
10575    if( home_dir==0 ){
10576      raw_printf(stderr, "-- warning: cannot find home directory;"
10577                      " cannot read ~/.sqliterc\n");
10578      return;
10579    }
10580    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10581    sqliterc = zBuf;
10582  }
10583  p->in = fopen(sqliterc,"rb");
10584  if( p->in ){
10585    if( stdin_is_interactive ){
10586      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10587    }
10588    process_input(p);
10589    fclose(p->in);
10590  }
10591  p->in = inSaved;
10592  p->lineno = savedLineno;
10593  sqlite3_free(zBuf);
10594}
10595
10596/*
10597** Show available command line options
10598*/
10599static const char zOptions[] =
10600#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10601  "   -A ARGS...           run \".archive ARGS\" and exit\n"
10602#endif
10603  "   -append              append the database to the end of the file\n"
10604  "   -ascii               set output mode to 'ascii'\n"
10605  "   -bail                stop after hitting an error\n"
10606  "   -batch               force batch I/O\n"
10607  "   -box                 set output mode to 'box'\n"
10608  "   -column              set output mode to 'column'\n"
10609  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
10610  "   -csv                 set output mode to 'csv'\n"
10611#if defined(SQLITE_ENABLE_DESERIALIZE)
10612  "   -deserialize         open the database using sqlite3_deserialize()\n"
10613#endif
10614  "   -echo                print commands before execution\n"
10615  "   -init FILENAME       read/process named file\n"
10616  "   -[no]header          turn headers on or off\n"
10617#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10618  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
10619#endif
10620  "   -help                show this message\n"
10621  "   -html                set output mode to HTML\n"
10622  "   -interactive         force interactive I/O\n"
10623  "   -json                set output mode to 'json'\n"
10624  "   -line                set output mode to 'line'\n"
10625  "   -list                set output mode to 'list'\n"
10626  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
10627  "   -markdown            set output mode to 'markdown'\n"
10628#if defined(SQLITE_ENABLE_DESERIALIZE)
10629  "   -maxsize N           maximum size for a --deserialize database\n"
10630#endif
10631  "   -memtrace            trace all memory allocations and deallocations\n"
10632  "   -mmap N              default mmap size set to N\n"
10633#ifdef SQLITE_ENABLE_MULTIPLEX
10634  "   -multiplex           enable the multiplexor VFS\n"
10635#endif
10636  "   -newline SEP         set output row separator. Default: '\\n'\n"
10637  "   -nofollow            refuse to open symbolic links to database files\n"
10638  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
10639  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
10640  "   -quote               set output mode to 'quote'\n"
10641  "   -readonly            open the database read-only\n"
10642  "   -separator SEP       set output column separator. Default: '|'\n"
10643#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10644  "   -sorterref SIZE      sorter references threshold size\n"
10645#endif
10646  "   -stats               print memory stats before each finalize\n"
10647  "   -table               set output mode to 'table'\n"
10648  "   -version             show SQLite version\n"
10649  "   -vfs NAME            use NAME as the default VFS\n"
10650#ifdef SQLITE_ENABLE_VFSTRACE
10651  "   -vfstrace            enable tracing of all VFS calls\n"
10652#endif
10653#ifdef SQLITE_HAVE_ZLIB
10654  "   -zip                 open the file as a ZIP Archive\n"
10655#endif
10656;
10657static void usage(int showDetail){
10658  utf8_printf(stderr,
10659      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10660      "FILENAME is the name of an SQLite database. A new database is created\n"
10661      "if the file does not previously exist.\n", Argv0);
10662  if( showDetail ){
10663    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10664  }else{
10665    raw_printf(stderr, "Use the -help option for additional information\n");
10666  }
10667  exit(1);
10668}
10669
10670/*
10671** Internal check:  Verify that the SQLite is uninitialized.  Print a
10672** error message if it is initialized.
10673*/
10674static void verify_uninitialized(void){
10675  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10676    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10677                        " initialization.\n");
10678  }
10679}
10680
10681/*
10682** Initialize the state information in data
10683*/
10684static void main_init(ShellState *data) {
10685  memset(data, 0, sizeof(*data));
10686  data->normalMode = data->cMode = data->mode = MODE_List;
10687  data->autoExplain = 1;
10688  memcpy(data->colSeparator,SEP_Column, 2);
10689  memcpy(data->rowSeparator,SEP_Row, 2);
10690  data->showHeader = 0;
10691  data->shellFlgs = SHFLG_Lookaside;
10692  verify_uninitialized();
10693  sqlite3_config(SQLITE_CONFIG_URI, 1);
10694  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10695  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10696  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10697  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10698}
10699
10700/*
10701** Output text to the console in a font that attracts extra attention.
10702*/
10703#ifdef _WIN32
10704static void printBold(const char *zText){
10705#if !SQLITE_OS_WINRT
10706  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10707  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10708  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10709  SetConsoleTextAttribute(out,
10710         FOREGROUND_RED|FOREGROUND_INTENSITY
10711  );
10712#endif
10713  printf("%s", zText);
10714#if !SQLITE_OS_WINRT
10715  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10716#endif
10717}
10718#else
10719static void printBold(const char *zText){
10720  printf("\033[1m%s\033[0m", zText);
10721}
10722#endif
10723
10724/*
10725** Get the argument to an --option.  Throw an error and die if no argument
10726** is available.
10727*/
10728static char *cmdline_option_value(int argc, char **argv, int i){
10729  if( i==argc ){
10730    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10731            argv[0], argv[argc-1]);
10732    exit(1);
10733  }
10734  return argv[i];
10735}
10736
10737#ifndef SQLITE_SHELL_IS_UTF8
10738#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
10739#    define SQLITE_SHELL_IS_UTF8          (0)
10740#  else
10741#    define SQLITE_SHELL_IS_UTF8          (1)
10742#  endif
10743#endif
10744
10745#if SQLITE_SHELL_IS_UTF8
10746int SQLITE_CDECL main(int argc, char **argv){
10747#else
10748int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10749  char **argv;
10750#endif
10751  char *zErrMsg = 0;
10752  ShellState data;
10753  const char *zInitFile = 0;
10754  int i;
10755  int rc = 0;
10756  int warnInmemoryDb = 0;
10757  int readStdin = 1;
10758  int nCmd = 0;
10759  char **azCmd = 0;
10760  const char *zVfs = 0;           /* Value of -vfs command-line option */
10761#if !SQLITE_SHELL_IS_UTF8
10762  char **argvToFree = 0;
10763  int argcToFree = 0;
10764#endif
10765
10766  setBinaryMode(stdin, 0);
10767  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10768  stdin_is_interactive = isatty(0);
10769  stdout_is_console = isatty(1);
10770
10771#ifdef SQLITE_DEBUG
10772  registerOomSimulator();
10773#endif
10774
10775#if !defined(_WIN32_WCE)
10776  if( getenv("SQLITE_DEBUG_BREAK") ){
10777    if( isatty(0) && isatty(2) ){
10778      fprintf(stderr,
10779          "attach debugger to process %d and press any key to continue.\n",
10780          GETPID());
10781      fgetc(stdin);
10782    }else{
10783#if defined(_WIN32) || defined(WIN32)
10784#if SQLITE_OS_WINRT
10785      __debugbreak();
10786#else
10787      DebugBreak();
10788#endif
10789#elif defined(SIGTRAP)
10790      raise(SIGTRAP);
10791#endif
10792    }
10793  }
10794#endif
10795
10796#if USE_SYSTEM_SQLITE+0!=1
10797  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10798    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10799            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10800    exit(1);
10801  }
10802#endif
10803  main_init(&data);
10804
10805  /* On Windows, we must translate command-line arguments into UTF-8.
10806  ** The SQLite memory allocator subsystem has to be enabled in order to
10807  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10808  ** subsequent sqlite3_config() calls will work.  So copy all results into
10809  ** memory that does not come from the SQLite memory allocator.
10810  */
10811#if !SQLITE_SHELL_IS_UTF8
10812  sqlite3_initialize();
10813  argvToFree = malloc(sizeof(argv[0])*argc*2);
10814  argcToFree = argc;
10815  argv = argvToFree + argc;
10816  if( argv==0 ) shell_out_of_memory();
10817  for(i=0; i<argc; i++){
10818    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10819    int n;
10820    if( z==0 ) shell_out_of_memory();
10821    n = (int)strlen(z);
10822    argv[i] = malloc( n+1 );
10823    if( argv[i]==0 ) shell_out_of_memory();
10824    memcpy(argv[i], z, n+1);
10825    argvToFree[i] = argv[i];
10826    sqlite3_free(z);
10827  }
10828  sqlite3_shutdown();
10829#endif
10830
10831  assert( argc>=1 && argv && argv[0] );
10832  Argv0 = argv[0];
10833
10834  /* Make sure we have a valid signal handler early, before anything
10835  ** else is done.
10836  */
10837#ifdef SIGINT
10838  signal(SIGINT, interrupt_handler);
10839#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10840  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10841#endif
10842
10843#ifdef SQLITE_SHELL_DBNAME_PROC
10844  {
10845    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10846    ** of a C-function that will provide the name of the database file.  Use
10847    ** this compile-time option to embed this shell program in larger
10848    ** applications. */
10849    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10850    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10851    warnInmemoryDb = 0;
10852  }
10853#endif
10854
10855  /* Do an initial pass through the command-line argument to locate
10856  ** the name of the database file, the name of the initialization file,
10857  ** the size of the alternative malloc heap,
10858  ** and the first command to execute.
10859  */
10860  verify_uninitialized();
10861  for(i=1; i<argc; i++){
10862    char *z;
10863    z = argv[i];
10864    if( z[0]!='-' ){
10865      if( data.zDbFilename==0 ){
10866        data.zDbFilename = z;
10867      }else{
10868        /* Excesss arguments are interpreted as SQL (or dot-commands) and
10869        ** mean that nothing is read from stdin */
10870        readStdin = 0;
10871        nCmd++;
10872        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
10873        if( azCmd==0 ) shell_out_of_memory();
10874        azCmd[nCmd-1] = z;
10875      }
10876    }
10877    if( z[1]=='-' ) z++;
10878    if( strcmp(z,"-separator")==0
10879     || strcmp(z,"-nullvalue")==0
10880     || strcmp(z,"-newline")==0
10881     || strcmp(z,"-cmd")==0
10882    ){
10883      (void)cmdline_option_value(argc, argv, ++i);
10884    }else if( strcmp(z,"-init")==0 ){
10885      zInitFile = cmdline_option_value(argc, argv, ++i);
10886    }else if( strcmp(z,"-batch")==0 ){
10887      /* Need to check for batch mode here to so we can avoid printing
10888      ** informational messages (like from process_sqliterc) before
10889      ** we do the actual processing of arguments later in a second pass.
10890      */
10891      stdin_is_interactive = 0;
10892    }else if( strcmp(z,"-heap")==0 ){
10893#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10894      const char *zSize;
10895      sqlite3_int64 szHeap;
10896
10897      zSize = cmdline_option_value(argc, argv, ++i);
10898      szHeap = integerValue(zSize);
10899      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
10900      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
10901#else
10902      (void)cmdline_option_value(argc, argv, ++i);
10903#endif
10904    }else if( strcmp(z,"-pagecache")==0 ){
10905      int n, sz;
10906      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10907      if( sz>70000 ) sz = 70000;
10908      if( sz<0 ) sz = 0;
10909      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10910      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
10911                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
10912      data.shellFlgs |= SHFLG_Pagecache;
10913    }else if( strcmp(z,"-lookaside")==0 ){
10914      int n, sz;
10915      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10916      if( sz<0 ) sz = 0;
10917      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10918      if( n<0 ) n = 0;
10919      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
10920      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
10921#ifdef SQLITE_ENABLE_VFSTRACE
10922    }else if( strcmp(z,"-vfstrace")==0 ){
10923      extern int vfstrace_register(
10924         const char *zTraceName,
10925         const char *zOldVfsName,
10926         int (*xOut)(const char*,void*),
10927         void *pOutArg,
10928         int makeDefault
10929      );
10930      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
10931#endif
10932#ifdef SQLITE_ENABLE_MULTIPLEX
10933    }else if( strcmp(z,"-multiplex")==0 ){
10934      extern int sqlite3_multiple_initialize(const char*,int);
10935      sqlite3_multiplex_initialize(0, 1);
10936#endif
10937    }else if( strcmp(z,"-mmap")==0 ){
10938      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10939      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
10940#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10941    }else if( strcmp(z,"-sorterref")==0 ){
10942      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10943      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
10944#endif
10945    }else if( strcmp(z,"-vfs")==0 ){
10946      zVfs = cmdline_option_value(argc, argv, ++i);
10947#ifdef SQLITE_HAVE_ZLIB
10948    }else if( strcmp(z,"-zip")==0 ){
10949      data.openMode = SHELL_OPEN_ZIPFILE;
10950#endif
10951    }else if( strcmp(z,"-append")==0 ){
10952      data.openMode = SHELL_OPEN_APPENDVFS;
10953#ifdef SQLITE_ENABLE_DESERIALIZE
10954    }else if( strcmp(z,"-deserialize")==0 ){
10955      data.openMode = SHELL_OPEN_DESERIALIZE;
10956    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10957      data.szMax = integerValue(argv[++i]);
10958#endif
10959    }else if( strcmp(z,"-readonly")==0 ){
10960      data.openMode = SHELL_OPEN_READONLY;
10961    }else if( strcmp(z,"-nofollow")==0 ){
10962      data.openFlags = SQLITE_OPEN_NOFOLLOW;
10963#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
10964    }else if( strncmp(z, "-A",2)==0 ){
10965      /* All remaining command-line arguments are passed to the ".archive"
10966      ** command, so ignore them */
10967      break;
10968#endif
10969    }else if( strcmp(z, "-memtrace")==0 ){
10970      sqlite3MemTraceActivate(stderr);
10971    }
10972  }
10973  verify_uninitialized();
10974
10975
10976#ifdef SQLITE_SHELL_INIT_PROC
10977  {
10978    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
10979    ** of a C-function that will perform initialization actions on SQLite that
10980    ** occur just before or after sqlite3_initialize(). Use this compile-time
10981    ** option to embed this shell program in larger applications. */
10982    extern void SQLITE_SHELL_INIT_PROC(void);
10983    SQLITE_SHELL_INIT_PROC();
10984  }
10985#else
10986  /* All the sqlite3_config() calls have now been made. So it is safe
10987  ** to call sqlite3_initialize() and process any command line -vfs option. */
10988  sqlite3_initialize();
10989#endif
10990
10991  if( zVfs ){
10992    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
10993    if( pVfs ){
10994      sqlite3_vfs_register(pVfs, 1);
10995    }else{
10996      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
10997      exit(1);
10998    }
10999  }
11000
11001  if( data.zDbFilename==0 ){
11002#ifndef SQLITE_OMIT_MEMORYDB
11003    data.zDbFilename = ":memory:";
11004    warnInmemoryDb = argc==1;
11005#else
11006    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11007    return 1;
11008#endif
11009  }
11010  data.out = stdout;
11011  sqlite3_appendvfs_init(0,0,0);
11012
11013  /* Go ahead and open the database file if it already exists.  If the
11014  ** file does not exist, delay opening it.  This prevents empty database
11015  ** files from being created if a user mistypes the database name argument
11016  ** to the sqlite command-line tool.
11017  */
11018  if( access(data.zDbFilename, 0)==0 ){
11019    open_db(&data, 0);
11020  }
11021
11022  /* Process the initialization file if there is one.  If no -init option
11023  ** is given on the command line, look for a file named ~/.sqliterc and
11024  ** try to process it.
11025  */
11026  process_sqliterc(&data,zInitFile);
11027
11028  /* Make a second pass through the command-line argument and set
11029  ** options.  This second pass is delayed until after the initialization
11030  ** file is processed so that the command-line arguments will override
11031  ** settings in the initialization file.
11032  */
11033  for(i=1; i<argc; i++){
11034    char *z = argv[i];
11035    if( z[0]!='-' ) continue;
11036    if( z[1]=='-' ){ z++; }
11037    if( strcmp(z,"-init")==0 ){
11038      i++;
11039    }else if( strcmp(z,"-html")==0 ){
11040      data.mode = MODE_Html;
11041    }else if( strcmp(z,"-list")==0 ){
11042      data.mode = MODE_List;
11043    }else if( strcmp(z,"-quote")==0 ){
11044      data.mode = MODE_Quote;
11045      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11046      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11047    }else if( strcmp(z,"-line")==0 ){
11048      data.mode = MODE_Line;
11049    }else if( strcmp(z,"-column")==0 ){
11050      data.mode = MODE_Column;
11051    }else if( strcmp(z,"-json")==0 ){
11052      data.mode = MODE_Json;
11053    }else if( strcmp(z,"-markdown")==0 ){
11054      data.mode = MODE_Markdown;
11055    }else if( strcmp(z,"-table")==0 ){
11056      data.mode = MODE_Table;
11057    }else if( strcmp(z,"-box")==0 ){
11058      data.mode = MODE_Box;
11059    }else if( strcmp(z,"-csv")==0 ){
11060      data.mode = MODE_Csv;
11061      memcpy(data.colSeparator,",",2);
11062#ifdef SQLITE_HAVE_ZLIB
11063    }else if( strcmp(z,"-zip")==0 ){
11064      data.openMode = SHELL_OPEN_ZIPFILE;
11065#endif
11066    }else if( strcmp(z,"-append")==0 ){
11067      data.openMode = SHELL_OPEN_APPENDVFS;
11068#ifdef SQLITE_ENABLE_DESERIALIZE
11069    }else if( strcmp(z,"-deserialize")==0 ){
11070      data.openMode = SHELL_OPEN_DESERIALIZE;
11071    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11072      data.szMax = integerValue(argv[++i]);
11073#endif
11074    }else if( strcmp(z,"-readonly")==0 ){
11075      data.openMode = SHELL_OPEN_READONLY;
11076    }else if( strcmp(z,"-nofollow")==0 ){
11077      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11078    }else if( strcmp(z,"-ascii")==0 ){
11079      data.mode = MODE_Ascii;
11080      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11081                       SEP_Unit);
11082      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11083                       SEP_Record);
11084    }else if( strcmp(z,"-separator")==0 ){
11085      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11086                       "%s",cmdline_option_value(argc,argv,++i));
11087    }else if( strcmp(z,"-newline")==0 ){
11088      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11089                       "%s",cmdline_option_value(argc,argv,++i));
11090    }else if( strcmp(z,"-nullvalue")==0 ){
11091      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11092                       "%s",cmdline_option_value(argc,argv,++i));
11093    }else if( strcmp(z,"-header")==0 ){
11094      data.showHeader = 1;
11095    }else if( strcmp(z,"-noheader")==0 ){
11096      data.showHeader = 0;
11097    }else if( strcmp(z,"-echo")==0 ){
11098      ShellSetFlag(&data, SHFLG_Echo);
11099    }else if( strcmp(z,"-eqp")==0 ){
11100      data.autoEQP = AUTOEQP_on;
11101    }else if( strcmp(z,"-eqpfull")==0 ){
11102      data.autoEQP = AUTOEQP_full;
11103    }else if( strcmp(z,"-stats")==0 ){
11104      data.statsOn = 1;
11105    }else if( strcmp(z,"-scanstats")==0 ){
11106      data.scanstatsOn = 1;
11107    }else if( strcmp(z,"-backslash")==0 ){
11108      /* Undocumented command-line option: -backslash
11109      ** Causes C-style backslash escapes to be evaluated in SQL statements
11110      ** prior to sending the SQL into SQLite.  Useful for injecting
11111      ** crazy bytes in the middle of SQL statements for testing and debugging.
11112      */
11113      ShellSetFlag(&data, SHFLG_Backslash);
11114    }else if( strcmp(z,"-bail")==0 ){
11115      bail_on_error = 1;
11116    }else if( strcmp(z,"-version")==0 ){
11117      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11118      return 0;
11119    }else if( strcmp(z,"-interactive")==0 ){
11120      stdin_is_interactive = 1;
11121    }else if( strcmp(z,"-batch")==0 ){
11122      stdin_is_interactive = 0;
11123    }else if( strcmp(z,"-heap")==0 ){
11124      i++;
11125    }else if( strcmp(z,"-pagecache")==0 ){
11126      i+=2;
11127    }else if( strcmp(z,"-lookaside")==0 ){
11128      i+=2;
11129    }else if( strcmp(z,"-mmap")==0 ){
11130      i++;
11131    }else if( strcmp(z,"-memtrace")==0 ){
11132      i++;
11133#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11134    }else if( strcmp(z,"-sorterref")==0 ){
11135      i++;
11136#endif
11137    }else if( strcmp(z,"-vfs")==0 ){
11138      i++;
11139#ifdef SQLITE_ENABLE_VFSTRACE
11140    }else if( strcmp(z,"-vfstrace")==0 ){
11141      i++;
11142#endif
11143#ifdef SQLITE_ENABLE_MULTIPLEX
11144    }else if( strcmp(z,"-multiplex")==0 ){
11145      i++;
11146#endif
11147    }else if( strcmp(z,"-help")==0 ){
11148      usage(1);
11149    }else if( strcmp(z,"-cmd")==0 ){
11150      /* Run commands that follow -cmd first and separately from commands
11151      ** that simply appear on the command-line.  This seems goofy.  It would
11152      ** be better if all commands ran in the order that they appear.  But
11153      ** we retain the goofy behavior for historical compatibility. */
11154      if( i==argc-1 ) break;
11155      z = cmdline_option_value(argc,argv,++i);
11156      if( z[0]=='.' ){
11157        rc = do_meta_command(z, &data);
11158        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11159      }else{
11160        open_db(&data, 0);
11161        rc = shell_exec(&data, z, &zErrMsg);
11162        if( zErrMsg!=0 ){
11163          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11164          if( bail_on_error ) return rc!=0 ? rc : 1;
11165        }else if( rc!=0 ){
11166          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11167          if( bail_on_error ) return rc;
11168        }
11169      }
11170#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11171    }else if( strncmp(z, "-A", 2)==0 ){
11172      if( nCmd>0 ){
11173        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11174                            " with \"%s\"\n", z);
11175        return 1;
11176      }
11177      open_db(&data, OPEN_DB_ZIPFILE);
11178      if( z[2] ){
11179        argv[i] = &z[2];
11180        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11181      }else{
11182        arDotCommand(&data, 1, argv+i, argc-i);
11183      }
11184      readStdin = 0;
11185      break;
11186#endif
11187    }else{
11188      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11189      raw_printf(stderr,"Use -help for a list of options.\n");
11190      return 1;
11191    }
11192    data.cMode = data.mode;
11193  }
11194
11195  if( !readStdin ){
11196    /* Run all arguments that do not begin with '-' as if they were separate
11197    ** command-line inputs, except for the argToSkip argument which contains
11198    ** the database filename.
11199    */
11200    for(i=0; i<nCmd; i++){
11201      if( azCmd[i][0]=='.' ){
11202        rc = do_meta_command(azCmd[i], &data);
11203        if( rc ) return rc==2 ? 0 : rc;
11204      }else{
11205        open_db(&data, 0);
11206        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11207        if( zErrMsg!=0 ){
11208          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11209          return rc!=0 ? rc : 1;
11210        }else if( rc!=0 ){
11211          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11212          return rc;
11213        }
11214      }
11215    }
11216    free(azCmd);
11217  }else{
11218    /* Run commands received from standard input
11219    */
11220    if( stdin_is_interactive ){
11221      char *zHome;
11222      char *zHistory;
11223      int nHistory;
11224      printf(
11225        "SQLite version %s %.19s\n" /*extra-version-info*/
11226        "Enter \".help\" for usage hints.\n",
11227        sqlite3_libversion(), sqlite3_sourceid()
11228      );
11229      if( warnInmemoryDb ){
11230        printf("Connected to a ");
11231        printBold("transient in-memory database");
11232        printf(".\nUse \".open FILENAME\" to reopen on a "
11233               "persistent database.\n");
11234      }
11235      zHistory = getenv("SQLITE_HISTORY");
11236      if( zHistory ){
11237        zHistory = strdup(zHistory);
11238      }else if( (zHome = find_home_dir(0))!=0 ){
11239        nHistory = strlen30(zHome) + 20;
11240        if( (zHistory = malloc(nHistory))!=0 ){
11241          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11242        }
11243      }
11244      if( zHistory ){ shell_read_history(zHistory); }
11245#if HAVE_READLINE || HAVE_EDITLINE
11246      rl_attempted_completion_function = readline_completion;
11247#elif HAVE_LINENOISE
11248      linenoiseSetCompletionCallback(linenoise_completion);
11249#endif
11250      data.in = 0;
11251      rc = process_input(&data);
11252      if( zHistory ){
11253        shell_stifle_history(2000);
11254        shell_write_history(zHistory);
11255        free(zHistory);
11256      }
11257    }else{
11258      data.in = stdin;
11259      rc = process_input(&data);
11260    }
11261  }
11262  set_table_name(&data, 0);
11263  if( data.db ){
11264    session_close_all(&data);
11265    close_db(data.db);
11266  }
11267  sqlite3_free(data.zFreeOnClose);
11268  find_home_dir(1);
11269  output_reset(&data);
11270  data.doXdgOpen = 0;
11271  clearTempFile(&data);
11272#if !SQLITE_SHELL_IS_UTF8
11273  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11274  free(argvToFree);
11275#endif
11276  free(data.colWidth);
11277  /* Clear the global data structure so that valgrind will detect memory
11278  ** leaks */
11279  memset(&data, 0, sizeof(data));
11280  return rc;
11281}
11282