xref: /sqlite-3.40.0/src/shell.c.in (revision 60c71b02)
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
1031INCLUDE ../ext/misc/series.c
1032#ifdef SQLITE_HAVE_ZLIB
1033INCLUDE ../ext/misc/zipfile.c
1034INCLUDE ../ext/misc/sqlar.c
1035#endif
1036INCLUDE ../ext/expert/sqlite3expert.h
1037INCLUDE ../ext/expert/sqlite3expert.c
1038
1039#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1040INCLUDE ../ext/misc/dbdata.c
1041#endif
1042
1043#if defined(SQLITE_ENABLE_SESSION)
1044/*
1045** State information for a single open session
1046*/
1047typedef struct OpenSession OpenSession;
1048struct OpenSession {
1049  char *zName;             /* Symbolic name for this session */
1050  int nFilter;             /* Number of xFilter rejection GLOB patterns */
1051  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
1052  sqlite3_session *p;      /* The open session */
1053};
1054#endif
1055
1056typedef struct ExpertInfo ExpertInfo;
1057struct ExpertInfo {
1058  sqlite3expert *pExpert;
1059  int bVerbose;
1060};
1061
1062/* A single line in the EQP output */
1063typedef struct EQPGraphRow EQPGraphRow;
1064struct EQPGraphRow {
1065  int iEqpId;           /* ID for this row */
1066  int iParentId;        /* ID of the parent row */
1067  EQPGraphRow *pNext;   /* Next row in sequence */
1068  char zText[1];        /* Text to display for this row */
1069};
1070
1071/* All EQP output is collected into an instance of the following */
1072typedef struct EQPGraph EQPGraph;
1073struct EQPGraph {
1074  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
1075  EQPGraphRow *pLast;   /* Last element of the pRow list */
1076  char zPrefix[100];    /* Graph prefix */
1077};
1078
1079/*
1080** State information about the database connection is contained in an
1081** instance of the following structure.
1082*/
1083typedef struct ShellState ShellState;
1084struct ShellState {
1085  sqlite3 *db;           /* The database */
1086  u8 autoExplain;        /* Automatically turn on .explain mode */
1087  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1088  u8 autoEQPtest;        /* autoEQP is in test mode */
1089  u8 autoEQPtrace;       /* autoEQP is in trace mode */
1090  u8 statsOn;            /* True to display memory stats before each finalize */
1091  u8 scanstatsOn;        /* True to display scan stats before each finalize */
1092  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1093  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
1094  u8 nEqpLevel;          /* Depth of the EQP output graph */
1095  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
1096  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
1097  int outCount;          /* Revert to stdout when reaching zero */
1098  int cnt;               /* Number of records displayed so far */
1099  int lineno;            /* Line number of last line read from in */
1100  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
1101  FILE *in;              /* Read commands from this stream */
1102  FILE *out;             /* Write results here */
1103  FILE *traceOut;        /* Output for sqlite3_trace() */
1104  int nErr;              /* Number of errors seen */
1105  int mode;              /* An output mode setting */
1106  int modePrior;         /* Saved mode */
1107  int cMode;             /* temporary output mode for the current query */
1108  int normalMode;        /* Output mode before ".explain on" */
1109  int writableSchema;    /* True if PRAGMA writable_schema=ON */
1110  int showHeader;        /* True to show column names in List or Column mode */
1111  int nCheck;            /* Number of ".check" commands run */
1112  unsigned nProgress;    /* Number of progress callbacks encountered */
1113  unsigned mxProgress;   /* Maximum progress callbacks before failing */
1114  unsigned flgProgress;  /* Flags for the progress callback */
1115  unsigned shellFlgs;    /* Various flags */
1116  unsigned priorShFlgs;  /* Saved copy of flags */
1117  sqlite3_int64 szMax;   /* --maxsize argument to .open */
1118  char *zDestTable;      /* Name of destination table when MODE_Insert */
1119  char *zTempFile;       /* Temporary file that might need deleting */
1120  char zTestcase[30];    /* Name of current test case */
1121  char colSeparator[20]; /* Column separator character for several modes */
1122  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1123  char colSepPrior[20];  /* Saved column separator */
1124  char rowSepPrior[20];  /* Saved row separator */
1125  int *colWidth;         /* Requested width of each column in columnar modes */
1126  int *actualWidth;      /* Actual width of each column */
1127  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
1128  char nullValue[20];    /* The text to print when a NULL comes back from
1129                         ** the database */
1130  char outfile[FILENAME_MAX]; /* Filename for *out */
1131  const char *zDbFilename;    /* name of the database file */
1132  char *zFreeOnClose;         /* Filename to free when closing */
1133  const char *zVfs;           /* Name of VFS to use */
1134  sqlite3_stmt *pStmt;   /* Current statement if any. */
1135  FILE *pLog;            /* Write log output here */
1136  int *aiIndent;         /* Array of indents used in MODE_Explain */
1137  int nIndent;           /* Size of array aiIndent[] */
1138  int iIndent;           /* Index of current op in aiIndent[] */
1139  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
1140#if defined(SQLITE_ENABLE_SESSION)
1141  int nSession;             /* Number of active sessions */
1142  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
1143#endif
1144  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
1145};
1146
1147
1148/* Allowed values for ShellState.autoEQP
1149*/
1150#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
1151#define AUTOEQP_on       1           /* Automatic EQP is on */
1152#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
1153#define AUTOEQP_full     3           /* Show full EXPLAIN */
1154
1155/* Allowed values for ShellState.openMode
1156*/
1157#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
1158#define SHELL_OPEN_NORMAL      1      /* Normal database file */
1159#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
1160#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
1161#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
1162#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
1163#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
1164
1165/* Allowed values for ShellState.eTraceType
1166*/
1167#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
1168#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
1169#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
1170
1171/* Bits in the ShellState.flgProgress variable */
1172#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
1173#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
1174                                   ** callback limit is reached, and for each
1175                                   ** top-level SQL statement */
1176#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
1177
1178/*
1179** These are the allowed shellFlgs values
1180*/
1181#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
1182#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
1183#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
1184#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
1185#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
1186#define SHFLG_CountChanges   0x00000020 /* .changes setting */
1187#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
1188#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
1189
1190/*
1191** Macros for testing and setting shellFlgs
1192*/
1193#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
1194#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
1195#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
1196
1197/*
1198** These are the allowed modes.
1199*/
1200#define MODE_Line     0  /* One column per line.  Blank line between records */
1201#define MODE_Column   1  /* One record per line in neat columns */
1202#define MODE_List     2  /* One record per line with a separator */
1203#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
1204#define MODE_Html     4  /* Generate an XHTML table */
1205#define MODE_Insert   5  /* Generate SQL "insert" statements */
1206#define MODE_Quote    6  /* Quote values as for SQL */
1207#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
1208#define MODE_Csv      8  /* Quote strings, numbers are plain */
1209#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
1210#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
1211#define MODE_Pretty  11  /* Pretty-print schemas */
1212#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
1213#define MODE_Json    13  /* Output JSON */
1214#define MODE_Markdown 14 /* Markdown formatting */
1215#define MODE_Table   15  /* MySQL-style table formatting */
1216#define MODE_Box     16  /* Unicode box-drawing characters */
1217
1218static const char *modeDescr[] = {
1219  "line",
1220  "column",
1221  "list",
1222  "semi",
1223  "html",
1224  "insert",
1225  "quote",
1226  "tcl",
1227  "csv",
1228  "explain",
1229  "ascii",
1230  "prettyprint",
1231  "eqp",
1232  "json",
1233  "markdown",
1234  "table",
1235  "box"
1236};
1237
1238/*
1239** These are the column/row/line separators used by the various
1240** import/export modes.
1241*/
1242#define SEP_Column    "|"
1243#define SEP_Row       "\n"
1244#define SEP_Tab       "\t"
1245#define SEP_Space     " "
1246#define SEP_Comma     ","
1247#define SEP_CrLf      "\r\n"
1248#define SEP_Unit      "\x1F"
1249#define SEP_Record    "\x1E"
1250
1251/*
1252** A callback for the sqlite3_log() interface.
1253*/
1254static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1255  ShellState *p = (ShellState*)pArg;
1256  if( p->pLog==0 ) return;
1257  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1258  fflush(p->pLog);
1259}
1260
1261/*
1262** SQL function:  shell_putsnl(X)
1263**
1264** Write the text X to the screen (or whatever output is being directed)
1265** adding a newline at the end, and then return X.
1266*/
1267static void shellPutsFunc(
1268  sqlite3_context *pCtx,
1269  int nVal,
1270  sqlite3_value **apVal
1271){
1272  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1273  (void)nVal;
1274  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1275  sqlite3_result_value(pCtx, apVal[0]);
1276}
1277
1278/*
1279** SQL function:   edit(VALUE)
1280**                 edit(VALUE,EDITOR)
1281**
1282** These steps:
1283**
1284**     (1) Write VALUE into a temporary file.
1285**     (2) Run program EDITOR on that temporary file.
1286**     (3) Read the temporary file back and return its content as the result.
1287**     (4) Delete the temporary file
1288**
1289** If the EDITOR argument is omitted, use the value in the VISUAL
1290** environment variable.  If still there is no EDITOR, through an error.
1291**
1292** Also throw an error if the EDITOR program returns a non-zero exit code.
1293*/
1294#ifndef SQLITE_NOHAVE_SYSTEM
1295static void editFunc(
1296  sqlite3_context *context,
1297  int argc,
1298  sqlite3_value **argv
1299){
1300  const char *zEditor;
1301  char *zTempFile = 0;
1302  sqlite3 *db;
1303  char *zCmd = 0;
1304  int bBin;
1305  int rc;
1306  int hasCRNL = 0;
1307  FILE *f = 0;
1308  sqlite3_int64 sz;
1309  sqlite3_int64 x;
1310  unsigned char *p = 0;
1311
1312  if( argc==2 ){
1313    zEditor = (const char*)sqlite3_value_text(argv[1]);
1314  }else{
1315    zEditor = getenv("VISUAL");
1316  }
1317  if( zEditor==0 ){
1318    sqlite3_result_error(context, "no editor for edit()", -1);
1319    return;
1320  }
1321  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1322    sqlite3_result_error(context, "NULL input to edit()", -1);
1323    return;
1324  }
1325  db = sqlite3_context_db_handle(context);
1326  zTempFile = 0;
1327  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1328  if( zTempFile==0 ){
1329    sqlite3_uint64 r = 0;
1330    sqlite3_randomness(sizeof(r), &r);
1331    zTempFile = sqlite3_mprintf("temp%llx", r);
1332    if( zTempFile==0 ){
1333      sqlite3_result_error_nomem(context);
1334      return;
1335    }
1336  }
1337  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1338  /* When writing the file to be edited, do \n to \r\n conversions on systems
1339  ** that want \r\n line endings */
1340  f = fopen(zTempFile, bBin ? "wb" : "w");
1341  if( f==0 ){
1342    sqlite3_result_error(context, "edit() cannot open temp file", -1);
1343    goto edit_func_end;
1344  }
1345  sz = sqlite3_value_bytes(argv[0]);
1346  if( bBin ){
1347    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1348  }else{
1349    const char *z = (const char*)sqlite3_value_text(argv[0]);
1350    /* Remember whether or not the value originally contained \r\n */
1351    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1352    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1353  }
1354  fclose(f);
1355  f = 0;
1356  if( x!=sz ){
1357    sqlite3_result_error(context, "edit() could not write the whole file", -1);
1358    goto edit_func_end;
1359  }
1360  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1361  if( zCmd==0 ){
1362    sqlite3_result_error_nomem(context);
1363    goto edit_func_end;
1364  }
1365  rc = system(zCmd);
1366  sqlite3_free(zCmd);
1367  if( rc ){
1368    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1369    goto edit_func_end;
1370  }
1371  f = fopen(zTempFile, "rb");
1372  if( f==0 ){
1373    sqlite3_result_error(context,
1374      "edit() cannot reopen temp file after edit", -1);
1375    goto edit_func_end;
1376  }
1377  fseek(f, 0, SEEK_END);
1378  sz = ftell(f);
1379  rewind(f);
1380  p = sqlite3_malloc64( sz+1 );
1381  if( p==0 ){
1382    sqlite3_result_error_nomem(context);
1383    goto edit_func_end;
1384  }
1385  x = fread(p, 1, (size_t)sz, f);
1386  fclose(f);
1387  f = 0;
1388  if( x!=sz ){
1389    sqlite3_result_error(context, "could not read back the whole file", -1);
1390    goto edit_func_end;
1391  }
1392  if( bBin ){
1393    sqlite3_result_blob64(context, p, sz, sqlite3_free);
1394  }else{
1395    sqlite3_int64 i, j;
1396    if( hasCRNL ){
1397      /* If the original contains \r\n then do no conversions back to \n */
1398      j = sz;
1399    }else{
1400      /* If the file did not originally contain \r\n then convert any new
1401      ** \r\n back into \n */
1402      for(i=j=0; i<sz; i++){
1403        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1404        p[j++] = p[i];
1405      }
1406      sz = j;
1407      p[sz] = 0;
1408    }
1409    sqlite3_result_text64(context, (const char*)p, sz,
1410                          sqlite3_free, SQLITE_UTF8);
1411  }
1412  p = 0;
1413
1414edit_func_end:
1415  if( f ) fclose(f);
1416  unlink(zTempFile);
1417  sqlite3_free(zTempFile);
1418  sqlite3_free(p);
1419}
1420#endif /* SQLITE_NOHAVE_SYSTEM */
1421
1422/*
1423** Save or restore the current output mode
1424*/
1425static void outputModePush(ShellState *p){
1426  p->modePrior = p->mode;
1427  p->priorShFlgs = p->shellFlgs;
1428  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1429  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1430}
1431static void outputModePop(ShellState *p){
1432  p->mode = p->modePrior;
1433  p->shellFlgs = p->priorShFlgs;
1434  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1435  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1436}
1437
1438/*
1439** Output the given string as a hex-encoded blob (eg. X'1234' )
1440*/
1441static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1442  int i;
1443  char *zBlob = (char *)pBlob;
1444  raw_printf(out,"X'");
1445  for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1446  raw_printf(out,"'");
1447}
1448
1449/*
1450** Find a string that is not found anywhere in z[].  Return a pointer
1451** to that string.
1452**
1453** Try to use zA and zB first.  If both of those are already found in z[]
1454** then make up some string and store it in the buffer zBuf.
1455*/
1456static const char *unused_string(
1457  const char *z,                    /* Result must not appear anywhere in z */
1458  const char *zA, const char *zB,   /* Try these first */
1459  char *zBuf                        /* Space to store a generated string */
1460){
1461  unsigned i = 0;
1462  if( strstr(z, zA)==0 ) return zA;
1463  if( strstr(z, zB)==0 ) return zB;
1464  do{
1465    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1466  }while( strstr(z,zBuf)!=0 );
1467  return zBuf;
1468}
1469
1470/*
1471** Output the given string as a quoted string using SQL quoting conventions.
1472**
1473** See also: output_quoted_escaped_string()
1474*/
1475static void output_quoted_string(FILE *out, const char *z){
1476  int i;
1477  char c;
1478  setBinaryMode(out, 1);
1479  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1480  if( c==0 ){
1481    utf8_printf(out,"'%s'",z);
1482  }else{
1483    raw_printf(out, "'");
1484    while( *z ){
1485      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1486      if( c=='\'' ) i++;
1487      if( i ){
1488        utf8_printf(out, "%.*s", i, z);
1489        z += i;
1490      }
1491      if( c=='\'' ){
1492        raw_printf(out, "'");
1493        continue;
1494      }
1495      if( c==0 ){
1496        break;
1497      }
1498      z++;
1499    }
1500    raw_printf(out, "'");
1501  }
1502  setTextMode(out, 1);
1503}
1504
1505/*
1506** Output the given string as a quoted string using SQL quoting conventions.
1507** Additionallly , escape the "\n" and "\r" characters so that they do not
1508** get corrupted by end-of-line translation facilities in some operating
1509** systems.
1510**
1511** This is like output_quoted_string() but with the addition of the \r\n
1512** escape mechanism.
1513*/
1514static void output_quoted_escaped_string(FILE *out, const char *z){
1515  int i;
1516  char c;
1517  setBinaryMode(out, 1);
1518  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1519  if( c==0 ){
1520    utf8_printf(out,"'%s'",z);
1521  }else{
1522    const char *zNL = 0;
1523    const char *zCR = 0;
1524    int nNL = 0;
1525    int nCR = 0;
1526    char zBuf1[20], zBuf2[20];
1527    for(i=0; z[i]; i++){
1528      if( z[i]=='\n' ) nNL++;
1529      if( z[i]=='\r' ) nCR++;
1530    }
1531    if( nNL ){
1532      raw_printf(out, "replace(");
1533      zNL = unused_string(z, "\\n", "\\012", zBuf1);
1534    }
1535    if( nCR ){
1536      raw_printf(out, "replace(");
1537      zCR = unused_string(z, "\\r", "\\015", zBuf2);
1538    }
1539    raw_printf(out, "'");
1540    while( *z ){
1541      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1542      if( c=='\'' ) i++;
1543      if( i ){
1544        utf8_printf(out, "%.*s", i, z);
1545        z += i;
1546      }
1547      if( c=='\'' ){
1548        raw_printf(out, "'");
1549        continue;
1550      }
1551      if( c==0 ){
1552        break;
1553      }
1554      z++;
1555      if( c=='\n' ){
1556        raw_printf(out, "%s", zNL);
1557        continue;
1558      }
1559      raw_printf(out, "%s", zCR);
1560    }
1561    raw_printf(out, "'");
1562    if( nCR ){
1563      raw_printf(out, ",'%s',char(13))", zCR);
1564    }
1565    if( nNL ){
1566      raw_printf(out, ",'%s',char(10))", zNL);
1567    }
1568  }
1569  setTextMode(out, 1);
1570}
1571
1572/*
1573** Output the given string as a quoted according to C or TCL quoting rules.
1574*/
1575static void output_c_string(FILE *out, const char *z){
1576  unsigned int c;
1577  fputc('"', out);
1578  while( (c = *(z++))!=0 ){
1579    if( c=='\\' ){
1580      fputc(c, out);
1581      fputc(c, out);
1582    }else if( c=='"' ){
1583      fputc('\\', out);
1584      fputc('"', out);
1585    }else if( c=='\t' ){
1586      fputc('\\', out);
1587      fputc('t', out);
1588    }else if( c=='\n' ){
1589      fputc('\\', out);
1590      fputc('n', out);
1591    }else if( c=='\r' ){
1592      fputc('\\', out);
1593      fputc('r', out);
1594    }else if( !isprint(c&0xff) ){
1595      raw_printf(out, "\\%03o", c&0xff);
1596    }else{
1597      fputc(c, out);
1598    }
1599  }
1600  fputc('"', out);
1601}
1602
1603/*
1604** Output the given string as a quoted according to JSON quoting rules.
1605*/
1606static void output_json_string(FILE *out, const char *z, int n){
1607  unsigned int c;
1608  if( n<0 ) n = (int)strlen(z);
1609  fputc('"', out);
1610  while( n-- ){
1611    c = *(z++);
1612    if( c=='\\' || c=='"' ){
1613      fputc('\\', out);
1614      fputc(c, out);
1615    }else if( c<=0x1f ){
1616      fputc('\\', out);
1617      if( c=='\b' ){
1618        fputc('b', out);
1619      }else if( c=='\f' ){
1620        fputc('f', out);
1621      }else if( c=='\n' ){
1622        fputc('n', out);
1623      }else if( c=='\r' ){
1624        fputc('r', out);
1625      }else if( c=='\t' ){
1626        fputc('t', out);
1627      }else{
1628         raw_printf(out, "u%04x",c);
1629      }
1630    }else{
1631      fputc(c, out);
1632    }
1633  }
1634  fputc('"', out);
1635}
1636
1637/*
1638** Output the given string with characters that are special to
1639** HTML escaped.
1640*/
1641static void output_html_string(FILE *out, const char *z){
1642  int i;
1643  if( z==0 ) z = "";
1644  while( *z ){
1645    for(i=0;   z[i]
1646            && z[i]!='<'
1647            && z[i]!='&'
1648            && z[i]!='>'
1649            && z[i]!='\"'
1650            && z[i]!='\'';
1651        i++){}
1652    if( i>0 ){
1653      utf8_printf(out,"%.*s",i,z);
1654    }
1655    if( z[i]=='<' ){
1656      raw_printf(out,"&lt;");
1657    }else if( z[i]=='&' ){
1658      raw_printf(out,"&amp;");
1659    }else if( z[i]=='>' ){
1660      raw_printf(out,"&gt;");
1661    }else if( z[i]=='\"' ){
1662      raw_printf(out,"&quot;");
1663    }else if( z[i]=='\'' ){
1664      raw_printf(out,"&#39;");
1665    }else{
1666      break;
1667    }
1668    z += i + 1;
1669  }
1670}
1671
1672/*
1673** If a field contains any character identified by a 1 in the following
1674** array, then the string must be quoted for CSV.
1675*/
1676static const char needCsvQuote[] = {
1677  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1678  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1679  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
1680  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1681  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1682  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1683  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
1684  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
1685  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1686  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1687  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1688  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1689  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1690  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1691  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1692  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
1693};
1694
1695/*
1696** Output a single term of CSV.  Actually, p->colSeparator is used for
1697** the separator, which may or may not be a comma.  p->nullValue is
1698** the null value.  Strings are quoted if necessary.  The separator
1699** is only issued if bSep is true.
1700*/
1701static void output_csv(ShellState *p, const char *z, int bSep){
1702  FILE *out = p->out;
1703  if( z==0 ){
1704    utf8_printf(out,"%s",p->nullValue);
1705  }else{
1706    int i;
1707    int nSep = strlen30(p->colSeparator);
1708    for(i=0; z[i]; i++){
1709      if( needCsvQuote[((unsigned char*)z)[i]]
1710         || (z[i]==p->colSeparator[0] &&
1711             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1712        i = 0;
1713        break;
1714      }
1715    }
1716    if( i==0 ){
1717      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1718      utf8_printf(out, "%s", zQuoted);
1719      sqlite3_free(zQuoted);
1720    }else{
1721      utf8_printf(out, "%s", z);
1722    }
1723  }
1724  if( bSep ){
1725    utf8_printf(p->out, "%s", p->colSeparator);
1726  }
1727}
1728
1729/*
1730** This routine runs when the user presses Ctrl-C
1731*/
1732static void interrupt_handler(int NotUsed){
1733  UNUSED_PARAMETER(NotUsed);
1734  seenInterrupt++;
1735  if( seenInterrupt>2 ) exit(1);
1736  if( globalDb ) sqlite3_interrupt(globalDb);
1737}
1738
1739#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1740/*
1741** This routine runs for console events (e.g. Ctrl-C) on Win32
1742*/
1743static BOOL WINAPI ConsoleCtrlHandler(
1744  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1745){
1746  if( dwCtrlType==CTRL_C_EVENT ){
1747    interrupt_handler(0);
1748    return TRUE;
1749  }
1750  return FALSE;
1751}
1752#endif
1753
1754#ifndef SQLITE_OMIT_AUTHORIZATION
1755/*
1756** When the ".auth ON" is set, the following authorizer callback is
1757** invoked.  It always returns SQLITE_OK.
1758*/
1759static int shellAuth(
1760  void *pClientData,
1761  int op,
1762  const char *zA1,
1763  const char *zA2,
1764  const char *zA3,
1765  const char *zA4
1766){
1767  ShellState *p = (ShellState*)pClientData;
1768  static const char *azAction[] = { 0,
1769     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
1770     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
1771     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
1772     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
1773     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
1774     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
1775     "PRAGMA",               "READ",                 "SELECT",
1776     "TRANSACTION",          "UPDATE",               "ATTACH",
1777     "DETACH",               "ALTER_TABLE",          "REINDEX",
1778     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
1779     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
1780  };
1781  int i;
1782  const char *az[4];
1783  az[0] = zA1;
1784  az[1] = zA2;
1785  az[2] = zA3;
1786  az[3] = zA4;
1787  utf8_printf(p->out, "authorizer: %s", azAction[op]);
1788  for(i=0; i<4; i++){
1789    raw_printf(p->out, " ");
1790    if( az[i] ){
1791      output_c_string(p->out, az[i]);
1792    }else{
1793      raw_printf(p->out, "NULL");
1794    }
1795  }
1796  raw_printf(p->out, "\n");
1797  return SQLITE_OK;
1798}
1799#endif
1800
1801/*
1802** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
1803**
1804** This routine converts some CREATE TABLE statements for shadow tables
1805** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1806*/
1807static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1808  if( z==0 ) return;
1809  if( zTail==0 ) return;
1810  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1811    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1812  }else{
1813    utf8_printf(out, "%s%s", z, zTail);
1814  }
1815}
1816static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1817  char c = z[n];
1818  z[n] = 0;
1819  printSchemaLine(out, z, zTail);
1820  z[n] = c;
1821}
1822
1823/*
1824** Return true if string z[] has nothing but whitespace and comments to the
1825** end of the first line.
1826*/
1827static int wsToEol(const char *z){
1828  int i;
1829  for(i=0; z[i]; i++){
1830    if( z[i]=='\n' ) return 1;
1831    if( IsSpace(z[i]) ) continue;
1832    if( z[i]=='-' && z[i+1]=='-' ) return 1;
1833    return 0;
1834  }
1835  return 1;
1836}
1837
1838/*
1839** Add a new entry to the EXPLAIN QUERY PLAN data
1840*/
1841static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
1842  EQPGraphRow *pNew;
1843  int nText = strlen30(zText);
1844  if( p->autoEQPtest ){
1845    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1846  }
1847  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1848  if( pNew==0 ) shell_out_of_memory();
1849  pNew->iEqpId = iEqpId;
1850  pNew->iParentId = p2;
1851  memcpy(pNew->zText, zText, nText+1);
1852  pNew->pNext = 0;
1853  if( p->sGraph.pLast ){
1854    p->sGraph.pLast->pNext = pNew;
1855  }else{
1856    p->sGraph.pRow = pNew;
1857  }
1858  p->sGraph.pLast = pNew;
1859}
1860
1861/*
1862** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1863** in p->sGraph.
1864*/
1865static void eqp_reset(ShellState *p){
1866  EQPGraphRow *pRow, *pNext;
1867  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1868    pNext = pRow->pNext;
1869    sqlite3_free(pRow);
1870  }
1871  memset(&p->sGraph, 0, sizeof(p->sGraph));
1872}
1873
1874/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1875** pOld, or return the first such line if pOld is NULL
1876*/
1877static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
1878  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
1879  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
1880  return pRow;
1881}
1882
1883/* Render a single level of the graph that has iEqpId as its parent.  Called
1884** recursively to render sublevels.
1885*/
1886static void eqp_render_level(ShellState *p, int iEqpId){
1887  EQPGraphRow *pRow, *pNext;
1888  int n = strlen30(p->sGraph.zPrefix);
1889  char *z;
1890  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1891    pNext = eqp_next_row(p, iEqpId, pRow);
1892    z = pRow->zText;
1893    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1894                pNext ? "|--" : "`--", z);
1895    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
1896      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
1897      eqp_render_level(p, pRow->iEqpId);
1898      p->sGraph.zPrefix[n] = 0;
1899    }
1900  }
1901}
1902
1903/*
1904** Display and reset the EXPLAIN QUERY PLAN data
1905*/
1906static void eqp_render(ShellState *p){
1907  EQPGraphRow *pRow = p->sGraph.pRow;
1908  if( pRow ){
1909    if( pRow->zText[0]=='-' ){
1910      if( pRow->pNext==0 ){
1911        eqp_reset(p);
1912        return;
1913      }
1914      utf8_printf(p->out, "%s\n", pRow->zText+3);
1915      p->sGraph.pRow = pRow->pNext;
1916      sqlite3_free(pRow);
1917    }else{
1918      utf8_printf(p->out, "QUERY PLAN\n");
1919    }
1920    p->sGraph.zPrefix[0] = 0;
1921    eqp_render_level(p, 0);
1922    eqp_reset(p);
1923  }
1924}
1925
1926#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1927/*
1928** Progress handler callback.
1929*/
1930static int progress_handler(void *pClientData) {
1931  ShellState *p = (ShellState*)pClientData;
1932  p->nProgress++;
1933  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1934    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
1935    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1936    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
1937    return 1;
1938  }
1939  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
1940    raw_printf(p->out, "Progress %u\n", p->nProgress);
1941  }
1942  return 0;
1943}
1944#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
1945
1946/*
1947** Print N dashes
1948*/
1949static void print_dashes(FILE *out, int N){
1950  const char zDash[] = "--------------------------------------------------";
1951  const int nDash = sizeof(zDash) - 1;
1952  while( N>nDash ){
1953    fputs(zDash, out);
1954    N -= nDash;
1955  }
1956  raw_printf(out, "%.*s", N, zDash);
1957}
1958
1959/*
1960** Print a markdown or table-style row separator using ascii-art
1961*/
1962static void print_row_separator(
1963  ShellState *p,
1964  int nArg,
1965  const char *zSep
1966){
1967  int i;
1968  if( nArg>0 ){
1969    fputs(zSep, p->out);
1970    print_dashes(p->out, p->actualWidth[0]+2);
1971    for(i=1; i<nArg; i++){
1972      fputs(zSep, p->out);
1973      print_dashes(p->out, p->actualWidth[i]+2);
1974    }
1975    fputs(zSep, p->out);
1976  }
1977  fputs("\n", p->out);
1978}
1979
1980/*
1981** This is the callback routine that the shell
1982** invokes for each row of a query result.
1983*/
1984static int shell_callback(
1985  void *pArg,
1986  int nArg,        /* Number of result columns */
1987  char **azArg,    /* Text of each result column */
1988  char **azCol,    /* Column names */
1989  int *aiType      /* Column types.  Might be NULL */
1990){
1991  int i;
1992  ShellState *p = (ShellState*)pArg;
1993
1994  if( azArg==0 ) return 0;
1995  switch( p->cMode ){
1996    case MODE_Line: {
1997      int w = 5;
1998      if( azArg==0 ) break;
1999      for(i=0; i<nArg; i++){
2000        int len = strlen30(azCol[i] ? azCol[i] : "");
2001        if( len>w ) w = len;
2002      }
2003      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2004      for(i=0; i<nArg; i++){
2005        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2006                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2007      }
2008      break;
2009    }
2010    case MODE_Explain: {
2011      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2012      if( nArg>ArraySize(aExplainWidth) ){
2013        nArg = ArraySize(aExplainWidth);
2014      }
2015      if( p->cnt++==0 ){
2016        for(i=0; i<nArg; i++){
2017          int w = aExplainWidth[i];
2018          utf8_width_print(p->out, w, azCol[i]);
2019          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2020        }
2021        for(i=0; i<nArg; i++){
2022          int w = aExplainWidth[i];
2023          print_dashes(p->out, w);
2024          fputs(i==nArg-1 ? "\n" : "  ", p->out);
2025        }
2026      }
2027      if( azArg==0 ) break;
2028      for(i=0; i<nArg; i++){
2029        int w = aExplainWidth[i];
2030        if( azArg[i] && strlenChar(azArg[i])>w ){
2031          w = strlenChar(azArg[i]);
2032        }
2033        if( i==1 && p->aiIndent && p->pStmt ){
2034          if( p->iIndent<p->nIndent ){
2035            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2036          }
2037          p->iIndent++;
2038        }
2039        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2040        fputs(i==nArg-1 ? "\n" : "  ", p->out);
2041      }
2042      break;
2043    }
2044    case MODE_Semi: {   /* .schema and .fullschema output */
2045      printSchemaLine(p->out, azArg[0], ";\n");
2046      break;
2047    }
2048    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
2049      char *z;
2050      int j;
2051      int nParen = 0;
2052      char cEnd = 0;
2053      char c;
2054      int nLine = 0;
2055      assert( nArg==1 );
2056      if( azArg[0]==0 ) break;
2057      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2058       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2059      ){
2060        utf8_printf(p->out, "%s;\n", azArg[0]);
2061        break;
2062      }
2063      z = sqlite3_mprintf("%s", azArg[0]);
2064      j = 0;
2065      for(i=0; IsSpace(z[i]); i++){}
2066      for(; (c = z[i])!=0; i++){
2067        if( IsSpace(c) ){
2068          if( z[j-1]=='\r' ) z[j-1] = '\n';
2069          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2070        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2071          j--;
2072        }
2073        z[j++] = c;
2074      }
2075      while( j>0 && IsSpace(z[j-1]) ){ j--; }
2076      z[j] = 0;
2077      if( strlen30(z)>=79 ){
2078        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2079          if( c==cEnd ){
2080            cEnd = 0;
2081          }else if( c=='"' || c=='\'' || c=='`' ){
2082            cEnd = c;
2083          }else if( c=='[' ){
2084            cEnd = ']';
2085          }else if( c=='-' && z[i+1]=='-' ){
2086            cEnd = '\n';
2087          }else if( c=='(' ){
2088            nParen++;
2089          }else if( c==')' ){
2090            nParen--;
2091            if( nLine>0 && nParen==0 && j>0 ){
2092              printSchemaLineN(p->out, z, j, "\n");
2093              j = 0;
2094            }
2095          }
2096          z[j++] = c;
2097          if( nParen==1 && cEnd==0
2098           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2099          ){
2100            if( c=='\n' ) j--;
2101            printSchemaLineN(p->out, z, j, "\n  ");
2102            j = 0;
2103            nLine++;
2104            while( IsSpace(z[i+1]) ){ i++; }
2105          }
2106        }
2107        z[j] = 0;
2108      }
2109      printSchemaLine(p->out, z, ";\n");
2110      sqlite3_free(z);
2111      break;
2112    }
2113    case MODE_List: {
2114      if( p->cnt++==0 && p->showHeader ){
2115        for(i=0; i<nArg; i++){
2116          utf8_printf(p->out,"%s%s",azCol[i],
2117                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
2118        }
2119      }
2120      if( azArg==0 ) break;
2121      for(i=0; i<nArg; i++){
2122        char *z = azArg[i];
2123        if( z==0 ) z = p->nullValue;
2124        utf8_printf(p->out, "%s", z);
2125        if( i<nArg-1 ){
2126          utf8_printf(p->out, "%s", p->colSeparator);
2127        }else{
2128          utf8_printf(p->out, "%s", p->rowSeparator);
2129        }
2130      }
2131      break;
2132    }
2133    case MODE_Html: {
2134      if( p->cnt++==0 && p->showHeader ){
2135        raw_printf(p->out,"<TR>");
2136        for(i=0; i<nArg; i++){
2137          raw_printf(p->out,"<TH>");
2138          output_html_string(p->out, azCol[i]);
2139          raw_printf(p->out,"</TH>\n");
2140        }
2141        raw_printf(p->out,"</TR>\n");
2142      }
2143      if( azArg==0 ) break;
2144      raw_printf(p->out,"<TR>");
2145      for(i=0; i<nArg; i++){
2146        raw_printf(p->out,"<TD>");
2147        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2148        raw_printf(p->out,"</TD>\n");
2149      }
2150      raw_printf(p->out,"</TR>\n");
2151      break;
2152    }
2153    case MODE_Tcl: {
2154      if( p->cnt++==0 && p->showHeader ){
2155        for(i=0; i<nArg; i++){
2156          output_c_string(p->out,azCol[i] ? azCol[i] : "");
2157          if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2158        }
2159        utf8_printf(p->out, "%s", p->rowSeparator);
2160      }
2161      if( azArg==0 ) break;
2162      for(i=0; i<nArg; i++){
2163        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2164        if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2165      }
2166      utf8_printf(p->out, "%s", p->rowSeparator);
2167      break;
2168    }
2169    case MODE_Csv: {
2170      setBinaryMode(p->out, 1);
2171      if( p->cnt++==0 && p->showHeader ){
2172        for(i=0; i<nArg; i++){
2173          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2174        }
2175        utf8_printf(p->out, "%s", p->rowSeparator);
2176      }
2177      if( nArg>0 ){
2178        for(i=0; i<nArg; i++){
2179          output_csv(p, azArg[i], i<nArg-1);
2180        }
2181        utf8_printf(p->out, "%s", p->rowSeparator);
2182      }
2183      setTextMode(p->out, 1);
2184      break;
2185    }
2186    case MODE_Insert: {
2187      if( azArg==0 ) break;
2188      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2189      if( p->showHeader ){
2190        raw_printf(p->out,"(");
2191        for(i=0; i<nArg; i++){
2192          if( i>0 ) raw_printf(p->out, ",");
2193          if( quoteChar(azCol[i]) ){
2194            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2195            utf8_printf(p->out, "%s", z);
2196            sqlite3_free(z);
2197          }else{
2198            raw_printf(p->out, "%s", azCol[i]);
2199          }
2200        }
2201        raw_printf(p->out,")");
2202      }
2203      p->cnt++;
2204      for(i=0; i<nArg; i++){
2205        raw_printf(p->out, i>0 ? "," : " VALUES(");
2206        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2207          utf8_printf(p->out,"NULL");
2208        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2209          if( ShellHasFlag(p, SHFLG_Newlines) ){
2210            output_quoted_string(p->out, azArg[i]);
2211          }else{
2212            output_quoted_escaped_string(p->out, azArg[i]);
2213          }
2214        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2215          utf8_printf(p->out,"%s", azArg[i]);
2216        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2217          char z[50];
2218          double r = sqlite3_column_double(p->pStmt, i);
2219          sqlite3_uint64 ur;
2220          memcpy(&ur,&r,sizeof(r));
2221          if( ur==0x7ff0000000000000LL ){
2222            raw_printf(p->out, "1e999");
2223          }else if( ur==0xfff0000000000000LL ){
2224            raw_printf(p->out, "-1e999");
2225          }else{
2226            sqlite3_snprintf(50,z,"%!.20g", r);
2227            raw_printf(p->out, "%s", z);
2228          }
2229        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2230          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2231          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2232          output_hex_blob(p->out, pBlob, nBlob);
2233        }else if( isNumber(azArg[i], 0) ){
2234          utf8_printf(p->out,"%s", azArg[i]);
2235        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2236          output_quoted_string(p->out, azArg[i]);
2237        }else{
2238          output_quoted_escaped_string(p->out, azArg[i]);
2239        }
2240      }
2241      raw_printf(p->out,");\n");
2242      break;
2243    }
2244    case MODE_Json: {
2245      if( azArg==0 ) break;
2246      if( p->cnt==0 ){
2247        fputs("[{", p->out);
2248      }else{
2249        fputs(",\n{", p->out);
2250      }
2251      p->cnt++;
2252      for(i=0; i<nArg; i++){
2253        output_json_string(p->out, azCol[i], -1);
2254        putc(':', p->out);
2255        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2256          fputs("null",p->out);
2257        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2258          char z[50];
2259          double r = sqlite3_column_double(p->pStmt, i);
2260          sqlite3_uint64 ur;
2261          memcpy(&ur,&r,sizeof(r));
2262          if( ur==0x7ff0000000000000LL ){
2263            raw_printf(p->out, "1e999");
2264          }else if( ur==0xfff0000000000000LL ){
2265            raw_printf(p->out, "-1e999");
2266          }else{
2267            sqlite3_snprintf(50,z,"%!.20g", r);
2268            raw_printf(p->out, "%s", z);
2269          }
2270        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2271          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2272          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2273          output_json_string(p->out, pBlob, nBlob);
2274        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2275          output_json_string(p->out, azArg[i], -1);
2276        }else{
2277          utf8_printf(p->out,"%s", azArg[i]);
2278        }
2279        if( i<nArg-1 ){
2280          putc(',', p->out);
2281        }
2282      }
2283      putc('}', p->out);
2284      break;
2285    }
2286    case MODE_Quote: {
2287      if( azArg==0 ) break;
2288      if( p->cnt==0 && p->showHeader ){
2289        for(i=0; i<nArg; i++){
2290          if( i>0 ) fputs(p->colSeparator, p->out);
2291          output_quoted_string(p->out, azCol[i]);
2292        }
2293        fputs(p->rowSeparator, p->out);
2294      }
2295      p->cnt++;
2296      for(i=0; i<nArg; i++){
2297        if( i>0 ) fputs(p->colSeparator, p->out);
2298        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2299          utf8_printf(p->out,"NULL");
2300        }else if( aiType && aiType[i]==SQLITE_TEXT ){
2301          output_quoted_string(p->out, azArg[i]);
2302        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2303          utf8_printf(p->out,"%s", azArg[i]);
2304        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2305          char z[50];
2306          double r = sqlite3_column_double(p->pStmt, i);
2307          sqlite3_snprintf(50,z,"%!.20g", r);
2308          raw_printf(p->out, "%s", z);
2309        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2310          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2311          int nBlob = sqlite3_column_bytes(p->pStmt, i);
2312          output_hex_blob(p->out, pBlob, nBlob);
2313        }else if( isNumber(azArg[i], 0) ){
2314          utf8_printf(p->out,"%s", azArg[i]);
2315        }else{
2316          output_quoted_string(p->out, azArg[i]);
2317        }
2318      }
2319      fputs(p->rowSeparator, p->out);
2320      break;
2321    }
2322    case MODE_Ascii: {
2323      if( p->cnt++==0 && p->showHeader ){
2324        for(i=0; i<nArg; i++){
2325          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2326          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2327        }
2328        utf8_printf(p->out, "%s", p->rowSeparator);
2329      }
2330      if( azArg==0 ) break;
2331      for(i=0; i<nArg; i++){
2332        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2333        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2334      }
2335      utf8_printf(p->out, "%s", p->rowSeparator);
2336      break;
2337    }
2338    case MODE_EQP: {
2339      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2340      break;
2341    }
2342  }
2343  return 0;
2344}
2345
2346/*
2347** This is the callback routine that the SQLite library
2348** invokes for each row of a query result.
2349*/
2350static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2351  /* since we don't have type info, call the shell_callback with a NULL value */
2352  return shell_callback(pArg, nArg, azArg, azCol, NULL);
2353}
2354
2355/*
2356** This is the callback routine from sqlite3_exec() that appends all
2357** output onto the end of a ShellText object.
2358*/
2359static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2360  ShellText *p = (ShellText*)pArg;
2361  int i;
2362  UNUSED_PARAMETER(az);
2363  if( azArg==0 ) return 0;
2364  if( p->n ) appendText(p, "|", 0);
2365  for(i=0; i<nArg; i++){
2366    if( i ) appendText(p, ",", 0);
2367    if( azArg[i] ) appendText(p, azArg[i], 0);
2368  }
2369  return 0;
2370}
2371
2372/*
2373** Generate an appropriate SELFTEST table in the main database.
2374*/
2375static void createSelftestTable(ShellState *p){
2376  char *zErrMsg = 0;
2377  sqlite3_exec(p->db,
2378    "SAVEPOINT selftest_init;\n"
2379    "CREATE TABLE IF NOT EXISTS selftest(\n"
2380    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
2381    "  op TEXT,\n"                   /* Operator:  memo run */
2382    "  cmd TEXT,\n"                  /* Command text */
2383    "  ans TEXT\n"                   /* Desired answer */
2384    ");"
2385    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2386    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2387    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2388    "         'memo','Tests generated by --init');\n"
2389    "INSERT INTO [_shell$self]\n"
2390    "  SELECT 'run',\n"
2391    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2392                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2393    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
2394                          "FROM sqlite_schema ORDER BY 2',224));\n"
2395    "INSERT INTO [_shell$self]\n"
2396    "  SELECT 'run',"
2397    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2398    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2399    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2400    "  FROM (\n"
2401    "    SELECT name FROM sqlite_schema\n"
2402    "     WHERE type='table'\n"
2403    "       AND name<>'selftest'\n"
2404    "       AND coalesce(rootpage,0)>0\n"
2405    "  )\n"
2406    " ORDER BY name;\n"
2407    "INSERT INTO [_shell$self]\n"
2408    "  VALUES('run','PRAGMA integrity_check','ok');\n"
2409    "INSERT INTO selftest(tno,op,cmd,ans)"
2410    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2411    "DROP TABLE [_shell$self];"
2412    ,0,0,&zErrMsg);
2413  if( zErrMsg ){
2414    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2415    sqlite3_free(zErrMsg);
2416  }
2417  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2418}
2419
2420
2421/*
2422** Set the destination table field of the ShellState structure to
2423** the name of the table given.  Escape any quote characters in the
2424** table name.
2425*/
2426static void set_table_name(ShellState *p, const char *zName){
2427  int i, n;
2428  char cQuote;
2429  char *z;
2430
2431  if( p->zDestTable ){
2432    free(p->zDestTable);
2433    p->zDestTable = 0;
2434  }
2435  if( zName==0 ) return;
2436  cQuote = quoteChar(zName);
2437  n = strlen30(zName);
2438  if( cQuote ) n += n+2;
2439  z = p->zDestTable = malloc( n+1 );
2440  if( z==0 ) shell_out_of_memory();
2441  n = 0;
2442  if( cQuote ) z[n++] = cQuote;
2443  for(i=0; zName[i]; i++){
2444    z[n++] = zName[i];
2445    if( zName[i]==cQuote ) z[n++] = cQuote;
2446  }
2447  if( cQuote ) z[n++] = cQuote;
2448  z[n] = 0;
2449}
2450
2451
2452/*
2453** Execute a query statement that will generate SQL output.  Print
2454** the result columns, comma-separated, on a line and then add a
2455** semicolon terminator to the end of that line.
2456**
2457** If the number of columns is 1 and that column contains text "--"
2458** then write the semicolon on a separate line.  That way, if a
2459** "--" comment occurs at the end of the statement, the comment
2460** won't consume the semicolon terminator.
2461*/
2462static int run_table_dump_query(
2463  ShellState *p,           /* Query context */
2464  const char *zSelect      /* SELECT statement to extract content */
2465){
2466  sqlite3_stmt *pSelect;
2467  int rc;
2468  int nResult;
2469  int i;
2470  const char *z;
2471  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2472  if( rc!=SQLITE_OK || !pSelect ){
2473    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2474                sqlite3_errmsg(p->db));
2475    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2476    return rc;
2477  }
2478  rc = sqlite3_step(pSelect);
2479  nResult = sqlite3_column_count(pSelect);
2480  while( rc==SQLITE_ROW ){
2481    z = (const char*)sqlite3_column_text(pSelect, 0);
2482    utf8_printf(p->out, "%s", z);
2483    for(i=1; i<nResult; i++){
2484      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2485    }
2486    if( z==0 ) z = "";
2487    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2488    if( z[0] ){
2489      raw_printf(p->out, "\n;\n");
2490    }else{
2491      raw_printf(p->out, ";\n");
2492    }
2493    rc = sqlite3_step(pSelect);
2494  }
2495  rc = sqlite3_finalize(pSelect);
2496  if( rc!=SQLITE_OK ){
2497    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2498                sqlite3_errmsg(p->db));
2499    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2500  }
2501  return rc;
2502}
2503
2504/*
2505** Allocate space and save off current error string.
2506*/
2507static char *save_err_msg(
2508  sqlite3 *db            /* Database to query */
2509){
2510  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2511  char *zErrMsg = sqlite3_malloc64(nErrMsg);
2512  if( zErrMsg ){
2513    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2514  }
2515  return zErrMsg;
2516}
2517
2518#ifdef __linux__
2519/*
2520** Attempt to display I/O stats on Linux using /proc/PID/io
2521*/
2522static void displayLinuxIoStats(FILE *out){
2523  FILE *in;
2524  char z[200];
2525  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2526  in = fopen(z, "rb");
2527  if( in==0 ) return;
2528  while( fgets(z, sizeof(z), in)!=0 ){
2529    static const struct {
2530      const char *zPattern;
2531      const char *zDesc;
2532    } aTrans[] = {
2533      { "rchar: ",                  "Bytes received by read():" },
2534      { "wchar: ",                  "Bytes sent to write():"    },
2535      { "syscr: ",                  "Read() system calls:"      },
2536      { "syscw: ",                  "Write() system calls:"     },
2537      { "read_bytes: ",             "Bytes read from storage:"  },
2538      { "write_bytes: ",            "Bytes written to storage:" },
2539      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
2540    };
2541    int i;
2542    for(i=0; i<ArraySize(aTrans); i++){
2543      int n = strlen30(aTrans[i].zPattern);
2544      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2545        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2546        break;
2547      }
2548    }
2549  }
2550  fclose(in);
2551}
2552#endif
2553
2554/*
2555** Display a single line of status using 64-bit values.
2556*/
2557static void displayStatLine(
2558  ShellState *p,            /* The shell context */
2559  char *zLabel,             /* Label for this one line */
2560  char *zFormat,            /* Format for the result */
2561  int iStatusCtrl,          /* Which status to display */
2562  int bReset                /* True to reset the stats */
2563){
2564  sqlite3_int64 iCur = -1;
2565  sqlite3_int64 iHiwtr = -1;
2566  int i, nPercent;
2567  char zLine[200];
2568  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2569  for(i=0, nPercent=0; zFormat[i]; i++){
2570    if( zFormat[i]=='%' ) nPercent++;
2571  }
2572  if( nPercent>1 ){
2573    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2574  }else{
2575    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2576  }
2577  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2578}
2579
2580/*
2581** Display memory stats.
2582*/
2583static int display_stats(
2584  sqlite3 *db,                /* Database to query */
2585  ShellState *pArg,           /* Pointer to ShellState */
2586  int bReset                  /* True to reset the stats */
2587){
2588  int iCur;
2589  int iHiwtr;
2590  FILE *out;
2591  if( pArg==0 || pArg->out==0 ) return 0;
2592  out = pArg->out;
2593
2594  if( pArg->pStmt && (pArg->statsOn & 2) ){
2595    int nCol, i, x;
2596    sqlite3_stmt *pStmt = pArg->pStmt;
2597    char z[100];
2598    nCol = sqlite3_column_count(pStmt);
2599    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2600    for(i=0; i<nCol; i++){
2601      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2602      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
2603#ifndef SQLITE_OMIT_DECLTYPE
2604      sqlite3_snprintf(30, z+x, "declared type:");
2605      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
2606#endif
2607#ifdef SQLITE_ENABLE_COLUMN_METADATA
2608      sqlite3_snprintf(30, z+x, "database name:");
2609      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2610      sqlite3_snprintf(30, z+x, "table name:");
2611      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2612      sqlite3_snprintf(30, z+x, "origin name:");
2613      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
2614#endif
2615    }
2616  }
2617
2618  displayStatLine(pArg, "Memory Used:",
2619     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2620  displayStatLine(pArg, "Number of Outstanding Allocations:",
2621     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2622  if( pArg->shellFlgs & SHFLG_Pagecache ){
2623    displayStatLine(pArg, "Number of Pcache Pages Used:",
2624       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2625  }
2626  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2627     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2628  displayStatLine(pArg, "Largest Allocation:",
2629     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2630  displayStatLine(pArg, "Largest Pcache Allocation:",
2631     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2632#ifdef YYTRACKMAXSTACKDEPTH
2633  displayStatLine(pArg, "Deepest Parser Stack:",
2634     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2635#endif
2636
2637  if( db ){
2638    if( pArg->shellFlgs & SHFLG_Lookaside ){
2639      iHiwtr = iCur = -1;
2640      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2641                        &iCur, &iHiwtr, bReset);
2642      raw_printf(pArg->out,
2643              "Lookaside Slots Used:                %d (max %d)\n",
2644              iCur, iHiwtr);
2645      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2646                        &iCur, &iHiwtr, bReset);
2647      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
2648              iHiwtr);
2649      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2650                        &iCur, &iHiwtr, bReset);
2651      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
2652              iHiwtr);
2653      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2654                        &iCur, &iHiwtr, bReset);
2655      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
2656              iHiwtr);
2657    }
2658    iHiwtr = iCur = -1;
2659    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2660    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
2661            iCur);
2662    iHiwtr = iCur = -1;
2663    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2664    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
2665    iHiwtr = iCur = -1;
2666    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2667    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
2668    iHiwtr = iCur = -1;
2669    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2670    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
2671    iHiwtr = iCur = -1;
2672    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2673    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
2674    iHiwtr = iCur = -1;
2675    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2676    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
2677            iCur);
2678    iHiwtr = iCur = -1;
2679    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2680    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
2681            iCur);
2682  }
2683
2684  if( pArg->pStmt ){
2685    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2686                               bReset);
2687    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
2688    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2689    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
2690    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2691    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
2692    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2693    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
2694    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
2695    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
2696    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2697    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
2698    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2699    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
2700  }
2701
2702#ifdef __linux__
2703  displayLinuxIoStats(pArg->out);
2704#endif
2705
2706  /* Do not remove this machine readable comment: extra-stats-output-here */
2707
2708  return 0;
2709}
2710
2711/*
2712** Display scan stats.
2713*/
2714static void display_scanstats(
2715  sqlite3 *db,                    /* Database to query */
2716  ShellState *pArg                /* Pointer to ShellState */
2717){
2718#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2719  UNUSED_PARAMETER(db);
2720  UNUSED_PARAMETER(pArg);
2721#else
2722  int i, k, n, mx;
2723  raw_printf(pArg->out, "-------- scanstats --------\n");
2724  mx = 0;
2725  for(k=0; k<=mx; k++){
2726    double rEstLoop = 1.0;
2727    for(i=n=0; 1; i++){
2728      sqlite3_stmt *p = pArg->pStmt;
2729      sqlite3_int64 nLoop, nVisit;
2730      double rEst;
2731      int iSid;
2732      const char *zExplain;
2733      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2734        break;
2735      }
2736      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2737      if( iSid>mx ) mx = iSid;
2738      if( iSid!=k ) continue;
2739      if( n==0 ){
2740        rEstLoop = (double)nLoop;
2741        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2742      }
2743      n++;
2744      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2745      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2746      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2747      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2748      rEstLoop *= rEst;
2749      raw_printf(pArg->out,
2750          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2751          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2752      );
2753    }
2754  }
2755  raw_printf(pArg->out, "---------------------------\n");
2756#endif
2757}
2758
2759/*
2760** Parameter azArray points to a zero-terminated array of strings. zStr
2761** points to a single nul-terminated string. Return non-zero if zStr
2762** is equal, according to strcmp(), to any of the strings in the array.
2763** Otherwise, return zero.
2764*/
2765static int str_in_array(const char *zStr, const char **azArray){
2766  int i;
2767  for(i=0; azArray[i]; i++){
2768    if( 0==strcmp(zStr, azArray[i]) ) return 1;
2769  }
2770  return 0;
2771}
2772
2773/*
2774** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2775** and populate the ShellState.aiIndent[] array with the number of
2776** spaces each opcode should be indented before it is output.
2777**
2778** The indenting rules are:
2779**
2780**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2781**       all opcodes that occur between the p2 jump destination and the opcode
2782**       itself by 2 spaces.
2783**
2784**     * For each "Goto", if the jump destination is earlier in the program
2785**       and ends on one of:
2786**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
2787**       or if the P1 parameter is one instead of zero,
2788**       then indent all opcodes between the earlier instruction
2789**       and "Goto" by 2 spaces.
2790*/
2791static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2792  const char *zSql;               /* The text of the SQL statement */
2793  const char *z;                  /* Used to check if this is an EXPLAIN */
2794  int *abYield = 0;               /* True if op is an OP_Yield */
2795  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
2796  int iOp;                        /* Index of operation in p->aiIndent[] */
2797
2798  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
2799  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2800                            "Rewind", 0 };
2801  const char *azGoto[] = { "Goto", 0 };
2802
2803  /* Try to figure out if this is really an EXPLAIN statement. If this
2804  ** cannot be verified, return early.  */
2805  if( sqlite3_column_count(pSql)!=8 ){
2806    p->cMode = p->mode;
2807    return;
2808  }
2809  zSql = sqlite3_sql(pSql);
2810  if( zSql==0 ) return;
2811  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2812  if( sqlite3_strnicmp(z, "explain", 7) ){
2813    p->cMode = p->mode;
2814    return;
2815  }
2816
2817  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2818    int i;
2819    int iAddr = sqlite3_column_int(pSql, 0);
2820    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2821
2822    /* Set p2 to the P2 field of the current opcode. Then, assuming that
2823    ** p2 is an instruction address, set variable p2op to the index of that
2824    ** instruction in the aiIndent[] array. p2 and p2op may be different if
2825    ** the current instruction is part of a sub-program generated by an
2826    ** SQL trigger or foreign key.  */
2827    int p2 = sqlite3_column_int(pSql, 3);
2828    int p2op = (p2 + (iOp-iAddr));
2829
2830    /* Grow the p->aiIndent array as required */
2831    if( iOp>=nAlloc ){
2832      if( iOp==0 ){
2833        /* Do further verfication that this is explain output.  Abort if
2834        ** it is not */
2835        static const char *explainCols[] = {
2836           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2837        int jj;
2838        for(jj=0; jj<ArraySize(explainCols); jj++){
2839          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2840            p->cMode = p->mode;
2841            sqlite3_reset(pSql);
2842            return;
2843          }
2844        }
2845      }
2846      nAlloc += 100;
2847      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2848      if( p->aiIndent==0 ) shell_out_of_memory();
2849      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2850      if( abYield==0 ) shell_out_of_memory();
2851    }
2852    abYield[iOp] = str_in_array(zOp, azYield);
2853    p->aiIndent[iOp] = 0;
2854    p->nIndent = iOp+1;
2855
2856    if( str_in_array(zOp, azNext) ){
2857      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2858    }
2859    if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2860     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2861    ){
2862      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2863    }
2864  }
2865
2866  p->iIndent = 0;
2867  sqlite3_free(abYield);
2868  sqlite3_reset(pSql);
2869}
2870
2871/*
2872** Free the array allocated by explain_data_prepare().
2873*/
2874static void explain_data_delete(ShellState *p){
2875  sqlite3_free(p->aiIndent);
2876  p->aiIndent = 0;
2877  p->nIndent = 0;
2878  p->iIndent = 0;
2879}
2880
2881/*
2882** Disable and restore .wheretrace and .selecttrace settings.
2883*/
2884#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2885extern unsigned int sqlite3_unsupported_selecttrace;
2886static int savedSelectTrace;
2887#endif
2888#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2889extern int sqlite3WhereTrace;
2890static int savedWhereTrace;
2891#endif
2892static void disable_debug_trace_modes(void){
2893#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2894  savedSelectTrace = sqlite3_unsupported_selecttrace;
2895  sqlite3_unsupported_selecttrace = 0;
2896#endif
2897#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2898  savedWhereTrace = sqlite3WhereTrace;
2899  sqlite3WhereTrace = 0;
2900#endif
2901}
2902static void restore_debug_trace_modes(void){
2903#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2904  sqlite3_unsupported_selecttrace = savedSelectTrace;
2905#endif
2906#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2907  sqlite3WhereTrace = savedWhereTrace;
2908#endif
2909}
2910
2911/* Create the TEMP table used to store parameter bindings */
2912static void bind_table_init(ShellState *p){
2913  int wrSchema = 0;
2914  int defensiveMode = 0;
2915  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2916  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
2917  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2918  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
2919  sqlite3_exec(p->db,
2920    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
2921    "  key TEXT PRIMARY KEY,\n"
2922    "  value ANY\n"
2923    ") WITHOUT ROWID;",
2924    0, 0, 0);
2925  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
2926  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
2927}
2928
2929/*
2930** Bind parameters on a prepared statement.
2931**
2932** Parameter bindings are taken from a TEMP table of the form:
2933**
2934**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
2935**    WITHOUT ROWID;
2936**
2937** No bindings occur if this table does not exist.  The name of the table
2938** begins with "sqlite_" so that it will not collide with ordinary application
2939** tables.  The table must be in the TEMP schema.
2940*/
2941static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2942  int nVar;
2943  int i;
2944  int rc;
2945  sqlite3_stmt *pQ = 0;
2946
2947  nVar = sqlite3_bind_parameter_count(pStmt);
2948  if( nVar==0 ) return;  /* Nothing to do */
2949  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
2950                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2951    return; /* Parameter table does not exist */
2952  }
2953  rc = sqlite3_prepare_v2(pArg->db,
2954          "SELECT value FROM temp.sqlite_parameters"
2955          " WHERE key=?1", -1, &pQ, 0);
2956  if( rc || pQ==0 ) return;
2957  for(i=1; i<=nVar; i++){
2958    char zNum[30];
2959    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2960    if( zVar==0 ){
2961      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2962      zVar = zNum;
2963    }
2964    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2965    if( sqlite3_step(pQ)==SQLITE_ROW ){
2966      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2967    }else{
2968      sqlite3_bind_null(pStmt, i);
2969    }
2970    sqlite3_reset(pQ);
2971  }
2972  sqlite3_finalize(pQ);
2973}
2974
2975/*
2976** UTF8 box-drawing characters.  Imagine box lines like this:
2977**
2978**           1
2979**           |
2980**       4 --+-- 2
2981**           |
2982**           3
2983**
2984** Each box characters has between 2 and 4 of the lines leading from
2985** the center.  The characters are here identified by the numbers of
2986** their corresponding lines.
2987*/
2988#define BOX_24   "\342\224\200"  /* U+2500 --- */
2989#define BOX_13   "\342\224\202"  /* U+2502  |  */
2990#define BOX_23   "\342\224\214"  /* U+250c  ,- */
2991#define BOX_34   "\342\224\220"  /* U+2510 -,  */
2992#define BOX_12   "\342\224\224"  /* U+2514  '- */
2993#define BOX_14   "\342\224\230"  /* U+2518 -'  */
2994#define BOX_123  "\342\224\234"  /* U+251c  |- */
2995#define BOX_134  "\342\224\244"  /* U+2524 -|  */
2996#define BOX_234  "\342\224\254"  /* U+252c -,- */
2997#define BOX_124  "\342\224\264"  /* U+2534 -'- */
2998#define BOX_1234 "\342\224\274"  /* U+253c -|- */
2999
3000/* Draw horizontal line N characters long using unicode box
3001** characters
3002*/
3003static void print_box_line(FILE *out, int N){
3004  const char zDash[] =
3005      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3006      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3007  const int nDash = sizeof(zDash) - 1;
3008  N *= 3;
3009  while( N>nDash ){
3010    utf8_printf(out, zDash);
3011    N -= nDash;
3012  }
3013  utf8_printf(out, "%.*s", N, zDash);
3014}
3015
3016/*
3017** Draw a horizontal separator for a MODE_Box table.
3018*/
3019static void print_box_row_separator(
3020  ShellState *p,
3021  int nArg,
3022  const char *zSep1,
3023  const char *zSep2,
3024  const char *zSep3
3025){
3026  int i;
3027  if( nArg>0 ){
3028    utf8_printf(p->out, "%s", zSep1);
3029    print_box_line(p->out, p->actualWidth[0]+2);
3030    for(i=1; i<nArg; i++){
3031      utf8_printf(p->out, "%s", zSep2);
3032      print_box_line(p->out, p->actualWidth[i]+2);
3033    }
3034    utf8_printf(p->out, "%s", zSep3);
3035  }
3036  fputs("\n", p->out);
3037}
3038
3039
3040
3041/*
3042** Run a prepared statement and output the result in one of the
3043** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3044** or MODE_Box.
3045**
3046** This is different from ordinary exec_prepared_stmt() in that
3047** it has to run the entire query and gather the results into memory
3048** first, in order to determine column widths, before providing
3049** any output.
3050*/
3051static void exec_prepared_stmt_columnar(
3052  ShellState *p,                        /* Pointer to ShellState */
3053  sqlite3_stmt *pStmt                   /* Statment to run */
3054){
3055  sqlite3_int64 nRow = 0;
3056  int nColumn = 0;
3057  char **azData = 0;
3058  sqlite3_int64 nAlloc = 0;
3059  const char *z;
3060  int rc;
3061  sqlite3_int64 i, nData;
3062  int j, nTotal, w, n;
3063  const char *colSep = 0;
3064  const char *rowSep = 0;
3065
3066  rc = sqlite3_step(pStmt);
3067  if( rc!=SQLITE_ROW ) return;
3068  nColumn = sqlite3_column_count(pStmt);
3069  nAlloc = nColumn*4;
3070  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3071  if( azData==0 ) shell_out_of_memory();
3072  for(i=0; i<nColumn; i++){
3073    azData[i] = strdup(sqlite3_column_name(pStmt,i));
3074  }
3075  do{
3076    if( (nRow+2)*nColumn >= nAlloc ){
3077      nAlloc *= 2;
3078      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3079      if( azData==0 ) shell_out_of_memory();
3080    }
3081    nRow++;
3082    for(i=0; i<nColumn; i++){
3083      z = (const char*)sqlite3_column_text(pStmt,i);
3084      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
3085    }
3086  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
3087  if( nColumn>p->nWidth ){
3088    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
3089    if( p->colWidth==0 ) shell_out_of_memory();
3090    for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3091    p->nWidth = nColumn;
3092    p->actualWidth = &p->colWidth[nColumn];
3093  }
3094  memset(p->actualWidth, 0, nColumn*sizeof(int));
3095  for(i=0; i<nColumn; i++){
3096    w = p->colWidth[i];
3097    if( w<0 ) w = -w;
3098    p->actualWidth[i] = w;
3099  }
3100  nTotal = nColumn*(nRow+1);
3101  for(i=0; i<nTotal; i++){
3102    z = azData[i];
3103    if( z==0 ) z = p->nullValue;
3104    n = strlenChar(z);
3105    j = i%nColumn;
3106    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3107  }
3108  if( seenInterrupt ) goto columnar_end;
3109  switch( p->cMode ){
3110    case MODE_Column: {
3111      colSep = "  ";
3112      rowSep = "\n";
3113      if( p->showHeader ){
3114        for(i=0; i<nColumn; i++){
3115          w = p->actualWidth[i];
3116          if( p->colWidth[i]<0 ) w = -w;
3117          utf8_width_print(p->out, w, azData[i]);
3118          fputs(i==nColumn-1?"\n":"  ", p->out);
3119        }
3120        for(i=0; i<nColumn; i++){
3121          print_dashes(p->out, p->actualWidth[i]);
3122          fputs(i==nColumn-1?"\n":"  ", p->out);
3123        }
3124      }
3125      break;
3126    }
3127    case MODE_Table: {
3128      colSep = " | ";
3129      rowSep = " |\n";
3130      print_row_separator(p, nColumn, "+");
3131      fputs("| ", p->out);
3132      for(i=0; i<nColumn; i++){
3133        w = p->actualWidth[i];
3134        n = strlenChar(azData[i]);
3135        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3136        fputs(i==nColumn-1?" |\n":" | ", p->out);
3137      }
3138      print_row_separator(p, nColumn, "+");
3139      break;
3140    }
3141    case MODE_Markdown: {
3142      colSep = " | ";
3143      rowSep = " |\n";
3144      fputs("| ", p->out);
3145      for(i=0; i<nColumn; i++){
3146        w = p->actualWidth[i];
3147        n = strlenChar(azData[i]);
3148        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3149        fputs(i==nColumn-1?" |\n":" | ", p->out);
3150      }
3151      print_row_separator(p, nColumn, "|");
3152      break;
3153    }
3154    case MODE_Box: {
3155      colSep = " " BOX_13 " ";
3156      rowSep = " " BOX_13 "\n";
3157      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3158      utf8_printf(p->out, BOX_13 " ");
3159      for(i=0; i<nColumn; i++){
3160        w = p->actualWidth[i];
3161        n = strlenChar(azData[i]);
3162        utf8_printf(p->out, "%*s%s%*s%s",
3163            (w-n)/2, "", azData[i], (w-n+1)/2, "",
3164            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3165      }
3166      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3167      break;
3168    }
3169  }
3170  for(i=nColumn, j=0; i<nTotal; i++, j++){
3171    if( j==0 && p->cMode!=MODE_Column ){
3172      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3173    }
3174    z = azData[i];
3175    if( z==0 ) z = p->nullValue;
3176    w = p->actualWidth[j];
3177    if( p->colWidth[j]<0 ) w = -w;
3178    utf8_width_print(p->out, w, z);
3179    if( j==nColumn-1 ){
3180      utf8_printf(p->out, "%s", rowSep);
3181      j = -1;
3182      if( seenInterrupt ) goto columnar_end;
3183    }else{
3184      utf8_printf(p->out, "%s", colSep);
3185    }
3186  }
3187  if( p->cMode==MODE_Table ){
3188    print_row_separator(p, nColumn, "+");
3189  }else if( p->cMode==MODE_Box ){
3190    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
3191  }
3192columnar_end:
3193  if( seenInterrupt ){
3194    utf8_printf(p->out, "Interrupt\n");
3195  }
3196  nData = (nRow+1)*nColumn;
3197  for(i=0; i<nData; i++) free(azData[i]);
3198  sqlite3_free(azData);
3199}
3200
3201/*
3202** Run a prepared statement
3203*/
3204static void exec_prepared_stmt(
3205  ShellState *pArg,                                /* Pointer to ShellState */
3206  sqlite3_stmt *pStmt                              /* Statment to run */
3207){
3208  int rc;
3209
3210  if( pArg->cMode==MODE_Column
3211   || pArg->cMode==MODE_Table
3212   || pArg->cMode==MODE_Box
3213   || pArg->cMode==MODE_Markdown
3214  ){
3215    exec_prepared_stmt_columnar(pArg, pStmt);
3216    return;
3217  }
3218
3219  /* perform the first step.  this will tell us if we
3220  ** have a result set or not and how wide it is.
3221  */
3222  rc = sqlite3_step(pStmt);
3223  /* if we have a result set... */
3224  if( SQLITE_ROW == rc ){
3225    /* allocate space for col name ptr, value ptr, and type */
3226    int nCol = sqlite3_column_count(pStmt);
3227    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3228    if( !pData ){
3229      rc = SQLITE_NOMEM;
3230    }else{
3231      char **azCols = (char **)pData;      /* Names of result columns */
3232      char **azVals = &azCols[nCol];       /* Results */
3233      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3234      int i, x;
3235      assert(sizeof(int) <= sizeof(char *));
3236      /* save off ptrs to column names */
3237      for(i=0; i<nCol; i++){
3238        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3239      }
3240      do{
3241        /* extract the data and data types */
3242        for(i=0; i<nCol; i++){
3243          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3244          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3245            azVals[i] = "";
3246          }else{
3247            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3248          }
3249          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3250            rc = SQLITE_NOMEM;
3251            break; /* from for */
3252          }
3253        } /* end for */
3254
3255        /* if data and types extracted successfully... */
3256        if( SQLITE_ROW == rc ){
3257          /* call the supplied callback with the result row data */
3258          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
3259            rc = SQLITE_ABORT;
3260          }else{
3261            rc = sqlite3_step(pStmt);
3262          }
3263        }
3264      } while( SQLITE_ROW == rc );
3265      sqlite3_free(pData);
3266      if( pArg->cMode==MODE_Json ){
3267        fputs("]\n", pArg->out);
3268      }
3269    }
3270  }
3271}
3272
3273#ifndef SQLITE_OMIT_VIRTUALTABLE
3274/*
3275** This function is called to process SQL if the previous shell command
3276** was ".expert". It passes the SQL in the second argument directly to
3277** the sqlite3expert object.
3278**
3279** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3280** code. In this case, (*pzErr) may be set to point to a buffer containing
3281** an English language error message. It is the responsibility of the
3282** caller to eventually free this buffer using sqlite3_free().
3283*/
3284static int expertHandleSQL(
3285  ShellState *pState,
3286  const char *zSql,
3287  char **pzErr
3288){
3289  assert( pState->expert.pExpert );
3290  assert( pzErr==0 || *pzErr==0 );
3291  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
3292}
3293
3294/*
3295** This function is called either to silently clean up the object
3296** created by the ".expert" command (if bCancel==1), or to generate a
3297** report from it and then clean it up (if bCancel==0).
3298**
3299** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
3300** code. In this case, (*pzErr) may be set to point to a buffer containing
3301** an English language error message. It is the responsibility of the
3302** caller to eventually free this buffer using sqlite3_free().
3303*/
3304static int expertFinish(
3305  ShellState *pState,
3306  int bCancel,
3307  char **pzErr
3308){
3309  int rc = SQLITE_OK;
3310  sqlite3expert *p = pState->expert.pExpert;
3311  assert( p );
3312  assert( bCancel || pzErr==0 || *pzErr==0 );
3313  if( bCancel==0 ){
3314    FILE *out = pState->out;
3315    int bVerbose = pState->expert.bVerbose;
3316
3317    rc = sqlite3_expert_analyze(p, pzErr);
3318    if( rc==SQLITE_OK ){
3319      int nQuery = sqlite3_expert_count(p);
3320      int i;
3321
3322      if( bVerbose ){
3323        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3324        raw_printf(out, "-- Candidates -----------------------------\n");
3325        raw_printf(out, "%s\n", zCand);
3326      }
3327      for(i=0; i<nQuery; i++){
3328        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3329        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3330        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3331        if( zIdx==0 ) zIdx = "(no new indexes)\n";
3332        if( bVerbose ){
3333          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3334          raw_printf(out, "%s\n\n", zSql);
3335        }
3336        raw_printf(out, "%s\n", zIdx);
3337        raw_printf(out, "%s\n", zEQP);
3338      }
3339    }
3340  }
3341  sqlite3_expert_destroy(p);
3342  pState->expert.pExpert = 0;
3343  return rc;
3344}
3345
3346/*
3347** Implementation of ".expert" dot command.
3348*/
3349static int expertDotCommand(
3350  ShellState *pState,             /* Current shell tool state */
3351  char **azArg,                   /* Array of arguments passed to dot command */
3352  int nArg                        /* Number of entries in azArg[] */
3353){
3354  int rc = SQLITE_OK;
3355  char *zErr = 0;
3356  int i;
3357  int iSample = 0;
3358
3359  assert( pState->expert.pExpert==0 );
3360  memset(&pState->expert, 0, sizeof(ExpertInfo));
3361
3362  for(i=1; rc==SQLITE_OK && i<nArg; i++){
3363    char *z = azArg[i];
3364    int n;
3365    if( z[0]=='-' && z[1]=='-' ) z++;
3366    n = strlen30(z);
3367    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3368      pState->expert.bVerbose = 1;
3369    }
3370    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3371      if( i==(nArg-1) ){
3372        raw_printf(stderr, "option requires an argument: %s\n", z);
3373        rc = SQLITE_ERROR;
3374      }else{
3375        iSample = (int)integerValue(azArg[++i]);
3376        if( iSample<0 || iSample>100 ){
3377          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3378          rc = SQLITE_ERROR;
3379        }
3380      }
3381    }
3382    else{
3383      raw_printf(stderr, "unknown option: %s\n", z);
3384      rc = SQLITE_ERROR;
3385    }
3386  }
3387
3388  if( rc==SQLITE_OK ){
3389    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3390    if( pState->expert.pExpert==0 ){
3391      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3392      rc = SQLITE_ERROR;
3393    }else{
3394      sqlite3_expert_config(
3395          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3396      );
3397    }
3398  }
3399
3400  return rc;
3401}
3402#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
3403
3404/*
3405** Execute a statement or set of statements.  Print
3406** any result rows/columns depending on the current mode
3407** set via the supplied callback.
3408**
3409** This is very similar to SQLite's built-in sqlite3_exec()
3410** function except it takes a slightly different callback
3411** and callback data argument.
3412*/
3413static int shell_exec(
3414  ShellState *pArg,                         /* Pointer to ShellState */
3415  const char *zSql,                         /* SQL to be evaluated */
3416  char **pzErrMsg                           /* Error msg written here */
3417){
3418  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
3419  int rc = SQLITE_OK;             /* Return Code */
3420  int rc2;
3421  const char *zLeftover;          /* Tail of unprocessed SQL */
3422  sqlite3 *db = pArg->db;
3423
3424  if( pzErrMsg ){
3425    *pzErrMsg = NULL;
3426  }
3427
3428#ifndef SQLITE_OMIT_VIRTUALTABLE
3429  if( pArg->expert.pExpert ){
3430    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3431    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3432  }
3433#endif
3434
3435  while( zSql[0] && (SQLITE_OK == rc) ){
3436    static const char *zStmtSql;
3437    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3438    if( SQLITE_OK != rc ){
3439      if( pzErrMsg ){
3440        *pzErrMsg = save_err_msg(db);
3441      }
3442    }else{
3443      if( !pStmt ){
3444        /* this happens for a comment or white-space */
3445        zSql = zLeftover;
3446        while( IsSpace(zSql[0]) ) zSql++;
3447        continue;
3448      }
3449      zStmtSql = sqlite3_sql(pStmt);
3450      if( zStmtSql==0 ) zStmtSql = "";
3451      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3452
3453      /* save off the prepared statment handle and reset row count */
3454      if( pArg ){
3455        pArg->pStmt = pStmt;
3456        pArg->cnt = 0;
3457      }
3458
3459      /* echo the sql statement if echo on */
3460      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3461        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3462      }
3463
3464      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3465      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
3466        sqlite3_stmt *pExplain;
3467        char *zEQP;
3468        int triggerEQP = 0;
3469        disable_debug_trace_modes();
3470        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3471        if( pArg->autoEQP>=AUTOEQP_trigger ){
3472          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3473        }
3474        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3475        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3476        if( rc==SQLITE_OK ){
3477          while( sqlite3_step(pExplain)==SQLITE_ROW ){
3478            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
3479            int iEqpId = sqlite3_column_int(pExplain, 0);
3480            int iParentId = sqlite3_column_int(pExplain, 1);
3481            if( zEQPLine==0 ) zEQPLine = "";
3482            if( zEQPLine[0]=='-' ) eqp_render(pArg);
3483            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
3484          }
3485          eqp_render(pArg);
3486        }
3487        sqlite3_finalize(pExplain);
3488        sqlite3_free(zEQP);
3489        if( pArg->autoEQP>=AUTOEQP_full ){
3490          /* Also do an EXPLAIN for ".eqp full" mode */
3491          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3492          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3493          if( rc==SQLITE_OK ){
3494            pArg->cMode = MODE_Explain;
3495            explain_data_prepare(pArg, pExplain);
3496            exec_prepared_stmt(pArg, pExplain);
3497            explain_data_delete(pArg);
3498          }
3499          sqlite3_finalize(pExplain);
3500          sqlite3_free(zEQP);
3501        }
3502        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3503          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3504          /* Reprepare pStmt before reactiving trace modes */
3505          sqlite3_finalize(pStmt);
3506          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3507          if( pArg ) pArg->pStmt = pStmt;
3508        }
3509        restore_debug_trace_modes();
3510      }
3511
3512      if( pArg ){
3513        pArg->cMode = pArg->mode;
3514        if( pArg->autoExplain ){
3515          if( sqlite3_stmt_isexplain(pStmt)==1 ){
3516            pArg->cMode = MODE_Explain;
3517          }
3518          if( sqlite3_stmt_isexplain(pStmt)==2 ){
3519            pArg->cMode = MODE_EQP;
3520          }
3521        }
3522
3523        /* If the shell is currently in ".explain" mode, gather the extra
3524        ** data required to add indents to the output.*/
3525        if( pArg->cMode==MODE_Explain ){
3526          explain_data_prepare(pArg, pStmt);
3527        }
3528      }
3529
3530      bind_prepared_stmt(pArg, pStmt);
3531      exec_prepared_stmt(pArg, pStmt);
3532      explain_data_delete(pArg);
3533      eqp_render(pArg);
3534
3535      /* print usage stats if stats on */
3536      if( pArg && pArg->statsOn ){
3537        display_stats(db, pArg, 0);
3538      }
3539
3540      /* print loop-counters if required */
3541      if( pArg && pArg->scanstatsOn ){
3542        display_scanstats(db, pArg);
3543      }
3544
3545      /* Finalize the statement just executed. If this fails, save a
3546      ** copy of the error message. Otherwise, set zSql to point to the
3547      ** next statement to execute. */
3548      rc2 = sqlite3_finalize(pStmt);
3549      if( rc!=SQLITE_NOMEM ) rc = rc2;
3550      if( rc==SQLITE_OK ){
3551        zSql = zLeftover;
3552        while( IsSpace(zSql[0]) ) zSql++;
3553      }else if( pzErrMsg ){
3554        *pzErrMsg = save_err_msg(db);
3555      }
3556
3557      /* clear saved stmt handle */
3558      if( pArg ){
3559        pArg->pStmt = NULL;
3560      }
3561    }
3562  } /* end while */
3563
3564  return rc;
3565}
3566
3567/*
3568** Release memory previously allocated by tableColumnList().
3569*/
3570static void freeColumnList(char **azCol){
3571  int i;
3572  for(i=1; azCol[i]; i++){
3573    sqlite3_free(azCol[i]);
3574  }
3575  /* azCol[0] is a static string */
3576  sqlite3_free(azCol);
3577}
3578
3579/*
3580** Return a list of pointers to strings which are the names of all
3581** columns in table zTab.   The memory to hold the names is dynamically
3582** allocated and must be released by the caller using a subsequent call
3583** to freeColumnList().
3584**
3585** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
3586** value that needs to be preserved, then azCol[0] is filled in with the
3587** name of the rowid column.
3588**
3589** The first regular column in the table is azCol[1].  The list is terminated
3590** by an entry with azCol[i]==0.
3591*/
3592static char **tableColumnList(ShellState *p, const char *zTab){
3593  char **azCol = 0;
3594  sqlite3_stmt *pStmt;
3595  char *zSql;
3596  int nCol = 0;
3597  int nAlloc = 0;
3598  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
3599  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
3600  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3601  int rc;
3602
3603  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3604  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3605  sqlite3_free(zSql);
3606  if( rc ) return 0;
3607  while( sqlite3_step(pStmt)==SQLITE_ROW ){
3608    if( nCol>=nAlloc-2 ){
3609      nAlloc = nAlloc*2 + nCol + 10;
3610      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3611      if( azCol==0 ) shell_out_of_memory();
3612    }
3613    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3614    if( sqlite3_column_int(pStmt, 5) ){
3615      nPK++;
3616      if( nPK==1
3617       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3618                          "INTEGER")==0
3619      ){
3620        isIPK = 1;
3621      }else{
3622        isIPK = 0;
3623      }
3624    }
3625  }
3626  sqlite3_finalize(pStmt);
3627  if( azCol==0 ) return 0;
3628  azCol[0] = 0;
3629  azCol[nCol+1] = 0;
3630
3631  /* The decision of whether or not a rowid really needs to be preserved
3632  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
3633  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
3634  ** rowids on tables where the rowid is inaccessible because there are other
3635  ** columns in the table named "rowid", "_rowid_", and "oid".
3636  */
3637  if( preserveRowid && isIPK ){
3638    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3639    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
3640    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3641    ** ROWID aliases.  To distinguish these cases, check to see if
3642    ** there is a "pk" entry in "PRAGMA index_list".  There will be
3643    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3644    */
3645    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3646                           " WHERE origin='pk'", zTab);
3647    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3648    sqlite3_free(zSql);
3649    if( rc ){
3650      freeColumnList(azCol);
3651      return 0;
3652    }
3653    rc = sqlite3_step(pStmt);
3654    sqlite3_finalize(pStmt);
3655    preserveRowid = rc==SQLITE_ROW;
3656  }
3657  if( preserveRowid ){
3658    /* Only preserve the rowid if we can find a name to use for the
3659    ** rowid */
3660    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3661    int i, j;
3662    for(j=0; j<3; j++){
3663      for(i=1; i<=nCol; i++){
3664        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3665      }
3666      if( i>nCol ){
3667        /* At this point, we know that azRowid[j] is not the name of any
3668        ** ordinary column in the table.  Verify that azRowid[j] is a valid
3669        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
3670        ** tables will fail this last check */
3671        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3672        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3673        break;
3674      }
3675    }
3676  }
3677  return azCol;
3678}
3679
3680/*
3681** Toggle the reverse_unordered_selects setting.
3682*/
3683static void toggleSelectOrder(sqlite3 *db){
3684  sqlite3_stmt *pStmt = 0;
3685  int iSetting = 0;
3686  char zStmt[100];
3687  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3688  if( sqlite3_step(pStmt)==SQLITE_ROW ){
3689    iSetting = sqlite3_column_int(pStmt, 0);
3690  }
3691  sqlite3_finalize(pStmt);
3692  sqlite3_snprintf(sizeof(zStmt), zStmt,
3693       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3694  sqlite3_exec(db, zStmt, 0, 0, 0);
3695}
3696
3697/*
3698** This is a different callback routine used for dumping the database.
3699** Each row received by this callback consists of a table name,
3700** the table type ("index" or "table") and SQL to create the table.
3701** This routine should print text sufficient to recreate the table.
3702*/
3703static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3704  int rc;
3705  const char *zTable;
3706  const char *zType;
3707  const char *zSql;
3708  ShellState *p = (ShellState *)pArg;
3709
3710  UNUSED_PARAMETER(azNotUsed);
3711  if( nArg!=3 || azArg==0 ) return 0;
3712  zTable = azArg[0];
3713  zType = azArg[1];
3714  zSql = azArg[2];
3715
3716  if( strcmp(zTable, "sqlite_sequence")==0 ){
3717    raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3718  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
3719    raw_printf(p->out, "ANALYZE sqlite_schema;\n");
3720  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3721    return 0;
3722  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3723    char *zIns;
3724    if( !p->writableSchema ){
3725      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3726      p->writableSchema = 1;
3727    }
3728    zIns = sqlite3_mprintf(
3729       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
3730       "VALUES('table','%q','%q',0,'%q');",
3731       zTable, zTable, zSql);
3732    utf8_printf(p->out, "%s\n", zIns);
3733    sqlite3_free(zIns);
3734    return 0;
3735  }else{
3736    printSchemaLine(p->out, zSql, ";\n");
3737  }
3738
3739  if( strcmp(zType, "table")==0 ){
3740    ShellText sSelect;
3741    ShellText sTable;
3742    char **azCol;
3743    int i;
3744    char *savedDestTable;
3745    int savedMode;
3746
3747    azCol = tableColumnList(p, zTable);
3748    if( azCol==0 ){
3749      p->nErr++;
3750      return 0;
3751    }
3752
3753    /* Always quote the table name, even if it appears to be pure ascii,
3754    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
3755    initText(&sTable);
3756    appendText(&sTable, zTable, quoteChar(zTable));
3757    /* If preserving the rowid, add a column list after the table name.
3758    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3759    ** instead of the usual "INSERT INTO tab VALUES(...)".
3760    */
3761    if( azCol[0] ){
3762      appendText(&sTable, "(", 0);
3763      appendText(&sTable, azCol[0], 0);
3764      for(i=1; azCol[i]; i++){
3765        appendText(&sTable, ",", 0);
3766        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3767      }
3768      appendText(&sTable, ")", 0);
3769    }
3770
3771    /* Build an appropriate SELECT statement */
3772    initText(&sSelect);
3773    appendText(&sSelect, "SELECT ", 0);
3774    if( azCol[0] ){
3775      appendText(&sSelect, azCol[0], 0);
3776      appendText(&sSelect, ",", 0);
3777    }
3778    for(i=1; azCol[i]; i++){
3779      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3780      if( azCol[i+1] ){
3781        appendText(&sSelect, ",", 0);
3782      }
3783    }
3784    freeColumnList(azCol);
3785    appendText(&sSelect, " FROM ", 0);
3786    appendText(&sSelect, zTable, quoteChar(zTable));
3787
3788    savedDestTable = p->zDestTable;
3789    savedMode = p->mode;
3790    p->zDestTable = sTable.z;
3791    p->mode = p->cMode = MODE_Insert;
3792    rc = shell_exec(p, sSelect.z, 0);
3793    if( (rc&0xff)==SQLITE_CORRUPT ){
3794      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3795      toggleSelectOrder(p->db);
3796      shell_exec(p, sSelect.z, 0);
3797      toggleSelectOrder(p->db);
3798    }
3799    p->zDestTable = savedDestTable;
3800    p->mode = savedMode;
3801    freeText(&sTable);
3802    freeText(&sSelect);
3803    if( rc ) p->nErr++;
3804  }
3805  return 0;
3806}
3807
3808/*
3809** Run zQuery.  Use dump_callback() as the callback routine so that
3810** the contents of the query are output as SQL statements.
3811**
3812** If we get a SQLITE_CORRUPT error, rerun the query after appending
3813** "ORDER BY rowid DESC" to the end.
3814*/
3815static int run_schema_dump_query(
3816  ShellState *p,
3817  const char *zQuery
3818){
3819  int rc;
3820  char *zErr = 0;
3821  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3822  if( rc==SQLITE_CORRUPT ){
3823    char *zQ2;
3824    int len = strlen30(zQuery);
3825    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3826    if( zErr ){
3827      utf8_printf(p->out, "/****** %s ******/\n", zErr);
3828      sqlite3_free(zErr);
3829      zErr = 0;
3830    }
3831    zQ2 = malloc( len+100 );
3832    if( zQ2==0 ) return rc;
3833    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3834    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3835    if( rc ){
3836      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3837    }else{
3838      rc = SQLITE_CORRUPT;
3839    }
3840    sqlite3_free(zErr);
3841    free(zQ2);
3842  }
3843  return rc;
3844}
3845
3846/*
3847** Text of help messages.
3848**
3849** The help text for each individual command begins with a line that starts
3850** with ".".  Subsequent lines are supplimental information.
3851**
3852** There must be two or more spaces between the end of the command and the
3853** start of the description of what that command does.
3854*/
3855static const char *(azHelp[]) = {
3856#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3857  ".archive ...             Manage SQL archives",
3858  "   Each command must have exactly one of the following options:",
3859  "     -c, --create               Create a new archive",
3860  "     -u, --update               Add or update files with changed mtime",
3861  "     -i, --insert               Like -u but always add even if unchanged",
3862  "     -t, --list                 List contents of archive",
3863  "     -x, --extract              Extract files from archive",
3864  "   Optional arguments:",
3865  "     -v, --verbose              Print each filename as it is processed",
3866  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
3867  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
3868  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
3869  "     -n, --dryrun               Show the SQL that would have occurred",
3870  "   Examples:",
3871  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
3872  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
3873  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
3874  "   See also:",
3875  "      http://sqlite.org/cli.html#sqlar_archive_support",
3876#endif
3877#ifndef SQLITE_OMIT_AUTHORIZATION
3878  ".auth ON|OFF             Show authorizer callbacks",
3879#endif
3880  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
3881  "       --append            Use the appendvfs",
3882  "       --async             Write to FILE without journal and fsync()",
3883  ".bail on|off             Stop after hitting an error.  Default OFF",
3884  ".binary on|off           Turn binary output on or off.  Default OFF",
3885  ".cd DIRECTORY            Change the working directory to DIRECTORY",
3886  ".changes on|off          Show number of rows changed by SQL",
3887  ".check GLOB              Fail if output since .testcase does not match",
3888  ".clone NEWDB             Clone data into NEWDB from the existing database",
3889  ".databases               List names and files of attached databases",
3890  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
3891  ".dbinfo ?DB?             Show status information about the database",
3892  ".dump ?TABLE?            Render database content as SQL",
3893  "   Options:",
3894  "     --preserve-rowids      Include ROWID values in the output",
3895  "     --newlines             Allow unescaped newline characters in output",
3896  "   TABLE is a LIKE pattern for the tables to dump",
3897  "   Additional LIKE patterns can be given in subsequent arguments",
3898  ".echo on|off             Turn command echo on or off",
3899  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
3900  "   Other Modes:",
3901#ifdef SQLITE_DEBUG
3902  "      test                  Show raw EXPLAIN QUERY PLAN output",
3903  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
3904#endif
3905  "      trigger               Like \"full\" but also show trigger bytecode",
3906  ".excel                   Display the output of next command in spreadsheet",
3907  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
3908  ".exit ?CODE?             Exit this program with return-code CODE",
3909  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
3910  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
3911  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
3912  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
3913  "   --help                  Show CMD details",
3914  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
3915  ".headers on|off          Turn display of headers on or off",
3916  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
3917  ".import FILE TABLE       Import data from FILE into TABLE",
3918  "   Options:",
3919  "     --ascii               Use \\037 and \\036 as column and row separators",
3920  "     --csv                 Use , and \\n as column and row separators",
3921  "     --skip N              Skip the first N rows of input",
3922  "     -v                    \"Verbose\" - increase auxiliary output",
3923  "   Notes:",
3924  "     *  If TABLE does not exist, it is created.  The first row of input",
3925  "        determines the column names.",
3926  "     *  If neither --csv or --ascii are used, the input mode is derived",
3927  "        from the \".mode\" output mode",
3928  "     *  If FILE begins with \"|\" then it is a command that generates the",
3929  "        input text.",
3930#ifndef SQLITE_OMIT_TEST_CONTROL
3931  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
3932#endif
3933  ".indexes ?TABLE?         Show names of indexes",
3934  "                           If TABLE is specified, only show indexes for",
3935  "                           tables matching TABLE using the LIKE operator.",
3936#ifdef SQLITE_ENABLE_IOTRACE
3937  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
3938#endif
3939  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
3940  ".lint OPTIONS            Report potential schema issues.",
3941  "     Options:",
3942  "        fkey-indexes     Find missing foreign key indexes",
3943#ifndef SQLITE_OMIT_LOAD_EXTENSION
3944  ".load FILE ?ENTRY?       Load an extension library",
3945#endif
3946  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
3947  ".mode MODE ?TABLE?       Set output mode",
3948  "   MODE is one of:",
3949  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
3950  "     box       Tables using unicode box-drawing characters",
3951  "     csv       Comma-separated values",
3952  "     column    Output in columns.  (See .width)",
3953  "     html      HTML <table> code",
3954  "     insert    SQL insert statements for TABLE",
3955  "     json      Results in a JSON array",
3956  "     line      One value per line",
3957  "     list      Values delimited by \"|\"",
3958  "     markdown  Markdown table format",
3959  "     quote     Escape answers as for SQL",
3960  "     table     ASCII-art table",
3961  "     tabs      Tab-separated values",
3962  "     tcl       TCL list elements",
3963  ".nullvalue STRING        Use STRING in place of NULL values",
3964  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
3965  "     If FILE begins with '|' then open as a pipe",
3966  "       --bom  Put a UTF8 byte-order mark at the beginning",
3967  "       -e     Send output to the system text editor",
3968  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
3969#ifdef SQLITE_DEBUG
3970  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
3971#endif
3972  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
3973  "     Options:",
3974  "        --append        Use appendvfs to append database to the end of FILE",
3975#ifdef SQLITE_ENABLE_DESERIALIZE
3976  "        --deserialize   Load into memory useing sqlite3_deserialize()",
3977  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
3978  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
3979#endif
3980  "        --new           Initialize FILE to an empty database",
3981  "        --nofollow      Do not follow symbolic links",
3982  "        --readonly      Open FILE readonly",
3983  "        --zip           FILE is a ZIP archive",
3984  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
3985  "   If FILE begins with '|' then open it as a pipe.",
3986  "   Options:",
3987  "     --bom                 Prefix output with a UTF8 byte-order mark",
3988  "     -e                    Send output to the system text editor",
3989  "     -x                    Send output as CSV to a spreadsheet",
3990  ".parameter CMD ...       Manage SQL parameter bindings",
3991  "   clear                   Erase all bindings",
3992  "   init                    Initialize the TEMP table that holds bindings",
3993  "   list                    List the current parameter bindings",
3994  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
3995  "                           PARAMETER should start with one of: $ : @ ?",
3996  "   unset PARAMETER         Remove PARAMETER from the binding table",
3997  ".print STRING...         Print literal STRING",
3998#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
3999  ".progress N              Invoke progress handler after every N opcodes",
4000  "   --limit N                 Interrupt after N progress callbacks",
4001  "   --once                    Do no more than one progress interrupt",
4002  "   --quiet|-q                No output except at interrupts",
4003  "   --reset                   Reset the count for each input and interrupt",
4004#endif
4005  ".prompt MAIN CONTINUE    Replace the standard prompts",
4006  ".quit                    Exit this program",
4007  ".read FILE               Read input from FILE",
4008#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4009  ".recover                 Recover as much data as possible from corrupt db.",
4010  "   --freelist-corrupt       Assume the freelist is corrupt",
4011  "   --recovery-db NAME       Store recovery metadata in database file NAME",
4012  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
4013  "   --no-rowids              Do not attempt to recover rowid values",
4014  "                            that are not also INTEGER PRIMARY KEYs",
4015#endif
4016  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
4017  ".save FILE               Write in-memory database into FILE",
4018  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
4019  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
4020  "     Options:",
4021  "         --indent            Try to pretty-print the schema",
4022  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
4023  "    Options:",
4024  "       --init               Create a new SELFTEST table",
4025  "       -v                   Verbose output",
4026  ".separator COL ?ROW?     Change the column and row separators",
4027#if defined(SQLITE_ENABLE_SESSION)
4028  ".session ?NAME? CMD ...  Create or control sessions",
4029  "   Subcommands:",
4030  "     attach TABLE             Attach TABLE",
4031  "     changeset FILE           Write a changeset into FILE",
4032  "     close                    Close one session",
4033  "     enable ?BOOLEAN?         Set or query the enable bit",
4034  "     filter GLOB...           Reject tables matching GLOBs",
4035  "     indirect ?BOOLEAN?       Mark or query the indirect status",
4036  "     isempty                  Query whether the session is empty",
4037  "     list                     List currently open session names",
4038  "     open DB NAME             Open a new session on DB",
4039  "     patchset FILE            Write a patchset into FILE",
4040  "   If ?NAME? is omitted, the first defined session is used.",
4041#endif
4042  ".sha3sum ...             Compute a SHA3 hash of database content",
4043  "    Options:",
4044  "      --schema              Also hash the sqlite_schema table",
4045  "      --sha3-224            Use the sha3-224 algorithm",
4046  "      --sha3-256            Use the sha3-256 algorithm (default)",
4047  "      --sha3-384            Use the sha3-384 algorithm",
4048  "      --sha3-512            Use the sha3-512 algorithm",
4049  "    Any other argument is a LIKE pattern for tables to hash",
4050#ifndef SQLITE_NOHAVE_SYSTEM
4051  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
4052#endif
4053  ".show                    Show the current values for various settings",
4054  ".stats ?on|off?          Show stats or turn stats on or off",
4055#ifndef SQLITE_NOHAVE_SYSTEM
4056  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
4057#endif
4058  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
4059  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
4060  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
4061  "                           Run \".testctrl\" with no arguments for details",
4062  ".timeout MS              Try opening locked tables for MS milliseconds",
4063  ".timer on|off            Turn SQL timer on or off",
4064#ifndef SQLITE_OMIT_TRACE
4065  ".trace ?OPTIONS?         Output each SQL statement as it is run",
4066  "    FILE                    Send output to FILE",
4067  "    stdout                  Send output to stdout",
4068  "    stderr                  Send output to stderr",
4069  "    off                     Disable tracing",
4070  "    --expanded              Expand query parameters",
4071#ifdef SQLITE_ENABLE_NORMALIZE
4072  "    --normalized            Normal the SQL statements",
4073#endif
4074  "    --plain                 Show SQL as it is input",
4075  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
4076  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
4077  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
4078  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
4079#endif /* SQLITE_OMIT_TRACE */
4080#ifdef SQLITE_DEBUG
4081  ".unmodule NAME ...       Unregister virtual table modules",
4082  "    --allexcept             Unregister everything except those named",
4083#endif
4084  ".vfsinfo ?AUX?           Information about the top-level VFS",
4085  ".vfslist                 List all available VFSes",
4086  ".vfsname ?AUX?           Print the name of the VFS stack",
4087  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
4088  "     Negative values right-justify",
4089};
4090
4091/*
4092** Output help text.
4093**
4094** zPattern describes the set of commands for which help text is provided.
4095** If zPattern is NULL, then show all commands, but only give a one-line
4096** description of each.
4097**
4098** Return the number of matches.
4099*/
4100static int showHelp(FILE *out, const char *zPattern){
4101  int i = 0;
4102  int j = 0;
4103  int n = 0;
4104  char *zPat;
4105  if( zPattern==0
4106   || zPattern[0]=='0'
4107   || strcmp(zPattern,"-a")==0
4108   || strcmp(zPattern,"-all")==0
4109   || strcmp(zPattern,"--all")==0
4110  ){
4111    /* Show all commands, but only one line per command */
4112    if( zPattern==0 ) zPattern = "";
4113    for(i=0; i<ArraySize(azHelp); i++){
4114      if( azHelp[i][0]=='.' || zPattern[0] ){
4115        utf8_printf(out, "%s\n", azHelp[i]);
4116        n++;
4117      }
4118    }
4119  }else{
4120    /* Look for commands that for which zPattern is an exact prefix */
4121    zPat = sqlite3_mprintf(".%s*", zPattern);
4122    for(i=0; i<ArraySize(azHelp); i++){
4123      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
4124        utf8_printf(out, "%s\n", azHelp[i]);
4125        j = i+1;
4126        n++;
4127      }
4128    }
4129    sqlite3_free(zPat);
4130    if( n ){
4131      if( n==1 ){
4132        /* when zPattern is a prefix of exactly one command, then include the
4133        ** details of that command, which should begin at offset j */
4134        while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
4135          utf8_printf(out, "%s\n", azHelp[j]);
4136          j++;
4137        }
4138      }
4139      return n;
4140    }
4141    /* Look for commands that contain zPattern anywhere.  Show the complete
4142    ** text of all commands that match. */
4143    zPat = sqlite3_mprintf("%%%s%%", zPattern);
4144    for(i=0; i<ArraySize(azHelp); i++){
4145      if( azHelp[i][0]=='.' ) j = i;
4146      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
4147        utf8_printf(out, "%s\n", azHelp[j]);
4148        while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
4149          j++;
4150          utf8_printf(out, "%s\n", azHelp[j]);
4151        }
4152        i = j;
4153        n++;
4154      }
4155    }
4156    sqlite3_free(zPat);
4157  }
4158  return n;
4159}
4160
4161/* Forward reference */
4162static int process_input(ShellState *p);
4163
4164/*
4165** Read the content of file zName into memory obtained from sqlite3_malloc64()
4166** and return a pointer to the buffer. The caller is responsible for freeing
4167** the memory.
4168**
4169** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4170** read.
4171**
4172** For convenience, a nul-terminator byte is always appended to the data read
4173** from the file before the buffer is returned. This byte is not included in
4174** the final value of (*pnByte), if applicable.
4175**
4176** NULL is returned if any error is encountered. The final value of *pnByte
4177** is undefined in this case.
4178*/
4179static char *readFile(const char *zName, int *pnByte){
4180  FILE *in = fopen(zName, "rb");
4181  long nIn;
4182  size_t nRead;
4183  char *pBuf;
4184  if( in==0 ) return 0;
4185  fseek(in, 0, SEEK_END);
4186  nIn = ftell(in);
4187  rewind(in);
4188  pBuf = sqlite3_malloc64( nIn+1 );
4189  if( pBuf==0 ){ fclose(in); return 0; }
4190  nRead = fread(pBuf, nIn, 1, in);
4191  fclose(in);
4192  if( nRead!=1 ){
4193    sqlite3_free(pBuf);
4194    return 0;
4195  }
4196  pBuf[nIn] = 0;
4197  if( pnByte ) *pnByte = nIn;
4198  return pBuf;
4199}
4200
4201#if defined(SQLITE_ENABLE_SESSION)
4202/*
4203** Close a single OpenSession object and release all of its associated
4204** resources.
4205*/
4206static void session_close(OpenSession *pSession){
4207  int i;
4208  sqlite3session_delete(pSession->p);
4209  sqlite3_free(pSession->zName);
4210  for(i=0; i<pSession->nFilter; i++){
4211    sqlite3_free(pSession->azFilter[i]);
4212  }
4213  sqlite3_free(pSession->azFilter);
4214  memset(pSession, 0, sizeof(OpenSession));
4215}
4216#endif
4217
4218/*
4219** Close all OpenSession objects and release all associated resources.
4220*/
4221#if defined(SQLITE_ENABLE_SESSION)
4222static void session_close_all(ShellState *p){
4223  int i;
4224  for(i=0; i<p->nSession; i++){
4225    session_close(&p->aSession[i]);
4226  }
4227  p->nSession = 0;
4228}
4229#else
4230# define session_close_all(X)
4231#endif
4232
4233/*
4234** Implementation of the xFilter function for an open session.  Omit
4235** any tables named by ".session filter" but let all other table through.
4236*/
4237#if defined(SQLITE_ENABLE_SESSION)
4238static int session_filter(void *pCtx, const char *zTab){
4239  OpenSession *pSession = (OpenSession*)pCtx;
4240  int i;
4241  for(i=0; i<pSession->nFilter; i++){
4242    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4243  }
4244  return 1;
4245}
4246#endif
4247
4248/*
4249** Try to deduce the type of file for zName based on its content.  Return
4250** one of the SHELL_OPEN_* constants.
4251**
4252** If the file does not exist or is empty but its name looks like a ZIP
4253** archive and the dfltZip flag is true, then assume it is a ZIP archive.
4254** Otherwise, assume an ordinary database regardless of the filename if
4255** the type cannot be determined from content.
4256*/
4257int deduceDatabaseType(const char *zName, int dfltZip){
4258  FILE *f = fopen(zName, "rb");
4259  size_t n;
4260  int rc = SHELL_OPEN_UNSPEC;
4261  char zBuf[100];
4262  if( f==0 ){
4263    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4264       return SHELL_OPEN_ZIPFILE;
4265    }else{
4266       return SHELL_OPEN_NORMAL;
4267    }
4268  }
4269  n = fread(zBuf, 16, 1, f);
4270  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
4271    fclose(f);
4272    return SHELL_OPEN_NORMAL;
4273  }
4274  fseek(f, -25, SEEK_END);
4275  n = fread(zBuf, 25, 1, f);
4276  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
4277    rc = SHELL_OPEN_APPENDVFS;
4278  }else{
4279    fseek(f, -22, SEEK_END);
4280    n = fread(zBuf, 22, 1, f);
4281    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
4282       && zBuf[3]==0x06 ){
4283      rc = SHELL_OPEN_ZIPFILE;
4284    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
4285      rc = SHELL_OPEN_ZIPFILE;
4286    }
4287  }
4288  fclose(f);
4289  return rc;
4290}
4291
4292#ifdef SQLITE_ENABLE_DESERIALIZE
4293/*
4294** Reconstruct an in-memory database using the output from the "dbtotxt"
4295** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
4296** is 0, then read from standard input.
4297*/
4298static unsigned char *readHexDb(ShellState *p, int *pnData){
4299  unsigned char *a = 0;
4300  int nLine;
4301  int n = 0;
4302  int pgsz = 0;
4303  int iOffset = 0;
4304  int j, k;
4305  int rc;
4306  FILE *in;
4307  unsigned int x[16];
4308  char zLine[1000];
4309  if( p->zDbFilename ){
4310    in = fopen(p->zDbFilename, "r");
4311    if( in==0 ){
4312      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
4313      return 0;
4314    }
4315    nLine = 0;
4316  }else{
4317    in = p->in;
4318    nLine = p->lineno;
4319    if( in==0 ) in = stdin;
4320  }
4321  *pnData = 0;
4322  nLine++;
4323  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4324  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4325  if( rc!=2 ) goto readHexDb_error;
4326  if( n<0 ) goto readHexDb_error;
4327  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4328  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
4329  a = sqlite3_malloc( n ? n : 1 );
4330  if( a==0 ){
4331    utf8_printf(stderr, "Out of memory!\n");
4332    goto readHexDb_error;
4333  }
4334  memset(a, 0, n);
4335  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4336    utf8_printf(stderr, "invalid pagesize\n");
4337    goto readHexDb_error;
4338  }
4339  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
4340    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4341    if( rc==2 ){
4342      iOffset = k;
4343      continue;
4344    }
4345    if( strncmp(zLine, "| end ", 6)==0 ){
4346      break;
4347    }
4348    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
4349                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4350                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4351    if( rc==17 ){
4352      k = iOffset+j;
4353      if( k+16<=n ){
4354        int ii;
4355        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
4356      }
4357    }
4358  }
4359  *pnData = n;
4360  if( in!=p->in ){
4361    fclose(in);
4362  }else{
4363    p->lineno = nLine;
4364  }
4365  return a;
4366
4367readHexDb_error:
4368  if( in!=p->in ){
4369    fclose(in);
4370  }else{
4371    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
4372      nLine++;
4373      if(strncmp(zLine, "| end ", 6)==0 ) break;
4374    }
4375    p->lineno = nLine;
4376  }
4377  sqlite3_free(a);
4378  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4379  return 0;
4380}
4381#endif /* SQLITE_ENABLE_DESERIALIZE */
4382
4383/*
4384** Scalar function "shell_int32". The first argument to this function
4385** must be a blob. The second a non-negative integer. This function
4386** reads and returns a 32-bit big-endian integer from byte
4387** offset (4*<arg2>) of the blob.
4388*/
4389static void shellInt32(
4390  sqlite3_context *context,
4391  int argc,
4392  sqlite3_value **argv
4393){
4394  const unsigned char *pBlob;
4395  int nBlob;
4396  int iInt;
4397
4398  UNUSED_PARAMETER(argc);
4399  nBlob = sqlite3_value_bytes(argv[0]);
4400  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4401  iInt = sqlite3_value_int(argv[1]);
4402
4403  if( iInt>=0 && (iInt+1)*4<=nBlob ){
4404    const unsigned char *a = &pBlob[iInt*4];
4405    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4406                       + ((sqlite3_int64)a[1]<<16)
4407                       + ((sqlite3_int64)a[2]<< 8)
4408                       + ((sqlite3_int64)a[3]<< 0);
4409    sqlite3_result_int64(context, iVal);
4410  }
4411}
4412
4413/*
4414** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4415** using "..." with internal double-quote characters doubled.
4416*/
4417static void shellIdQuote(
4418  sqlite3_context *context,
4419  int argc,
4420  sqlite3_value **argv
4421){
4422  const char *zName = (const char*)sqlite3_value_text(argv[0]);
4423  UNUSED_PARAMETER(argc);
4424  if( zName ){
4425    char *z = sqlite3_mprintf("\"%w\"", zName);
4426    sqlite3_result_text(context, z, -1, sqlite3_free);
4427  }
4428}
4429
4430/*
4431** Scalar function "shell_escape_crnl" used by the .recover command.
4432** The argument passed to this function is the output of built-in
4433** function quote(). If the first character of the input is "'",
4434** indicating that the value passed to quote() was a text value,
4435** then this function searches the input for "\n" and "\r" characters
4436** and adds a wrapper similar to the following:
4437**
4438**   replace(replace(<input>, '\n', char(10), '\r', char(13));
4439**
4440** Or, if the first character of the input is not "'", then a copy
4441** of the input is returned.
4442*/
4443static void shellEscapeCrnl(
4444  sqlite3_context *context,
4445  int argc,
4446  sqlite3_value **argv
4447){
4448  const char *zText = (const char*)sqlite3_value_text(argv[0]);
4449  UNUSED_PARAMETER(argc);
4450  if( zText[0]=='\'' ){
4451    int nText = sqlite3_value_bytes(argv[0]);
4452    int i;
4453    char zBuf1[20];
4454    char zBuf2[20];
4455    const char *zNL = 0;
4456    const char *zCR = 0;
4457    int nCR = 0;
4458    int nNL = 0;
4459
4460    for(i=0; zText[i]; i++){
4461      if( zNL==0 && zText[i]=='\n' ){
4462        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4463        nNL = (int)strlen(zNL);
4464      }
4465      if( zCR==0 && zText[i]=='\r' ){
4466        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4467        nCR = (int)strlen(zCR);
4468      }
4469    }
4470
4471    if( zNL || zCR ){
4472      int iOut = 0;
4473      i64 nMax = (nNL > nCR) ? nNL : nCR;
4474      i64 nAlloc = nMax * nText + (nMax+64)*2;
4475      char *zOut = (char*)sqlite3_malloc64(nAlloc);
4476      if( zOut==0 ){
4477        sqlite3_result_error_nomem(context);
4478        return;
4479      }
4480
4481      if( zNL && zCR ){
4482        memcpy(&zOut[iOut], "replace(replace(", 16);
4483        iOut += 16;
4484      }else{
4485        memcpy(&zOut[iOut], "replace(", 8);
4486        iOut += 8;
4487      }
4488      for(i=0; zText[i]; i++){
4489        if( zText[i]=='\n' ){
4490          memcpy(&zOut[iOut], zNL, nNL);
4491          iOut += nNL;
4492        }else if( zText[i]=='\r' ){
4493          memcpy(&zOut[iOut], zCR, nCR);
4494          iOut += nCR;
4495        }else{
4496          zOut[iOut] = zText[i];
4497          iOut++;
4498        }
4499      }
4500
4501      if( zNL ){
4502        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4503        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4504        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4505      }
4506      if( zCR ){
4507        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4508        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4509        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4510      }
4511
4512      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4513      sqlite3_free(zOut);
4514      return;
4515    }
4516  }
4517
4518  sqlite3_result_value(context, argv[0]);
4519}
4520
4521/* Flags for open_db().
4522**
4523** The default behavior of open_db() is to exit(1) if the database fails to
4524** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4525** but still returns without calling exit.
4526**
4527** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4528** ZIP archive if the file does not exist or is empty and its name matches
4529** the *.zip pattern.
4530*/
4531#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
4532#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
4533
4534/*
4535** Make sure the database is open.  If it is not, then open it.  If
4536** the database fails to open, print an error message and exit.
4537*/
4538static void open_db(ShellState *p, int openFlags){
4539  if( p->db==0 ){
4540    if( p->openMode==SHELL_OPEN_UNSPEC ){
4541      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4542        p->openMode = SHELL_OPEN_NORMAL;
4543      }else{
4544        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4545                             (openFlags & OPEN_DB_ZIPFILE)!=0);
4546      }
4547    }
4548    switch( p->openMode ){
4549      case SHELL_OPEN_APPENDVFS: {
4550        sqlite3_open_v2(p->zDbFilename, &p->db,
4551           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
4552        break;
4553      }
4554      case SHELL_OPEN_HEXDB:
4555      case SHELL_OPEN_DESERIALIZE: {
4556        sqlite3_open(0, &p->db);
4557        break;
4558      }
4559      case SHELL_OPEN_ZIPFILE: {
4560        sqlite3_open(":memory:", &p->db);
4561        break;
4562      }
4563      case SHELL_OPEN_READONLY: {
4564        sqlite3_open_v2(p->zDbFilename, &p->db,
4565            SQLITE_OPEN_READONLY|p->openFlags, 0);
4566        break;
4567      }
4568      case SHELL_OPEN_UNSPEC:
4569      case SHELL_OPEN_NORMAL: {
4570        sqlite3_open_v2(p->zDbFilename, &p->db,
4571           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
4572        break;
4573      }
4574    }
4575    globalDb = p->db;
4576    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4577      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4578          p->zDbFilename, sqlite3_errmsg(p->db));
4579      if( openFlags & OPEN_DB_KEEPALIVE ){
4580        sqlite3_open(":memory:", &p->db);
4581        return;
4582      }
4583      exit(1);
4584    }
4585#ifndef SQLITE_OMIT_LOAD_EXTENSION
4586    sqlite3_enable_load_extension(p->db, 1);
4587#endif
4588    sqlite3_fileio_init(p->db, 0, 0);
4589    sqlite3_shathree_init(p->db, 0, 0);
4590    sqlite3_completion_init(p->db, 0, 0);
4591    sqlite3_uint_init(p->db, 0, 0);
4592    sqlite3_decimal_init(p->db, 0, 0);
4593    sqlite3_ieee_init(p->db, 0, 0);
4594    sqlite3_series_init(p->db, 0, 0);
4595#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
4596    sqlite3_dbdata_init(p->db, 0, 0);
4597#endif
4598#ifdef SQLITE_HAVE_ZLIB
4599    sqlite3_zipfile_init(p->db, 0, 0);
4600    sqlite3_sqlar_init(p->db, 0, 0);
4601#endif
4602    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
4603                            shellAddSchemaName, 0, 0);
4604    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4605                            shellModuleSchema, 0, 0);
4606    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4607                            shellPutsFunc, 0, 0);
4608    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4609                            shellEscapeCrnl, 0, 0);
4610    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4611                            shellInt32, 0, 0);
4612    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4613                            shellIdQuote, 0, 0);
4614#ifndef SQLITE_NOHAVE_SYSTEM
4615    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4616                            editFunc, 0, 0);
4617    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4618                            editFunc, 0, 0);
4619#endif
4620    if( p->openMode==SHELL_OPEN_ZIPFILE ){
4621      char *zSql = sqlite3_mprintf(
4622         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4623      sqlite3_exec(p->db, zSql, 0, 0, 0);
4624      sqlite3_free(zSql);
4625    }
4626#ifdef SQLITE_ENABLE_DESERIALIZE
4627    else
4628    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
4629      int rc;
4630      int nData = 0;
4631      unsigned char *aData;
4632      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4633        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4634      }else{
4635        aData = readHexDb(p, &nData);
4636        if( aData==0 ){
4637          return;
4638        }
4639      }
4640      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
4641                   SQLITE_DESERIALIZE_RESIZEABLE |
4642                   SQLITE_DESERIALIZE_FREEONCLOSE);
4643      if( rc ){
4644        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4645      }
4646      if( p->szMax>0 ){
4647        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4648      }
4649    }
4650#endif
4651  }
4652}
4653
4654/*
4655** Attempt to close the databaes connection.  Report errors.
4656*/
4657void close_db(sqlite3 *db){
4658  int rc = sqlite3_close(db);
4659  if( rc ){
4660    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4661        rc, sqlite3_errmsg(db));
4662  }
4663}
4664
4665#if HAVE_READLINE || HAVE_EDITLINE
4666/*
4667** Readline completion callbacks
4668*/
4669static char *readline_completion_generator(const char *text, int state){
4670  static sqlite3_stmt *pStmt = 0;
4671  char *zRet;
4672  if( state==0 ){
4673    char *zSql;
4674    sqlite3_finalize(pStmt);
4675    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4676                           "  FROM completion(%Q) ORDER BY 1", text);
4677    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4678    sqlite3_free(zSql);
4679  }
4680  if( sqlite3_step(pStmt)==SQLITE_ROW ){
4681    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4682  }else{
4683    sqlite3_finalize(pStmt);
4684    pStmt = 0;
4685    zRet = 0;
4686  }
4687  return zRet;
4688}
4689static char **readline_completion(const char *zText, int iStart, int iEnd){
4690  rl_attempted_completion_over = 1;
4691  return rl_completion_matches(zText, readline_completion_generator);
4692}
4693
4694#elif HAVE_LINENOISE
4695/*
4696** Linenoise completion callback
4697*/
4698static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4699  int nLine = strlen30(zLine);
4700  int i, iStart;
4701  sqlite3_stmt *pStmt = 0;
4702  char *zSql;
4703  char zBuf[1000];
4704
4705  if( nLine>sizeof(zBuf)-30 ) return;
4706  if( zLine[0]=='.' || zLine[0]=='#') return;
4707  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4708  if( i==nLine-1 ) return;
4709  iStart = i+1;
4710  memcpy(zBuf, zLine, iStart);
4711  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4712                         "  FROM completion(%Q,%Q) ORDER BY 1",
4713                         &zLine[iStart], zLine);
4714  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4715  sqlite3_free(zSql);
4716  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4717  while( sqlite3_step(pStmt)==SQLITE_ROW ){
4718    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4719    int nCompletion = sqlite3_column_bytes(pStmt, 0);
4720    if( iStart+nCompletion < sizeof(zBuf)-1 ){
4721      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4722      linenoiseAddCompletion(lc, zBuf);
4723    }
4724  }
4725  sqlite3_finalize(pStmt);
4726}
4727#endif
4728
4729/*
4730** Do C-language style dequoting.
4731**
4732**    \a    -> alarm
4733**    \b    -> backspace
4734**    \t    -> tab
4735**    \n    -> newline
4736**    \v    -> vertical tab
4737**    \f    -> form feed
4738**    \r    -> carriage return
4739**    \s    -> space
4740**    \"    -> "
4741**    \'    -> '
4742**    \\    -> backslash
4743**    \NNN  -> ascii character NNN in octal
4744*/
4745static void resolve_backslashes(char *z){
4746  int i, j;
4747  char c;
4748  while( *z && *z!='\\' ) z++;
4749  for(i=j=0; (c = z[i])!=0; i++, j++){
4750    if( c=='\\' && z[i+1]!=0 ){
4751      c = z[++i];
4752      if( c=='a' ){
4753        c = '\a';
4754      }else if( c=='b' ){
4755        c = '\b';
4756      }else if( c=='t' ){
4757        c = '\t';
4758      }else if( c=='n' ){
4759        c = '\n';
4760      }else if( c=='v' ){
4761        c = '\v';
4762      }else if( c=='f' ){
4763        c = '\f';
4764      }else if( c=='r' ){
4765        c = '\r';
4766      }else if( c=='"' ){
4767        c = '"';
4768      }else if( c=='\'' ){
4769        c = '\'';
4770      }else if( c=='\\' ){
4771        c = '\\';
4772      }else if( c>='0' && c<='7' ){
4773        c -= '0';
4774        if( z[i+1]>='0' && z[i+1]<='7' ){
4775          i++;
4776          c = (c<<3) + z[i] - '0';
4777          if( z[i+1]>='0' && z[i+1]<='7' ){
4778            i++;
4779            c = (c<<3) + z[i] - '0';
4780          }
4781        }
4782      }
4783    }
4784    z[j] = c;
4785  }
4786  if( j<i ) z[j] = 0;
4787}
4788
4789/*
4790** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
4791** for TRUE and FALSE.  Return the integer value if appropriate.
4792*/
4793static int booleanValue(const char *zArg){
4794  int i;
4795  if( zArg[0]=='0' && zArg[1]=='x' ){
4796    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4797  }else{
4798    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4799  }
4800  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4801  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4802    return 1;
4803  }
4804  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4805    return 0;
4806  }
4807  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4808          zArg);
4809  return 0;
4810}
4811
4812/*
4813** Set or clear a shell flag according to a boolean value.
4814*/
4815static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4816  if( booleanValue(zArg) ){
4817    ShellSetFlag(p, mFlag);
4818  }else{
4819    ShellClearFlag(p, mFlag);
4820  }
4821}
4822
4823/*
4824** Close an output file, assuming it is not stderr or stdout
4825*/
4826static void output_file_close(FILE *f){
4827  if( f && f!=stdout && f!=stderr ) fclose(f);
4828}
4829
4830/*
4831** Try to open an output file.   The names "stdout" and "stderr" are
4832** recognized and do the right thing.  NULL is returned if the output
4833** filename is "off".
4834*/
4835static FILE *output_file_open(const char *zFile, int bTextMode){
4836  FILE *f;
4837  if( strcmp(zFile,"stdout")==0 ){
4838    f = stdout;
4839  }else if( strcmp(zFile, "stderr")==0 ){
4840    f = stderr;
4841  }else if( strcmp(zFile, "off")==0 ){
4842    f = 0;
4843  }else{
4844    f = fopen(zFile, bTextMode ? "w" : "wb");
4845    if( f==0 ){
4846      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4847    }
4848  }
4849  return f;
4850}
4851
4852#ifndef SQLITE_OMIT_TRACE
4853/*
4854** A routine for handling output from sqlite3_trace().
4855*/
4856static int sql_trace_callback(
4857  unsigned mType,         /* The trace type */
4858  void *pArg,             /* The ShellState pointer */
4859  void *pP,               /* Usually a pointer to sqlite_stmt */
4860  void *pX                /* Auxiliary output */
4861){
4862  ShellState *p = (ShellState*)pArg;
4863  sqlite3_stmt *pStmt;
4864  const char *zSql;
4865  int nSql;
4866  if( p->traceOut==0 ) return 0;
4867  if( mType==SQLITE_TRACE_CLOSE ){
4868    utf8_printf(p->traceOut, "-- closing database connection\n");
4869    return 0;
4870  }
4871  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4872    zSql = (const char*)pX;
4873  }else{
4874    pStmt = (sqlite3_stmt*)pP;
4875    switch( p->eTraceType ){
4876      case SHELL_TRACE_EXPANDED: {
4877        zSql = sqlite3_expanded_sql(pStmt);
4878        break;
4879      }
4880#ifdef SQLITE_ENABLE_NORMALIZE
4881      case SHELL_TRACE_NORMALIZED: {
4882        zSql = sqlite3_normalized_sql(pStmt);
4883        break;
4884      }
4885#endif
4886      default: {
4887        zSql = sqlite3_sql(pStmt);
4888        break;
4889      }
4890    }
4891  }
4892  if( zSql==0 ) return 0;
4893  nSql = strlen30(zSql);
4894  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4895  switch( mType ){
4896    case SQLITE_TRACE_ROW:
4897    case SQLITE_TRACE_STMT: {
4898      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4899      break;
4900    }
4901    case SQLITE_TRACE_PROFILE: {
4902      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4903      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4904      break;
4905    }
4906  }
4907  return 0;
4908}
4909#endif
4910
4911/*
4912** A no-op routine that runs with the ".breakpoint" doc-command.  This is
4913** a useful spot to set a debugger breakpoint.
4914*/
4915static void test_breakpoint(void){
4916  static int nCall = 0;
4917  nCall++;
4918}
4919
4920/*
4921** An object used to read a CSV and other files for import.
4922*/
4923typedef struct ImportCtx ImportCtx;
4924struct ImportCtx {
4925  const char *zFile;  /* Name of the input file */
4926  FILE *in;           /* Read the CSV text from this input stream */
4927  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
4928  char *z;            /* Accumulated text for a field */
4929  int n;              /* Number of bytes in z */
4930  int nAlloc;         /* Space allocated for z[] */
4931  int nLine;          /* Current line number */
4932  int nRow;           /* Number of rows imported */
4933  int nErr;           /* Number of errors encountered */
4934  int bNotFirst;      /* True if one or more bytes already read */
4935  int cTerm;          /* Character that terminated the most recent field */
4936  int cColSep;        /* The column separator character.  (Usually ",") */
4937  int cRowSep;        /* The row separator character.  (Usually "\n") */
4938};
4939
4940/* Clean up resourced used by an ImportCtx */
4941static void import_cleanup(ImportCtx *p){
4942  if( p->in!=0 && p->xCloser!=0 ){
4943    p->xCloser(p->in);
4944    p->in = 0;
4945  }
4946  sqlite3_free(p->z);
4947  p->z = 0;
4948}
4949
4950/* Append a single byte to z[] */
4951static void import_append_char(ImportCtx *p, int c){
4952  if( p->n+1>=p->nAlloc ){
4953    p->nAlloc += p->nAlloc + 100;
4954    p->z = sqlite3_realloc64(p->z, p->nAlloc);
4955    if( p->z==0 ) shell_out_of_memory();
4956  }
4957  p->z[p->n++] = (char)c;
4958}
4959
4960/* Read a single field of CSV text.  Compatible with rfc4180 and extended
4961** with the option of having a separator other than ",".
4962**
4963**   +  Input comes from p->in.
4964**   +  Store results in p->z of length p->n.  Space to hold p->z comes
4965**      from sqlite3_malloc64().
4966**   +  Use p->cSep as the column separator.  The default is ",".
4967**   +  Use p->rSep as the row separator.  The default is "\n".
4968**   +  Keep track of the line number in p->nLine.
4969**   +  Store the character that terminates the field in p->cTerm.  Store
4970**      EOF on end-of-file.
4971**   +  Report syntax errors on stderr
4972*/
4973static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4974  int c;
4975  int cSep = p->cColSep;
4976  int rSep = p->cRowSep;
4977  p->n = 0;
4978  c = fgetc(p->in);
4979  if( c==EOF || seenInterrupt ){
4980    p->cTerm = EOF;
4981    return 0;
4982  }
4983  if( c=='"' ){
4984    int pc, ppc;
4985    int startLine = p->nLine;
4986    int cQuote = c;
4987    pc = ppc = 0;
4988    while( 1 ){
4989      c = fgetc(p->in);
4990      if( c==rSep ) p->nLine++;
4991      if( c==cQuote ){
4992        if( pc==cQuote ){
4993          pc = 0;
4994          continue;
4995        }
4996      }
4997      if( (c==cSep && pc==cQuote)
4998       || (c==rSep && pc==cQuote)
4999       || (c==rSep && pc=='\r' && ppc==cQuote)
5000       || (c==EOF && pc==cQuote)
5001      ){
5002        do{ p->n--; }while( p->z[p->n]!=cQuote );
5003        p->cTerm = c;
5004        break;
5005      }
5006      if( pc==cQuote && c!='\r' ){
5007        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5008                p->zFile, p->nLine, cQuote);
5009      }
5010      if( c==EOF ){
5011        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5012                p->zFile, startLine, cQuote);
5013        p->cTerm = c;
5014        break;
5015      }
5016      import_append_char(p, c);
5017      ppc = pc;
5018      pc = c;
5019    }
5020  }else{
5021    /* If this is the first field being parsed and it begins with the
5022    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
5023    if( (c&0xff)==0xef && p->bNotFirst==0 ){
5024      import_append_char(p, c);
5025      c = fgetc(p->in);
5026      if( (c&0xff)==0xbb ){
5027        import_append_char(p, c);
5028        c = fgetc(p->in);
5029        if( (c&0xff)==0xbf ){
5030          p->bNotFirst = 1;
5031          p->n = 0;
5032          return csv_read_one_field(p);
5033        }
5034      }
5035    }
5036    while( c!=EOF && c!=cSep && c!=rSep ){
5037      import_append_char(p, c);
5038      c = fgetc(p->in);
5039    }
5040    if( c==rSep ){
5041      p->nLine++;
5042      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5043    }
5044    p->cTerm = c;
5045  }
5046  if( p->z ) p->z[p->n] = 0;
5047  p->bNotFirst = 1;
5048  return p->z;
5049}
5050
5051/* Read a single field of ASCII delimited text.
5052**
5053**   +  Input comes from p->in.
5054**   +  Store results in p->z of length p->n.  Space to hold p->z comes
5055**      from sqlite3_malloc64().
5056**   +  Use p->cSep as the column separator.  The default is "\x1F".
5057**   +  Use p->rSep as the row separator.  The default is "\x1E".
5058**   +  Keep track of the row number in p->nLine.
5059**   +  Store the character that terminates the field in p->cTerm.  Store
5060**      EOF on end-of-file.
5061**   +  Report syntax errors on stderr
5062*/
5063static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5064  int c;
5065  int cSep = p->cColSep;
5066  int rSep = p->cRowSep;
5067  p->n = 0;
5068  c = fgetc(p->in);
5069  if( c==EOF || seenInterrupt ){
5070    p->cTerm = EOF;
5071    return 0;
5072  }
5073  while( c!=EOF && c!=cSep && c!=rSep ){
5074    import_append_char(p, c);
5075    c = fgetc(p->in);
5076  }
5077  if( c==rSep ){
5078    p->nLine++;
5079  }
5080  p->cTerm = c;
5081  if( p->z ) p->z[p->n] = 0;
5082  return p->z;
5083}
5084
5085/*
5086** Try to transfer data for table zTable.  If an error is seen while
5087** moving forward, try to go backwards.  The backwards movement won't
5088** work for WITHOUT ROWID tables.
5089*/
5090static void tryToCloneData(
5091  ShellState *p,
5092  sqlite3 *newDb,
5093  const char *zTable
5094){
5095  sqlite3_stmt *pQuery = 0;
5096  sqlite3_stmt *pInsert = 0;
5097  char *zQuery = 0;
5098  char *zInsert = 0;
5099  int rc;
5100  int i, j, n;
5101  int nTable = strlen30(zTable);
5102  int k = 0;
5103  int cnt = 0;
5104  const int spinRate = 10000;
5105
5106  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
5107  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5108  if( rc ){
5109    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5110            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5111            zQuery);
5112    goto end_data_xfer;
5113  }
5114  n = sqlite3_column_count(pQuery);
5115  zInsert = sqlite3_malloc64(200 + nTable + n*3);
5116  if( zInsert==0 ) shell_out_of_memory();
5117  sqlite3_snprintf(200+nTable,zInsert,
5118                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
5119  i = strlen30(zInsert);
5120  for(j=1; j<n; j++){
5121    memcpy(zInsert+i, ",?", 2);
5122    i += 2;
5123  }
5124  memcpy(zInsert+i, ");", 3);
5125  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
5126  if( rc ){
5127    utf8_printf(stderr, "Error %d: %s on [%s]\n",
5128            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
5129            zQuery);
5130    goto end_data_xfer;
5131  }
5132  for(k=0; k<2; k++){
5133    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5134      for(i=0; i<n; i++){
5135        switch( sqlite3_column_type(pQuery, i) ){
5136          case SQLITE_NULL: {
5137            sqlite3_bind_null(pInsert, i+1);
5138            break;
5139          }
5140          case SQLITE_INTEGER: {
5141            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
5142            break;
5143          }
5144          case SQLITE_FLOAT: {
5145            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
5146            break;
5147          }
5148          case SQLITE_TEXT: {
5149            sqlite3_bind_text(pInsert, i+1,
5150                             (const char*)sqlite3_column_text(pQuery,i),
5151                             -1, SQLITE_STATIC);
5152            break;
5153          }
5154          case SQLITE_BLOB: {
5155            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
5156                                            sqlite3_column_bytes(pQuery,i),
5157                                            SQLITE_STATIC);
5158            break;
5159          }
5160        }
5161      } /* End for */
5162      rc = sqlite3_step(pInsert);
5163      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
5164        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
5165                        sqlite3_errmsg(newDb));
5166      }
5167      sqlite3_reset(pInsert);
5168      cnt++;
5169      if( (cnt%spinRate)==0 ){
5170        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
5171        fflush(stdout);
5172      }
5173    } /* End while */
5174    if( rc==SQLITE_DONE ) break;
5175    sqlite3_finalize(pQuery);
5176    sqlite3_free(zQuery);
5177    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
5178                             zTable);
5179    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5180    if( rc ){
5181      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
5182      break;
5183    }
5184  } /* End for(k=0...) */
5185
5186end_data_xfer:
5187  sqlite3_finalize(pQuery);
5188  sqlite3_finalize(pInsert);
5189  sqlite3_free(zQuery);
5190  sqlite3_free(zInsert);
5191}
5192
5193
5194/*
5195** Try to transfer all rows of the schema that match zWhere.  For
5196** each row, invoke xForEach() on the object defined by that row.
5197** If an error is encountered while moving forward through the
5198** sqlite_schema table, try again moving backwards.
5199*/
5200static void tryToCloneSchema(
5201  ShellState *p,
5202  sqlite3 *newDb,
5203  const char *zWhere,
5204  void (*xForEach)(ShellState*,sqlite3*,const char*)
5205){
5206  sqlite3_stmt *pQuery = 0;
5207  char *zQuery = 0;
5208  int rc;
5209  const unsigned char *zName;
5210  const unsigned char *zSql;
5211  char *zErrMsg = 0;
5212
5213  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5214                           " WHERE %s", zWhere);
5215  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5216  if( rc ){
5217    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5218                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5219                    zQuery);
5220    goto end_schema_xfer;
5221  }
5222  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5223    zName = sqlite3_column_text(pQuery, 0);
5224    zSql = sqlite3_column_text(pQuery, 1);
5225    printf("%s... ", zName); fflush(stdout);
5226    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5227    if( zErrMsg ){
5228      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5229      sqlite3_free(zErrMsg);
5230      zErrMsg = 0;
5231    }
5232    if( xForEach ){
5233      xForEach(p, newDb, (const char*)zName);
5234    }
5235    printf("done\n");
5236  }
5237  if( rc!=SQLITE_DONE ){
5238    sqlite3_finalize(pQuery);
5239    sqlite3_free(zQuery);
5240    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
5241                             " WHERE %s ORDER BY rowid DESC", zWhere);
5242    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
5243    if( rc ){
5244      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
5245                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
5246                      zQuery);
5247      goto end_schema_xfer;
5248    }
5249    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
5250      zName = sqlite3_column_text(pQuery, 0);
5251      zSql = sqlite3_column_text(pQuery, 1);
5252      printf("%s... ", zName); fflush(stdout);
5253      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
5254      if( zErrMsg ){
5255        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
5256        sqlite3_free(zErrMsg);
5257        zErrMsg = 0;
5258      }
5259      if( xForEach ){
5260        xForEach(p, newDb, (const char*)zName);
5261      }
5262      printf("done\n");
5263    }
5264  }
5265end_schema_xfer:
5266  sqlite3_finalize(pQuery);
5267  sqlite3_free(zQuery);
5268}
5269
5270/*
5271** Open a new database file named "zNewDb".  Try to recover as much information
5272** as possible out of the main database (which might be corrupt) and write it
5273** into zNewDb.
5274*/
5275static void tryToClone(ShellState *p, const char *zNewDb){
5276  int rc;
5277  sqlite3 *newDb = 0;
5278  if( access(zNewDb,0)==0 ){
5279    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
5280    return;
5281  }
5282  rc = sqlite3_open(zNewDb, &newDb);
5283  if( rc ){
5284    utf8_printf(stderr, "Cannot create output database: %s\n",
5285            sqlite3_errmsg(newDb));
5286  }else{
5287    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
5288    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
5289    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
5290    tryToCloneSchema(p, newDb, "type!='table'", 0);
5291    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
5292    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5293  }
5294  close_db(newDb);
5295}
5296
5297/*
5298** Change the output file back to stdout.
5299**
5300** If the p->doXdgOpen flag is set, that means the output was being
5301** redirected to a temporary file named by p->zTempFile.  In that case,
5302** launch start/open/xdg-open on that temporary file.
5303*/
5304static void output_reset(ShellState *p){
5305  if( p->outfile[0]=='|' ){
5306#ifndef SQLITE_OMIT_POPEN
5307    pclose(p->out);
5308#endif
5309  }else{
5310    output_file_close(p->out);
5311#ifndef SQLITE_NOHAVE_SYSTEM
5312    if( p->doXdgOpen ){
5313      const char *zXdgOpenCmd =
5314#if defined(_WIN32)
5315      "start";
5316#elif defined(__APPLE__)
5317      "open";
5318#else
5319      "xdg-open";
5320#endif
5321      char *zCmd;
5322      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
5323      if( system(zCmd) ){
5324        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
5325      }else{
5326        /* Give the start/open/xdg-open command some time to get
5327        ** going before we continue, and potential delete the
5328        ** p->zTempFile data file out from under it */
5329        sqlite3_sleep(2000);
5330      }
5331      sqlite3_free(zCmd);
5332      outputModePop(p);
5333      p->doXdgOpen = 0;
5334    }
5335#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
5336  }
5337  p->outfile[0] = 0;
5338  p->out = stdout;
5339}
5340
5341/*
5342** Run an SQL command and return the single integer result.
5343*/
5344static int db_int(ShellState *p, const char *zSql){
5345  sqlite3_stmt *pStmt;
5346  int res = 0;
5347  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5348  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5349    res = sqlite3_column_int(pStmt,0);
5350  }
5351  sqlite3_finalize(pStmt);
5352  return res;
5353}
5354
5355/*
5356** Convert a 2-byte or 4-byte big-endian integer into a native integer
5357*/
5358static unsigned int get2byteInt(unsigned char *a){
5359  return (a[0]<<8) + a[1];
5360}
5361static unsigned int get4byteInt(unsigned char *a){
5362  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5363}
5364
5365/*
5366** Implementation of the ".dbinfo" command.
5367**
5368** Return 1 on error, 2 to exit, and 0 otherwise.
5369*/
5370static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5371  static const struct { const char *zName; int ofst; } aField[] = {
5372     { "file change counter:",  24  },
5373     { "database page count:",  28  },
5374     { "freelist page count:",  36  },
5375     { "schema cookie:",        40  },
5376     { "schema format:",        44  },
5377     { "default cache size:",   48  },
5378     { "autovacuum top root:",  52  },
5379     { "incremental vacuum:",   64  },
5380     { "text encoding:",        56  },
5381     { "user version:",         60  },
5382     { "application id:",       68  },
5383     { "software version:",     96  },
5384  };
5385  static const struct { const char *zName; const char *zSql; } aQuery[] = {
5386     { "number of tables:",
5387       "SELECT count(*) FROM %s WHERE type='table'" },
5388     { "number of indexes:",
5389       "SELECT count(*) FROM %s WHERE type='index'" },
5390     { "number of triggers:",
5391       "SELECT count(*) FROM %s WHERE type='trigger'" },
5392     { "number of views:",
5393       "SELECT count(*) FROM %s WHERE type='view'" },
5394     { "schema size:",
5395       "SELECT total(length(sql)) FROM %s" },
5396  };
5397  int i, rc;
5398  unsigned iDataVersion;
5399  char *zSchemaTab;
5400  char *zDb = nArg>=2 ? azArg[1] : "main";
5401  sqlite3_stmt *pStmt = 0;
5402  unsigned char aHdr[100];
5403  open_db(p, 0);
5404  if( p->db==0 ) return 1;
5405  rc = sqlite3_prepare_v2(p->db,
5406             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5407             -1, &pStmt, 0);
5408  if( rc ){
5409    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
5410    sqlite3_finalize(pStmt);
5411    return 1;
5412  }
5413  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5414  if( sqlite3_step(pStmt)==SQLITE_ROW
5415   && sqlite3_column_bytes(pStmt,0)>100
5416  ){
5417    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5418    sqlite3_finalize(pStmt);
5419  }else{
5420    raw_printf(stderr, "unable to read database header\n");
5421    sqlite3_finalize(pStmt);
5422    return 1;
5423  }
5424  i = get2byteInt(aHdr+16);
5425  if( i==1 ) i = 65536;
5426  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5427  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5428  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5429  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5430  for(i=0; i<ArraySize(aField); i++){
5431    int ofst = aField[i].ofst;
5432    unsigned int val = get4byteInt(aHdr + ofst);
5433    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5434    switch( ofst ){
5435      case 56: {
5436        if( val==1 ) raw_printf(p->out, " (utf8)");
5437        if( val==2 ) raw_printf(p->out, " (utf16le)");
5438        if( val==3 ) raw_printf(p->out, " (utf16be)");
5439      }
5440    }
5441    raw_printf(p->out, "\n");
5442  }
5443  if( zDb==0 ){
5444    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
5445  }else if( strcmp(zDb,"temp")==0 ){
5446    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
5447  }else{
5448    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
5449  }
5450  for(i=0; i<ArraySize(aQuery); i++){
5451    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5452    int val = db_int(p, zSql);
5453    sqlite3_free(zSql);
5454    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5455  }
5456  sqlite3_free(zSchemaTab);
5457  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5458  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
5459  return 0;
5460}
5461
5462/*
5463** Print the current sqlite3_errmsg() value to stderr and return 1.
5464*/
5465static int shellDatabaseError(sqlite3 *db){
5466  const char *zErr = sqlite3_errmsg(db);
5467  utf8_printf(stderr, "Error: %s\n", zErr);
5468  return 1;
5469}
5470
5471/*
5472** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
5473** if they match and FALSE (0) if they do not match.
5474**
5475** Globbing rules:
5476**
5477**      '*'       Matches any sequence of zero or more characters.
5478**
5479**      '?'       Matches exactly one character.
5480**
5481**     [...]      Matches one character from the enclosed list of
5482**                characters.
5483**
5484**     [^...]     Matches one character not in the enclosed list.
5485**
5486**      '#'       Matches any sequence of one or more digits with an
5487**                optional + or - sign in front
5488**
5489**      ' '       Any span of whitespace matches any other span of
5490**                whitespace.
5491**
5492** Extra whitespace at the end of z[] is ignored.
5493*/
5494static int testcase_glob(const char *zGlob, const char *z){
5495  int c, c2;
5496  int invert;
5497  int seen;
5498
5499  while( (c = (*(zGlob++)))!=0 ){
5500    if( IsSpace(c) ){
5501      if( !IsSpace(*z) ) return 0;
5502      while( IsSpace(*zGlob) ) zGlob++;
5503      while( IsSpace(*z) ) z++;
5504    }else if( c=='*' ){
5505      while( (c=(*(zGlob++))) == '*' || c=='?' ){
5506        if( c=='?' && (*(z++))==0 ) return 0;
5507      }
5508      if( c==0 ){
5509        return 1;
5510      }else if( c=='[' ){
5511        while( *z && testcase_glob(zGlob-1,z)==0 ){
5512          z++;
5513        }
5514        return (*z)!=0;
5515      }
5516      while( (c2 = (*(z++)))!=0 ){
5517        while( c2!=c ){
5518          c2 = *(z++);
5519          if( c2==0 ) return 0;
5520        }
5521        if( testcase_glob(zGlob,z) ) return 1;
5522      }
5523      return 0;
5524    }else if( c=='?' ){
5525      if( (*(z++))==0 ) return 0;
5526    }else if( c=='[' ){
5527      int prior_c = 0;
5528      seen = 0;
5529      invert = 0;
5530      c = *(z++);
5531      if( c==0 ) return 0;
5532      c2 = *(zGlob++);
5533      if( c2=='^' ){
5534        invert = 1;
5535        c2 = *(zGlob++);
5536      }
5537      if( c2==']' ){
5538        if( c==']' ) seen = 1;
5539        c2 = *(zGlob++);
5540      }
5541      while( c2 && c2!=']' ){
5542        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5543          c2 = *(zGlob++);
5544          if( c>=prior_c && c<=c2 ) seen = 1;
5545          prior_c = 0;
5546        }else{
5547          if( c==c2 ){
5548            seen = 1;
5549          }
5550          prior_c = c2;
5551        }
5552        c2 = *(zGlob++);
5553      }
5554      if( c2==0 || (seen ^ invert)==0 ) return 0;
5555    }else if( c=='#' ){
5556      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5557      if( !IsDigit(z[0]) ) return 0;
5558      z++;
5559      while( IsDigit(z[0]) ){ z++; }
5560    }else{
5561      if( c!=(*(z++)) ) return 0;
5562    }
5563  }
5564  while( IsSpace(*z) ){ z++; }
5565  return *z==0;
5566}
5567
5568
5569/*
5570** Compare the string as a command-line option with either one or two
5571** initial "-" characters.
5572*/
5573static int optionMatch(const char *zStr, const char *zOpt){
5574  if( zStr[0]!='-' ) return 0;
5575  zStr++;
5576  if( zStr[0]=='-' ) zStr++;
5577  return strcmp(zStr, zOpt)==0;
5578}
5579
5580/*
5581** Delete a file.
5582*/
5583int shellDeleteFile(const char *zFilename){
5584  int rc;
5585#ifdef _WIN32
5586  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5587  rc = _wunlink(z);
5588  sqlite3_free(z);
5589#else
5590  rc = unlink(zFilename);
5591#endif
5592  return rc;
5593}
5594
5595/*
5596** Try to delete the temporary file (if there is one) and free the
5597** memory used to hold the name of the temp file.
5598*/
5599static void clearTempFile(ShellState *p){
5600  if( p->zTempFile==0 ) return;
5601  if( p->doXdgOpen ) return;
5602  if( shellDeleteFile(p->zTempFile) ) return;
5603  sqlite3_free(p->zTempFile);
5604  p->zTempFile = 0;
5605}
5606
5607/*
5608** Create a new temp file name with the given suffix.
5609*/
5610static void newTempFile(ShellState *p, const char *zSuffix){
5611  clearTempFile(p);
5612  sqlite3_free(p->zTempFile);
5613  p->zTempFile = 0;
5614  if( p->db ){
5615    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5616  }
5617  if( p->zTempFile==0 ){
5618    /* If p->db is an in-memory database then the TEMPFILENAME file-control
5619    ** will not work and we will need to fallback to guessing */
5620    char *zTemp;
5621    sqlite3_uint64 r;
5622    sqlite3_randomness(sizeof(r), &r);
5623    zTemp = getenv("TEMP");
5624    if( zTemp==0 ) zTemp = getenv("TMP");
5625    if( zTemp==0 ){
5626#ifdef _WIN32
5627      zTemp = "\\tmp";
5628#else
5629      zTemp = "/tmp";
5630#endif
5631    }
5632    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
5633  }else{
5634    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5635  }
5636  if( p->zTempFile==0 ){
5637    raw_printf(stderr, "out of memory\n");
5638    exit(1);
5639  }
5640}
5641
5642
5643/*
5644** The implementation of SQL scalar function fkey_collate_clause(), used
5645** by the ".lint fkey-indexes" command. This scalar function is always
5646** called with four arguments - the parent table name, the parent column name,
5647** the child table name and the child column name.
5648**
5649**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5650**
5651** If either of the named tables or columns do not exist, this function
5652** returns an empty string. An empty string is also returned if both tables
5653** and columns exist but have the same default collation sequence. Or,
5654** if both exist but the default collation sequences are different, this
5655** function returns the string " COLLATE <parent-collation>", where
5656** <parent-collation> is the default collation sequence of the parent column.
5657*/
5658static void shellFkeyCollateClause(
5659  sqlite3_context *pCtx,
5660  int nVal,
5661  sqlite3_value **apVal
5662){
5663  sqlite3 *db = sqlite3_context_db_handle(pCtx);
5664  const char *zParent;
5665  const char *zParentCol;
5666  const char *zParentSeq;
5667  const char *zChild;
5668  const char *zChildCol;
5669  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
5670  int rc;
5671
5672  assert( nVal==4 );
5673  zParent = (const char*)sqlite3_value_text(apVal[0]);
5674  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5675  zChild = (const char*)sqlite3_value_text(apVal[2]);
5676  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5677
5678  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5679  rc = sqlite3_table_column_metadata(
5680      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5681  );
5682  if( rc==SQLITE_OK ){
5683    rc = sqlite3_table_column_metadata(
5684        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5685    );
5686  }
5687
5688  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5689    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5690    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5691    sqlite3_free(z);
5692  }
5693}
5694
5695
5696/*
5697** The implementation of dot-command ".lint fkey-indexes".
5698*/
5699static int lintFkeyIndexes(
5700  ShellState *pState,             /* Current shell tool state */
5701  char **azArg,                   /* Array of arguments passed to dot command */
5702  int nArg                        /* Number of entries in azArg[] */
5703){
5704  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
5705  FILE *out = pState->out;        /* Stream to write non-error output to */
5706  int bVerbose = 0;               /* If -verbose is present */
5707  int bGroupByParent = 0;         /* If -groupbyparent is present */
5708  int i;                          /* To iterate through azArg[] */
5709  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
5710  int rc;                         /* Return code */
5711  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
5712
5713  /*
5714  ** This SELECT statement returns one row for each foreign key constraint
5715  ** in the schema of the main database. The column values are:
5716  **
5717  ** 0. The text of an SQL statement similar to:
5718  **
5719  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
5720  **
5721  **    This SELECT is similar to the one that the foreign keys implementation
5722  **    needs to run internally on child tables. If there is an index that can
5723  **    be used to optimize this query, then it can also be used by the FK
5724  **    implementation to optimize DELETE or UPDATE statements on the parent
5725  **    table.
5726  **
5727  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5728  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5729  **    contains an index that can be used to optimize the query.
5730  **
5731  ** 2. Human readable text that describes the child table and columns. e.g.
5732  **
5733  **       "child_table(child_key1, child_key2)"
5734  **
5735  ** 3. Human readable text that describes the parent table and columns. e.g.
5736  **
5737  **       "parent_table(parent_key1, parent_key2)"
5738  **
5739  ** 4. A full CREATE INDEX statement for an index that could be used to
5740  **    optimize DELETE or UPDATE statements on the parent table. e.g.
5741  **
5742  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
5743  **
5744  ** 5. The name of the parent table.
5745  **
5746  ** These six values are used by the C logic below to generate the report.
5747  */
5748  const char *zSql =
5749  "SELECT "
5750    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
5751    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5752    "  || fkey_collate_clause("
5753    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5754    ", "
5755    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5756    "  || group_concat('*=?', ' AND ') || ')'"
5757    ", "
5758    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
5759    ", "
5760    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5761    ", "
5762    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5763    "  || ' ON ' || quote(s.name) || '('"
5764    "  || group_concat(quote(f.[from]) ||"
5765    "        fkey_collate_clause("
5766    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5767    "  || ');'"
5768    ", "
5769    "     f.[table] "
5770    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
5771    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5772    "GROUP BY s.name, f.id "
5773    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5774  ;
5775  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5776
5777  for(i=2; i<nArg; i++){
5778    int n = strlen30(azArg[i]);
5779    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5780      bVerbose = 1;
5781    }
5782    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5783      bGroupByParent = 1;
5784      zIndent = "    ";
5785    }
5786    else{
5787      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5788          azArg[0], azArg[1]
5789      );
5790      return SQLITE_ERROR;
5791    }
5792  }
5793
5794  /* Register the fkey_collate_clause() SQL function */
5795  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5796      0, shellFkeyCollateClause, 0, 0
5797  );
5798
5799
5800  if( rc==SQLITE_OK ){
5801    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5802  }
5803  if( rc==SQLITE_OK ){
5804    sqlite3_bind_int(pSql, 1, bGroupByParent);
5805  }
5806
5807  if( rc==SQLITE_OK ){
5808    int rc2;
5809    char *zPrev = 0;
5810    while( SQLITE_ROW==sqlite3_step(pSql) ){
5811      int res = -1;
5812      sqlite3_stmt *pExplain = 0;
5813      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5814      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5815      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5816      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5817      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5818      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5819
5820      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5821      if( rc!=SQLITE_OK ) break;
5822      if( SQLITE_ROW==sqlite3_step(pExplain) ){
5823        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5824        res = (
5825              0==sqlite3_strglob(zGlob, zPlan)
5826           || 0==sqlite3_strglob(zGlobIPK, zPlan)
5827        );
5828      }
5829      rc = sqlite3_finalize(pExplain);
5830      if( rc!=SQLITE_OK ) break;
5831
5832      if( res<0 ){
5833        raw_printf(stderr, "Error: internal error");
5834        break;
5835      }else{
5836        if( bGroupByParent
5837        && (bVerbose || res==0)
5838        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5839        ){
5840          raw_printf(out, "-- Parent table %s\n", zParent);
5841          sqlite3_free(zPrev);
5842          zPrev = sqlite3_mprintf("%s", zParent);
5843        }
5844
5845        if( res==0 ){
5846          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5847        }else if( bVerbose ){
5848          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5849              zIndent, zFrom, zTarget
5850          );
5851        }
5852      }
5853    }
5854    sqlite3_free(zPrev);
5855
5856    if( rc!=SQLITE_OK ){
5857      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5858    }
5859
5860    rc2 = sqlite3_finalize(pSql);
5861    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5862      rc = rc2;
5863      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5864    }
5865  }else{
5866    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5867  }
5868
5869  return rc;
5870}
5871
5872/*
5873** Implementation of ".lint" dot command.
5874*/
5875static int lintDotCommand(
5876  ShellState *pState,             /* Current shell tool state */
5877  char **azArg,                   /* Array of arguments passed to dot command */
5878  int nArg                        /* Number of entries in azArg[] */
5879){
5880  int n;
5881  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
5882  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5883  return lintFkeyIndexes(pState, azArg, nArg);
5884
5885 usage:
5886  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5887  raw_printf(stderr, "Where sub-commands are:\n");
5888  raw_printf(stderr, "    fkey-indexes\n");
5889  return SQLITE_ERROR;
5890}
5891
5892#if !defined SQLITE_OMIT_VIRTUALTABLE
5893static void shellPrepare(
5894  sqlite3 *db,
5895  int *pRc,
5896  const char *zSql,
5897  sqlite3_stmt **ppStmt
5898){
5899  *ppStmt = 0;
5900  if( *pRc==SQLITE_OK ){
5901    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
5902    if( rc!=SQLITE_OK ){
5903      raw_printf(stderr, "sql error: %s (%d)\n",
5904          sqlite3_errmsg(db), sqlite3_errcode(db)
5905      );
5906      *pRc = rc;
5907    }
5908  }
5909}
5910
5911/*
5912** Create a prepared statement using printf-style arguments for the SQL.
5913**
5914** This routine is could be marked "static".  But it is not always used,
5915** depending on compile-time options.  By omitting the "static", we avoid
5916** nuisance compiler warnings about "defined but not used".
5917*/
5918void shellPreparePrintf(
5919  sqlite3 *db,
5920  int *pRc,
5921  sqlite3_stmt **ppStmt,
5922  const char *zFmt,
5923  ...
5924){
5925  *ppStmt = 0;
5926  if( *pRc==SQLITE_OK ){
5927    va_list ap;
5928    char *z;
5929    va_start(ap, zFmt);
5930    z = sqlite3_vmprintf(zFmt, ap);
5931    va_end(ap);
5932    if( z==0 ){
5933      *pRc = SQLITE_NOMEM;
5934    }else{
5935      shellPrepare(db, pRc, z, ppStmt);
5936      sqlite3_free(z);
5937    }
5938  }
5939}
5940
5941/* Finalize the prepared statement created using shellPreparePrintf().
5942**
5943** This routine is could be marked "static".  But it is not always used,
5944** depending on compile-time options.  By omitting the "static", we avoid
5945** nuisance compiler warnings about "defined but not used".
5946*/
5947void shellFinalize(
5948  int *pRc,
5949  sqlite3_stmt *pStmt
5950){
5951  if( pStmt ){
5952    sqlite3 *db = sqlite3_db_handle(pStmt);
5953    int rc = sqlite3_finalize(pStmt);
5954    if( *pRc==SQLITE_OK ){
5955      if( rc!=SQLITE_OK ){
5956        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5957      }
5958      *pRc = rc;
5959    }
5960  }
5961}
5962
5963/* Reset the prepared statement created using shellPreparePrintf().
5964**
5965** This routine is could be marked "static".  But it is not always used,
5966** depending on compile-time options.  By omitting the "static", we avoid
5967** nuisance compiler warnings about "defined but not used".
5968*/
5969void shellReset(
5970  int *pRc,
5971  sqlite3_stmt *pStmt
5972){
5973  int rc = sqlite3_reset(pStmt);
5974  if( *pRc==SQLITE_OK ){
5975    if( rc!=SQLITE_OK ){
5976      sqlite3 *db = sqlite3_db_handle(pStmt);
5977      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5978    }
5979    *pRc = rc;
5980  }
5981}
5982#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
5983
5984#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5985/******************************************************************************
5986** The ".archive" or ".ar" command.
5987*/
5988/*
5989** Structure representing a single ".ar" command.
5990*/
5991typedef struct ArCommand ArCommand;
5992struct ArCommand {
5993  u8 eCmd;                        /* An AR_CMD_* value */
5994  u8 bVerbose;                    /* True if --verbose */
5995  u8 bZip;                        /* True if the archive is a ZIP */
5996  u8 bDryRun;                     /* True if --dry-run */
5997  u8 bAppend;                     /* True if --append */
5998  u8 fromCmdLine;                 /* Run from -A instead of .archive */
5999  int nArg;                       /* Number of command arguments */
6000  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
6001  const char *zFile;              /* --file argument, or NULL */
6002  const char *zDir;               /* --directory argument, or NULL */
6003  char **azArg;                   /* Array of command arguments */
6004  ShellState *p;                  /* Shell state */
6005  sqlite3 *db;                    /* Database containing the archive */
6006};
6007
6008/*
6009** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6010*/
6011static int arUsage(FILE *f){
6012  showHelp(f,"archive");
6013  return SQLITE_ERROR;
6014}
6015
6016/*
6017** Print an error message for the .ar command to stderr and return
6018** SQLITE_ERROR.
6019*/
6020static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6021  va_list ap;
6022  char *z;
6023  va_start(ap, zFmt);
6024  z = sqlite3_vmprintf(zFmt, ap);
6025  va_end(ap);
6026  utf8_printf(stderr, "Error: %s\n", z);
6027  if( pAr->fromCmdLine ){
6028    utf8_printf(stderr, "Use \"-A\" for more help\n");
6029  }else{
6030    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6031  }
6032  sqlite3_free(z);
6033  return SQLITE_ERROR;
6034}
6035
6036/*
6037** Values for ArCommand.eCmd.
6038*/
6039#define AR_CMD_CREATE       1
6040#define AR_CMD_UPDATE       2
6041#define AR_CMD_INSERT       3
6042#define AR_CMD_EXTRACT      4
6043#define AR_CMD_LIST         5
6044#define AR_CMD_HELP         6
6045
6046/*
6047** Other (non-command) switches.
6048*/
6049#define AR_SWITCH_VERBOSE     7
6050#define AR_SWITCH_FILE        8
6051#define AR_SWITCH_DIRECTORY   9
6052#define AR_SWITCH_APPEND     10
6053#define AR_SWITCH_DRYRUN     11
6054
6055static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6056  switch( eSwitch ){
6057    case AR_CMD_CREATE:
6058    case AR_CMD_EXTRACT:
6059    case AR_CMD_LIST:
6060    case AR_CMD_UPDATE:
6061    case AR_CMD_INSERT:
6062    case AR_CMD_HELP:
6063      if( pAr->eCmd ){
6064        return arErrorMsg(pAr, "multiple command options");
6065      }
6066      pAr->eCmd = eSwitch;
6067      break;
6068
6069    case AR_SWITCH_DRYRUN:
6070      pAr->bDryRun = 1;
6071      break;
6072    case AR_SWITCH_VERBOSE:
6073      pAr->bVerbose = 1;
6074      break;
6075    case AR_SWITCH_APPEND:
6076      pAr->bAppend = 1;
6077      /* Fall thru into --file */
6078    case AR_SWITCH_FILE:
6079      pAr->zFile = zArg;
6080      break;
6081    case AR_SWITCH_DIRECTORY:
6082      pAr->zDir = zArg;
6083      break;
6084  }
6085
6086  return SQLITE_OK;
6087}
6088
6089/*
6090** Parse the command line for an ".ar" command. The results are written into
6091** structure (*pAr). SQLITE_OK is returned if the command line is parsed
6092** successfully, otherwise an error message is written to stderr and
6093** SQLITE_ERROR returned.
6094*/
6095static int arParseCommand(
6096  char **azArg,                   /* Array of arguments passed to dot command */
6097  int nArg,                       /* Number of entries in azArg[] */
6098  ArCommand *pAr                  /* Populate this object */
6099){
6100  struct ArSwitch {
6101    const char *zLong;
6102    char cShort;
6103    u8 eSwitch;
6104    u8 bArg;
6105  } aSwitch[] = {
6106    { "create",    'c', AR_CMD_CREATE,       0 },
6107    { "extract",   'x', AR_CMD_EXTRACT,      0 },
6108    { "insert",    'i', AR_CMD_INSERT,       0 },
6109    { "list",      't', AR_CMD_LIST,         0 },
6110    { "update",    'u', AR_CMD_UPDATE,       0 },
6111    { "help",      'h', AR_CMD_HELP,         0 },
6112    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
6113    { "file",      'f', AR_SWITCH_FILE,      1 },
6114    { "append",    'a', AR_SWITCH_APPEND,    1 },
6115    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
6116    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
6117  };
6118  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
6119  struct ArSwitch *pEnd = &aSwitch[nSwitch];
6120
6121  if( nArg<=1 ){
6122    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
6123    return arUsage(stderr);
6124  }else{
6125    char *z = azArg[1];
6126    if( z[0]!='-' ){
6127      /* Traditional style [tar] invocation */
6128      int i;
6129      int iArg = 2;
6130      for(i=0; z[i]; i++){
6131        const char *zArg = 0;
6132        struct ArSwitch *pOpt;
6133        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6134          if( z[i]==pOpt->cShort ) break;
6135        }
6136        if( pOpt==pEnd ){
6137          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6138        }
6139        if( pOpt->bArg ){
6140          if( iArg>=nArg ){
6141            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
6142          }
6143          zArg = azArg[iArg++];
6144        }
6145        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6146      }
6147      pAr->nArg = nArg-iArg;
6148      if( pAr->nArg>0 ){
6149        pAr->azArg = &azArg[iArg];
6150      }
6151    }else{
6152      /* Non-traditional invocation */
6153      int iArg;
6154      for(iArg=1; iArg<nArg; iArg++){
6155        int n;
6156        z = azArg[iArg];
6157        if( z[0]!='-' ){
6158          /* All remaining command line words are command arguments. */
6159          pAr->azArg = &azArg[iArg];
6160          pAr->nArg = nArg-iArg;
6161          break;
6162        }
6163        n = strlen30(z);
6164
6165        if( z[1]!='-' ){
6166          int i;
6167          /* One or more short options */
6168          for(i=1; i<n; i++){
6169            const char *zArg = 0;
6170            struct ArSwitch *pOpt;
6171            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6172              if( z[i]==pOpt->cShort ) break;
6173            }
6174            if( pOpt==pEnd ){
6175              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
6176            }
6177            if( pOpt->bArg ){
6178              if( i<(n-1) ){
6179                zArg = &z[i+1];
6180                i = n;
6181              }else{
6182                if( iArg>=(nArg-1) ){
6183                  return arErrorMsg(pAr, "option requires an argument: %c",
6184                                    z[i]);
6185                }
6186                zArg = azArg[++iArg];
6187              }
6188            }
6189            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
6190          }
6191        }else if( z[2]=='\0' ){
6192          /* A -- option, indicating that all remaining command line words
6193          ** are command arguments.  */
6194          pAr->azArg = &azArg[iArg+1];
6195          pAr->nArg = nArg-iArg-1;
6196          break;
6197        }else{
6198          /* A long option */
6199          const char *zArg = 0;             /* Argument for option, if any */
6200          struct ArSwitch *pMatch = 0;      /* Matching option */
6201          struct ArSwitch *pOpt;            /* Iterator */
6202          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
6203            const char *zLong = pOpt->zLong;
6204            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
6205              if( pMatch ){
6206                return arErrorMsg(pAr, "ambiguous option: %s",z);
6207              }else{
6208                pMatch = pOpt;
6209              }
6210            }
6211          }
6212
6213          if( pMatch==0 ){
6214            return arErrorMsg(pAr, "unrecognized option: %s", z);
6215          }
6216          if( pMatch->bArg ){
6217            if( iArg>=(nArg-1) ){
6218              return arErrorMsg(pAr, "option requires an argument: %s", z);
6219            }
6220            zArg = azArg[++iArg];
6221          }
6222          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
6223        }
6224      }
6225    }
6226  }
6227
6228  return SQLITE_OK;
6229}
6230
6231/*
6232** This function assumes that all arguments within the ArCommand.azArg[]
6233** array refer to archive members, as for the --extract or --list commands.
6234** It checks that each of them are present. If any specified file is not
6235** present in the archive, an error is printed to stderr and an error
6236** code returned. Otherwise, if all specified arguments are present in
6237** the archive, SQLITE_OK is returned.
6238**
6239** This function strips any trailing '/' characters from each argument.
6240** This is consistent with the way the [tar] command seems to work on
6241** Linux.
6242*/
6243static int arCheckEntries(ArCommand *pAr){
6244  int rc = SQLITE_OK;
6245  if( pAr->nArg ){
6246    int i, j;
6247    sqlite3_stmt *pTest = 0;
6248
6249    shellPreparePrintf(pAr->db, &rc, &pTest,
6250        "SELECT name FROM %s WHERE name=$name",
6251        pAr->zSrcTable
6252    );
6253    j = sqlite3_bind_parameter_index(pTest, "$name");
6254    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6255      char *z = pAr->azArg[i];
6256      int n = strlen30(z);
6257      int bOk = 0;
6258      while( n>0 && z[n-1]=='/' ) n--;
6259      z[n] = '\0';
6260      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
6261      if( SQLITE_ROW==sqlite3_step(pTest) ){
6262        bOk = 1;
6263      }
6264      shellReset(&rc, pTest);
6265      if( rc==SQLITE_OK && bOk==0 ){
6266        utf8_printf(stderr, "not found in archive: %s\n", z);
6267        rc = SQLITE_ERROR;
6268      }
6269    }
6270    shellFinalize(&rc, pTest);
6271  }
6272  return rc;
6273}
6274
6275/*
6276** Format a WHERE clause that can be used against the "sqlar" table to
6277** identify all archive members that match the command arguments held
6278** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
6279** The caller is responsible for eventually calling sqlite3_free() on
6280** any non-NULL (*pzWhere) value.
6281*/
6282static void arWhereClause(
6283  int *pRc,
6284  ArCommand *pAr,
6285  char **pzWhere                  /* OUT: New WHERE clause */
6286){
6287  char *zWhere = 0;
6288  if( *pRc==SQLITE_OK ){
6289    if( pAr->nArg==0 ){
6290      zWhere = sqlite3_mprintf("1");
6291    }else{
6292      int i;
6293      const char *zSep = "";
6294      for(i=0; i<pAr->nArg; i++){
6295        const char *z = pAr->azArg[i];
6296        zWhere = sqlite3_mprintf(
6297          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
6298          zWhere, zSep, z, strlen30(z)+1, z
6299        );
6300        if( zWhere==0 ){
6301          *pRc = SQLITE_NOMEM;
6302          break;
6303        }
6304        zSep = " OR ";
6305      }
6306    }
6307  }
6308  *pzWhere = zWhere;
6309}
6310
6311/*
6312** Implementation of .ar "lisT" command.
6313*/
6314static int arListCommand(ArCommand *pAr){
6315  const char *zSql = "SELECT %s FROM %s WHERE %s";
6316  const char *azCols[] = {
6317    "name",
6318    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
6319  };
6320
6321  char *zWhere = 0;
6322  sqlite3_stmt *pSql = 0;
6323  int rc;
6324
6325  rc = arCheckEntries(pAr);
6326  arWhereClause(&rc, pAr, &zWhere);
6327
6328  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
6329                     pAr->zSrcTable, zWhere);
6330  if( pAr->bDryRun ){
6331    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6332  }else{
6333    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6334      if( pAr->bVerbose ){
6335        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
6336            sqlite3_column_text(pSql, 0),
6337            sqlite3_column_int(pSql, 1),
6338            sqlite3_column_text(pSql, 2),
6339            sqlite3_column_text(pSql, 3)
6340        );
6341      }else{
6342        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6343      }
6344    }
6345  }
6346  shellFinalize(&rc, pSql);
6347  sqlite3_free(zWhere);
6348  return rc;
6349}
6350
6351
6352/*
6353** Implementation of .ar "eXtract" command.
6354*/
6355static int arExtractCommand(ArCommand *pAr){
6356  const char *zSql1 =
6357    "SELECT "
6358    " ($dir || name),"
6359    " writefile(($dir || name), %s, mode, mtime) "
6360    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6361    " AND name NOT GLOB '*..[/\\]*'";
6362
6363  const char *azExtraArg[] = {
6364    "sqlar_uncompress(data, sz)",
6365    "data"
6366  };
6367
6368  sqlite3_stmt *pSql = 0;
6369  int rc = SQLITE_OK;
6370  char *zDir = 0;
6371  char *zWhere = 0;
6372  int i, j;
6373
6374  /* If arguments are specified, check that they actually exist within
6375  ** the archive before proceeding. And formulate a WHERE clause to
6376  ** match them.  */
6377  rc = arCheckEntries(pAr);
6378  arWhereClause(&rc, pAr, &zWhere);
6379
6380  if( rc==SQLITE_OK ){
6381    if( pAr->zDir ){
6382      zDir = sqlite3_mprintf("%s/", pAr->zDir);
6383    }else{
6384      zDir = sqlite3_mprintf("");
6385    }
6386    if( zDir==0 ) rc = SQLITE_NOMEM;
6387  }
6388
6389  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6390      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
6391  );
6392
6393  if( rc==SQLITE_OK ){
6394    j = sqlite3_bind_parameter_index(pSql, "$dir");
6395    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
6396
6397    /* Run the SELECT statement twice. The first time, writefile() is called
6398    ** for all archive members that should be extracted. The second time,
6399    ** only for the directories. This is because the timestamps for
6400    ** extracted directories must be reset after they are populated (as
6401    ** populating them changes the timestamp).  */
6402    for(i=0; i<2; i++){
6403      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6404      sqlite3_bind_int(pSql, j, i);
6405      if( pAr->bDryRun ){
6406        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6407      }else{
6408        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6409          if( i==0 && pAr->bVerbose ){
6410            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6411          }
6412        }
6413      }
6414      shellReset(&rc, pSql);
6415    }
6416    shellFinalize(&rc, pSql);
6417  }
6418
6419  sqlite3_free(zDir);
6420  sqlite3_free(zWhere);
6421  return rc;
6422}
6423
6424/*
6425** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
6426*/
6427static int arExecSql(ArCommand *pAr, const char *zSql){
6428  int rc;
6429  if( pAr->bDryRun ){
6430    utf8_printf(pAr->p->out, "%s\n", zSql);
6431    rc = SQLITE_OK;
6432  }else{
6433    char *zErr = 0;
6434    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6435    if( zErr ){
6436      utf8_printf(stdout, "ERROR: %s\n", zErr);
6437      sqlite3_free(zErr);
6438    }
6439  }
6440  return rc;
6441}
6442
6443
6444/*
6445** Implementation of .ar "create", "insert", and "update" commands.
6446**
6447**     create    ->     Create a new SQL archive
6448**     insert    ->     Insert or reinsert all files listed
6449**     update    ->     Insert files that have changed or that were not
6450**                      previously in the archive
6451**
6452** Create the "sqlar" table in the database if it does not already exist.
6453** Then add each file in the azFile[] array to the archive. Directories
6454** are added recursively. If argument bVerbose is non-zero, a message is
6455** printed on stdout for each file archived.
6456**
6457** The create command is the same as update, except that it drops
6458** any existing "sqlar" table before beginning.  The "insert" command
6459** always overwrites every file named on the command-line, where as
6460** "update" only overwrites if the size or mtime or mode has changed.
6461*/
6462static int arCreateOrUpdateCommand(
6463  ArCommand *pAr,                 /* Command arguments and options */
6464  int bUpdate,                    /* true for a --create. */
6465  int bOnlyIfChanged              /* Only update if file has changed */
6466){
6467  const char *zCreate =
6468      "CREATE TABLE IF NOT EXISTS sqlar(\n"
6469      "  name TEXT PRIMARY KEY,  -- name of the file\n"
6470      "  mode INT,               -- access permissions\n"
6471      "  mtime INT,              -- last modification time\n"
6472      "  sz INT,                 -- original file size\n"
6473      "  data BLOB               -- compressed content\n"
6474      ")";
6475  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
6476  const char *zInsertFmt[2] = {
6477     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
6478     "  SELECT\n"
6479     "    %s,\n"
6480     "    mode,\n"
6481     "    mtime,\n"
6482     "    CASE substr(lsmode(mode),1,1)\n"
6483     "      WHEN '-' THEN length(data)\n"
6484     "      WHEN 'd' THEN 0\n"
6485     "      ELSE -1 END,\n"
6486     "    sqlar_compress(data)\n"
6487     "  FROM fsdir(%Q,%Q) AS disk\n"
6488     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6489     ,
6490     "REPLACE INTO %s(name,mode,mtime,data)\n"
6491     "  SELECT\n"
6492     "    %s,\n"
6493     "    mode,\n"
6494     "    mtime,\n"
6495     "    data\n"
6496     "  FROM fsdir(%Q,%Q) AS disk\n"
6497     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6498  };
6499  int i;                          /* For iterating through azFile[] */
6500  int rc;                         /* Return code */
6501  const char *zTab = 0;           /* SQL table into which to insert */
6502  char *zSql;
6503  char zTemp[50];
6504  char *zExists = 0;
6505
6506  arExecSql(pAr, "PRAGMA page_size=512");
6507  rc = arExecSql(pAr, "SAVEPOINT ar;");
6508  if( rc!=SQLITE_OK ) return rc;
6509  zTemp[0] = 0;
6510  if( pAr->bZip ){
6511    /* Initialize the zipfile virtual table, if necessary */
6512    if( pAr->zFile ){
6513      sqlite3_uint64 r;
6514      sqlite3_randomness(sizeof(r),&r);
6515      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6516      zTab = zTemp;
6517      zSql = sqlite3_mprintf(
6518         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6519         zTab, pAr->zFile
6520      );
6521      rc = arExecSql(pAr, zSql);
6522      sqlite3_free(zSql);
6523    }else{
6524      zTab = "zip";
6525    }
6526  }else{
6527    /* Initialize the table for an SQLAR */
6528    zTab = "sqlar";
6529    if( bUpdate==0 ){
6530      rc = arExecSql(pAr, zDrop);
6531      if( rc!=SQLITE_OK ) goto end_ar_transaction;
6532    }
6533    rc = arExecSql(pAr, zCreate);
6534  }
6535  if( bOnlyIfChanged ){
6536    zExists = sqlite3_mprintf(
6537      " AND NOT EXISTS("
6538          "SELECT 1 FROM %s AS mem"
6539          " WHERE mem.name=disk.name"
6540          " AND mem.mtime=disk.mtime"
6541          " AND mem.mode=disk.mode)", zTab);
6542  }else{
6543    zExists = sqlite3_mprintf("");
6544  }
6545  if( zExists==0 ) rc = SQLITE_NOMEM;
6546  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
6547    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
6548        pAr->bVerbose ? "shell_putsnl(name)" : "name",
6549        pAr->azArg[i], pAr->zDir, zExists);
6550    rc = arExecSql(pAr, zSql2);
6551    sqlite3_free(zSql2);
6552  }
6553end_ar_transaction:
6554  if( rc!=SQLITE_OK ){
6555    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
6556  }else{
6557    rc = arExecSql(pAr, "RELEASE ar;");
6558    if( pAr->bZip && pAr->zFile ){
6559      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6560      arExecSql(pAr, zSql);
6561      sqlite3_free(zSql);
6562    }
6563  }
6564  sqlite3_free(zExists);
6565  return rc;
6566}
6567
6568/*
6569** Implementation of ".ar" dot command.
6570*/
6571static int arDotCommand(
6572  ShellState *pState,          /* Current shell tool state */
6573  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
6574  char **azArg,                /* Array of arguments passed to dot command */
6575  int nArg                     /* Number of entries in azArg[] */
6576){
6577  ArCommand cmd;
6578  int rc;
6579  memset(&cmd, 0, sizeof(cmd));
6580  cmd.fromCmdLine = fromCmdLine;
6581  rc = arParseCommand(azArg, nArg, &cmd);
6582  if( rc==SQLITE_OK ){
6583    int eDbType = SHELL_OPEN_UNSPEC;
6584    cmd.p = pState;
6585    cmd.db = pState->db;
6586    if( cmd.zFile ){
6587      eDbType = deduceDatabaseType(cmd.zFile, 1);
6588    }else{
6589      eDbType = pState->openMode;
6590    }
6591    if( eDbType==SHELL_OPEN_ZIPFILE ){
6592      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6593        if( cmd.zFile==0 ){
6594          cmd.zSrcTable = sqlite3_mprintf("zip");
6595        }else{
6596          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6597        }
6598      }
6599      cmd.bZip = 1;
6600    }else if( cmd.zFile ){
6601      int flags;
6602      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
6603      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6604           || cmd.eCmd==AR_CMD_UPDATE ){
6605        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6606      }else{
6607        flags = SQLITE_OPEN_READONLY;
6608      }
6609      cmd.db = 0;
6610      if( cmd.bDryRun ){
6611        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6612             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6613      }
6614      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6615             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
6616      if( rc!=SQLITE_OK ){
6617        utf8_printf(stderr, "cannot open file: %s (%s)\n",
6618            cmd.zFile, sqlite3_errmsg(cmd.db)
6619        );
6620        goto end_ar_command;
6621      }
6622      sqlite3_fileio_init(cmd.db, 0, 0);
6623      sqlite3_sqlar_init(cmd.db, 0, 0);
6624      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6625                              shellPutsFunc, 0, 0);
6626
6627    }
6628    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
6629      if( cmd.eCmd!=AR_CMD_CREATE
6630       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6631      ){
6632        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6633        rc = SQLITE_ERROR;
6634        goto end_ar_command;
6635      }
6636      cmd.zSrcTable = sqlite3_mprintf("sqlar");
6637    }
6638
6639    switch( cmd.eCmd ){
6640      case AR_CMD_CREATE:
6641        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
6642        break;
6643
6644      case AR_CMD_EXTRACT:
6645        rc = arExtractCommand(&cmd);
6646        break;
6647
6648      case AR_CMD_LIST:
6649        rc = arListCommand(&cmd);
6650        break;
6651
6652      case AR_CMD_HELP:
6653        arUsage(pState->out);
6654        break;
6655
6656      case AR_CMD_INSERT:
6657        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6658        break;
6659
6660      default:
6661        assert( cmd.eCmd==AR_CMD_UPDATE );
6662        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
6663        break;
6664    }
6665  }
6666end_ar_command:
6667  if( cmd.db!=pState->db ){
6668    close_db(cmd.db);
6669  }
6670  sqlite3_free(cmd.zSrcTable);
6671
6672  return rc;
6673}
6674/* End of the ".archive" or ".ar" command logic
6675*******************************************************************************/
6676#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
6677
6678#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
6679/*
6680** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6681** Otherwise, the SQL statement or statements in zSql are executed using
6682** database connection db and the error code written to *pRc before
6683** this function returns.
6684*/
6685static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6686  int rc = *pRc;
6687  if( rc==SQLITE_OK ){
6688    char *zErr = 0;
6689    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6690    if( rc!=SQLITE_OK ){
6691      raw_printf(stderr, "SQL error: %s\n", zErr);
6692    }
6693    *pRc = rc;
6694  }
6695}
6696
6697/*
6698** Like shellExec(), except that zFmt is a printf() style format string.
6699*/
6700static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6701  char *z = 0;
6702  if( *pRc==SQLITE_OK ){
6703    va_list ap;
6704    va_start(ap, zFmt);
6705    z = sqlite3_vmprintf(zFmt, ap);
6706    va_end(ap);
6707    if( z==0 ){
6708      *pRc = SQLITE_NOMEM;
6709    }else{
6710      shellExec(db, pRc, z);
6711    }
6712    sqlite3_free(z);
6713  }
6714}
6715
6716/*
6717** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6718** Otherwise, an attempt is made to allocate, zero and return a pointer
6719** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6720** to SQLITE_NOMEM and NULL returned.
6721*/
6722static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6723  void *pRet = 0;
6724  if( *pRc==SQLITE_OK ){
6725    pRet = sqlite3_malloc64(nByte);
6726    if( pRet==0 ){
6727      *pRc = SQLITE_NOMEM;
6728    }else{
6729      memset(pRet, 0, nByte);
6730    }
6731  }
6732  return pRet;
6733}
6734
6735/*
6736** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6737** Otherwise, zFmt is treated as a printf() style string. The result of
6738** formatting it along with any trailing arguments is written into a
6739** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6740** It is the responsibility of the caller to eventually free this buffer
6741** using a call to sqlite3_free().
6742**
6743** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6744** pointer returned.
6745*/
6746static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6747  char *z = 0;
6748  if( *pRc==SQLITE_OK ){
6749    va_list ap;
6750    va_start(ap, zFmt);
6751    z = sqlite3_vmprintf(zFmt, ap);
6752    va_end(ap);
6753    if( z==0 ){
6754      *pRc = SQLITE_NOMEM;
6755    }
6756  }
6757  return z;
6758}
6759
6760/*
6761** When running the ".recover" command, each output table, and the special
6762** orphaned row table if it is required, is represented by an instance
6763** of the following struct.
6764*/
6765typedef struct RecoverTable RecoverTable;
6766struct RecoverTable {
6767  char *zQuoted;                  /* Quoted version of table name */
6768  int nCol;                       /* Number of columns in table */
6769  char **azlCol;                  /* Array of column lists */
6770  int iPk;                        /* Index of IPK column */
6771};
6772
6773/*
6774** Free a RecoverTable object allocated by recoverFindTable() or
6775** recoverOrphanTable().
6776*/
6777static void recoverFreeTable(RecoverTable *pTab){
6778  if( pTab ){
6779    sqlite3_free(pTab->zQuoted);
6780    if( pTab->azlCol ){
6781      int i;
6782      for(i=0; i<=pTab->nCol; i++){
6783        sqlite3_free(pTab->azlCol[i]);
6784      }
6785      sqlite3_free(pTab->azlCol);
6786    }
6787    sqlite3_free(pTab);
6788  }
6789}
6790
6791/*
6792** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6793** Otherwise, it allocates and returns a RecoverTable object based on the
6794** final four arguments passed to this function. It is the responsibility
6795** of the caller to eventually free the returned object using
6796** recoverFreeTable().
6797*/
6798static RecoverTable *recoverNewTable(
6799  int *pRc,                       /* IN/OUT: Error code */
6800  const char *zName,              /* Name of table */
6801  const char *zSql,               /* CREATE TABLE statement */
6802  int bIntkey,
6803  int nCol
6804){
6805  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
6806  int rc = *pRc;
6807  RecoverTable *pTab = 0;
6808
6809  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
6810  if( rc==SQLITE_OK ){
6811    int nSqlCol = 0;
6812    int bSqlIntkey = 0;
6813    sqlite3_stmt *pStmt = 0;
6814
6815    rc = sqlite3_open("", &dbtmp);
6816    if( rc==SQLITE_OK ){
6817      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6818                              shellIdQuote, 0, 0);
6819    }
6820    if( rc==SQLITE_OK ){
6821      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6822    }
6823    if( rc==SQLITE_OK ){
6824      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6825      if( rc==SQLITE_ERROR ){
6826        rc = SQLITE_OK;
6827        goto finished;
6828      }
6829    }
6830    shellPreparePrintf(dbtmp, &rc, &pStmt,
6831        "SELECT count(*) FROM pragma_table_info(%Q)", zName
6832    );
6833    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6834      nSqlCol = sqlite3_column_int(pStmt, 0);
6835    }
6836    shellFinalize(&rc, pStmt);
6837
6838    if( rc!=SQLITE_OK || nSqlCol<nCol ){
6839      goto finished;
6840    }
6841
6842    shellPreparePrintf(dbtmp, &rc, &pStmt,
6843      "SELECT ("
6844      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6845      ") FROM sqlite_schema WHERE name = %Q", zName
6846    );
6847    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6848      bSqlIntkey = sqlite3_column_int(pStmt, 0);
6849    }
6850    shellFinalize(&rc, pStmt);
6851
6852    if( bIntkey==bSqlIntkey ){
6853      int i;
6854      const char *zPk = "_rowid_";
6855      sqlite3_stmt *pPkFinder = 0;
6856
6857      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6858      ** set zPk to the name of the PK column, and pTab->iPk to the index
6859      ** of the column, where columns are 0-numbered from left to right.
6860      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6861      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
6862      pTab->iPk = -2;
6863      if( bIntkey ){
6864        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
6865          "SELECT cid, name FROM pragma_table_info(%Q) "
6866          "  WHERE pk=1 AND type='integer' COLLATE nocase"
6867          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6868          , zName, zName
6869        );
6870        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6871          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6872          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6873        }
6874      }
6875
6876      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
6877      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
6878      pTab->nCol = nSqlCol;
6879
6880      if( bIntkey ){
6881        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
6882      }else{
6883        pTab->azlCol[0] = shellMPrintf(&rc, "");
6884      }
6885      i = 1;
6886      shellPreparePrintf(dbtmp, &rc, &pStmt,
6887          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
6888          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
6889          "FROM pragma_table_info(%Q)",
6890          bIntkey ? ", " : "", pTab->iPk,
6891          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6892          zName
6893      );
6894      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6895        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6896        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6897        i++;
6898      }
6899      shellFinalize(&rc, pStmt);
6900
6901      shellFinalize(&rc, pPkFinder);
6902    }
6903  }
6904
6905 finished:
6906  sqlite3_close(dbtmp);
6907  *pRc = rc;
6908  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
6909    recoverFreeTable(pTab);
6910    pTab = 0;
6911  }
6912  return pTab;
6913}
6914
6915/*
6916** This function is called to search the schema recovered from the
6917** sqlite_schema table of the (possibly) corrupt database as part
6918** of a ".recover" command. Specifically, for a table with root page
6919** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6920** table must be a WITHOUT ROWID table, or if non-zero, not one of
6921** those.
6922**
6923** If a table is found, a (RecoverTable*) object is returned. Or, if
6924** no such table is found, but bIntkey is false and iRoot is the
6925** root page of an index in the recovered schema, then (*pbNoop) is
6926** set to true and NULL returned. Or, if there is no such table or
6927** index, NULL is returned and (*pbNoop) set to 0, indicating that
6928** the caller should write data to the orphans table.
6929*/
6930static RecoverTable *recoverFindTable(
6931  ShellState *pState,             /* Shell state object */
6932  int *pRc,                       /* IN/OUT: Error code */
6933  int iRoot,                      /* Root page of table */
6934  int bIntkey,                    /* True for an intkey table */
6935  int nCol,                       /* Number of columns in table */
6936  int *pbNoop                     /* OUT: True if iRoot is root of index */
6937){
6938  sqlite3_stmt *pStmt = 0;
6939  RecoverTable *pRet = 0;
6940  int bNoop = 0;
6941  const char *zSql = 0;
6942  const char *zName = 0;
6943
6944  /* Search the recovered schema for an object with root page iRoot. */
6945  shellPreparePrintf(pState->db, pRc, &pStmt,
6946      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6947  );
6948  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6949    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6950    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6951      bNoop = 1;
6952      break;
6953    }
6954    if( sqlite3_stricmp(zType, "table")==0 ){
6955      zName = (const char*)sqlite3_column_text(pStmt, 1);
6956      zSql = (const char*)sqlite3_column_text(pStmt, 2);
6957      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
6958      break;
6959    }
6960  }
6961
6962  shellFinalize(pRc, pStmt);
6963  *pbNoop = bNoop;
6964  return pRet;
6965}
6966
6967/*
6968** Return a RecoverTable object representing the orphans table.
6969*/
6970static RecoverTable *recoverOrphanTable(
6971  ShellState *pState,             /* Shell state object */
6972  int *pRc,                       /* IN/OUT: Error code */
6973  const char *zLostAndFound,      /* Base name for orphans table */
6974  int nCol                        /* Number of user data columns */
6975){
6976  RecoverTable *pTab = 0;
6977  if( nCol>=0 && *pRc==SQLITE_OK ){
6978    int i;
6979
6980    /* This block determines the name of the orphan table. The prefered
6981    ** name is zLostAndFound. But if that clashes with another name
6982    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
6983    ** and so on until a non-clashing name is found.  */
6984    int iTab = 0;
6985    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
6986    sqlite3_stmt *pTest = 0;
6987    shellPrepare(pState->db, pRc,
6988        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
6989    );
6990    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6991    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
6992      shellReset(pRc, pTest);
6993      sqlite3_free(zTab);
6994      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
6995      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6996    }
6997    shellFinalize(pRc, pTest);
6998
6999    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
7000    if( pTab ){
7001      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
7002      pTab->nCol = nCol;
7003      pTab->iPk = -2;
7004      if( nCol>0 ){
7005        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
7006        if( pTab->azlCol ){
7007          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
7008          for(i=nCol-1; i>=0; i--){
7009            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
7010          }
7011        }
7012      }
7013
7014      if( *pRc!=SQLITE_OK ){
7015        recoverFreeTable(pTab);
7016        pTab = 0;
7017      }else{
7018        raw_printf(pState->out,
7019            "CREATE TABLE %s(rootpgno INTEGER, "
7020            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
7021        );
7022        for(i=0; i<nCol; i++){
7023          raw_printf(pState->out, ", c%d", i);
7024        }
7025        raw_printf(pState->out, ");\n");
7026      }
7027    }
7028    sqlite3_free(zTab);
7029  }
7030  return pTab;
7031}
7032
7033/*
7034** This function is called to recover data from the database. A script
7035** to construct a new database containing all recovered data is output
7036** on stream pState->out.
7037*/
7038static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7039  int rc = SQLITE_OK;
7040  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
7041  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
7042  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
7043  const char *zRecoveryDb = "";   /* Name of "recovery" database */
7044  const char *zLostAndFound = "lost_and_found";
7045  int i;
7046  int nOrphan = -1;
7047  RecoverTable *pOrphan = 0;
7048
7049  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
7050  int bRowids = 1;                /* 0 if --no-rowids */
7051  for(i=1; i<nArg; i++){
7052    char *z = azArg[i];
7053    int n;
7054    if( z[0]=='-' && z[1]=='-' ) z++;
7055    n = strlen30(z);
7056    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
7057      bFreelist = 0;
7058    }else
7059    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7060      i++;
7061      zRecoveryDb = azArg[i];
7062    }else
7063    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7064      i++;
7065      zLostAndFound = azArg[i];
7066    }else
7067    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7068      bRowids = 0;
7069    }
7070    else{
7071      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7072      showHelp(pState->out, azArg[0]);
7073      return 1;
7074    }
7075  }
7076
7077  shellExecPrintf(pState->db, &rc,
7078    /* Attach an in-memory database named 'recovery'. Create an indexed
7079    ** cache of the sqlite_dbptr virtual table. */
7080    "PRAGMA writable_schema = on;"
7081    "ATTACH %Q AS recovery;"
7082    "DROP TABLE IF EXISTS recovery.dbptr;"
7083    "DROP TABLE IF EXISTS recovery.freelist;"
7084    "DROP TABLE IF EXISTS recovery.map;"
7085    "DROP TABLE IF EXISTS recovery.schema;"
7086    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
7087  );
7088
7089  if( bFreelist ){
7090    shellExec(pState->db, &rc,
7091      "WITH trunk(pgno) AS ("
7092      "  SELECT shell_int32("
7093      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
7094      "      WHERE x>0"
7095      "    UNION"
7096      "  SELECT shell_int32("
7097      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
7098      "      FROM trunk WHERE x>0"
7099      "),"
7100      "freelist(data, n, freepgno) AS ("
7101      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
7102      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
7103      "    UNION ALL"
7104      "  SELECT data, n-1, shell_int32(data, 2+n) "
7105      "      FROM freelist WHERE n>=0"
7106      ")"
7107      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
7108    );
7109  }
7110
7111  /* If this is an auto-vacuum database, add all pointer-map pages to
7112  ** the freelist table. Do this regardless of whether or not
7113  ** --freelist-corrupt was specified.  */
7114  shellExec(pState->db, &rc,
7115    "WITH ptrmap(pgno) AS ("
7116    "  SELECT 2 WHERE shell_int32("
7117    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
7118    "  )"
7119    "    UNION ALL "
7120    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
7121    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
7122    ")"
7123    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
7124  );
7125
7126  shellExec(pState->db, &rc,
7127    "CREATE TABLE recovery.dbptr("
7128    "      pgno, child, PRIMARY KEY(child, pgno)"
7129    ") WITHOUT ROWID;"
7130    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
7131    "    SELECT * FROM sqlite_dbptr"
7132    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
7133
7134    /* Delete any pointer to page 1. This ensures that page 1 is considered
7135    ** a root page, regardless of how corrupt the db is. */
7136    "DELETE FROM recovery.dbptr WHERE child = 1;"
7137
7138    /* Delete all pointers to any pages that have more than one pointer
7139    ** to them. Such pages will be treated as root pages when recovering
7140    ** data.  */
7141    "DELETE FROM recovery.dbptr WHERE child IN ("
7142    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
7143    ");"
7144
7145    /* Create the "map" table that will (eventually) contain instructions
7146    ** for dealing with each page in the db that contains one or more
7147    ** records. */
7148    "CREATE TABLE recovery.map("
7149      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
7150    ");"
7151
7152    /* Populate table [map]. If there are circular loops of pages in the
7153    ** database, the following adds all pages in such a loop to the map
7154    ** as individual root pages. This could be handled better.  */
7155    "WITH pages(i, maxlen) AS ("
7156    "  SELECT page_count, ("
7157    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
7158    "  ) FROM pragma_page_count WHERE page_count>0"
7159    "    UNION ALL"
7160    "  SELECT i-1, ("
7161    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
7162    "  ) FROM pages WHERE i>=2"
7163    ")"
7164    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
7165    "  SELECT i, maxlen, NULL, ("
7166    "    WITH p(orig, pgno, parent) AS ("
7167    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
7168    "        UNION "
7169    "      SELECT i, p.parent, "
7170    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
7171    "    )"
7172    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
7173    ") "
7174    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
7175    "UPDATE recovery.map AS o SET intkey = ("
7176    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
7177    ");"
7178
7179    /* Extract data from page 1 and any linked pages into table
7180    ** recovery.schema. With the same schema as an sqlite_schema table.  */
7181    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
7182    "INSERT INTO recovery.schema SELECT "
7183    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
7184    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
7185    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
7186    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
7187    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
7188    "FROM sqlite_dbdata WHERE pgno IN ("
7189    "  SELECT pgno FROM recovery.map WHERE root=1"
7190    ")"
7191    "GROUP BY pgno, cell;"
7192    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
7193  );
7194
7195  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
7196  ** CREATE TABLE statements that extracted from the existing schema.  */
7197  if( rc==SQLITE_OK ){
7198    sqlite3_stmt *pStmt = 0;
7199    /* ".recover" might output content in an order which causes immediate
7200    ** foreign key constraints to be violated. So disable foreign-key
7201    ** constraint enforcement to prevent problems when running the output
7202    ** script. */
7203    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
7204    raw_printf(pState->out, "BEGIN;\n");
7205    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
7206    shellPrepare(pState->db, &rc,
7207        "SELECT sql FROM recovery.schema "
7208        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
7209    );
7210    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7211      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
7212      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
7213          &zCreateTable[12]
7214      );
7215    }
7216    shellFinalize(&rc, pStmt);
7217  }
7218
7219  /* Figure out if an orphan table will be required. And if so, how many
7220  ** user columns it should contain */
7221  shellPrepare(pState->db, &rc,
7222      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
7223      , &pLoop
7224  );
7225  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7226    nOrphan = sqlite3_column_int(pLoop, 0);
7227  }
7228  shellFinalize(&rc, pLoop);
7229  pLoop = 0;
7230
7231  shellPrepare(pState->db, &rc,
7232      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
7233  );
7234
7235  shellPrepare(pState->db, &rc,
7236      "SELECT max(field), group_concat(shell_escape_crnl(quote"
7237      "(case when (? AND field<0) then NULL else value end)"
7238      "), ', ')"
7239      ", min(field) "
7240      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
7241      "GROUP BY cell", &pCells
7242  );
7243
7244  /* Loop through each root page. */
7245  shellPrepare(pState->db, &rc,
7246      "SELECT root, intkey, max(maxlen) FROM recovery.map"
7247      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
7248      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
7249      ")", &pLoop
7250  );
7251  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
7252    int iRoot = sqlite3_column_int(pLoop, 0);
7253    int bIntkey = sqlite3_column_int(pLoop, 1);
7254    int nCol = sqlite3_column_int(pLoop, 2);
7255    int bNoop = 0;
7256    RecoverTable *pTab;
7257
7258    assert( bIntkey==0 || bIntkey==1 );
7259    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
7260    if( bNoop || rc ) continue;
7261    if( pTab==0 ){
7262      if( pOrphan==0 ){
7263        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7264      }
7265      pTab = pOrphan;
7266      if( pTab==0 ) break;
7267    }
7268
7269    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
7270      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
7271    }
7272    sqlite3_bind_int(pPages, 1, iRoot);
7273    if( bRowids==0 && pTab->iPk<0 ){
7274      sqlite3_bind_int(pCells, 1, 1);
7275    }else{
7276      sqlite3_bind_int(pCells, 1, 0);
7277    }
7278    sqlite3_bind_int(pCells, 3, pTab->iPk);
7279
7280    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
7281      int iPgno = sqlite3_column_int(pPages, 0);
7282      sqlite3_bind_int(pCells, 2, iPgno);
7283      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
7284        int nField = sqlite3_column_int(pCells, 0);
7285        int iMin = sqlite3_column_int(pCells, 2);
7286        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
7287
7288        RecoverTable *pTab2 = pTab;
7289        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
7290          if( pOrphan==0 ){
7291            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
7292          }
7293          pTab2 = pOrphan;
7294          if( pTab2==0 ) break;
7295        }
7296
7297        nField = nField+1;
7298        if( pTab2==pOrphan ){
7299          raw_printf(pState->out,
7300              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
7301              pTab2->zQuoted, iRoot, iPgno, nField,
7302              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
7303          );
7304        }else{
7305          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
7306              pTab2->zQuoted, pTab2->azlCol[nField], zVal
7307          );
7308        }
7309      }
7310      shellReset(&rc, pCells);
7311    }
7312    shellReset(&rc, pPages);
7313    if( pTab!=pOrphan ) recoverFreeTable(pTab);
7314  }
7315  shellFinalize(&rc, pLoop);
7316  shellFinalize(&rc, pPages);
7317  shellFinalize(&rc, pCells);
7318  recoverFreeTable(pOrphan);
7319
7320  /* The rest of the schema */
7321  if( rc==SQLITE_OK ){
7322    sqlite3_stmt *pStmt = 0;
7323    shellPrepare(pState->db, &rc,
7324        "SELECT sql, name FROM recovery.schema "
7325        "WHERE sql NOT LIKE 'create table%'", &pStmt
7326    );
7327    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
7328      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
7329      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
7330        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
7331        char *zPrint = shellMPrintf(&rc,
7332          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
7333          zName, zName, zSql
7334        );
7335        raw_printf(pState->out, "%s;\n", zPrint);
7336        sqlite3_free(zPrint);
7337      }else{
7338        raw_printf(pState->out, "%s;\n", zSql);
7339      }
7340    }
7341    shellFinalize(&rc, pStmt);
7342  }
7343
7344  if( rc==SQLITE_OK ){
7345    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
7346    raw_printf(pState->out, "COMMIT;\n");
7347  }
7348  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7349  return rc;
7350}
7351#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7352
7353
7354/*
7355** If an input line begins with "." then invoke this routine to
7356** process that line.
7357**
7358** Return 1 on error, 2 to exit, and 0 otherwise.
7359*/
7360static int do_meta_command(char *zLine, ShellState *p){
7361  int h = 1;
7362  int nArg = 0;
7363  int n, c;
7364  int rc = 0;
7365  char *azArg[52];
7366
7367#ifndef SQLITE_OMIT_VIRTUALTABLE
7368  if( p->expert.pExpert ){
7369    expertFinish(p, 1, 0);
7370  }
7371#endif
7372
7373  /* Parse the input line into tokens.
7374  */
7375  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7376    while( IsSpace(zLine[h]) ){ h++; }
7377    if( zLine[h]==0 ) break;
7378    if( zLine[h]=='\'' || zLine[h]=='"' ){
7379      int delim = zLine[h++];
7380      azArg[nArg++] = &zLine[h];
7381      while( zLine[h] && zLine[h]!=delim ){
7382        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7383        h++;
7384      }
7385      if( zLine[h]==delim ){
7386        zLine[h++] = 0;
7387      }
7388      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7389    }else{
7390      azArg[nArg++] = &zLine[h];
7391      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7392      if( zLine[h] ) zLine[h++] = 0;
7393      resolve_backslashes(azArg[nArg-1]);
7394    }
7395  }
7396  azArg[nArg] = 0;
7397
7398  /* Process the input line.
7399  */
7400  if( nArg==0 ) return 0; /* no tokens, no error */
7401  n = strlen30(azArg[0]);
7402  c = azArg[0][0];
7403  clearTempFile(p);
7404
7405#ifndef SQLITE_OMIT_AUTHORIZATION
7406  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7407    if( nArg!=2 ){
7408      raw_printf(stderr, "Usage: .auth ON|OFF\n");
7409      rc = 1;
7410      goto meta_command_exit;
7411    }
7412    open_db(p, 0);
7413    if( booleanValue(azArg[1]) ){
7414      sqlite3_set_authorizer(p->db, shellAuth, p);
7415    }else{
7416      sqlite3_set_authorizer(p->db, 0, 0);
7417    }
7418  }else
7419#endif
7420
7421#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7422  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
7423    open_db(p, 0);
7424    rc = arDotCommand(p, 0, azArg, nArg);
7425  }else
7426#endif
7427
7428  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7429   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7430  ){
7431    const char *zDestFile = 0;
7432    const char *zDb = 0;
7433    sqlite3 *pDest;
7434    sqlite3_backup *pBackup;
7435    int j;
7436    int bAsync = 0;
7437    const char *zVfs = 0;
7438    for(j=1; j<nArg; j++){
7439      const char *z = azArg[j];
7440      if( z[0]=='-' ){
7441        if( z[1]=='-' ) z++;
7442        if( strcmp(z, "-append")==0 ){
7443          zVfs = "apndvfs";
7444        }else
7445        if( strcmp(z, "-async")==0 ){
7446          bAsync = 1;
7447        }else
7448        {
7449          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7450          return 1;
7451        }
7452      }else if( zDestFile==0 ){
7453        zDestFile = azArg[j];
7454      }else if( zDb==0 ){
7455        zDb = zDestFile;
7456        zDestFile = azArg[j];
7457      }else{
7458        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
7459        return 1;
7460      }
7461    }
7462    if( zDestFile==0 ){
7463      raw_printf(stderr, "missing FILENAME argument on .backup\n");
7464      return 1;
7465    }
7466    if( zDb==0 ) zDb = "main";
7467    rc = sqlite3_open_v2(zDestFile, &pDest,
7468                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
7469    if( rc!=SQLITE_OK ){
7470      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
7471      close_db(pDest);
7472      return 1;
7473    }
7474    if( bAsync ){
7475      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7476                   0, 0, 0);
7477    }
7478    open_db(p, 0);
7479    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7480    if( pBackup==0 ){
7481      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7482      close_db(pDest);
7483      return 1;
7484    }
7485    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7486    sqlite3_backup_finish(pBackup);
7487    if( rc==SQLITE_DONE ){
7488      rc = 0;
7489    }else{
7490      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7491      rc = 1;
7492    }
7493    close_db(pDest);
7494  }else
7495
7496  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7497    if( nArg==2 ){
7498      bail_on_error = booleanValue(azArg[1]);
7499    }else{
7500      raw_printf(stderr, "Usage: .bail on|off\n");
7501      rc = 1;
7502    }
7503  }else
7504
7505  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7506    if( nArg==2 ){
7507      if( booleanValue(azArg[1]) ){
7508        setBinaryMode(p->out, 1);
7509      }else{
7510        setTextMode(p->out, 1);
7511      }
7512    }else{
7513      raw_printf(stderr, "Usage: .binary on|off\n");
7514      rc = 1;
7515    }
7516  }else
7517
7518  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7519    if( nArg==2 ){
7520#if defined(_WIN32) || defined(WIN32)
7521      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7522      rc = !SetCurrentDirectoryW(z);
7523      sqlite3_free(z);
7524#else
7525      rc = chdir(azArg[1]);
7526#endif
7527      if( rc ){
7528        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7529        rc = 1;
7530      }
7531    }else{
7532      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7533      rc = 1;
7534    }
7535  }else
7536
7537  /* The undocumented ".breakpoint" command causes a call to the no-op
7538  ** routine named test_breakpoint().
7539  */
7540  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7541    test_breakpoint();
7542  }else
7543
7544  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7545    if( nArg==2 ){
7546      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7547    }else{
7548      raw_printf(stderr, "Usage: .changes on|off\n");
7549      rc = 1;
7550    }
7551  }else
7552
7553  /* Cancel output redirection, if it is currently set (by .testcase)
7554  ** Then read the content of the testcase-out.txt file and compare against
7555  ** azArg[1].  If there are differences, report an error and exit.
7556  */
7557  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7558    char *zRes = 0;
7559    output_reset(p);
7560    if( nArg!=2 ){
7561      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7562      rc = 2;
7563    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7564      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7565      rc = 2;
7566    }else if( testcase_glob(azArg[1],zRes)==0 ){
7567      utf8_printf(stderr,
7568                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
7569                 p->zTestcase, azArg[1], zRes);
7570      rc = 1;
7571    }else{
7572      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7573      p->nCheck++;
7574    }
7575    sqlite3_free(zRes);
7576  }else
7577
7578  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7579    if( nArg==2 ){
7580      tryToClone(p, azArg[1]);
7581    }else{
7582      raw_printf(stderr, "Usage: .clone FILENAME\n");
7583      rc = 1;
7584    }
7585  }else
7586
7587  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7588    char **azName = 0;
7589    int nName = 0;
7590    sqlite3_stmt *pStmt;
7591    int i;
7592    open_db(p, 0);
7593    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7594    if( rc ){
7595      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7596      rc = 1;
7597    }else{
7598      while( sqlite3_step(pStmt)==SQLITE_ROW ){
7599        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
7600        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
7601        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
7602        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
7603        azName[nName*2] = strdup(zSchema);
7604        azName[nName*2+1] = strdup(zFile);
7605        nName++;
7606      }
7607    }
7608    sqlite3_finalize(pStmt);
7609    for(i=0; i<nName; i++){
7610      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
7611      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
7612      const char *z = azName[i*2+1];
7613      utf8_printf(p->out, "%s: %s %s%s\n",
7614         azName[i*2],
7615         z && z[0] ? z : "\"\"",
7616         bRdonly ? "r/o" : "r/w",
7617         eTxn==SQLITE_TXN_NONE ? "" :
7618            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
7619      free(azName[i*2]);
7620      free(azName[i*2+1]);
7621    }
7622    sqlite3_free(azName);
7623  }else
7624
7625  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
7626    static const struct DbConfigChoices {
7627      const char *zName;
7628      int op;
7629    } aDbConfig[] = {
7630        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
7631        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
7632        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
7633        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
7634        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
7635        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
7636        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
7637        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
7638        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
7639        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
7640        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7641        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
7642        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
7643        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
7644        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
7645        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
7646    };
7647    int ii, v;
7648    open_db(p, 0);
7649    for(ii=0; ii<ArraySize(aDbConfig); ii++){
7650      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7651      if( nArg>=3 ){
7652        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7653      }
7654      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
7655      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
7656      if( nArg>1 ) break;
7657    }
7658    if( nArg>1 && ii==ArraySize(aDbConfig) ){
7659      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7660      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7661    }
7662  }else
7663
7664  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
7665    rc = shell_dbinfo_command(p, nArg, azArg);
7666  }else
7667
7668#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
7669  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7670    open_db(p, 0);
7671    rc = recoverDatabaseCmd(p, nArg, azArg);
7672  }else
7673#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
7674
7675  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
7676    char *zLike = 0;
7677    char *zSql;
7678    int i;
7679    int savedShowHeader = p->showHeader;
7680    int savedShellFlags = p->shellFlgs;
7681    ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
7682    for(i=1; i<nArg; i++){
7683      if( azArg[i][0]=='-' ){
7684        const char *z = azArg[i]+1;
7685        if( z[0]=='-' ) z++;
7686        if( strcmp(z,"preserve-rowids")==0 ){
7687#ifdef SQLITE_OMIT_VIRTUALTABLE
7688          raw_printf(stderr, "The --preserve-rowids option is not compatible"
7689                             " with SQLITE_OMIT_VIRTUALTABLE\n");
7690          rc = 1;
7691          sqlite3_free(zLike);
7692          goto meta_command_exit;
7693#else
7694          ShellSetFlag(p, SHFLG_PreserveRowid);
7695#endif
7696        }else
7697        if( strcmp(z,"newlines")==0 ){
7698          ShellSetFlag(p, SHFLG_Newlines);
7699        }else
7700        {
7701          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7702          rc = 1;
7703          sqlite3_free(zLike);
7704          goto meta_command_exit;
7705        }
7706      }else if( zLike ){
7707        zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
7708                zLike, azArg[i]);
7709      }else{
7710        zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
7711      }
7712    }
7713
7714    open_db(p, 0);
7715
7716    /* When playing back a "dump", the content might appear in an order
7717    ** which causes immediate foreign key constraints to be violated.
7718    ** So disable foreign-key constraint enforcement to prevent problems. */
7719    raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7720    raw_printf(p->out, "BEGIN TRANSACTION;\n");
7721    p->writableSchema = 0;
7722    p->showHeader = 0;
7723    /* Set writable_schema=ON since doing so forces SQLite to initialize
7724    ** as much of the schema as it can even if the sqlite_schema table is
7725    ** corrupt. */
7726    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7727    p->nErr = 0;
7728    if( zLike==0 ) zLike = sqlite3_mprintf("true");
7729    zSql = sqlite3_mprintf(
7730      "SELECT name, type, sql FROM sqlite_schema "
7731      "WHERE (%s) AND type=='table'"
7732      "  AND sql NOT NULL"
7733      " ORDER BY tbl_name='sqlite_sequence', rowid",
7734      zLike
7735    );
7736    run_schema_dump_query(p,zSql);
7737    sqlite3_free(zSql);
7738    zSql = sqlite3_mprintf(
7739      "SELECT sql FROM sqlite_schema "
7740      "WHERE (%s) AND sql NOT NULL"
7741      "  AND type IN ('index','trigger','view')",
7742      zLike
7743    );
7744    run_table_dump_query(p, zSql);
7745    sqlite3_free(zSql);
7746    sqlite3_free(zLike);
7747    if( p->writableSchema ){
7748      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7749      p->writableSchema = 0;
7750    }
7751    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7752    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
7753    raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
7754    p->showHeader = savedShowHeader;
7755    p->shellFlgs = savedShellFlags;
7756  }else
7757
7758  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7759    if( nArg==2 ){
7760      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7761    }else{
7762      raw_printf(stderr, "Usage: .echo on|off\n");
7763      rc = 1;
7764    }
7765  }else
7766
7767  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7768    if( nArg==2 ){
7769      p->autoEQPtest = 0;
7770      if( p->autoEQPtrace ){
7771        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7772        p->autoEQPtrace = 0;
7773      }
7774      if( strcmp(azArg[1],"full")==0 ){
7775        p->autoEQP = AUTOEQP_full;
7776      }else if( strcmp(azArg[1],"trigger")==0 ){
7777        p->autoEQP = AUTOEQP_trigger;
7778#ifdef SQLITE_DEBUG
7779      }else if( strcmp(azArg[1],"test")==0 ){
7780        p->autoEQP = AUTOEQP_on;
7781        p->autoEQPtest = 1;
7782      }else if( strcmp(azArg[1],"trace")==0 ){
7783        p->autoEQP = AUTOEQP_full;
7784        p->autoEQPtrace = 1;
7785        open_db(p, 0);
7786        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
7787        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7788#endif
7789      }else{
7790        p->autoEQP = (u8)booleanValue(azArg[1]);
7791      }
7792    }else{
7793      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
7794      rc = 1;
7795    }
7796  }else
7797
7798  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7799    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7800    rc = 2;
7801  }else
7802
7803  /* The ".explain" command is automatic now.  It is largely pointless.  It
7804  ** retained purely for backwards compatibility */
7805  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7806    int val = 1;
7807    if( nArg>=2 ){
7808      if( strcmp(azArg[1],"auto")==0 ){
7809        val = 99;
7810      }else{
7811        val =  booleanValue(azArg[1]);
7812      }
7813    }
7814    if( val==1 && p->mode!=MODE_Explain ){
7815      p->normalMode = p->mode;
7816      p->mode = MODE_Explain;
7817      p->autoExplain = 0;
7818    }else if( val==0 ){
7819      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7820      p->autoExplain = 0;
7821    }else if( val==99 ){
7822      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7823      p->autoExplain = 1;
7824    }
7825  }else
7826
7827#ifndef SQLITE_OMIT_VIRTUALTABLE
7828  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7829    open_db(p, 0);
7830    expertDotCommand(p, azArg, nArg);
7831  }else
7832#endif
7833
7834  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7835    static const struct {
7836       const char *zCtrlName;   /* Name of a test-control option */
7837       int ctrlCode;            /* Integer code for that option */
7838       const char *zUsage;      /* Usage notes */
7839    } aCtrl[] = {
7840      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
7841      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
7842   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
7843      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
7844      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
7845   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
7846      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
7847      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
7848      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
7849      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
7850    };
7851    int filectrl = -1;
7852    int iCtrl = -1;
7853    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
7854    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
7855    int n2, i;
7856    const char *zCmd = 0;
7857    const char *zSchema = 0;
7858
7859    open_db(p, 0);
7860    zCmd = nArg>=2 ? azArg[1] : "help";
7861
7862    if( zCmd[0]=='-'
7863     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7864     && nArg>=4
7865    ){
7866      zSchema = azArg[2];
7867      for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7868      nArg -= 2;
7869      zCmd = azArg[1];
7870    }
7871
7872    /* The argument can optionally begin with "-" or "--" */
7873    if( zCmd[0]=='-' && zCmd[1] ){
7874      zCmd++;
7875      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7876    }
7877
7878    /* --help lists all file-controls */
7879    if( strcmp(zCmd,"help")==0 ){
7880      utf8_printf(p->out, "Available file-controls:\n");
7881      for(i=0; i<ArraySize(aCtrl); i++){
7882        utf8_printf(p->out, "  .filectrl %s %s\n",
7883                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7884      }
7885      rc = 1;
7886      goto meta_command_exit;
7887    }
7888
7889    /* convert filectrl text option to value. allow any unique prefix
7890    ** of the option name, or a numerical value. */
7891    n2 = strlen30(zCmd);
7892    for(i=0; i<ArraySize(aCtrl); i++){
7893      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7894        if( filectrl<0 ){
7895          filectrl = aCtrl[i].ctrlCode;
7896          iCtrl = i;
7897        }else{
7898          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7899                              "Use \".filectrl --help\" for help\n", zCmd);
7900          rc = 1;
7901          goto meta_command_exit;
7902        }
7903      }
7904    }
7905    if( filectrl<0 ){
7906      utf8_printf(stderr,"Error: unknown file-control: %s\n"
7907                         "Use \".filectrl --help\" for help\n", zCmd);
7908    }else{
7909      switch(filectrl){
7910        case SQLITE_FCNTL_SIZE_LIMIT: {
7911          if( nArg!=2 && nArg!=3 ) break;
7912          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
7913          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
7914          isOk = 1;
7915          break;
7916        }
7917        case SQLITE_FCNTL_LOCK_TIMEOUT:
7918        case SQLITE_FCNTL_CHUNK_SIZE: {
7919          int x;
7920          if( nArg!=3 ) break;
7921          x = (int)integerValue(azArg[2]);
7922          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7923          isOk = 2;
7924          break;
7925        }
7926        case SQLITE_FCNTL_PERSIST_WAL:
7927        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7928          int x;
7929          if( nArg!=2 && nArg!=3 ) break;
7930          x = nArg==3 ? booleanValue(azArg[2]) : -1;
7931          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7932          iRes = x;
7933          isOk = 1;
7934          break;
7935        }
7936        case SQLITE_FCNTL_HAS_MOVED: {
7937          int x;
7938          if( nArg!=2 ) break;
7939          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7940          iRes = x;
7941          isOk = 1;
7942          break;
7943        }
7944        case SQLITE_FCNTL_TEMPFILENAME: {
7945          char *z = 0;
7946          if( nArg!=2 ) break;
7947          sqlite3_file_control(p->db, zSchema, filectrl, &z);
7948          if( z ){
7949            utf8_printf(p->out, "%s\n", z);
7950            sqlite3_free(z);
7951          }
7952          isOk = 2;
7953          break;
7954        }
7955        case SQLITE_FCNTL_RESERVE_BYTES: {
7956          int x;
7957          if( nArg>=3 ){
7958            x = atoi(azArg[2]);
7959            sqlite3_file_control(p->db, zSchema, filectrl, &x);
7960          }
7961          x = -1;
7962          sqlite3_file_control(p->db, zSchema, filectrl, &x);
7963          utf8_printf(p->out,"%d\n", x);
7964          isOk = 2;
7965          break;
7966        }
7967      }
7968    }
7969    if( isOk==0 && iCtrl>=0 ){
7970      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
7971      rc = 1;
7972    }else if( isOk==1 ){
7973      char zBuf[100];
7974      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
7975      raw_printf(p->out, "%s\n", zBuf);
7976    }
7977  }else
7978
7979  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
7980    ShellState data;
7981    char *zErrMsg = 0;
7982    int doStats = 0;
7983    memcpy(&data, p, sizeof(data));
7984    data.showHeader = 0;
7985    data.cMode = data.mode = MODE_Semi;
7986    if( nArg==2 && optionMatch(azArg[1], "indent") ){
7987      data.cMode = data.mode = MODE_Pretty;
7988      nArg = 1;
7989    }
7990    if( nArg!=1 ){
7991      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
7992      rc = 1;
7993      goto meta_command_exit;
7994    }
7995    open_db(p, 0);
7996    rc = sqlite3_exec(p->db,
7997       "SELECT sql FROM"
7998       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
7999       "     FROM sqlite_schema UNION ALL"
8000       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8001       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8002       "ORDER BY rowid",
8003       callback, &data, &zErrMsg
8004    );
8005    if( rc==SQLITE_OK ){
8006      sqlite3_stmt *pStmt;
8007      rc = sqlite3_prepare_v2(p->db,
8008               "SELECT rowid FROM sqlite_schema"
8009               " WHERE name GLOB 'sqlite_stat[134]'",
8010               -1, &pStmt, 0);
8011      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8012      sqlite3_finalize(pStmt);
8013    }
8014    if( doStats==0 ){
8015      raw_printf(p->out, "/* No STAT tables available */\n");
8016    }else{
8017      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8018      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
8019                   callback, &data, &zErrMsg);
8020      data.cMode = data.mode = MODE_Insert;
8021      data.zDestTable = "sqlite_stat1";
8022      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
8023      data.zDestTable = "sqlite_stat4";
8024      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
8025      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8026    }
8027  }else
8028
8029  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
8030    if( nArg==2 ){
8031      p->showHeader = booleanValue(azArg[1]);
8032      p->shellFlgs |= SHFLG_HeaderSet;
8033    }else{
8034      raw_printf(stderr, "Usage: .headers on|off\n");
8035      rc = 1;
8036    }
8037  }else
8038
8039  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
8040    if( nArg>=2 ){
8041      n = showHelp(p->out, azArg[1]);
8042      if( n==0 ){
8043        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8044      }
8045    }else{
8046      showHelp(p->out, 0);
8047    }
8048  }else
8049
8050  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
8051    char *zTable = 0;           /* Insert data into this table */
8052    char *zFile = 0;            /* Name of file to extra content from */
8053    sqlite3_stmt *pStmt = NULL; /* A statement */
8054    int nCol;                   /* Number of columns in the table */
8055    int nByte;                  /* Number of bytes in an SQL string */
8056    int i, j;                   /* Loop counters */
8057    int needCommit;             /* True to COMMIT or ROLLBACK at end */
8058    int nSep;                   /* Number of bytes in p->colSeparator[] */
8059    char *zSql;                 /* An SQL statement */
8060    ImportCtx sCtx;             /* Reader context */
8061    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8062    int eVerbose = 0;           /* Larger for more console output */
8063    int nSkip = 0;              /* Initial lines to skip */
8064    int useOutputMode = 1;      /* Use output mode to determine separators */
8065
8066    memset(&sCtx, 0, sizeof(sCtx));
8067    if( p->mode==MODE_Ascii ){
8068      xRead = ascii_read_one_field;
8069    }else{
8070      xRead = csv_read_one_field;
8071    }
8072    for(i=1; i<nArg; i++){
8073      char *z = azArg[i];
8074      if( z[0]=='-' && z[1]=='-' ) z++;
8075      if( z[0]!='-' ){
8076        if( zFile==0 ){
8077          zFile = z;
8078        }else if( zTable==0 ){
8079          zTable = z;
8080        }else{
8081          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
8082          showHelp(p->out, "import");
8083          rc = 1;
8084          goto meta_command_exit;
8085        }
8086      }else if( strcmp(z,"-v")==0 ){
8087        eVerbose++;
8088      }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
8089        nSkip = integerValue(azArg[++i]);
8090      }else if( strcmp(z,"-ascii")==0 ){
8091        sCtx.cColSep = SEP_Unit[0];
8092        sCtx.cRowSep = SEP_Record[0];
8093        xRead = ascii_read_one_field;
8094        useOutputMode = 0;
8095      }else if( strcmp(z,"-csv")==0 ){
8096        sCtx.cColSep = ',';
8097        sCtx.cRowSep = '\n';
8098        xRead = csv_read_one_field;
8099        useOutputMode = 0;
8100      }else{
8101        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
8102        showHelp(p->out, "import");
8103        rc = 1;
8104        goto meta_command_exit;
8105      }
8106    }
8107    if( zTable==0 ){
8108      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8109                  zFile==0 ? "FILE" : "TABLE");
8110      showHelp(p->out, "import");
8111      rc = 1;
8112      goto meta_command_exit;
8113    }
8114    seenInterrupt = 0;
8115    open_db(p, 0);
8116    if( useOutputMode ){
8117      /* If neither the --csv or --ascii options are specified, then set
8118      ** the column and row separator characters from the output mode. */
8119      nSep = strlen30(p->colSeparator);
8120      if( nSep==0 ){
8121        raw_printf(stderr,
8122                   "Error: non-null column separator required for import\n");
8123        rc = 1;
8124        goto meta_command_exit;
8125      }
8126      if( nSep>1 ){
8127        raw_printf(stderr,
8128              "Error: multi-character column separators not allowed"
8129              " for import\n");
8130        rc = 1;
8131        goto meta_command_exit;
8132      }
8133      nSep = strlen30(p->rowSeparator);
8134      if( nSep==0 ){
8135        raw_printf(stderr,
8136            "Error: non-null row separator required for import\n");
8137        rc = 1;
8138        goto meta_command_exit;
8139      }
8140      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
8141        /* When importing CSV (only), if the row separator is set to the
8142        ** default output row separator, change it to the default input
8143        ** row separator.  This avoids having to maintain different input
8144        ** and output row separators. */
8145        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8146        nSep = strlen30(p->rowSeparator);
8147      }
8148      if( nSep>1 ){
8149        raw_printf(stderr, "Error: multi-character row separators not allowed"
8150                           " for import\n");
8151        rc = 1;
8152        goto meta_command_exit;
8153      }
8154      sCtx.cColSep = p->colSeparator[0];
8155      sCtx.cRowSep = p->rowSeparator[0];
8156    }
8157    sCtx.zFile = zFile;
8158    sCtx.nLine = 1;
8159    if( sCtx.zFile[0]=='|' ){
8160#ifdef SQLITE_OMIT_POPEN
8161      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8162      rc = 1;
8163      goto meta_command_exit;
8164#else
8165      sCtx.in = popen(sCtx.zFile+1, "r");
8166      sCtx.zFile = "<pipe>";
8167      sCtx.xCloser = pclose;
8168#endif
8169    }else{
8170      sCtx.in = fopen(sCtx.zFile, "rb");
8171      sCtx.xCloser = fclose;
8172    }
8173    if( sCtx.in==0 ){
8174      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8175      rc = 1;
8176      goto meta_command_exit;
8177    }
8178    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8179      char zSep[2];
8180      zSep[1] = 0;
8181      zSep[0] = sCtx.cColSep;
8182      utf8_printf(p->out, "Column separator ");
8183      output_c_string(p->out, zSep);
8184      utf8_printf(p->out, ", row separator ");
8185      zSep[0] = sCtx.cRowSep;
8186      output_c_string(p->out, zSep);
8187      utf8_printf(p->out, "\n");
8188    }
8189    while( (nSkip--)>0 ){
8190      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8191    }
8192    zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
8193    if( zSql==0 ){
8194      import_cleanup(&sCtx);
8195      shell_out_of_memory();
8196    }
8197    nByte = strlen30(zSql);
8198    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8199    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
8200    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8201      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
8202      char cSep = '(';
8203      while( xRead(&sCtx) ){
8204        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
8205        cSep = ',';
8206        if( sCtx.cTerm!=sCtx.cColSep ) break;
8207      }
8208      if( cSep=='(' ){
8209        sqlite3_free(zCreate);
8210        import_cleanup(&sCtx);
8211        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8212        rc = 1;
8213        goto meta_command_exit;
8214      }
8215      zCreate = sqlite3_mprintf("%z\n)", zCreate);
8216      if( eVerbose>=1 ){
8217        utf8_printf(p->out, "%s\n", zCreate);
8218      }
8219      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8220      sqlite3_free(zCreate);
8221      if( rc ){
8222        utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
8223                sqlite3_errmsg(p->db));
8224        import_cleanup(&sCtx);
8225        rc = 1;
8226        goto meta_command_exit;
8227      }
8228      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8229    }
8230    sqlite3_free(zSql);
8231    if( rc ){
8232      if (pStmt) sqlite3_finalize(pStmt);
8233      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8234      import_cleanup(&sCtx);
8235      rc = 1;
8236      goto meta_command_exit;
8237    }
8238    nCol = sqlite3_column_count(pStmt);
8239    sqlite3_finalize(pStmt);
8240    pStmt = 0;
8241    if( nCol==0 ) return 0; /* no columns, no error */
8242    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8243    if( zSql==0 ){
8244      import_cleanup(&sCtx);
8245      shell_out_of_memory();
8246    }
8247    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
8248    j = strlen30(zSql);
8249    for(i=1; i<nCol; i++){
8250      zSql[j++] = ',';
8251      zSql[j++] = '?';
8252    }
8253    zSql[j++] = ')';
8254    zSql[j] = 0;
8255    if( eVerbose>=2 ){
8256      utf8_printf(p->out, "Insert using: %s\n", zSql);
8257    }
8258    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8259    sqlite3_free(zSql);
8260    if( rc ){
8261      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8262      if (pStmt) sqlite3_finalize(pStmt);
8263      import_cleanup(&sCtx);
8264      rc = 1;
8265      goto meta_command_exit;
8266    }
8267    needCommit = sqlite3_get_autocommit(p->db);
8268    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8269    do{
8270      int startLine = sCtx.nLine;
8271      for(i=0; i<nCol; i++){
8272        char *z = xRead(&sCtx);
8273        /*
8274        ** Did we reach end-of-file before finding any columns?
8275        ** If so, stop instead of NULL filling the remaining columns.
8276        */
8277        if( z==0 && i==0 ) break;
8278        /*
8279        ** Did we reach end-of-file OR end-of-line before finding any
8280        ** columns in ASCII mode?  If so, stop instead of NULL filling
8281        ** the remaining columns.
8282        */
8283        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8284        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8285        if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8286          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8287                          "filling the rest with NULL\n",
8288                          sCtx.zFile, startLine, nCol, i+1);
8289          i += 2;
8290          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
8291        }
8292      }
8293      if( sCtx.cTerm==sCtx.cColSep ){
8294        do{
8295          xRead(&sCtx);
8296          i++;
8297        }while( sCtx.cTerm==sCtx.cColSep );
8298        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8299                        "extras ignored\n",
8300                        sCtx.zFile, startLine, nCol, i);
8301      }
8302      if( i>=nCol ){
8303        sqlite3_step(pStmt);
8304        rc = sqlite3_reset(pStmt);
8305        if( rc!=SQLITE_OK ){
8306          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
8307                      startLine, sqlite3_errmsg(p->db));
8308          sCtx.nErr++;
8309        }else{
8310          sCtx.nRow++;
8311        }
8312      }
8313    }while( sCtx.cTerm!=EOF );
8314
8315    import_cleanup(&sCtx);
8316    sqlite3_finalize(pStmt);
8317    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
8318    if( eVerbose>0 ){
8319      utf8_printf(p->out,
8320          "Added %d rows with %d errors using %d lines of input\n",
8321          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
8322    }
8323  }else
8324
8325#ifndef SQLITE_UNTESTABLE
8326  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
8327    char *zSql;
8328    char *zCollist = 0;
8329    sqlite3_stmt *pStmt;
8330    int tnum = 0;
8331    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
8332    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
8333    int i;
8334    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
8335      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
8336                          "       .imposter off\n");
8337      /* Also allowed, but not documented:
8338      **
8339      **    .imposter TABLE IMPOSTER
8340      **
8341      ** where TABLE is a WITHOUT ROWID table.  In that case, the
8342      ** imposter is another WITHOUT ROWID table with the columns in
8343      ** storage order. */
8344      rc = 1;
8345      goto meta_command_exit;
8346    }
8347    open_db(p, 0);
8348    if( nArg==2 ){
8349      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
8350      goto meta_command_exit;
8351    }
8352    zSql = sqlite3_mprintf(
8353      "SELECT rootpage, 0 FROM sqlite_schema"
8354      " WHERE name='%q' AND type='index'"
8355      "UNION ALL "
8356      "SELECT rootpage, 1 FROM sqlite_schema"
8357      " WHERE name='%q' AND type='table'"
8358      "   AND sql LIKE '%%without%%rowid%%'",
8359      azArg[1], azArg[1]
8360    );
8361    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8362    sqlite3_free(zSql);
8363    if( sqlite3_step(pStmt)==SQLITE_ROW ){
8364      tnum = sqlite3_column_int(pStmt, 0);
8365      isWO = sqlite3_column_int(pStmt, 1);
8366    }
8367    sqlite3_finalize(pStmt);
8368    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8369    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8370    sqlite3_free(zSql);
8371    i = 0;
8372    while( sqlite3_step(pStmt)==SQLITE_ROW ){
8373      char zLabel[20];
8374      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8375      i++;
8376      if( zCol==0 ){
8377        if( sqlite3_column_int(pStmt,1)==-1 ){
8378          zCol = "_ROWID_";
8379        }else{
8380          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8381          zCol = zLabel;
8382        }
8383      }
8384      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8385        lenPK = (int)strlen(zCollist);
8386      }
8387      if( zCollist==0 ){
8388        zCollist = sqlite3_mprintf("\"%w\"", zCol);
8389      }else{
8390        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8391      }
8392    }
8393    sqlite3_finalize(pStmt);
8394    if( i==0 || tnum==0 ){
8395      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8396      rc = 1;
8397      sqlite3_free(zCollist);
8398      goto meta_command_exit;
8399    }
8400    if( lenPK==0 ) lenPK = 100000;
8401    zSql = sqlite3_mprintf(
8402          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8403          azArg[2], zCollist, lenPK, zCollist);
8404    sqlite3_free(zCollist);
8405    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8406    if( rc==SQLITE_OK ){
8407      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8408      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8409      if( rc ){
8410        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8411      }else{
8412        utf8_printf(stdout, "%s;\n", zSql);
8413        raw_printf(stdout,
8414          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8415          azArg[1], isWO ? "table" : "index"
8416        );
8417      }
8418    }else{
8419      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8420      rc = 1;
8421    }
8422    sqlite3_free(zSql);
8423  }else
8424#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8425
8426#ifdef SQLITE_ENABLE_IOTRACE
8427  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8428    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8429    if( iotrace && iotrace!=stdout ) fclose(iotrace);
8430    iotrace = 0;
8431    if( nArg<2 ){
8432      sqlite3IoTrace = 0;
8433    }else if( strcmp(azArg[1], "-")==0 ){
8434      sqlite3IoTrace = iotracePrintf;
8435      iotrace = stdout;
8436    }else{
8437      iotrace = fopen(azArg[1], "w");
8438      if( iotrace==0 ){
8439        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8440        sqlite3IoTrace = 0;
8441        rc = 1;
8442      }else{
8443        sqlite3IoTrace = iotracePrintf;
8444      }
8445    }
8446  }else
8447#endif
8448
8449  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8450    static const struct {
8451       const char *zLimitName;   /* Name of a limit */
8452       int limitCode;            /* Integer code for that limit */
8453    } aLimit[] = {
8454      { "length",                SQLITE_LIMIT_LENGTH                    },
8455      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
8456      { "column",                SQLITE_LIMIT_COLUMN                    },
8457      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
8458      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
8459      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
8460      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
8461      { "attached",              SQLITE_LIMIT_ATTACHED                  },
8462      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
8463      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
8464      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
8465      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
8466    };
8467    int i, n2;
8468    open_db(p, 0);
8469    if( nArg==1 ){
8470      for(i=0; i<ArraySize(aLimit); i++){
8471        printf("%20s %d\n", aLimit[i].zLimitName,
8472               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8473      }
8474    }else if( nArg>3 ){
8475      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8476      rc = 1;
8477      goto meta_command_exit;
8478    }else{
8479      int iLimit = -1;
8480      n2 = strlen30(azArg[1]);
8481      for(i=0; i<ArraySize(aLimit); i++){
8482        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8483          if( iLimit<0 ){
8484            iLimit = i;
8485          }else{
8486            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8487            rc = 1;
8488            goto meta_command_exit;
8489          }
8490        }
8491      }
8492      if( iLimit<0 ){
8493        utf8_printf(stderr, "unknown limit: \"%s\"\n"
8494                        "enter \".limits\" with no arguments for a list.\n",
8495                         azArg[1]);
8496        rc = 1;
8497        goto meta_command_exit;
8498      }
8499      if( nArg==3 ){
8500        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8501                      (int)integerValue(azArg[2]));
8502      }
8503      printf("%20s %d\n", aLimit[iLimit].zLimitName,
8504             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8505    }
8506  }else
8507
8508  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8509    open_db(p, 0);
8510    lintDotCommand(p, azArg, nArg);
8511  }else
8512
8513#ifndef SQLITE_OMIT_LOAD_EXTENSION
8514  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8515    const char *zFile, *zProc;
8516    char *zErrMsg = 0;
8517    if( nArg<2 ){
8518      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8519      rc = 1;
8520      goto meta_command_exit;
8521    }
8522    zFile = azArg[1];
8523    zProc = nArg>=3 ? azArg[2] : 0;
8524    open_db(p, 0);
8525    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8526    if( rc!=SQLITE_OK ){
8527      utf8_printf(stderr, "Error: %s\n", zErrMsg);
8528      sqlite3_free(zErrMsg);
8529      rc = 1;
8530    }
8531  }else
8532#endif
8533
8534  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8535    if( nArg!=2 ){
8536      raw_printf(stderr, "Usage: .log FILENAME\n");
8537      rc = 1;
8538    }else{
8539      const char *zFile = azArg[1];
8540      output_file_close(p->pLog);
8541      p->pLog = output_file_open(zFile, 0);
8542    }
8543  }else
8544
8545  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8546    const char *zMode = nArg>=2 ? azArg[1] : "";
8547    int n2 = strlen30(zMode);
8548    int c2 = zMode[0];
8549    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8550      p->mode = MODE_Line;
8551      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8552    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8553      p->mode = MODE_Column;
8554      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
8555        p->showHeader = 1;
8556      }
8557      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8558    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8559      p->mode = MODE_List;
8560      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8561      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8562    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8563      p->mode = MODE_Html;
8564    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8565      p->mode = MODE_Tcl;
8566      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8567      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8568    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8569      p->mode = MODE_Csv;
8570      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8571      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8572    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8573      p->mode = MODE_List;
8574      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8575    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8576      p->mode = MODE_Insert;
8577      set_table_name(p, nArg>=3 ? azArg[2] : "table");
8578    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8579      p->mode = MODE_Quote;
8580      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8581      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8582    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8583      p->mode = MODE_Ascii;
8584      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8585      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8586    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
8587      p->mode = MODE_Markdown;
8588    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
8589      p->mode = MODE_Table;
8590    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
8591      p->mode = MODE_Box;
8592    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
8593      p->mode = MODE_Json;
8594    }else if( nArg==1 ){
8595      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8596    }else{
8597      raw_printf(stderr, "Error: mode should be one of: "
8598         "ascii box column csv html insert json line list markdown "
8599         "quote table tabs tcl\n");
8600      rc = 1;
8601    }
8602    p->cMode = p->mode;
8603  }else
8604
8605  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8606    if( nArg==2 ){
8607      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8608                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8609    }else{
8610      raw_printf(stderr, "Usage: .nullvalue STRING\n");
8611      rc = 1;
8612    }
8613  }else
8614
8615#ifdef SQLITE_DEBUG
8616  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8617    int i;
8618    for(i=1; i<nArg; i++){
8619      const char *z = azArg[i];
8620      if( z[0]=='-' && z[1]=='-' ) z++;
8621      if( strcmp(z,"-repeat")==0 ){
8622        if( i==nArg-1 ){
8623          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8624          rc = 1;
8625        }else{
8626          oomRepeat = (int)integerValue(azArg[++i]);
8627        }
8628      }else if( IsDigit(z[0]) ){
8629        oomCounter = (int)integerValue(azArg[i]);
8630      }else{
8631        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8632        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8633        rc = 1;
8634      }
8635    }
8636    if( rc==0 ){
8637      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8638      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
8639    }
8640  }else
8641#endif /* SQLITE_DEBUG */
8642
8643  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8644    char *zNewFilename;  /* Name of the database file to open */
8645    int iName = 1;       /* Index in azArg[] of the filename */
8646    int newFlag = 0;     /* True to delete file before opening */
8647    /* Close the existing database */
8648    session_close_all(p);
8649    close_db(p->db);
8650    p->db = 0;
8651    p->zDbFilename = 0;
8652    sqlite3_free(p->zFreeOnClose);
8653    p->zFreeOnClose = 0;
8654    p->openMode = SHELL_OPEN_UNSPEC;
8655    p->openFlags = 0;
8656    p->szMax = 0;
8657    /* Check for command-line arguments */
8658    for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
8659      const char *z = azArg[iName];
8660      if( optionMatch(z,"new") ){
8661        newFlag = 1;
8662#ifdef SQLITE_HAVE_ZLIB
8663      }else if( optionMatch(z, "zip") ){
8664        p->openMode = SHELL_OPEN_ZIPFILE;
8665#endif
8666      }else if( optionMatch(z, "append") ){
8667        p->openMode = SHELL_OPEN_APPENDVFS;
8668      }else if( optionMatch(z, "readonly") ){
8669        p->openMode = SHELL_OPEN_READONLY;
8670      }else if( optionMatch(z, "nofollow") ){
8671        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
8672#ifdef SQLITE_ENABLE_DESERIALIZE
8673      }else if( optionMatch(z, "deserialize") ){
8674        p->openMode = SHELL_OPEN_DESERIALIZE;
8675      }else if( optionMatch(z, "hexdb") ){
8676        p->openMode = SHELL_OPEN_HEXDB;
8677      }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8678        p->szMax = integerValue(azArg[++iName]);
8679#endif /* SQLITE_ENABLE_DESERIALIZE */
8680      }else if( z[0]=='-' ){
8681        utf8_printf(stderr, "unknown option: %s\n", z);
8682        rc = 1;
8683        goto meta_command_exit;
8684      }
8685    }
8686    /* If a filename is specified, try to open it first */
8687    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
8688    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
8689      if( newFlag ) shellDeleteFile(zNewFilename);
8690      p->zDbFilename = zNewFilename;
8691      open_db(p, OPEN_DB_KEEPALIVE);
8692      if( p->db==0 ){
8693        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8694        sqlite3_free(zNewFilename);
8695      }else{
8696        p->zFreeOnClose = zNewFilename;
8697      }
8698    }
8699    if( p->db==0 ){
8700      /* As a fall-back open a TEMP database */
8701      p->zDbFilename = 0;
8702      open_db(p, 0);
8703    }
8704  }else
8705
8706  if( (c=='o'
8707        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8708   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
8709  ){
8710    const char *zFile = 0;
8711    int bTxtMode = 0;
8712    int i;
8713    int eMode = 0;
8714    int bBOM = 0;
8715    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
8716
8717    if( c=='e' ){
8718      eMode = 'x';
8719      bOnce = 2;
8720    }else if( strncmp(azArg[0],"once",n)==0 ){
8721      bOnce = 1;
8722    }
8723    for(i=1; i<nArg; i++){
8724      char *z = azArg[i];
8725      if( z[0]=='-' ){
8726        if( z[1]=='-' ) z++;
8727        if( strcmp(z,"-bom")==0 ){
8728          bBOM = 1;
8729        }else if( c!='e' && strcmp(z,"-x")==0 ){
8730          eMode = 'x';  /* spreadsheet */
8731        }else if( c!='e' && strcmp(z,"-e")==0 ){
8732          eMode = 'e';  /* text editor */
8733        }else{
8734          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
8735                      azArg[i]);
8736          showHelp(p->out, azArg[0]);
8737          rc = 1;
8738          goto meta_command_exit;
8739        }
8740      }else if( zFile==0 ){
8741        zFile = z;
8742      }else{
8743        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
8744                    azArg[i]);
8745        showHelp(p->out, azArg[0]);
8746        rc = 1;
8747        goto meta_command_exit;
8748      }
8749    }
8750    if( zFile==0 ) zFile = "stdout";
8751    if( bOnce ){
8752      p->outCount = 2;
8753    }else{
8754      p->outCount = 0;
8755    }
8756    output_reset(p);
8757#ifndef SQLITE_NOHAVE_SYSTEM
8758    if( eMode=='e' || eMode=='x' ){
8759      p->doXdgOpen = 1;
8760      outputModePush(p);
8761      if( eMode=='x' ){
8762        /* spreadsheet mode.  Output as CSV. */
8763        newTempFile(p, "csv");
8764        ShellClearFlag(p, SHFLG_Echo);
8765        p->mode = MODE_Csv;
8766        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8767        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8768      }else{
8769        /* text editor mode */
8770        newTempFile(p, "txt");
8771        bTxtMode = 1;
8772      }
8773      zFile = p->zTempFile;
8774    }
8775#endif /* SQLITE_NOHAVE_SYSTEM */
8776    if( zFile[0]=='|' ){
8777#ifdef SQLITE_OMIT_POPEN
8778      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8779      rc = 1;
8780      p->out = stdout;
8781#else
8782      p->out = popen(zFile + 1, "w");
8783      if( p->out==0 ){
8784        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8785        p->out = stdout;
8786        rc = 1;
8787      }else{
8788        if( bBOM ) fprintf(p->out,"\357\273\277");
8789        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8790      }
8791#endif
8792    }else{
8793      p->out = output_file_open(zFile, bTxtMode);
8794      if( p->out==0 ){
8795        if( strcmp(zFile,"off")!=0 ){
8796          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8797        }
8798        p->out = stdout;
8799        rc = 1;
8800      } else {
8801        if( bBOM ) fprintf(p->out,"\357\273\277");
8802        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8803      }
8804    }
8805  }else
8806
8807  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8808    open_db(p,0);
8809    if( nArg<=1 ) goto parameter_syntax_error;
8810
8811    /* .parameter clear
8812    ** Clear all bind parameters by dropping the TEMP table that holds them.
8813    */
8814    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
8815      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
8816                   0, 0, 0);
8817    }else
8818
8819    /* .parameter list
8820    ** List all bind parameters.
8821    */
8822    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8823      sqlite3_stmt *pStmt = 0;
8824      int rx;
8825      int len = 0;
8826      rx = sqlite3_prepare_v2(p->db,
8827             "SELECT max(length(key)) "
8828             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8829      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8830        len = sqlite3_column_int(pStmt, 0);
8831        if( len>40 ) len = 40;
8832      }
8833      sqlite3_finalize(pStmt);
8834      pStmt = 0;
8835      if( len ){
8836        rx = sqlite3_prepare_v2(p->db,
8837             "SELECT key, quote(value) "
8838             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
8839        while( sqlite3_step(pStmt)==SQLITE_ROW ){
8840          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8841                      sqlite3_column_text(pStmt,1));
8842        }
8843        sqlite3_finalize(pStmt);
8844      }
8845    }else
8846
8847    /* .parameter init
8848    ** Make sure the TEMP table used to hold bind parameters exists.
8849    ** Create it if necessary.
8850    */
8851    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8852      bind_table_init(p);
8853    }else
8854
8855    /* .parameter set NAME VALUE
8856    ** Set or reset a bind parameter.  NAME should be the full parameter
8857    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
8858    ** VALUE can be in either SQL literal notation, or if not it will be
8859    ** understood to be a text string.
8860    */
8861    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8862      int rx;
8863      char *zSql;
8864      sqlite3_stmt *pStmt;
8865      const char *zKey = azArg[2];
8866      const char *zValue = azArg[3];
8867      bind_table_init(p);
8868      zSql = sqlite3_mprintf(
8869                  "REPLACE INTO temp.sqlite_parameters(key,value)"
8870                  "VALUES(%Q,%s);", zKey, zValue);
8871      if( zSql==0 ) shell_out_of_memory();
8872      pStmt = 0;
8873      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8874      sqlite3_free(zSql);
8875      if( rx!=SQLITE_OK ){
8876        sqlite3_finalize(pStmt);
8877        pStmt = 0;
8878        zSql = sqlite3_mprintf(
8879                   "REPLACE INTO temp.sqlite_parameters(key,value)"
8880                   "VALUES(%Q,%Q);", zKey, zValue);
8881        if( zSql==0 ) shell_out_of_memory();
8882        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8883        sqlite3_free(zSql);
8884        if( rx!=SQLITE_OK ){
8885          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8886          sqlite3_finalize(pStmt);
8887          pStmt = 0;
8888          rc = 1;
8889        }
8890      }
8891      sqlite3_step(pStmt);
8892      sqlite3_finalize(pStmt);
8893    }else
8894
8895    /* .parameter unset NAME
8896    ** Remove the NAME binding from the parameter binding table, if it
8897    ** exists.
8898    */
8899    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8900      char *zSql = sqlite3_mprintf(
8901          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
8902      if( zSql==0 ) shell_out_of_memory();
8903      sqlite3_exec(p->db, zSql, 0, 0, 0);
8904      sqlite3_free(zSql);
8905    }else
8906    /* If no command name matches, show a syntax error */
8907    parameter_syntax_error:
8908    showHelp(p->out, "parameter");
8909  }else
8910
8911  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8912    int i;
8913    for(i=1; i<nArg; i++){
8914      if( i>1 ) raw_printf(p->out, " ");
8915      utf8_printf(p->out, "%s", azArg[i]);
8916    }
8917    raw_printf(p->out, "\n");
8918  }else
8919
8920#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
8921  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8922    int i;
8923    int nn = 0;
8924    p->flgProgress = 0;
8925    p->mxProgress = 0;
8926    p->nProgress = 0;
8927    for(i=1; i<nArg; i++){
8928      const char *z = azArg[i];
8929      if( z[0]=='-' ){
8930        z++;
8931        if( z[0]=='-' ) z++;
8932        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
8933          p->flgProgress |= SHELL_PROGRESS_QUIET;
8934          continue;
8935        }
8936        if( strcmp(z,"reset")==0 ){
8937          p->flgProgress |= SHELL_PROGRESS_RESET;
8938          continue;
8939        }
8940        if( strcmp(z,"once")==0 ){
8941          p->flgProgress |= SHELL_PROGRESS_ONCE;
8942          continue;
8943        }
8944        if( strcmp(z,"limit")==0 ){
8945          if( i+1>=nArg ){
8946            utf8_printf(stderr, "Error: missing argument on --limit\n");
8947            rc = 1;
8948            goto meta_command_exit;
8949          }else{
8950            p->mxProgress = (int)integerValue(azArg[++i]);
8951          }
8952          continue;
8953        }
8954        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
8955        rc = 1;
8956        goto meta_command_exit;
8957      }else{
8958        nn = (int)integerValue(z);
8959      }
8960    }
8961    open_db(p, 0);
8962    sqlite3_progress_handler(p->db, nn, progress_handler, p);
8963  }else
8964#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
8965
8966  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
8967    if( nArg >= 2) {
8968      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
8969    }
8970    if( nArg >= 3) {
8971      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
8972    }
8973  }else
8974
8975  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
8976    rc = 2;
8977  }else
8978
8979  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
8980    FILE *inSaved = p->in;
8981    int savedLineno = p->lineno;
8982    if( nArg!=2 ){
8983      raw_printf(stderr, "Usage: .read FILE\n");
8984      rc = 1;
8985      goto meta_command_exit;
8986    }
8987    if( azArg[1][0]=='|' ){
8988      p->in = popen(azArg[1]+1, "r");
8989      if( p->in==0 ){
8990        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8991        rc = 1;
8992      }else{
8993        rc = process_input(p);
8994        pclose(p->in);
8995      }
8996    }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
8997      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
8998      rc = 1;
8999    }else{
9000      rc = process_input(p);
9001      fclose(p->in);
9002    }
9003    p->in = inSaved;
9004    p->lineno = savedLineno;
9005  }else
9006
9007  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
9008    const char *zSrcFile;
9009    const char *zDb;
9010    sqlite3 *pSrc;
9011    sqlite3_backup *pBackup;
9012    int nTimeout = 0;
9013
9014    if( nArg==2 ){
9015      zSrcFile = azArg[1];
9016      zDb = "main";
9017    }else if( nArg==3 ){
9018      zSrcFile = azArg[2];
9019      zDb = azArg[1];
9020    }else{
9021      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9022      rc = 1;
9023      goto meta_command_exit;
9024    }
9025    rc = sqlite3_open(zSrcFile, &pSrc);
9026    if( rc!=SQLITE_OK ){
9027      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9028      close_db(pSrc);
9029      return 1;
9030    }
9031    open_db(p, 0);
9032    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9033    if( pBackup==0 ){
9034      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9035      close_db(pSrc);
9036      return 1;
9037    }
9038    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9039          || rc==SQLITE_BUSY  ){
9040      if( rc==SQLITE_BUSY ){
9041        if( nTimeout++ >= 3 ) break;
9042        sqlite3_sleep(100);
9043      }
9044    }
9045    sqlite3_backup_finish(pBackup);
9046    if( rc==SQLITE_DONE ){
9047      rc = 0;
9048    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9049      raw_printf(stderr, "Error: source database is busy\n");
9050      rc = 1;
9051    }else{
9052      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9053      rc = 1;
9054    }
9055    close_db(pSrc);
9056  }else
9057
9058  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
9059    if( nArg==2 ){
9060      p->scanstatsOn = (u8)booleanValue(azArg[1]);
9061#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9062      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9063#endif
9064    }else{
9065      raw_printf(stderr, "Usage: .scanstats on|off\n");
9066      rc = 1;
9067    }
9068  }else
9069
9070  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
9071    ShellText sSelect;
9072    ShellState data;
9073    char *zErrMsg = 0;
9074    const char *zDiv = "(";
9075    const char *zName = 0;
9076    int iSchema = 0;
9077    int bDebug = 0;
9078    int ii;
9079
9080    open_db(p, 0);
9081    memcpy(&data, p, sizeof(data));
9082    data.showHeader = 0;
9083    data.cMode = data.mode = MODE_Semi;
9084    initText(&sSelect);
9085    for(ii=1; ii<nArg; ii++){
9086      if( optionMatch(azArg[ii],"indent") ){
9087        data.cMode = data.mode = MODE_Pretty;
9088      }else if( optionMatch(azArg[ii],"debug") ){
9089        bDebug = 1;
9090      }else if( zName==0 ){
9091        zName = azArg[ii];
9092      }else{
9093        raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
9094        rc = 1;
9095        goto meta_command_exit;
9096      }
9097    }
9098    if( zName!=0 ){
9099      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9100                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9101                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9102                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9103      if( isSchema ){
9104        char *new_argv[2], *new_colv[2];
9105        new_argv[0] = sqlite3_mprintf(
9106                      "CREATE TABLE %s (\n"
9107                      "  type text,\n"
9108                      "  name text,\n"
9109                      "  tbl_name text,\n"
9110                      "  rootpage integer,\n"
9111                      "  sql text\n"
9112                      ")", zName);
9113        new_argv[1] = 0;
9114        new_colv[0] = "sql";
9115        new_colv[1] = 0;
9116        callback(&data, 1, new_argv, new_colv);
9117        sqlite3_free(new_argv[0]);
9118      }
9119    }
9120    if( zDiv ){
9121      sqlite3_stmt *pStmt = 0;
9122      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9123                              -1, &pStmt, 0);
9124      if( rc ){
9125        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9126        sqlite3_finalize(pStmt);
9127        rc = 1;
9128        goto meta_command_exit;
9129      }
9130      appendText(&sSelect, "SELECT sql FROM", 0);
9131      iSchema = 0;
9132      while( sqlite3_step(pStmt)==SQLITE_ROW ){
9133        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9134        char zScNum[30];
9135        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9136        appendText(&sSelect, zDiv, 0);
9137        zDiv = " UNION ALL ";
9138        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9139        if( sqlite3_stricmp(zDb, "main")!=0 ){
9140          appendText(&sSelect, zDb, '\'');
9141        }else{
9142          appendText(&sSelect, "NULL", 0);
9143        }
9144        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
9145        appendText(&sSelect, zScNum, 0);
9146        appendText(&sSelect, " AS snum, ", 0);
9147        appendText(&sSelect, zDb, '\'');
9148        appendText(&sSelect, " AS sname FROM ", 0);
9149        appendText(&sSelect, zDb, quoteChar(zDb));
9150        appendText(&sSelect, ".sqlite_schema", 0);
9151      }
9152      sqlite3_finalize(pStmt);
9153#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
9154      if( zName ){
9155        appendText(&sSelect,
9156           " UNION ALL SELECT shell_module_schema(name),"
9157           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
9158        0);
9159      }
9160#endif
9161      appendText(&sSelect, ") WHERE ", 0);
9162      if( zName ){
9163        char *zQarg = sqlite3_mprintf("%Q", zName);
9164        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
9165                    strchr(zName, '[') != 0;
9166        if( strchr(zName, '.') ){
9167          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
9168        }else{
9169          appendText(&sSelect, "lower(tbl_name)", 0);
9170        }
9171        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
9172        appendText(&sSelect, zQarg, 0);
9173        if( !bGlob ){
9174          appendText(&sSelect, " ESCAPE '\\' ", 0);
9175        }
9176        appendText(&sSelect, " AND ", 0);
9177        sqlite3_free(zQarg);
9178      }
9179      appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
9180                           " ORDER BY snum, rowid", 0);
9181      if( bDebug ){
9182        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
9183      }else{
9184        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
9185      }
9186      freeText(&sSelect);
9187    }
9188    if( zErrMsg ){
9189      utf8_printf(stderr,"Error: %s\n", zErrMsg);
9190      sqlite3_free(zErrMsg);
9191      rc = 1;
9192    }else if( rc != SQLITE_OK ){
9193      raw_printf(stderr,"Error: querying schema information\n");
9194      rc = 1;
9195    }else{
9196      rc = 0;
9197    }
9198  }else
9199
9200#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9201  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
9202    sqlite3_unsupported_selecttrace = nArg>=2 ? (int)integerValue(azArg[1]) : 0xffff;
9203  }else
9204#endif
9205
9206#if defined(SQLITE_ENABLE_SESSION)
9207  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
9208    OpenSession *pSession = &p->aSession[0];
9209    char **azCmd = &azArg[1];
9210    int iSes = 0;
9211    int nCmd = nArg - 1;
9212    int i;
9213    if( nArg<=1 ) goto session_syntax_error;
9214    open_db(p, 0);
9215    if( nArg>=3 ){
9216      for(iSes=0; iSes<p->nSession; iSes++){
9217        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
9218      }
9219      if( iSes<p->nSession ){
9220        pSession = &p->aSession[iSes];
9221        azCmd++;
9222        nCmd--;
9223      }else{
9224        pSession = &p->aSession[0];
9225        iSes = 0;
9226      }
9227    }
9228
9229    /* .session attach TABLE
9230    ** Invoke the sqlite3session_attach() interface to attach a particular
9231    ** table so that it is never filtered.
9232    */
9233    if( strcmp(azCmd[0],"attach")==0 ){
9234      if( nCmd!=2 ) goto session_syntax_error;
9235      if( pSession->p==0 ){
9236        session_not_open:
9237        raw_printf(stderr, "ERROR: No sessions are open\n");
9238      }else{
9239        rc = sqlite3session_attach(pSession->p, azCmd[1]);
9240        if( rc ){
9241          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
9242          rc = 0;
9243        }
9244      }
9245    }else
9246
9247    /* .session changeset FILE
9248    ** .session patchset FILE
9249    ** Write a changeset or patchset into a file.  The file is overwritten.
9250    */
9251    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
9252      FILE *out = 0;
9253      if( nCmd!=2 ) goto session_syntax_error;
9254      if( pSession->p==0 ) goto session_not_open;
9255      out = fopen(azCmd[1], "wb");
9256      if( out==0 ){
9257        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
9258                    azCmd[1]);
9259      }else{
9260        int szChng;
9261        void *pChng;
9262        if( azCmd[0][0]=='c' ){
9263          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
9264        }else{
9265          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
9266        }
9267        if( rc ){
9268          printf("Error: error code %d\n", rc);
9269          rc = 0;
9270        }
9271        if( pChng
9272          && fwrite(pChng, szChng, 1, out)!=1 ){
9273          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
9274                  szChng);
9275        }
9276        sqlite3_free(pChng);
9277        fclose(out);
9278      }
9279    }else
9280
9281    /* .session close
9282    ** Close the identified session
9283    */
9284    if( strcmp(azCmd[0], "close")==0 ){
9285      if( nCmd!=1 ) goto session_syntax_error;
9286      if( p->nSession ){
9287        session_close(pSession);
9288        p->aSession[iSes] = p->aSession[--p->nSession];
9289      }
9290    }else
9291
9292    /* .session enable ?BOOLEAN?
9293    ** Query or set the enable flag
9294    */
9295    if( strcmp(azCmd[0], "enable")==0 ){
9296      int ii;
9297      if( nCmd>2 ) goto session_syntax_error;
9298      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9299      if( p->nSession ){
9300        ii = sqlite3session_enable(pSession->p, ii);
9301        utf8_printf(p->out, "session %s enable flag = %d\n",
9302                    pSession->zName, ii);
9303      }
9304    }else
9305
9306    /* .session filter GLOB ....
9307    ** Set a list of GLOB patterns of table names to be excluded.
9308    */
9309    if( strcmp(azCmd[0], "filter")==0 ){
9310      int ii, nByte;
9311      if( nCmd<2 ) goto session_syntax_error;
9312      if( p->nSession ){
9313        for(ii=0; ii<pSession->nFilter; ii++){
9314          sqlite3_free(pSession->azFilter[ii]);
9315        }
9316        sqlite3_free(pSession->azFilter);
9317        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
9318        pSession->azFilter = sqlite3_malloc( nByte );
9319        if( pSession->azFilter==0 ){
9320          raw_printf(stderr, "Error: out or memory\n");
9321          exit(1);
9322        }
9323        for(ii=1; ii<nCmd; ii++){
9324          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
9325        }
9326        pSession->nFilter = ii-1;
9327      }
9328    }else
9329
9330    /* .session indirect ?BOOLEAN?
9331    ** Query or set the indirect flag
9332    */
9333    if( strcmp(azCmd[0], "indirect")==0 ){
9334      int ii;
9335      if( nCmd>2 ) goto session_syntax_error;
9336      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
9337      if( p->nSession ){
9338        ii = sqlite3session_indirect(pSession->p, ii);
9339        utf8_printf(p->out, "session %s indirect flag = %d\n",
9340                    pSession->zName, ii);
9341      }
9342    }else
9343
9344    /* .session isempty
9345    ** Determine if the session is empty
9346    */
9347    if( strcmp(azCmd[0], "isempty")==0 ){
9348      int ii;
9349      if( nCmd!=1 ) goto session_syntax_error;
9350      if( p->nSession ){
9351        ii = sqlite3session_isempty(pSession->p);
9352        utf8_printf(p->out, "session %s isempty flag = %d\n",
9353                    pSession->zName, ii);
9354      }
9355    }else
9356
9357    /* .session list
9358    ** List all currently open sessions
9359    */
9360    if( strcmp(azCmd[0],"list")==0 ){
9361      for(i=0; i<p->nSession; i++){
9362        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
9363      }
9364    }else
9365
9366    /* .session open DB NAME
9367    ** Open a new session called NAME on the attached database DB.
9368    ** DB is normally "main".
9369    */
9370    if( strcmp(azCmd[0],"open")==0 ){
9371      char *zName;
9372      if( nCmd!=3 ) goto session_syntax_error;
9373      zName = azCmd[2];
9374      if( zName[0]==0 ) goto session_syntax_error;
9375      for(i=0; i<p->nSession; i++){
9376        if( strcmp(p->aSession[i].zName,zName)==0 ){
9377          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
9378          goto meta_command_exit;
9379        }
9380      }
9381      if( p->nSession>=ArraySize(p->aSession) ){
9382        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9383        goto meta_command_exit;
9384      }
9385      pSession = &p->aSession[p->nSession];
9386      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9387      if( rc ){
9388        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9389        rc = 0;
9390        goto meta_command_exit;
9391      }
9392      pSession->nFilter = 0;
9393      sqlite3session_table_filter(pSession->p, session_filter, pSession);
9394      p->nSession++;
9395      pSession->zName = sqlite3_mprintf("%s", zName);
9396    }else
9397    /* If no command name matches, show a syntax error */
9398    session_syntax_error:
9399    showHelp(p->out, "session");
9400  }else
9401#endif
9402
9403#ifdef SQLITE_DEBUG
9404  /* Undocumented commands for internal testing.  Subject to change
9405  ** without notice. */
9406  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9407    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9408      int i, v;
9409      for(i=1; i<nArg; i++){
9410        v = booleanValue(azArg[i]);
9411        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9412      }
9413    }
9414    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9415      int i; sqlite3_int64 v;
9416      for(i=1; i<nArg; i++){
9417        char zBuf[200];
9418        v = integerValue(azArg[i]);
9419        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9420        utf8_printf(p->out, "%s", zBuf);
9421      }
9422    }
9423  }else
9424#endif
9425
9426  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9427    int bIsInit = 0;         /* True to initialize the SELFTEST table */
9428    int bVerbose = 0;        /* Verbose output */
9429    int bSelftestExists;     /* True if SELFTEST already exists */
9430    int i, k;                /* Loop counters */
9431    int nTest = 0;           /* Number of tests runs */
9432    int nErr = 0;            /* Number of errors seen */
9433    ShellText str;           /* Answer for a query */
9434    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9435
9436    open_db(p,0);
9437    for(i=1; i<nArg; i++){
9438      const char *z = azArg[i];
9439      if( z[0]=='-' && z[1]=='-' ) z++;
9440      if( strcmp(z,"-init")==0 ){
9441        bIsInit = 1;
9442      }else
9443      if( strcmp(z,"-v")==0 ){
9444        bVerbose++;
9445      }else
9446      {
9447        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9448                    azArg[i], azArg[0]);
9449        raw_printf(stderr, "Should be one of: --init -v\n");
9450        rc = 1;
9451        goto meta_command_exit;
9452      }
9453    }
9454    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9455           != SQLITE_OK ){
9456      bSelftestExists = 0;
9457    }else{
9458      bSelftestExists = 1;
9459    }
9460    if( bIsInit ){
9461      createSelftestTable(p);
9462      bSelftestExists = 1;
9463    }
9464    initText(&str);
9465    appendText(&str, "x", 0);
9466    for(k=bSelftestExists; k>=0; k--){
9467      if( k==1 ){
9468        rc = sqlite3_prepare_v2(p->db,
9469            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9470            -1, &pStmt, 0);
9471      }else{
9472        rc = sqlite3_prepare_v2(p->db,
9473          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9474          "      (1,'run','PRAGMA integrity_check','ok')",
9475          -1, &pStmt, 0);
9476      }
9477      if( rc ){
9478        raw_printf(stderr, "Error querying the selftest table\n");
9479        rc = 1;
9480        sqlite3_finalize(pStmt);
9481        goto meta_command_exit;
9482      }
9483      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9484        int tno = sqlite3_column_int(pStmt, 0);
9485        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9486        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9487        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9488
9489        k = 0;
9490        if( bVerbose>0 ){
9491          char *zQuote = sqlite3_mprintf("%q", zSql);
9492          printf("%d: %s %s\n", tno, zOp, zSql);
9493          sqlite3_free(zQuote);
9494        }
9495        if( strcmp(zOp,"memo")==0 ){
9496          utf8_printf(p->out, "%s\n", zSql);
9497        }else
9498        if( strcmp(zOp,"run")==0 ){
9499          char *zErrMsg = 0;
9500          str.n = 0;
9501          str.z[0] = 0;
9502          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9503          nTest++;
9504          if( bVerbose ){
9505            utf8_printf(p->out, "Result: %s\n", str.z);
9506          }
9507          if( rc || zErrMsg ){
9508            nErr++;
9509            rc = 1;
9510            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9511            sqlite3_free(zErrMsg);
9512          }else if( strcmp(zAns,str.z)!=0 ){
9513            nErr++;
9514            rc = 1;
9515            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9516            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
9517          }
9518        }else
9519        {
9520          utf8_printf(stderr,
9521            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9522          rc = 1;
9523          break;
9524        }
9525      } /* End loop over rows of content from SELFTEST */
9526      sqlite3_finalize(pStmt);
9527    } /* End loop over k */
9528    freeText(&str);
9529    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9530  }else
9531
9532  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9533    if( nArg<2 || nArg>3 ){
9534      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9535      rc = 1;
9536    }
9537    if( nArg>=2 ){
9538      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9539                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9540    }
9541    if( nArg>=3 ){
9542      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9543                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9544    }
9545  }else
9546
9547  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9548    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
9549    int i;                   /* Loop counter */
9550    int bSchema = 0;         /* Also hash the schema */
9551    int bSeparate = 0;       /* Hash each table separately */
9552    int iSize = 224;         /* Hash algorithm to use */
9553    int bDebug = 0;          /* Only show the query that would have run */
9554    sqlite3_stmt *pStmt;     /* For querying tables names */
9555    char *zSql;              /* SQL to be run */
9556    char *zSep;              /* Separator */
9557    ShellText sSql;          /* Complete SQL for the query to run the hash */
9558    ShellText sQuery;        /* Set of queries used to read all content */
9559    open_db(p, 0);
9560    for(i=1; i<nArg; i++){
9561      const char *z = azArg[i];
9562      if( z[0]=='-' ){
9563        z++;
9564        if( z[0]=='-' ) z++;
9565        if( strcmp(z,"schema")==0 ){
9566          bSchema = 1;
9567        }else
9568        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9569         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9570        ){
9571          iSize = atoi(&z[5]);
9572        }else
9573        if( strcmp(z,"debug")==0 ){
9574          bDebug = 1;
9575        }else
9576        {
9577          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9578                      azArg[i], azArg[0]);
9579          showHelp(p->out, azArg[0]);
9580          rc = 1;
9581          goto meta_command_exit;
9582        }
9583      }else if( zLike ){
9584        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9585        rc = 1;
9586        goto meta_command_exit;
9587      }else{
9588        zLike = z;
9589        bSeparate = 1;
9590        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
9591      }
9592    }
9593    if( bSchema ){
9594      zSql = "SELECT lower(name) FROM sqlite_schema"
9595             " WHERE type='table' AND coalesce(rootpage,0)>1"
9596             " UNION ALL SELECT 'sqlite_schema'"
9597             " ORDER BY 1 collate nocase";
9598    }else{
9599      zSql = "SELECT lower(name) FROM sqlite_schema"
9600             " WHERE type='table' AND coalesce(rootpage,0)>1"
9601             " AND name NOT LIKE 'sqlite_%'"
9602             " ORDER BY 1 collate nocase";
9603    }
9604    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9605    initText(&sQuery);
9606    initText(&sSql);
9607    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9608    zSep = "VALUES(";
9609    while( SQLITE_ROW==sqlite3_step(pStmt) ){
9610      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9611      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9612      if( strncmp(zTab, "sqlite_",7)!=0 ){
9613        appendText(&sQuery,"SELECT * FROM ", 0);
9614        appendText(&sQuery,zTab,'"');
9615        appendText(&sQuery," NOT INDEXED;", 0);
9616      }else if( strcmp(zTab, "sqlite_schema")==0 ){
9617        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
9618                           " ORDER BY name;", 0);
9619      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9620        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9621                           " ORDER BY name;", 0);
9622      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9623        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9624                           " ORDER BY tbl,idx;", 0);
9625      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
9626        appendText(&sQuery, "SELECT * FROM ", 0);
9627        appendText(&sQuery, zTab, 0);
9628        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9629      }
9630      appendText(&sSql, zSep, 0);
9631      appendText(&sSql, sQuery.z, '\'');
9632      sQuery.n = 0;
9633      appendText(&sSql, ",", 0);
9634      appendText(&sSql, zTab, '\'');
9635      zSep = "),(";
9636    }
9637    sqlite3_finalize(pStmt);
9638    if( bSeparate ){
9639      zSql = sqlite3_mprintf(
9640          "%s))"
9641          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9642          "   FROM [sha3sum$query]",
9643          sSql.z, iSize);
9644    }else{
9645      zSql = sqlite3_mprintf(
9646          "%s))"
9647          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9648          "   FROM [sha3sum$query]",
9649          sSql.z, iSize);
9650    }
9651    freeText(&sQuery);
9652    freeText(&sSql);
9653    if( bDebug ){
9654      utf8_printf(p->out, "%s\n", zSql);
9655    }else{
9656      shell_exec(p, zSql, 0);
9657    }
9658    sqlite3_free(zSql);
9659  }else
9660
9661#ifndef SQLITE_NOHAVE_SYSTEM
9662  if( c=='s'
9663   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9664  ){
9665    char *zCmd;
9666    int i, x;
9667    if( nArg<2 ){
9668      raw_printf(stderr, "Usage: .system COMMAND\n");
9669      rc = 1;
9670      goto meta_command_exit;
9671    }
9672    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9673    for(i=2; i<nArg; i++){
9674      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9675                             zCmd, azArg[i]);
9676    }
9677    x = system(zCmd);
9678    sqlite3_free(zCmd);
9679    if( x ) raw_printf(stderr, "System command returns %d\n", x);
9680  }else
9681#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
9682
9683  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
9684    static const char *azBool[] = { "off", "on", "trigger", "full"};
9685    int i;
9686    if( nArg!=1 ){
9687      raw_printf(stderr, "Usage: .show\n");
9688      rc = 1;
9689      goto meta_command_exit;
9690    }
9691    utf8_printf(p->out, "%12.12s: %s\n","echo",
9692                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
9693    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9694    utf8_printf(p->out, "%12.12s: %s\n","explain",
9695         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9696    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9697    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9698    utf8_printf(p->out, "%12.12s: ", "nullvalue");
9699      output_c_string(p->out, p->nullValue);
9700      raw_printf(p->out, "\n");
9701    utf8_printf(p->out,"%12.12s: %s\n","output",
9702            strlen30(p->outfile) ? p->outfile : "stdout");
9703    utf8_printf(p->out,"%12.12s: ", "colseparator");
9704      output_c_string(p->out, p->colSeparator);
9705      raw_printf(p->out, "\n");
9706    utf8_printf(p->out,"%12.12s: ", "rowseparator");
9707      output_c_string(p->out, p->rowSeparator);
9708      raw_printf(p->out, "\n");
9709    utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
9710    utf8_printf(p->out, "%12.12s: ", "width");
9711    for (i=0;i<p->nWidth;i++) {
9712      raw_printf(p->out, "%d ", p->colWidth[i]);
9713    }
9714    raw_printf(p->out, "\n");
9715    utf8_printf(p->out, "%12.12s: %s\n", "filename",
9716                p->zDbFilename ? p->zDbFilename : "");
9717  }else
9718
9719  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9720    if( nArg==2 ){
9721      p->statsOn = (u8)booleanValue(azArg[1]);
9722    }else if( nArg==1 ){
9723      display_stats(p->db, p, 0);
9724    }else{
9725      raw_printf(stderr, "Usage: .stats ?on|off?\n");
9726      rc = 1;
9727    }
9728  }else
9729
9730  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9731   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9732                 || strncmp(azArg[0], "indexes", n)==0) )
9733  ){
9734    sqlite3_stmt *pStmt;
9735    char **azResult;
9736    int nRow, nAlloc;
9737    int ii;
9738    ShellText s;
9739    initText(&s);
9740    open_db(p, 0);
9741    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
9742    if( rc ){
9743      sqlite3_finalize(pStmt);
9744      return shellDatabaseError(p->db);
9745    }
9746
9747    if( nArg>2 && c=='i' ){
9748      /* It is an historical accident that the .indexes command shows an error
9749      ** when called with the wrong number of arguments whereas the .tables
9750      ** command does not. */
9751      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9752      rc = 1;
9753      sqlite3_finalize(pStmt);
9754      goto meta_command_exit;
9755    }
9756    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9757      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9758      if( zDbName==0 ) continue;
9759      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9760      if( sqlite3_stricmp(zDbName, "main")==0 ){
9761        appendText(&s, "SELECT name FROM ", 0);
9762      }else{
9763        appendText(&s, "SELECT ", 0);
9764        appendText(&s, zDbName, '\'');
9765        appendText(&s, "||'.'||name FROM ", 0);
9766      }
9767      appendText(&s, zDbName, '"');
9768      appendText(&s, ".sqlite_schema ", 0);
9769      if( c=='t' ){
9770        appendText(&s," WHERE type IN ('table','view')"
9771                      "   AND name NOT LIKE 'sqlite_%'"
9772                      "   AND name LIKE ?1", 0);
9773      }else{
9774        appendText(&s," WHERE type='index'"
9775                      "   AND tbl_name LIKE ?1", 0);
9776      }
9777    }
9778    rc = sqlite3_finalize(pStmt);
9779    appendText(&s, " ORDER BY 1", 0);
9780    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9781    freeText(&s);
9782    if( rc ) return shellDatabaseError(p->db);
9783
9784    /* Run the SQL statement prepared by the above block. Store the results
9785    ** as an array of nul-terminated strings in azResult[].  */
9786    nRow = nAlloc = 0;
9787    azResult = 0;
9788    if( nArg>1 ){
9789      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9790    }else{
9791      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9792    }
9793    while( sqlite3_step(pStmt)==SQLITE_ROW ){
9794      if( nRow>=nAlloc ){
9795        char **azNew;
9796        int n2 = nAlloc*2 + 10;
9797        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
9798        if( azNew==0 ) shell_out_of_memory();
9799        nAlloc = n2;
9800        azResult = azNew;
9801      }
9802      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
9803      if( 0==azResult[nRow] ) shell_out_of_memory();
9804      nRow++;
9805    }
9806    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9807      rc = shellDatabaseError(p->db);
9808    }
9809
9810    /* Pretty-print the contents of array azResult[] to the output */
9811    if( rc==0 && nRow>0 ){
9812      int len, maxlen = 0;
9813      int i, j;
9814      int nPrintCol, nPrintRow;
9815      for(i=0; i<nRow; i++){
9816        len = strlen30(azResult[i]);
9817        if( len>maxlen ) maxlen = len;
9818      }
9819      nPrintCol = 80/(maxlen+2);
9820      if( nPrintCol<1 ) nPrintCol = 1;
9821      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9822      for(i=0; i<nPrintRow; i++){
9823        for(j=i; j<nRow; j+=nPrintRow){
9824          char *zSp = j<nPrintRow ? "" : "  ";
9825          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9826                      azResult[j] ? azResult[j]:"");
9827        }
9828        raw_printf(p->out, "\n");
9829      }
9830    }
9831
9832    for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9833    sqlite3_free(azResult);
9834  }else
9835
9836  /* Begin redirecting output to the file "testcase-out.txt" */
9837  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9838    output_reset(p);
9839    p->out = output_file_open("testcase-out.txt", 0);
9840    if( p->out==0 ){
9841      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9842    }
9843    if( nArg>=2 ){
9844      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9845    }else{
9846      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9847    }
9848  }else
9849
9850#ifndef SQLITE_UNTESTABLE
9851  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
9852    static const struct {
9853       const char *zCtrlName;   /* Name of a test-control option */
9854       int ctrlCode;            /* Integer code for that option */
9855       const char *zUsage;      /* Usage notes */
9856    } aCtrl[] = {
9857      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
9858      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
9859    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
9860    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
9861      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
9862      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
9863    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
9864      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
9865      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
9866      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
9867      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
9868      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
9869#ifdef YYCOVERAGE
9870      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
9871#endif
9872      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
9873      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
9874      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
9875      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
9876      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
9877    };
9878    int testctrl = -1;
9879    int iCtrl = -1;
9880    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
9881    int isOk = 0;
9882    int i, n2;
9883    const char *zCmd = 0;
9884
9885    open_db(p, 0);
9886    zCmd = nArg>=2 ? azArg[1] : "help";
9887
9888    /* The argument can optionally begin with "-" or "--" */
9889    if( zCmd[0]=='-' && zCmd[1] ){
9890      zCmd++;
9891      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9892    }
9893
9894    /* --help lists all test-controls */
9895    if( strcmp(zCmd,"help")==0 ){
9896      utf8_printf(p->out, "Available test-controls:\n");
9897      for(i=0; i<ArraySize(aCtrl); i++){
9898        utf8_printf(p->out, "  .testctrl %s %s\n",
9899                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
9900      }
9901      rc = 1;
9902      goto meta_command_exit;
9903    }
9904
9905    /* convert testctrl text option to value. allow any unique prefix
9906    ** of the option name, or a numerical value. */
9907    n2 = strlen30(zCmd);
9908    for(i=0; i<ArraySize(aCtrl); i++){
9909      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
9910        if( testctrl<0 ){
9911          testctrl = aCtrl[i].ctrlCode;
9912          iCtrl = i;
9913        }else{
9914          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
9915                              "Use \".testctrl --help\" for help\n", zCmd);
9916          rc = 1;
9917          goto meta_command_exit;
9918        }
9919      }
9920    }
9921    if( testctrl<0 ){
9922      utf8_printf(stderr,"Error: unknown test-control: %s\n"
9923                         "Use \".testctrl --help\" for help\n", zCmd);
9924    }else{
9925      switch(testctrl){
9926
9927        /* sqlite3_test_control(int, db, int) */
9928        case SQLITE_TESTCTRL_OPTIMIZATIONS:
9929          if( nArg==3 ){
9930            int opt = (int)strtol(azArg[2], 0, 0);
9931            rc2 = sqlite3_test_control(testctrl, p->db, opt);
9932            isOk = 3;
9933          }
9934          break;
9935
9936        /* sqlite3_test_control(int) */
9937        case SQLITE_TESTCTRL_PRNG_SAVE:
9938        case SQLITE_TESTCTRL_PRNG_RESTORE:
9939        case SQLITE_TESTCTRL_BYTEORDER:
9940          if( nArg==2 ){
9941            rc2 = sqlite3_test_control(testctrl);
9942            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
9943          }
9944          break;
9945
9946        /* sqlite3_test_control(int, uint) */
9947        case SQLITE_TESTCTRL_PENDING_BYTE:
9948          if( nArg==3 ){
9949            unsigned int opt = (unsigned int)integerValue(azArg[2]);
9950            rc2 = sqlite3_test_control(testctrl, opt);
9951            isOk = 3;
9952          }
9953          break;
9954
9955        /* sqlite3_test_control(int, int, sqlite3*) */
9956        case SQLITE_TESTCTRL_PRNG_SEED:
9957          if( nArg==3 || nArg==4 ){
9958            int ii = (int)integerValue(azArg[2]);
9959            sqlite3 *db;
9960            if( ii==0 && strcmp(azArg[2],"random")==0 ){
9961              sqlite3_randomness(sizeof(ii),&ii);
9962              printf("-- random seed: %d\n", ii);
9963            }
9964            if( nArg==3 ){
9965              db = 0;
9966            }else{
9967              db = p->db;
9968              /* Make sure the schema has been loaded */
9969              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
9970            }
9971            rc2 = sqlite3_test_control(testctrl, ii, db);
9972            isOk = 3;
9973          }
9974          break;
9975
9976        /* sqlite3_test_control(int, int) */
9977        case SQLITE_TESTCTRL_ASSERT:
9978        case SQLITE_TESTCTRL_ALWAYS:
9979          if( nArg==3 ){
9980            int opt = booleanValue(azArg[2]);
9981            rc2 = sqlite3_test_control(testctrl, opt);
9982            isOk = 1;
9983          }
9984          break;
9985
9986        /* sqlite3_test_control(int, int) */
9987        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
9988        case SQLITE_TESTCTRL_NEVER_CORRUPT:
9989          if( nArg==3 ){
9990            int opt = booleanValue(azArg[2]);
9991            rc2 = sqlite3_test_control(testctrl, opt);
9992            isOk = 3;
9993          }
9994          break;
9995
9996        /* sqlite3_test_control(sqlite3*) */
9997        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
9998          rc2 = sqlite3_test_control(testctrl, p->db);
9999          isOk = 3;
10000          break;
10001
10002        case SQLITE_TESTCTRL_IMPOSTER:
10003          if( nArg==5 ){
10004            rc2 = sqlite3_test_control(testctrl, p->db,
10005                          azArg[2],
10006                          integerValue(azArg[3]),
10007                          integerValue(azArg[4]));
10008            isOk = 3;
10009          }
10010          break;
10011
10012        case SQLITE_TESTCTRL_SEEK_COUNT: {
10013          u64 x = 0;
10014          rc2 = sqlite3_test_control(testctrl, p->db, &x);
10015          utf8_printf(p->out, "%llu\n", x);
10016          isOk = 3;
10017          break;
10018        }
10019
10020#ifdef YYCOVERAGE
10021        case SQLITE_TESTCTRL_PARSER_COVERAGE:
10022          if( nArg==2 ){
10023            sqlite3_test_control(testctrl, p->out);
10024            isOk = 3;
10025          }
10026#endif
10027      }
10028    }
10029    if( isOk==0 && iCtrl>=0 ){
10030      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
10031      rc = 1;
10032    }else if( isOk==1 ){
10033      raw_printf(p->out, "%d\n", rc2);
10034    }else if( isOk==2 ){
10035      raw_printf(p->out, "0x%08x\n", rc2);
10036    }
10037  }else
10038#endif /* !defined(SQLITE_UNTESTABLE) */
10039
10040  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
10041    open_db(p, 0);
10042    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
10043  }else
10044
10045  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
10046    if( nArg==2 ){
10047      enableTimer = booleanValue(azArg[1]);
10048      if( enableTimer && !HAS_TIMER ){
10049        raw_printf(stderr, "Error: timer not available on this system.\n");
10050        enableTimer = 0;
10051      }
10052    }else{
10053      raw_printf(stderr, "Usage: .timer on|off\n");
10054      rc = 1;
10055    }
10056  }else
10057
10058#ifndef SQLITE_OMIT_TRACE
10059  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
10060    int mType = 0;
10061    int jj;
10062    open_db(p, 0);
10063    for(jj=1; jj<nArg; jj++){
10064      const char *z = azArg[jj];
10065      if( z[0]=='-' ){
10066        if( optionMatch(z, "expanded") ){
10067          p->eTraceType = SHELL_TRACE_EXPANDED;
10068        }
10069#ifdef SQLITE_ENABLE_NORMALIZE
10070        else if( optionMatch(z, "normalized") ){
10071          p->eTraceType = SHELL_TRACE_NORMALIZED;
10072        }
10073#endif
10074        else if( optionMatch(z, "plain") ){
10075          p->eTraceType = SHELL_TRACE_PLAIN;
10076        }
10077        else if( optionMatch(z, "profile") ){
10078          mType |= SQLITE_TRACE_PROFILE;
10079        }
10080        else if( optionMatch(z, "row") ){
10081          mType |= SQLITE_TRACE_ROW;
10082        }
10083        else if( optionMatch(z, "stmt") ){
10084          mType |= SQLITE_TRACE_STMT;
10085        }
10086        else if( optionMatch(z, "close") ){
10087          mType |= SQLITE_TRACE_CLOSE;
10088        }
10089        else {
10090          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
10091          rc = 1;
10092          goto meta_command_exit;
10093        }
10094      }else{
10095        output_file_close(p->traceOut);
10096        p->traceOut = output_file_open(azArg[1], 0);
10097      }
10098    }
10099    if( p->traceOut==0 ){
10100      sqlite3_trace_v2(p->db, 0, 0, 0);
10101    }else{
10102      if( mType==0 ) mType = SQLITE_TRACE_STMT;
10103      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
10104    }
10105  }else
10106#endif /* !defined(SQLITE_OMIT_TRACE) */
10107
10108#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10109  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
10110    int ii;
10111    int lenOpt;
10112    char *zOpt;
10113    if( nArg<2 ){
10114      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
10115      rc = 1;
10116      goto meta_command_exit;
10117    }
10118    open_db(p, 0);
10119    zOpt = azArg[1];
10120    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
10121    lenOpt = (int)strlen(zOpt);
10122    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
10123      assert( azArg[nArg]==0 );
10124      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
10125    }else{
10126      for(ii=1; ii<nArg; ii++){
10127        sqlite3_create_module(p->db, azArg[ii], 0, 0);
10128      }
10129    }
10130  }else
10131#endif
10132
10133#if SQLITE_USER_AUTHENTICATION
10134  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
10135    if( nArg<2 ){
10136      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
10137      rc = 1;
10138      goto meta_command_exit;
10139    }
10140    open_db(p, 0);
10141    if( strcmp(azArg[1],"login")==0 ){
10142      if( nArg!=4 ){
10143        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
10144        rc = 1;
10145        goto meta_command_exit;
10146      }
10147      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
10148                                     strlen30(azArg[3]));
10149      if( rc ){
10150        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
10151        rc = 1;
10152      }
10153    }else if( strcmp(azArg[1],"add")==0 ){
10154      if( nArg!=5 ){
10155        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
10156        rc = 1;
10157        goto meta_command_exit;
10158      }
10159      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10160                            booleanValue(azArg[4]));
10161      if( rc ){
10162        raw_printf(stderr, "User-Add failed: %d\n", rc);
10163        rc = 1;
10164      }
10165    }else if( strcmp(azArg[1],"edit")==0 ){
10166      if( nArg!=5 ){
10167        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
10168        rc = 1;
10169        goto meta_command_exit;
10170      }
10171      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
10172                              booleanValue(azArg[4]));
10173      if( rc ){
10174        raw_printf(stderr, "User-Edit failed: %d\n", rc);
10175        rc = 1;
10176      }
10177    }else if( strcmp(azArg[1],"delete")==0 ){
10178      if( nArg!=3 ){
10179        raw_printf(stderr, "Usage: .user delete USER\n");
10180        rc = 1;
10181        goto meta_command_exit;
10182      }
10183      rc = sqlite3_user_delete(p->db, azArg[2]);
10184      if( rc ){
10185        raw_printf(stderr, "User-Delete failed: %d\n", rc);
10186        rc = 1;
10187      }
10188    }else{
10189      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
10190      rc = 1;
10191      goto meta_command_exit;
10192    }
10193  }else
10194#endif /* SQLITE_USER_AUTHENTICATION */
10195
10196  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
10197    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
10198        sqlite3_libversion(), sqlite3_sourceid());
10199#if SQLITE_HAVE_ZLIB
10200    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
10201#endif
10202#define CTIMEOPT_VAL_(opt) #opt
10203#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
10204#if defined(__clang__) && defined(__clang_major__)
10205    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
10206                    CTIMEOPT_VAL(__clang_minor__) "."
10207                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
10208#elif defined(_MSC_VER)
10209    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
10210#elif defined(__GNUC__) && defined(__VERSION__)
10211    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
10212#endif
10213  }else
10214
10215  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
10216    const char *zDbName = nArg==2 ? azArg[1] : "main";
10217    sqlite3_vfs *pVfs = 0;
10218    if( p->db ){
10219      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
10220      if( pVfs ){
10221        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
10222        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10223        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10224        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10225      }
10226    }
10227  }else
10228
10229  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
10230    sqlite3_vfs *pVfs;
10231    sqlite3_vfs *pCurrent = 0;
10232    if( p->db ){
10233      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
10234    }
10235    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
10236      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
10237           pVfs==pCurrent ? "  <--- CURRENT" : "");
10238      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
10239      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
10240      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
10241      if( pVfs->pNext ){
10242        raw_printf(p->out, "-----------------------------------\n");
10243      }
10244    }
10245  }else
10246
10247  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
10248    const char *zDbName = nArg==2 ? azArg[1] : "main";
10249    char *zVfsName = 0;
10250    if( p->db ){
10251      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
10252      if( zVfsName ){
10253        utf8_printf(p->out, "%s\n", zVfsName);
10254        sqlite3_free(zVfsName);
10255      }
10256    }
10257  }else
10258
10259#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10260  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
10261    sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
10262  }else
10263#endif
10264
10265  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
10266    int j;
10267    assert( nArg<=ArraySize(azArg) );
10268    p->nWidth = nArg-1;
10269    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
10270    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
10271    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
10272    for(j=1; j<nArg; j++){
10273      p->colWidth[j-1] = (int)integerValue(azArg[j]);
10274    }
10275  }else
10276
10277  {
10278    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
10279      " \"%s\". Enter \".help\" for help\n", azArg[0]);
10280    rc = 1;
10281  }
10282
10283meta_command_exit:
10284  if( p->outCount ){
10285    p->outCount--;
10286    if( p->outCount==0 ) output_reset(p);
10287  }
10288  return rc;
10289}
10290
10291/*
10292** Return TRUE if a semicolon occurs anywhere in the first N characters
10293** of string z[].
10294*/
10295static int line_contains_semicolon(const char *z, int N){
10296  int i;
10297  for(i=0; i<N; i++){  if( z[i]==';' ) return 1; }
10298  return 0;
10299}
10300
10301/*
10302** Test to see if a line consists entirely of whitespace.
10303*/
10304static int _all_whitespace(const char *z){
10305  for(; *z; z++){
10306    if( IsSpace(z[0]) ) continue;
10307    if( *z=='/' && z[1]=='*' ){
10308      z += 2;
10309      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
10310      if( *z==0 ) return 0;
10311      z++;
10312      continue;
10313    }
10314    if( *z=='-' && z[1]=='-' ){
10315      z += 2;
10316      while( *z && *z!='\n' ){ z++; }
10317      if( *z==0 ) return 1;
10318      continue;
10319    }
10320    return 0;
10321  }
10322  return 1;
10323}
10324
10325/*
10326** Return TRUE if the line typed in is an SQL command terminator other
10327** than a semi-colon.  The SQL Server style "go" command is understood
10328** as is the Oracle "/".
10329*/
10330static int line_is_command_terminator(const char *zLine){
10331  while( IsSpace(zLine[0]) ){ zLine++; };
10332  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
10333    return 1;  /* Oracle */
10334  }
10335  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
10336         && _all_whitespace(&zLine[2]) ){
10337    return 1;  /* SQL Server */
10338  }
10339  return 0;
10340}
10341
10342/*
10343** We need a default sqlite3_complete() implementation to use in case
10344** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
10345** any arbitrary text is a complete SQL statement.  This is not very
10346** user-friendly, but it does seem to work.
10347*/
10348#ifdef SQLITE_OMIT_COMPLETE
10349#define sqlite3_complete(x) 1
10350#endif
10351
10352/*
10353** Return true if zSql is a complete SQL statement.  Return false if it
10354** ends in the middle of a string literal or C-style comment.
10355*/
10356static int line_is_complete(char *zSql, int nSql){
10357  int rc;
10358  if( zSql==0 ) return 1;
10359  zSql[nSql] = ';';
10360  zSql[nSql+1] = 0;
10361  rc = sqlite3_complete(zSql);
10362  zSql[nSql] = 0;
10363  return rc;
10364}
10365
10366/*
10367** Run a single line of SQL.  Return the number of errors.
10368*/
10369static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
10370  int rc;
10371  char *zErrMsg = 0;
10372
10373  open_db(p, 0);
10374  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
10375  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10376  BEGIN_TIMER;
10377  rc = shell_exec(p, zSql, &zErrMsg);
10378  END_TIMER;
10379  if( rc || zErrMsg ){
10380    char zPrefix[100];
10381    if( in!=0 || !stdin_is_interactive ){
10382      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
10383                       "Error: near line %d:", startline);
10384    }else{
10385      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
10386    }
10387    if( zErrMsg!=0 ){
10388      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
10389      sqlite3_free(zErrMsg);
10390      zErrMsg = 0;
10391    }else{
10392      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10393    }
10394    return 1;
10395  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10396    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
10397            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
10398  }
10399  return 0;
10400}
10401
10402
10403/*
10404** Read input from *in and process it.  If *in==0 then input
10405** is interactive - the user is typing it it.  Otherwise, input
10406** is coming from a file or device.  A prompt is issued and history
10407** is saved only if input is interactive.  An interrupt signal will
10408** cause this routine to exit immediately, unless input is interactive.
10409**
10410** Return the number of errors.
10411*/
10412static int process_input(ShellState *p){
10413  char *zLine = 0;          /* A single input line */
10414  char *zSql = 0;           /* Accumulated SQL text */
10415  int nLine;                /* Length of current line */
10416  int nSql = 0;             /* Bytes of zSql[] used */
10417  int nAlloc = 0;           /* Allocated zSql[] space */
10418  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
10419  int rc;                   /* Error code */
10420  int errCnt = 0;           /* Number of errors seen */
10421  int startline = 0;        /* Line number for start of current input */
10422
10423  p->lineno = 0;
10424  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
10425    fflush(p->out);
10426    zLine = one_input_line(p->in, zLine, nSql>0);
10427    if( zLine==0 ){
10428      /* End of input */
10429      if( p->in==0 && stdin_is_interactive ) printf("\n");
10430      break;
10431    }
10432    if( seenInterrupt ){
10433      if( p->in!=0 ) break;
10434      seenInterrupt = 0;
10435    }
10436    p->lineno++;
10437    if( nSql==0 && _all_whitespace(zLine) ){
10438      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10439      continue;
10440    }
10441    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
10442      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10443      if( zLine[0]=='.' ){
10444        rc = do_meta_command(zLine, p);
10445        if( rc==2 ){ /* exit requested */
10446          break;
10447        }else if( rc ){
10448          errCnt++;
10449        }
10450      }
10451      continue;
10452    }
10453    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10454      memcpy(zLine,";",2);
10455    }
10456    nLine = strlen30(zLine);
10457    if( nSql+nLine+2>=nAlloc ){
10458      nAlloc = nSql+nLine+100;
10459      zSql = realloc(zSql, nAlloc);
10460      if( zSql==0 ) shell_out_of_memory();
10461    }
10462    nSqlPrior = nSql;
10463    if( nSql==0 ){
10464      int i;
10465      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10466      assert( nAlloc>0 && zSql!=0 );
10467      memcpy(zSql, zLine+i, nLine+1-i);
10468      startline = p->lineno;
10469      nSql = nLine-i;
10470    }else{
10471      zSql[nSql++] = '\n';
10472      memcpy(zSql+nSql, zLine, nLine+1);
10473      nSql += nLine;
10474    }
10475    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10476                && sqlite3_complete(zSql) ){
10477      errCnt += runOneSqlLine(p, zSql, p->in, startline);
10478      nSql = 0;
10479      if( p->outCount ){
10480        output_reset(p);
10481        p->outCount = 0;
10482      }else{
10483        clearTempFile(p);
10484      }
10485    }else if( nSql && _all_whitespace(zSql) ){
10486      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10487      nSql = 0;
10488    }
10489  }
10490  if( nSql && !_all_whitespace(zSql) ){
10491    errCnt += runOneSqlLine(p, zSql, p->in, startline);
10492  }
10493  free(zSql);
10494  free(zLine);
10495  return errCnt>0;
10496}
10497
10498/*
10499** Return a pathname which is the user's home directory.  A
10500** 0 return indicates an error of some kind.
10501*/
10502static char *find_home_dir(int clearFlag){
10503  static char *home_dir = NULL;
10504  if( clearFlag ){
10505    free(home_dir);
10506    home_dir = 0;
10507    return 0;
10508  }
10509  if( home_dir ) return home_dir;
10510
10511#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10512     && !defined(__RTP__) && !defined(_WRS_KERNEL)
10513  {
10514    struct passwd *pwent;
10515    uid_t uid = getuid();
10516    if( (pwent=getpwuid(uid)) != NULL) {
10517      home_dir = pwent->pw_dir;
10518    }
10519  }
10520#endif
10521
10522#if defined(_WIN32_WCE)
10523  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10524   */
10525  home_dir = "/";
10526#else
10527
10528#if defined(_WIN32) || defined(WIN32)
10529  if (!home_dir) {
10530    home_dir = getenv("USERPROFILE");
10531  }
10532#endif
10533
10534  if (!home_dir) {
10535    home_dir = getenv("HOME");
10536  }
10537
10538#if defined(_WIN32) || defined(WIN32)
10539  if (!home_dir) {
10540    char *zDrive, *zPath;
10541    int n;
10542    zDrive = getenv("HOMEDRIVE");
10543    zPath = getenv("HOMEPATH");
10544    if( zDrive && zPath ){
10545      n = strlen30(zDrive) + strlen30(zPath) + 1;
10546      home_dir = malloc( n );
10547      if( home_dir==0 ) return 0;
10548      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10549      return home_dir;
10550    }
10551    home_dir = "c:\\";
10552  }
10553#endif
10554
10555#endif /* !_WIN32_WCE */
10556
10557  if( home_dir ){
10558    int n = strlen30(home_dir) + 1;
10559    char *z = malloc( n );
10560    if( z ) memcpy(z, home_dir, n);
10561    home_dir = z;
10562  }
10563
10564  return home_dir;
10565}
10566
10567/*
10568** Read input from the file given by sqliterc_override.  Or if that
10569** parameter is NULL, take input from ~/.sqliterc
10570**
10571** Returns the number of errors.
10572*/
10573static void process_sqliterc(
10574  ShellState *p,                  /* Configuration data */
10575  const char *sqliterc_override   /* Name of config file. NULL to use default */
10576){
10577  char *home_dir = NULL;
10578  const char *sqliterc = sqliterc_override;
10579  char *zBuf = 0;
10580  FILE *inSaved = p->in;
10581  int savedLineno = p->lineno;
10582
10583  if (sqliterc == NULL) {
10584    home_dir = find_home_dir(0);
10585    if( home_dir==0 ){
10586      raw_printf(stderr, "-- warning: cannot find home directory;"
10587                      " cannot read ~/.sqliterc\n");
10588      return;
10589    }
10590    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10591    sqliterc = zBuf;
10592  }
10593  p->in = fopen(sqliterc,"rb");
10594  if( p->in ){
10595    if( stdin_is_interactive ){
10596      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10597    }
10598    process_input(p);
10599    fclose(p->in);
10600  }
10601  p->in = inSaved;
10602  p->lineno = savedLineno;
10603  sqlite3_free(zBuf);
10604}
10605
10606/*
10607** Show available command line options
10608*/
10609static const char zOptions[] =
10610#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10611  "   -A ARGS...           run \".archive ARGS\" and exit\n"
10612#endif
10613  "   -append              append the database to the end of the file\n"
10614  "   -ascii               set output mode to 'ascii'\n"
10615  "   -bail                stop after hitting an error\n"
10616  "   -batch               force batch I/O\n"
10617  "   -box                 set output mode to 'box'\n"
10618  "   -column              set output mode to 'column'\n"
10619  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
10620  "   -csv                 set output mode to 'csv'\n"
10621#if defined(SQLITE_ENABLE_DESERIALIZE)
10622  "   -deserialize         open the database using sqlite3_deserialize()\n"
10623#endif
10624  "   -echo                print commands before execution\n"
10625  "   -init FILENAME       read/process named file\n"
10626  "   -[no]header          turn headers on or off\n"
10627#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10628  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
10629#endif
10630  "   -help                show this message\n"
10631  "   -html                set output mode to HTML\n"
10632  "   -interactive         force interactive I/O\n"
10633  "   -json                set output mode to 'json'\n"
10634  "   -line                set output mode to 'line'\n"
10635  "   -list                set output mode to 'list'\n"
10636  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
10637  "   -markdown            set output mode to 'markdown'\n"
10638#if defined(SQLITE_ENABLE_DESERIALIZE)
10639  "   -maxsize N           maximum size for a --deserialize database\n"
10640#endif
10641  "   -memtrace            trace all memory allocations and deallocations\n"
10642  "   -mmap N              default mmap size set to N\n"
10643#ifdef SQLITE_ENABLE_MULTIPLEX
10644  "   -multiplex           enable the multiplexor VFS\n"
10645#endif
10646  "   -newline SEP         set output row separator. Default: '\\n'\n"
10647  "   -nofollow            refuse to open symbolic links to database files\n"
10648  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
10649  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
10650  "   -quote               set output mode to 'quote'\n"
10651  "   -readonly            open the database read-only\n"
10652  "   -separator SEP       set output column separator. Default: '|'\n"
10653#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10654  "   -sorterref SIZE      sorter references threshold size\n"
10655#endif
10656  "   -stats               print memory stats before each finalize\n"
10657  "   -table               set output mode to 'table'\n"
10658  "   -version             show SQLite version\n"
10659  "   -vfs NAME            use NAME as the default VFS\n"
10660#ifdef SQLITE_ENABLE_VFSTRACE
10661  "   -vfstrace            enable tracing of all VFS calls\n"
10662#endif
10663#ifdef SQLITE_HAVE_ZLIB
10664  "   -zip                 open the file as a ZIP Archive\n"
10665#endif
10666;
10667static void usage(int showDetail){
10668  utf8_printf(stderr,
10669      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10670      "FILENAME is the name of an SQLite database. A new database is created\n"
10671      "if the file does not previously exist.\n", Argv0);
10672  if( showDetail ){
10673    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10674  }else{
10675    raw_printf(stderr, "Use the -help option for additional information\n");
10676  }
10677  exit(1);
10678}
10679
10680/*
10681** Internal check:  Verify that the SQLite is uninitialized.  Print a
10682** error message if it is initialized.
10683*/
10684static void verify_uninitialized(void){
10685  if( sqlite3_config(-1)==SQLITE_MISUSE ){
10686    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
10687                        " initialization.\n");
10688  }
10689}
10690
10691/*
10692** Initialize the state information in data
10693*/
10694static void main_init(ShellState *data) {
10695  memset(data, 0, sizeof(*data));
10696  data->normalMode = data->cMode = data->mode = MODE_List;
10697  data->autoExplain = 1;
10698  memcpy(data->colSeparator,SEP_Column, 2);
10699  memcpy(data->rowSeparator,SEP_Row, 2);
10700  data->showHeader = 0;
10701  data->shellFlgs = SHFLG_Lookaside;
10702  verify_uninitialized();
10703  sqlite3_config(SQLITE_CONFIG_URI, 1);
10704  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10705  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10706  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10707  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
10708}
10709
10710/*
10711** Output text to the console in a font that attracts extra attention.
10712*/
10713#ifdef _WIN32
10714static void printBold(const char *zText){
10715#if !SQLITE_OS_WINRT
10716  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10717  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10718  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10719  SetConsoleTextAttribute(out,
10720         FOREGROUND_RED|FOREGROUND_INTENSITY
10721  );
10722#endif
10723  printf("%s", zText);
10724#if !SQLITE_OS_WINRT
10725  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
10726#endif
10727}
10728#else
10729static void printBold(const char *zText){
10730  printf("\033[1m%s\033[0m", zText);
10731}
10732#endif
10733
10734/*
10735** Get the argument to an --option.  Throw an error and die if no argument
10736** is available.
10737*/
10738static char *cmdline_option_value(int argc, char **argv, int i){
10739  if( i==argc ){
10740    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10741            argv[0], argv[argc-1]);
10742    exit(1);
10743  }
10744  return argv[i];
10745}
10746
10747#ifndef SQLITE_SHELL_IS_UTF8
10748#  if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
10749#    define SQLITE_SHELL_IS_UTF8          (0)
10750#  else
10751#    define SQLITE_SHELL_IS_UTF8          (1)
10752#  endif
10753#endif
10754
10755#if SQLITE_SHELL_IS_UTF8
10756int SQLITE_CDECL main(int argc, char **argv){
10757#else
10758int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10759  char **argv;
10760#endif
10761  char *zErrMsg = 0;
10762  ShellState data;
10763  const char *zInitFile = 0;
10764  int i;
10765  int rc = 0;
10766  int warnInmemoryDb = 0;
10767  int readStdin = 1;
10768  int nCmd = 0;
10769  char **azCmd = 0;
10770  const char *zVfs = 0;           /* Value of -vfs command-line option */
10771#if !SQLITE_SHELL_IS_UTF8
10772  char **argvToFree = 0;
10773  int argcToFree = 0;
10774#endif
10775
10776  setBinaryMode(stdin, 0);
10777  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10778  stdin_is_interactive = isatty(0);
10779  stdout_is_console = isatty(1);
10780
10781#ifdef SQLITE_DEBUG
10782  registerOomSimulator();
10783#endif
10784
10785#if !defined(_WIN32_WCE)
10786  if( getenv("SQLITE_DEBUG_BREAK") ){
10787    if( isatty(0) && isatty(2) ){
10788      fprintf(stderr,
10789          "attach debugger to process %d and press any key to continue.\n",
10790          GETPID());
10791      fgetc(stdin);
10792    }else{
10793#if defined(_WIN32) || defined(WIN32)
10794#if SQLITE_OS_WINRT
10795      __debugbreak();
10796#else
10797      DebugBreak();
10798#endif
10799#elif defined(SIGTRAP)
10800      raise(SIGTRAP);
10801#endif
10802    }
10803  }
10804#endif
10805
10806#if USE_SYSTEM_SQLITE+0!=1
10807  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
10808    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10809            sqlite3_sourceid(), SQLITE_SOURCE_ID);
10810    exit(1);
10811  }
10812#endif
10813  main_init(&data);
10814
10815  /* On Windows, we must translate command-line arguments into UTF-8.
10816  ** The SQLite memory allocator subsystem has to be enabled in order to
10817  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
10818  ** subsequent sqlite3_config() calls will work.  So copy all results into
10819  ** memory that does not come from the SQLite memory allocator.
10820  */
10821#if !SQLITE_SHELL_IS_UTF8
10822  sqlite3_initialize();
10823  argvToFree = malloc(sizeof(argv[0])*argc*2);
10824  argcToFree = argc;
10825  argv = argvToFree + argc;
10826  if( argv==0 ) shell_out_of_memory();
10827  for(i=0; i<argc; i++){
10828    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10829    int n;
10830    if( z==0 ) shell_out_of_memory();
10831    n = (int)strlen(z);
10832    argv[i] = malloc( n+1 );
10833    if( argv[i]==0 ) shell_out_of_memory();
10834    memcpy(argv[i], z, n+1);
10835    argvToFree[i] = argv[i];
10836    sqlite3_free(z);
10837  }
10838  sqlite3_shutdown();
10839#endif
10840
10841  assert( argc>=1 && argv && argv[0] );
10842  Argv0 = argv[0];
10843
10844  /* Make sure we have a valid signal handler early, before anything
10845  ** else is done.
10846  */
10847#ifdef SIGINT
10848  signal(SIGINT, interrupt_handler);
10849#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10850  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
10851#endif
10852
10853#ifdef SQLITE_SHELL_DBNAME_PROC
10854  {
10855    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10856    ** of a C-function that will provide the name of the database file.  Use
10857    ** this compile-time option to embed this shell program in larger
10858    ** applications. */
10859    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10860    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10861    warnInmemoryDb = 0;
10862  }
10863#endif
10864
10865  /* Do an initial pass through the command-line argument to locate
10866  ** the name of the database file, the name of the initialization file,
10867  ** the size of the alternative malloc heap,
10868  ** and the first command to execute.
10869  */
10870  verify_uninitialized();
10871  for(i=1; i<argc; i++){
10872    char *z;
10873    z = argv[i];
10874    if( z[0]!='-' ){
10875      if( data.zDbFilename==0 ){
10876        data.zDbFilename = z;
10877      }else{
10878        /* Excesss arguments are interpreted as SQL (or dot-commands) and
10879        ** mean that nothing is read from stdin */
10880        readStdin = 0;
10881        nCmd++;
10882        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
10883        if( azCmd==0 ) shell_out_of_memory();
10884        azCmd[nCmd-1] = z;
10885      }
10886    }
10887    if( z[1]=='-' ) z++;
10888    if( strcmp(z,"-separator")==0
10889     || strcmp(z,"-nullvalue")==0
10890     || strcmp(z,"-newline")==0
10891     || strcmp(z,"-cmd")==0
10892    ){
10893      (void)cmdline_option_value(argc, argv, ++i);
10894    }else if( strcmp(z,"-init")==0 ){
10895      zInitFile = cmdline_option_value(argc, argv, ++i);
10896    }else if( strcmp(z,"-batch")==0 ){
10897      /* Need to check for batch mode here to so we can avoid printing
10898      ** informational messages (like from process_sqliterc) before
10899      ** we do the actual processing of arguments later in a second pass.
10900      */
10901      stdin_is_interactive = 0;
10902    }else if( strcmp(z,"-heap")==0 ){
10903#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10904      const char *zSize;
10905      sqlite3_int64 szHeap;
10906
10907      zSize = cmdline_option_value(argc, argv, ++i);
10908      szHeap = integerValue(zSize);
10909      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
10910      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
10911#else
10912      (void)cmdline_option_value(argc, argv, ++i);
10913#endif
10914    }else if( strcmp(z,"-pagecache")==0 ){
10915      int n, sz;
10916      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10917      if( sz>70000 ) sz = 70000;
10918      if( sz<0 ) sz = 0;
10919      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10920      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
10921                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
10922      data.shellFlgs |= SHFLG_Pagecache;
10923    }else if( strcmp(z,"-lookaside")==0 ){
10924      int n, sz;
10925      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10926      if( sz<0 ) sz = 0;
10927      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10928      if( n<0 ) n = 0;
10929      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
10930      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
10931#ifdef SQLITE_ENABLE_VFSTRACE
10932    }else if( strcmp(z,"-vfstrace")==0 ){
10933      extern int vfstrace_register(
10934         const char *zTraceName,
10935         const char *zOldVfsName,
10936         int (*xOut)(const char*,void*),
10937         void *pOutArg,
10938         int makeDefault
10939      );
10940      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
10941#endif
10942#ifdef SQLITE_ENABLE_MULTIPLEX
10943    }else if( strcmp(z,"-multiplex")==0 ){
10944      extern int sqlite3_multiple_initialize(const char*,int);
10945      sqlite3_multiplex_initialize(0, 1);
10946#endif
10947    }else if( strcmp(z,"-mmap")==0 ){
10948      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10949      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
10950#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10951    }else if( strcmp(z,"-sorterref")==0 ){
10952      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10953      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
10954#endif
10955    }else if( strcmp(z,"-vfs")==0 ){
10956      zVfs = cmdline_option_value(argc, argv, ++i);
10957#ifdef SQLITE_HAVE_ZLIB
10958    }else if( strcmp(z,"-zip")==0 ){
10959      data.openMode = SHELL_OPEN_ZIPFILE;
10960#endif
10961    }else if( strcmp(z,"-append")==0 ){
10962      data.openMode = SHELL_OPEN_APPENDVFS;
10963#ifdef SQLITE_ENABLE_DESERIALIZE
10964    }else if( strcmp(z,"-deserialize")==0 ){
10965      data.openMode = SHELL_OPEN_DESERIALIZE;
10966    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10967      data.szMax = integerValue(argv[++i]);
10968#endif
10969    }else if( strcmp(z,"-readonly")==0 ){
10970      data.openMode = SHELL_OPEN_READONLY;
10971    }else if( strcmp(z,"-nofollow")==0 ){
10972      data.openFlags = SQLITE_OPEN_NOFOLLOW;
10973#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
10974    }else if( strncmp(z, "-A",2)==0 ){
10975      /* All remaining command-line arguments are passed to the ".archive"
10976      ** command, so ignore them */
10977      break;
10978#endif
10979    }else if( strcmp(z, "-memtrace")==0 ){
10980      sqlite3MemTraceActivate(stderr);
10981    }
10982  }
10983  verify_uninitialized();
10984
10985
10986#ifdef SQLITE_SHELL_INIT_PROC
10987  {
10988    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
10989    ** of a C-function that will perform initialization actions on SQLite that
10990    ** occur just before or after sqlite3_initialize(). Use this compile-time
10991    ** option to embed this shell program in larger applications. */
10992    extern void SQLITE_SHELL_INIT_PROC(void);
10993    SQLITE_SHELL_INIT_PROC();
10994  }
10995#else
10996  /* All the sqlite3_config() calls have now been made. So it is safe
10997  ** to call sqlite3_initialize() and process any command line -vfs option. */
10998  sqlite3_initialize();
10999#endif
11000
11001  if( zVfs ){
11002    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
11003    if( pVfs ){
11004      sqlite3_vfs_register(pVfs, 1);
11005    }else{
11006      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
11007      exit(1);
11008    }
11009  }
11010
11011  if( data.zDbFilename==0 ){
11012#ifndef SQLITE_OMIT_MEMORYDB
11013    data.zDbFilename = ":memory:";
11014    warnInmemoryDb = argc==1;
11015#else
11016    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
11017    return 1;
11018#endif
11019  }
11020  data.out = stdout;
11021  sqlite3_appendvfs_init(0,0,0);
11022
11023  /* Go ahead and open the database file if it already exists.  If the
11024  ** file does not exist, delay opening it.  This prevents empty database
11025  ** files from being created if a user mistypes the database name argument
11026  ** to the sqlite command-line tool.
11027  */
11028  if( access(data.zDbFilename, 0)==0 ){
11029    open_db(&data, 0);
11030  }
11031
11032  /* Process the initialization file if there is one.  If no -init option
11033  ** is given on the command line, look for a file named ~/.sqliterc and
11034  ** try to process it.
11035  */
11036  process_sqliterc(&data,zInitFile);
11037
11038  /* Make a second pass through the command-line argument and set
11039  ** options.  This second pass is delayed until after the initialization
11040  ** file is processed so that the command-line arguments will override
11041  ** settings in the initialization file.
11042  */
11043  for(i=1; i<argc; i++){
11044    char *z = argv[i];
11045    if( z[0]!='-' ) continue;
11046    if( z[1]=='-' ){ z++; }
11047    if( strcmp(z,"-init")==0 ){
11048      i++;
11049    }else if( strcmp(z,"-html")==0 ){
11050      data.mode = MODE_Html;
11051    }else if( strcmp(z,"-list")==0 ){
11052      data.mode = MODE_List;
11053    }else if( strcmp(z,"-quote")==0 ){
11054      data.mode = MODE_Quote;
11055      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
11056      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
11057    }else if( strcmp(z,"-line")==0 ){
11058      data.mode = MODE_Line;
11059    }else if( strcmp(z,"-column")==0 ){
11060      data.mode = MODE_Column;
11061    }else if( strcmp(z,"-json")==0 ){
11062      data.mode = MODE_Json;
11063    }else if( strcmp(z,"-markdown")==0 ){
11064      data.mode = MODE_Markdown;
11065    }else if( strcmp(z,"-table")==0 ){
11066      data.mode = MODE_Table;
11067    }else if( strcmp(z,"-box")==0 ){
11068      data.mode = MODE_Box;
11069    }else if( strcmp(z,"-csv")==0 ){
11070      data.mode = MODE_Csv;
11071      memcpy(data.colSeparator,",",2);
11072#ifdef SQLITE_HAVE_ZLIB
11073    }else if( strcmp(z,"-zip")==0 ){
11074      data.openMode = SHELL_OPEN_ZIPFILE;
11075#endif
11076    }else if( strcmp(z,"-append")==0 ){
11077      data.openMode = SHELL_OPEN_APPENDVFS;
11078#ifdef SQLITE_ENABLE_DESERIALIZE
11079    }else if( strcmp(z,"-deserialize")==0 ){
11080      data.openMode = SHELL_OPEN_DESERIALIZE;
11081    }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
11082      data.szMax = integerValue(argv[++i]);
11083#endif
11084    }else if( strcmp(z,"-readonly")==0 ){
11085      data.openMode = SHELL_OPEN_READONLY;
11086    }else if( strcmp(z,"-nofollow")==0 ){
11087      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
11088    }else if( strcmp(z,"-ascii")==0 ){
11089      data.mode = MODE_Ascii;
11090      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11091                       SEP_Unit);
11092      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11093                       SEP_Record);
11094    }else if( strcmp(z,"-separator")==0 ){
11095      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
11096                       "%s",cmdline_option_value(argc,argv,++i));
11097    }else if( strcmp(z,"-newline")==0 ){
11098      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
11099                       "%s",cmdline_option_value(argc,argv,++i));
11100    }else if( strcmp(z,"-nullvalue")==0 ){
11101      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
11102                       "%s",cmdline_option_value(argc,argv,++i));
11103    }else if( strcmp(z,"-header")==0 ){
11104      data.showHeader = 1;
11105    }else if( strcmp(z,"-noheader")==0 ){
11106      data.showHeader = 0;
11107    }else if( strcmp(z,"-echo")==0 ){
11108      ShellSetFlag(&data, SHFLG_Echo);
11109    }else if( strcmp(z,"-eqp")==0 ){
11110      data.autoEQP = AUTOEQP_on;
11111    }else if( strcmp(z,"-eqpfull")==0 ){
11112      data.autoEQP = AUTOEQP_full;
11113    }else if( strcmp(z,"-stats")==0 ){
11114      data.statsOn = 1;
11115    }else if( strcmp(z,"-scanstats")==0 ){
11116      data.scanstatsOn = 1;
11117    }else if( strcmp(z,"-backslash")==0 ){
11118      /* Undocumented command-line option: -backslash
11119      ** Causes C-style backslash escapes to be evaluated in SQL statements
11120      ** prior to sending the SQL into SQLite.  Useful for injecting
11121      ** crazy bytes in the middle of SQL statements for testing and debugging.
11122      */
11123      ShellSetFlag(&data, SHFLG_Backslash);
11124    }else if( strcmp(z,"-bail")==0 ){
11125      bail_on_error = 1;
11126    }else if( strcmp(z,"-version")==0 ){
11127      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
11128      return 0;
11129    }else if( strcmp(z,"-interactive")==0 ){
11130      stdin_is_interactive = 1;
11131    }else if( strcmp(z,"-batch")==0 ){
11132      stdin_is_interactive = 0;
11133    }else if( strcmp(z,"-heap")==0 ){
11134      i++;
11135    }else if( strcmp(z,"-pagecache")==0 ){
11136      i+=2;
11137    }else if( strcmp(z,"-lookaside")==0 ){
11138      i+=2;
11139    }else if( strcmp(z,"-mmap")==0 ){
11140      i++;
11141    }else if( strcmp(z,"-memtrace")==0 ){
11142      i++;
11143#ifdef SQLITE_ENABLE_SORTER_REFERENCES
11144    }else if( strcmp(z,"-sorterref")==0 ){
11145      i++;
11146#endif
11147    }else if( strcmp(z,"-vfs")==0 ){
11148      i++;
11149#ifdef SQLITE_ENABLE_VFSTRACE
11150    }else if( strcmp(z,"-vfstrace")==0 ){
11151      i++;
11152#endif
11153#ifdef SQLITE_ENABLE_MULTIPLEX
11154    }else if( strcmp(z,"-multiplex")==0 ){
11155      i++;
11156#endif
11157    }else if( strcmp(z,"-help")==0 ){
11158      usage(1);
11159    }else if( strcmp(z,"-cmd")==0 ){
11160      /* Run commands that follow -cmd first and separately from commands
11161      ** that simply appear on the command-line.  This seems goofy.  It would
11162      ** be better if all commands ran in the order that they appear.  But
11163      ** we retain the goofy behavior for historical compatibility. */
11164      if( i==argc-1 ) break;
11165      z = cmdline_option_value(argc,argv,++i);
11166      if( z[0]=='.' ){
11167        rc = do_meta_command(z, &data);
11168        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
11169      }else{
11170        open_db(&data, 0);
11171        rc = shell_exec(&data, z, &zErrMsg);
11172        if( zErrMsg!=0 ){
11173          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11174          if( bail_on_error ) return rc!=0 ? rc : 1;
11175        }else if( rc!=0 ){
11176          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
11177          if( bail_on_error ) return rc;
11178        }
11179      }
11180#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11181    }else if( strncmp(z, "-A", 2)==0 ){
11182      if( nCmd>0 ){
11183        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
11184                            " with \"%s\"\n", z);
11185        return 1;
11186      }
11187      open_db(&data, OPEN_DB_ZIPFILE);
11188      if( z[2] ){
11189        argv[i] = &z[2];
11190        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
11191      }else{
11192        arDotCommand(&data, 1, argv+i, argc-i);
11193      }
11194      readStdin = 0;
11195      break;
11196#endif
11197    }else{
11198      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
11199      raw_printf(stderr,"Use -help for a list of options.\n");
11200      return 1;
11201    }
11202    data.cMode = data.mode;
11203  }
11204
11205  if( !readStdin ){
11206    /* Run all arguments that do not begin with '-' as if they were separate
11207    ** command-line inputs, except for the argToSkip argument which contains
11208    ** the database filename.
11209    */
11210    for(i=0; i<nCmd; i++){
11211      if( azCmd[i][0]=='.' ){
11212        rc = do_meta_command(azCmd[i], &data);
11213        if( rc ) return rc==2 ? 0 : rc;
11214      }else{
11215        open_db(&data, 0);
11216        rc = shell_exec(&data, azCmd[i], &zErrMsg);
11217        if( zErrMsg!=0 ){
11218          utf8_printf(stderr,"Error: %s\n", zErrMsg);
11219          return rc!=0 ? rc : 1;
11220        }else if( rc!=0 ){
11221          utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
11222          return rc;
11223        }
11224      }
11225    }
11226    free(azCmd);
11227  }else{
11228    /* Run commands received from standard input
11229    */
11230    if( stdin_is_interactive ){
11231      char *zHome;
11232      char *zHistory;
11233      int nHistory;
11234      printf(
11235        "SQLite version %s %.19s\n" /*extra-version-info*/
11236        "Enter \".help\" for usage hints.\n",
11237        sqlite3_libversion(), sqlite3_sourceid()
11238      );
11239      if( warnInmemoryDb ){
11240        printf("Connected to a ");
11241        printBold("transient in-memory database");
11242        printf(".\nUse \".open FILENAME\" to reopen on a "
11243               "persistent database.\n");
11244      }
11245      zHistory = getenv("SQLITE_HISTORY");
11246      if( zHistory ){
11247        zHistory = strdup(zHistory);
11248      }else if( (zHome = find_home_dir(0))!=0 ){
11249        nHistory = strlen30(zHome) + 20;
11250        if( (zHistory = malloc(nHistory))!=0 ){
11251          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
11252        }
11253      }
11254      if( zHistory ){ shell_read_history(zHistory); }
11255#if HAVE_READLINE || HAVE_EDITLINE
11256      rl_attempted_completion_function = readline_completion;
11257#elif HAVE_LINENOISE
11258      linenoiseSetCompletionCallback(linenoise_completion);
11259#endif
11260      data.in = 0;
11261      rc = process_input(&data);
11262      if( zHistory ){
11263        shell_stifle_history(2000);
11264        shell_write_history(zHistory);
11265        free(zHistory);
11266      }
11267    }else{
11268      data.in = stdin;
11269      rc = process_input(&data);
11270    }
11271  }
11272  set_table_name(&data, 0);
11273  if( data.db ){
11274    session_close_all(&data);
11275    close_db(data.db);
11276  }
11277  sqlite3_free(data.zFreeOnClose);
11278  find_home_dir(1);
11279  output_reset(&data);
11280  data.doXdgOpen = 0;
11281  clearTempFile(&data);
11282#if !SQLITE_SHELL_IS_UTF8
11283  for(i=0; i<argcToFree; i++) free(argvToFree[i]);
11284  free(argvToFree);
11285#endif
11286  free(data.colWidth);
11287  /* Clear the global data structure so that valgrind will detect memory
11288  ** leaks */
11289  memset(&data, 0, sizeof(data));
11290  return rc;
11291}
11292